blob: 4dcb90b2cb7de9e3dcf282e502c0a92fdb26fc05 [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
Michal Vaskoba417ac2020-08-06 14:48:20 +02001483range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype,
1484 int first, int length_restr, 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
Michal Vaskoba417ac2020-08-06 14:48:20 +02001646lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr,
1647 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
Radek Krejci19a96102018-11-15 13:38:09 +01001648{
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;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003374 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003375 LY_ERR ret = LY_SUCCESS;
3376
Radek Krejcife909632019-02-12 15:34:42 +01003377 if (cont_p->presence) {
Michal Vaskoba417ac2020-08-06 14:48:20 +02003378 /* explicit presence */
Radek Krejcife909632019-02-12 15:34:42 +01003379 cont->flags |= LYS_PRESENCE;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003380 } else if (cont_p->musts) {
3381 /* container with a must condition */
3382 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a \"must\" condition.", cont_p->name);
3383 cont->flags |= LYS_PRESENCE;
3384 } else if (cont_p->parent) {
3385 if (cont_p->parent->nodetype == LYS_CHOICE) {
3386 /* container is an implicit case, so its existence decides the existence of the whole case */
3387 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it is an implicit case of choice \"%s\".",
3388 cont_p->name, cont_p->parent->name);
3389 cont->flags |= LYS_PRESENCE;
3390 } else if ((cont_p->parent->nodetype == LYS_CASE)
3391 && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) {
3392 /* container is the only node in a case, so its existence decides the existence of the whole case */
3393 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it is the only data node of case \"%s\".",
3394 cont_p->name, cont_p->parent->name);
3395 cont->flags |= LYS_PRESENCE;
3396 }
Radek Krejcife909632019-02-12 15:34:42 +01003397 }
3398
Michal Vaskoba417ac2020-08-06 14:48:20 +02003399 /* more cases when the container has meaning but is kept NP for convenience:
3400 * - when condition
3401 * - direct child action/notification
3402 */
3403
Radek Krejci19a96102018-11-15 13:38:09 +01003404 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003405 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003406 }
3407
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003408 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003409 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3410 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003411 ly_set_add(&ctx->xpath, cont, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003412 }
Radek Krejciec4da802019-05-02 13:02:41 +02003413 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3414 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003415
3416done:
3417 return ret;
3418}
3419
Radek Krejci33f72892019-02-21 10:36:58 +01003420/*
3421 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3422 * @param[in] ctx Compile context.
3423 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3424 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003425 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3426 * @return LY_ERR value.
3427 */
3428static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003429lys_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 +01003430{
Radek Krejci33f72892019-02-21 10:36:58 +01003431 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3432
Radek Krejciec4da802019-05-02 13:02:41 +02003433 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 +01003434 leaf->units ? NULL : &leaf->units));
3435 if (leaf->nodetype == LYS_LEAFLIST) {
3436 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003437 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3438 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003439 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003440 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3441 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3442 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3443 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003444 LY_ARRAY_INCREMENT(llist->dflts);
3445 }
3446 } else {
3447 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003448 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003449 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3450 leaf->dflt->realtype = leaf->type->dflt->realtype;
3451 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3452 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003453 }
3454 }
3455 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3456 /* 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 +02003457 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003458 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003459 LY_ARRAY_COUNT_TYPE u;
Radek Krejci33f72892019-02-21 10:36:58 +01003460 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3461 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3462 /* 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 +02003463 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003464 }
3465 }
3466 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003467 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3468 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3469 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3470 return LY_EVALID;
3471 }
3472 }
3473
Radek Krejci33f72892019-02-21 10:36:58 +01003474 return LY_SUCCESS;
3475}
3476
Radek Krejcia3045382018-11-22 14:30:31 +01003477/**
3478 * @brief Compile parsed leaf node information.
3479 * @param[in] ctx Compile context
3480 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003481 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3482 * is enriched with the leaf-specific information.
3483 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3484 */
Radek Krejci19a96102018-11-15 13:38:09 +01003485static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003486lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003487{
3488 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3489 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Michal Vasko7c8439f2020-08-05 13:25:19 +02003490 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003491 LY_ERR ret = LY_SUCCESS;
3492
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003493 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003494 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3495 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003496 ly_set_add(&ctx->xpath, leaf, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003497 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003498 if (leaf_p->units) {
3499 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3500 leaf->flags |= LYS_SET_UNITS;
3501 }
Radek Krejcia1911222019-07-22 17:24:50 +02003502
3503 /* the dflt member is just filled to avoid getting the default value from the type */
3504 leaf->dflt = (void*)leaf_p->dflt;
3505 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003506 if (ret) {
3507 leaf->dflt = NULL;
3508 return ret;
3509 }
Radek Krejcia1911222019-07-22 17:24:50 +02003510
Radek Krejciccd20f12019-02-15 14:12:27 +01003511 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003512 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003513 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003514 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3515 leaf->dflt->realtype = leaf->type;
3516 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3517 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003518 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003519 leaf->dflt->realtype->refcount++;
3520 if (err) {
3521 ly_err_print(err);
3522 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3523 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3524 ly_err_free(err);
3525 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003526 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003527 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003528 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003529
3530 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003531 ret = LY_SUCCESS;
3532 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003533 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003534 leaf->flags |= LYS_SET_DFLT;
3535 }
Radek Krejci43699232018-11-23 14:59:46 +01003536
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003537
Radek Krejci19a96102018-11-15 13:38:09 +01003538done:
3539 return ret;
3540}
3541
Radek Krejcia3045382018-11-22 14:30:31 +01003542/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003543 * @brief Compile parsed leaf-list node information.
3544 * @param[in] ctx Compile context
3545 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003546 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3547 * is enriched with the leaf-list-specific information.
3548 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3549 */
3550static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003551lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003552{
3553 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3554 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003555 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003556 LY_ERR ret = LY_SUCCESS;
3557
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003558 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003559 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3560 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003561 ly_set_add(&ctx->xpath, llist, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003562 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003563 if (llist_p->units) {
3564 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3565 llist->flags |= LYS_SET_UNITS;
3566 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003567
Radek Krejcia1911222019-07-22 17:24:50 +02003568 /* the dflts member is just filled to avoid getting the default value from the type */
3569 llist->dflts = (void*)llist_p->dflts;
3570 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Michal Vasko6a044b22020-01-15 12:25:39 +01003571 if (ret != LY_SUCCESS) {
3572 /* make sure the defaults are freed correctly */
3573 if (llist_p->dflts) {
3574 llist->dflts = NULL;
3575 }
3576 return ret;
3577 }
3578
Radek Krejci0e5d8382018-11-28 16:37:53 +01003579 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003580 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003581 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
3582 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003583 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003584 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003585 LY_ARRAY_INCREMENT(llist->dflts_mods);
3586 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003587 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003588 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3589 llist->dflts[u]->realtype = llist->type;
3590 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3591 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003592 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003593 llist->dflts[u]->realtype->refcount++;
3594 if (err) {
3595 ly_err_print(err);
3596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3597 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3598 ly_err_free(err);
3599 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003600 if (ret == LY_EINCOMPLETE) {
3601 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003602 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003603
3604 /* but in general result is so far ok */
3605 ret = LY_SUCCESS;
3606 }
Radek Krejcia1911222019-07-22 17:24:50 +02003607 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003608 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003609 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003610 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003611 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02003612 /* configuration data values must be unique - so check the default values */
3613 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003614 for (v = u + 1; v < LY_ARRAY_COUNT(llist->dflts); ++v) {
Radek Krejcia1911222019-07-22 17:24:50 +02003615 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3616 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003617 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 +02003618 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3619 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3620 if (dynamic) {
3621 free((char*)val);
3622 }
3623 return LY_EVALID;
3624 }
3625 }
3626 }
3627 }
3628
3629 /* 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 +01003630
3631 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003632 if (llist->min) {
3633 llist->flags |= LYS_MAND_TRUE;
3634 }
Radek Krejcib7408632018-11-28 17:12:11 +01003635 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003636
Radek Krejci0e5d8382018-11-28 16:37:53 +01003637done:
3638 return ret;
3639}
3640
3641/**
Radek Krejci7af64242019-02-18 13:07:53 +01003642 * @brief Compile information about list's uniques.
3643 * @param[in] ctx Compile context.
3644 * @param[in] context_module Module where the prefixes are going to be resolved.
3645 * @param[in] uniques Sized array list of unique statements.
3646 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3647 * @return LY_ERR value.
3648 */
3649static LY_ERR
3650lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3651{
3652 LY_ERR ret = LY_SUCCESS;
3653 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003654 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003655 const char *keystr, *delim;
3656 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003657 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7af64242019-02-18 13:07:53 +01003658 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003659 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003660
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003661 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003662 config = -1;
3663 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3664 keystr = uniques[v];
3665 while (keystr) {
3666 delim = strpbrk(keystr, " \t\n");
3667 if (delim) {
3668 len = delim - keystr;
3669 while (isspace(*delim)) {
3670 ++delim;
3671 }
3672 } else {
3673 len = strlen(keystr);
3674 }
3675
3676 /* unique node must be present */
3677 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003678 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3679 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003680 if (ret != LY_SUCCESS) {
3681 if (ret == LY_EDENIED) {
3682 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003683 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003684 len, keystr, lys_nodetype2str((*key)->nodetype));
3685 }
3686 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003687 } else if (flags) {
3688 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3689 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003690 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003691 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003692 }
3693
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003694
Radek Krejci7af64242019-02-18 13:07:53 +01003695 /* all referenced leafs must be of the same config type */
3696 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003698 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003699 return LY_EVALID;
3700 } else if ((*key)->flags & LYS_CONFIG_W) {
3701 config = 1;
3702 } else { /* LYS_CONFIG_R */
3703 config = 0;
3704 }
3705
Michal Vasko14654712020-02-06 08:35:21 +01003706 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3707 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3708 if (parent->nodetype == LYS_LIST) {
3709 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3710 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3711 return LY_EVALID;
3712 }
3713 }
3714
Radek Krejci7af64242019-02-18 13:07:53 +01003715 /* check status */
3716 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3717 (*key)->flags, (*key)->module, (*key)->name));
3718
3719 /* mark leaf as unique */
3720 (*key)->flags |= LYS_UNIQUE;
3721
3722 /* next unique value in line */
3723 keystr = delim;
3724 }
3725 /* next unique definition */
3726 }
3727
3728 return LY_SUCCESS;
3729}
3730
3731/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003732 * @brief Compile parsed list node information.
3733 * @param[in] ctx Compile context
3734 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003735 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3736 * is enriched with the list-specific information.
3737 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3738 */
3739static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003740lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003741{
3742 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3743 struct lysc_node_list *list = (struct lysc_node_list*)node;
3744 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003745 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003746 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003747 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003748 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003749 LY_ERR ret = LY_SUCCESS;
3750
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003751 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003752 if (list->min) {
3753 list->flags |= LYS_MAND_TRUE;
3754 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003755 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3756
3757 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003758 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003759 }
3760
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003761 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003762 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3763 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003764 ly_set_add(&ctx->xpath, list, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003765 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003766
3767 /* keys */
3768 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3769 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3770 return LY_EVALID;
3771 }
3772
3773 /* find all the keys (must be direct children) */
3774 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003775 if (!keystr) {
3776 /* keyless list */
3777 list->flags |= LYS_KEYLESS;
3778 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003779 while (keystr) {
3780 delim = strpbrk(keystr, " \t\n");
3781 if (delim) {
3782 len = delim - keystr;
3783 while (isspace(*delim)) {
3784 ++delim;
3785 }
3786 } else {
3787 len = strlen(keystr);
3788 }
3789
3790 /* key node must be present */
Michal Vaskoe444f752020-02-10 12:20:06 +01003791 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 +02003792 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003793 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3794 "The list's key \"%.*s\" not found.", len, keystr);
3795 return LY_EVALID;
3796 }
3797 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003798 if (key->flags & LYS_KEY) {
3799 /* the node was already marked as a key */
3800 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3801 "Duplicated key identifier \"%.*s\".", len, keystr);
3802 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003803 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003804
3805 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003806 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003807 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003808 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3809 return LY_EVALID;
3810 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003811 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003812 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003813 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003815 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003816 return LY_EVALID;
3817 }
3818 } else {
3819 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003820 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003821 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003822 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003823 return LY_EVALID;
3824 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003825 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003826 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003827 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003828 return LY_EVALID;
3829 }
3830 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003831
3832 /* check status */
3833 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003834 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003835
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003836 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003837 if (key->dflt) {
3838 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3839 lysc_type_free(ctx->ctx, key->dflt->realtype);
3840 free(key->dflt);
3841 key->dflt = NULL;
3842 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003843 }
3844 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003845 key->flags |= LYS_KEY;
3846
3847 /* move it to the correct position */
3848 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3849 /* fix links in closest previous siblings of the key */
3850 if (key->next) {
3851 key->next->prev = key->prev;
3852 } else {
3853 /* last child */
3854 list->child->prev = key->prev;
3855 }
3856 if (key->prev->next) {
3857 key->prev->next = key->next;
3858 }
3859 /* fix links in the key */
3860 if (prev_key) {
3861 key->prev = (struct lysc_node*)prev_key;
3862 key->next = prev_key->next;
3863 } else {
3864 key->prev = list->child->prev;
3865 key->next = list->child;
3866 }
3867 /* fix links in closes future siblings of the key */
3868 if (prev_key) {
3869 if (prev_key->next) {
3870 prev_key->next->prev = (struct lysc_node*)key;
3871 } else {
3872 list->child->prev = (struct lysc_node*)key;
3873 }
3874 prev_key->next = (struct lysc_node*)key;
3875 } else {
3876 list->child->prev = (struct lysc_node*)key;
3877 }
3878 /* fix links in parent */
3879 if (!key->prev->next) {
3880 list->child = (struct lysc_node*)key;
3881 }
3882 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003883
3884 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003885 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003886 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003887 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003888 }
3889
3890 /* uniques */
3891 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003892 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003893 }
3894
Radek Krejciec4da802019-05-02 13:02:41 +02003895 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3896 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003897
3898done:
3899 return ret;
3900}
3901
Radek Krejcib56c7502019-02-13 14:19:54 +01003902/**
3903 * @brief Do some checks and set the default choice's case.
3904 *
3905 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3906 *
3907 * @param[in] ctx Compile context.
3908 * @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,
3909 * not the reference to the imported module.
3910 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3911 * @return LY_ERR value.
3912 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003913static LY_ERR
3914lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3915{
3916 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3917 const char *prefix = NULL, *name;
3918 size_t prefix_len = 0;
3919
3920 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3921 name = strchr(dflt, ':');
3922 if (name) {
3923 prefix = dflt;
3924 prefix_len = name - prefix;
3925 ++name;
3926 } else {
3927 name = dflt;
3928 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003929 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003930 /* prefixed default case make sense only for the prefix of the schema itself */
3931 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3932 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3933 prefix_len, prefix);
3934 return LY_EVALID;
3935 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003936 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 +01003937 if (!ch->dflt) {
3938 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3939 "Default case \"%s\" not found.", dflt);
3940 return LY_EVALID;
3941 }
3942 /* no mandatory nodes directly under the default case */
3943 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003944 if (iter->parent != (struct lysc_node*)ch->dflt) {
3945 break;
3946 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003947 if (iter->flags & LYS_MAND_TRUE) {
3948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3949 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3950 return LY_EVALID;
3951 }
3952 }
Radek Krejci01342af2019-01-03 15:18:08 +01003953 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003954 return LY_SUCCESS;
3955}
3956
Radek Krejciccd20f12019-02-15 14:12:27 +01003957static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003958lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003959{
3960 struct lys_module *mod;
3961 const char *prefix = NULL, *name;
3962 size_t prefix_len = 0;
3963 struct lysc_node_case *cs;
3964 struct lysc_node *node;
3965
3966 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3967 name = strchr(dflt, ':');
3968 if (name) {
3969 prefix = dflt;
3970 prefix_len = name - prefix;
3971 ++name;
3972 } else {
3973 name = dflt;
3974 }
3975 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3976 if (prefix) {
3977 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003979 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3980 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003981 return LY_EVALID;
3982 }
3983 } else {
3984 mod = ctx->mod;
3985 }
3986 /* get the default case */
Michal Vaskoe444f752020-02-10 12:20:06 +01003987 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 +01003988 if (!cs) {
3989 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003990 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003991 return LY_EVALID;
3992 }
3993
3994 /* check that there is no mandatory node */
3995 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003996 if (node->parent != (struct lysc_node*)cs) {
3997 break;
3998 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003999 if (node->flags & LYS_MAND_TRUE) {
4000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004001 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4002 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004003 return LY_EVALID;
4004 }
4005 }
4006
4007 /* set the default case in choice */
4008 ch->dflt = cs;
4009 cs->flags |= LYS_SET_DFLT;
4010
4011 return LY_SUCCESS;
4012}
4013
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004014/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004015 * @brief Compile parsed choice node information.
4016 * @param[in] ctx Compile context
4017 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004018 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004019 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004020 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4021 */
4022static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004023lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004024{
4025 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4026 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4027 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004028 LY_ERR ret = LY_SUCCESS;
4029
Radek Krejci056d0a82018-12-06 16:57:25 +01004030 LY_LIST_FOR(ch_p->child, child_p) {
4031 if (child_p->nodetype == LYS_CASE) {
4032 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004033 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004034 }
4035 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004036 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004037 }
4038 }
4039
4040 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004041 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004042 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004043 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004044
Radek Krejci9800fb82018-12-13 14:26:23 +01004045 return ret;
4046}
4047
4048/**
4049 * @brief Compile parsed anydata or anyxml node information.
4050 * @param[in] ctx Compile context
4051 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004052 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4053 * is enriched with the any-specific information.
4054 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4055 */
4056static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004057lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004058{
4059 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4060 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4061 unsigned int u;
4062 LY_ERR ret = LY_SUCCESS;
4063
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004064 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004065 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4066 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004067 ly_set_add(&ctx->xpath, any, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004068 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004069
4070 if (any->flags & LYS_CONFIG_W) {
4071 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004072 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004073 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004074done:
4075 return ret;
4076}
4077
Radek Krejcib56c7502019-02-13 14:19:54 +01004078/**
Michal Vasko795b3752020-07-13 15:24:27 +02004079 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
4080 * keep specific order of augments targetting the same node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004081 *
4082 * @param[in] ctx Compile context
4083 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4084 * the choice itself is expected instead of a specific case node.
4085 * @param[in] node Schema node to connect into the list.
4086 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004087 * 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 +01004088 */
4089static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004090lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004091{
Michal Vasko795b3752020-07-13 15:24:27 +02004092 struct lysc_node **children, *start, *end;
4093 const struct lys_module *mod;
Radek Krejci056d0a82018-12-06 16:57:25 +01004094
4095 if (node->nodetype == LYS_CASE) {
4096 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4097 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004098 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004099 }
4100 if (children) {
4101 if (!(*children)) {
4102 /* first child */
4103 *children = node;
4104 } else if (*children != node) {
4105 /* by the condition in previous branch we cover the choice/case children
4106 * - the children list is shared by the choice and the the first case, in addition
4107 * the first child of each case must be referenced from the case node. So the node is
4108 * actually always already inserted in case it is the first children - so here such
4109 * a situation actually corresponds to the first branch */
Michal Vasko795b3752020-07-13 15:24:27 +02004110 if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module)
4111 && (strcmp((*children)->prev->module->name, node->module->name) > 0)) {
4112 /* some augments are already connected and we are connecting new ones,
4113 * keep module name order and insert the node into the children list */
4114 end = (*children);
4115 do {
4116 end = end->prev;
4117 mod = end->module;
4118 while (end->prev->module == mod) {
4119 end = end->prev;
4120 }
4121 } while ((end->prev->module != (*children)->module) && (end->prev->module != node->module)
4122 && (strcmp(mod->name, node->module->name) > 0));
4123
4124 /* we have the last existing node after our node, easily get the first before and connect it */
4125 start = end->prev;
4126 start->next = node;
4127 node->next = end;
4128 end->prev = node;
4129 node->prev = start;
4130 } else {
4131 /* insert at the end of the parent's children list */
4132 (*children)->prev->next = node;
4133 node->prev = (*children)->prev;
4134 (*children)->prev = node;
4135 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004136
4137 /* check the name uniqueness */
4138 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4139 lysc_node_notifs(parent), node->name, node)) {
4140 return LY_EEXIST;
4141 }
4142 }
4143 }
4144 return LY_SUCCESS;
4145}
4146
Radek Krejci95710c92019-02-11 15:49:55 +01004147/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004148 * @brief Prepare the case structure in choice node for the new data node.
4149 *
4150 * 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
4151 * created in the choice when the first child was processed.
4152 *
4153 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004154 * @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,
4155 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004156 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4157 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4158 * @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,
4159 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004160 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004161static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004162lys_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 +01004163{
4164 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004165 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004166 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004167 unsigned int u;
4168 LY_ERR ret;
4169
Radek Krejci95710c92019-02-11 15:49:55 +01004170#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004171 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004172 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004173 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4174 return NULL; \
4175 } \
4176 }
4177
Radek Krejci95710c92019-02-11 15:49:55 +01004178 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4179 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004180
4181 /* we have to add an implicit case node into the parent choice */
4182 cs = calloc(1, sizeof(struct lysc_node_case));
4183 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004184 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004185 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004186 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004187 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4188 /* the case is already present since the child is not its first children */
4189 return (struct lysc_node_case*)ch->cases->prev;
4190 }
Radek Krejci95710c92019-02-11 15:49:55 +01004191 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004192
4193 /* explicit parent case is not present (this is its first child) */
4194 cs = calloc(1, sizeof(struct lysc_node_case));
4195 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004196 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004197 cs->flags = LYS_STATUS_MASK & node_p->flags;
4198 cs->sp = node_p;
4199
Radek Krejcib1b59152019-01-07 13:21:56 +01004200 /* 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 +02004201 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004202
4203 if (node_p->when) {
4204 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004205 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004206 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004207
4208 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4209 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004210 ly_set_add(&ctx->xpath, cs, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004211 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004212 }
Radek Krejciec4da802019-05-02 13:02:41 +02004213 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004214 } else {
4215 LOGINT(ctx->ctx);
4216 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004217 }
4218 cs->module = ctx->mod;
4219 cs->prev = (struct lysc_node*)cs;
4220 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004221 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004222 cs->child = child;
4223
4224 return cs;
4225error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004226 if (cs) {
4227 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4228 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004229 return NULL;
4230
4231#undef UNIQUE_CHECK
4232}
4233
Radek Krejcib56c7502019-02-13 14:19:54 +01004234/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004235 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004236 *
4237 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004238 * @param[in] node Target node where the config is supposed to be changed.
4239 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004240 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4241 * 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 +01004242 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4243 * @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 +01004244 * @return LY_ERR value.
4245 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004246static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004247lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004248 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004249{
4250 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004251 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004252
4253 if (config == (node->flags & LYS_CONFIG_MASK)) {
4254 /* nothing to do */
4255 return LY_SUCCESS;
4256 }
4257
4258 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004259 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004260 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4261 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004262 "Invalid %s of config - configuration node cannot be child of any state data node.",
4263 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004264 return LY_EVALID;
4265 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004266 node->flags |= LYS_SET_CONFIG;
4267 } else {
4268 if (node->flags & LYS_SET_CONFIG) {
4269 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4270 /* setting config flags, but have node with explicit config true */
4271 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004272 "Invalid %s of config - configuration node cannot be child of any state data node.",
4273 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004274 return LY_EVALID;
4275 }
4276 /* do not change config on nodes where the config is explicitely set, this does not apply to
4277 * nodes, which are being changed explicitly (targets of refine or deviation) */
4278 return LY_SUCCESS;
4279 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004280 }
4281 node->flags &= ~LYS_CONFIG_MASK;
4282 node->flags |= config;
4283
4284 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004285 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004286 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004287 }
4288
Radek Krejci76b3e962018-12-14 17:01:25 +01004289 return LY_SUCCESS;
4290}
4291
Radek Krejcib56c7502019-02-13 14:19:54 +01004292/**
4293 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4294 *
4295 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4296 * the flag to such parents from a mandatory children.
4297 *
4298 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4299 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4300 * (mandatory children was removed).
4301 */
Radek Krejcife909632019-02-12 15:34:42 +01004302void
4303lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4304{
4305 struct lysc_node *iter;
4306
4307 if (add) { /* set flag */
4308 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4309 parent = parent->parent) {
4310 parent->flags |= LYS_MAND_TRUE;
4311 }
4312 } else { /* unset flag */
4313 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004314 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004315 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004316 /* there is another mandatory node */
4317 return;
4318 }
4319 }
4320 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4321 parent->flags &= ~LYS_MAND_TRUE;
4322 }
4323 }
4324}
4325
Radek Krejci056d0a82018-12-06 16:57:25 +01004326/**
Radek Krejci3641f562019-02-13 15:38:40 +01004327 * @brief Internal sorting process for the lys_compile_augment_sort().
4328 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4329 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4330 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4331 */
4332static void
4333lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4334{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004335 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004336 size_t len;
4337
4338 len = strlen(aug_p->nodeid);
4339 LY_ARRAY_FOR(result, v) {
4340 if (strlen(result[v]->nodeid) <= len) {
4341 continue;
4342 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004343 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004344 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004345 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004346 break;
4347 }
4348 }
4349 result[v] = aug_p;
4350 LY_ARRAY_INCREMENT(result);
4351}
4352
4353/**
4354 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4355 *
4356 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4357 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4358 *
4359 * @param[in] ctx Compile context.
4360 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4361 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4362 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4363 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4364 * @return LY_ERR value.
4365 */
4366LY_ERR
4367lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4368{
4369 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004370 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004371
4372 assert(augments);
4373
4374 /* get count of the augments in module and all its submodules */
4375 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004376 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004377 }
4378 LY_ARRAY_FOR(inc_p, u) {
4379 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004380 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004381 }
4382 }
4383
4384 if (!count) {
4385 *augments = NULL;
4386 return LY_SUCCESS;
4387 }
4388 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4389
4390 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4391 * together, so there can be even /z/y betwwen them. */
4392 LY_ARRAY_FOR(aug_p, u) {
4393 lys_compile_augment_sort_(&aug_p[u], result);
4394 }
4395 LY_ARRAY_FOR(inc_p, u) {
4396 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4397 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4398 }
4399 }
4400
4401 *augments = result;
4402 return LY_SUCCESS;
4403}
4404
4405/**
4406 * @brief Compile the parsed augment connecting it into its target.
4407 *
4408 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4409 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4410 * are already implemented and compiled.
4411 *
4412 * @param[in] ctx Compile context.
4413 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004414 * @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
4415 * children in case of the augmenting uses data.
4416 * @return LY_SUCCESS on success.
4417 * @return LY_EVALID on failure.
4418 */
4419LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004420lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004421{
Michal Vaskoe6143202020-07-03 13:02:08 +02004422 LY_ERR ret = LY_SUCCESS, rc;
Radek Krejci3641f562019-02-13 15:38:40 +01004423 struct lysp_node *node_p, *case_node_p;
4424 struct lysc_node *target; /* target target of the augment */
4425 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004426 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004427 struct lys_module **aug_mod;
Radek Krejci3641f562019-02-13 15:38:40 +01004428 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004429 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004430 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejciec4da802019-05-02 13:02:41 +02004431 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004432
Radek Krejci327de162019-06-14 12:52:07 +02004433 lysc_update_path(ctx, NULL, "{augment}");
4434 lysc_update_path(ctx, NULL, aug_p->nodeid);
4435
Radek Krejci7af64242019-02-18 13:07:53 +01004436 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004437 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004438 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004439 if (ret != LY_SUCCESS) {
4440 if (ret == LY_EDENIED) {
4441 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4442 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4443 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4444 }
4445 return LY_EVALID;
4446 }
4447
4448 /* check for mandatory nodes
4449 * - new cases augmenting some choice can have mandatory nodes
4450 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4451 */
Radek Krejci733988a2019-02-15 15:12:44 +01004452 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004453 allow_mandatory = 1;
4454 }
4455
4456 when_shared = NULL;
4457 LY_LIST_FOR(aug_p->child, node_p) {
4458 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4459 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004460 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004461 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4462 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004463 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004464 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4465 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004466 return LY_EVALID;
4467 }
4468
4469 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004470 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004471 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004472 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004473 } else {
4474 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004475 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004476 }
4477 }
Radek Krejciec4da802019-05-02 13:02:41 +02004478 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004479
Radek Krejcife13da42019-02-15 14:51:01 +01004480 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4481 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004482 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004483 /* 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 +01004484 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 +01004485 } else if (target->nodetype == LYS_CHOICE) {
4486 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4487 node = ((struct lysc_node_choice*)target)->cases->prev;
4488 } else {
4489 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004490 node = (struct lysc_node*)lysc_node_children(target, flags);
4491 if (!node) {
4492 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4493 break;
4494 }
4495 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004496 }
4497
Radek Krejci733988a2019-02-15 15:12:44 +01004498 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004499 node->flags &= ~LYS_MAND_TRUE;
4500 lys_compile_mandatory_parents(target, 0);
4501 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004502 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004503 return LY_EVALID;
4504 }
4505
4506 /* pass augment's when to all the children */
4507 if (aug_p->when) {
4508 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4509 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004510 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004511 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004512
4513 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4514 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004515 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004516 }
4517
Radek Krejci3641f562019-02-13 15:38:40 +01004518 when_shared = *when;
4519 } else {
4520 ++when_shared->refcount;
4521 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004522
4523 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4524 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004525 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004526 }
Radek Krejci3641f562019-02-13 15:38:40 +01004527 }
4528 }
4529 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004530
Radek Krejciec4da802019-05-02 13:02:41 +02004531 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004532 switch (target->nodetype) {
4533 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004534 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004535 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004536 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004537 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004538 break;
4539 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004540 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004541 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004542 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004543 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004544 break;
4545 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004546 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004547 if (aug_p->actions) {
4548 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004549 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4550 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004551 return LY_EVALID;
4552 }
4553 if (aug_p->notifs) {
4554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004555 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004556 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004557 return LY_EVALID;
4558 }
4559 }
Radek Krejci3641f562019-02-13 15:38:40 +01004560
Michal Vaskoe6143202020-07-03 13:02:08 +02004561 /* add this module into the target module augmented_by, if not there already */
4562 rc = LY_SUCCESS;
4563 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4564 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4565 rc = LY_EEXIST;
4566 break;
4567 }
4568 }
4569 if (!rc) {
4570 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4571 *aug_mod = ctx->mod;
4572 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004573
Radek Krejci327de162019-06-14 12:52:07 +02004574 lysc_update_path(ctx, NULL, NULL);
4575 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004576
Radek Krejci3641f562019-02-13 15:38:40 +01004577error:
Radek Krejciec4da802019-05-02 13:02:41 +02004578 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004579 return ret;
4580}
4581
4582/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004583 * @brief Apply refined or deviated mandatory flag to the target node.
4584 *
4585 * @param[in] ctx Compile context.
4586 * @param[in] node Target node where the mandatory property is supposed to be changed.
4587 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004588 * @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 +01004589 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4590 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4591 * 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 +01004592 * @return LY_ERR value.
4593 */
4594static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004595lys_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 +01004596{
4597 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4598 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004599 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4600 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004601 return LY_EVALID;
4602 }
4603
4604 if (mandatory_flag & LYS_MAND_TRUE) {
4605 /* check if node has default value */
4606 if (node->nodetype & LYS_LEAF) {
4607 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004608 if (refine_flag) {
4609 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004610 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004611 return LY_EVALID;
4612 }
Radek Krejcia1911222019-07-22 17:24:50 +02004613 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004614 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004615 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004616
4617 /* update the list of incomplete default values if needed */
4618 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4619
Radek Krejcia1911222019-07-22 17:24:50 +02004620 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4621 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4622 free(leaf->dflt);
4623 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004624 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004625 }
4626 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004627 if (refine_flag) {
4628 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004629 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004630 return LY_EVALID;
4631 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004632 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004633 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004634 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 +01004635 return LY_EVALID;
4636 }
4637
4638 node->flags &= ~LYS_MAND_FALSE;
4639 node->flags |= LYS_MAND_TRUE;
4640 lys_compile_mandatory_parents(node->parent, 1);
4641 } else {
4642 /* make mandatory false */
4643 node->flags &= ~LYS_MAND_TRUE;
4644 node->flags |= LYS_MAND_FALSE;
4645 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004646 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 +01004647 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004648 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004649 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004650 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4651 leaf->dflt->realtype = leaf->type->dflt->realtype;
4652 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4653 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004654 }
4655 }
4656 return LY_SUCCESS;
4657}
4658
4659/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004660 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4661 * If present, also apply uses's modificators.
4662 *
4663 * @param[in] ctx Compile context
4664 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004665 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4666 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4667 * the compile context.
4668 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4669 */
4670static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004671lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004672{
4673 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004674 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004675 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004676 struct lysc_node_container context_node_fake =
4677 {.nodetype = LYS_CONTAINER,
4678 .module = ctx->mod,
4679 .flags = parent ? parent->flags : 0,
4680 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004681 .prev = (struct lysc_node*)&context_node_fake,
4682 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004683 struct lysp_grp *grp = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004684 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004685 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004686 int found;
4687 const char *id, *name, *prefix;
4688 size_t prefix_len, name_len;
4689 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004690 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004691 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004692 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004693 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004694 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004695 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004696 struct lysp_augment **augments = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004697 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004698 struct lysc_notif **notifs = NULL;
4699 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004700
4701 /* search for the grouping definition */
4702 found = 0;
4703 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004704 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004705 if (prefix) {
4706 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4707 if (!mod) {
4708 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004709 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004710 return LY_EVALID;
4711 }
4712 } else {
4713 mod = ctx->mod_def;
4714 }
4715 if (mod == ctx->mod_def) {
4716 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004717 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004718 LY_ARRAY_FOR(grp, u) {
4719 if (!strcmp(grp[u].name, name)) {
4720 grp = &grp[u];
4721 found = 1;
4722 break;
4723 }
4724 }
4725 }
4726 }
4727 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004728 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004729 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004730 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004731 for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004732 if (!strcmp(grp[u].name, name)) {
4733 grp = &grp[u];
4734 found = 1;
4735 }
4736 }
4737 }
4738 if (!found && mod->parsed->includes) {
4739 /* ... and all the submodules */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004740 for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004741 grp = mod->parsed->includes[u].submodule->groupings;
4742 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004743 for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004744 if (!strcmp(grp[v].name, name)) {
4745 grp = &grp[v];
4746 found = 1;
4747 }
4748 }
4749 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004750 }
4751 }
4752 }
4753 if (!found) {
4754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4755 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4756 return LY_EVALID;
4757 }
4758
4759 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4760 grp_stack_count = ctx->groupings.count;
4761 ly_set_add(&ctx->groupings, (void*)grp, 0);
4762 if (grp_stack_count == ctx->groupings.count) {
4763 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4764 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4765 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4766 return LY_EVALID;
4767 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004768 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4769 /* remember that the grouping is instantiated to avoid its standalone validation */
4770 grp->flags |= LYS_USED_GRP;
4771 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004772
4773 /* switch context's mod_def */
4774 mod_old = ctx->mod_def;
4775 ctx->mod_def = mod;
4776
4777 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004778 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 +01004779
Radek Krejcifc11bd72019-04-11 16:00:05 +02004780 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004781 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004782 /* 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 +02004783 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 +01004784
4785 /* some preparation for applying refines */
4786 if (grp->data == node_p) {
4787 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004788 if (parent) {
4789 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4790 } else if (ctx->mod->compiled->data) {
4791 child = ctx->mod->compiled->data;
4792 } else {
4793 child = NULL;
4794 }
4795 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004796 }
4797 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004798 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004799 LY_LIST_FOR(context_node_fake.child, child) {
4800 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004801
Radek Krejcifc11bd72019-04-11 16:00:05 +02004802 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004803 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004804 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004805 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004806 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4807
4808 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4809 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004810 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004811 }
4812
Radek Krejci00b874b2019-02-12 10:54:50 +01004813 when_shared = *when;
4814 } else {
4815 ++when_shared->refcount;
4816 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004817
4818 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4819 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004820 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004821 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004822 }
4823 }
Radek Krejci01342af2019-01-03 15:18:08 +01004824 }
4825 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004826 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004827 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004828 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004829 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 +01004830 }
4831
Radek Krejcifc11bd72019-04-11 16:00:05 +02004832 /* compile actions */
4833 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4834 if (actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004835 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004836 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004837 if (*actions && (uses_p->augments || uses_p->refines)) {
4838 /* 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 +02004839 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
4840 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
4841 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004842 }
4843 }
4844
4845 /* compile notifications */
4846 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4847 if (notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004848 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004849 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004850 if (*notifs && (uses_p->augments || uses_p->refines)) {
4851 /* 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 +02004852 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
4853 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
4854 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004855 }
4856 }
4857
4858
Radek Krejci3641f562019-02-13 15:38:40 +01004859 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004860 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004861 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004862 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004863 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004864
Radek Krejcif0089082019-01-07 16:42:01 +01004865 /* reload previous context's mod_def */
4866 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004867 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004868
Radek Krejci76b3e962018-12-14 17:01:25 +01004869 /* apply refine */
4870 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004871 lysc_update_path(ctx, NULL, rfn->nodeid);
4872
Radek Krejci7af64242019-02-18 13:07:53 +01004873 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 +01004874 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004875 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004876 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004877
4878 /* default value */
4879 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004880 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004882 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.",
4883 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004884 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004885 }
4886 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4887 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004888 "Invalid refine of default - %s cannot hold default value(s).",
4889 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004890 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004891 }
4892 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004893 struct ly_err_item *err = NULL;
4894 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4895 if (leaf->dflt) {
4896 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004897 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004898 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4899 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4900 } else {
4901 /* prepare a new one */
4902 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4903 leaf->dflt->realtype = leaf->type;
4904 }
4905 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004906 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004907 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4908 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004909 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004910 leaf->dflt->realtype->refcount++;
4911 if (err) {
4912 ly_err_print(err);
4913 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4914 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4915 ly_err_free(err);
4916 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004917 if (rc == LY_EINCOMPLETE) {
4918 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004919 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004920
4921 /* but in general result is so far ok */
4922 rc = LY_SUCCESS;
4923 }
Radek Krejcia1911222019-07-22 17:24:50 +02004924 LY_CHECK_GOTO(rc, cleanup);
4925 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004926 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004927 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4928
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004929 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004930 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4931 "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 +02004932 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004933 }
Radek Krejcia1911222019-07-22 17:24:50 +02004934
4935 /* remove previous set of default values */
4936 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004937 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004938 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4939 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4940 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004941 }
Radek Krejcia1911222019-07-22 17:24:50 +02004942 LY_ARRAY_FREE(llist->dflts);
4943 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004944 LY_ARRAY_FREE(llist->dflts_mods);
4945 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004946
4947 /* create the new set of the default values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004948 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
4949 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004950 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004951 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004952 LY_ARRAY_INCREMENT(llist->dflts_mods);
4953 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004954 LY_ARRAY_INCREMENT(llist->dflts);
4955 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4956 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004957 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004958 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004959 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004960 llist->dflts[u]->realtype->refcount++;
4961 if (err) {
4962 ly_err_print(err);
4963 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4964 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4965 ly_err_free(err);
4966 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004967 if (rc == LY_EINCOMPLETE) {
4968 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004969 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004970
4971 /* but in general result is so far ok */
4972 rc = LY_SUCCESS;
4973 }
4974 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004975 }
Radek Krejcia1911222019-07-22 17:24:50 +02004976 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004977 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004978 if (((struct lysc_node_choice*)node)->dflt) {
4979 /* unset LYS_SET_DFLT from the current default case */
4980 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4981 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004982 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 +01004983 }
4984 }
4985
Radek Krejci12fb9142019-01-08 09:45:30 +01004986 /* description */
4987 if (rfn->dsc) {
4988 FREE_STRING(ctx->ctx, node->dsc);
4989 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4990 }
4991
4992 /* reference */
4993 if (rfn->ref) {
4994 FREE_STRING(ctx->ctx, node->ref);
4995 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4996 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004997
4998 /* config */
4999 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005000 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005001 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005002 } else {
5003 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01005004 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005005 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005006 }
5007
5008 /* mandatory */
5009 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02005010 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005011 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005012
5013 /* presence */
5014 if (rfn->presence) {
5015 if (node->nodetype != LYS_CONTAINER) {
5016 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005017 "Invalid refine of presence statement - %s cannot hold the presence statement.",
5018 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005019 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005020 }
5021 node->flags |= LYS_PRESENCE;
5022 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005023
5024 /* must */
5025 if (rfn->musts) {
5026 switch (node->nodetype) {
5027 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005028 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 +01005029 break;
5030 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005031 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 +01005032 break;
5033 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005034 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 +01005035 break;
5036 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005037 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 +01005038 break;
5039 case LYS_ANYXML:
5040 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005041 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 +01005042 break;
5043 default:
5044 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005045 "Invalid refine of must statement - %s cannot hold any must statement.",
5046 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005047 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005048 }
Michal Vasko004d3152020-06-11 19:59:22 +02005049 ly_set_add(&ctx->xpath, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01005050 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005051
5052 /* min/max-elements */
5053 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5054 switch (node->nodetype) {
5055 case LYS_LEAFLIST:
5056 if (rfn->flags & LYS_SET_MAX) {
5057 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5058 }
5059 if (rfn->flags & LYS_SET_MIN) {
5060 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005061 if (rfn->min) {
5062 node->flags |= LYS_MAND_TRUE;
5063 lys_compile_mandatory_parents(node->parent, 1);
5064 } else {
5065 node->flags &= ~LYS_MAND_TRUE;
5066 lys_compile_mandatory_parents(node->parent, 0);
5067 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005068 }
5069 break;
5070 case LYS_LIST:
5071 if (rfn->flags & LYS_SET_MAX) {
5072 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5073 }
5074 if (rfn->flags & LYS_SET_MIN) {
5075 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005076 if (rfn->min) {
5077 node->flags |= LYS_MAND_TRUE;
5078 lys_compile_mandatory_parents(node->parent, 1);
5079 } else {
5080 node->flags &= ~LYS_MAND_TRUE;
5081 lys_compile_mandatory_parents(node->parent, 0);
5082 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005083 }
5084 break;
5085 default:
5086 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005087 "Invalid refine of %s statement - %s cannot hold this statement.",
5088 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005089 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005090 }
5091 }
Radek Krejcif0089082019-01-07 16:42:01 +01005092
5093 /* if-feature */
5094 if (rfn->iffeatures) {
5095 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005096 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005097 }
Radek Krejci327de162019-06-14 12:52:07 +02005098
5099 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005100 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005101
Radek Krejcif2271f12019-01-07 16:42:23 +01005102 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005103 for (uint32_t i = 0; i < refined.count; ++i) {
5104 node = (struct lysc_node*)refined.objs[i];
5105 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02005106 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005107
5108 /* check possible conflict with default value (default added, mandatory left true) */
5109 if ((node->flags & LYS_MAND_TRUE) &&
5110 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5111 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5112 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005113 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005114 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005115 }
5116
5117 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5118 if (node->nodetype == LYS_LIST) {
5119 min = ((struct lysc_node_list*)node)->min;
5120 max = ((struct lysc_node_list*)node)->max;
5121 } else {
5122 min = ((struct lysc_node_leaflist*)node)->min;
5123 max = ((struct lysc_node_leaflist*)node)->max;
5124 }
5125 if (min > max) {
5126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005127 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5128 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005129 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005130 }
5131 }
5132 }
5133
Radek Krejci327de162019-06-14 12:52:07 +02005134 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005135 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005136
5137cleanup:
5138 /* fix connection of the children nodes from fake context node back into the parent */
5139 if (context_node_fake.child) {
5140 context_node_fake.child->prev = child;
5141 }
5142 LY_LIST_FOR(context_node_fake.child, child) {
5143 child->parent = parent;
5144 }
5145
5146 if (uses_p->augments || uses_p->refines) {
5147 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005148 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005149 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005150 LY_ARRAY_FREE(context_node_fake.actions);
5151 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005152 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005153 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005154 LY_ARRAY_FREE(context_node_fake.notifs);
5155 }
5156 }
5157
Radek Krejcie86bf772018-12-14 11:39:53 +01005158 /* reload previous context's mod_def */
5159 ctx->mod_def = mod_old;
5160 /* remove the grouping from the stack for circular groupings dependency check */
5161 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5162 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005163 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005164 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005165
5166 return ret;
5167}
5168
Radek Krejci327de162019-06-14 12:52:07 +02005169static int
5170lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5171{
5172 struct lysp_node *iter;
5173 int len = 0;
5174
5175 *path = NULL;
5176 for (iter = node; iter && len >= 0; iter = iter->parent) {
5177 char *s = *path;
5178 char *id;
5179
5180 switch (iter->nodetype) {
5181 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005182 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005183 break;
5184 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005185 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005186 break;
5187 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005188 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005189 break;
5190 default:
5191 id = strdup(iter->name);
5192 break;
5193 }
5194
5195 if (!iter->parent) {
5196 /* print prefix */
5197 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5198 } else {
5199 /* prefix is the same as in parent */
5200 len = asprintf(path, "/%s%s", id, s ? s : "");
5201 }
5202 free(s);
5203 free(id);
5204 }
5205
5206 if (len < 0) {
5207 free(*path);
5208 *path = NULL;
5209 } else if (len == 0) {
5210 *path = strdup("/");
5211 len = 1;
5212 }
5213 return len;
5214}
5215
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005216/**
5217 * @brief Validate groupings that were defined but not directly used in the schema itself.
5218 *
5219 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5220 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5221 */
5222static LY_ERR
5223lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5224{
5225 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005226 char *path;
5227 int len;
5228
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005229 struct lysp_node_uses fake_uses = {
5230 .parent = node_p,
5231 .nodetype = LYS_USES,
5232 .flags = 0, .next = NULL,
5233 .name = grp->name,
5234 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5235 .refines = NULL, .augments = NULL
5236 };
5237 struct lysc_node_container fake_container = {
5238 .nodetype = LYS_CONTAINER,
5239 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5240 .module = ctx->mod,
5241 .sp = NULL, .parent = NULL, .next = NULL,
5242 .prev = (struct lysc_node*)&fake_container,
5243 .name = "fake",
5244 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5245 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5246 };
5247
5248 if (grp->parent) {
5249 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5250 }
Radek Krejci327de162019-06-14 12:52:07 +02005251
5252 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5253 if (len < 0) {
5254 LOGMEM(ctx->ctx);
5255 return LY_EMEM;
5256 }
5257 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5258 ctx->path_len = (uint16_t)len;
5259 free(path);
5260
5261 lysc_update_path(ctx, NULL, "{grouping}");
5262 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005263 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005264 lysc_update_path(ctx, NULL, NULL);
5265 lysc_update_path(ctx, NULL, NULL);
5266
5267 ctx->path_len = 1;
5268 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005269
5270 /* cleanup */
5271 lysc_node_container_free(ctx->ctx, &fake_container);
5272
5273 return ret;
5274}
Radek Krejcife909632019-02-12 15:34:42 +01005275
Radek Krejcie86bf772018-12-14 11:39:53 +01005276/**
Radek Krejcia3045382018-11-22 14:30:31 +01005277 * @brief Compile parsed schema node information.
5278 * @param[in] ctx Compile context
5279 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005280 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5281 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5282 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005283 * @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).
5284 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005285 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5286 */
Radek Krejci19a96102018-11-15 13:38:09 +01005287static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005288lys_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 +01005289{
Radek Krejci1c54f462020-05-12 17:25:34 +02005290 LY_ERR ret = LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01005291 struct lysc_node *node;
5292 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005293 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005294 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005295 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005296
Radek Krejci327de162019-06-14 12:52:07 +02005297 if (node_p->nodetype != LYS_USES) {
5298 lysc_update_path(ctx, parent, node_p->name);
5299 } else {
5300 lysc_update_path(ctx, NULL, "{uses}");
5301 lysc_update_path(ctx, NULL, node_p->name);
5302 }
5303
Radek Krejci19a96102018-11-15 13:38:09 +01005304 switch (node_p->nodetype) {
5305 case LYS_CONTAINER:
5306 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5307 node_compile_spec = lys_compile_node_container;
5308 break;
5309 case LYS_LEAF:
5310 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5311 node_compile_spec = lys_compile_node_leaf;
5312 break;
5313 case LYS_LIST:
5314 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005315 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005316 break;
5317 case LYS_LEAFLIST:
5318 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005319 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005320 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005321 case LYS_CHOICE:
5322 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005323 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005324 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005325 case LYS_ANYXML:
5326 case LYS_ANYDATA:
5327 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005328 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005329 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005330 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005331 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5332 lysc_update_path(ctx, NULL, NULL);
5333 lysc_update_path(ctx, NULL, NULL);
5334 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005335 default:
5336 LOGINT(ctx->ctx);
5337 return LY_EINT;
5338 }
5339 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5340 node->nodetype = node_p->nodetype;
5341 node->module = ctx->mod;
5342 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005343 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005344
5345 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005346 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005347 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005348 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005349 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5350 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005351 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005352 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005353 node->flags |= LYS_CONFIG_R;
5354 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005355 /* config not explicitely set, inherit it from parent */
5356 if (parent) {
5357 node->flags |= parent->flags & LYS_CONFIG_MASK;
5358 } else {
5359 /* default is config true */
5360 node->flags |= LYS_CONFIG_W;
5361 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005362 } else {
5363 /* config set explicitely */
5364 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005365 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005366 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5367 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5368 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005369 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005370 goto error;
5371 }
Radek Krejci19a96102018-11-15 13:38:09 +01005372
Radek Krejcia6d57732018-11-29 13:40:37 +01005373 /* *list ordering */
5374 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5375 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005376 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005377 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5378 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005379 node->flags &= ~LYS_ORDBY_MASK;
5380 node->flags |= LYS_ORDBY_SYSTEM;
5381 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5382 /* default ordering is system */
5383 node->flags |= LYS_ORDBY_SYSTEM;
5384 }
5385 }
5386
Radek Krejci19a96102018-11-15 13:38:09 +01005387 /* status - it is not inherited by specification, but it does not make sense to have
5388 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005389 if (!parent || parent->nodetype != LYS_CHOICE) {
5390 /* in case of choice/case's children, postpone the check to the moment we know if
5391 * 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 +02005392 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 +01005393 }
5394
Radek Krejciec4da802019-05-02 13:02:41 +02005395 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005396 node->sp = node_p;
5397 }
5398 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005399 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5400 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005401 if (node_p->when) {
5402 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005403 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005404
5405 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5406 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02005407 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01005408 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005409 }
Radek Krejciec4da802019-05-02 13:02:41 +02005410 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005411
5412 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005413 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005414
Radek Krejci0935f412019-08-20 16:15:18 +02005415 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5416
Radek Krejcife909632019-02-12 15:34:42 +01005417 /* inherit LYS_MAND_TRUE in parent containers */
5418 if (node->flags & LYS_MAND_TRUE) {
5419 lys_compile_mandatory_parents(parent, 1);
5420 }
5421
Radek Krejci327de162019-06-14 12:52:07 +02005422 lysc_update_path(ctx, NULL, NULL);
5423
Radek Krejci19a96102018-11-15 13:38:09 +01005424 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005425 if (parent) {
5426 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005427 if (node_p->parent->nodetype == LYS_CASE) {
5428 lysc_update_path(ctx, parent, node_p->parent->name);
5429 } else {
5430 lysc_update_path(ctx, parent, node->name);
5431 }
Radek Krejciec4da802019-05-02 13:02:41 +02005432 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005433 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005434 if (uses_status) {
5435
5436 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005437 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005438 * it directly gets the same status flags as the choice;
5439 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005440 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005441 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005442 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005443 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005444 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005445 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005446 }
Radek Krejciec4da802019-05-02 13:02:41 +02005447 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 +02005448
5449 if (parent->nodetype == LYS_CHOICE) {
5450 lysc_update_path(ctx, NULL, NULL);
5451 }
Radek Krejci19a96102018-11-15 13:38:09 +01005452 } else {
5453 /* top-level element */
5454 if (!ctx->mod->compiled->data) {
5455 ctx->mod->compiled->data = node;
5456 } else {
5457 /* insert at the end of the module's top-level nodes list */
5458 ctx->mod->compiled->data->prev->next = node;
5459 node->prev = ctx->mod->compiled->data->prev;
5460 ctx->mod->compiled->data->prev = node;
5461 }
Radek Krejci327de162019-06-14 12:52:07 +02005462 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005463 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5464 ctx->mod->compiled->notifs, node->name, node)) {
5465 return LY_EVALID;
5466 }
Radek Krejci19a96102018-11-15 13:38:09 +01005467 }
Radek Krejci327de162019-06-14 12:52:07 +02005468 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005469
5470 return LY_SUCCESS;
5471
5472error:
5473 lysc_node_free(ctx->ctx, node);
5474 return ret;
5475}
5476
Radek Krejciccd20f12019-02-15 14:12:27 +01005477static void
5478lysc_disconnect(struct lysc_node *node)
5479{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005480 struct lysc_node *parent, *child, *prev = NULL, *next;
5481 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005482 int remove_cs = 0;
5483
5484 parent = node->parent;
5485
5486 /* parent's first child */
5487 if (!parent) {
5488 return;
5489 }
5490 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005491 cs = (struct lysc_node_case*)node;
5492 } else if (parent->nodetype == LYS_CASE) {
5493 /* disconnecting some node in a case */
5494 cs = (struct lysc_node_case*)parent;
5495 parent = cs->parent;
5496 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5497 if (child == node) {
5498 if (cs->child == child) {
5499 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5500 /* case with a single child -> remove also the case */
5501 child->parent = NULL;
5502 remove_cs = 1;
5503 } else {
5504 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005505 }
5506 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005507 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005508 }
5509 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005510 if (!remove_cs) {
5511 cs = NULL;
5512 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005513 } else if (lysc_node_children(parent, node->flags) == node) {
5514 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005515 }
5516
5517 if (cs) {
5518 if (remove_cs) {
5519 /* cs has only one child which is being also removed */
5520 lysc_disconnect((struct lysc_node*)cs);
5521 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5522 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005523 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5524 /* default case removed */
5525 ((struct lysc_node_choice*)parent)->dflt = NULL;
5526 }
5527 if (((struct lysc_node_choice*)parent)->cases == cs) {
5528 /* first case removed */
5529 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5530 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005531 if (cs->child) {
5532 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5533 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5534 prev = cs->child->prev;
5535 } /* else all the children are under a single case */
5536 LY_LIST_FOR_SAFE(cs->child, next, child) {
5537 if (child->parent != (struct lysc_node*)cs) {
5538 break;
5539 }
5540 lysc_node_free(node->module->ctx, child);
5541 }
5542 if (prev) {
5543 if (prev->next) {
5544 prev->next = child;
5545 }
5546 if (child) {
5547 child->prev = prev;
5548 } else {
5549 /* link from the first child under the cases */
5550 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5551 }
5552 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005553 }
5554 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005555 }
5556
5557 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005558 if (node->prev->next) {
5559 node->prev->next = node->next;
5560 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005561 if (node->next) {
5562 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005563 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005564 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005565 if (child) {
5566 child->prev = node->prev;
5567 }
5568 } else if (((struct lysc_node_choice*)parent)->cases) {
5569 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005570 }
5571}
5572
5573LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005574lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005575{
Radek Krejcia1911222019-07-22 17:24:50 +02005576 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005577 struct ly_set devs_p = {0};
5578 struct ly_set targets = {0};
5579 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005580 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005581 struct lysc_action *rpcs;
5582 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005583 struct lysp_deviation *dev;
5584 struct lysp_deviate *d, **dp_new;
5585 struct lysp_deviate_add *d_add;
5586 struct lysp_deviate_del *d_del;
5587 struct lysp_deviate_rpl *d_rpl;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005588 LY_ARRAY_COUNT_TYPE u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005589 struct lysc_deviation {
5590 const char *nodeid;
5591 struct lysc_node *target; /* target node of the deviation */
5592 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005593 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005594 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5595 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005596 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005597 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005598 const char *prefix, *name, *nodeid, *dflt;
Michal Vasko57c10cd2020-05-27 15:57:11 +02005599 struct lys_module *mod, **dev_mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005600 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005601 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005602
5603 /* get all deviations from the module and all its submodules ... */
5604 LY_ARRAY_FOR(mod_p->deviations, u) {
5605 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5606 }
5607 LY_ARRAY_FOR(mod_p->includes, v) {
5608 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5609 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5610 }
5611 }
5612 if (!devs_p.count) {
5613 /* nothing to do */
5614 return LY_SUCCESS;
5615 }
5616
Radek Krejci327de162019-06-14 12:52:07 +02005617 lysc_update_path(ctx, NULL, "{deviation}");
5618
Radek Krejciccd20f12019-02-15 14:12:27 +01005619 /* ... and group them by the target node */
5620 devs = calloc(devs_p.count, sizeof *devs);
5621 for (u = 0; u < devs_p.count; ++u) {
5622 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005623 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005624
5625 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005626 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5627 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01005628 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005629 /* move the target pointer to input/output to make them different from the action and
5630 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5631 * back to the RPC/action node due to a better compatibility and decision code in this function.
5632 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5633 if (flags & LYSC_OPT_RPC_INPUT) {
5634 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5635 flags |= LYSC_OPT_INTERNAL;
5636 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5637 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5638 flags |= LYSC_OPT_INTERNAL;
5639 }
5640 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005641 /* insert into the set of targets with duplicity detection */
5642 i = ly_set_add(&targets, target, 0);
5643 if (!devs[i]) {
5644 /* new record */
5645 devs[i] = calloc(1, sizeof **devs);
5646 devs[i]->target = target;
5647 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005648 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005649 }
5650 /* add deviates into the deviation's list of deviates */
5651 for (d = dev->deviates; d; d = d->next) {
5652 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5653 *dp_new = d;
5654 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5655 devs[i]->not_supported = 1;
5656 }
5657 }
Radek Krejci327de162019-06-14 12:52:07 +02005658
5659 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005660 }
5661
5662 /* MACROS for deviates checking */
5663#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5664 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005665 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 +01005666 goto cleanup; \
5667 }
5668
5669#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005670 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5672 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005673 goto cleanup; \
5674 }
5675
Radek Krejcia1911222019-07-22 17:24:50 +02005676
Radek Krejciccd20f12019-02-15 14:12:27 +01005677#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005678 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5680 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5681 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5682 goto cleanup; \
5683 }
5684
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005685#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005686 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005687 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005688 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5689 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005690 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005691 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5692 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005693 goto cleanup; \
5694 }
5695
Radek Krejci551b12c2019-02-19 16:11:21 +01005696#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5697 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005699 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5700 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005701 goto cleanup; \
5702 }
5703
Radek Krejciccd20f12019-02-15 14:12:27 +01005704#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5705 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005707 goto cleanup; \
5708 }
5709
Radek Krejci551b12c2019-02-19 16:11:21 +01005710#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5711 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5712 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005713 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005714 goto cleanup; \
5715 }
5716
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005717#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5718 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5719 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5720 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5721 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 +01005722 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005723 if (y == LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005724 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005725 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5726 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005727 goto cleanup; \
5728 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005729 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5730 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5731 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5732 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005733 (LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005734 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005735 if (!LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005736 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5737 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005738 }
5739
5740 /* apply deviations */
5741 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005742 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5743 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5744 struct ly_err_item *err = NULL;
5745
5746 dflt = NULL;
5747 changed_type = 0;
5748
Radek Krejci327de162019-06-14 12:52:07 +02005749 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5750
Radek Krejcif538ce52019-03-05 10:46:14 +01005751 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5752 /* fix the target pointer in case of RPC's/action's input/output */
5753 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5754 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5755 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5756 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5757 }
5758 }
5759
Radek Krejciccd20f12019-02-15 14:12:27 +01005760 /* not-supported */
5761 if (devs[u]->not_supported) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005762 if (LY_ARRAY_COUNT(devs[u]->deviates) > 1) {
5763 LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.",
5764 LY_ARRAY_COUNT(devs[u]->deviates), devs[u]->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005765 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005766
5767#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5768 if (devs[u]->target->parent) { \
5769 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5770 } else { \
5771 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5772 } \
5773 LY_ARRAY_FOR(ARRAY, x) { \
5774 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5775 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005776 if (x < LY_ARRAY_COUNT(ARRAY)) { \
Radek Krejcifc11bd72019-04-11 16:00:05 +02005777 FREEFUNC(ctx->ctx, &ARRAY[x]); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005778 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
Radek Krejcifc11bd72019-04-11 16:00:05 +02005779 LY_ARRAY_DECREMENT(ARRAY); \
5780 }
5781
Michal Vasko1bf09392020-03-27 12:38:10 +01005782 if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005783 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5784 /* remove RPC's/action's input */
5785 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5786 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005787 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5788 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005789 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5790 /* remove RPC's/action's output */
5791 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5792 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005793 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5794 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005795 } else {
5796 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005797 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005798 }
5799 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005800 /* remove Notification */
5801 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005802 } else {
5803 /* remove the target node */
5804 lysc_disconnect(devs[u]->target);
5805 lysc_node_free(ctx->ctx, devs[u]->target);
5806 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005807
Radek Krejci474f9b82019-07-24 11:36:37 +02005808 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5809 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005810 continue;
5811 }
5812
5813 /* list of deviates (not-supported is not present in the list) */
5814 LY_ARRAY_FOR(devs[u]->deviates, v) {
5815 d = devs[u]->deviates[v];
5816
5817 switch (d->mod) {
5818 case LYS_DEV_ADD:
5819 d_add = (struct lysp_deviate_add*)d;
5820 /* [units-stmt] */
5821 if (d_add->units) {
5822 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5823 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5824
5825 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5826 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5827 }
5828
5829 /* *must-stmt */
5830 if (d_add->musts) {
5831 switch (devs[u]->target->nodetype) {
5832 case LYS_CONTAINER:
5833 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005834 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5835 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005836 break;
5837 case LYS_LEAF:
5838 case LYS_LEAFLIST:
5839 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005840 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5841 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005842 break;
5843 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005844 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5845 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005846 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005847 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005848 case LYS_ACTION:
5849 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005850 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5851 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005852 break;
Michal Vaskocc048b22020-03-27 15:52:38 +01005853 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005854 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5855 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005856 break;
5857 }
5858 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005859 default:
5860 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005861 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005862 goto cleanup;
5863 }
Michal Vasko004d3152020-06-11 19:59:22 +02005864 ly_set_add(&ctx->xpath, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01005865 }
5866
5867 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005868 if (d_add->uniques) {
5869 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5870 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5871 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005872
5873 /* *default-stmt */
5874 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005875 switch (devs[u]->target->nodetype) {
5876 case LYS_LEAF:
5877 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005878 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 +02005879 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005880 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005881 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005882 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5883 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5884 } else {
5885 /* prepare new default value storage */
5886 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005887 }
Radek Krejcia1911222019-07-22 17:24:50 +02005888 dflt = d_add->dflts[0];
5889 /* parsing is done at the end after possible replace of the leaf's type */
5890
Radek Krejci551b12c2019-02-19 16:11:21 +01005891 /* mark the new default values as leaf's own */
5892 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005893 break;
5894 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005895 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005896 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005897 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005898 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005899 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5900 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5901 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005902 }
Radek Krejcia1911222019-07-22 17:24:50 +02005903 LY_ARRAY_FREE(llist->dflts);
5904 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005905 LY_ARRAY_FREE(llist->dflts_mods);
5906 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005907 }
5908 /* add new default value(s) */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005909 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup);
5910 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup);
5911 for (x = y = LY_ARRAY_COUNT(llist->dflts);
5912 x < LY_ARRAY_COUNT(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005913 LY_ARRAY_INCREMENT(llist->dflts_mods);
5914 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005915 LY_ARRAY_INCREMENT(llist->dflts);
5916 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5917 llist->dflts[x]->realtype = llist->type;
5918 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 +02005919 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005920 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005921 llist->dflts[x]->realtype->refcount++;
5922 if (err) {
5923 ly_err_print(err);
5924 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5925 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5926 d_add->dflts[x - y], err->msg);
5927 ly_err_free(err);
5928 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005929 if (rc == LY_EINCOMPLETE) {
5930 /* postpone default compilation when the tree is complete */
5931 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5932
5933 /* but in general result is so far ok */
5934 rc = LY_SUCCESS;
5935 }
Radek Krejcia1911222019-07-22 17:24:50 +02005936 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005937 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005938 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005939 devs[u]->target->flags |= LYS_SET_DFLT;
5940 break;
5941 case LYS_CHOICE:
5942 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5943 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5944 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5945 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005946 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 +01005947 goto cleanup;
5948 }
5949 break;
5950 default:
5951 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005952 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005953 goto cleanup;
5954 }
5955 }
5956
5957 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005958 if (d_add->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01005959 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005960 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005961 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5962 goto cleanup;
5963 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005964 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005965 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
Michal Vaskoa3881362020-01-21 15:57:35 +01005966 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005967 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005968 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5969 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005970 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5971 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005972 goto cleanup;
5973 }
Radek Krejci327de162019-06-14 12:52:07 +02005974 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005975 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005976
5977 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005978 if (d_add->flags & LYS_MAND_MASK) {
5979 if (devs[u]->target->flags & LYS_MAND_MASK) {
5980 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005981 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5982 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005983 goto cleanup;
5984 }
Radek Krejci327de162019-06-14 12:52:07 +02005985 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005986 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005987
5988 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005989 if (d_add->flags & LYS_SET_MIN) {
5990 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5991 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5992 /* change value */
5993 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5994 } else if (devs[u]->target->nodetype == LYS_LIST) {
5995 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5996 /* change value */
5997 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5998 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005999 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006000 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
6001 goto cleanup;
6002 }
6003 if (d_add->min) {
6004 devs[u]->target->flags |= LYS_MAND_TRUE;
6005 }
6006 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006007
6008 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006009 if (d_add->flags & LYS_SET_MAX) {
6010 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6011 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6012 /* change value */
6013 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6014 } else if (devs[u]->target->nodetype == LYS_LIST) {
6015 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6016 /* change value */
6017 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6018 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006019 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006020 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6021 goto cleanup;
6022 }
6023 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006024
6025 break;
6026 case LYS_DEV_DELETE:
6027 d_del = (struct lysp_deviate_del*)d;
6028
6029 /* [units-stmt] */
6030 if (d_del->units) {
6031 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006032 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6033 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6034 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6035 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6036 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6037 goto cleanup;
6038 }
6039 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6040 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006041 }
6042
6043 /* *must-stmt */
6044 if (d_del->musts) {
6045 switch (devs[u]->target->nodetype) {
6046 case LYS_CONTAINER:
6047 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006048 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006049 break;
6050 case LYS_LEAF:
6051 case LYS_LEAFLIST:
6052 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006053 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006054 break;
6055 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006056 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006057 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006058 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006059 case LYS_ACTION:
6060 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6061 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6062 break;
6063 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6064 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6065 break;
6066 }
6067 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006068 default:
6069 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006070 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006071 goto cleanup;
6072 }
6073 }
6074
6075 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006076 if (d_del->uniques) {
6077 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6078 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6079 LY_ARRAY_FOR(d_del->uniques, x) {
6080 LY_ARRAY_FOR(list->uniques, z) {
6081 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6082 nodeid = strpbrk(name, " \t\n");
6083 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02006084 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006085 break;
6086 }
6087 while (isspace(*nodeid)) {
6088 ++nodeid;
6089 }
6090 } else {
6091 if (strcmp(name, list->uniques[z][y]->name)) {
6092 break;
6093 }
6094 }
6095 }
6096 if (!name) {
6097 /* complete match - remove the unique */
6098 LY_ARRAY_DECREMENT(list->uniques);
6099 LY_ARRAY_FREE(list->uniques[z]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006100 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
Radek Krejci7af64242019-02-18 13:07:53 +01006101 --z;
6102 break;
6103 }
6104 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006105 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006106 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006107 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6108 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006109 goto cleanup;
6110 }
6111 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006112 if (!LY_ARRAY_COUNT(list->uniques)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006113 LY_ARRAY_FREE(list->uniques);
6114 list->uniques = NULL;
6115 }
6116 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006117
6118 /* *default-stmt */
6119 if (d_del->dflts) {
6120 switch (devs[u]->target->nodetype) {
6121 case LYS_LEAF:
6122 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6123 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6124 dflt, "deleting", "default", d_del->dflts[0]);
6125
Radek Krejcia1911222019-07-22 17:24:50 +02006126 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006127 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006128 if (strcmp(dflt, d_del->dflts[0])) {
6129 if (i) {
6130 free((char*)dflt);
6131 }
6132 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6133 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6134 d_del->dflts[0], dflt);
6135 goto cleanup;
6136 }
6137 if (i) {
6138 free((char*)dflt);
6139 }
6140 dflt = NULL;
6141
Radek Krejci474f9b82019-07-24 11:36:37 +02006142 /* update the list of incomplete default values if needed */
6143 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6144
Radek Krejcia1911222019-07-22 17:24:50 +02006145 /* remove the default specification */
6146 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6147 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6148 free(leaf->dflt);
6149 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006150 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006151 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006152 break;
6153 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006154 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6155 LY_ARRAY_FOR(d_del->dflts, x) {
6156 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006157 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 +02006158 if (!strcmp(dflt, d_del->dflts[x])) {
6159 if (i) {
6160 free((char*)dflt);
6161 }
6162 dflt = NULL;
6163 break;
6164 }
6165 if (i) {
6166 free((char*)dflt);
6167 }
6168 dflt = NULL;
6169 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006170 if (y == LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02006171 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6172 "which does not match any of the target's property values.", d_del->dflts[x]);
6173 goto cleanup;
6174 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006175
6176 /* update the list of incomplete default values if needed */
6177 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6178
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006179 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006180 LY_ARRAY_DECREMENT(llist->dflts);
6181 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6182 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6183 free(llist->dflts[y]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006184 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
6185 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 +02006186 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006187 if (!LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006188 LY_ARRAY_FREE(llist->dflts_mods);
6189 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006190 LY_ARRAY_FREE(llist->dflts);
6191 llist->dflts = NULL;
6192 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006193 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006194 break;
6195 case LYS_CHOICE:
6196 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6197 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6198 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006199 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006200 if (prefix) {
6201 /* use module prefixes from the deviation module to match the module of the default case */
6202 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6203 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006204 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6205 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006206 goto cleanup;
6207 }
6208 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6209 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006210 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6211 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006212 goto cleanup;
6213 }
6214 }
6215 /* else {
6216 * strictly, the default prefix would point to the deviation module, but the value should actually
6217 * match the default string in the original module (usually unprefixed), so in this case we do not check
6218 * the module of the default case, just matching its name */
6219 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6220 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006221 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6222 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006223 goto cleanup;
6224 }
6225 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6226 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6227 break;
6228 default:
6229 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006230 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006231 goto cleanup;
6232 }
6233 }
6234
6235 break;
6236 case LYS_DEV_REPLACE:
6237 d_rpl = (struct lysp_deviate_rpl*)d;
6238
6239 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006240 if (d_rpl->type) {
6241 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6242 /* type is mandatory, so checking for its presence is not necessary */
6243 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006244
6245 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6246 /* the target has default from the previous type - remove it */
6247 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006248 /* update the list of incomplete default values if needed */
6249 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6250
Radek Krejcia1911222019-07-22 17:24:50 +02006251 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6252 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6253 free(leaf->dflt);
6254 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006255 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006256 } else { /* LYS_LEAFLIST */
6257 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006258 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006259 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6260 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6261 free(llist->dflts[x]);
6262 }
6263 LY_ARRAY_FREE(llist->dflts);
6264 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006265 LY_ARRAY_FREE(llist->dflts_mods);
6266 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006267 }
6268 }
6269 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006270 /* there is no default value, do not set changed_type after type compilation
6271 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006272 changed_type = -1;
6273 }
Radek Krejciec4da802019-05-02 13:02:41 +02006274 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 +02006275 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006276 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006277
6278 /* [units-stmt] */
6279 if (d_rpl->units) {
6280 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6281 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6282 units, "replacing", "units", d_rpl->units);
6283
6284 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6285 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6286 }
6287
6288 /* [default-stmt] */
6289 if (d_rpl->dflt) {
6290 switch (devs[u]->target->nodetype) {
6291 case LYS_LEAF:
6292 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6293 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006294 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006295 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006296 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6297 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6298 dflt = d_rpl->dflt;
6299 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006300 break;
6301 case LYS_CHOICE:
6302 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006303 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 +01006304 goto cleanup;
6305 }
6306 break;
6307 default:
6308 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006309 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006310 goto cleanup;
6311 }
6312 }
6313
6314 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006315 if (d_rpl->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01006316 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006318 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6319 goto cleanup;
6320 }
6321 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006322 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006323 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6324 goto cleanup;
6325 }
Radek Krejci327de162019-06-14 12:52:07 +02006326 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006327 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006328
6329 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006330 if (d_rpl->flags & LYS_MAND_MASK) {
6331 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006332 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006333 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6334 goto cleanup;
6335 }
Radek Krejci327de162019-06-14 12:52:07 +02006336 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006337 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006338
6339 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006340 if (d_rpl->flags & LYS_SET_MIN) {
6341 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6342 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6343 /* change value */
6344 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6345 } else if (devs[u]->target->nodetype == LYS_LIST) {
6346 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6347 /* change value */
6348 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6349 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006350 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006351 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6352 goto cleanup;
6353 }
6354 if (d_rpl->min) {
6355 devs[u]->target->flags |= LYS_MAND_TRUE;
6356 }
6357 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006358
6359 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006360 if (d_rpl->flags & LYS_SET_MAX) {
6361 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6362 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6363 /* change value */
6364 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6365 } else if (devs[u]->target->nodetype == LYS_LIST) {
6366 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6367 /* change value */
6368 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6369 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006370 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006371 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6372 goto cleanup;
6373 }
6374 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006375
6376 break;
6377 default:
6378 LOGINT(ctx->ctx);
6379 goto cleanup;
6380 }
6381 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006382
Radek Krejci33f72892019-02-21 10:36:58 +01006383 /* final check when all deviations of a single target node are applied */
6384
Radek Krejci551b12c2019-02-19 16:11:21 +01006385 /* check min-max compatibility */
6386 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6387 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6388 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6389 } else if (devs[u]->target->nodetype == LYS_LIST) {
6390 min = ((struct lysc_node_list*)devs[u]->target)->min;
6391 max = ((struct lysc_node_list*)devs[u]->target)->max;
6392 } else {
6393 min = max = 0;
6394 }
6395 if (min > max) {
6396 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 +02006397 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006398 goto cleanup;
6399 }
6400
Radek Krejcia1911222019-07-22 17:24:50 +02006401 if (dflt) {
6402 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006403 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006404 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006405 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6406 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006407 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006408 leaf->dflt->realtype->refcount++;
6409 if (err) {
6410 ly_err_print(err);
6411 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6412 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6413 ly_err_free(err);
6414 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006415 if (rc == LY_EINCOMPLETE) {
6416 /* postpone default compilation when the tree is complete */
6417 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6418
6419 /* but in general result is so far ok */
6420 rc = LY_SUCCESS;
6421 }
Radek Krejcia1911222019-07-22 17:24:50 +02006422 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006423 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006424 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6425 int dynamic;
6426 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006427 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006428
6429 /* update the list of incomplete default values if needed */
6430 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6431
6432 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006433 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6434 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6435 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006436 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6437 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006438 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006439 leaf->dflt->realtype->refcount++;
6440 if (err) {
6441 ly_err_print(err);
6442 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6443 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6444 ly_err_free(err);
6445 }
6446 if (dynamic) {
6447 free((void*)dflt);
6448 }
Radek Krejcia1911222019-07-22 17:24:50 +02006449 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006450 if (rc == LY_EINCOMPLETE) {
6451 /* postpone default compilation when the tree is complete */
6452 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6453
6454 /* but in general result is so far ok */
6455 rc = LY_SUCCESS;
6456 }
6457 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006458 } else { /* LYS_LEAFLIST */
6459 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006460 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 +02006461 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6462 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6463 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006464 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6465 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006466 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6467 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006468 llist->dflts[x]->realtype->refcount++;
6469 if (err) {
6470 ly_err_print(err);
6471 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6472 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6473 dflt, err->msg);
6474 ly_err_free(err);
6475 }
6476 if (dynamic) {
6477 free((void*)dflt);
6478 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006479 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006480 if (rc == LY_EINCOMPLETE) {
6481 /* postpone default compilation when the tree is complete */
6482 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6483
6484 /* but in general result is so far ok */
6485 rc = LY_SUCCESS;
6486 }
Radek Krejcia1911222019-07-22 17:24:50 +02006487 LY_CHECK_GOTO(rc, cleanup);
6488 }
6489 }
6490 }
6491
Radek Krejci551b12c2019-02-19 16:11:21 +01006492 /* check mandatory - default compatibility */
6493 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6494 && (devs[u]->target->flags & LYS_SET_DFLT)
6495 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6496 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006497 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006498 goto cleanup;
6499 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6500 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6501 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006502 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 +01006503 goto cleanup;
6504 }
6505 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6506 /* mandatory node under a default case */
6507 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006508 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6509 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006510 goto cleanup;
6511 }
Radek Krejci33f72892019-02-21 10:36:58 +01006512
Michal Vaskoe6143202020-07-03 13:02:08 +02006513 /* add this module into the target module deviated_by, if not there already */
6514 rc = LY_SUCCESS;
6515 LY_ARRAY_FOR(devs[u]->target->module->compiled->deviated_by, v) {
6516 if (devs[u]->target->module->compiled->deviated_by[v] == mod_p->mod) {
6517 rc = LY_EEXIST;
6518 break;
6519 }
6520 }
6521 if (!rc) {
6522 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup);
6523 *dev_mod = mod_p->mod;
6524 }
Michal Vasko57c10cd2020-05-27 15:57:11 +02006525
Radek Krejci327de162019-06-14 12:52:07 +02006526 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006527 }
6528
Radek Krejci327de162019-06-14 12:52:07 +02006529 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006530 ret = LY_SUCCESS;
6531
6532cleanup:
6533 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6534 LY_ARRAY_FREE(devs[u]->deviates);
6535 free(devs[u]);
6536 }
6537 free(devs);
6538 ly_set_erase(&targets, NULL);
6539 ly_set_erase(&devs_p, NULL);
6540
6541 return ret;
6542}
6543
Radek Krejcib56c7502019-02-13 14:19:54 +01006544/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006545 * @brief Compile the given YANG submodule into the main module.
6546 * @param[in] ctx Compile context
6547 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006548 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6549 */
6550LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006551lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006552{
6553 unsigned int u;
6554 LY_ERR ret = LY_SUCCESS;
6555 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006556 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006557 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006558 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006559
Michal Vasko33ff9422020-07-03 09:50:39 +02006560 if (!mainmod->mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01006561 /* features are compiled directly into the compiled module structure,
6562 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6563 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006564 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6565 LY_CHECK_GOTO(ret, error);
6566 }
Radek Krejci0af46292019-01-11 16:02:31 +01006567
Michal Vasko33ff9422020-07-03 09:50:39 +02006568 if (!mainmod->mod->dis_identities) {
6569 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities);
6570 LY_CHECK_GOTO(ret, error);
6571 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01006572
Radek Krejci474f9b82019-07-24 11:36:37 +02006573 /* data nodes */
6574 LY_LIST_FOR(submod->data, node_p) {
6575 ret = lys_compile_node(ctx, node_p, NULL, 0);
6576 LY_CHECK_GOTO(ret, error);
6577 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006578
Radek Krejciec4da802019-05-02 13:02:41 +02006579 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6580 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006581
Radek Krejcid05cbd92018-12-05 14:26:40 +01006582error:
6583 return ret;
6584}
6585
Radek Krejci335332a2019-09-05 13:03:35 +02006586static void *
6587lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6588{
6589 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6590 if (substmts[u].stmt == stmt) {
6591 return substmts[u].storage;
6592 }
6593 }
6594 return NULL;
6595}
6596
6597LY_ERR
6598lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6599{
6600 LY_ERR ret = LY_EVALID, r;
6601 unsigned int u;
6602 struct lysp_stmt *stmt;
6603 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006604
6605 /* check for invalid substatements */
6606 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006607 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6608 continue;
6609 }
Radek Krejci335332a2019-09-05 13:03:35 +02006610 for (u = 0; substmts[u].stmt; ++u) {
6611 if (substmts[u].stmt == stmt->kw) {
6612 break;
6613 }
6614 }
6615 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006616 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6617 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006618 goto cleanup;
6619 }
Radek Krejci335332a2019-09-05 13:03:35 +02006620 }
6621
Radek Krejciad5963b2019-09-06 16:03:05 +02006622 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6623
Radek Krejci335332a2019-09-05 13:03:35 +02006624 /* keep order of the processing the same as the order in the defined substmts,
6625 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6626 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006627 int stmt_present = 0;
6628
Radek Krejci335332a2019-09-05 13:03:35 +02006629 for (stmt = ext->child; stmt; stmt = stmt->next) {
6630 if (substmts[u].stmt != stmt->kw) {
6631 continue;
6632 }
6633
Radek Krejciad5963b2019-09-06 16:03:05 +02006634 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006635 if (substmts[u].storage) {
6636 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006637 case LY_STMT_STATUS:
6638 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6639 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6640 break;
6641 case LY_STMT_UNITS: {
6642 const char **units;
6643
6644 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6645 /* single item */
6646 if (*((const char **)substmts[u].storage)) {
6647 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6648 goto cleanup;
6649 }
6650 units = (const char **)substmts[u].storage;
6651 } else {
6652 /* sized array */
6653 const char ***units_array = (const char ***)substmts[u].storage;
6654 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6655 }
6656 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6657 break;
6658 }
Radek Krejci335332a2019-09-05 13:03:35 +02006659 case LY_STMT_TYPE: {
6660 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6661 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6662
6663 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6664 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006665 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6667 goto cleanup;
6668 }
6669 compiled = substmts[u].storage;
6670 } else {
6671 /* sized array */
6672 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6673 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6674 compiled = (void*)type;
6675 }
6676
Radek Krejciad5963b2019-09-06 16:03:05 +02006677 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006678 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6679 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006680 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6681 lysp_type_free(ctx->ctx, parsed);
6682 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006683 break;
6684 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006685 case LY_STMT_IF_FEATURE: {
6686 struct lysc_iffeature *iff = NULL;
6687
6688 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6689 /* single item */
6690 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6692 goto cleanup;
6693 }
6694 iff = (struct lysc_iffeature*)substmts[u].storage;
6695 } else {
6696 /* sized array */
6697 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6698 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6699 }
6700 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6701 break;
6702 }
6703 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6704 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006705 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006706 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.",
6707 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006708 goto cleanup;
6709 }
6710 }
Radek Krejci335332a2019-09-05 13:03:35 +02006711 }
Radek Krejci335332a2019-09-05 13:03:35 +02006712
Radek Krejciad5963b2019-09-06 16:03:05 +02006713 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6714 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6715 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6716 goto cleanup;
6717 }
Radek Krejci335332a2019-09-05 13:03:35 +02006718 }
6719
6720 ret = LY_SUCCESS;
6721
6722cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006723 return ret;
6724}
6725
Michal Vasko175012e2019-11-06 15:49:14 +01006726/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006727 * @brief Check when for cyclic dependencies.
6728 * @param[in] set Set with all the referenced nodes.
6729 * @param[in] node Node whose "when" referenced nodes are in @p set.
6730 * @return LY_ERR value
6731 */
6732static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006733lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006734{
6735 struct lyxp_set tmp_set;
6736 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006737 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006738 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006739 int idx;
6740 struct lysc_when *when;
6741 LY_ERR ret = LY_SUCCESS;
6742
6743 memset(&tmp_set, 0, sizeof tmp_set);
6744
6745 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006746 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006747 xp_scnode = &set->val.scnodes[i];
6748
Michal Vasko5c4e5892019-11-14 12:31:38 +01006749 if (xp_scnode->in_ctx != -1) {
6750 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006751 xp_scnode->in_ctx = 1;
6752 }
6753 }
6754
6755 for (i = 0; i < set->used; ++i) {
6756 xp_scnode = &set->val.scnodes[i];
6757 if (xp_scnode->in_ctx != 1) {
6758 /* already checked */
6759 continue;
6760 }
6761
Michal Vasko1bf09392020-03-27 12:38:10 +01006762 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006763 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006764 /* no when to check */
6765 xp_scnode->in_ctx = 0;
6766 continue;
6767 }
6768
6769 node = xp_scnode->scnode;
6770 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006771 LY_ARRAY_FOR(node->when, u) {
6772 when = node->when[u];
Michal Vasko52927e22020-03-16 17:26:14 +01006773 ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006774 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6775 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006776 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006777 goto cleanup;
6778 }
6779
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006780 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006781 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006782 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006783 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006784 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 +01006785 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006786 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 +01006787 ret = LY_EVALID;
6788 goto cleanup;
6789 }
6790
6791 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006792 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006793 } else {
6794 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006795 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006796 }
6797 }
6798
6799 /* merge this set into the global when set */
6800 lyxp_set_scnode_merge(set, &tmp_set);
6801 }
6802
6803 /* check when of non-data parents as well */
6804 node = node->parent;
6805 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6806
Michal Vasko251f56e2019-11-14 16:06:47 +01006807 /* this node when was checked (xp_scnode could have been reallocd) */
6808 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006809 }
6810
6811cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006812 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006813 return ret;
6814}
6815
6816/**
Michal Vasko175012e2019-11-06 15:49:14 +01006817 * @brief Check when/must expressions of a node on a compiled schema tree.
6818 * @param[in] ctx Compile context.
6819 * @param[in] node Node to check.
6820 * @return LY_ERR value
6821 */
6822static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006823lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006824{
Michal Vasko175012e2019-11-06 15:49:14 +01006825 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006826 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006827 LY_ARRAY_COUNT_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01006828 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006829 struct lysc_when **when = NULL;
6830 struct lysc_must *musts = NULL;
6831 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b26e742020-07-17 15:02:10 +02006832 const struct lysc_node *op;
Michal Vasko175012e2019-11-06 15:49:14 +01006833
6834 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006835 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko6b26e742020-07-17 15:02:10 +02006836 if (node->flags & LYS_CONFIG_R) {
6837 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent);
6838 if (op) {
6839 /* we are actually in output */
6840 opts = LYXP_SCNODE_OUTPUT;
6841 }
6842 }
Michal Vasko175012e2019-11-06 15:49:14 +01006843
6844 switch (node->nodetype) {
6845 case LYS_CONTAINER:
6846 when = ((struct lysc_node_container *)node)->when;
6847 musts = ((struct lysc_node_container *)node)->musts;
6848 break;
6849 case LYS_CHOICE:
6850 when = ((struct lysc_node_choice *)node)->when;
6851 break;
6852 case LYS_LEAF:
6853 when = ((struct lysc_node_leaf *)node)->when;
6854 musts = ((struct lysc_node_leaf *)node)->musts;
6855 break;
6856 case LYS_LEAFLIST:
6857 when = ((struct lysc_node_leaflist *)node)->when;
6858 musts = ((struct lysc_node_leaflist *)node)->musts;
6859 break;
6860 case LYS_LIST:
6861 when = ((struct lysc_node_list *)node)->when;
6862 musts = ((struct lysc_node_list *)node)->musts;
6863 break;
6864 case LYS_ANYXML:
6865 case LYS_ANYDATA:
6866 when = ((struct lysc_node_anydata *)node)->when;
6867 musts = ((struct lysc_node_anydata *)node)->musts;
6868 break;
6869 case LYS_CASE:
6870 when = ((struct lysc_node_case *)node)->when;
6871 break;
6872 case LYS_NOTIF:
6873 musts = ((struct lysc_notif *)node)->musts;
6874 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006875 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006876 case LYS_ACTION:
6877 /* first process input musts */
6878 musts = ((struct lysc_action *)node)->input.musts;
6879 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006880 default:
6881 /* nothing to check */
6882 break;
6883 }
6884
Michal Vasko175012e2019-11-06 15:49:14 +01006885 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006886 LY_ARRAY_FOR(when, u) {
Michal Vasko004d3152020-06-11 19:59:22 +02006887 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 +02006888 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006889 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006890 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006891 goto cleanup;
6892 }
6893
Michal Vaskodc052f32019-11-07 11:11:38 +01006894 ctx->path[0] = '\0';
6895 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006896 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006897 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006898 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6899 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006900
Michal Vaskoecd62de2019-11-13 12:35:11 +01006901 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006902 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006903 schema->name);
6904 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006905
6906 /* check dummy node accessing */
6907 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006908 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006909 ret = LY_EVALID;
6910 goto cleanup;
6911 }
Michal Vasko175012e2019-11-06 15:49:14 +01006912 }
6913 }
6914
Michal Vaskoecd62de2019-11-13 12:35:11 +01006915 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006916 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006917 LY_CHECK_GOTO(ret, cleanup);
6918
Michal Vaskod3678892020-05-21 10:06:58 +02006919 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006920 }
6921
Michal Vasko5d8756a2019-11-07 15:21:00 +01006922check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006923 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006924 LY_ARRAY_FOR(musts, u) {
6925 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 +01006926 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006927 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006928 goto cleanup;
6929 }
6930
Michal Vaskodc052f32019-11-07 11:11:38 +01006931 ctx->path[0] = '\0';
6932 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006933 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006934 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006935 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006936 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006937 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6938 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006939 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006940 }
6941 }
6942
Michal Vaskod3678892020-05-21 10:06:58 +02006943 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006944 }
6945
Michal Vasko1bf09392020-03-27 12:38:10 +01006946 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006947 /* now check output musts */
6948 input_done = 1;
6949 musts = ((struct lysc_action *)node)->output.musts;
6950 opts = LYXP_SCNODE_OUTPUT;
6951 goto check_musts;
6952 }
6953
Michal Vasko175012e2019-11-06 15:49:14 +01006954cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006955 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006956 return ret;
6957}
6958
Michal Vasko8d544252020-03-02 10:19:52 +01006959static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006960lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6961{
Michal Vasko6b26e742020-07-17 15:02:10 +02006962 const struct lysc_node *target = NULL, *siter;
Michal Vasko004d3152020-06-11 19:59:22 +02006963 struct ly_path *p;
6964 struct lysc_type *type;
6965
6966 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6967
6968 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02006969 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
6970 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
6971 lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02006972
6973 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006974 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02006975 ly_path_free(node->module->ctx, p);
6976
6977 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
6978 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6979 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
6980 lref->path->expr, lys_nodetype2str(target->nodetype));
6981 return LY_EVALID;
6982 }
6983
6984 /* check status */
6985 ctx->path[0] = '\0';
6986 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6987 ctx->path_len = strlen(ctx->path);
6988 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
6989 return LY_EVALID;
6990 }
6991 ctx->path_len = 1;
6992 ctx->path[1] = '\0';
6993
6994 /* check config */
Michal Vasko6b26e742020-07-17 15:02:10 +02006995 if (lref->require_instance) {
6996 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent);
6997 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006998 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
6999 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
7000 return LY_EVALID;
7001 }
7002 }
7003
7004 /* store the target's type and check for circular chain of leafrefs */
7005 lref->realtype = ((struct lysc_node_leaf *)target)->type;
7006 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
7007 if (type == (struct lysc_type *)lref) {
7008 /* circular chain detected */
7009 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7010 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
7011 return LY_EVALID;
7012 }
7013 }
7014
7015 /* check if leafref and its target are under common if-features */
7016 if (lys_compile_leafref_features_validate(node, target)) {
7017 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7018 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
7019 " features applicable to the leafref itself.", lref->path->expr);
7020 return LY_EVALID;
7021 }
7022
7023 return LY_SUCCESS;
7024}
7025
7026static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01007027lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7028{
7029 struct lysc_ext_instance *ext;
7030 struct lysp_ext_instance *ext_p = NULL;
7031 struct lysp_stmt *stmt;
7032 const struct lys_module *ext_mod;
7033 LY_ERR ret = LY_SUCCESS;
7034
7035 /* create the parsed extension instance manually */
7036 ext_p = calloc(1, sizeof *ext_p);
7037 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
7038 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
7039 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
7040 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7041 ext_p->insubstmt_index = 0;
7042
7043 stmt = calloc(1, sizeof *ext_p->child);
7044 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
7045 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
7046 stmt->kw = LY_STMT_TYPE;
7047 ext_p->child = stmt;
7048
7049 /* allocate new extension instance */
7050 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7051
7052 /* manually get extension definition module */
7053 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7054
7055 /* compile the extension instance */
7056 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7057
7058cleanup:
7059 lysp_ext_instance_free(ctx->ctx, ext_p);
7060 free(ext_p);
7061 return ret;
7062}
7063
Michal Vasko004d3152020-06-11 19:59:22 +02007064static LY_ERR
7065lys_compile_unres(struct lysc_ctx *ctx)
7066{
7067 struct lysc_node *node;
7068 struct lysc_type *type, *typeiter;
7069 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007070 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007071 uint32_t i;
7072
7073 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7074 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7075 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7076 for (i = 0; i < ctx->leafrefs.count; ++i) {
7077 node = ctx->leafrefs.objs[i];
7078 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7079 type = ((struct lysc_node_leaf *)node)->type;
7080 if (type->basetype == LY_TYPE_LEAFREF) {
7081 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7082 } else if (type->basetype == LY_TYPE_UNION) {
7083 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7084 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7085 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7086 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7087 }
7088 }
7089 }
7090 }
7091 for (i = 0; i < ctx->leafrefs.count; ++i) {
7092 /* store pointer to the real type */
7093 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7094 if (type->basetype == LY_TYPE_LEAFREF) {
7095 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7096 typeiter->basetype == LY_TYPE_LEAFREF;
7097 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7098 ((struct lysc_type_leafref*)type)->realtype = typeiter;
7099 } else if (type->basetype == LY_TYPE_UNION) {
7100 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7101 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7102 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7103 typeiter->basetype == LY_TYPE_LEAFREF;
7104 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7105 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7106 }
7107 }
7108 }
7109 }
7110
7111 /* check xpath */
7112 for (i = 0; i < ctx->xpath.count; ++i) {
7113 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7114 }
7115
7116 /* finish incomplete default values compilation */
7117 for (i = 0; i < ctx->dflts.count; ++i) {
7118 struct ly_err_item *err = NULL;
7119 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
7120 LY_ERR ret;
7121 ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
7122 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7123 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7124 if (err) {
7125 ly_err_print(err);
7126 ctx->path[0] = '\0';
7127 lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7128 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7129 "Invalid default - value does not fit the type (%s).", err->msg);
7130 ly_err_free(err);
7131 }
7132 LY_CHECK_RET(ret);
7133 }
7134
7135 return LY_SUCCESS;
7136}
7137
Radek Krejci19a96102018-11-15 13:38:09 +01007138LY_ERR
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007139lys_compile(struct lys_module **mod, int options)
Radek Krejci19a96102018-11-15 13:38:09 +01007140{
7141 struct lysc_ctx ctx = {0};
7142 struct lysc_module *mod_c;
7143 struct lysp_module *sp;
7144 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007145 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007146 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007147 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007148 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007149 uint32_t i;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007150 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007151
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007152 LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007153
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007154 if (!(*mod)->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007155 /* just imported modules are not compiled */
7156 return LY_SUCCESS;
7157 }
7158
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007159 sp = (*mod)->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007160
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007161 ctx.ctx = (*mod)->ctx;
7162 ctx.mod = *mod;
7163 ctx.mod_def = *mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007164 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007165 ctx.path_len = 1;
7166 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007167
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007168 (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
7169 LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
7170 mod_c->mod = *mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007171
Michal Vasko7c8439f2020-08-05 13:25:19 +02007172 LY_ARRAY_FOR(sp->imports, u) {
7173 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
7174 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01007175 LY_ARRAY_FOR(sp->includes, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02007176 LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007177 }
Radek Krejci0935f412019-08-20 16:15:18 +02007178
7179 /* features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007180 if ((*mod)->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007181 /* there is already precompiled array of features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007182 mod_c->features = (*mod)->dis_features;
7183 (*mod)->dis_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007184 } else {
7185 /* features are compiled directly into the compiled module structure,
7186 * 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 +02007187 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007188 LY_CHECK_GOTO(ret, error);
7189 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007190
Radek Krejci0af46292019-01-11 16:02:31 +01007191 /* finish feature compilation, not only for the main module, but also for the submodules.
7192 * Due to possible forward references, it must be done when all the features (including submodules)
7193 * are present. */
7194 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007195 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007196 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7197 }
Radek Krejci327de162019-06-14 12:52:07 +02007198 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007199 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007200 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007201 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007202 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007203 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7204 }
Radek Krejci327de162019-06-14 12:52:07 +02007205 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007206 }
Radek Krejci327de162019-06-14 12:52:07 +02007207 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007208
Michal Vasko33ff9422020-07-03 09:50:39 +02007209 /* identities, work similarly to features with the precompilation */
7210 if ((*mod)->dis_identities) {
7211 mod_c->identities = (*mod)->dis_identities;
7212 (*mod)->dis_identities = NULL;
7213 } else {
7214 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
7215 LY_CHECK_GOTO(ret, error);
7216 }
Radek Krejci19a96102018-11-15 13:38:09 +01007217 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007218 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007219 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007220 lysc_update_path(&ctx, NULL, "{submodule}");
7221 LY_ARRAY_FOR(sp->includes, v) {
7222 if (sp->includes[v].submodule->identities) {
7223 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7224 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities);
7225 LY_CHECK_GOTO(ret, error);
7226 lysc_update_path(&ctx, NULL, NULL);
7227 }
7228 }
Radek Krejci327de162019-06-14 12:52:07 +02007229 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007230
Radek Krejci95710c92019-02-11 15:49:55 +01007231 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007232 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007233 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007234 }
Radek Krejci95710c92019-02-11 15:49:55 +01007235
Radek Krejciec4da802019-05-02 13:02:41 +02007236 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7237 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007238
Radek Krejci95710c92019-02-11 15:49:55 +01007239 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007240 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007241 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007242 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007243 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007244
Radek Krejci474f9b82019-07-24 11:36:37 +02007245 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007246 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007247
Radek Krejci0935f412019-08-20 16:15:18 +02007248 /* extension instances TODO cover extension instances from submodules */
7249 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007250
Michal Vasko004d3152020-06-11 19:59:22 +02007251 /* finish compilation for all unresolved items in the context */
7252 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007253
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007254 /* validate non-instantiated groupings from the parsed schema,
7255 * without it we would accept even the schemas with invalid grouping specification */
7256 ctx.options |= LYSC_OPT_GROUPING;
7257 LY_ARRAY_FOR(sp->groupings, u) {
7258 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007259 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007260 }
7261 }
7262 LY_LIST_FOR(sp->data, node_p) {
7263 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7264 LY_ARRAY_FOR(grps, u) {
7265 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007266 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007267 }
7268 }
7269 }
7270
Radek Krejci474f9b82019-07-24 11:36:37 +02007271 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7272 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7273 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7274 }
7275
Michal Vasko8d544252020-03-02 10:19:52 +01007276#if 0
7277 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7278 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7279 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7280 * the anotation definitions available in the internal schema structure. */
7281 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7282 if (lyp_add_ietf_netconf_annotations(mod)) {
7283 lys_free(mod, NULL, 1, 1);
7284 return NULL;
7285 }
7286 }
7287#endif
7288
7289 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007290 if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
7291 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007292 }
7293
Radek Krejci1c0c3442019-07-23 16:08:47 +02007294 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007295 ly_set_erase(&ctx.xpath, NULL);
7296 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007297 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007298 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007299 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007300
Radek Krejciec4da802019-05-02 13:02:41 +02007301 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007302 lysp_module_free((*mod)->parsed);
7303 (*mod)->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007304 }
7305
Radek Krejciec4da802019-05-02 13:02:41 +02007306 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007307 /* remove flag of the 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];
Radek Krejci95710c92019-02-11 15:49:55 +01007310 if (m->implemented == 2) {
7311 m->implemented = 1;
7312 }
7313 }
7314 }
7315
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007316 (*mod)->compiled = mod_c;
Radek Krejci19a96102018-11-15 13:38:09 +01007317 return LY_SUCCESS;
7318
7319error:
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007320 lys_feature_precompile_revert(&ctx, *mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007321 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007322 ly_set_erase(&ctx.xpath, NULL);
7323 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007324 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007325 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007326 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007327 lysc_module_free(mod_c, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007328 (*mod)->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007329
7330 /* revert compilation of modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007331 for (i = 0; i < ctx.ctx->list.count; ++i) {
7332 m = ctx.ctx->list.objs[i];
Michal Vasko2947ce12019-12-16 10:37:19 +01007333 if ((m->implemented == 2) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007334 /* revert features list to the precompiled state */
7335 lys_feature_precompile_revert(&ctx, m);
7336 /* mark module as imported-only / not-implemented */
7337 m->implemented = 0;
7338 /* free the compiled version of the module */
7339 lysc_module_free(m->compiled, NULL);
7340 m->compiled = NULL;
7341 }
7342 }
7343
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007344 /* remove the module itself from the context and free it */
7345 ly_set_rm(&ctx.ctx->list, *mod, NULL);
7346 lys_module_free(*mod, NULL);
7347 *mod = NULL;
7348
Radek Krejci19a96102018-11-15 13:38:09 +01007349 return ret;
7350}