blob: 41871e39a8c3e38a2db3c5d88abe28ed6bd67146 [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"
Radek Krejcie7b95092019-05-15 11:03:07 +020039#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010040#include "tree_schema_internal.h"
41#include "xpath.h"
42
Michal Vasko5fe75f12020-03-02 13:52:37 +010043static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
44 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
45
Radek Krejci19a96102018-11-15 13:38:09 +010046/**
47 * @brief Duplicate string into dictionary
48 * @param[in] CTX libyang context of the dictionary.
49 * @param[in] ORIG String to duplicate.
50 * @param[out] DUP Where to store the result.
51 */
52#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
53
Radek Krejciec4da802019-05-02 13:02:41 +020054#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010055 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020056 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
57 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
58 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci19a96102018-11-15 13:38:09 +010059 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020060 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010061 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
62 } \
63 }
64
Radek Krejciec4da802019-05-02 13:02:41 +020065#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010066 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020067 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
68 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
69 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010070 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020071 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010072 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
73 } \
74 }
75
Radek Krejci0935f412019-08-20 16:15:18 +020076#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
77 if (EXTS_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020078 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
79 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 +020080 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010081 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 +020082 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
83 } \
84 }
85
Radek Krejciec4da802019-05-02 13:02:41 +020086#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010087 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020088 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
89 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
90 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010091 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020092 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010093 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
94 } \
95 }
96
Radek Krejciec4da802019-05-02 13:02:41 +020097#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010098 if (MEMBER_P) { \
99 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
100 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200101 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100102 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
103 }
104
Radek Krejciec4da802019-05-02 13:02:41 +0200105#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100106 if (MEMBER_P) { \
107 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200108 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100109 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200110 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100111 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
112 }
113
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100114#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100115 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200116 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100117 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100118 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
119 return LY_EVALID; \
120 } \
121 } \
122 }
123
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100124#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
125 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200126 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100127 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
128 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
129 return LY_EVALID; \
130 } \
131 } \
132 }
133
134struct lysc_ext *
135lysc_ext_dup(struct lysc_ext *orig)
136{
137 ++orig->refcount;
138 return orig;
139}
140
Radek Krejci19a96102018-11-15 13:38:09 +0100141static struct lysc_ext_instance *
142lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
143{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100144 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100145 (void) ctx;
146 (void) orig;
147 return NULL;
148}
149
Radek Krejcib56c7502019-02-13 14:19:54 +0100150/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200151 * @brief Add record into the compile context's list of incomplete default values.
152 * @param[in] ctx Compile context with the incomplete default values list.
153 * @param[in] context_node Context schema node to store in the record.
154 * @param[in] dflt Incomplete default value to store in the record.
155 * @param[in] dflt_mod Module of the default value definition to store in the record.
156 * @return LY_EMEM in case of memory allocation failure.
157 * @return LY_SUCCESS
158 */
159static LY_ERR
160lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
161{
162 struct lysc_incomplete_dflt *r;
163 r = malloc(sizeof *r);
164 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
165 r->context_node = context_node;
166 r->dflt = dflt;
167 r->dflt_mod = dflt_mod;
168 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
169
170 return LY_SUCCESS;
171}
172
173/**
174 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
175 * @param[in] ctx Compile context with the incomplete default values list.
176 * @param[in] dflt Incomplete default values identifying the record to remove.
177 */
178static void
179lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
180{
181 unsigned int u;
182 struct lysc_incomplete_dflt *r;
183
184 for (u = 0; u < ctx->dflts.count; ++u) {
185 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
186 if (r->dflt == dflt) {
187 free(ctx->dflts.objs[u]);
188 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
189 --ctx->dflts.count;
190 return;
191 }
192 }
193}
194
Radek Krejci0e59c312019-08-15 15:34:15 +0200195void
Radek Krejci327de162019-06-14 12:52:07 +0200196lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
197{
198 int len;
199 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
200
201 if (!name) {
202 /* removing last path segment */
203 if (ctx->path[ctx->path_len - 1] == '}') {
204 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
205 if (ctx->path[ctx->path_len] == '=') {
206 ctx->path[ctx->path_len++] = '}';
207 } else {
208 /* not a top-level special tag, remove also preceiding '/' */
209 goto remove_nodelevel;
210 }
211 } else {
212remove_nodelevel:
213 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
214 if (ctx->path_len == 0) {
215 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200216 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200217 }
218 }
219 /* set new terminating NULL-byte */
220 ctx->path[ctx->path_len] = '\0';
221 } else {
222 if (ctx->path_len > 1) {
223 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
224 /* extension of the special tag */
225 nextlevel = 2;
226 --ctx->path_len;
227 } else {
228 /* there is already some path, so add next level */
229 nextlevel = 1;
230 }
231 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
232
233 if (nextlevel != 2) {
234 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
235 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200236 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200237 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200238 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 +0200239 }
240 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200241 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200242 }
Radek Krejciacc79042019-07-25 14:14:57 +0200243 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
244 /* output truncated */
245 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
246 } else {
247 ctx->path_len += len;
248 }
Radek Krejci327de162019-06-14 12:52:07 +0200249 }
250}
251
252/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100253 * @brief Duplicate the compiled pattern structure.
254 *
255 * Instead of duplicating memory, the reference counter in the @p orig is increased.
256 *
257 * @param[in] orig The pattern structure to duplicate.
258 * @return The duplicated structure to use.
259 */
Radek Krejci19a96102018-11-15 13:38:09 +0100260static struct lysc_pattern*
261lysc_pattern_dup(struct lysc_pattern *orig)
262{
263 ++orig->refcount;
264 return orig;
265}
266
Radek Krejcib56c7502019-02-13 14:19:54 +0100267/**
268 * @brief Duplicate the array of compiled patterns.
269 *
270 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
271 *
272 * @param[in] ctx Libyang context for logging.
273 * @param[in] orig The patterns sized array to duplicate.
274 * @return New sized array as a copy of @p orig.
275 * @return NULL in case of memory allocation error.
276 */
Radek Krejci19a96102018-11-15 13:38:09 +0100277static struct lysc_pattern**
278lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
279{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100280 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200281 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100282
Radek Krejcib56c7502019-02-13 14:19:54 +0100283 assert(orig);
284
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200285 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100286 LY_ARRAY_FOR(orig, u) {
287 dup[u] = lysc_pattern_dup(orig[u]);
288 LY_ARRAY_INCREMENT(dup);
289 }
290 return dup;
291}
292
Radek Krejcib56c7502019-02-13 14:19:54 +0100293/**
294 * @brief Duplicate compiled range structure.
295 *
296 * @param[in] ctx Libyang context for logging.
297 * @param[in] orig The range structure to be duplicated.
298 * @return New compiled range structure as a copy of @p orig.
299 * @return NULL in case of memory allocation error.
300 */
Radek Krejci19a96102018-11-15 13:38:09 +0100301struct lysc_range*
302lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
303{
304 struct lysc_range *dup;
305 LY_ERR ret;
306
Radek Krejcib56c7502019-02-13 14:19:54 +0100307 assert(orig);
308
Radek Krejci19a96102018-11-15 13:38:09 +0100309 dup = calloc(1, sizeof *dup);
310 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
311 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200312 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
313 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
314 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100315 }
316 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
317 DUP_STRING(ctx, orig->emsg, dup->emsg);
318 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
319
320 return dup;
321cleanup:
322 free(dup);
323 (void) ret; /* set but not used due to the return type */
324 return NULL;
325}
326
Radek Krejcib56c7502019-02-13 14:19:54 +0100327/**
328 * @brief Stack for processing if-feature expressions.
329 */
Radek Krejci19a96102018-11-15 13:38:09 +0100330struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100331 int size; /**< number of items in the stack */
332 int index; /**< first empty item */
333 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100334};
335
Radek Krejcib56c7502019-02-13 14:19:54 +0100336/**
337 * @brief Add @ref ifftokens into the stack.
338 * @param[in] stack The if-feature stack to use.
339 * @param[in] value One of the @ref ifftokens to store in the stack.
340 * @return LY_EMEM in case of memory allocation error
341 * @return LY_ESUCCESS if the value successfully stored.
342 */
Radek Krejci19a96102018-11-15 13:38:09 +0100343static LY_ERR
344iff_stack_push(struct iff_stack *stack, uint8_t value)
345{
346 if (stack->index == stack->size) {
347 stack->size += 4;
348 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
349 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
350 }
351 stack->stack[stack->index++] = value;
352 return LY_SUCCESS;
353}
354
Radek Krejcib56c7502019-02-13 14:19:54 +0100355/**
356 * @brief Get (and remove) the last item form the stack.
357 * @param[in] stack The if-feature stack to use.
358 * @return The value from the top of the stack.
359 */
Radek Krejci19a96102018-11-15 13:38:09 +0100360static uint8_t
361iff_stack_pop(struct iff_stack *stack)
362{
Radek Krejcib56c7502019-02-13 14:19:54 +0100363 assert(stack && stack->index);
364
Radek Krejci19a96102018-11-15 13:38:09 +0100365 stack->index--;
366 return stack->stack[stack->index];
367}
368
Radek Krejcib56c7502019-02-13 14:19:54 +0100369/**
370 * @brief Clean up the stack.
371 * @param[in] stack The if-feature stack to use.
372 */
Radek Krejci19a96102018-11-15 13:38:09 +0100373static void
374iff_stack_clean(struct iff_stack *stack)
375{
376 stack->size = 0;
377 free(stack->stack);
378}
379
Radek Krejcib56c7502019-02-13 14:19:54 +0100380/**
381 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
382 * (libyang format of the if-feature expression).
383 * @param[in,out] list The 2bits array to modify.
384 * @param[in] op The operand (@ref ifftokens) to store.
385 * @param[in] pos Position (0-based) where to store the given @p op.
386 */
Radek Krejci19a96102018-11-15 13:38:09 +0100387static void
388iff_setop(uint8_t *list, uint8_t op, int pos)
389{
390 uint8_t *item;
391 uint8_t mask = 3;
392
393 assert(pos >= 0);
394 assert(op <= 3); /* max 2 bits */
395
396 item = &list[pos / 4];
397 mask = mask << 2 * (pos % 4);
398 *item = (*item) & ~mask;
399 *item = (*item) | (op << 2 * (pos % 4));
400}
401
Radek Krejcib56c7502019-02-13 14:19:54 +0100402#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
403#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100404
Radek Krejci0af46292019-01-11 16:02:31 +0100405/**
406 * @brief Find a feature of the given name and referenced in the given module.
407 *
408 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
409 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
410 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
411 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
412 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
413 * assigned till that time will be still valid.
414 *
415 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
416 * @param[in] name Name of the feature including possible prefix.
417 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
418 * @return Pointer to the feature structure if found, NULL otherwise.
419 */
Radek Krejci19a96102018-11-15 13:38:09 +0100420static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100421lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100422{
423 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200424 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0af46292019-01-11 16:02:31 +0100425 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100426
427 for (i = 0; i < len; ++i) {
428 if (name[i] == ':') {
429 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100430 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100431 LY_CHECK_RET(!mod, NULL);
432
433 name = &name[i + 1];
434 len = len - i - 1;
435 }
436 }
437
438 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100439 if (mod->implemented) {
440 /* module is implemented so there is already the compiled schema */
441 flist = mod->compiled->features;
442 } else {
Michal Vasko33ff9422020-07-03 09:50:39 +0200443 flist = mod->dis_features;
Radek Krejci0af46292019-01-11 16:02:31 +0100444 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200445 LY_ARRAY_FOR(flist, u) {
446 f = &flist[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200447 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100448 return f;
449 }
450 }
451
452 return NULL;
453}
454
Michal Vasko8d544252020-03-02 10:19:52 +0100455/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100456 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
457 */
458static LY_ERR
459lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
460{
461 LY_ERR ret = LY_SUCCESS;
462
463 if (!ext_p->compiled) {
464 lysc_update_path(ctx, NULL, "{extension}");
465 lysc_update_path(ctx, NULL, ext_p->name);
466
467 /* compile the extension definition */
468 ext_p->compiled = calloc(1, sizeof **ext);
469 ext_p->compiled->refcount = 1;
470 DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name);
471 DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument);
472 ext_p->compiled->module = (struct lys_module *)ext_mod;
473 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
474
475 lysc_update_path(ctx, NULL, NULL);
476 lysc_update_path(ctx, NULL, NULL);
477
478 /* find extension definition plugin */
479 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
480 }
481
482 *ext = lysc_ext_dup(ext_p->compiled);
483
484done:
485 return ret;
486}
487
488/**
Michal Vasko8d544252020-03-02 10:19:52 +0100489 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
490 *
491 * @param[in] ctx Compilation context.
492 * @param[in] ext_p Parsed extension instance.
493 * @param[in,out] ext Prepared compiled extension instance.
494 * @param[in] parent Extension instance parent.
495 * @param[in] parent_type Extension instance parent type.
496 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
497 */
Radek Krejci19a96102018-11-15 13:38:09 +0100498static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100499lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
500 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100501{
Radek Krejci7c960162019-09-18 14:16:12 +0200502 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100503 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200504 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200505 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200506 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100507
508 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
509 ext->insubstmt = ext_p->insubstmt;
510 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200511 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200512 ext->parent = parent;
513 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100514
Radek Krejcif56e2a42019-09-09 14:15:25 +0200515 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200516
Radek Krejci19a96102018-11-15 13:38:09 +0100517 /* get module where the extension definition should be placed */
Radek Krejci7c960162019-09-18 14:16:12 +0200518 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u);
519 if (ext_p->yin) {
520 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
521 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
522 * YANG (import) prefix must be done here. */
523 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
524 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
525 u = 0;
526 } else if (ctx->mod_def->compiled) {
Radek Krejci7c960162019-09-18 14:16:12 +0200527 LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) {
528 if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) {
529 char *s;
Radek Krejci200f1062020-07-11 22:51:03 +0200530 LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]) == -1,
531 ret = LY_EMEM, cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200532 prefixed_name = lydict_insert_zc(ctx->ctx, s);
533 u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */
534 break;
535 }
536 }
537 }
538 if (!prefixed_name) {
539 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
540 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
541 goto cleanup;
542 }
543 } else {
544 prefixed_name = ext_p->name;
545 }
546 lysc_update_path(ctx, NULL, prefixed_name);
547
Michal Vasko8d544252020-03-02 10:19:52 +0100548 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200549 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100550 if (!ext_mod) {
551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
552 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
553 goto cleanup;
554 } else if (!ext_mod->parsed->extensions) {
555 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
556 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
557 prefixed_name, ext_mod->name);
558 goto cleanup;
559 }
Radek Krejci7c960162019-09-18 14:16:12 +0200560 }
561 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200562
Michal Vasko5fe75f12020-03-02 13:52:37 +0100563 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200564 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
565 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100566 /* compile extension definition and assign it */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200567 LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def));
Radek Krejci19a96102018-11-15 13:38:09 +0100568 break;
569 }
570 }
Radek Krejci7c960162019-09-18 14:16:12 +0200571 if (!ext->def) {
572 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
573 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
574 goto cleanup;
575 }
Radek Krejci0935f412019-08-20 16:15:18 +0200576
Radek Krejcif56e2a42019-09-09 14:15:25 +0200577 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
578 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
579 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200580 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200581 /* Schema was parsed from YIN and an argument is expected, ... */
582 struct lysp_stmt *stmt = NULL;
583
584 if (ext->def->flags & LYS_YINELEM_TRUE) {
585 /* ... argument was the first XML child element */
586 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
587 /* TODO check namespace of the statement */
588 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
589 stmt = ext_p->child;
590 }
591 }
592 } else {
593 /* ... argument was one of the XML attributes which are represented as child stmt
594 * with LYS_YIN_ATTR flag */
595 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
596 if (!strcmp(stmt->stmt, ext->def->argument)) {
597 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200598 break;
599 }
600 }
601 }
602 if (!stmt) {
603 /* missing extension's argument */
604 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200605 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
606 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200607
608 }
609 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
610 stmt->flags |= LYS_YIN_ARGUMENT;
611 }
Radek Krejci7c960162019-09-18 14:16:12 +0200612 if (prefixed_name != ext_p->name) {
613 lydict_remove(ctx->ctx, ext_p->name);
614 ext_p->name = prefixed_name;
615 if (!ext_p->argument && ext->argument) {
616 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
617 }
618 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200619
Radek Krejci0935f412019-08-20 16:15:18 +0200620 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200621 if (ext->argument) {
622 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
623 }
Radek Krejci7c960162019-09-18 14:16:12 +0200624 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200625 if (ext->argument) {
626 lysc_update_path(ctx, NULL, NULL);
627 }
Radek Krejci0935f412019-08-20 16:15:18 +0200628 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200629 ext_p->compiled = ext;
630
Radek Krejci7c960162019-09-18 14:16:12 +0200631 ret = LY_SUCCESS;
632cleanup:
633 if (prefixed_name && prefixed_name != ext_p->name) {
634 lydict_remove(ctx->ctx, prefixed_name);
635 }
636
Radek Krejcif56e2a42019-09-09 14:15:25 +0200637 lysc_update_path(ctx, NULL, NULL);
638 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200639
Radek Krejci7c960162019-09-18 14:16:12 +0200640 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200641}
642
643/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100644 * @brief Compile information from the if-feature statement
645 * @param[in] ctx Compile context.
646 * @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 +0100647 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
648 * @return LY_ERR value.
649 */
Radek Krejci19a96102018-11-15 13:38:09 +0100650static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200651lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100652{
653 const char *c = *value;
654 int r, rc = EXIT_FAILURE;
655 int i, j, last_not, checkversion = 0;
656 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
657 uint8_t op;
658 struct iff_stack stack = {0, 0, NULL};
659 struct lysc_feature *f;
660
661 assert(c);
662
663 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
664 for (i = j = last_not = 0; c[i]; i++) {
665 if (c[i] == '(') {
666 j++;
667 checkversion = 1;
668 continue;
669 } else if (c[i] == ')') {
670 j--;
671 continue;
672 } else if (isspace(c[i])) {
673 checkversion = 1;
674 continue;
675 }
676
677 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 +0200678 int sp;
679 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
680 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
682 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
683 return LY_EVALID;
684 } else if (!isspace(c[i + r])) {
685 /* feature name starting with the not/and/or */
686 last_not = 0;
687 f_size++;
688 } else if (c[i] == 'n') { /* not operation */
689 if (last_not) {
690 /* double not */
691 expr_size = expr_size - 2;
692 last_not = 0;
693 } else {
694 last_not = 1;
695 }
696 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200697 if (f_exp != f_size) {
698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
699 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
700 return LY_EVALID;
701 }
Radek Krejci19a96102018-11-15 13:38:09 +0100702 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200703
Radek Krejci19a96102018-11-15 13:38:09 +0100704 /* not a not operation */
705 last_not = 0;
706 }
707 i += r;
708 } else {
709 f_size++;
710 last_not = 0;
711 }
712 expr_size++;
713
714 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200715 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100716 i--;
717 break;
718 }
719 i++;
720 }
721 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200722 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100723 /* not matching count of ( and ) */
724 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
725 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
726 return LY_EVALID;
727 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200728 if (f_exp != f_size) {
729 /* features do not match the needed arguments for the logical operations */
730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
731 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
732 "the required number of operands for the operations.", *value);
733 return LY_EVALID;
734 }
Radek Krejci19a96102018-11-15 13:38:09 +0100735
736 if (checkversion || expr_size > 1) {
737 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100738 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100739 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
740 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
741 return LY_EVALID;
742 }
743 }
744
745 /* allocate the memory */
746 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
747 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
748 stack.stack = malloc(expr_size * sizeof *stack.stack);
749 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
750
751 stack.size = expr_size;
752 f_size--; expr_size--; /* used as indexes from now */
753
754 for (i--; i >= 0; i--) {
755 if (c[i] == ')') {
756 /* push it on stack */
757 iff_stack_push(&stack, LYS_IFF_RP);
758 continue;
759 } else if (c[i] == '(') {
760 /* pop from the stack into result all operators until ) */
761 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
762 iff_setop(iff->expr, op, expr_size--);
763 }
764 continue;
765 } else if (isspace(c[i])) {
766 continue;
767 }
768
769 /* end of operator or operand -> find beginning and get what is it */
770 j = i + 1;
771 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
772 i--;
773 }
774 i++; /* go back by one step */
775
776 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
777 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
778 /* double not */
779 iff_stack_pop(&stack);
780 } else {
781 /* not has the highest priority, so do not pop from the stack
782 * as in case of AND and OR */
783 iff_stack_push(&stack, LYS_IFF_NOT);
784 }
785 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
786 /* as for OR - pop from the stack all operators with the same or higher
787 * priority and store them to the result, then push the AND to the stack */
788 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
789 op = iff_stack_pop(&stack);
790 iff_setop(iff->expr, op, expr_size--);
791 }
792 iff_stack_push(&stack, LYS_IFF_AND);
793 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
794 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
795 op = iff_stack_pop(&stack);
796 iff_setop(iff->expr, op, expr_size--);
797 }
798 iff_stack_push(&stack, LYS_IFF_OR);
799 } else {
800 /* feature name, length is j - i */
801
802 /* add it to the expression */
803 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
804
805 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100806 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
807 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
808 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
809 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100810 iff->features[f_size] = f;
811 LY_ARRAY_INCREMENT(iff->features);
812 f_size--;
813 }
814 }
815 while (stack.index) {
816 op = iff_stack_pop(&stack);
817 iff_setop(iff->expr, op, expr_size--);
818 }
819
820 if (++expr_size || ++f_size) {
821 /* not all expected operators and operands found */
822 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
823 "Invalid value \"%s\" of if-feature - processing error.", *value);
824 rc = LY_EINT;
825 } else {
826 rc = LY_SUCCESS;
827 }
828
829error:
830 /* cleanup */
831 iff_stack_clean(&stack);
832
833 return rc;
834}
835
Radek Krejcib56c7502019-02-13 14:19:54 +0100836/**
Michal Vasko175012e2019-11-06 15:49:14 +0100837 * @brief Get the XPath context node for the given schema node.
838 * @param[in] start The schema node where the XPath expression appears.
839 * @return The context node to evaluate XPath expression in given schema node.
840 * @return NULL in case the context node is the root node.
841 */
842static struct lysc_node *
843lysc_xpath_context(struct lysc_node *start)
844{
Michal Vasko1bf09392020-03-27 12:38:10 +0100845 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 +0100846 start = start->parent);
847 return start;
848}
849
850/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100851 * @brief Compile information from the when statement
852 * @param[in] ctx Compile context.
853 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100854 * @param[in] flags Flags of the node with the "when" defiition.
855 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100856 * @param[out] when Pointer where to store pointer to the created compiled when structure.
857 * @return LY_ERR value.
858 */
Radek Krejci19a96102018-11-15 13:38:09 +0100859static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100860lys_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 +0100861{
Radek Krejci19a96102018-11-15 13:38:09 +0100862 LY_ERR ret = LY_SUCCESS;
863
Radek Krejci00b874b2019-02-12 10:54:50 +0100864 *when = calloc(1, sizeof **when);
865 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200866 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200867 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100868 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100869 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
870 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
871 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200872 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100873 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100874
875done:
876 return ret;
877}
878
Radek Krejcib56c7502019-02-13 14:19:54 +0100879/**
880 * @brief Compile information from the must statement
881 * @param[in] ctx Compile context.
882 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100883 * @param[in,out] must Prepared (empty) compiled must structure to fill.
884 * @return LY_ERR value.
885 */
Radek Krejci19a96102018-11-15 13:38:09 +0100886static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200887lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100888{
Radek Krejci19a96102018-11-15 13:38:09 +0100889 LY_ERR ret = LY_SUCCESS;
890
Michal Vasko004d3152020-06-11 19:59:22 +0200891 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100892 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200893 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100894 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
895 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100896 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
897 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200898 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100899
900done:
901 return ret;
902}
903
Radek Krejcib56c7502019-02-13 14:19:54 +0100904/**
905 * @brief Compile information from the import statement
906 * @param[in] ctx Compile context.
907 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100908 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
909 * @return LY_ERR value.
910 */
Radek Krejci19a96102018-11-15 13:38:09 +0100911static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200912lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100913{
Radek Krejci19a96102018-11-15 13:38:09 +0100914 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100915 LY_ERR ret = LY_SUCCESS;
916
917 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200918 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100919 imp->module = imp_p->module;
920
Radek Krejci7f2a5362018-11-28 13:05:37 +0100921 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
922 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100923 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100924 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100925 /* try to use filepath if present */
926 if (imp->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200927 struct ly_in *in;
928 if (ly_in_new_filepath(imp->module->filepath, 0, &in)) {
929 LOGINT(ctx->ctx);
930 } else {
931 mod = lys_parse(ctx->ctx, in, !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
932 if (mod != imp->module) {
933 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", imp->module->filepath, imp->module->name);
934 mod = NULL;
935 }
Radek Krejci19a96102018-11-15 13:38:09 +0100936 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200937 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100938 }
939 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100940 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100941 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100942 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100943 return LY_ENOTFOUND;
944 }
945 }
Radek Krejci19a96102018-11-15 13:38:09 +0100946 }
947
948done:
949 return ret;
950}
951
Michal Vasko33ff9422020-07-03 09:50:39 +0200952LY_ERR
953lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
954 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +0100955{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200956 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +0200957 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +0100958 LY_ERR ret = LY_SUCCESS;
959
Michal Vasko33ff9422020-07-03 09:50:39 +0200960 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +0200961
Michal Vasko33ff9422020-07-03 09:50:39 +0200962 if (!ctx_sc) {
963 context.ctx = ctx;
964 context.mod = module;
965 context.path_len = 1;
966 context.path[0] = '/';
967 ctx_sc = &context;
968 }
Radek Krejci19a96102018-11-15 13:38:09 +0100969
Michal Vasko33ff9422020-07-03 09:50:39 +0200970 if (!identities_p) {
971 return LY_SUCCESS;
972 }
973 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200974 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +0200975 }
976
977 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200978 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +0200979 LY_ARRAY_FOR(identities_p, u) {
980 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
981
982 LY_ARRAY_INCREMENT(*identities);
983 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
984 DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name);
985 DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc);
986 DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref);
987 (*identities)[offset + u].module = ctx_sc->mod;
988 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
989 lys_compile_iffeature, ret, done);
990 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
991 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
992 LYEXT_PAR_IDENT, ret, done);
993 (*identities)[offset + u].flags = identities_p[u].flags;
994
995 lysc_update_path(ctx_sc, NULL, NULL);
996 }
997 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100998done:
999 return ret;
1000}
1001
Radek Krejcib56c7502019-02-13 14:19:54 +01001002/**
1003 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1004 *
1005 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1006 *
1007 * @param[in] ctx Compile context for logging.
1008 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1009 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1010 * @return LY_SUCCESS if everything is ok.
1011 * @return LY_EVALID if the identity is derived from itself.
1012 */
Radek Krejci38222632019-02-12 16:55:05 +01001013static LY_ERR
1014lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1015{
1016 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001017 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001018 struct ly_set recursion = {0};
1019 struct lysc_ident *drv;
1020
1021 if (!derived) {
1022 return LY_SUCCESS;
1023 }
1024
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001025 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001026 if (ident == derived[u]) {
1027 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1028 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1029 goto cleanup;
1030 }
1031 ly_set_add(&recursion, derived[u], 0);
1032 }
1033
1034 for (v = 0; v < recursion.count; ++v) {
1035 drv = recursion.objs[v];
1036 if (!drv->derived) {
1037 continue;
1038 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001039 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001040 if (ident == drv->derived[u]) {
1041 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1042 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1043 goto cleanup;
1044 }
1045 ly_set_add(&recursion, drv->derived[u], 0);
1046 }
1047 }
1048 ret = LY_SUCCESS;
1049
1050cleanup:
1051 ly_set_erase(&recursion, NULL);
1052 return ret;
1053}
1054
Radek Krejcia3045382018-11-22 14:30:31 +01001055/**
1056 * @brief Find and process the referenced base identities from another identity or identityref
1057 *
Radek Krejciaca74032019-06-04 08:53:06 +02001058 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001059 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1060 * to distinguish these two use cases.
1061 *
1062 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1063 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001064 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001065 * @param[in] bases Array of bases of identityref to fill in.
1066 * @return LY_ERR value.
1067 */
Radek Krejci19a96102018-11-15 13:38:09 +01001068static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001069lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
1070 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001071{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001072 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001073 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001074 struct lys_module *mod;
Michal Vasko33ff9422020-07-03 09:50:39 +02001075 struct lysc_ident **idref, *identities;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001076
1077 assert(ident || bases);
1078
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001079 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001080 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1081 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1082 return LY_EVALID;
1083 }
1084
Michal Vasko33ff9422020-07-03 09:50:39 +02001085 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001086 s = strchr(bases_p[u], ':');
1087 if (s) {
1088 /* prefixed identity */
1089 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001090 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001091 } else {
1092 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001093 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001094 }
1095 if (!mod) {
1096 if (ident) {
1097 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1098 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1099 } else {
1100 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1101 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1102 }
1103 return LY_EVALID;
1104 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001105
Radek Krejci555cb5b2018-11-16 14:54:33 +01001106 idref = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02001107 if (mod->compiled) {
1108 identities = mod->compiled->identities;
1109 } else {
1110 identities = mod->dis_identities;
1111 }
1112 LY_ARRAY_FOR(identities, v) {
1113 if (!strcmp(name, identities[v].name)) {
1114 if (ident) {
1115 if (ident == &identities[v]) {
1116 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1117 "Identity \"%s\" is derived from itself.", ident->name);
1118 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001119 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001120 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived));
1121 /* we have match! store the backlink */
1122 LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM);
1123 *idref = ident;
1124 } else {
1125 /* we have match! store the found identity */
1126 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
1127 *idref = &identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001128 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001129 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001130 }
1131 }
1132 if (!idref || !(*idref)) {
1133 if (ident) {
1134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1135 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1136 } else {
1137 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1138 "Unable to find base (%s) of identityref.", bases_p[u]);
1139 }
1140 return LY_EVALID;
1141 }
1142 }
1143 return LY_SUCCESS;
1144}
1145
Radek Krejcia3045382018-11-22 14:30:31 +01001146/**
1147 * @brief For the given array of identities, set the backlinks from all their base identities.
1148 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1149 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1150 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1151 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1152 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001153static LY_ERR
1154lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1155{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001156 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001157
Michal Vasko33ff9422020-07-03 09:50:39 +02001158 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001159 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001160 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001161 continue;
1162 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001163 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001164 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 +02001165 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001166 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001167 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001168 return LY_SUCCESS;
1169}
1170
Radek Krejci0af46292019-01-11 16:02:31 +01001171LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001172lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
1173 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001174{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001175 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001176 struct lysc_ctx context = {0};
1177
Radek Krejci327de162019-06-14 12:52:07 +02001178 assert(ctx_sc || ctx);
1179
1180 if (!ctx_sc) {
1181 context.ctx = ctx;
1182 context.mod = module;
1183 context.path_len = 1;
1184 context.path[0] = '/';
1185 ctx_sc = &context;
1186 }
Radek Krejci0af46292019-01-11 16:02:31 +01001187
1188 if (!features_p) {
1189 return LY_SUCCESS;
1190 }
1191 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001192 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001193 }
1194
Radek Krejci327de162019-06-14 12:52:07 +02001195 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001196 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001197 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001198 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1199
Radek Krejci0af46292019-01-11 16:02:31 +01001200 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001201 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci327de162019-06-14 12:52:07 +02001202 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1203 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1204 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001205 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001206 (*features)[offset + u].module = ctx_sc->mod;
1207
1208 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001209 }
Radek Krejci327de162019-06-14 12:52:07 +02001210 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001211
1212 return LY_SUCCESS;
1213}
1214
Radek Krejcia3045382018-11-22 14:30:31 +01001215/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001216 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001217 *
1218 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1219 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001220 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001221 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1222 * being currently processed).
1223 * @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 +01001224 * @return LY_SUCCESS if everything is ok.
1225 * @return LY_EVALID if the feature references indirectly itself.
1226 */
1227static LY_ERR
1228lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1229{
1230 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001231 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001232 struct ly_set recursion = {0};
1233 struct lysc_feature *drv;
1234
1235 if (!depfeatures) {
1236 return LY_SUCCESS;
1237 }
1238
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001239 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001240 if (feature == depfeatures[u]) {
1241 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1242 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1243 goto cleanup;
1244 }
1245 ly_set_add(&recursion, depfeatures[u], 0);
1246 }
1247
1248 for (v = 0; v < recursion.count; ++v) {
1249 drv = recursion.objs[v];
1250 if (!drv->depfeatures) {
1251 continue;
1252 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001253 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001254 if (feature == drv->depfeatures[u]) {
1255 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1256 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1257 goto cleanup;
1258 }
1259 ly_set_add(&recursion, drv->depfeatures[u], 0);
1260 }
1261 }
1262 ret = LY_SUCCESS;
1263
1264cleanup:
1265 ly_set_erase(&recursion, NULL);
1266 return ret;
1267}
1268
1269/**
Radek Krejci0af46292019-01-11 16:02:31 +01001270 * @brief Create pre-compiled features array.
1271 *
1272 * See lys_feature_precompile() for more details.
1273 *
Radek Krejcia3045382018-11-22 14:30:31 +01001274 * @param[in] ctx Compile context.
1275 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001276 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001277 * @return LY_ERR value.
1278 */
Radek Krejci19a96102018-11-15 13:38:09 +01001279static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001280lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001281{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001282 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001283 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001284 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001285
Radek Krejci327de162019-06-14 12:52:07 +02001286
Radek Krejci0af46292019-01-11 16:02:31 +01001287 /* find the preprecompiled feature */
1288 LY_ARRAY_FOR(features, x) {
1289 if (strcmp(features[x].name, feature_p->name)) {
1290 continue;
1291 }
1292 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001293 lysc_update_path(ctx, NULL, "{feature}");
1294 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001295
Radek Krejci0af46292019-01-11 16:02:31 +01001296 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001297 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001298 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001299 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001300 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001301 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001302 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001303 /* check for circular dependency - direct reference first,... */
1304 if (feature == feature->iffeatures[u].features[v]) {
1305 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1306 "Feature \"%s\" is referenced from itself.", feature->name);
1307 return LY_EVALID;
1308 }
1309 /* ... and indirect circular reference */
1310 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1311
Radek Krejci0af46292019-01-11 16:02:31 +01001312 /* add itself into the dependants list */
1313 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1314 *df = feature;
1315 }
Radek Krejci19a96102018-11-15 13:38:09 +01001316 }
Radek Krejci19a96102018-11-15 13:38:09 +01001317 }
1318 }
Radek Krejci327de162019-06-14 12:52:07 +02001319 lysc_update_path(ctx, NULL, NULL);
1320 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001321 done:
1322 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001323 }
Radek Krejci0af46292019-01-11 16:02:31 +01001324
1325 LOGINT(ctx->ctx);
1326 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001327}
1328
Radek Krejcib56c7502019-02-13 14:19:54 +01001329/**
1330 * @brief Revert compiled list of features back to the precompiled state.
1331 *
1332 * 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 +02001333 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001334 *
1335 * @param[in] ctx Compilation context.
1336 * @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 +02001337 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001338 */
Radek Krejci95710c92019-02-11 15:49:55 +01001339static void
1340lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1341{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001342 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001343
Michal Vasko33ff9422020-07-03 09:50:39 +02001344 /* keep the dis_features list until the complete lys_module is freed */
1345 mod->dis_features = mod->compiled->features;
Radek Krejci95710c92019-02-11 15:49:55 +01001346 mod->compiled->features = NULL;
1347
Michal Vasko33ff9422020-07-03 09:50:39 +02001348 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001349 * which may points into the data being freed here */
Michal Vasko33ff9422020-07-03 09:50:39 +02001350 LY_ARRAY_FOR(mod->dis_features, u) {
1351 LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) {
1352 lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001353 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001354 LY_ARRAY_FREE(mod->dis_features[u].iffeatures);
1355 mod->dis_features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001356
Michal Vasko33ff9422020-07-03 09:50:39 +02001357 LY_ARRAY_FOR(mod->dis_features[u].exts, v) {
1358 lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001359 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001360 LY_ARRAY_FREE(mod->dis_features[u].exts);
1361 mod->dis_features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001362 }
1363}
1364
Radek Krejcia3045382018-11-22 14:30:31 +01001365/**
1366 * @brief Validate and normalize numeric value from a range definition.
1367 * @param[in] ctx Compile context.
1368 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1369 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1370 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1371 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1372 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1373 * @param[in] value String value of the range boundary.
1374 * @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.
1375 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1376 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1377 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001378LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001379range_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 +01001380{
Radek Krejci6cba4292018-11-15 17:33:29 +01001381 size_t fraction = 0, size;
1382
Radek Krejci19a96102018-11-15 13:38:09 +01001383 *len = 0;
1384
1385 assert(value);
1386 /* parse value */
1387 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1388 return LY_EVALID;
1389 }
1390
1391 if ((value[*len] == '-') || (value[*len] == '+')) {
1392 ++(*len);
1393 }
1394
1395 while (isdigit(value[*len])) {
1396 ++(*len);
1397 }
1398
1399 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001400 if (basetype == LY_TYPE_DEC64) {
1401 goto decimal;
1402 } else {
1403 *valcopy = strndup(value, *len);
1404 return LY_SUCCESS;
1405 }
Radek Krejci19a96102018-11-15 13:38:09 +01001406 }
1407 fraction = *len;
1408
1409 ++(*len);
1410 while (isdigit(value[*len])) {
1411 ++(*len);
1412 }
1413
Radek Krejci6cba4292018-11-15 17:33:29 +01001414 if (basetype == LY_TYPE_DEC64) {
1415decimal:
1416 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001417 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001418 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1419 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1420 *len, value, frdigits);
1421 return LY_EINVAL;
1422 }
1423 if (fraction) {
1424 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1425 } else {
1426 size = (*len) + frdigits + 1;
1427 }
1428 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001429 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1430
Radek Krejci6cba4292018-11-15 17:33:29 +01001431 (*valcopy)[size - 1] = '\0';
1432 if (fraction) {
1433 memcpy(&(*valcopy)[0], &value[0], fraction);
1434 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1435 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1436 } else {
1437 memcpy(&(*valcopy)[0], &value[0], *len);
1438 memset(&(*valcopy)[*len], '0', frdigits);
1439 }
Radek Krejci19a96102018-11-15 13:38:09 +01001440 }
1441 return LY_SUCCESS;
1442}
1443
Radek Krejcia3045382018-11-22 14:30:31 +01001444/**
1445 * @brief Check that values in range are in ascendant order.
1446 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001447 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1448 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001449 * @param[in] value Current value to check.
1450 * @param[in] prev_value The last seen value.
1451 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1452 */
Radek Krejci19a96102018-11-15 13:38:09 +01001453static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001454range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001455{
1456 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001457 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001458 return LY_EEXIST;
1459 }
1460 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001461 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001462 return LY_EEXIST;
1463 }
1464 }
1465 return LY_SUCCESS;
1466}
1467
Radek Krejcia3045382018-11-22 14:30:31 +01001468/**
1469 * @brief Set min/max value of the range part.
1470 * @param[in] ctx Compile context.
1471 * @param[in] part Range part structure to fill.
1472 * @param[in] max Flag to distinguish if storing min or max value.
1473 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1474 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1475 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1476 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1477 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001478 * @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 +01001479 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1480 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1481 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1482 * frdigits value), LY_EMEM.
1483 */
Radek Krejci19a96102018-11-15 13:38:09 +01001484static LY_ERR
1485range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr,
Radek Krejci5969f272018-11-23 10:03:58 +01001486 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001487{
1488 LY_ERR ret = LY_SUCCESS;
1489 char *valcopy = NULL;
1490 size_t len;
1491
1492 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001493 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001494 LY_CHECK_GOTO(ret, finalize);
1495 }
1496 if (!valcopy && base_range) {
1497 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001498 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001499 } else {
1500 part->min_64 = base_range->parts[0].min_64;
1501 }
1502 if (!first) {
1503 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1504 }
1505 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001506 }
1507
1508 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001509 case LY_TYPE_INT8: /* range */
1510 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001511 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 +01001512 } else if (max) {
1513 part->max_64 = INT64_C(127);
1514 } else {
1515 part->min_64 = INT64_C(-128);
1516 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001517 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001518 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001519 }
1520 break;
1521 case LY_TYPE_INT16: /* range */
1522 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001523 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 +01001524 } else if (max) {
1525 part->max_64 = INT64_C(32767);
1526 } else {
1527 part->min_64 = INT64_C(-32768);
1528 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001529 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001530 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001531 }
1532 break;
1533 case LY_TYPE_INT32: /* range */
1534 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001535 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 +01001536 } else if (max) {
1537 part->max_64 = INT64_C(2147483647);
1538 } else {
1539 part->min_64 = INT64_C(-2147483648);
1540 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001541 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001542 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001543 }
1544 break;
1545 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001546 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001547 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001548 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001549 max ? &part->max_64 : &part->min_64);
1550 } else if (max) {
1551 part->max_64 = INT64_C(9223372036854775807);
1552 } else {
1553 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1554 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001555 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001556 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001557 }
1558 break;
1559 case LY_TYPE_UINT8: /* range */
1560 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001561 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 +01001562 } else if (max) {
1563 part->max_u64 = UINT64_C(255);
1564 } else {
1565 part->min_u64 = UINT64_C(0);
1566 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001567 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001568 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001569 }
1570 break;
1571 case LY_TYPE_UINT16: /* range */
1572 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001573 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 +01001574 } else if (max) {
1575 part->max_u64 = UINT64_C(65535);
1576 } else {
1577 part->min_u64 = UINT64_C(0);
1578 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001579 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001580 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001581 }
1582 break;
1583 case LY_TYPE_UINT32: /* range */
1584 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001585 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 +01001586 } else if (max) {
1587 part->max_u64 = UINT64_C(4294967295);
1588 } else {
1589 part->min_u64 = UINT64_C(0);
1590 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001591 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001592 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001593 }
1594 break;
1595 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001596 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001597 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001598 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001599 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 +01001600 } else if (max) {
1601 part->max_u64 = UINT64_C(18446744073709551615);
1602 } else {
1603 part->min_u64 = UINT64_C(0);
1604 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001605 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001606 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001607 }
1608 break;
1609 default:
1610 LOGINT(ctx->ctx);
1611 ret = LY_EINT;
1612 }
1613
Radek Krejci5969f272018-11-23 10:03:58 +01001614finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001615 if (ret == LY_EDENIED) {
1616 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1617 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1618 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1619 } else if (ret == LY_EVALID) {
1620 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1621 "Invalid %s restriction - invalid value \"%s\".",
1622 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1623 } else if (ret == LY_EEXIST) {
1624 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1625 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001626 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001627 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001628 } else if (!ret && value) {
1629 *value = *value + len;
1630 }
1631 free(valcopy);
1632 return ret;
1633}
1634
Radek Krejcia3045382018-11-22 14:30:31 +01001635/**
1636 * @brief Compile the parsed range restriction.
1637 * @param[in] ctx Compile context.
1638 * @param[in] range_p Parsed range structure to compile.
1639 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1640 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1641 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1642 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1643 * range restriction must be more restrictive than the base_range.
1644 * @param[in,out] range Pointer to the created current range structure.
1645 * @return LY_ERR value.
1646 */
Radek Krejci19a96102018-11-15 13:38:09 +01001647static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001648lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits,
Radek Krejci19a96102018-11-15 13:38:09 +01001649 struct lysc_range *base_range, struct lysc_range **range)
1650{
1651 LY_ERR ret = LY_EVALID;
1652 const char *expr;
1653 struct lysc_range_part *parts = NULL, *part;
1654 int range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001655 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001656
1657 assert(range);
1658 assert(range_p);
1659
1660 expr = range_p->arg;
1661 while(1) {
1662 if (isspace(*expr)) {
1663 ++expr;
1664 } else if (*expr == '\0') {
1665 if (range_expected) {
1666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1667 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1668 length_restr ? "length" : "range", range_p->arg);
1669 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001670 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1672 "Invalid %s restriction - unexpected end of the expression (%s).",
1673 length_restr ? "length" : "range", range_p->arg);
1674 goto cleanup;
1675 }
1676 parts_done++;
1677 break;
1678 } else if (!strncmp(expr, "min", 3)) {
1679 if (parts) {
1680 /* min cannot be used elsewhere than in the first part */
1681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1682 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1683 expr - range_p->arg, range_p->arg);
1684 goto cleanup;
1685 }
1686 expr += 3;
1687
1688 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001689 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 +01001690 part->max_64 = part->min_64;
1691 } else if (*expr == '|') {
1692 if (!parts || range_expected) {
1693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1694 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1695 goto cleanup;
1696 }
1697 expr++;
1698 parts_done++;
1699 /* process next part of the expression */
1700 } else if (!strncmp(expr, "..", 2)) {
1701 expr += 2;
1702 while (isspace(*expr)) {
1703 expr++;
1704 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001705 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1707 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1708 goto cleanup;
1709 }
1710 /* continue expecting the upper boundary */
1711 range_expected = 1;
1712 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1713 /* number */
1714 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001715 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001716 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 +01001717 range_expected = 0;
1718 } else {
1719 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001720 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 +01001721 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001722 part->max_64 = part->min_64;
1723 }
1724
1725 /* continue with possible another expression part */
1726 } else if (!strncmp(expr, "max", 3)) {
1727 expr += 3;
1728 while (isspace(*expr)) {
1729 expr++;
1730 }
1731 if (*expr != '\0') {
1732 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1733 length_restr ? "length" : "range", expr);
1734 goto cleanup;
1735 }
1736 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001737 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001738 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 +01001739 range_expected = 0;
1740 } else {
1741 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001742 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 +01001743 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001744 part->min_64 = part->max_64;
1745 }
1746 } else {
1747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1748 length_restr ? "length" : "range", expr);
1749 goto cleanup;
1750 }
1751 }
1752
1753 /* check with the previous range/length restriction */
1754 if (base_range) {
1755 switch (basetype) {
1756 case LY_TYPE_BINARY:
1757 case LY_TYPE_UINT8:
1758 case LY_TYPE_UINT16:
1759 case LY_TYPE_UINT32:
1760 case LY_TYPE_UINT64:
1761 case LY_TYPE_STRING:
1762 uns = 1;
1763 break;
1764 case LY_TYPE_DEC64:
1765 case LY_TYPE_INT8:
1766 case LY_TYPE_INT16:
1767 case LY_TYPE_INT32:
1768 case LY_TYPE_INT64:
1769 uns = 0;
1770 break;
1771 default:
1772 LOGINT(ctx->ctx);
1773 ret = LY_EINT;
1774 goto cleanup;
1775 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001776 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001777 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1778 goto baseerror;
1779 }
1780 /* current lower bound is not lower than the base */
1781 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1782 /* base has single value */
1783 if (base_range->parts[v].min_64 == parts[u].min_64) {
1784 /* both lower bounds are the same */
1785 if (parts[u].min_64 != parts[u].max_64) {
1786 /* current continues with a range */
1787 goto baseerror;
1788 } else {
1789 /* equal single values, move both forward */
1790 ++v;
1791 continue;
1792 }
1793 } else {
1794 /* base is single value lower than current range, so the
1795 * value from base range is removed in the current,
1796 * move only base and repeat checking */
1797 ++v;
1798 --u;
1799 continue;
1800 }
1801 } else {
1802 /* base is the range */
1803 if (parts[u].min_64 == parts[u].max_64) {
1804 /* current is a single value */
1805 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1806 /* current is behind the base range, so base range is omitted,
1807 * move the base and keep the current for further check */
1808 ++v;
1809 --u;
1810 } /* else it is within the base range, so move the current, but keep the base */
1811 continue;
1812 } else {
1813 /* both are ranges - check the higher bound, the lower was already checked */
1814 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1815 /* higher bound is higher than the current higher bound */
1816 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1817 /* but the current lower bound is also higher, so the base range is omitted,
1818 * continue with the same current, but move the base */
1819 --u;
1820 ++v;
1821 continue;
1822 }
1823 /* current range starts within the base range but end behind it */
1824 goto baseerror;
1825 } else {
1826 /* current range is smaller than the base,
1827 * move current, but stay with the base */
1828 continue;
1829 }
1830 }
1831 }
1832 }
1833 if (u != parts_done) {
1834baseerror:
1835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1836 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1837 length_restr ? "length" : "range", range_p->arg);
1838 goto cleanup;
1839 }
1840 }
1841
1842 if (!(*range)) {
1843 *range = calloc(1, sizeof **range);
1844 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1845 }
1846
Radek Krejcic8b31002019-01-08 10:24:45 +01001847 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001848 if (range_p->eapptag) {
1849 lydict_remove(ctx->ctx, (*range)->eapptag);
1850 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1851 }
1852 if (range_p->emsg) {
1853 lydict_remove(ctx->ctx, (*range)->emsg);
1854 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1855 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001856 if (range_p->dsc) {
1857 lydict_remove(ctx->ctx, (*range)->dsc);
1858 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1859 }
1860 if (range_p->ref) {
1861 lydict_remove(ctx->ctx, (*range)->ref);
1862 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1863 }
Radek Krejci19a96102018-11-15 13:38:09 +01001864 /* extensions are taken only from the last range by the caller */
1865
1866 (*range)->parts = parts;
1867 parts = NULL;
1868 ret = LY_SUCCESS;
1869cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001870 LY_ARRAY_FREE(parts);
1871
1872 return ret;
1873}
1874
1875/**
1876 * @brief Checks pattern syntax.
1877 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001878 * @param[in] ctx Context.
1879 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001880 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001881 * @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 +01001882 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001883 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001884LY_ERR
1885lys_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 +01001886{
Michal Vasko40a00082020-05-27 15:20:01 +02001887 int idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001888 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001889 int err_code;
1890 const char *orig_ptr;
1891 PCRE2_SIZE err_offset;
1892 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001893#define URANGE_LEN 19
1894 char *ublock2urange[][2] = {
1895 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1896 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1897 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1898 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1899 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1900 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1901 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1902 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1903 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1904 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1905 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1906 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1907 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1908 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1909 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1910 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1911 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1912 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1913 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1914 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1915 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1916 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1917 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1918 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1919 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1920 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1921 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1922 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1923 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1924 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1925 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1926 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1927 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1928 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1929 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1930 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1931 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1932 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1933 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1934 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1935 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1936 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1937 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1938 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1939 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1940 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1941 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1942 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1943 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1944 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1945 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1946 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1947 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1948 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1949 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1950 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1951 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1952 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1953 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1954 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1955 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1956 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1957 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1958 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1959 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1960 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1961 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1962 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1963 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1964 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1965 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1966 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1967 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1968 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1969 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1970 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1971 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1972 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1973 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1974 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1975 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1976 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1977 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1978 {NULL, NULL}
1979 };
1980
1981 /* adjust the expression to a Perl equivalent
1982 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1983
Michal Vasko40a00082020-05-27 15:20:01 +02001984 /* allocate space for the transformed pattern */
1985 size = strlen(pattern) + 1;
1986 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001988 perl_regex[0] = '\0';
1989
Michal Vasko40a00082020-05-27 15:20:01 +02001990 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1991 brack = 0;
1992 idx = 0;
1993 orig_ptr = pattern;
1994 while (orig_ptr[0]) {
1995 switch (orig_ptr[0]) {
1996 case '$':
1997 case '^':
1998 if (!brack) {
1999 /* make space for the extra character */
2000 ++size;
2001 perl_regex = ly_realloc(perl_regex, size);
2002 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002003
Michal Vasko40a00082020-05-27 15:20:01 +02002004 /* print escape slash */
2005 perl_regex[idx] = '\\';
2006 ++idx;
2007 }
2008 break;
2009 case '[':
2010 /* must not be escaped */
2011 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2012 ++brack;
2013 }
2014 break;
2015 case ']':
2016 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2017 /* pattern was checked and compiled already */
2018 assert(brack);
2019 --brack;
2020 }
2021 break;
2022 default:
2023 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002024 }
Michal Vasko40a00082020-05-27 15:20:01 +02002025
2026 /* copy char */
2027 perl_regex[idx] = orig_ptr[0];
2028
2029 ++idx;
2030 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002031 }
Michal Vasko40a00082020-05-27 15:20:01 +02002032 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002033
2034 /* substitute Unicode Character Blocks with exact Character Ranges */
2035 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2036 start = ptr - perl_regex;
2037
2038 ptr = strchr(ptr, '}');
2039 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002041 pattern, perl_regex + start + 2, "unterminated character property");
2042 free(perl_regex);
2043 return LY_EVALID;
2044 }
2045 end = (ptr - perl_regex) + 1;
2046
2047 /* need more space */
2048 if (end - start < URANGE_LEN) {
2049 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002050 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002051 }
2052
2053 /* find our range */
2054 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2055 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2056 break;
2057 }
2058 }
2059 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002061 pattern, perl_regex + start + 5, "unknown block name");
2062 free(perl_regex);
2063 return LY_EVALID;
2064 }
2065
2066 /* 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 +02002067 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002068 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002069 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002070 }
2071 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002072 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002073 }
2074 }
Michal Vasko40a00082020-05-27 15:20:01 +02002075 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002076 /* skip brackets */
2077 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2078 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2079 } else {
2080 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2081 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2082 }
2083 }
2084
2085 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002086 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2087 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002088 &err_code, &err_offset, NULL);
2089 if (!code_local) {
2090 PCRE2_UCHAR err_msg[256] = {0};
2091 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002093 free(perl_regex);
2094 return LY_EVALID;
2095 }
2096 free(perl_regex);
2097
Radek Krejci54579462019-04-30 12:47:06 +02002098 if (code) {
2099 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002100 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002101 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002102 }
2103
2104 return LY_SUCCESS;
2105
2106#undef URANGE_LEN
2107}
2108
Radek Krejcia3045382018-11-22 14:30:31 +01002109/**
2110 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2111 * @param[in] ctx Compile context.
2112 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002113 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2114 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2115 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2116 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2117 */
Radek Krejci19a96102018-11-15 13:38:09 +01002118static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002119lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002120 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2121{
2122 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002123 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002124 LY_ERR ret = LY_SUCCESS;
2125
2126 /* first, copy the patterns from the base type */
2127 if (base_patterns) {
2128 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2129 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2130 }
2131
2132 LY_ARRAY_FOR(patterns_p, u) {
2133 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2134 *pattern = calloc(1, sizeof **pattern);
2135 ++(*pattern)->refcount;
2136
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002138 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002139
2140 if (patterns_p[u].arg[0] == 0x15) {
2141 (*pattern)->inverted = 1;
2142 }
Radek Krejci54579462019-04-30 12:47:06 +02002143 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002144 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2145 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002146 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2147 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002148 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002149 }
2150done:
2151 return ret;
2152}
2153
Radek Krejcia3045382018-11-22 14:30:31 +01002154/**
2155 * @brief map of the possible restrictions combination for the specific built-in type.
2156 */
Radek Krejci19a96102018-11-15 13:38:09 +01002157static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2158 0 /* LY_TYPE_UNKNOWN */,
2159 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002160 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2161 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2162 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2163 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2164 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002165 LYS_SET_BIT /* LY_TYPE_BITS */,
2166 0 /* LY_TYPE_BOOL */,
2167 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2168 0 /* LY_TYPE_EMPTY */,
2169 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2170 LYS_SET_BASE /* LY_TYPE_IDENT */,
2171 LYS_SET_REQINST /* LY_TYPE_INST */,
2172 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002173 LYS_SET_TYPE /* LY_TYPE_UNION */,
2174 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002175 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002176 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002177 LYS_SET_RANGE /* LY_TYPE_INT64 */
2178};
2179
2180/**
2181 * @brief stringification of the YANG built-in data types
2182 */
2183const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2184 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2185 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002186};
2187
Radek Krejcia3045382018-11-22 14:30:31 +01002188/**
2189 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2190 * @param[in] ctx Compile context.
2191 * @param[in] enums_p Array of the parsed enum structures to compile.
2192 * @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 +01002193 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2194 * @param[out] enums Newly created array of the compiled enums information for the current type.
2195 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2196 */
Radek Krejci19a96102018-11-15 13:38:09 +01002197static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002198lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002199 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002200{
2201 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002202 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002203 int32_t value = 0;
2204 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002205 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002206
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002207 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002208 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2209 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2210 return LY_EVALID;
2211 }
2212
2213 LY_ARRAY_FOR(enums_p, u) {
2214 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2215 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002216 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2217 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002218 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002219 if (base_enums) {
2220 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2221 LY_ARRAY_FOR(base_enums, v) {
2222 if (!strcmp(e->name, base_enums[v].name)) {
2223 break;
2224 }
2225 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002226 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002227 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2228 "Invalid %s - derived type adds new item \"%s\".",
2229 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2230 return LY_EVALID;
2231 }
2232 match = v;
2233 }
2234
2235 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002236 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002237 if (enums_p[u].flags & LYS_SET_VALUE) {
2238 e->value = (int32_t)enums_p[u].value;
2239 if (!u || e->value >= value) {
2240 value = e->value + 1;
2241 }
2242 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002243 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002244 if (e->value == (*enums)[v].value) {
2245 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2246 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2247 e->value, e->name, (*enums)[v].name);
2248 return LY_EVALID;
2249 }
2250 }
2251 } else if (base_enums) {
2252 /* inherit the assigned value */
2253 e->value = base_enums[match].value;
2254 if (!u || e->value >= value) {
2255 value = e->value + 1;
2256 }
2257 } else {
2258 /* assign value automatically */
2259 if (u && value == INT32_MIN) {
2260 /* counter overflow */
2261 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2262 "Invalid enumeration - it is not possible to auto-assign enum value for "
2263 "\"%s\" since the highest value is already 2147483647.", e->name);
2264 return LY_EVALID;
2265 }
2266 e->value = value++;
2267 }
2268 } else { /* LY_TYPE_BITS */
2269 if (enums_p[u].flags & LYS_SET_VALUE) {
2270 e->value = (int32_t)enums_p[u].value;
2271 if (!u || (uint32_t)e->value >= position) {
2272 position = (uint32_t)e->value + 1;
2273 }
2274 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002275 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002276 if (e->value == (*enums)[v].value) {
2277 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2278 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2279 (uint32_t)e->value, e->name, (*enums)[v].name);
2280 return LY_EVALID;
2281 }
2282 }
2283 } else if (base_enums) {
2284 /* inherit the assigned value */
2285 e->value = base_enums[match].value;
2286 if (!u || (uint32_t)e->value >= position) {
2287 position = (uint32_t)e->value + 1;
2288 }
2289 } else {
2290 /* assign value automatically */
2291 if (u && position == 0) {
2292 /* counter overflow */
2293 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2294 "Invalid bits - it is not possible to auto-assign bit position for "
2295 "\"%s\" since the highest value is already 4294967295.", e->name);
2296 return LY_EVALID;
2297 }
2298 e->value = position++;
2299 }
2300 }
2301
2302 if (base_enums) {
2303 /* the assigned values must not change from the derived type */
2304 if (e->value != base_enums[match].value) {
2305 if (basetype == LY_TYPE_ENUM) {
2306 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2307 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2308 e->name, base_enums[match].value, e->value);
2309 } else {
2310 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2311 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2312 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2313 }
2314 return LY_EVALID;
2315 }
2316 }
2317
Radek Krejciec4da802019-05-02 13:02:41 +02002318 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002319 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 +01002320
2321 if (basetype == LY_TYPE_BITS) {
2322 /* keep bits ordered by position */
2323 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2324 if (v != u) {
2325 memcpy(&storage, e, sizeof *e);
2326 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2327 memcpy(&(*enums)[v], &storage, sizeof storage);
2328 }
2329 }
2330 }
2331
2332done:
2333 return ret;
2334}
2335
Radek Krejcia3045382018-11-22 14:30:31 +01002336/**
2337 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2338 *
2339 * path-arg = absolute-path / relative-path
2340 * absolute-path = 1*("/" (node-identifier *path-predicate))
2341 * relative-path = 1*(".." "/") descendant-path
2342 *
2343 * @param[in,out] path Path to parse.
2344 * @param[out] prefix Prefix of the token, NULL if there is not any.
2345 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2346 * @param[out] name Name of the token.
2347 * @param[out] nam_len Length of the name.
2348 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2349 * must not be changed between consecutive calls. -1 if the
2350 * path is absolute.
2351 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2352 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2353 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002354LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002355lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2356 int *parent_times, int *has_predicate)
2357{
2358 int par_times = 0;
2359
2360 assert(path && *path);
2361 assert(parent_times);
2362 assert(prefix);
2363 assert(prefix_len);
2364 assert(name);
2365 assert(name_len);
2366 assert(has_predicate);
2367
2368 *prefix = NULL;
2369 *prefix_len = 0;
2370 *name = NULL;
2371 *name_len = 0;
2372 *has_predicate = 0;
2373
2374 if (!*parent_times) {
2375 if (!strncmp(*path, "..", 2)) {
2376 *path += 2;
2377 ++par_times;
2378 while (!strncmp(*path, "/..", 3)) {
2379 *path += 3;
2380 ++par_times;
2381 }
2382 }
2383 if (par_times) {
2384 *parent_times = par_times;
2385 } else {
2386 *parent_times = -1;
2387 }
2388 }
2389
2390 if (**path != '/') {
2391 return LY_EINVAL;
2392 }
2393 /* skip '/' */
2394 ++(*path);
2395
2396 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002397 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002398
2399 if ((**path == '/' && (*path)[1]) || !**path) {
2400 /* path continues by another token or this is the last token */
2401 return LY_SUCCESS;
2402 } else if ((*path)[0] != '[') {
2403 /* unexpected character */
2404 return LY_EINVAL;
2405 } else {
2406 /* predicate starting with [ */
2407 *has_predicate = 1;
2408 return LY_SUCCESS;
2409 }
2410}
2411
2412/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002413 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2414 *
2415 * The set of features used for target must be a subset of features used for the leafref.
2416 * This is not a perfect, we should compare the truth tables but it could require too much resources
2417 * and RFC 7950 does not require it explicitely, so we simplify that.
2418 *
2419 * @param[in] refnode The leafref node.
2420 * @param[in] target Tha target node of the leafref.
2421 * @return LY_SUCCESS or LY_EVALID;
2422 */
2423static LY_ERR
2424lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2425{
2426 LY_ERR ret = LY_EVALID;
2427 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002428 LY_ARRAY_COUNT_TYPE u, v, count;
Radek Krejci58d171e2018-11-23 13:50:55 +01002429 struct ly_set features = {0};
2430
2431 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002432 if (iter->iffeatures) {
2433 LY_ARRAY_FOR(iter->iffeatures, u) {
2434 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2435 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002436 }
2437 }
2438 }
2439 }
2440
2441 /* we should have, in features set, a superset of features applicable to the target node.
2442 * So when adding features applicable to the target into the features set, we should not be
2443 * able to actually add any new feature, otherwise it is not a subset of features applicable
2444 * to the leafref itself. */
2445 count = features.count;
2446 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002447 if (iter->iffeatures) {
2448 LY_ARRAY_FOR(iter->iffeatures, u) {
2449 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2450 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002451 /* new feature was added (or LY_EMEM) */
2452 goto cleanup;
2453 }
2454 }
2455 }
2456 }
2457 }
2458 ret = LY_SUCCESS;
2459
2460cleanup:
2461 ly_set_erase(&features, NULL);
2462 return ret;
2463}
2464
Michal Vasko004d3152020-06-11 19:59:22 +02002465static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2466 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2467 struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002468
Radek Krejcia3045382018-11-22 14:30:31 +01002469/**
2470 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2471 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002472 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2473 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2474 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2475 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002476 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002477 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002478 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002479 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2480 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2481 * @param[out] type Newly created type structure with the filled information about the type.
2482 * @return LY_ERR value.
2483 */
Radek Krejci19a96102018-11-15 13:38:09 +01002484static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002485lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2486 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2487 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2488 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002489{
2490 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002491 struct lysc_type_bin *bin;
2492 struct lysc_type_num *num;
2493 struct lysc_type_str *str;
2494 struct lysc_type_bits *bits;
2495 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002496 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002497 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002498 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002499 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002500
2501 switch (basetype) {
2502 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002503 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002504
2505 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002506 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002507 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2508 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002509 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002510 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 +01002511 }
2512 }
2513
2514 if (tpdfname) {
2515 type_p->compiled = *type;
2516 *type = calloc(1, sizeof(struct lysc_type_bin));
2517 }
2518 break;
2519 case LY_TYPE_BITS:
2520 /* RFC 7950 9.7 - bits */
2521 bits = (struct lysc_type_bits*)(*type);
2522 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002523 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002524 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2525 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002526 }
2527
Radek Krejci555cb5b2018-11-16 14:54:33 +01002528 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002529 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002530 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002532 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002533 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002534 free(*type);
2535 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002536 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002537 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002538 }
2539
2540 if (tpdfname) {
2541 type_p->compiled = *type;
2542 *type = calloc(1, sizeof(struct lysc_type_bits));
2543 }
2544 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002545 case LY_TYPE_DEC64:
2546 dec = (struct lysc_type_dec*)(*type);
2547
2548 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002549 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002550 if (!type_p->fraction_digits) {
2551 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002552 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002553 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002555 free(*type);
2556 *type = NULL;
2557 }
2558 return LY_EVALID;
2559 }
2560 } else if (type_p->fraction_digits) {
2561 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002562 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002563 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2564 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002565 tpdfname);
2566 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002567 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2568 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002569 free(*type);
2570 *type = NULL;
2571 }
2572 return LY_EVALID;
2573 }
2574 dec->fraction_digits = type_p->fraction_digits;
2575
2576 /* RFC 7950 9.2.4 - range */
2577 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002578 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2579 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002580 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002581 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 +01002582 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002583 }
2584
2585 if (tpdfname) {
2586 type_p->compiled = *type;
2587 *type = calloc(1, sizeof(struct lysc_type_dec));
2588 }
2589 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002590 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002591 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002592
2593 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002594 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002595 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2596 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002597 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002598 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 +01002599 }
2600 } else if (base && ((struct lysc_type_str*)base)->length) {
2601 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2602 }
2603
2604 /* RFC 7950 9.4.5 - pattern */
2605 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002606 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002607 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002608 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2609 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2610 }
2611
2612 if (tpdfname) {
2613 type_p->compiled = *type;
2614 *type = calloc(1, sizeof(struct lysc_type_str));
2615 }
2616 break;
2617 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002618 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002619
2620 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002621 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002622 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002623 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002624 }
2625
Radek Krejci555cb5b2018-11-16 14:54:33 +01002626 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002627 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002628 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002630 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002631 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002632 free(*type);
2633 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002634 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002635 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002636 }
2637
2638 if (tpdfname) {
2639 type_p->compiled = *type;
2640 *type = calloc(1, sizeof(struct lysc_type_enum));
2641 }
2642 break;
2643 case LY_TYPE_INT8:
2644 case LY_TYPE_UINT8:
2645 case LY_TYPE_INT16:
2646 case LY_TYPE_UINT16:
2647 case LY_TYPE_INT32:
2648 case LY_TYPE_UINT32:
2649 case LY_TYPE_INT64:
2650 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002651 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002652
2653 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002654 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002655 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2656 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002657 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002658 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 +01002659 }
2660 }
2661
2662 if (tpdfname) {
2663 type_p->compiled = *type;
2664 *type = calloc(1, sizeof(struct lysc_type_num));
2665 }
2666 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002667 case LY_TYPE_IDENT:
2668 idref = (struct lysc_type_identityref*)(*type);
2669
2670 /* RFC 7950 9.10.2 - base */
2671 if (type_p->bases) {
2672 if (base) {
2673 /* only the directly derived identityrefs can contain base specification */
2674 if (tpdfname) {
2675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002676 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002677 tpdfname);
2678 } else {
2679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002680 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002681 free(*type);
2682 *type = NULL;
2683 }
2684 return LY_EVALID;
2685 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002686 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002687 }
2688
2689 if (!base && !type_p->flags) {
2690 /* type derived from identityref built-in type must contain at least one base */
2691 if (tpdfname) {
2692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2693 } else {
2694 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2695 free(*type);
2696 *type = NULL;
2697 }
2698 return LY_EVALID;
2699 }
2700
2701 if (tpdfname) {
2702 type_p->compiled = *type;
2703 *type = calloc(1, sizeof(struct lysc_type_identityref));
2704 }
2705 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002706 case LY_TYPE_LEAFREF:
Michal Vasko004d3152020-06-11 19:59:22 +02002707 lref = (struct lysc_type_leafref*)*type;
2708
Radek Krejcia3045382018-11-22 14:30:31 +01002709 /* RFC 7950 9.9.3 - require-instance */
2710 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002711 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002712 if (tpdfname) {
2713 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2714 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2715 } else {
2716 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2717 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2718 free(*type);
2719 *type = NULL;
2720 }
2721 return LY_EVALID;
2722 }
Michal Vasko004d3152020-06-11 19:59:22 +02002723 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002724 } else if (base) {
2725 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002726 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002727 } else {
2728 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002729 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002730 }
2731 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002732 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2733 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002734 } else if (base) {
Michal Vasko004d3152020-06-11 19:59:22 +02002735 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path);
2736 lref->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002737 } else if (tpdfname) {
2738 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2739 return LY_EVALID;
2740 } else {
2741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2742 free(*type);
2743 *type = NULL;
2744 return LY_EVALID;
2745 }
2746 if (tpdfname) {
2747 type_p->compiled = *type;
2748 *type = calloc(1, sizeof(struct lysc_type_leafref));
2749 }
2750 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002751 case LY_TYPE_INST:
2752 /* RFC 7950 9.9.3 - require-instance */
2753 if (type_p->flags & LYS_SET_REQINST) {
2754 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2755 } else {
2756 /* default is true */
2757 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2758 }
2759
2760 if (tpdfname) {
2761 type_p->compiled = *type;
2762 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2763 }
2764 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002765 case LY_TYPE_UNION:
2766 un = (struct lysc_type_union*)(*type);
2767
2768 /* RFC 7950 7.4 - type */
2769 if (type_p->types) {
2770 if (base) {
2771 /* only the directly derived union can contain types specification */
2772 if (tpdfname) {
2773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2774 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2775 tpdfname);
2776 } else {
2777 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2778 "Invalid type substatement for the type not directly derived from union built-in type.");
2779 free(*type);
2780 *type = NULL;
2781 }
2782 return LY_EVALID;
2783 }
2784 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002785 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2786 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002787 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
2788 &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002789 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2790 /* add space for additional types from the union subtype */
2791 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002792 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 +02002793 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002794
2795 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002796 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002797 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2798 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2799 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2800 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 +02002801 lref = (struct lysc_type_leafref *)un->types[u + additional];
2802
2803 lref->basetype = LY_TYPE_LEAFREF;
2804 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path);
2805 lref->refcount = 1;
2806 lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2807 lref->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002808 /* TODO extensions */
2809
2810 } else {
2811 un->types[u + additional] = un_aux->types[v];
2812 ++un_aux->types[v]->refcount;
2813 }
2814 ++additional;
2815 LY_ARRAY_INCREMENT(un->types);
2816 }
2817 /* compensate u increment in main loop */
2818 --additional;
2819
2820 /* free the replaced union subtype */
2821 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2822 } else {
2823 LY_ARRAY_INCREMENT(un->types);
2824 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002825 }
2826 }
2827
2828 if (!base && !type_p->flags) {
2829 /* type derived from union built-in type must contain at least one type */
2830 if (tpdfname) {
2831 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2832 } else {
2833 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2834 free(*type);
2835 *type = NULL;
2836 }
2837 return LY_EVALID;
2838 }
2839
2840 if (tpdfname) {
2841 type_p->compiled = *type;
2842 *type = calloc(1, sizeof(struct lysc_type_union));
2843 }
2844 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002845 case LY_TYPE_BOOL:
2846 case LY_TYPE_EMPTY:
2847 case LY_TYPE_UNKNOWN: /* just to complete switch */
2848 break;
2849 }
2850 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2851done:
2852 return ret;
2853}
2854
Radek Krejcia3045382018-11-22 14:30:31 +01002855/**
2856 * @brief Compile information about the leaf/leaf-list's type.
2857 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002858 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2859 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2860 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2861 * @param[in] context_name Name of the context node or referencing typedef for logging.
2862 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002863 * @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 +01002864 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002865 * @return LY_ERR value.
2866 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002867static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002868lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2869 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2870 struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002871{
2872 LY_ERR ret = LY_SUCCESS;
2873 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002874 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002875 struct type_context {
2876 const struct lysp_tpdf *tpdf;
2877 struct lysp_node *node;
2878 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002879 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002880 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002881 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002882 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002883 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002884 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002885
2886 (*type) = NULL;
2887
2888 tctx = calloc(1, sizeof *tctx);
2889 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002890 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002891 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2892 ret == LY_SUCCESS;
2893 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2894 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2895 if (basetype) {
2896 break;
2897 }
2898
2899 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002900 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2901 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002902 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2903
Radek Krejcicdfecd92018-11-26 11:27:32 +01002904 if (units && !*units) {
2905 /* inherit units */
2906 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2907 }
Radek Krejci01342af2019-01-03 15:18:08 +01002908 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002909 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002910 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02002911 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002912 }
Radek Krejci01342af2019-01-03 15:18:08 +01002913 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002914 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2915 break;
2916 }
2917
Radek Krejci19a96102018-11-15 13:38:09 +01002918 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002919 /* it is not necessary to continue, the rest of the chain was already compiled,
2920 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002921 basetype = tctx->tpdf->type.compiled->basetype;
2922 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002923 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002924 dummyloops = 1;
2925 goto preparenext;
2926 } else {
2927 tctx = NULL;
2928 break;
2929 }
Radek Krejci19a96102018-11-15 13:38:09 +01002930 }
2931
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002932 /* circular typedef reference detection */
2933 for (u = 0; u < tpdf_chain.count; u++) {
2934 /* local part */
2935 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2936 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2937 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2938 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2939 free(tctx);
2940 ret = LY_EVALID;
2941 goto cleanup;
2942 }
2943 }
2944 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2945 /* global part for unions corner case */
2946 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2947 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2949 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2950 free(tctx);
2951 ret = LY_EVALID;
2952 goto cleanup;
2953 }
2954 }
2955
Radek Krejci19a96102018-11-15 13:38:09 +01002956 /* store information for the following processing */
2957 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2958
Radek Krejcicdfecd92018-11-26 11:27:32 +01002959preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002960 /* prepare next loop */
2961 tctx_prev = tctx;
2962 tctx = calloc(1, sizeof *tctx);
2963 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2964 }
2965 free(tctx);
2966
2967 /* allocate type according to the basetype */
2968 switch (basetype) {
2969 case LY_TYPE_BINARY:
2970 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002971 break;
2972 case LY_TYPE_BITS:
2973 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002974 break;
2975 case LY_TYPE_BOOL:
2976 case LY_TYPE_EMPTY:
2977 *type = calloc(1, sizeof(struct lysc_type));
2978 break;
2979 case LY_TYPE_DEC64:
2980 *type = calloc(1, sizeof(struct lysc_type_dec));
2981 break;
2982 case LY_TYPE_ENUM:
2983 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002984 break;
2985 case LY_TYPE_IDENT:
2986 *type = calloc(1, sizeof(struct lysc_type_identityref));
2987 break;
2988 case LY_TYPE_INST:
2989 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2990 break;
2991 case LY_TYPE_LEAFREF:
2992 *type = calloc(1, sizeof(struct lysc_type_leafref));
2993 break;
2994 case LY_TYPE_STRING:
2995 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002996 break;
2997 case LY_TYPE_UNION:
2998 *type = calloc(1, sizeof(struct lysc_type_union));
2999 break;
3000 case LY_TYPE_INT8:
3001 case LY_TYPE_UINT8:
3002 case LY_TYPE_INT16:
3003 case LY_TYPE_UINT16:
3004 case LY_TYPE_INT32:
3005 case LY_TYPE_UINT32:
3006 case LY_TYPE_INT64:
3007 case LY_TYPE_UINT64:
3008 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003009 break;
3010 case LY_TYPE_UNKNOWN:
3011 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3012 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3013 ret = LY_EVALID;
3014 goto cleanup;
3015 }
3016 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003017 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003018 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3019 ly_data_type2str[basetype]);
3020 free(*type);
3021 (*type) = NULL;
3022 ret = LY_EVALID;
3023 goto cleanup;
3024 }
3025
3026 /* get restrictions from the referred typedefs */
3027 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3028 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003029
3030 /* remember the typedef context for circular check */
3031 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3032
Radek Krejci43699232018-11-23 14:59:46 +01003033 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003034 base = tctx->tpdf->type.compiled;
3035 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003036 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003037 /* no change, just use the type information from the base */
3038 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3039 ++base->refcount;
3040 continue;
3041 }
3042
3043 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003044 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3045 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3046 tctx->tpdf->name, ly_data_type2str[basetype]);
3047 ret = LY_EVALID;
3048 goto cleanup;
3049 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3050 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3051 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3052 tctx->tpdf->name, tctx->tpdf->dflt);
3053 ret = LY_EVALID;
3054 goto cleanup;
3055 }
3056
Radek Krejci19a96102018-11-15 13:38:09 +01003057 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003058 /* TODO user type plugins */
3059 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003060 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003061 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 +01003062 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003063 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003064 LY_CHECK_GOTO(ret, cleanup);
3065 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003066 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003067 /* remove the processed typedef contexts from the stack for circular check */
3068 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003069
Radek Krejcic5c27e52018-11-15 14:38:11 +01003070 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003071 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003072 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003073 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003074 /* TODO user type plugins */
3075 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003076 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003077 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 +01003078 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003079 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003080 /* no specific restriction in leaf's type definition, copy from the base */
3081 free(*type);
3082 (*type) = base;
3083 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003084 }
Radek Krejcia1911222019-07-22 17:24:50 +02003085 if (dflt && !(*type)->dflt) {
3086 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003087 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003088 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3089 (*type)->dflt->realtype = (*type);
3090 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3091 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003092 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003093 if (err) {
3094 ly_err_print(err);
3095 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3096 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3097 ly_err_free(err);
3098 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003099 if (ret == LY_EINCOMPLETE) {
3100 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003101 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003102
3103 /* but in general result is so far ok */
3104 ret = LY_SUCCESS;
3105 }
Radek Krejcia1911222019-07-22 17:24:50 +02003106 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003107 }
Radek Krejci19a96102018-11-15 13:38:09 +01003108
Radek Krejci0935f412019-08-20 16:15:18 +02003109 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003110
3111cleanup:
3112 ly_set_erase(&tpdf_chain, free);
3113 return ret;
3114}
3115
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003116/**
3117 * @brief Compile status information of the given node.
3118 *
3119 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3120 * has the status correctly set during the compilation.
3121 *
3122 * @param[in] ctx Compile context
3123 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3124 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3125 * the compatibility with the parent's status value.
3126 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3127 * @return LY_ERR value.
3128 */
3129static LY_ERR
3130lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3131{
3132 /* status - it is not inherited by specification, but it does not make sense to have
3133 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3134 if (!((*node_flags) & LYS_STATUS_MASK)) {
3135 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3136 if ((parent_flags & 0x3) != 0x3) {
3137 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3138 * combination of bits (0x3) which marks the uses_status value */
3139 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3140 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3141 }
3142 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3143 } else {
3144 (*node_flags) |= LYS_STATUS_CURR;
3145 }
3146 } else if (parent_flags & LYS_STATUS_MASK) {
3147 /* check status compatibility with the parent */
3148 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3149 if ((*node_flags) & LYS_STATUS_CURR) {
3150 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3151 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3152 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3153 } else { /* LYS_STATUS_DEPRC */
3154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3155 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3156 }
3157 return LY_EVALID;
3158 }
3159 }
3160 return LY_SUCCESS;
3161}
3162
Radek Krejci8cce8532019-03-05 11:27:45 +01003163/**
3164 * @brief Check uniqness of the node/action/notification name.
3165 *
3166 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3167 * structures, but they share the namespace so we need to check their name collisions.
3168 *
3169 * @param[in] ctx Compile context.
3170 * @param[in] children List (linked list) of data nodes to go through.
3171 * @param[in] actions List (sized array) of actions or RPCs to go through.
3172 * @param[in] notifs List (sized array) of Notifications to go through.
3173 * @param[in] name Name of the item to find in the given lists.
3174 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3175 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3176 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3177 */
3178static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02003179lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions,
3180 const struct lysc_notif *notifs, const char *name, void *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003181{
3182 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003183 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003184
3185 LY_LIST_FOR(children, iter) {
3186 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3187 goto error;
3188 }
3189 }
3190 LY_ARRAY_FOR(actions, u) {
3191 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3192 goto error;
3193 }
3194 }
3195 LY_ARRAY_FOR(notifs, u) {
3196 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3197 goto error;
3198 }
3199 }
3200 return LY_SUCCESS;
3201error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003202 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003203 return LY_EEXIST;
3204}
3205
Radek Krejciec4da802019-05-02 13:02:41 +02003206static 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 +01003207
Radek Krejcia3045382018-11-22 14:30:31 +01003208/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003209 * @brief Compile parsed RPC/action schema node information.
3210 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003211 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003212 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003213 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3214 * @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).
3215 * Zero means no uses, non-zero value with no status bit set mean the default status.
3216 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3217 */
3218static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003219lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003220 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3221{
3222 LY_ERR ret = LY_SUCCESS;
3223 struct lysp_node *child_p;
3224 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003225 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003226
Radek Krejci327de162019-06-14 12:52:07 +02003227 lysc_update_path(ctx, parent, action_p->name);
3228
Radek Krejci8cce8532019-03-05 11:27:45 +01003229 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3230 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3231 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3232 action_p->name, action)) {
3233 return LY_EVALID;
3234 }
3235
Radek Krejciec4da802019-05-02 13:02:41 +02003236 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003237 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003238 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003239 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003240 return LY_EVALID;
3241 }
3242
Michal Vasko1bf09392020-03-27 12:38:10 +01003243 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003244 action->module = ctx->mod;
3245 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003246 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003247 action->sp = action_p;
3248 }
3249 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3250
3251 /* status - it is not inherited by specification, but it does not make sense to have
3252 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003253 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003254
3255 DUP_STRING(ctx->ctx, action_p->name, action->name);
3256 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3257 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003258 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003259 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003260
3261 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003262 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003263 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003264 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 +02003265 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003266 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003267 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003268 }
Radek Krejci327de162019-06-14 12:52:07 +02003269 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003270 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003271
3272 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003273 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003274 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003275 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 +02003276 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003277 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003278 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003279 }
Radek Krejci327de162019-06-14 12:52:07 +02003280 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003281 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003282
3283 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3284 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003285 ly_set_add(&ctx->xpath, action, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003286 }
3287
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003288cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003289 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003290 return ret;
3291}
3292
3293/**
Radek Krejci43981a32019-04-12 09:44:11 +02003294 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003295 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003296 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003297 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3298 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3299 * @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 +02003300 * Zero means no uses, non-zero value with no status bit set mean the default status.
3301 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3302 */
3303static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003304lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003305 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3306{
3307 LY_ERR ret = LY_SUCCESS;
3308 struct lysp_node *child_p;
3309 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003310 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003311
Radek Krejci327de162019-06-14 12:52:07 +02003312 lysc_update_path(ctx, parent, notif_p->name);
3313
Radek Krejcifc11bd72019-04-11 16:00:05 +02003314 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3315 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3316 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3317 notif_p->name, notif)) {
3318 return LY_EVALID;
3319 }
3320
Radek Krejciec4da802019-05-02 13:02:41 +02003321 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003322 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3323 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003324 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003325 return LY_EVALID;
3326 }
3327
3328 notif->nodetype = LYS_NOTIF;
3329 notif->module = ctx->mod;
3330 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003331 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003332 notif->sp = notif_p;
3333 }
3334 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3335
3336 /* status - it is not inherited by specification, but it does not make sense to have
3337 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3338 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3339
3340 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3341 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3342 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003343 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003344 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003345 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3346 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003347 ly_set_add(&ctx->xpath, notif, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003348 }
Radek Krejci0935f412019-08-20 16:15:18 +02003349 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003350
Radek Krejciec4da802019-05-02 13:02:41 +02003351 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003352 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003353 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003354 }
3355
Radek Krejci327de162019-06-14 12:52:07 +02003356 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003357cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003358 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003359 return ret;
3360}
3361
3362/**
Radek Krejcia3045382018-11-22 14:30:31 +01003363 * @brief Compile parsed container node information.
3364 * @param[in] ctx Compile context
3365 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003366 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3367 * is enriched with the container-specific information.
3368 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3369 */
Radek Krejci19a96102018-11-15 13:38:09 +01003370static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003371lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003372{
3373 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3374 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3375 struct lysp_node *child_p;
3376 unsigned int u;
3377 LY_ERR ret = LY_SUCCESS;
3378
Radek Krejcife909632019-02-12 15:34:42 +01003379 if (cont_p->presence) {
3380 cont->flags |= LYS_PRESENCE;
3381 }
3382
Radek Krejci19a96102018-11-15 13:38:09 +01003383 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003384 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003385 }
3386
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003387 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003388 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3389 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003390 ly_set_add(&ctx->xpath, cont, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003391 }
Radek Krejciec4da802019-05-02 13:02:41 +02003392 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3393 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003394
3395done:
3396 return ret;
3397}
3398
Radek Krejci33f72892019-02-21 10:36:58 +01003399/*
3400 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3401 * @param[in] ctx Compile context.
3402 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3403 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003404 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3405 * @return LY_ERR value.
3406 */
3407static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003408lys_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 +01003409{
Radek Krejci33f72892019-02-21 10:36:58 +01003410 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3411
Radek Krejciec4da802019-05-02 13:02:41 +02003412 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 +01003413 leaf->units ? NULL : &leaf->units));
3414 if (leaf->nodetype == LYS_LEAFLIST) {
3415 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003416 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3417 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003418 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003419 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3420 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3421 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3422 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003423 LY_ARRAY_INCREMENT(llist->dflts);
3424 }
3425 } else {
3426 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003427 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003428 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3429 leaf->dflt->realtype = leaf->type->dflt->realtype;
3430 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3431 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003432 }
3433 }
3434 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3435 /* 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 +02003436 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003437 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003438 LY_ARRAY_COUNT_TYPE u;
Radek Krejci33f72892019-02-21 10:36:58 +01003439 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3440 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3441 /* 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 +02003442 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003443 }
3444 }
3445 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003446 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3447 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3448 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3449 return LY_EVALID;
3450 }
3451 }
3452
Radek Krejci33f72892019-02-21 10:36:58 +01003453 return LY_SUCCESS;
3454}
3455
Radek Krejcia3045382018-11-22 14:30:31 +01003456/**
3457 * @brief Compile parsed leaf node information.
3458 * @param[in] ctx Compile context
3459 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003460 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3461 * is enriched with the leaf-specific information.
3462 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3463 */
Radek Krejci19a96102018-11-15 13:38:09 +01003464static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003465lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003466{
3467 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3468 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3469 unsigned int u;
3470 LY_ERR ret = LY_SUCCESS;
3471
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003472 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003473 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3474 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003475 ly_set_add(&ctx->xpath, leaf, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003476 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003477 if (leaf_p->units) {
3478 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3479 leaf->flags |= LYS_SET_UNITS;
3480 }
Radek Krejcia1911222019-07-22 17:24:50 +02003481
3482 /* the dflt member is just filled to avoid getting the default value from the type */
3483 leaf->dflt = (void*)leaf_p->dflt;
3484 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003485 if (ret) {
3486 leaf->dflt = NULL;
3487 return ret;
3488 }
Radek Krejcia1911222019-07-22 17:24:50 +02003489
Radek Krejciccd20f12019-02-15 14:12:27 +01003490 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003491 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003492 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003493 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3494 leaf->dflt->realtype = leaf->type;
3495 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3496 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003497 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003498 leaf->dflt->realtype->refcount++;
3499 if (err) {
3500 ly_err_print(err);
3501 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3502 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3503 ly_err_free(err);
3504 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003505 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003506 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003507 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003508
3509 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003510 ret = LY_SUCCESS;
3511 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003512 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003513 leaf->flags |= LYS_SET_DFLT;
3514 }
Radek Krejci43699232018-11-23 14:59:46 +01003515
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003516
Radek Krejci19a96102018-11-15 13:38:09 +01003517done:
3518 return ret;
3519}
3520
Radek Krejcia3045382018-11-22 14:30:31 +01003521/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003522 * @brief Compile parsed leaf-list node information.
3523 * @param[in] ctx Compile context
3524 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003525 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3526 * is enriched with the leaf-list-specific information.
3527 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3528 */
3529static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003530lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003531{
3532 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3533 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003534 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003535 LY_ERR ret = LY_SUCCESS;
3536
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003537 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003538 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3539 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003540 ly_set_add(&ctx->xpath, llist, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003541 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003542 if (llist_p->units) {
3543 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3544 llist->flags |= LYS_SET_UNITS;
3545 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003546
Radek Krejcia1911222019-07-22 17:24:50 +02003547 /* the dflts member is just filled to avoid getting the default value from the type */
3548 llist->dflts = (void*)llist_p->dflts;
3549 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Michal Vasko6a044b22020-01-15 12:25:39 +01003550 if (ret != LY_SUCCESS) {
3551 /* make sure the defaults are freed correctly */
3552 if (llist_p->dflts) {
3553 llist->dflts = NULL;
3554 }
3555 return ret;
3556 }
3557
Radek Krejci0e5d8382018-11-28 16:37:53 +01003558 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003559 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003560 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
3561 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003562 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003563 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003564 LY_ARRAY_INCREMENT(llist->dflts_mods);
3565 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003566 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003567 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3568 llist->dflts[u]->realtype = llist->type;
3569 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3570 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003571 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003572 llist->dflts[u]->realtype->refcount++;
3573 if (err) {
3574 ly_err_print(err);
3575 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3576 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3577 ly_err_free(err);
3578 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003579 if (ret == LY_EINCOMPLETE) {
3580 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003581 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003582
3583 /* but in general result is so far ok */
3584 ret = LY_SUCCESS;
3585 }
Radek Krejcia1911222019-07-22 17:24:50 +02003586 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003587 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003588 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003589 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003590 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02003591 /* configuration data values must be unique - so check the default values */
3592 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003593 for (v = u + 1; v < LY_ARRAY_COUNT(llist->dflts); ++v) {
Radek Krejcia1911222019-07-22 17:24:50 +02003594 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3595 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003596 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 +02003597 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3598 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3599 if (dynamic) {
3600 free((char*)val);
3601 }
3602 return LY_EVALID;
3603 }
3604 }
3605 }
3606 }
3607
3608 /* 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 +01003609
3610 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003611 if (llist->min) {
3612 llist->flags |= LYS_MAND_TRUE;
3613 }
Radek Krejcib7408632018-11-28 17:12:11 +01003614 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003615
Radek Krejci0e5d8382018-11-28 16:37:53 +01003616done:
3617 return ret;
3618}
3619
3620/**
Radek Krejci7af64242019-02-18 13:07:53 +01003621 * @brief Compile information about list's uniques.
3622 * @param[in] ctx Compile context.
3623 * @param[in] context_module Module where the prefixes are going to be resolved.
3624 * @param[in] uniques Sized array list of unique statements.
3625 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3626 * @return LY_ERR value.
3627 */
3628static LY_ERR
3629lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3630{
3631 LY_ERR ret = LY_SUCCESS;
3632 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003633 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003634 const char *keystr, *delim;
3635 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003636 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7af64242019-02-18 13:07:53 +01003637 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003638 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003639
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003640 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003641 config = -1;
3642 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3643 keystr = uniques[v];
3644 while (keystr) {
3645 delim = strpbrk(keystr, " \t\n");
3646 if (delim) {
3647 len = delim - keystr;
3648 while (isspace(*delim)) {
3649 ++delim;
3650 }
3651 } else {
3652 len = strlen(keystr);
3653 }
3654
3655 /* unique node must be present */
3656 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003657 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3658 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003659 if (ret != LY_SUCCESS) {
3660 if (ret == LY_EDENIED) {
3661 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003662 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003663 len, keystr, lys_nodetype2str((*key)->nodetype));
3664 }
3665 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003666 } else if (flags) {
3667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3668 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003669 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003670 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003671 }
3672
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003673
Radek Krejci7af64242019-02-18 13:07:53 +01003674 /* all referenced leafs must be of the same config type */
3675 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3676 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003677 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003678 return LY_EVALID;
3679 } else if ((*key)->flags & LYS_CONFIG_W) {
3680 config = 1;
3681 } else { /* LYS_CONFIG_R */
3682 config = 0;
3683 }
3684
Michal Vasko14654712020-02-06 08:35:21 +01003685 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3686 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3687 if (parent->nodetype == LYS_LIST) {
3688 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3689 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3690 return LY_EVALID;
3691 }
3692 }
3693
Radek Krejci7af64242019-02-18 13:07:53 +01003694 /* check status */
3695 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3696 (*key)->flags, (*key)->module, (*key)->name));
3697
3698 /* mark leaf as unique */
3699 (*key)->flags |= LYS_UNIQUE;
3700
3701 /* next unique value in line */
3702 keystr = delim;
3703 }
3704 /* next unique definition */
3705 }
3706
3707 return LY_SUCCESS;
3708}
3709
3710/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003711 * @brief Compile parsed list node information.
3712 * @param[in] ctx Compile context
3713 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003714 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3715 * is enriched with the list-specific information.
3716 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3717 */
3718static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003719lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003720{
3721 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3722 struct lysc_node_list *list = (struct lysc_node_list*)node;
3723 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003724 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003725 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003726 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003727 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003728 LY_ERR ret = LY_SUCCESS;
3729
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003730 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003731 if (list->min) {
3732 list->flags |= LYS_MAND_TRUE;
3733 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003734 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3735
3736 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003737 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003738 }
3739
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003740 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003741 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3742 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003743 ly_set_add(&ctx->xpath, list, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003744 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003745
3746 /* keys */
3747 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3749 return LY_EVALID;
3750 }
3751
3752 /* find all the keys (must be direct children) */
3753 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003754 if (!keystr) {
3755 /* keyless list */
3756 list->flags |= LYS_KEYLESS;
3757 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003758 while (keystr) {
3759 delim = strpbrk(keystr, " \t\n");
3760 if (delim) {
3761 len = delim - keystr;
3762 while (isspace(*delim)) {
3763 ++delim;
3764 }
3765 } else {
3766 len = strlen(keystr);
3767 }
3768
3769 /* key node must be present */
Michal Vaskoe444f752020-02-10 12:20:06 +01003770 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 +02003771 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003772 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3773 "The list's key \"%.*s\" not found.", len, keystr);
3774 return LY_EVALID;
3775 }
3776 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003777 if (key->flags & LYS_KEY) {
3778 /* the node was already marked as a key */
3779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3780 "Duplicated key identifier \"%.*s\".", len, keystr);
3781 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003782 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003783
3784 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003785 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003786 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003787 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3788 return LY_EVALID;
3789 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003790 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003791 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003792 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003793 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003794 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003795 return LY_EVALID;
3796 }
3797 } else {
3798 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003799 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003800 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003801 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003802 return LY_EVALID;
3803 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003804 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003805 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003806 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003807 return LY_EVALID;
3808 }
3809 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003810
3811 /* check status */
3812 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003813 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003814
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003815 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003816 if (key->dflt) {
3817 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3818 lysc_type_free(ctx->ctx, key->dflt->realtype);
3819 free(key->dflt);
3820 key->dflt = NULL;
3821 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003822 }
3823 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003824 key->flags |= LYS_KEY;
3825
3826 /* move it to the correct position */
3827 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3828 /* fix links in closest previous siblings of the key */
3829 if (key->next) {
3830 key->next->prev = key->prev;
3831 } else {
3832 /* last child */
3833 list->child->prev = key->prev;
3834 }
3835 if (key->prev->next) {
3836 key->prev->next = key->next;
3837 }
3838 /* fix links in the key */
3839 if (prev_key) {
3840 key->prev = (struct lysc_node*)prev_key;
3841 key->next = prev_key->next;
3842 } else {
3843 key->prev = list->child->prev;
3844 key->next = list->child;
3845 }
3846 /* fix links in closes future siblings of the key */
3847 if (prev_key) {
3848 if (prev_key->next) {
3849 prev_key->next->prev = (struct lysc_node*)key;
3850 } else {
3851 list->child->prev = (struct lysc_node*)key;
3852 }
3853 prev_key->next = (struct lysc_node*)key;
3854 } else {
3855 list->child->prev = (struct lysc_node*)key;
3856 }
3857 /* fix links in parent */
3858 if (!key->prev->next) {
3859 list->child = (struct lysc_node*)key;
3860 }
3861 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003862
3863 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003864 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003865 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003866 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003867 }
3868
3869 /* uniques */
3870 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003871 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003872 }
3873
Radek Krejciec4da802019-05-02 13:02:41 +02003874 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3875 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003876
3877done:
3878 return ret;
3879}
3880
Radek Krejcib56c7502019-02-13 14:19:54 +01003881/**
3882 * @brief Do some checks and set the default choice's case.
3883 *
3884 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3885 *
3886 * @param[in] ctx Compile context.
3887 * @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,
3888 * not the reference to the imported module.
3889 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3890 * @return LY_ERR value.
3891 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003892static LY_ERR
3893lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3894{
3895 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3896 const char *prefix = NULL, *name;
3897 size_t prefix_len = 0;
3898
3899 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3900 name = strchr(dflt, ':');
3901 if (name) {
3902 prefix = dflt;
3903 prefix_len = name - prefix;
3904 ++name;
3905 } else {
3906 name = dflt;
3907 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003908 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003909 /* prefixed default case make sense only for the prefix of the schema itself */
3910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3911 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3912 prefix_len, prefix);
3913 return LY_EVALID;
3914 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003915 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 +01003916 if (!ch->dflt) {
3917 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3918 "Default case \"%s\" not found.", dflt);
3919 return LY_EVALID;
3920 }
3921 /* no mandatory nodes directly under the default case */
3922 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003923 if (iter->parent != (struct lysc_node*)ch->dflt) {
3924 break;
3925 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003926 if (iter->flags & LYS_MAND_TRUE) {
3927 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3928 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3929 return LY_EVALID;
3930 }
3931 }
Radek Krejci01342af2019-01-03 15:18:08 +01003932 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003933 return LY_SUCCESS;
3934}
3935
Radek Krejciccd20f12019-02-15 14:12:27 +01003936static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003937lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003938{
3939 struct lys_module *mod;
3940 const char *prefix = NULL, *name;
3941 size_t prefix_len = 0;
3942 struct lysc_node_case *cs;
3943 struct lysc_node *node;
3944
3945 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3946 name = strchr(dflt, ':');
3947 if (name) {
3948 prefix = dflt;
3949 prefix_len = name - prefix;
3950 ++name;
3951 } else {
3952 name = dflt;
3953 }
3954 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3955 if (prefix) {
3956 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3957 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003958 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3959 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003960 return LY_EVALID;
3961 }
3962 } else {
3963 mod = ctx->mod;
3964 }
3965 /* get the default case */
Michal Vaskoe444f752020-02-10 12:20:06 +01003966 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 +01003967 if (!cs) {
3968 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003969 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003970 return LY_EVALID;
3971 }
3972
3973 /* check that there is no mandatory node */
3974 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003975 if (node->parent != (struct lysc_node*)cs) {
3976 break;
3977 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003978 if (node->flags & LYS_MAND_TRUE) {
3979 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003980 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3981 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003982 return LY_EVALID;
3983 }
3984 }
3985
3986 /* set the default case in choice */
3987 ch->dflt = cs;
3988 cs->flags |= LYS_SET_DFLT;
3989
3990 return LY_SUCCESS;
3991}
3992
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003993/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003994 * @brief Compile parsed choice node information.
3995 * @param[in] ctx Compile context
3996 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003997 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003998 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003999 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4000 */
4001static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004002lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004003{
4004 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4005 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4006 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004007 LY_ERR ret = LY_SUCCESS;
4008
Radek Krejci056d0a82018-12-06 16:57:25 +01004009 LY_LIST_FOR(ch_p->child, child_p) {
4010 if (child_p->nodetype == LYS_CASE) {
4011 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004012 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004013 }
4014 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004015 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004016 }
4017 }
4018
4019 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004020 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004021 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004022 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004023
Radek Krejci9800fb82018-12-13 14:26:23 +01004024 return ret;
4025}
4026
4027/**
4028 * @brief Compile parsed anydata or anyxml node information.
4029 * @param[in] ctx Compile context
4030 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004031 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4032 * is enriched with the any-specific information.
4033 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4034 */
4035static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004036lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004037{
4038 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4039 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4040 unsigned int u;
4041 LY_ERR ret = LY_SUCCESS;
4042
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004043 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004044 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4045 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004046 ly_set_add(&ctx->xpath, any, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004047 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004048
4049 if (any->flags & LYS_CONFIG_W) {
4050 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004051 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004052 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004053done:
4054 return ret;
4055}
4056
Radek Krejcib56c7502019-02-13 14:19:54 +01004057/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004058 * @brief Connect the node into the siblings list and check its name uniqueness.
4059 *
4060 * @param[in] ctx Compile context
4061 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4062 * the choice itself is expected instead of a specific case node.
4063 * @param[in] node Schema node to connect into the list.
4064 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004065 * 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 +01004066 */
4067static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004068lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004069{
4070 struct lysc_node **children;
4071
4072 if (node->nodetype == LYS_CASE) {
4073 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4074 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004075 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004076 }
4077 if (children) {
4078 if (!(*children)) {
4079 /* first child */
4080 *children = node;
4081 } else if (*children != node) {
4082 /* by the condition in previous branch we cover the choice/case children
4083 * - the children list is shared by the choice and the the first case, in addition
4084 * the first child of each case must be referenced from the case node. So the node is
4085 * actually always already inserted in case it is the first children - so here such
4086 * a situation actually corresponds to the first branch */
4087 /* insert at the end of the parent's children list */
4088 (*children)->prev->next = node;
4089 node->prev = (*children)->prev;
4090 (*children)->prev = node;
4091
4092 /* check the name uniqueness */
4093 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4094 lysc_node_notifs(parent), node->name, node)) {
4095 return LY_EEXIST;
4096 }
4097 }
4098 }
4099 return LY_SUCCESS;
4100}
4101
Radek Krejci95710c92019-02-11 15:49:55 +01004102/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004103 * @brief Prepare the case structure in choice node for the new data node.
4104 *
4105 * 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
4106 * created in the choice when the first child was processed.
4107 *
4108 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004109 * @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,
4110 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004111 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4112 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4113 * @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,
4114 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004115 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004116static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004117lys_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 +01004118{
4119 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004120 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004121 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004122 unsigned int u;
4123 LY_ERR ret;
4124
Radek Krejci95710c92019-02-11 15:49:55 +01004125#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004126 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004127 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004128 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4129 return NULL; \
4130 } \
4131 }
4132
Radek Krejci95710c92019-02-11 15:49:55 +01004133 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4134 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004135
4136 /* we have to add an implicit case node into the parent choice */
4137 cs = calloc(1, sizeof(struct lysc_node_case));
4138 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004139 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004140 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004141 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004142 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4143 /* the case is already present since the child is not its first children */
4144 return (struct lysc_node_case*)ch->cases->prev;
4145 }
Radek Krejci95710c92019-02-11 15:49:55 +01004146 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004147
4148 /* explicit parent case is not present (this is its first child) */
4149 cs = calloc(1, sizeof(struct lysc_node_case));
4150 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004151 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004152 cs->flags = LYS_STATUS_MASK & node_p->flags;
4153 cs->sp = node_p;
4154
Radek Krejcib1b59152019-01-07 13:21:56 +01004155 /* 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 +02004156 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004157
4158 if (node_p->when) {
4159 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004160 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004161 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004162
4163 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4164 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004165 ly_set_add(&ctx->xpath, cs, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004166 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004167 }
Radek Krejciec4da802019-05-02 13:02:41 +02004168 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004169 } else {
4170 LOGINT(ctx->ctx);
4171 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004172 }
4173 cs->module = ctx->mod;
4174 cs->prev = (struct lysc_node*)cs;
4175 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004176 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004177 cs->child = child;
4178
4179 return cs;
4180error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004181 if (cs) {
4182 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4183 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004184 return NULL;
4185
4186#undef UNIQUE_CHECK
4187}
4188
Radek Krejcib56c7502019-02-13 14:19:54 +01004189/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004190 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004191 *
4192 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004193 * @param[in] node Target node where the config is supposed to be changed.
4194 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004195 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4196 * 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 +01004197 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4198 * @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 +01004199 * @return LY_ERR value.
4200 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004201static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004202lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004203 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004204{
4205 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004206 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004207
4208 if (config == (node->flags & LYS_CONFIG_MASK)) {
4209 /* nothing to do */
4210 return LY_SUCCESS;
4211 }
4212
4213 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004214 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004215 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4216 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004217 "Invalid %s of config - configuration node cannot be child of any state data node.",
4218 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004219 return LY_EVALID;
4220 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004221 node->flags |= LYS_SET_CONFIG;
4222 } else {
4223 if (node->flags & LYS_SET_CONFIG) {
4224 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4225 /* setting config flags, but have node with explicit config true */
4226 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004227 "Invalid %s of config - configuration node cannot be child of any state data node.",
4228 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004229 return LY_EVALID;
4230 }
4231 /* do not change config on nodes where the config is explicitely set, this does not apply to
4232 * nodes, which are being changed explicitly (targets of refine or deviation) */
4233 return LY_SUCCESS;
4234 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004235 }
4236 node->flags &= ~LYS_CONFIG_MASK;
4237 node->flags |= config;
4238
4239 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004240 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004241 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004242 }
4243
Radek Krejci76b3e962018-12-14 17:01:25 +01004244 return LY_SUCCESS;
4245}
4246
Radek Krejcib56c7502019-02-13 14:19:54 +01004247/**
4248 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4249 *
4250 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4251 * the flag to such parents from a mandatory children.
4252 *
4253 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4254 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4255 * (mandatory children was removed).
4256 */
Radek Krejcife909632019-02-12 15:34:42 +01004257void
4258lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4259{
4260 struct lysc_node *iter;
4261
4262 if (add) { /* set flag */
4263 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4264 parent = parent->parent) {
4265 parent->flags |= LYS_MAND_TRUE;
4266 }
4267 } else { /* unset flag */
4268 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004269 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004270 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004271 /* there is another mandatory node */
4272 return;
4273 }
4274 }
4275 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4276 parent->flags &= ~LYS_MAND_TRUE;
4277 }
4278 }
4279}
4280
Radek Krejci056d0a82018-12-06 16:57:25 +01004281/**
Radek Krejci3641f562019-02-13 15:38:40 +01004282 * @brief Internal sorting process for the lys_compile_augment_sort().
4283 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4284 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4285 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4286 */
4287static void
4288lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4289{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004290 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004291 size_t len;
4292
4293 len = strlen(aug_p->nodeid);
4294 LY_ARRAY_FOR(result, v) {
4295 if (strlen(result[v]->nodeid) <= len) {
4296 continue;
4297 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004298 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004299 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004300 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004301 break;
4302 }
4303 }
4304 result[v] = aug_p;
4305 LY_ARRAY_INCREMENT(result);
4306}
4307
4308/**
4309 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4310 *
4311 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4312 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4313 *
4314 * @param[in] ctx Compile context.
4315 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4316 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4317 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4318 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4319 * @return LY_ERR value.
4320 */
4321LY_ERR
4322lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4323{
4324 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004325 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004326
4327 assert(augments);
4328
4329 /* get count of the augments in module and all its submodules */
4330 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004331 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004332 }
4333 LY_ARRAY_FOR(inc_p, u) {
4334 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004335 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004336 }
4337 }
4338
4339 if (!count) {
4340 *augments = NULL;
4341 return LY_SUCCESS;
4342 }
4343 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4344
4345 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4346 * together, so there can be even /z/y betwwen them. */
4347 LY_ARRAY_FOR(aug_p, u) {
4348 lys_compile_augment_sort_(&aug_p[u], result);
4349 }
4350 LY_ARRAY_FOR(inc_p, u) {
4351 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4352 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4353 }
4354 }
4355
4356 *augments = result;
4357 return LY_SUCCESS;
4358}
4359
4360/**
4361 * @brief Compile the parsed augment connecting it into its target.
4362 *
4363 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4364 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4365 * are already implemented and compiled.
4366 *
4367 * @param[in] ctx Compile context.
4368 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004369 * @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
4370 * children in case of the augmenting uses data.
4371 * @return LY_SUCCESS on success.
4372 * @return LY_EVALID on failure.
4373 */
4374LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004375lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004376{
Michal Vaskoe6143202020-07-03 13:02:08 +02004377 LY_ERR ret = LY_SUCCESS, rc;
Radek Krejci3641f562019-02-13 15:38:40 +01004378 struct lysp_node *node_p, *case_node_p;
4379 struct lysc_node *target; /* target target of the augment */
4380 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004381 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004382 struct lys_module **aug_mod;
Radek Krejci3641f562019-02-13 15:38:40 +01004383 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004384 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004385 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejciec4da802019-05-02 13:02:41 +02004386 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004387
Radek Krejci327de162019-06-14 12:52:07 +02004388 lysc_update_path(ctx, NULL, "{augment}");
4389 lysc_update_path(ctx, NULL, aug_p->nodeid);
4390
Radek Krejci7af64242019-02-18 13:07:53 +01004391 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004392 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004393 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004394 if (ret != LY_SUCCESS) {
4395 if (ret == LY_EDENIED) {
4396 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4397 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4398 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4399 }
4400 return LY_EVALID;
4401 }
4402
4403 /* check for mandatory nodes
4404 * - new cases augmenting some choice can have mandatory nodes
4405 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4406 */
Radek Krejci733988a2019-02-15 15:12:44 +01004407 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004408 allow_mandatory = 1;
4409 }
4410
4411 when_shared = NULL;
4412 LY_LIST_FOR(aug_p->child, node_p) {
4413 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4414 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004415 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004416 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4417 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004418 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004419 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4420 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004421 return LY_EVALID;
4422 }
4423
4424 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004425 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004426 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004427 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004428 } else {
4429 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004430 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004431 }
4432 }
Radek Krejciec4da802019-05-02 13:02:41 +02004433 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004434
Radek Krejcife13da42019-02-15 14:51:01 +01004435 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4436 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004437 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004438 /* 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 +01004439 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 +01004440 } else if (target->nodetype == LYS_CHOICE) {
4441 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4442 node = ((struct lysc_node_choice*)target)->cases->prev;
4443 } else {
4444 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004445 node = (struct lysc_node*)lysc_node_children(target, flags);
4446 if (!node) {
4447 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4448 break;
4449 }
4450 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004451 }
4452
Radek Krejci733988a2019-02-15 15:12:44 +01004453 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004454 node->flags &= ~LYS_MAND_TRUE;
4455 lys_compile_mandatory_parents(target, 0);
4456 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004457 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004458 return LY_EVALID;
4459 }
4460
4461 /* pass augment's when to all the children */
4462 if (aug_p->when) {
4463 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4464 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004465 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004466 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004467
4468 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4469 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004470 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004471 }
4472
Radek Krejci3641f562019-02-13 15:38:40 +01004473 when_shared = *when;
4474 } else {
4475 ++when_shared->refcount;
4476 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004477
4478 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4479 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004480 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004481 }
Radek Krejci3641f562019-02-13 15:38:40 +01004482 }
4483 }
4484 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004485
Radek Krejciec4da802019-05-02 13:02:41 +02004486 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004487 switch (target->nodetype) {
4488 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004489 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004490 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004491 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004492 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004493 break;
4494 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004495 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004496 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004497 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004498 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004499 break;
4500 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004501 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004502 if (aug_p->actions) {
4503 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004504 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4505 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004506 return LY_EVALID;
4507 }
4508 if (aug_p->notifs) {
4509 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004510 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004511 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004512 return LY_EVALID;
4513 }
4514 }
Radek Krejci3641f562019-02-13 15:38:40 +01004515
Michal Vaskoe6143202020-07-03 13:02:08 +02004516 /* add this module into the target module augmented_by, if not there already */
4517 rc = LY_SUCCESS;
4518 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4519 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4520 rc = LY_EEXIST;
4521 break;
4522 }
4523 }
4524 if (!rc) {
4525 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4526 *aug_mod = ctx->mod;
4527 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004528
Radek Krejci327de162019-06-14 12:52:07 +02004529 lysc_update_path(ctx, NULL, NULL);
4530 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004531
Radek Krejci3641f562019-02-13 15:38:40 +01004532error:
Radek Krejciec4da802019-05-02 13:02:41 +02004533 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004534 return ret;
4535}
4536
4537/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004538 * @brief Apply refined or deviated mandatory flag to the target node.
4539 *
4540 * @param[in] ctx Compile context.
4541 * @param[in] node Target node where the mandatory property is supposed to be changed.
4542 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004543 * @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 +01004544 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4545 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4546 * 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 +01004547 * @return LY_ERR value.
4548 */
4549static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004550lys_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 +01004551{
4552 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4553 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004554 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4555 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004556 return LY_EVALID;
4557 }
4558
4559 if (mandatory_flag & LYS_MAND_TRUE) {
4560 /* check if node has default value */
4561 if (node->nodetype & LYS_LEAF) {
4562 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004563 if (refine_flag) {
4564 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004565 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004566 return LY_EVALID;
4567 }
Radek Krejcia1911222019-07-22 17:24:50 +02004568 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004569 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004570 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004571
4572 /* update the list of incomplete default values if needed */
4573 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4574
Radek Krejcia1911222019-07-22 17:24:50 +02004575 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4576 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4577 free(leaf->dflt);
4578 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004579 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004580 }
4581 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004582 if (refine_flag) {
4583 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004584 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004585 return LY_EVALID;
4586 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004587 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004588 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004589 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 +01004590 return LY_EVALID;
4591 }
4592
4593 node->flags &= ~LYS_MAND_FALSE;
4594 node->flags |= LYS_MAND_TRUE;
4595 lys_compile_mandatory_parents(node->parent, 1);
4596 } else {
4597 /* make mandatory false */
4598 node->flags &= ~LYS_MAND_TRUE;
4599 node->flags |= LYS_MAND_FALSE;
4600 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004601 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 +01004602 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004603 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004604 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004605 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4606 leaf->dflt->realtype = leaf->type->dflt->realtype;
4607 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4608 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004609 }
4610 }
4611 return LY_SUCCESS;
4612}
4613
4614/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004615 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4616 * If present, also apply uses's modificators.
4617 *
4618 * @param[in] ctx Compile context
4619 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004620 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4621 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4622 * the compile context.
4623 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4624 */
4625static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004626lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004627{
4628 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004629 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004630 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004631 struct lysc_node_container context_node_fake =
4632 {.nodetype = LYS_CONTAINER,
4633 .module = ctx->mod,
4634 .flags = parent ? parent->flags : 0,
4635 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004636 .prev = (struct lysc_node*)&context_node_fake,
4637 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004638 struct lysp_grp *grp = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004639 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004640 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004641 int found;
4642 const char *id, *name, *prefix;
4643 size_t prefix_len, name_len;
4644 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004645 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004646 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004647 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004648 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004649 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004650 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004651 struct lysp_augment **augments = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004652 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004653 struct lysc_notif **notifs = NULL;
4654 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004655
4656 /* search for the grouping definition */
4657 found = 0;
4658 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004659 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004660 if (prefix) {
4661 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4662 if (!mod) {
4663 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004664 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004665 return LY_EVALID;
4666 }
4667 } else {
4668 mod = ctx->mod_def;
4669 }
4670 if (mod == ctx->mod_def) {
4671 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004672 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004673 LY_ARRAY_FOR(grp, u) {
4674 if (!strcmp(grp[u].name, name)) {
4675 grp = &grp[u];
4676 found = 1;
4677 break;
4678 }
4679 }
4680 }
4681 }
4682 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004683 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004684 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004685 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004686 for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004687 if (!strcmp(grp[u].name, name)) {
4688 grp = &grp[u];
4689 found = 1;
4690 }
4691 }
4692 }
4693 if (!found && mod->parsed->includes) {
4694 /* ... and all the submodules */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004695 for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004696 grp = mod->parsed->includes[u].submodule->groupings;
4697 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004698 for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004699 if (!strcmp(grp[v].name, name)) {
4700 grp = &grp[v];
4701 found = 1;
4702 }
4703 }
4704 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004705 }
4706 }
4707 }
4708 if (!found) {
4709 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4710 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4711 return LY_EVALID;
4712 }
4713
4714 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4715 grp_stack_count = ctx->groupings.count;
4716 ly_set_add(&ctx->groupings, (void*)grp, 0);
4717 if (grp_stack_count == ctx->groupings.count) {
4718 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4719 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4720 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4721 return LY_EVALID;
4722 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004723 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4724 /* remember that the grouping is instantiated to avoid its standalone validation */
4725 grp->flags |= LYS_USED_GRP;
4726 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004727
4728 /* switch context's mod_def */
4729 mod_old = ctx->mod_def;
4730 ctx->mod_def = mod;
4731
4732 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004733 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 +01004734
Radek Krejcifc11bd72019-04-11 16:00:05 +02004735 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004736 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004737 /* 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 +02004738 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 +01004739
4740 /* some preparation for applying refines */
4741 if (grp->data == node_p) {
4742 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004743 if (parent) {
4744 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4745 } else if (ctx->mod->compiled->data) {
4746 child = ctx->mod->compiled->data;
4747 } else {
4748 child = NULL;
4749 }
4750 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004751 }
4752 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004753 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004754 LY_LIST_FOR(context_node_fake.child, child) {
4755 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004756
Radek Krejcifc11bd72019-04-11 16:00:05 +02004757 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004758 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004759 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004760 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004761 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4762
4763 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4764 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004765 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004766 }
4767
Radek Krejci00b874b2019-02-12 10:54:50 +01004768 when_shared = *when;
4769 } else {
4770 ++when_shared->refcount;
4771 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004772
4773 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4774 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004775 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004776 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004777 }
4778 }
Radek Krejci01342af2019-01-03 15:18:08 +01004779 }
4780 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004781 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004782 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004783 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004784 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 +01004785 }
4786
Radek Krejcifc11bd72019-04-11 16:00:05 +02004787 /* compile actions */
4788 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4789 if (actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004790 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004791 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004792 if (*actions && (uses_p->augments || uses_p->refines)) {
4793 /* 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 +02004794 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
4795 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
4796 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004797 }
4798 }
4799
4800 /* compile notifications */
4801 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4802 if (notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004803 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004804 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004805 if (*notifs && (uses_p->augments || uses_p->refines)) {
4806 /* 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 +02004807 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
4808 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
4809 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004810 }
4811 }
4812
4813
Radek Krejci3641f562019-02-13 15:38:40 +01004814 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004815 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004816 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004817 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004818 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004819
Radek Krejcif0089082019-01-07 16:42:01 +01004820 /* reload previous context's mod_def */
4821 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004822 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004823
Radek Krejci76b3e962018-12-14 17:01:25 +01004824 /* apply refine */
4825 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004826 lysc_update_path(ctx, NULL, rfn->nodeid);
4827
Radek Krejci7af64242019-02-18 13:07:53 +01004828 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 +01004829 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004830 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004831 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004832
4833 /* default value */
4834 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004835 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004836 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004837 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.",
4838 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004839 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004840 }
4841 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4842 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004843 "Invalid refine of default - %s cannot hold default value(s).",
4844 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004845 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004846 }
4847 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004848 struct ly_err_item *err = NULL;
4849 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4850 if (leaf->dflt) {
4851 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004852 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004853 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4854 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4855 } else {
4856 /* prepare a new one */
4857 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4858 leaf->dflt->realtype = leaf->type;
4859 }
4860 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004861 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004862 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4863 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004864 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004865 leaf->dflt->realtype->refcount++;
4866 if (err) {
4867 ly_err_print(err);
4868 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4869 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4870 ly_err_free(err);
4871 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004872 if (rc == LY_EINCOMPLETE) {
4873 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004874 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004875
4876 /* but in general result is so far ok */
4877 rc = LY_SUCCESS;
4878 }
Radek Krejcia1911222019-07-22 17:24:50 +02004879 LY_CHECK_GOTO(rc, cleanup);
4880 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004881 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004882 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4883
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004884 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004885 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4886 "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 +02004887 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004888 }
Radek Krejcia1911222019-07-22 17:24:50 +02004889
4890 /* remove previous set of default values */
4891 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004892 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004893 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4894 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4895 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004896 }
Radek Krejcia1911222019-07-22 17:24:50 +02004897 LY_ARRAY_FREE(llist->dflts);
4898 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004899 LY_ARRAY_FREE(llist->dflts_mods);
4900 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004901
4902 /* create the new set of the default values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004903 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
4904 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004905 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004906 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004907 LY_ARRAY_INCREMENT(llist->dflts_mods);
4908 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004909 LY_ARRAY_INCREMENT(llist->dflts);
4910 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4911 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004912 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004913 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004914 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004915 llist->dflts[u]->realtype->refcount++;
4916 if (err) {
4917 ly_err_print(err);
4918 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4919 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4920 ly_err_free(err);
4921 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004922 if (rc == LY_EINCOMPLETE) {
4923 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004924 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004925
4926 /* but in general result is so far ok */
4927 rc = LY_SUCCESS;
4928 }
4929 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004930 }
Radek Krejcia1911222019-07-22 17:24:50 +02004931 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004932 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004933 if (((struct lysc_node_choice*)node)->dflt) {
4934 /* unset LYS_SET_DFLT from the current default case */
4935 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4936 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004937 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 +01004938 }
4939 }
4940
Radek Krejci12fb9142019-01-08 09:45:30 +01004941 /* description */
4942 if (rfn->dsc) {
4943 FREE_STRING(ctx->ctx, node->dsc);
4944 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4945 }
4946
4947 /* reference */
4948 if (rfn->ref) {
4949 FREE_STRING(ctx->ctx, node->ref);
4950 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4951 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004952
4953 /* config */
4954 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004955 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004956 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004957 } else {
4958 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004959 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004960 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004961 }
4962
4963 /* mandatory */
4964 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004965 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004966 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004967
4968 /* presence */
4969 if (rfn->presence) {
4970 if (node->nodetype != LYS_CONTAINER) {
4971 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004972 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4973 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004974 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004975 }
4976 node->flags |= LYS_PRESENCE;
4977 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004978
4979 /* must */
4980 if (rfn->musts) {
4981 switch (node->nodetype) {
4982 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004983 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 +01004984 break;
4985 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004986 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 +01004987 break;
4988 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004989 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 +01004990 break;
4991 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004992 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 +01004993 break;
4994 case LYS_ANYXML:
4995 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004996 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 +01004997 break;
4998 default:
4999 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005000 "Invalid refine of must statement - %s cannot hold any must statement.",
5001 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005002 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005003 }
Michal Vasko004d3152020-06-11 19:59:22 +02005004 ly_set_add(&ctx->xpath, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01005005 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005006
5007 /* min/max-elements */
5008 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5009 switch (node->nodetype) {
5010 case LYS_LEAFLIST:
5011 if (rfn->flags & LYS_SET_MAX) {
5012 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5013 }
5014 if (rfn->flags & LYS_SET_MIN) {
5015 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005016 if (rfn->min) {
5017 node->flags |= LYS_MAND_TRUE;
5018 lys_compile_mandatory_parents(node->parent, 1);
5019 } else {
5020 node->flags &= ~LYS_MAND_TRUE;
5021 lys_compile_mandatory_parents(node->parent, 0);
5022 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005023 }
5024 break;
5025 case LYS_LIST:
5026 if (rfn->flags & LYS_SET_MAX) {
5027 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5028 }
5029 if (rfn->flags & LYS_SET_MIN) {
5030 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005031 if (rfn->min) {
5032 node->flags |= LYS_MAND_TRUE;
5033 lys_compile_mandatory_parents(node->parent, 1);
5034 } else {
5035 node->flags &= ~LYS_MAND_TRUE;
5036 lys_compile_mandatory_parents(node->parent, 0);
5037 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005038 }
5039 break;
5040 default:
5041 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005042 "Invalid refine of %s statement - %s cannot hold this statement.",
5043 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005044 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005045 }
5046 }
Radek Krejcif0089082019-01-07 16:42:01 +01005047
5048 /* if-feature */
5049 if (rfn->iffeatures) {
5050 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005051 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005052 }
Radek Krejci327de162019-06-14 12:52:07 +02005053
5054 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005055 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005056
Radek Krejcif2271f12019-01-07 16:42:23 +01005057 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005058 for (uint32_t i = 0; i < refined.count; ++i) {
5059 node = (struct lysc_node*)refined.objs[i];
5060 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02005061 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005062
5063 /* check possible conflict with default value (default added, mandatory left true) */
5064 if ((node->flags & LYS_MAND_TRUE) &&
5065 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5066 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5067 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005068 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005069 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005070 }
5071
5072 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5073 if (node->nodetype == LYS_LIST) {
5074 min = ((struct lysc_node_list*)node)->min;
5075 max = ((struct lysc_node_list*)node)->max;
5076 } else {
5077 min = ((struct lysc_node_leaflist*)node)->min;
5078 max = ((struct lysc_node_leaflist*)node)->max;
5079 }
5080 if (min > max) {
5081 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005082 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5083 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005084 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005085 }
5086 }
5087 }
5088
Radek Krejci327de162019-06-14 12:52:07 +02005089 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005090 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005091
5092cleanup:
5093 /* fix connection of the children nodes from fake context node back into the parent */
5094 if (context_node_fake.child) {
5095 context_node_fake.child->prev = child;
5096 }
5097 LY_LIST_FOR(context_node_fake.child, child) {
5098 child->parent = parent;
5099 }
5100
5101 if (uses_p->augments || uses_p->refines) {
5102 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005103 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005104 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005105 LY_ARRAY_FREE(context_node_fake.actions);
5106 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005107 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005108 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005109 LY_ARRAY_FREE(context_node_fake.notifs);
5110 }
5111 }
5112
Radek Krejcie86bf772018-12-14 11:39:53 +01005113 /* reload previous context's mod_def */
5114 ctx->mod_def = mod_old;
5115 /* remove the grouping from the stack for circular groupings dependency check */
5116 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5117 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005118 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005119 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005120
5121 return ret;
5122}
5123
Radek Krejci327de162019-06-14 12:52:07 +02005124static int
5125lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5126{
5127 struct lysp_node *iter;
5128 int len = 0;
5129
5130 *path = NULL;
5131 for (iter = node; iter && len >= 0; iter = iter->parent) {
5132 char *s = *path;
5133 char *id;
5134
5135 switch (iter->nodetype) {
5136 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005137 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005138 break;
5139 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005140 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005141 break;
5142 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005143 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005144 break;
5145 default:
5146 id = strdup(iter->name);
5147 break;
5148 }
5149
5150 if (!iter->parent) {
5151 /* print prefix */
5152 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5153 } else {
5154 /* prefix is the same as in parent */
5155 len = asprintf(path, "/%s%s", id, s ? s : "");
5156 }
5157 free(s);
5158 free(id);
5159 }
5160
5161 if (len < 0) {
5162 free(*path);
5163 *path = NULL;
5164 } else if (len == 0) {
5165 *path = strdup("/");
5166 len = 1;
5167 }
5168 return len;
5169}
5170
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005171/**
5172 * @brief Validate groupings that were defined but not directly used in the schema itself.
5173 *
5174 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5175 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5176 */
5177static LY_ERR
5178lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5179{
5180 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005181 char *path;
5182 int len;
5183
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005184 struct lysp_node_uses fake_uses = {
5185 .parent = node_p,
5186 .nodetype = LYS_USES,
5187 .flags = 0, .next = NULL,
5188 .name = grp->name,
5189 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5190 .refines = NULL, .augments = NULL
5191 };
5192 struct lysc_node_container fake_container = {
5193 .nodetype = LYS_CONTAINER,
5194 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5195 .module = ctx->mod,
5196 .sp = NULL, .parent = NULL, .next = NULL,
5197 .prev = (struct lysc_node*)&fake_container,
5198 .name = "fake",
5199 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5200 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5201 };
5202
5203 if (grp->parent) {
5204 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5205 }
Radek Krejci327de162019-06-14 12:52:07 +02005206
5207 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5208 if (len < 0) {
5209 LOGMEM(ctx->ctx);
5210 return LY_EMEM;
5211 }
5212 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5213 ctx->path_len = (uint16_t)len;
5214 free(path);
5215
5216 lysc_update_path(ctx, NULL, "{grouping}");
5217 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005218 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005219 lysc_update_path(ctx, NULL, NULL);
5220 lysc_update_path(ctx, NULL, NULL);
5221
5222 ctx->path_len = 1;
5223 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005224
5225 /* cleanup */
5226 lysc_node_container_free(ctx->ctx, &fake_container);
5227
5228 return ret;
5229}
Radek Krejcife909632019-02-12 15:34:42 +01005230
Radek Krejcie86bf772018-12-14 11:39:53 +01005231/**
Radek Krejcia3045382018-11-22 14:30:31 +01005232 * @brief Compile parsed schema node information.
5233 * @param[in] ctx Compile context
5234 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005235 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5236 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5237 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005238 * @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).
5239 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005240 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5241 */
Radek Krejci19a96102018-11-15 13:38:09 +01005242static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005243lys_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 +01005244{
Radek Krejci1c54f462020-05-12 17:25:34 +02005245 LY_ERR ret = LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01005246 struct lysc_node *node;
5247 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005248 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005249 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005250 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005251
Radek Krejci327de162019-06-14 12:52:07 +02005252 if (node_p->nodetype != LYS_USES) {
5253 lysc_update_path(ctx, parent, node_p->name);
5254 } else {
5255 lysc_update_path(ctx, NULL, "{uses}");
5256 lysc_update_path(ctx, NULL, node_p->name);
5257 }
5258
Radek Krejci19a96102018-11-15 13:38:09 +01005259 switch (node_p->nodetype) {
5260 case LYS_CONTAINER:
5261 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5262 node_compile_spec = lys_compile_node_container;
5263 break;
5264 case LYS_LEAF:
5265 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5266 node_compile_spec = lys_compile_node_leaf;
5267 break;
5268 case LYS_LIST:
5269 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005270 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005271 break;
5272 case LYS_LEAFLIST:
5273 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005274 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005275 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005276 case LYS_CHOICE:
5277 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005278 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005279 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005280 case LYS_ANYXML:
5281 case LYS_ANYDATA:
5282 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005283 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005284 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005285 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005286 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5287 lysc_update_path(ctx, NULL, NULL);
5288 lysc_update_path(ctx, NULL, NULL);
5289 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005290 default:
5291 LOGINT(ctx->ctx);
5292 return LY_EINT;
5293 }
5294 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5295 node->nodetype = node_p->nodetype;
5296 node->module = ctx->mod;
5297 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005298 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005299
5300 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005301 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005302 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005303 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005304 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5305 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005306 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005307 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005308 node->flags |= LYS_CONFIG_R;
5309 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005310 /* config not explicitely set, inherit it from parent */
5311 if (parent) {
5312 node->flags |= parent->flags & LYS_CONFIG_MASK;
5313 } else {
5314 /* default is config true */
5315 node->flags |= LYS_CONFIG_W;
5316 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005317 } else {
5318 /* config set explicitely */
5319 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005320 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005321 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5322 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5323 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005324 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005325 goto error;
5326 }
Radek Krejci19a96102018-11-15 13:38:09 +01005327
Radek Krejcia6d57732018-11-29 13:40:37 +01005328 /* *list ordering */
5329 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5330 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005331 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005332 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5333 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005334 node->flags &= ~LYS_ORDBY_MASK;
5335 node->flags |= LYS_ORDBY_SYSTEM;
5336 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5337 /* default ordering is system */
5338 node->flags |= LYS_ORDBY_SYSTEM;
5339 }
5340 }
5341
Radek Krejci19a96102018-11-15 13:38:09 +01005342 /* status - it is not inherited by specification, but it does not make sense to have
5343 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005344 if (!parent || parent->nodetype != LYS_CHOICE) {
5345 /* in case of choice/case's children, postpone the check to the moment we know if
5346 * 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 +02005347 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 +01005348 }
5349
Radek Krejciec4da802019-05-02 13:02:41 +02005350 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005351 node->sp = node_p;
5352 }
5353 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005354 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5355 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005356 if (node_p->when) {
5357 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005358 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005359
5360 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5361 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02005362 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01005363 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005364 }
Radek Krejciec4da802019-05-02 13:02:41 +02005365 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005366
5367 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005368 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005369
Radek Krejci0935f412019-08-20 16:15:18 +02005370 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5371
Radek Krejcife909632019-02-12 15:34:42 +01005372 /* inherit LYS_MAND_TRUE in parent containers */
5373 if (node->flags & LYS_MAND_TRUE) {
5374 lys_compile_mandatory_parents(parent, 1);
5375 }
5376
Radek Krejci327de162019-06-14 12:52:07 +02005377 lysc_update_path(ctx, NULL, NULL);
5378
Radek Krejci19a96102018-11-15 13:38:09 +01005379 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005380 if (parent) {
5381 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005382 if (node_p->parent->nodetype == LYS_CASE) {
5383 lysc_update_path(ctx, parent, node_p->parent->name);
5384 } else {
5385 lysc_update_path(ctx, parent, node->name);
5386 }
Radek Krejciec4da802019-05-02 13:02:41 +02005387 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005388 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005389 if (uses_status) {
5390
5391 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005392 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005393 * it directly gets the same status flags as the choice;
5394 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005395 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005396 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005397 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005398 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005399 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005400 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005401 }
Radek Krejciec4da802019-05-02 13:02:41 +02005402 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 +02005403
5404 if (parent->nodetype == LYS_CHOICE) {
5405 lysc_update_path(ctx, NULL, NULL);
5406 }
Radek Krejci19a96102018-11-15 13:38:09 +01005407 } else {
5408 /* top-level element */
5409 if (!ctx->mod->compiled->data) {
5410 ctx->mod->compiled->data = node;
5411 } else {
5412 /* insert at the end of the module's top-level nodes list */
5413 ctx->mod->compiled->data->prev->next = node;
5414 node->prev = ctx->mod->compiled->data->prev;
5415 ctx->mod->compiled->data->prev = node;
5416 }
Radek Krejci327de162019-06-14 12:52:07 +02005417 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005418 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5419 ctx->mod->compiled->notifs, node->name, node)) {
5420 return LY_EVALID;
5421 }
Radek Krejci19a96102018-11-15 13:38:09 +01005422 }
Radek Krejci327de162019-06-14 12:52:07 +02005423 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005424
5425 return LY_SUCCESS;
5426
5427error:
5428 lysc_node_free(ctx->ctx, node);
5429 return ret;
5430}
5431
Radek Krejciccd20f12019-02-15 14:12:27 +01005432static void
5433lysc_disconnect(struct lysc_node *node)
5434{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005435 struct lysc_node *parent, *child, *prev = NULL, *next;
5436 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005437 int remove_cs = 0;
5438
5439 parent = node->parent;
5440
5441 /* parent's first child */
5442 if (!parent) {
5443 return;
5444 }
5445 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005446 cs = (struct lysc_node_case*)node;
5447 } else if (parent->nodetype == LYS_CASE) {
5448 /* disconnecting some node in a case */
5449 cs = (struct lysc_node_case*)parent;
5450 parent = cs->parent;
5451 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5452 if (child == node) {
5453 if (cs->child == child) {
5454 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5455 /* case with a single child -> remove also the case */
5456 child->parent = NULL;
5457 remove_cs = 1;
5458 } else {
5459 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005460 }
5461 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005462 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005463 }
5464 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005465 if (!remove_cs) {
5466 cs = NULL;
5467 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005468 } else if (lysc_node_children(parent, node->flags) == node) {
5469 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005470 }
5471
5472 if (cs) {
5473 if (remove_cs) {
5474 /* cs has only one child which is being also removed */
5475 lysc_disconnect((struct lysc_node*)cs);
5476 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5477 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005478 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5479 /* default case removed */
5480 ((struct lysc_node_choice*)parent)->dflt = NULL;
5481 }
5482 if (((struct lysc_node_choice*)parent)->cases == cs) {
5483 /* first case removed */
5484 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5485 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005486 if (cs->child) {
5487 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5488 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5489 prev = cs->child->prev;
5490 } /* else all the children are under a single case */
5491 LY_LIST_FOR_SAFE(cs->child, next, child) {
5492 if (child->parent != (struct lysc_node*)cs) {
5493 break;
5494 }
5495 lysc_node_free(node->module->ctx, child);
5496 }
5497 if (prev) {
5498 if (prev->next) {
5499 prev->next = child;
5500 }
5501 if (child) {
5502 child->prev = prev;
5503 } else {
5504 /* link from the first child under the cases */
5505 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5506 }
5507 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005508 }
5509 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005510 }
5511
5512 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005513 if (node->prev->next) {
5514 node->prev->next = node->next;
5515 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005516 if (node->next) {
5517 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005518 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005519 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005520 if (child) {
5521 child->prev = node->prev;
5522 }
5523 } else if (((struct lysc_node_choice*)parent)->cases) {
5524 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005525 }
5526}
5527
5528LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005529lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005530{
Radek Krejcia1911222019-07-22 17:24:50 +02005531 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005532 struct ly_set devs_p = {0};
5533 struct ly_set targets = {0};
5534 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005535 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005536 struct lysc_action *rpcs;
5537 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005538 struct lysp_deviation *dev;
5539 struct lysp_deviate *d, **dp_new;
5540 struct lysp_deviate_add *d_add;
5541 struct lysp_deviate_del *d_del;
5542 struct lysp_deviate_rpl *d_rpl;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005543 LY_ARRAY_COUNT_TYPE u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005544 struct lysc_deviation {
5545 const char *nodeid;
5546 struct lysc_node *target; /* target node of the deviation */
5547 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005548 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005549 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5550 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005551 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005552 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005553 const char *prefix, *name, *nodeid, *dflt;
Michal Vasko57c10cd2020-05-27 15:57:11 +02005554 struct lys_module *mod, **dev_mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005555 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005556 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005557
5558 /* get all deviations from the module and all its submodules ... */
5559 LY_ARRAY_FOR(mod_p->deviations, u) {
5560 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5561 }
5562 LY_ARRAY_FOR(mod_p->includes, v) {
5563 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5564 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5565 }
5566 }
5567 if (!devs_p.count) {
5568 /* nothing to do */
5569 return LY_SUCCESS;
5570 }
5571
Radek Krejci327de162019-06-14 12:52:07 +02005572 lysc_update_path(ctx, NULL, "{deviation}");
5573
Radek Krejciccd20f12019-02-15 14:12:27 +01005574 /* ... and group them by the target node */
5575 devs = calloc(devs_p.count, sizeof *devs);
5576 for (u = 0; u < devs_p.count; ++u) {
5577 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005578 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005579
5580 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005581 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5582 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01005583 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005584 /* move the target pointer to input/output to make them different from the action and
5585 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5586 * back to the RPC/action node due to a better compatibility and decision code in this function.
5587 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5588 if (flags & LYSC_OPT_RPC_INPUT) {
5589 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5590 flags |= LYSC_OPT_INTERNAL;
5591 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5592 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5593 flags |= LYSC_OPT_INTERNAL;
5594 }
5595 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005596 /* insert into the set of targets with duplicity detection */
5597 i = ly_set_add(&targets, target, 0);
5598 if (!devs[i]) {
5599 /* new record */
5600 devs[i] = calloc(1, sizeof **devs);
5601 devs[i]->target = target;
5602 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005603 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005604 }
5605 /* add deviates into the deviation's list of deviates */
5606 for (d = dev->deviates; d; d = d->next) {
5607 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5608 *dp_new = d;
5609 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5610 devs[i]->not_supported = 1;
5611 }
5612 }
Radek Krejci327de162019-06-14 12:52:07 +02005613
5614 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005615 }
5616
5617 /* MACROS for deviates checking */
5618#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5619 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005620 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 +01005621 goto cleanup; \
5622 }
5623
5624#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005625 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5626 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5627 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005628 goto cleanup; \
5629 }
5630
Radek Krejcia1911222019-07-22 17:24:50 +02005631
Radek Krejciccd20f12019-02-15 14:12:27 +01005632#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005633 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5634 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5635 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5636 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5637 goto cleanup; \
5638 }
5639
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005640#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005641 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005642 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005643 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5644 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005645 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005646 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5647 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005648 goto cleanup; \
5649 }
5650
Radek Krejci551b12c2019-02-19 16:11:21 +01005651#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5652 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5653 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005654 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5655 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005656 goto cleanup; \
5657 }
5658
Radek Krejciccd20f12019-02-15 14:12:27 +01005659#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5660 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005661 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005662 goto cleanup; \
5663 }
5664
Radek Krejci551b12c2019-02-19 16:11:21 +01005665#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5666 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005668 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005669 goto cleanup; \
5670 }
5671
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005672#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5673 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5674 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5675 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5676 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 +01005677 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005678 if (y == LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005680 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5681 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005682 goto cleanup; \
5683 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005684 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5685 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5686 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5687 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005688 (LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005689 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005690 if (!LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005691 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5692 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005693 }
5694
5695 /* apply deviations */
5696 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005697 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5698 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5699 struct ly_err_item *err = NULL;
5700
5701 dflt = NULL;
5702 changed_type = 0;
5703
Radek Krejci327de162019-06-14 12:52:07 +02005704 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5705
Radek Krejcif538ce52019-03-05 10:46:14 +01005706 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5707 /* fix the target pointer in case of RPC's/action's input/output */
5708 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5709 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5710 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5711 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5712 }
5713 }
5714
Radek Krejciccd20f12019-02-15 14:12:27 +01005715 /* not-supported */
5716 if (devs[u]->not_supported) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005717 if (LY_ARRAY_COUNT(devs[u]->deviates) > 1) {
5718 LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.",
5719 LY_ARRAY_COUNT(devs[u]->deviates), devs[u]->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005720 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005721
5722#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5723 if (devs[u]->target->parent) { \
5724 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5725 } else { \
5726 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5727 } \
5728 LY_ARRAY_FOR(ARRAY, x) { \
5729 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5730 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005731 if (x < LY_ARRAY_COUNT(ARRAY)) { \
Radek Krejcifc11bd72019-04-11 16:00:05 +02005732 FREEFUNC(ctx->ctx, &ARRAY[x]); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005733 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
Radek Krejcifc11bd72019-04-11 16:00:05 +02005734 LY_ARRAY_DECREMENT(ARRAY); \
5735 }
5736
Michal Vasko1bf09392020-03-27 12:38:10 +01005737 if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005738 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5739 /* remove RPC's/action's input */
5740 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5741 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005742 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5743 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005744 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5745 /* remove RPC's/action's output */
5746 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5747 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005748 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5749 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005750 } else {
5751 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005752 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005753 }
5754 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005755 /* remove Notification */
5756 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005757 } else {
5758 /* remove the target node */
5759 lysc_disconnect(devs[u]->target);
5760 lysc_node_free(ctx->ctx, devs[u]->target);
5761 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005762
Radek Krejci474f9b82019-07-24 11:36:37 +02005763 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5764 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005765 continue;
5766 }
5767
5768 /* list of deviates (not-supported is not present in the list) */
5769 LY_ARRAY_FOR(devs[u]->deviates, v) {
5770 d = devs[u]->deviates[v];
5771
5772 switch (d->mod) {
5773 case LYS_DEV_ADD:
5774 d_add = (struct lysp_deviate_add*)d;
5775 /* [units-stmt] */
5776 if (d_add->units) {
5777 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5778 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5779
5780 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5781 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5782 }
5783
5784 /* *must-stmt */
5785 if (d_add->musts) {
5786 switch (devs[u]->target->nodetype) {
5787 case LYS_CONTAINER:
5788 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005789 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5790 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005791 break;
5792 case LYS_LEAF:
5793 case LYS_LEAFLIST:
5794 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005795 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5796 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005797 break;
5798 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005799 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5800 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005801 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005802 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005803 case LYS_ACTION:
5804 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005805 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5806 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005807 break;
Michal Vaskocc048b22020-03-27 15:52:38 +01005808 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005809 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5810 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005811 break;
5812 }
5813 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005814 default:
5815 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005816 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005817 goto cleanup;
5818 }
Michal Vasko004d3152020-06-11 19:59:22 +02005819 ly_set_add(&ctx->xpath, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01005820 }
5821
5822 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005823 if (d_add->uniques) {
5824 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5825 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5826 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005827
5828 /* *default-stmt */
5829 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005830 switch (devs[u]->target->nodetype) {
5831 case LYS_LEAF:
5832 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005833 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 +02005834 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005835 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005836 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005837 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5838 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5839 } else {
5840 /* prepare new default value storage */
5841 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005842 }
Radek Krejcia1911222019-07-22 17:24:50 +02005843 dflt = d_add->dflts[0];
5844 /* parsing is done at the end after possible replace of the leaf's type */
5845
Radek Krejci551b12c2019-02-19 16:11:21 +01005846 /* mark the new default values as leaf's own */
5847 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005848 break;
5849 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005850 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005851 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005852 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005853 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005854 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5855 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5856 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005857 }
Radek Krejcia1911222019-07-22 17:24:50 +02005858 LY_ARRAY_FREE(llist->dflts);
5859 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005860 LY_ARRAY_FREE(llist->dflts_mods);
5861 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005862 }
5863 /* add new default value(s) */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005864 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup);
5865 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup);
5866 for (x = y = LY_ARRAY_COUNT(llist->dflts);
5867 x < LY_ARRAY_COUNT(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005868 LY_ARRAY_INCREMENT(llist->dflts_mods);
5869 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005870 LY_ARRAY_INCREMENT(llist->dflts);
5871 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5872 llist->dflts[x]->realtype = llist->type;
5873 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 +02005874 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005875 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005876 llist->dflts[x]->realtype->refcount++;
5877 if (err) {
5878 ly_err_print(err);
5879 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5880 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5881 d_add->dflts[x - y], err->msg);
5882 ly_err_free(err);
5883 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005884 if (rc == LY_EINCOMPLETE) {
5885 /* postpone default compilation when the tree is complete */
5886 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5887
5888 /* but in general result is so far ok */
5889 rc = LY_SUCCESS;
5890 }
Radek Krejcia1911222019-07-22 17:24:50 +02005891 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005892 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005893 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005894 devs[u]->target->flags |= LYS_SET_DFLT;
5895 break;
5896 case LYS_CHOICE:
5897 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5898 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5899 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5900 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005901 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 +01005902 goto cleanup;
5903 }
5904 break;
5905 default:
5906 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005907 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005908 goto cleanup;
5909 }
5910 }
5911
5912 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005913 if (d_add->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01005914 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005915 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005916 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5917 goto cleanup;
5918 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005919 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005920 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
Michal Vaskoa3881362020-01-21 15:57:35 +01005921 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005922 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005923 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5924 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005925 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5926 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005927 goto cleanup;
5928 }
Radek Krejci327de162019-06-14 12:52:07 +02005929 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005930 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005931
5932 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005933 if (d_add->flags & LYS_MAND_MASK) {
5934 if (devs[u]->target->flags & LYS_MAND_MASK) {
5935 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005936 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5937 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005938 goto cleanup;
5939 }
Radek Krejci327de162019-06-14 12:52:07 +02005940 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005941 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005942
5943 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005944 if (d_add->flags & LYS_SET_MIN) {
5945 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5946 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5947 /* change value */
5948 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5949 } else if (devs[u]->target->nodetype == LYS_LIST) {
5950 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5951 /* change value */
5952 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5953 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005954 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005955 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5956 goto cleanup;
5957 }
5958 if (d_add->min) {
5959 devs[u]->target->flags |= LYS_MAND_TRUE;
5960 }
5961 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005962
5963 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005964 if (d_add->flags & LYS_SET_MAX) {
5965 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5966 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5967 /* change value */
5968 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5969 } else if (devs[u]->target->nodetype == LYS_LIST) {
5970 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5971 /* change value */
5972 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5973 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005974 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005975 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5976 goto cleanup;
5977 }
5978 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005979
5980 break;
5981 case LYS_DEV_DELETE:
5982 d_del = (struct lysp_deviate_del*)d;
5983
5984 /* [units-stmt] */
5985 if (d_del->units) {
5986 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02005987 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
5988 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
5989 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5990 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
5991 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5992 goto cleanup;
5993 }
5994 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5995 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005996 }
5997
5998 /* *must-stmt */
5999 if (d_del->musts) {
6000 switch (devs[u]->target->nodetype) {
6001 case LYS_CONTAINER:
6002 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006003 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006004 break;
6005 case LYS_LEAF:
6006 case LYS_LEAFLIST:
6007 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006008 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006009 break;
6010 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006011 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006012 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006013 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006014 case LYS_ACTION:
6015 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6016 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6017 break;
6018 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6019 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6020 break;
6021 }
6022 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006023 default:
6024 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006025 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006026 goto cleanup;
6027 }
6028 }
6029
6030 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006031 if (d_del->uniques) {
6032 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6033 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6034 LY_ARRAY_FOR(d_del->uniques, x) {
6035 LY_ARRAY_FOR(list->uniques, z) {
6036 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6037 nodeid = strpbrk(name, " \t\n");
6038 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02006039 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006040 break;
6041 }
6042 while (isspace(*nodeid)) {
6043 ++nodeid;
6044 }
6045 } else {
6046 if (strcmp(name, list->uniques[z][y]->name)) {
6047 break;
6048 }
6049 }
6050 }
6051 if (!name) {
6052 /* complete match - remove the unique */
6053 LY_ARRAY_DECREMENT(list->uniques);
6054 LY_ARRAY_FREE(list->uniques[z]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006055 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
Radek Krejci7af64242019-02-18 13:07:53 +01006056 --z;
6057 break;
6058 }
6059 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006060 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006061 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006062 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6063 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006064 goto cleanup;
6065 }
6066 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006067 if (!LY_ARRAY_COUNT(list->uniques)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006068 LY_ARRAY_FREE(list->uniques);
6069 list->uniques = NULL;
6070 }
6071 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006072
6073 /* *default-stmt */
6074 if (d_del->dflts) {
6075 switch (devs[u]->target->nodetype) {
6076 case LYS_LEAF:
6077 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6078 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6079 dflt, "deleting", "default", d_del->dflts[0]);
6080
Radek Krejcia1911222019-07-22 17:24:50 +02006081 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006082 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006083 if (strcmp(dflt, d_del->dflts[0])) {
6084 if (i) {
6085 free((char*)dflt);
6086 }
6087 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6088 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6089 d_del->dflts[0], dflt);
6090 goto cleanup;
6091 }
6092 if (i) {
6093 free((char*)dflt);
6094 }
6095 dflt = NULL;
6096
Radek Krejci474f9b82019-07-24 11:36:37 +02006097 /* update the list of incomplete default values if needed */
6098 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6099
Radek Krejcia1911222019-07-22 17:24:50 +02006100 /* remove the default specification */
6101 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6102 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6103 free(leaf->dflt);
6104 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006105 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006106 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006107 break;
6108 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006109 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6110 LY_ARRAY_FOR(d_del->dflts, x) {
6111 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006112 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 +02006113 if (!strcmp(dflt, d_del->dflts[x])) {
6114 if (i) {
6115 free((char*)dflt);
6116 }
6117 dflt = NULL;
6118 break;
6119 }
6120 if (i) {
6121 free((char*)dflt);
6122 }
6123 dflt = NULL;
6124 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006125 if (y == LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02006126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6127 "which does not match any of the target's property values.", d_del->dflts[x]);
6128 goto cleanup;
6129 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006130
6131 /* update the list of incomplete default values if needed */
6132 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6133
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006134 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006135 LY_ARRAY_DECREMENT(llist->dflts);
6136 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6137 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6138 free(llist->dflts[y]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006139 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
6140 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 +02006141 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006142 if (!LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006143 LY_ARRAY_FREE(llist->dflts_mods);
6144 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006145 LY_ARRAY_FREE(llist->dflts);
6146 llist->dflts = NULL;
6147 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006148 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006149 break;
6150 case LYS_CHOICE:
6151 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6152 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6153 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006154 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006155 if (prefix) {
6156 /* use module prefixes from the deviation module to match the module of the default case */
6157 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6158 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006159 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6160 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006161 goto cleanup;
6162 }
6163 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6164 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006165 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6166 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006167 goto cleanup;
6168 }
6169 }
6170 /* else {
6171 * strictly, the default prefix would point to the deviation module, but the value should actually
6172 * match the default string in the original module (usually unprefixed), so in this case we do not check
6173 * the module of the default case, just matching its name */
6174 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6175 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006176 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6177 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006178 goto cleanup;
6179 }
6180 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6181 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6182 break;
6183 default:
6184 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006185 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006186 goto cleanup;
6187 }
6188 }
6189
6190 break;
6191 case LYS_DEV_REPLACE:
6192 d_rpl = (struct lysp_deviate_rpl*)d;
6193
6194 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006195 if (d_rpl->type) {
6196 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6197 /* type is mandatory, so checking for its presence is not necessary */
6198 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006199
6200 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6201 /* the target has default from the previous type - remove it */
6202 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006203 /* update the list of incomplete default values if needed */
6204 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6205
Radek Krejcia1911222019-07-22 17:24:50 +02006206 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6207 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6208 free(leaf->dflt);
6209 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006210 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006211 } else { /* LYS_LEAFLIST */
6212 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006213 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006214 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6215 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6216 free(llist->dflts[x]);
6217 }
6218 LY_ARRAY_FREE(llist->dflts);
6219 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006220 LY_ARRAY_FREE(llist->dflts_mods);
6221 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006222 }
6223 }
6224 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006225 /* there is no default value, do not set changed_type after type compilation
6226 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006227 changed_type = -1;
6228 }
Radek Krejciec4da802019-05-02 13:02:41 +02006229 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 +02006230 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006231 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006232
6233 /* [units-stmt] */
6234 if (d_rpl->units) {
6235 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6236 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6237 units, "replacing", "units", d_rpl->units);
6238
6239 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6240 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6241 }
6242
6243 /* [default-stmt] */
6244 if (d_rpl->dflt) {
6245 switch (devs[u]->target->nodetype) {
6246 case LYS_LEAF:
6247 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6248 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006249 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006250 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
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 dflt = d_rpl->dflt;
6254 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006255 break;
6256 case LYS_CHOICE:
6257 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006258 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 +01006259 goto cleanup;
6260 }
6261 break;
6262 default:
6263 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006264 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006265 goto cleanup;
6266 }
6267 }
6268
6269 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006270 if (d_rpl->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01006271 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006272 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006273 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6274 goto cleanup;
6275 }
6276 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006277 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006278 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6279 goto cleanup;
6280 }
Radek Krejci327de162019-06-14 12:52:07 +02006281 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006282 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006283
6284 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006285 if (d_rpl->flags & LYS_MAND_MASK) {
6286 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006287 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006288 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6289 goto cleanup;
6290 }
Radek Krejci327de162019-06-14 12:52:07 +02006291 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006292 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006293
6294 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006295 if (d_rpl->flags & LYS_SET_MIN) {
6296 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6297 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6298 /* change value */
6299 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6300 } else if (devs[u]->target->nodetype == LYS_LIST) {
6301 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6302 /* change value */
6303 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6304 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006305 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006306 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6307 goto cleanup;
6308 }
6309 if (d_rpl->min) {
6310 devs[u]->target->flags |= LYS_MAND_TRUE;
6311 }
6312 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006313
6314 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006315 if (d_rpl->flags & LYS_SET_MAX) {
6316 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6317 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6318 /* change value */
6319 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6320 } else if (devs[u]->target->nodetype == LYS_LIST) {
6321 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6322 /* change value */
6323 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6324 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006325 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006326 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6327 goto cleanup;
6328 }
6329 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006330
6331 break;
6332 default:
6333 LOGINT(ctx->ctx);
6334 goto cleanup;
6335 }
6336 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006337
Radek Krejci33f72892019-02-21 10:36:58 +01006338 /* final check when all deviations of a single target node are applied */
6339
Radek Krejci551b12c2019-02-19 16:11:21 +01006340 /* check min-max compatibility */
6341 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6342 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6343 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6344 } else if (devs[u]->target->nodetype == LYS_LIST) {
6345 min = ((struct lysc_node_list*)devs[u]->target)->min;
6346 max = ((struct lysc_node_list*)devs[u]->target)->max;
6347 } else {
6348 min = max = 0;
6349 }
6350 if (min > max) {
6351 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 +02006352 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006353 goto cleanup;
6354 }
6355
Radek Krejcia1911222019-07-22 17:24:50 +02006356 if (dflt) {
6357 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006358 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006359 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006360 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6361 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006362 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006363 leaf->dflt->realtype->refcount++;
6364 if (err) {
6365 ly_err_print(err);
6366 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6367 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6368 ly_err_free(err);
6369 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006370 if (rc == LY_EINCOMPLETE) {
6371 /* postpone default compilation when the tree is complete */
6372 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6373
6374 /* but in general result is so far ok */
6375 rc = LY_SUCCESS;
6376 }
Radek Krejcia1911222019-07-22 17:24:50 +02006377 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006378 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006379 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6380 int dynamic;
6381 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006382 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006383
6384 /* update the list of incomplete default values if needed */
6385 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6386
6387 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006388 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6389 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6390 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006391 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6392 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006393 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006394 leaf->dflt->realtype->refcount++;
6395 if (err) {
6396 ly_err_print(err);
6397 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6398 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6399 ly_err_free(err);
6400 }
6401 if (dynamic) {
6402 free((void*)dflt);
6403 }
Radek Krejcia1911222019-07-22 17:24:50 +02006404 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006405 if (rc == LY_EINCOMPLETE) {
6406 /* postpone default compilation when the tree is complete */
6407 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6408
6409 /* but in general result is so far ok */
6410 rc = LY_SUCCESS;
6411 }
6412 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006413 } else { /* LYS_LEAFLIST */
6414 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006415 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 +02006416 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6417 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6418 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006419 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6420 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006421 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6422 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006423 llist->dflts[x]->realtype->refcount++;
6424 if (err) {
6425 ly_err_print(err);
6426 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6427 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6428 dflt, err->msg);
6429 ly_err_free(err);
6430 }
6431 if (dynamic) {
6432 free((void*)dflt);
6433 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006434 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006435 if (rc == LY_EINCOMPLETE) {
6436 /* postpone default compilation when the tree is complete */
6437 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6438
6439 /* but in general result is so far ok */
6440 rc = LY_SUCCESS;
6441 }
Radek Krejcia1911222019-07-22 17:24:50 +02006442 LY_CHECK_GOTO(rc, cleanup);
6443 }
6444 }
6445 }
6446
Radek Krejci551b12c2019-02-19 16:11:21 +01006447 /* check mandatory - default compatibility */
6448 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6449 && (devs[u]->target->flags & LYS_SET_DFLT)
6450 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6451 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006452 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006453 goto cleanup;
6454 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6455 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6456 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006457 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 +01006458 goto cleanup;
6459 }
6460 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6461 /* mandatory node under a default case */
6462 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006463 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6464 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006465 goto cleanup;
6466 }
Radek Krejci33f72892019-02-21 10:36:58 +01006467
Michal Vaskoe6143202020-07-03 13:02:08 +02006468 /* add this module into the target module deviated_by, if not there already */
6469 rc = LY_SUCCESS;
6470 LY_ARRAY_FOR(devs[u]->target->module->compiled->deviated_by, v) {
6471 if (devs[u]->target->module->compiled->deviated_by[v] == mod_p->mod) {
6472 rc = LY_EEXIST;
6473 break;
6474 }
6475 }
6476 if (!rc) {
6477 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup);
6478 *dev_mod = mod_p->mod;
6479 }
Michal Vasko57c10cd2020-05-27 15:57:11 +02006480
Radek Krejci327de162019-06-14 12:52:07 +02006481 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006482 }
6483
Radek Krejci327de162019-06-14 12:52:07 +02006484 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006485 ret = LY_SUCCESS;
6486
6487cleanup:
6488 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6489 LY_ARRAY_FREE(devs[u]->deviates);
6490 free(devs[u]);
6491 }
6492 free(devs);
6493 ly_set_erase(&targets, NULL);
6494 ly_set_erase(&devs_p, NULL);
6495
6496 return ret;
6497}
6498
Radek Krejcib56c7502019-02-13 14:19:54 +01006499/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006500 * @brief Compile the given YANG submodule into the main module.
6501 * @param[in] ctx Compile context
6502 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006503 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6504 */
6505LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006506lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006507{
6508 unsigned int u;
6509 LY_ERR ret = LY_SUCCESS;
6510 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006511 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006512 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006513 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006514
Michal Vasko33ff9422020-07-03 09:50:39 +02006515 if (!mainmod->mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01006516 /* features are compiled directly into the compiled module structure,
6517 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6518 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006519 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6520 LY_CHECK_GOTO(ret, error);
6521 }
Radek Krejci0af46292019-01-11 16:02:31 +01006522
Michal Vasko33ff9422020-07-03 09:50:39 +02006523 if (!mainmod->mod->dis_identities) {
6524 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities);
6525 LY_CHECK_GOTO(ret, error);
6526 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01006527
Radek Krejci474f9b82019-07-24 11:36:37 +02006528 /* data nodes */
6529 LY_LIST_FOR(submod->data, node_p) {
6530 ret = lys_compile_node(ctx, node_p, NULL, 0);
6531 LY_CHECK_GOTO(ret, error);
6532 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006533
Radek Krejciec4da802019-05-02 13:02:41 +02006534 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6535 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006536
Radek Krejcid05cbd92018-12-05 14:26:40 +01006537error:
6538 return ret;
6539}
6540
Radek Krejci335332a2019-09-05 13:03:35 +02006541static void *
6542lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6543{
6544 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6545 if (substmts[u].stmt == stmt) {
6546 return substmts[u].storage;
6547 }
6548 }
6549 return NULL;
6550}
6551
6552LY_ERR
6553lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6554{
6555 LY_ERR ret = LY_EVALID, r;
6556 unsigned int u;
6557 struct lysp_stmt *stmt;
6558 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006559
6560 /* check for invalid substatements */
6561 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006562 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6563 continue;
6564 }
Radek Krejci335332a2019-09-05 13:03:35 +02006565 for (u = 0; substmts[u].stmt; ++u) {
6566 if (substmts[u].stmt == stmt->kw) {
6567 break;
6568 }
6569 }
6570 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006571 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6572 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006573 goto cleanup;
6574 }
Radek Krejci335332a2019-09-05 13:03:35 +02006575 }
6576
Radek Krejciad5963b2019-09-06 16:03:05 +02006577 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6578
Radek Krejci335332a2019-09-05 13:03:35 +02006579 /* keep order of the processing the same as the order in the defined substmts,
6580 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6581 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006582 int stmt_present = 0;
6583
Radek Krejci335332a2019-09-05 13:03:35 +02006584 for (stmt = ext->child; stmt; stmt = stmt->next) {
6585 if (substmts[u].stmt != stmt->kw) {
6586 continue;
6587 }
6588
Radek Krejciad5963b2019-09-06 16:03:05 +02006589 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006590 if (substmts[u].storage) {
6591 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006592 case LY_STMT_STATUS:
6593 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6594 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6595 break;
6596 case LY_STMT_UNITS: {
6597 const char **units;
6598
6599 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6600 /* single item */
6601 if (*((const char **)substmts[u].storage)) {
6602 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6603 goto cleanup;
6604 }
6605 units = (const char **)substmts[u].storage;
6606 } else {
6607 /* sized array */
6608 const char ***units_array = (const char ***)substmts[u].storage;
6609 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6610 }
6611 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6612 break;
6613 }
Radek Krejci335332a2019-09-05 13:03:35 +02006614 case LY_STMT_TYPE: {
6615 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6616 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6617
6618 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6619 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006620 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006621 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6622 goto cleanup;
6623 }
6624 compiled = substmts[u].storage;
6625 } else {
6626 /* sized array */
6627 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6628 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6629 compiled = (void*)type;
6630 }
6631
Radek Krejciad5963b2019-09-06 16:03:05 +02006632 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006633 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6634 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006635 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6636 lysp_type_free(ctx->ctx, parsed);
6637 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006638 break;
6639 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006640 case LY_STMT_IF_FEATURE: {
6641 struct lysc_iffeature *iff = NULL;
6642
6643 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6644 /* single item */
6645 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6646 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6647 goto cleanup;
6648 }
6649 iff = (struct lysc_iffeature*)substmts[u].storage;
6650 } else {
6651 /* sized array */
6652 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6653 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6654 }
6655 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6656 break;
6657 }
6658 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6659 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006660 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006661 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.",
6662 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006663 goto cleanup;
6664 }
6665 }
Radek Krejci335332a2019-09-05 13:03:35 +02006666 }
Radek Krejci335332a2019-09-05 13:03:35 +02006667
Radek Krejciad5963b2019-09-06 16:03:05 +02006668 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6670 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6671 goto cleanup;
6672 }
Radek Krejci335332a2019-09-05 13:03:35 +02006673 }
6674
6675 ret = LY_SUCCESS;
6676
6677cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006678 return ret;
6679}
6680
Michal Vasko175012e2019-11-06 15:49:14 +01006681/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006682 * @brief Check when for cyclic dependencies.
6683 * @param[in] set Set with all the referenced nodes.
6684 * @param[in] node Node whose "when" referenced nodes are in @p set.
6685 * @return LY_ERR value
6686 */
6687static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006688lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006689{
6690 struct lyxp_set tmp_set;
6691 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006692 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006693 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006694 int idx;
6695 struct lysc_when *when;
6696 LY_ERR ret = LY_SUCCESS;
6697
6698 memset(&tmp_set, 0, sizeof tmp_set);
6699
6700 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006701 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006702 xp_scnode = &set->val.scnodes[i];
6703
Michal Vasko5c4e5892019-11-14 12:31:38 +01006704 if (xp_scnode->in_ctx != -1) {
6705 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006706 xp_scnode->in_ctx = 1;
6707 }
6708 }
6709
6710 for (i = 0; i < set->used; ++i) {
6711 xp_scnode = &set->val.scnodes[i];
6712 if (xp_scnode->in_ctx != 1) {
6713 /* already checked */
6714 continue;
6715 }
6716
Michal Vasko1bf09392020-03-27 12:38:10 +01006717 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006718 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006719 /* no when to check */
6720 xp_scnode->in_ctx = 0;
6721 continue;
6722 }
6723
6724 node = xp_scnode->scnode;
6725 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006726 LY_ARRAY_FOR(node->when, u) {
6727 when = node->when[u];
Michal Vasko52927e22020-03-16 17:26:14 +01006728 ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006729 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6730 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006731 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006732 goto cleanup;
6733 }
6734
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006735 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006736 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006737 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006738 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006739 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 +01006740 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006741 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 +01006742 ret = LY_EVALID;
6743 goto cleanup;
6744 }
6745
6746 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006747 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006748 } else {
6749 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006750 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006751 }
6752 }
6753
6754 /* merge this set into the global when set */
6755 lyxp_set_scnode_merge(set, &tmp_set);
6756 }
6757
6758 /* check when of non-data parents as well */
6759 node = node->parent;
6760 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6761
Michal Vasko251f56e2019-11-14 16:06:47 +01006762 /* this node when was checked (xp_scnode could have been reallocd) */
6763 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006764 }
6765
6766cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006767 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006768 return ret;
6769}
6770
6771/**
Michal Vasko175012e2019-11-06 15:49:14 +01006772 * @brief Check when/must expressions of a node on a compiled schema tree.
6773 * @param[in] ctx Compile context.
6774 * @param[in] node Node to check.
6775 * @return LY_ERR value
6776 */
6777static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006778lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006779{
Michal Vasko175012e2019-11-06 15:49:14 +01006780 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006781 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006782 LY_ARRAY_COUNT_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01006783 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006784 struct lysc_when **when = NULL;
6785 struct lysc_must *musts = NULL;
6786 LY_ERR ret = LY_SUCCESS;
6787
6788 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006789 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko175012e2019-11-06 15:49:14 +01006790
6791 switch (node->nodetype) {
6792 case LYS_CONTAINER:
6793 when = ((struct lysc_node_container *)node)->when;
6794 musts = ((struct lysc_node_container *)node)->musts;
6795 break;
6796 case LYS_CHOICE:
6797 when = ((struct lysc_node_choice *)node)->when;
6798 break;
6799 case LYS_LEAF:
6800 when = ((struct lysc_node_leaf *)node)->when;
6801 musts = ((struct lysc_node_leaf *)node)->musts;
6802 break;
6803 case LYS_LEAFLIST:
6804 when = ((struct lysc_node_leaflist *)node)->when;
6805 musts = ((struct lysc_node_leaflist *)node)->musts;
6806 break;
6807 case LYS_LIST:
6808 when = ((struct lysc_node_list *)node)->when;
6809 musts = ((struct lysc_node_list *)node)->musts;
6810 break;
6811 case LYS_ANYXML:
6812 case LYS_ANYDATA:
6813 when = ((struct lysc_node_anydata *)node)->when;
6814 musts = ((struct lysc_node_anydata *)node)->musts;
6815 break;
6816 case LYS_CASE:
6817 when = ((struct lysc_node_case *)node)->when;
6818 break;
6819 case LYS_NOTIF:
6820 musts = ((struct lysc_notif *)node)->musts;
6821 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006822 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006823 case LYS_ACTION:
6824 /* first process input musts */
6825 musts = ((struct lysc_action *)node)->input.musts;
6826 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006827 default:
6828 /* nothing to check */
6829 break;
6830 }
6831
Michal Vasko175012e2019-11-06 15:49:14 +01006832 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006833 LY_ARRAY_FOR(when, u) {
Michal Vasko004d3152020-06-11 19:59:22 +02006834 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 +02006835 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006836 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006837 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006838 goto cleanup;
6839 }
6840
Michal Vaskodc052f32019-11-07 11:11:38 +01006841 ctx->path[0] = '\0';
6842 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006843 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006844 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006845 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6846 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006847
Michal Vaskoecd62de2019-11-13 12:35:11 +01006848 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006849 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006850 schema->name);
6851 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006852
6853 /* check dummy node accessing */
6854 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006855 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006856 ret = LY_EVALID;
6857 goto cleanup;
6858 }
Michal Vasko175012e2019-11-06 15:49:14 +01006859 }
6860 }
6861
Michal Vaskoecd62de2019-11-13 12:35:11 +01006862 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006863 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006864 LY_CHECK_GOTO(ret, cleanup);
6865
Michal Vaskod3678892020-05-21 10:06:58 +02006866 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006867 }
6868
Michal Vasko5d8756a2019-11-07 15:21:00 +01006869check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006870 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006871 LY_ARRAY_FOR(musts, u) {
6872 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 +01006873 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006874 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006875 goto cleanup;
6876 }
6877
Michal Vaskodc052f32019-11-07 11:11:38 +01006878 ctx->path[0] = '\0';
6879 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006880 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006881 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006882 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006883 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006884 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6885 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006886 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006887 }
6888 }
6889
Michal Vaskod3678892020-05-21 10:06:58 +02006890 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006891 }
6892
Michal Vasko1bf09392020-03-27 12:38:10 +01006893 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006894 /* now check output musts */
6895 input_done = 1;
6896 musts = ((struct lysc_action *)node)->output.musts;
6897 opts = LYXP_SCNODE_OUTPUT;
6898 goto check_musts;
6899 }
6900
Michal Vasko175012e2019-11-06 15:49:14 +01006901cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006902 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006903 return ret;
6904}
6905
Michal Vasko8d544252020-03-02 10:19:52 +01006906static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006907lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6908{
6909 const struct lysc_node *target = NULL;
6910 struct ly_path *p;
6911 struct lysc_type *type;
6912
6913 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6914
6915 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02006916 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
6917 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
6918 lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02006919
6920 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006921 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02006922 ly_path_free(node->module->ctx, p);
6923
6924 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
6925 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6926 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
6927 lref->path->expr, lys_nodetype2str(target->nodetype));
6928 return LY_EVALID;
6929 }
6930
6931 /* check status */
6932 ctx->path[0] = '\0';
6933 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6934 ctx->path_len = strlen(ctx->path);
6935 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
6936 return LY_EVALID;
6937 }
6938 ctx->path_len = 1;
6939 ctx->path[1] = '\0';
6940
6941 /* check config */
6942 if (lref->require_instance && (node->flags & LYS_CONFIG_W)) {
6943 if (target->flags & LYS_CONFIG_R) {
6944 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
6945 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
6946 return LY_EVALID;
6947 }
6948 }
6949
6950 /* store the target's type and check for circular chain of leafrefs */
6951 lref->realtype = ((struct lysc_node_leaf *)target)->type;
6952 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
6953 if (type == (struct lysc_type *)lref) {
6954 /* circular chain detected */
6955 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6956 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
6957 return LY_EVALID;
6958 }
6959 }
6960
6961 /* check if leafref and its target are under common if-features */
6962 if (lys_compile_leafref_features_validate(node, target)) {
6963 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6964 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
6965 " features applicable to the leafref itself.", lref->path->expr);
6966 return LY_EVALID;
6967 }
6968
6969 return LY_SUCCESS;
6970}
6971
6972static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01006973lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
6974{
6975 struct lysc_ext_instance *ext;
6976 struct lysp_ext_instance *ext_p = NULL;
6977 struct lysp_stmt *stmt;
6978 const struct lys_module *ext_mod;
6979 LY_ERR ret = LY_SUCCESS;
6980
6981 /* create the parsed extension instance manually */
6982 ext_p = calloc(1, sizeof *ext_p);
6983 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
6984 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
6985 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
6986 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
6987 ext_p->insubstmt_index = 0;
6988
6989 stmt = calloc(1, sizeof *ext_p->child);
6990 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
6991 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
6992 stmt->kw = LY_STMT_TYPE;
6993 ext_p->child = stmt;
6994
6995 /* allocate new extension instance */
6996 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
6997
6998 /* manually get extension definition module */
6999 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7000
7001 /* compile the extension instance */
7002 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7003
7004cleanup:
7005 lysp_ext_instance_free(ctx->ctx, ext_p);
7006 free(ext_p);
7007 return ret;
7008}
7009
Michal Vasko004d3152020-06-11 19:59:22 +02007010static LY_ERR
7011lys_compile_unres(struct lysc_ctx *ctx)
7012{
7013 struct lysc_node *node;
7014 struct lysc_type *type, *typeiter;
7015 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007016 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007017 uint32_t i;
7018
7019 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7020 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7021 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7022 for (i = 0; i < ctx->leafrefs.count; ++i) {
7023 node = ctx->leafrefs.objs[i];
7024 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7025 type = ((struct lysc_node_leaf *)node)->type;
7026 if (type->basetype == LY_TYPE_LEAFREF) {
7027 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7028 } else if (type->basetype == LY_TYPE_UNION) {
7029 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7030 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7031 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7032 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7033 }
7034 }
7035 }
7036 }
7037 for (i = 0; i < ctx->leafrefs.count; ++i) {
7038 /* store pointer to the real type */
7039 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7040 if (type->basetype == LY_TYPE_LEAFREF) {
7041 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7042 typeiter->basetype == LY_TYPE_LEAFREF;
7043 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7044 ((struct lysc_type_leafref*)type)->realtype = typeiter;
7045 } else if (type->basetype == LY_TYPE_UNION) {
7046 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7047 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7048 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7049 typeiter->basetype == LY_TYPE_LEAFREF;
7050 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7051 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7052 }
7053 }
7054 }
7055 }
7056
7057 /* check xpath */
7058 for (i = 0; i < ctx->xpath.count; ++i) {
7059 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7060 }
7061
7062 /* finish incomplete default values compilation */
7063 for (i = 0; i < ctx->dflts.count; ++i) {
7064 struct ly_err_item *err = NULL;
7065 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
7066 LY_ERR ret;
7067 ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
7068 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7069 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7070 if (err) {
7071 ly_err_print(err);
7072 ctx->path[0] = '\0';
7073 lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7074 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7075 "Invalid default - value does not fit the type (%s).", err->msg);
7076 ly_err_free(err);
7077 }
7078 LY_CHECK_RET(ret);
7079 }
7080
7081 return LY_SUCCESS;
7082}
7083
Radek Krejci19a96102018-11-15 13:38:09 +01007084LY_ERR
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007085lys_compile(struct lys_module **mod, int options)
Radek Krejci19a96102018-11-15 13:38:09 +01007086{
7087 struct lysc_ctx ctx = {0};
7088 struct lysc_module *mod_c;
7089 struct lysp_module *sp;
7090 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007091 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007092 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007093 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007094 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007095 uint32_t i;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007096 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007097
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007098 LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007099
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007100 if (!(*mod)->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007101 /* just imported modules are not compiled */
7102 return LY_SUCCESS;
7103 }
7104
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007105 sp = (*mod)->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007106
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007107 ctx.ctx = (*mod)->ctx;
7108 ctx.mod = *mod;
7109 ctx.mod_def = *mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007110 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007111 ctx.path_len = 1;
7112 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007113
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007114 (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
7115 LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
7116 mod_c->mod = *mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007117
Radek Krejciec4da802019-05-02 13:02:41 +02007118 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007119 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007120 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007121 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7122 }
Radek Krejci0935f412019-08-20 16:15:18 +02007123
7124 /* features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007125 if ((*mod)->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007126 /* there is already precompiled array of features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007127 mod_c->features = (*mod)->dis_features;
7128 (*mod)->dis_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007129 } else {
7130 /* features are compiled directly into the compiled module structure,
7131 * 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 +02007132 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007133 LY_CHECK_GOTO(ret, error);
7134 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007135
Radek Krejci0af46292019-01-11 16:02:31 +01007136 /* finish feature compilation, not only for the main module, but also for the submodules.
7137 * Due to possible forward references, it must be done when all the features (including submodules)
7138 * are present. */
7139 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007140 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007141 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7142 }
Radek Krejci327de162019-06-14 12:52:07 +02007143 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007144 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007145 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007146 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007147 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007148 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7149 }
Radek Krejci327de162019-06-14 12:52:07 +02007150 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007151 }
Radek Krejci327de162019-06-14 12:52:07 +02007152 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007153
Michal Vasko33ff9422020-07-03 09:50:39 +02007154 /* identities, work similarly to features with the precompilation */
7155 if ((*mod)->dis_identities) {
7156 mod_c->identities = (*mod)->dis_identities;
7157 (*mod)->dis_identities = NULL;
7158 } else {
7159 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
7160 LY_CHECK_GOTO(ret, error);
7161 }
Radek Krejci19a96102018-11-15 13:38:09 +01007162 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007163 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007164 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007165 lysc_update_path(&ctx, NULL, "{submodule}");
7166 LY_ARRAY_FOR(sp->includes, v) {
7167 if (sp->includes[v].submodule->identities) {
7168 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7169 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities);
7170 LY_CHECK_GOTO(ret, error);
7171 lysc_update_path(&ctx, NULL, NULL);
7172 }
7173 }
Radek Krejci327de162019-06-14 12:52:07 +02007174 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007175
Radek Krejci95710c92019-02-11 15:49:55 +01007176 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007177 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007178 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007179 }
Radek Krejci95710c92019-02-11 15:49:55 +01007180
Radek Krejciec4da802019-05-02 13:02:41 +02007181 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7182 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007183
Radek Krejci95710c92019-02-11 15:49:55 +01007184 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007185 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007186 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007187 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007188 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007189
Radek Krejci474f9b82019-07-24 11:36:37 +02007190 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007191 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007192
Radek Krejci0935f412019-08-20 16:15:18 +02007193 /* extension instances TODO cover extension instances from submodules */
7194 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007195
Michal Vasko004d3152020-06-11 19:59:22 +02007196 /* finish compilation for all unresolved items in the context */
7197 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007198
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007199 /* validate non-instantiated groupings from the parsed schema,
7200 * without it we would accept even the schemas with invalid grouping specification */
7201 ctx.options |= LYSC_OPT_GROUPING;
7202 LY_ARRAY_FOR(sp->groupings, u) {
7203 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007204 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007205 }
7206 }
7207 LY_LIST_FOR(sp->data, node_p) {
7208 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7209 LY_ARRAY_FOR(grps, u) {
7210 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007211 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007212 }
7213 }
7214 }
7215
Radek Krejci474f9b82019-07-24 11:36:37 +02007216 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7217 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7218 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7219 }
7220
Michal Vasko8d544252020-03-02 10:19:52 +01007221#if 0
7222 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7223 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7224 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7225 * the anotation definitions available in the internal schema structure. */
7226 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7227 if (lyp_add_ietf_netconf_annotations(mod)) {
7228 lys_free(mod, NULL, 1, 1);
7229 return NULL;
7230 }
7231 }
7232#endif
7233
7234 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007235 if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
7236 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007237 }
7238
Radek Krejci1c0c3442019-07-23 16:08:47 +02007239 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007240 ly_set_erase(&ctx.xpath, NULL);
7241 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007242 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007243 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007244 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007245
Radek Krejciec4da802019-05-02 13:02:41 +02007246 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007247 lysp_module_free((*mod)->parsed);
7248 (*mod)->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007249 }
7250
Radek Krejciec4da802019-05-02 13:02:41 +02007251 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007252 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007253 for (i = 0; i < ctx.ctx->list.count; ++i) {
7254 m = ctx.ctx->list.objs[i];
Radek Krejci95710c92019-02-11 15:49:55 +01007255 if (m->implemented == 2) {
7256 m->implemented = 1;
7257 }
7258 }
7259 }
7260
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007261 (*mod)->compiled = mod_c;
Radek Krejci19a96102018-11-15 13:38:09 +01007262 return LY_SUCCESS;
7263
7264error:
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007265 lys_feature_precompile_revert(&ctx, *mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007266 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007267 ly_set_erase(&ctx.xpath, NULL);
7268 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007269 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007270 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007271 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007272 lysc_module_free(mod_c, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007273 (*mod)->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007274
7275 /* revert compilation of modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007276 for (i = 0; i < ctx.ctx->list.count; ++i) {
7277 m = ctx.ctx->list.objs[i];
Michal Vasko2947ce12019-12-16 10:37:19 +01007278 if ((m->implemented == 2) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007279 /* revert features list to the precompiled state */
7280 lys_feature_precompile_revert(&ctx, m);
7281 /* mark module as imported-only / not-implemented */
7282 m->implemented = 0;
7283 /* free the compiled version of the module */
7284 lysc_module_free(m->compiled, NULL);
7285 m->compiled = NULL;
7286 }
7287 }
7288
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007289 /* remove the module itself from the context and free it */
7290 ly_set_rm(&ctx.ctx->list, *mod, NULL);
7291 lys_module_free(*mod, NULL);
7292 *mod = NULL;
7293
Radek Krejci19a96102018-11-15 13:38:09 +01007294 return ret;
7295}