blob: 1cfba45b001d346112188bb0182f96c5ddf3ef22 [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) { \
56 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020057 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010058 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
59 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) { \
67 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020068 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010069 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
70 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) { \
78 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020079 for (LY_ARRAY_SIZE_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(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) { \
88 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020089 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010090 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
91 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); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200108 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(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) { \
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200116 for (LY_ARRAY_SIZE_TYPE u__ = 0; u__ < LY_ARRAY_SIZE(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) { \
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200126 for (LY_ARRAY_SIZE_TYPE u__ = 0; u__ < LY_ARRAY_SIZE(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;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200281 LY_ARRAY_SIZE_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100282
Radek Krejcib56c7502019-02-13 14:19:54 +0100283 assert(orig);
284
Radek Krejci19a96102018-11-15 13:38:09 +0100285 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
286 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) {
312 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
313 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
314 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
315 }
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;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200424 LY_ARRAY_SIZE_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 {
443 flist = mod->off_features;
444 }
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;
505 LY_ARRAY_SIZE_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;
530 asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]);
531 prefixed_name = lydict_insert_zc(ctx->ctx, s);
532 u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */
533 break;
534 }
535 }
536 }
537 if (!prefixed_name) {
538 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
539 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
540 goto cleanup;
541 }
542 } else {
543 prefixed_name = ext_p->name;
544 }
545 lysc_update_path(ctx, NULL, prefixed_name);
546
Michal Vasko8d544252020-03-02 10:19:52 +0100547 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200548 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100549 if (!ext_mod) {
550 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
551 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
552 goto cleanup;
553 } else if (!ext_mod->parsed->extensions) {
554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
555 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
556 prefixed_name, ext_mod->name);
557 goto cleanup;
558 }
Radek Krejci7c960162019-09-18 14:16:12 +0200559 }
560 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200561
Michal Vasko5fe75f12020-03-02 13:52:37 +0100562 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200563 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
564 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100565 /* compile extension definition and assign it */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200566 LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def));
Radek Krejci19a96102018-11-15 13:38:09 +0100567 break;
568 }
569 }
Radek Krejci7c960162019-09-18 14:16:12 +0200570 if (!ext->def) {
571 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
572 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
573 goto cleanup;
574 }
Radek Krejci0935f412019-08-20 16:15:18 +0200575
Radek Krejcif56e2a42019-09-09 14:15:25 +0200576 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
577 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
578 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200579 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200580 /* Schema was parsed from YIN and an argument is expected, ... */
581 struct lysp_stmt *stmt = NULL;
582
583 if (ext->def->flags & LYS_YINELEM_TRUE) {
584 /* ... argument was the first XML child element */
585 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
586 /* TODO check namespace of the statement */
587 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
588 stmt = ext_p->child;
589 }
590 }
591 } else {
592 /* ... argument was one of the XML attributes which are represented as child stmt
593 * with LYS_YIN_ATTR flag */
594 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
595 if (!strcmp(stmt->stmt, ext->def->argument)) {
596 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200597 break;
598 }
599 }
600 }
601 if (!stmt) {
602 /* missing extension's argument */
603 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200604 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
605 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200606
607 }
608 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
609 stmt->flags |= LYS_YIN_ARGUMENT;
610 }
Radek Krejci7c960162019-09-18 14:16:12 +0200611 if (prefixed_name != ext_p->name) {
612 lydict_remove(ctx->ctx, ext_p->name);
613 ext_p->name = prefixed_name;
614 if (!ext_p->argument && ext->argument) {
615 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
616 }
617 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200618
Radek Krejci0935f412019-08-20 16:15:18 +0200619 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200620 if (ext->argument) {
621 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
622 }
Radek Krejci7c960162019-09-18 14:16:12 +0200623 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200624 if (ext->argument) {
625 lysc_update_path(ctx, NULL, NULL);
626 }
Radek Krejci0935f412019-08-20 16:15:18 +0200627 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200628 ext_p->compiled = ext;
629
Radek Krejci7c960162019-09-18 14:16:12 +0200630 ret = LY_SUCCESS;
631cleanup:
632 if (prefixed_name && prefixed_name != ext_p->name) {
633 lydict_remove(ctx->ctx, prefixed_name);
634 }
635
Radek Krejcif56e2a42019-09-09 14:15:25 +0200636 lysc_update_path(ctx, NULL, NULL);
637 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200638
Radek Krejci7c960162019-09-18 14:16:12 +0200639 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200640}
641
642/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100643 * @brief Compile information from the if-feature statement
644 * @param[in] ctx Compile context.
645 * @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 +0100646 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
647 * @return LY_ERR value.
648 */
Radek Krejci19a96102018-11-15 13:38:09 +0100649static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200650lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100651{
652 const char *c = *value;
653 int r, rc = EXIT_FAILURE;
654 int i, j, last_not, checkversion = 0;
655 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
656 uint8_t op;
657 struct iff_stack stack = {0, 0, NULL};
658 struct lysc_feature *f;
659
660 assert(c);
661
662 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
663 for (i = j = last_not = 0; c[i]; i++) {
664 if (c[i] == '(') {
665 j++;
666 checkversion = 1;
667 continue;
668 } else if (c[i] == ')') {
669 j--;
670 continue;
671 } else if (isspace(c[i])) {
672 checkversion = 1;
673 continue;
674 }
675
676 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 +0200677 int sp;
678 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
679 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
681 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
682 return LY_EVALID;
683 } else if (!isspace(c[i + r])) {
684 /* feature name starting with the not/and/or */
685 last_not = 0;
686 f_size++;
687 } else if (c[i] == 'n') { /* not operation */
688 if (last_not) {
689 /* double not */
690 expr_size = expr_size - 2;
691 last_not = 0;
692 } else {
693 last_not = 1;
694 }
695 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200696 if (f_exp != f_size) {
697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
698 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
699 return LY_EVALID;
700 }
Radek Krejci19a96102018-11-15 13:38:09 +0100701 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200702
Radek Krejci19a96102018-11-15 13:38:09 +0100703 /* not a not operation */
704 last_not = 0;
705 }
706 i += r;
707 } else {
708 f_size++;
709 last_not = 0;
710 }
711 expr_size++;
712
713 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200714 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100715 i--;
716 break;
717 }
718 i++;
719 }
720 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200721 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100722 /* not matching count of ( and ) */
723 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
724 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
725 return LY_EVALID;
726 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200727 if (f_exp != f_size) {
728 /* features do not match the needed arguments for the logical operations */
729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
730 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
731 "the required number of operands for the operations.", *value);
732 return LY_EVALID;
733 }
Radek Krejci19a96102018-11-15 13:38:09 +0100734
735 if (checkversion || expr_size > 1) {
736 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100737 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100738 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
739 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
740 return LY_EVALID;
741 }
742 }
743
744 /* allocate the memory */
745 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
746 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
747 stack.stack = malloc(expr_size * sizeof *stack.stack);
748 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
749
750 stack.size = expr_size;
751 f_size--; expr_size--; /* used as indexes from now */
752
753 for (i--; i >= 0; i--) {
754 if (c[i] == ')') {
755 /* push it on stack */
756 iff_stack_push(&stack, LYS_IFF_RP);
757 continue;
758 } else if (c[i] == '(') {
759 /* pop from the stack into result all operators until ) */
760 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
761 iff_setop(iff->expr, op, expr_size--);
762 }
763 continue;
764 } else if (isspace(c[i])) {
765 continue;
766 }
767
768 /* end of operator or operand -> find beginning and get what is it */
769 j = i + 1;
770 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
771 i--;
772 }
773 i++; /* go back by one step */
774
775 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
776 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
777 /* double not */
778 iff_stack_pop(&stack);
779 } else {
780 /* not has the highest priority, so do not pop from the stack
781 * as in case of AND and OR */
782 iff_stack_push(&stack, LYS_IFF_NOT);
783 }
784 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
785 /* as for OR - pop from the stack all operators with the same or higher
786 * priority and store them to the result, then push the AND to the stack */
787 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
788 op = iff_stack_pop(&stack);
789 iff_setop(iff->expr, op, expr_size--);
790 }
791 iff_stack_push(&stack, LYS_IFF_AND);
792 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
793 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
794 op = iff_stack_pop(&stack);
795 iff_setop(iff->expr, op, expr_size--);
796 }
797 iff_stack_push(&stack, LYS_IFF_OR);
798 } else {
799 /* feature name, length is j - i */
800
801 /* add it to the expression */
802 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
803
804 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100805 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
806 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
807 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
808 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100809 iff->features[f_size] = f;
810 LY_ARRAY_INCREMENT(iff->features);
811 f_size--;
812 }
813 }
814 while (stack.index) {
815 op = iff_stack_pop(&stack);
816 iff_setop(iff->expr, op, expr_size--);
817 }
818
819 if (++expr_size || ++f_size) {
820 /* not all expected operators and operands found */
821 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
822 "Invalid value \"%s\" of if-feature - processing error.", *value);
823 rc = LY_EINT;
824 } else {
825 rc = LY_SUCCESS;
826 }
827
828error:
829 /* cleanup */
830 iff_stack_clean(&stack);
831
832 return rc;
833}
834
Radek Krejcib56c7502019-02-13 14:19:54 +0100835/**
Michal Vasko175012e2019-11-06 15:49:14 +0100836 * @brief Get the XPath context node for the given schema node.
837 * @param[in] start The schema node where the XPath expression appears.
838 * @return The context node to evaluate XPath expression in given schema node.
839 * @return NULL in case the context node is the root node.
840 */
841static struct lysc_node *
842lysc_xpath_context(struct lysc_node *start)
843{
Michal Vasko1bf09392020-03-27 12:38:10 +0100844 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 +0100845 start = start->parent);
846 return start;
847}
848
849/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100850 * @brief Compile information from the when statement
851 * @param[in] ctx Compile context.
852 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100853 * @param[in] flags Flags of the node with the "when" defiition.
854 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100855 * @param[out] when Pointer where to store pointer to the created compiled when structure.
856 * @return LY_ERR value.
857 */
Radek Krejci19a96102018-11-15 13:38:09 +0100858static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100859lys_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 +0100860{
Radek Krejci19a96102018-11-15 13:38:09 +0100861 LY_ERR ret = LY_SUCCESS;
862
Radek Krejci00b874b2019-02-12 10:54:50 +0100863 *when = calloc(1, sizeof **when);
864 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200865 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200866 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100867 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100868 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
869 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
870 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200871 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100872 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100873
874done:
875 return ret;
876}
877
Radek Krejcib56c7502019-02-13 14:19:54 +0100878/**
879 * @brief Compile information from the must statement
880 * @param[in] ctx Compile context.
881 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100882 * @param[in,out] must Prepared (empty) compiled must structure to fill.
883 * @return LY_ERR value.
884 */
Radek Krejci19a96102018-11-15 13:38:09 +0100885static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200886lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100887{
Radek Krejci19a96102018-11-15 13:38:09 +0100888 LY_ERR ret = LY_SUCCESS;
889
Michal Vasko004d3152020-06-11 19:59:22 +0200890 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100891 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200892 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100893 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
894 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100895 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
896 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200897 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100898
899done:
900 return ret;
901}
902
Radek Krejcib56c7502019-02-13 14:19:54 +0100903/**
904 * @brief Compile information from the import statement
905 * @param[in] ctx Compile context.
906 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100907 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
908 * @return LY_ERR value.
909 */
Radek Krejci19a96102018-11-15 13:38:09 +0100910static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200911lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100912{
Radek Krejci19a96102018-11-15 13:38:09 +0100913 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100914 LY_ERR ret = LY_SUCCESS;
915
916 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200917 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100918 imp->module = imp_p->module;
919
Radek Krejci7f2a5362018-11-28 13:05:37 +0100920 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
921 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100922 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100923 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100924 /* try to use filepath if present */
925 if (imp->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200926 struct ly_in *in;
927 if (ly_in_new_filepath(imp->module->filepath, 0, &in)) {
928 LOGINT(ctx->ctx);
929 } else {
930 mod = lys_parse(ctx->ctx, in, !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
931 if (mod != imp->module) {
932 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", imp->module->filepath, imp->module->name);
933 mod = NULL;
934 }
Radek Krejci19a96102018-11-15 13:38:09 +0100935 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200936 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100937 }
938 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100939 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100940 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100941 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100942 return LY_ENOTFOUND;
943 }
944 }
Radek Krejci19a96102018-11-15 13:38:09 +0100945 }
946
947done:
948 return ret;
949}
950
Radek Krejcib56c7502019-02-13 14:19:54 +0100951/**
952 * @brief Compile information from the identity statement
953 *
954 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
955 *
956 * @param[in] ctx Compile context.
957 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100958 * @param[in] idents List of so far compiled identities to check the name uniqueness.
959 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
960 * @return LY_ERR value.
961 */
Radek Krejci19a96102018-11-15 13:38:09 +0100962static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200963lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, struct lysc_ident *idents, struct lysc_ident *ident)
Radek Krejci19a96102018-11-15 13:38:09 +0100964{
965 unsigned int u;
966 LY_ERR ret = LY_SUCCESS;
967
Radek Krejci327de162019-06-14 12:52:07 +0200968 lysc_update_path(ctx, NULL, ident_p->name);
969
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100970 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100971 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200972 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
973 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200974 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200975 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100976 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
Radek Krejci0935f412019-08-20 16:15:18 +0200977 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100978 ident->flags = ident_p->flags;
979
Radek Krejci327de162019-06-14 12:52:07 +0200980 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100981done:
982 return ret;
983}
984
Radek Krejcib56c7502019-02-13 14:19:54 +0100985/**
986 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
987 *
988 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
989 *
990 * @param[in] ctx Compile context for logging.
991 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
992 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
993 * @return LY_SUCCESS if everything is ok.
994 * @return LY_EVALID if the identity is derived from itself.
995 */
Radek Krejci38222632019-02-12 16:55:05 +0100996static LY_ERR
997lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
998{
999 LY_ERR ret = LY_EVALID;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001000 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001001 struct ly_set recursion = {0};
1002 struct lysc_ident *drv;
1003
1004 if (!derived) {
1005 return LY_SUCCESS;
1006 }
1007
1008 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
1009 if (ident == derived[u]) {
1010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1011 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1012 goto cleanup;
1013 }
1014 ly_set_add(&recursion, derived[u], 0);
1015 }
1016
1017 for (v = 0; v < recursion.count; ++v) {
1018 drv = recursion.objs[v];
1019 if (!drv->derived) {
1020 continue;
1021 }
1022 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
1023 if (ident == drv->derived[u]) {
1024 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1025 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1026 goto cleanup;
1027 }
1028 ly_set_add(&recursion, drv->derived[u], 0);
1029 }
1030 }
1031 ret = LY_SUCCESS;
1032
1033cleanup:
1034 ly_set_erase(&recursion, NULL);
1035 return ret;
1036}
1037
Radek Krejcia3045382018-11-22 14:30:31 +01001038/**
1039 * @brief Find and process the referenced base identities from another identity or identityref
1040 *
Radek Krejciaca74032019-06-04 08:53:06 +02001041 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001042 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1043 * to distinguish these two use cases.
1044 *
1045 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1046 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
1047 * @param[in] ident Referencing identity to work with.
1048 * @param[in] bases Array of bases of identityref to fill in.
1049 * @return LY_ERR value.
1050 */
Radek Krejci19a96102018-11-15 13:38:09 +01001051static LY_ERR
Radek Krejci0a33b042020-05-27 10:05:06 +02001052lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001053{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001054 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001055 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001056 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001057 struct lysc_ident **idref;
1058
1059 assert(ident || bases);
1060
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001061 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001062 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1063 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1064 return LY_EVALID;
1065 }
1066
1067 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
1068 s = strchr(bases_p[u], ':');
1069 if (s) {
1070 /* prefixed identity */
1071 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001072 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001073 } else {
1074 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001075 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001076 }
1077 if (!mod) {
1078 if (ident) {
1079 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1080 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1081 } else {
1082 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1083 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1084 }
1085 return LY_EVALID;
1086 }
1087 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001088 if (mod->compiled && mod->compiled->identities) {
1089 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
1090 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001091 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +01001092 if (ident == &mod->compiled->identities[v]) {
1093 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1094 "Identity \"%s\" is derived from itself.", ident->name);
1095 return LY_EVALID;
1096 }
1097 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001098 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +01001099 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001100 *idref = ident;
1101 } else {
1102 /* we have match! store the found identity */
1103 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01001104 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001105 }
1106 break;
1107 }
1108 }
1109 }
1110 if (!idref || !(*idref)) {
1111 if (ident) {
1112 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1113 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1114 } else {
1115 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1116 "Unable to find base (%s) of identityref.", bases_p[u]);
1117 }
1118 return LY_EVALID;
1119 }
1120 }
1121 return LY_SUCCESS;
1122}
1123
Radek Krejcia3045382018-11-22 14:30:31 +01001124/**
1125 * @brief For the given array of identities, set the backlinks from all their base identities.
1126 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1127 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1128 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1129 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1130 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001131static LY_ERR
1132lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1133{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001134 LY_ARRAY_SIZE_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001135
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001136 for (u = 0; u < LY_ARRAY_SIZE(idents_p); ++u) {
1137 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001138 continue;
1139 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001140 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001141 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 +02001142 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001143 }
1144 return LY_SUCCESS;
1145}
1146
Radek Krejci0af46292019-01-11 16:02:31 +01001147LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001148lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001149{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001150 LY_ARRAY_SIZE_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001151 struct lysc_ctx context = {0};
1152
Radek Krejci327de162019-06-14 12:52:07 +02001153 assert(ctx_sc || ctx);
1154
1155 if (!ctx_sc) {
1156 context.ctx = ctx;
1157 context.mod = module;
1158 context.path_len = 1;
1159 context.path[0] = '/';
1160 ctx_sc = &context;
1161 }
Radek Krejci0af46292019-01-11 16:02:31 +01001162
1163 if (!features_p) {
1164 return LY_SUCCESS;
1165 }
1166 if (*features) {
1167 offset = LY_ARRAY_SIZE(*features);
1168 }
1169
Radek Krejci327de162019-06-14 12:52:07 +02001170 lysc_update_path(ctx_sc, NULL, "{feature}");
1171 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001172 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001173 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1174
Radek Krejci0af46292019-01-11 16:02:31 +01001175 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001176 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci327de162019-06-14 12:52:07 +02001177 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1178 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1179 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001180 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001181 (*features)[offset + u].module = ctx_sc->mod;
1182
1183 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001184 }
Radek Krejci327de162019-06-14 12:52:07 +02001185 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001186
1187 return LY_SUCCESS;
1188}
1189
Radek Krejcia3045382018-11-22 14:30:31 +01001190/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001191 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001192 *
1193 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1194 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001195 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001196 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1197 * being currently processed).
1198 * @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 +01001199 * @return LY_SUCCESS if everything is ok.
1200 * @return LY_EVALID if the feature references indirectly itself.
1201 */
1202static LY_ERR
1203lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1204{
1205 LY_ERR ret = LY_EVALID;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001206 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001207 struct ly_set recursion = {0};
1208 struct lysc_feature *drv;
1209
1210 if (!depfeatures) {
1211 return LY_SUCCESS;
1212 }
1213
1214 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1215 if (feature == depfeatures[u]) {
1216 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1217 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1218 goto cleanup;
1219 }
1220 ly_set_add(&recursion, depfeatures[u], 0);
1221 }
1222
1223 for (v = 0; v < recursion.count; ++v) {
1224 drv = recursion.objs[v];
1225 if (!drv->depfeatures) {
1226 continue;
1227 }
1228 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1229 if (feature == drv->depfeatures[u]) {
1230 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1231 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1232 goto cleanup;
1233 }
1234 ly_set_add(&recursion, drv->depfeatures[u], 0);
1235 }
1236 }
1237 ret = LY_SUCCESS;
1238
1239cleanup:
1240 ly_set_erase(&recursion, NULL);
1241 return ret;
1242}
1243
1244/**
Radek Krejci0af46292019-01-11 16:02:31 +01001245 * @brief Create pre-compiled features array.
1246 *
1247 * See lys_feature_precompile() for more details.
1248 *
Radek Krejcia3045382018-11-22 14:30:31 +01001249 * @param[in] ctx Compile context.
1250 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001251 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001252 * @return LY_ERR value.
1253 */
Radek Krejci19a96102018-11-15 13:38:09 +01001254static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001255lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001256{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001257 LY_ARRAY_SIZE_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001258 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001259 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001260
Radek Krejci327de162019-06-14 12:52:07 +02001261
Radek Krejci0af46292019-01-11 16:02:31 +01001262 /* find the preprecompiled feature */
1263 LY_ARRAY_FOR(features, x) {
1264 if (strcmp(features[x].name, feature_p->name)) {
1265 continue;
1266 }
1267 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001268 lysc_update_path(ctx, NULL, "{feature}");
1269 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001270
Radek Krejci0af46292019-01-11 16:02:31 +01001271 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001272 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001273 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001274 if (feature->iffeatures) {
1275 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1276 if (feature->iffeatures[u].features) {
1277 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001278 /* check for circular dependency - direct reference first,... */
1279 if (feature == feature->iffeatures[u].features[v]) {
1280 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1281 "Feature \"%s\" is referenced from itself.", feature->name);
1282 return LY_EVALID;
1283 }
1284 /* ... and indirect circular reference */
1285 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1286
Radek Krejci0af46292019-01-11 16:02:31 +01001287 /* add itself into the dependants list */
1288 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1289 *df = feature;
1290 }
Radek Krejci19a96102018-11-15 13:38:09 +01001291 }
Radek Krejci19a96102018-11-15 13:38:09 +01001292 }
1293 }
Radek Krejci327de162019-06-14 12:52:07 +02001294 lysc_update_path(ctx, NULL, NULL);
1295 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001296 done:
1297 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001298 }
Radek Krejci0af46292019-01-11 16:02:31 +01001299
1300 LOGINT(ctx->ctx);
1301 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001302}
1303
Radek Krejcib56c7502019-02-13 14:19:54 +01001304/**
1305 * @brief Revert compiled list of features back to the precompiled state.
1306 *
1307 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1308 * The features are supposed to be stored again as off_features in ::lys_module structure.
1309 *
1310 * @param[in] ctx Compilation context.
1311 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1312 * and supposed to hold the off_features list.
1313 */
Radek Krejci95710c92019-02-11 15:49:55 +01001314static void
1315lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1316{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001317 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001318
1319 /* keep the off_features list until the complete lys_module is freed */
1320 mod->off_features = mod->compiled->features;
1321 mod->compiled->features = NULL;
1322
1323 /* in the off_features list, remove all the parts (from finished compiling process)
1324 * which may points into the data being freed here */
1325 LY_ARRAY_FOR(mod->off_features, u) {
1326 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1327 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1328 }
1329 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1330 mod->off_features[u].iffeatures = NULL;
1331
1332 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1333 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1334 }
1335 LY_ARRAY_FREE(mod->off_features[u].exts);
1336 mod->off_features[u].exts = NULL;
1337 }
1338}
1339
Radek Krejcia3045382018-11-22 14:30:31 +01001340/**
1341 * @brief Validate and normalize numeric value from a range definition.
1342 * @param[in] ctx Compile context.
1343 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1344 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1345 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1346 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1347 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1348 * @param[in] value String value of the range boundary.
1349 * @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.
1350 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1351 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1352 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001353LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001354range_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 +01001355{
Radek Krejci6cba4292018-11-15 17:33:29 +01001356 size_t fraction = 0, size;
1357
Radek Krejci19a96102018-11-15 13:38:09 +01001358 *len = 0;
1359
1360 assert(value);
1361 /* parse value */
1362 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1363 return LY_EVALID;
1364 }
1365
1366 if ((value[*len] == '-') || (value[*len] == '+')) {
1367 ++(*len);
1368 }
1369
1370 while (isdigit(value[*len])) {
1371 ++(*len);
1372 }
1373
1374 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001375 if (basetype == LY_TYPE_DEC64) {
1376 goto decimal;
1377 } else {
1378 *valcopy = strndup(value, *len);
1379 return LY_SUCCESS;
1380 }
Radek Krejci19a96102018-11-15 13:38:09 +01001381 }
1382 fraction = *len;
1383
1384 ++(*len);
1385 while (isdigit(value[*len])) {
1386 ++(*len);
1387 }
1388
Radek Krejci6cba4292018-11-15 17:33:29 +01001389 if (basetype == LY_TYPE_DEC64) {
1390decimal:
1391 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001392 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001393 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1394 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1395 *len, value, frdigits);
1396 return LY_EINVAL;
1397 }
1398 if (fraction) {
1399 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1400 } else {
1401 size = (*len) + frdigits + 1;
1402 }
1403 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001404 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1405
Radek Krejci6cba4292018-11-15 17:33:29 +01001406 (*valcopy)[size - 1] = '\0';
1407 if (fraction) {
1408 memcpy(&(*valcopy)[0], &value[0], fraction);
1409 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1410 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1411 } else {
1412 memcpy(&(*valcopy)[0], &value[0], *len);
1413 memset(&(*valcopy)[*len], '0', frdigits);
1414 }
Radek Krejci19a96102018-11-15 13:38:09 +01001415 }
1416 return LY_SUCCESS;
1417}
1418
Radek Krejcia3045382018-11-22 14:30:31 +01001419/**
1420 * @brief Check that values in range are in ascendant order.
1421 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001422 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1423 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001424 * @param[in] value Current value to check.
1425 * @param[in] prev_value The last seen value.
1426 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1427 */
Radek Krejci19a96102018-11-15 13:38:09 +01001428static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001429range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001430{
1431 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001432 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001433 return LY_EEXIST;
1434 }
1435 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001436 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001437 return LY_EEXIST;
1438 }
1439 }
1440 return LY_SUCCESS;
1441}
1442
Radek Krejcia3045382018-11-22 14:30:31 +01001443/**
1444 * @brief Set min/max value of the range part.
1445 * @param[in] ctx Compile context.
1446 * @param[in] part Range part structure to fill.
1447 * @param[in] max Flag to distinguish if storing min or max value.
1448 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1449 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1450 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1451 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1452 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001453 * @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 +01001454 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1455 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1456 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1457 * frdigits value), LY_EMEM.
1458 */
Radek Krejci19a96102018-11-15 13:38:09 +01001459static LY_ERR
1460range_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 +01001461 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001462{
1463 LY_ERR ret = LY_SUCCESS;
1464 char *valcopy = NULL;
1465 size_t len;
1466
1467 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001468 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001469 LY_CHECK_GOTO(ret, finalize);
1470 }
1471 if (!valcopy && base_range) {
1472 if (max) {
1473 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1474 } else {
1475 part->min_64 = base_range->parts[0].min_64;
1476 }
1477 if (!first) {
1478 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1479 }
1480 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001481 }
1482
1483 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001484 case LY_TYPE_INT8: /* range */
1485 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001486 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 +01001487 } else if (max) {
1488 part->max_64 = INT64_C(127);
1489 } else {
1490 part->min_64 = INT64_C(-128);
1491 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001492 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001493 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001494 }
1495 break;
1496 case LY_TYPE_INT16: /* range */
1497 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001498 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 +01001499 } else if (max) {
1500 part->max_64 = INT64_C(32767);
1501 } else {
1502 part->min_64 = INT64_C(-32768);
1503 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001504 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001505 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001506 }
1507 break;
1508 case LY_TYPE_INT32: /* range */
1509 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001510 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 +01001511 } else if (max) {
1512 part->max_64 = INT64_C(2147483647);
1513 } else {
1514 part->min_64 = INT64_C(-2147483648);
1515 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001516 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001517 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001518 }
1519 break;
1520 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001521 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001522 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001523 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001524 max ? &part->max_64 : &part->min_64);
1525 } else if (max) {
1526 part->max_64 = INT64_C(9223372036854775807);
1527 } else {
1528 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1529 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001530 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001531 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001532 }
1533 break;
1534 case LY_TYPE_UINT8: /* range */
1535 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001536 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 +01001537 } else if (max) {
1538 part->max_u64 = UINT64_C(255);
1539 } else {
1540 part->min_u64 = UINT64_C(0);
1541 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001542 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001543 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001544 }
1545 break;
1546 case LY_TYPE_UINT16: /* range */
1547 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001548 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 +01001549 } else if (max) {
1550 part->max_u64 = UINT64_C(65535);
1551 } else {
1552 part->min_u64 = UINT64_C(0);
1553 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001554 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001555 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001556 }
1557 break;
1558 case LY_TYPE_UINT32: /* range */
1559 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001560 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 +01001561 } else if (max) {
1562 part->max_u64 = UINT64_C(4294967295);
1563 } else {
1564 part->min_u64 = UINT64_C(0);
1565 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001566 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001567 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001568 }
1569 break;
1570 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001571 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001572 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001573 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001574 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 +01001575 } else if (max) {
1576 part->max_u64 = UINT64_C(18446744073709551615);
1577 } else {
1578 part->min_u64 = UINT64_C(0);
1579 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001580 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001581 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001582 }
1583 break;
1584 default:
1585 LOGINT(ctx->ctx);
1586 ret = LY_EINT;
1587 }
1588
Radek Krejci5969f272018-11-23 10:03:58 +01001589finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001590 if (ret == LY_EDENIED) {
1591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1592 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1593 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1594 } else if (ret == LY_EVALID) {
1595 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1596 "Invalid %s restriction - invalid value \"%s\".",
1597 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1598 } else if (ret == LY_EEXIST) {
1599 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1600 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001601 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001602 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001603 } else if (!ret && value) {
1604 *value = *value + len;
1605 }
1606 free(valcopy);
1607 return ret;
1608}
1609
Radek Krejcia3045382018-11-22 14:30:31 +01001610/**
1611 * @brief Compile the parsed range restriction.
1612 * @param[in] ctx Compile context.
1613 * @param[in] range_p Parsed range structure to compile.
1614 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1615 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1616 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1617 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1618 * range restriction must be more restrictive than the base_range.
1619 * @param[in,out] range Pointer to the created current range structure.
1620 * @return LY_ERR value.
1621 */
Radek Krejci19a96102018-11-15 13:38:09 +01001622static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001623lys_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 +01001624 struct lysc_range *base_range, struct lysc_range **range)
1625{
1626 LY_ERR ret = LY_EVALID;
1627 const char *expr;
1628 struct lysc_range_part *parts = NULL, *part;
1629 int range_expected = 0, uns;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001630 LY_ARRAY_SIZE_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001631
1632 assert(range);
1633 assert(range_p);
1634
1635 expr = range_p->arg;
1636 while(1) {
1637 if (isspace(*expr)) {
1638 ++expr;
1639 } else if (*expr == '\0') {
1640 if (range_expected) {
1641 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1642 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1643 length_restr ? "length" : "range", range_p->arg);
1644 goto cleanup;
1645 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1646 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1647 "Invalid %s restriction - unexpected end of the expression (%s).",
1648 length_restr ? "length" : "range", range_p->arg);
1649 goto cleanup;
1650 }
1651 parts_done++;
1652 break;
1653 } else if (!strncmp(expr, "min", 3)) {
1654 if (parts) {
1655 /* min cannot be used elsewhere than in the first part */
1656 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1657 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1658 expr - range_p->arg, range_p->arg);
1659 goto cleanup;
1660 }
1661 expr += 3;
1662
1663 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001664 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 +01001665 part->max_64 = part->min_64;
1666 } else if (*expr == '|') {
1667 if (!parts || range_expected) {
1668 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1669 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1670 goto cleanup;
1671 }
1672 expr++;
1673 parts_done++;
1674 /* process next part of the expression */
1675 } else if (!strncmp(expr, "..", 2)) {
1676 expr += 2;
1677 while (isspace(*expr)) {
1678 expr++;
1679 }
1680 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1682 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1683 goto cleanup;
1684 }
1685 /* continue expecting the upper boundary */
1686 range_expected = 1;
1687 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1688 /* number */
1689 if (range_expected) {
1690 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001691 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 +01001692 range_expected = 0;
1693 } else {
1694 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1695 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001696 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001697 part->max_64 = part->min_64;
1698 }
1699
1700 /* continue with possible another expression part */
1701 } else if (!strncmp(expr, "max", 3)) {
1702 expr += 3;
1703 while (isspace(*expr)) {
1704 expr++;
1705 }
1706 if (*expr != '\0') {
1707 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1708 length_restr ? "length" : "range", expr);
1709 goto cleanup;
1710 }
1711 if (range_expected) {
1712 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001713 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 +01001714 range_expected = 0;
1715 } else {
1716 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1717 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001718 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001719 part->min_64 = part->max_64;
1720 }
1721 } else {
1722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1723 length_restr ? "length" : "range", expr);
1724 goto cleanup;
1725 }
1726 }
1727
1728 /* check with the previous range/length restriction */
1729 if (base_range) {
1730 switch (basetype) {
1731 case LY_TYPE_BINARY:
1732 case LY_TYPE_UINT8:
1733 case LY_TYPE_UINT16:
1734 case LY_TYPE_UINT32:
1735 case LY_TYPE_UINT64:
1736 case LY_TYPE_STRING:
1737 uns = 1;
1738 break;
1739 case LY_TYPE_DEC64:
1740 case LY_TYPE_INT8:
1741 case LY_TYPE_INT16:
1742 case LY_TYPE_INT32:
1743 case LY_TYPE_INT64:
1744 uns = 0;
1745 break;
1746 default:
1747 LOGINT(ctx->ctx);
1748 ret = LY_EINT;
1749 goto cleanup;
1750 }
1751 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1752 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1753 goto baseerror;
1754 }
1755 /* current lower bound is not lower than the base */
1756 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1757 /* base has single value */
1758 if (base_range->parts[v].min_64 == parts[u].min_64) {
1759 /* both lower bounds are the same */
1760 if (parts[u].min_64 != parts[u].max_64) {
1761 /* current continues with a range */
1762 goto baseerror;
1763 } else {
1764 /* equal single values, move both forward */
1765 ++v;
1766 continue;
1767 }
1768 } else {
1769 /* base is single value lower than current range, so the
1770 * value from base range is removed in the current,
1771 * move only base and repeat checking */
1772 ++v;
1773 --u;
1774 continue;
1775 }
1776 } else {
1777 /* base is the range */
1778 if (parts[u].min_64 == parts[u].max_64) {
1779 /* current is a single value */
1780 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1781 /* current is behind the base range, so base range is omitted,
1782 * move the base and keep the current for further check */
1783 ++v;
1784 --u;
1785 } /* else it is within the base range, so move the current, but keep the base */
1786 continue;
1787 } else {
1788 /* both are ranges - check the higher bound, the lower was already checked */
1789 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1790 /* higher bound is higher than the current higher bound */
1791 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1792 /* but the current lower bound is also higher, so the base range is omitted,
1793 * continue with the same current, but move the base */
1794 --u;
1795 ++v;
1796 continue;
1797 }
1798 /* current range starts within the base range but end behind it */
1799 goto baseerror;
1800 } else {
1801 /* current range is smaller than the base,
1802 * move current, but stay with the base */
1803 continue;
1804 }
1805 }
1806 }
1807 }
1808 if (u != parts_done) {
1809baseerror:
1810 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1811 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1812 length_restr ? "length" : "range", range_p->arg);
1813 goto cleanup;
1814 }
1815 }
1816
1817 if (!(*range)) {
1818 *range = calloc(1, sizeof **range);
1819 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1820 }
1821
Radek Krejcic8b31002019-01-08 10:24:45 +01001822 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001823 if (range_p->eapptag) {
1824 lydict_remove(ctx->ctx, (*range)->eapptag);
1825 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1826 }
1827 if (range_p->emsg) {
1828 lydict_remove(ctx->ctx, (*range)->emsg);
1829 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1830 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001831 if (range_p->dsc) {
1832 lydict_remove(ctx->ctx, (*range)->dsc);
1833 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1834 }
1835 if (range_p->ref) {
1836 lydict_remove(ctx->ctx, (*range)->ref);
1837 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1838 }
Radek Krejci19a96102018-11-15 13:38:09 +01001839 /* extensions are taken only from the last range by the caller */
1840
1841 (*range)->parts = parts;
1842 parts = NULL;
1843 ret = LY_SUCCESS;
1844cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001845 LY_ARRAY_FREE(parts);
1846
1847 return ret;
1848}
1849
1850/**
1851 * @brief Checks pattern syntax.
1852 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001853 * @param[in] ctx Context.
1854 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001855 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001856 * @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 +01001857 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001858 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001859LY_ERR
1860lys_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 +01001861{
Michal Vasko40a00082020-05-27 15:20:01 +02001862 int idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001863 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001864 int err_code;
1865 const char *orig_ptr;
1866 PCRE2_SIZE err_offset;
1867 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001868#define URANGE_LEN 19
1869 char *ublock2urange[][2] = {
1870 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1871 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1872 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1873 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1874 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1875 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1876 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1877 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1878 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1879 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1880 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1881 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1882 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1883 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1884 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1885 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1886 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1887 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1888 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1889 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1890 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1891 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1892 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1893 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1894 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1895 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1896 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1897 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1898 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1899 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1900 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1901 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1902 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1903 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1904 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1905 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1906 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1907 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1908 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1909 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1910 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1911 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1912 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1913 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1914 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1915 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1916 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1917 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1918 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1919 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1920 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1921 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1922 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1923 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1924 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1925 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1926 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1927 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1928 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1929 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1930 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1931 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1932 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1933 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1934 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1935 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1936 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1937 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1938 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1939 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1940 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1941 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1942 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1943 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1944 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1945 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1946 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1947 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1948 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1949 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1950 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1951 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1952 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1953 {NULL, NULL}
1954 };
1955
1956 /* adjust the expression to a Perl equivalent
1957 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1958
Michal Vasko40a00082020-05-27 15:20:01 +02001959 /* allocate space for the transformed pattern */
1960 size = strlen(pattern) + 1;
1961 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001963 perl_regex[0] = '\0';
1964
Michal Vasko40a00082020-05-27 15:20:01 +02001965 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1966 brack = 0;
1967 idx = 0;
1968 orig_ptr = pattern;
1969 while (orig_ptr[0]) {
1970 switch (orig_ptr[0]) {
1971 case '$':
1972 case '^':
1973 if (!brack) {
1974 /* make space for the extra character */
1975 ++size;
1976 perl_regex = ly_realloc(perl_regex, size);
1977 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001978
Michal Vasko40a00082020-05-27 15:20:01 +02001979 /* print escape slash */
1980 perl_regex[idx] = '\\';
1981 ++idx;
1982 }
1983 break;
1984 case '[':
1985 /* must not be escaped */
1986 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
1987 ++brack;
1988 }
1989 break;
1990 case ']':
1991 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
1992 /* pattern was checked and compiled already */
1993 assert(brack);
1994 --brack;
1995 }
1996 break;
1997 default:
1998 break;
Radek Krejci19a96102018-11-15 13:38:09 +01001999 }
Michal Vasko40a00082020-05-27 15:20:01 +02002000
2001 /* copy char */
2002 perl_regex[idx] = orig_ptr[0];
2003
2004 ++idx;
2005 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002006 }
Michal Vasko40a00082020-05-27 15:20:01 +02002007 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002008
2009 /* substitute Unicode Character Blocks with exact Character Ranges */
2010 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2011 start = ptr - perl_regex;
2012
2013 ptr = strchr(ptr, '}');
2014 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002015 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002016 pattern, perl_regex + start + 2, "unterminated character property");
2017 free(perl_regex);
2018 return LY_EVALID;
2019 }
2020 end = (ptr - perl_regex) + 1;
2021
2022 /* need more space */
2023 if (end - start < URANGE_LEN) {
2024 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002026 }
2027
2028 /* find our range */
2029 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2030 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2031 break;
2032 }
2033 }
2034 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002035 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002036 pattern, perl_regex + start + 5, "unknown block name");
2037 free(perl_regex);
2038 return LY_EVALID;
2039 }
2040
2041 /* 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 +02002042 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002043 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002044 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002045 }
2046 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002047 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002048 }
2049 }
Michal Vasko40a00082020-05-27 15:20:01 +02002050 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002051 /* skip brackets */
2052 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2053 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2054 } else {
2055 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2056 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2057 }
2058 }
2059
2060 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002061 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2062 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002063 &err_code, &err_offset, NULL);
2064 if (!code_local) {
2065 PCRE2_UCHAR err_msg[256] = {0};
2066 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002068 free(perl_regex);
2069 return LY_EVALID;
2070 }
2071 free(perl_regex);
2072
Radek Krejci54579462019-04-30 12:47:06 +02002073 if (code) {
2074 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002075 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002076 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002077 }
2078
2079 return LY_SUCCESS;
2080
2081#undef URANGE_LEN
2082}
2083
Radek Krejcia3045382018-11-22 14:30:31 +01002084/**
2085 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2086 * @param[in] ctx Compile context.
2087 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002088 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2089 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2090 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2091 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2092 */
Radek Krejci19a96102018-11-15 13:38:09 +01002093static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002094lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002095 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2096{
2097 struct lysc_pattern **pattern;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002098 LY_ARRAY_SIZE_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002099 LY_ERR ret = LY_SUCCESS;
2100
2101 /* first, copy the patterns from the base type */
2102 if (base_patterns) {
2103 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2104 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2105 }
2106
2107 LY_ARRAY_FOR(patterns_p, u) {
2108 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2109 *pattern = calloc(1, sizeof **pattern);
2110 ++(*pattern)->refcount;
2111
Michal Vasko03ff5a72019-09-11 13:49:33 +02002112 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002113 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002114
2115 if (patterns_p[u].arg[0] == 0x15) {
2116 (*pattern)->inverted = 1;
2117 }
Radek Krejci54579462019-04-30 12:47:06 +02002118 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002119 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2120 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002121 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2122 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002123 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002124 }
2125done:
2126 return ret;
2127}
2128
Radek Krejcia3045382018-11-22 14:30:31 +01002129/**
2130 * @brief map of the possible restrictions combination for the specific built-in type.
2131 */
Radek Krejci19a96102018-11-15 13:38:09 +01002132static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2133 0 /* LY_TYPE_UNKNOWN */,
2134 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002135 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2136 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2137 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2138 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2139 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002140 LYS_SET_BIT /* LY_TYPE_BITS */,
2141 0 /* LY_TYPE_BOOL */,
2142 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2143 0 /* LY_TYPE_EMPTY */,
2144 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2145 LYS_SET_BASE /* LY_TYPE_IDENT */,
2146 LYS_SET_REQINST /* LY_TYPE_INST */,
2147 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002148 LYS_SET_TYPE /* LY_TYPE_UNION */,
2149 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002150 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002151 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002152 LYS_SET_RANGE /* LY_TYPE_INT64 */
2153};
2154
2155/**
2156 * @brief stringification of the YANG built-in data types
2157 */
2158const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2159 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2160 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002161};
2162
Radek Krejcia3045382018-11-22 14:30:31 +01002163/**
2164 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2165 * @param[in] ctx Compile context.
2166 * @param[in] enums_p Array of the parsed enum structures to compile.
2167 * @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 +01002168 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2169 * @param[out] enums Newly created array of the compiled enums information for the current type.
2170 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2171 */
Radek Krejci19a96102018-11-15 13:38:09 +01002172static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002173lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002174 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002175{
2176 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002177 LY_ARRAY_SIZE_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002178 int32_t value = 0;
2179 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002180 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002181
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002182 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002183 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2184 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2185 return LY_EVALID;
2186 }
2187
2188 LY_ARRAY_FOR(enums_p, u) {
2189 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2190 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002191 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2192 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002193 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002194 if (base_enums) {
2195 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2196 LY_ARRAY_FOR(base_enums, v) {
2197 if (!strcmp(e->name, base_enums[v].name)) {
2198 break;
2199 }
2200 }
2201 if (v == LY_ARRAY_SIZE(base_enums)) {
2202 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2203 "Invalid %s - derived type adds new item \"%s\".",
2204 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2205 return LY_EVALID;
2206 }
2207 match = v;
2208 }
2209
2210 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002211 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002212 if (enums_p[u].flags & LYS_SET_VALUE) {
2213 e->value = (int32_t)enums_p[u].value;
2214 if (!u || e->value >= value) {
2215 value = e->value + 1;
2216 }
2217 /* check collision with other values */
2218 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2219 if (e->value == (*enums)[v].value) {
2220 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2221 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2222 e->value, e->name, (*enums)[v].name);
2223 return LY_EVALID;
2224 }
2225 }
2226 } else if (base_enums) {
2227 /* inherit the assigned value */
2228 e->value = base_enums[match].value;
2229 if (!u || e->value >= value) {
2230 value = e->value + 1;
2231 }
2232 } else {
2233 /* assign value automatically */
2234 if (u && value == INT32_MIN) {
2235 /* counter overflow */
2236 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2237 "Invalid enumeration - it is not possible to auto-assign enum value for "
2238 "\"%s\" since the highest value is already 2147483647.", e->name);
2239 return LY_EVALID;
2240 }
2241 e->value = value++;
2242 }
2243 } else { /* LY_TYPE_BITS */
2244 if (enums_p[u].flags & LYS_SET_VALUE) {
2245 e->value = (int32_t)enums_p[u].value;
2246 if (!u || (uint32_t)e->value >= position) {
2247 position = (uint32_t)e->value + 1;
2248 }
2249 /* check collision with other values */
2250 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2251 if (e->value == (*enums)[v].value) {
2252 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2253 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2254 (uint32_t)e->value, e->name, (*enums)[v].name);
2255 return LY_EVALID;
2256 }
2257 }
2258 } else if (base_enums) {
2259 /* inherit the assigned value */
2260 e->value = base_enums[match].value;
2261 if (!u || (uint32_t)e->value >= position) {
2262 position = (uint32_t)e->value + 1;
2263 }
2264 } else {
2265 /* assign value automatically */
2266 if (u && position == 0) {
2267 /* counter overflow */
2268 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2269 "Invalid bits - it is not possible to auto-assign bit position for "
2270 "\"%s\" since the highest value is already 4294967295.", e->name);
2271 return LY_EVALID;
2272 }
2273 e->value = position++;
2274 }
2275 }
2276
2277 if (base_enums) {
2278 /* the assigned values must not change from the derived type */
2279 if (e->value != base_enums[match].value) {
2280 if (basetype == LY_TYPE_ENUM) {
2281 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2282 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2283 e->name, base_enums[match].value, e->value);
2284 } else {
2285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2286 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2287 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2288 }
2289 return LY_EVALID;
2290 }
2291 }
2292
Radek Krejciec4da802019-05-02 13:02:41 +02002293 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002294 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 +01002295
2296 if (basetype == LY_TYPE_BITS) {
2297 /* keep bits ordered by position */
2298 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2299 if (v != u) {
2300 memcpy(&storage, e, sizeof *e);
2301 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2302 memcpy(&(*enums)[v], &storage, sizeof storage);
2303 }
2304 }
2305 }
2306
2307done:
2308 return ret;
2309}
2310
Radek Krejcia3045382018-11-22 14:30:31 +01002311/**
2312 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2313 *
2314 * path-arg = absolute-path / relative-path
2315 * absolute-path = 1*("/" (node-identifier *path-predicate))
2316 * relative-path = 1*(".." "/") descendant-path
2317 *
2318 * @param[in,out] path Path to parse.
2319 * @param[out] prefix Prefix of the token, NULL if there is not any.
2320 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2321 * @param[out] name Name of the token.
2322 * @param[out] nam_len Length of the name.
2323 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2324 * must not be changed between consecutive calls. -1 if the
2325 * path is absolute.
2326 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2327 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2328 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002329LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002330lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2331 int *parent_times, int *has_predicate)
2332{
2333 int par_times = 0;
2334
2335 assert(path && *path);
2336 assert(parent_times);
2337 assert(prefix);
2338 assert(prefix_len);
2339 assert(name);
2340 assert(name_len);
2341 assert(has_predicate);
2342
2343 *prefix = NULL;
2344 *prefix_len = 0;
2345 *name = NULL;
2346 *name_len = 0;
2347 *has_predicate = 0;
2348
2349 if (!*parent_times) {
2350 if (!strncmp(*path, "..", 2)) {
2351 *path += 2;
2352 ++par_times;
2353 while (!strncmp(*path, "/..", 3)) {
2354 *path += 3;
2355 ++par_times;
2356 }
2357 }
2358 if (par_times) {
2359 *parent_times = par_times;
2360 } else {
2361 *parent_times = -1;
2362 }
2363 }
2364
2365 if (**path != '/') {
2366 return LY_EINVAL;
2367 }
2368 /* skip '/' */
2369 ++(*path);
2370
2371 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002372 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002373
2374 if ((**path == '/' && (*path)[1]) || !**path) {
2375 /* path continues by another token or this is the last token */
2376 return LY_SUCCESS;
2377 } else if ((*path)[0] != '[') {
2378 /* unexpected character */
2379 return LY_EINVAL;
2380 } else {
2381 /* predicate starting with [ */
2382 *has_predicate = 1;
2383 return LY_SUCCESS;
2384 }
2385}
2386
2387/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002388 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2389 *
2390 * The set of features used for target must be a subset of features used for the leafref.
2391 * This is not a perfect, we should compare the truth tables but it could require too much resources
2392 * and RFC 7950 does not require it explicitely, so we simplify that.
2393 *
2394 * @param[in] refnode The leafref node.
2395 * @param[in] target Tha target node of the leafref.
2396 * @return LY_SUCCESS or LY_EVALID;
2397 */
2398static LY_ERR
2399lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2400{
2401 LY_ERR ret = LY_EVALID;
2402 const struct lysc_node *iter;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002403 LY_ARRAY_SIZE_TYPE u, v, count;
Radek Krejci58d171e2018-11-23 13:50:55 +01002404 struct ly_set features = {0};
2405
2406 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002407 if (iter->iffeatures) {
2408 LY_ARRAY_FOR(iter->iffeatures, u) {
2409 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2410 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002411 }
2412 }
2413 }
2414 }
2415
2416 /* we should have, in features set, a superset of features applicable to the target node.
2417 * So when adding features applicable to the target into the features set, we should not be
2418 * able to actually add any new feature, otherwise it is not a subset of features applicable
2419 * to the leafref itself. */
2420 count = features.count;
2421 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002422 if (iter->iffeatures) {
2423 LY_ARRAY_FOR(iter->iffeatures, u) {
2424 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2425 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002426 /* new feature was added (or LY_EMEM) */
2427 goto cleanup;
2428 }
2429 }
2430 }
2431 }
2432 }
2433 ret = LY_SUCCESS;
2434
2435cleanup:
2436 ly_set_erase(&features, NULL);
2437 return ret;
2438}
2439
Michal Vasko004d3152020-06-11 19:59:22 +02002440static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2441 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2442 struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002443
Radek Krejcia3045382018-11-22 14:30:31 +01002444/**
2445 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2446 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002447 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2448 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2449 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2450 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002451 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002452 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002453 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002454 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2455 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2456 * @param[out] type Newly created type structure with the filled information about the type.
2457 * @return LY_ERR value.
2458 */
Radek Krejci19a96102018-11-15 13:38:09 +01002459static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002460lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2461 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2462 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2463 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002464{
2465 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002466 struct lysc_type_bin *bin;
2467 struct lysc_type_num *num;
2468 struct lysc_type_str *str;
2469 struct lysc_type_bits *bits;
2470 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002471 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002472 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002473 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002474 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002475
2476 switch (basetype) {
2477 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002478 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002479
2480 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002481 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002482 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2483 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002484 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002485 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 +01002486 }
2487 }
2488
2489 if (tpdfname) {
2490 type_p->compiled = *type;
2491 *type = calloc(1, sizeof(struct lysc_type_bin));
2492 }
2493 break;
2494 case LY_TYPE_BITS:
2495 /* RFC 7950 9.7 - bits */
2496 bits = (struct lysc_type_bits*)(*type);
2497 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002498 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002499 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2500 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002501 }
2502
Radek Krejci555cb5b2018-11-16 14:54:33 +01002503 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002504 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002505 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002506 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002507 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002509 free(*type);
2510 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002511 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002512 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002513 }
2514
2515 if (tpdfname) {
2516 type_p->compiled = *type;
2517 *type = calloc(1, sizeof(struct lysc_type_bits));
2518 }
2519 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002520 case LY_TYPE_DEC64:
2521 dec = (struct lysc_type_dec*)(*type);
2522
2523 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002524 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002525 if (!type_p->fraction_digits) {
2526 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002527 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002528 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002529 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002530 free(*type);
2531 *type = NULL;
2532 }
2533 return LY_EVALID;
2534 }
2535 } else if (type_p->fraction_digits) {
2536 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002537 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002538 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2539 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002540 tpdfname);
2541 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002542 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2543 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002544 free(*type);
2545 *type = NULL;
2546 }
2547 return LY_EVALID;
2548 }
2549 dec->fraction_digits = type_p->fraction_digits;
2550
2551 /* RFC 7950 9.2.4 - range */
2552 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002553 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2554 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002555 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002556 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 +01002557 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002558 }
2559
2560 if (tpdfname) {
2561 type_p->compiled = *type;
2562 *type = calloc(1, sizeof(struct lysc_type_dec));
2563 }
2564 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002565 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002566 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002567
2568 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002569 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002570 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2571 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002572 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002573 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 +01002574 }
2575 } else if (base && ((struct lysc_type_str*)base)->length) {
2576 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2577 }
2578
2579 /* RFC 7950 9.4.5 - pattern */
2580 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002581 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002582 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002583 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2584 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2585 }
2586
2587 if (tpdfname) {
2588 type_p->compiled = *type;
2589 *type = calloc(1, sizeof(struct lysc_type_str));
2590 }
2591 break;
2592 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002593 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002594
2595 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002596 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002597 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002598 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002599 }
2600
Radek Krejci555cb5b2018-11-16 14:54:33 +01002601 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002602 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002603 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002604 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002605 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002606 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002607 free(*type);
2608 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002609 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002610 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002611 }
2612
2613 if (tpdfname) {
2614 type_p->compiled = *type;
2615 *type = calloc(1, sizeof(struct lysc_type_enum));
2616 }
2617 break;
2618 case LY_TYPE_INT8:
2619 case LY_TYPE_UINT8:
2620 case LY_TYPE_INT16:
2621 case LY_TYPE_UINT16:
2622 case LY_TYPE_INT32:
2623 case LY_TYPE_UINT32:
2624 case LY_TYPE_INT64:
2625 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002626 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002627
2628 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002629 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002630 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2631 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002632 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002633 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 +01002634 }
2635 }
2636
2637 if (tpdfname) {
2638 type_p->compiled = *type;
2639 *type = calloc(1, sizeof(struct lysc_type_num));
2640 }
2641 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002642 case LY_TYPE_IDENT:
2643 idref = (struct lysc_type_identityref*)(*type);
2644
2645 /* RFC 7950 9.10.2 - base */
2646 if (type_p->bases) {
2647 if (base) {
2648 /* only the directly derived identityrefs can contain base specification */
2649 if (tpdfname) {
2650 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002651 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002652 tpdfname);
2653 } else {
2654 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002655 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002656 free(*type);
2657 *type = NULL;
2658 }
2659 return LY_EVALID;
2660 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002661 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002662 }
2663
2664 if (!base && !type_p->flags) {
2665 /* type derived from identityref built-in type must contain at least one base */
2666 if (tpdfname) {
2667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2668 } else {
2669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2670 free(*type);
2671 *type = NULL;
2672 }
2673 return LY_EVALID;
2674 }
2675
2676 if (tpdfname) {
2677 type_p->compiled = *type;
2678 *type = calloc(1, sizeof(struct lysc_type_identityref));
2679 }
2680 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002681 case LY_TYPE_LEAFREF:
Michal Vasko004d3152020-06-11 19:59:22 +02002682 lref = (struct lysc_type_leafref*)*type;
2683
Radek Krejcia3045382018-11-22 14:30:31 +01002684 /* RFC 7950 9.9.3 - require-instance */
2685 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002686 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002687 if (tpdfname) {
2688 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2689 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2690 } else {
2691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2692 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2693 free(*type);
2694 *type = NULL;
2695 }
2696 return LY_EVALID;
2697 }
Michal Vasko004d3152020-06-11 19:59:22 +02002698 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002699 } else if (base) {
2700 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002701 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002702 } else {
2703 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002704 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002705 }
2706 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002707 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2708 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002709 } else if (base) {
Michal Vasko004d3152020-06-11 19:59:22 +02002710 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path);
2711 lref->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002712 } else if (tpdfname) {
2713 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2714 return LY_EVALID;
2715 } else {
2716 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2717 free(*type);
2718 *type = NULL;
2719 return LY_EVALID;
2720 }
2721 if (tpdfname) {
2722 type_p->compiled = *type;
2723 *type = calloc(1, sizeof(struct lysc_type_leafref));
2724 }
2725 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002726 case LY_TYPE_INST:
2727 /* RFC 7950 9.9.3 - require-instance */
2728 if (type_p->flags & LYS_SET_REQINST) {
2729 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2730 } else {
2731 /* default is true */
2732 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2733 }
2734
2735 if (tpdfname) {
2736 type_p->compiled = *type;
2737 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2738 }
2739 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002740 case LY_TYPE_UNION:
2741 un = (struct lysc_type_union*)(*type);
2742
2743 /* RFC 7950 7.4 - type */
2744 if (type_p->types) {
2745 if (base) {
2746 /* only the directly derived union can contain types specification */
2747 if (tpdfname) {
2748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2749 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2750 tpdfname);
2751 } else {
2752 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2753 "Invalid type substatement for the type not directly derived from union built-in type.");
2754 free(*type);
2755 *type = NULL;
2756 }
2757 return LY_EVALID;
2758 }
2759 /* compile the type */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002760 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002761 for (LY_ARRAY_SIZE_TYPE u = 0, additional = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002762 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
2763 &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002764 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2765 /* add space for additional types from the union subtype */
2766 un_aux = (struct lysc_type_union *)un->types[u + additional];
Radek Krejcic4fa0292020-05-14 10:54:49 +02002767 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t*)(type_p->types) - 1)) + additional + LY_ARRAY_SIZE(un_aux->types) - 1,
2768 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002769
2770 /* copy subtypes of the subtype union */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002771 for (LY_ARRAY_SIZE_TYPE v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002772 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2773 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2774 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2775 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 +02002776 lref = (struct lysc_type_leafref *)un->types[u + additional];
2777
2778 lref->basetype = LY_TYPE_LEAFREF;
2779 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path);
2780 lref->refcount = 1;
2781 lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2782 lref->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002783 /* TODO extensions */
2784
2785 } else {
2786 un->types[u + additional] = un_aux->types[v];
2787 ++un_aux->types[v]->refcount;
2788 }
2789 ++additional;
2790 LY_ARRAY_INCREMENT(un->types);
2791 }
2792 /* compensate u increment in main loop */
2793 --additional;
2794
2795 /* free the replaced union subtype */
2796 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2797 } else {
2798 LY_ARRAY_INCREMENT(un->types);
2799 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002800 }
2801 }
2802
2803 if (!base && !type_p->flags) {
2804 /* type derived from union built-in type must contain at least one type */
2805 if (tpdfname) {
2806 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2807 } else {
2808 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2809 free(*type);
2810 *type = NULL;
2811 }
2812 return LY_EVALID;
2813 }
2814
2815 if (tpdfname) {
2816 type_p->compiled = *type;
2817 *type = calloc(1, sizeof(struct lysc_type_union));
2818 }
2819 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002820 case LY_TYPE_BOOL:
2821 case LY_TYPE_EMPTY:
2822 case LY_TYPE_UNKNOWN: /* just to complete switch */
2823 break;
2824 }
2825 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2826done:
2827 return ret;
2828}
2829
Radek Krejcia3045382018-11-22 14:30:31 +01002830/**
2831 * @brief Compile information about the leaf/leaf-list's type.
2832 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002833 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2834 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2835 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2836 * @param[in] context_name Name of the context node or referencing typedef for logging.
2837 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002838 * @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 +01002839 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002840 * @return LY_ERR value.
2841 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002842static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002843lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2844 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2845 struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002846{
2847 LY_ERR ret = LY_SUCCESS;
2848 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002849 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002850 struct type_context {
2851 const struct lysp_tpdf *tpdf;
2852 struct lysp_node *node;
2853 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002854 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002855 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002856 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002857 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002858 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002859 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002860
2861 (*type) = NULL;
2862
2863 tctx = calloc(1, sizeof *tctx);
2864 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002865 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002866 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2867 ret == LY_SUCCESS;
2868 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2869 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2870 if (basetype) {
2871 break;
2872 }
2873
2874 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002875 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2876 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002877 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2878
Radek Krejcicdfecd92018-11-26 11:27:32 +01002879 if (units && !*units) {
2880 /* inherit units */
2881 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2882 }
Radek Krejci01342af2019-01-03 15:18:08 +01002883 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002884 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002885 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02002886 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002887 }
Radek Krejci01342af2019-01-03 15:18:08 +01002888 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002889 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2890 break;
2891 }
2892
Radek Krejci19a96102018-11-15 13:38:09 +01002893 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002894 /* it is not necessary to continue, the rest of the chain was already compiled,
2895 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002896 basetype = tctx->tpdf->type.compiled->basetype;
2897 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002898 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002899 dummyloops = 1;
2900 goto preparenext;
2901 } else {
2902 tctx = NULL;
2903 break;
2904 }
Radek Krejci19a96102018-11-15 13:38:09 +01002905 }
2906
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002907 /* circular typedef reference detection */
2908 for (u = 0; u < tpdf_chain.count; u++) {
2909 /* local part */
2910 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2911 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2912 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2913 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2914 free(tctx);
2915 ret = LY_EVALID;
2916 goto cleanup;
2917 }
2918 }
2919 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2920 /* global part for unions corner case */
2921 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2922 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2923 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2924 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2925 free(tctx);
2926 ret = LY_EVALID;
2927 goto cleanup;
2928 }
2929 }
2930
Radek Krejci19a96102018-11-15 13:38:09 +01002931 /* store information for the following processing */
2932 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2933
Radek Krejcicdfecd92018-11-26 11:27:32 +01002934preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002935 /* prepare next loop */
2936 tctx_prev = tctx;
2937 tctx = calloc(1, sizeof *tctx);
2938 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2939 }
2940 free(tctx);
2941
2942 /* allocate type according to the basetype */
2943 switch (basetype) {
2944 case LY_TYPE_BINARY:
2945 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002946 break;
2947 case LY_TYPE_BITS:
2948 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002949 break;
2950 case LY_TYPE_BOOL:
2951 case LY_TYPE_EMPTY:
2952 *type = calloc(1, sizeof(struct lysc_type));
2953 break;
2954 case LY_TYPE_DEC64:
2955 *type = calloc(1, sizeof(struct lysc_type_dec));
2956 break;
2957 case LY_TYPE_ENUM:
2958 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002959 break;
2960 case LY_TYPE_IDENT:
2961 *type = calloc(1, sizeof(struct lysc_type_identityref));
2962 break;
2963 case LY_TYPE_INST:
2964 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2965 break;
2966 case LY_TYPE_LEAFREF:
2967 *type = calloc(1, sizeof(struct lysc_type_leafref));
2968 break;
2969 case LY_TYPE_STRING:
2970 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002971 break;
2972 case LY_TYPE_UNION:
2973 *type = calloc(1, sizeof(struct lysc_type_union));
2974 break;
2975 case LY_TYPE_INT8:
2976 case LY_TYPE_UINT8:
2977 case LY_TYPE_INT16:
2978 case LY_TYPE_UINT16:
2979 case LY_TYPE_INT32:
2980 case LY_TYPE_UINT32:
2981 case LY_TYPE_INT64:
2982 case LY_TYPE_UINT64:
2983 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002984 break;
2985 case LY_TYPE_UNKNOWN:
2986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2987 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2988 ret = LY_EVALID;
2989 goto cleanup;
2990 }
2991 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002992 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002993 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2994 ly_data_type2str[basetype]);
2995 free(*type);
2996 (*type) = NULL;
2997 ret = LY_EVALID;
2998 goto cleanup;
2999 }
3000
3001 /* get restrictions from the referred typedefs */
3002 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3003 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003004
3005 /* remember the typedef context for circular check */
3006 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3007
Radek Krejci43699232018-11-23 14:59:46 +01003008 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003009 base = tctx->tpdf->type.compiled;
3010 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003011 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003012 /* no change, just use the type information from the base */
3013 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3014 ++base->refcount;
3015 continue;
3016 }
3017
3018 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003019 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3021 tctx->tpdf->name, ly_data_type2str[basetype]);
3022 ret = LY_EVALID;
3023 goto cleanup;
3024 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3025 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3026 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3027 tctx->tpdf->name, tctx->tpdf->dflt);
3028 ret = LY_EVALID;
3029 goto cleanup;
3030 }
3031
Radek Krejci19a96102018-11-15 13:38:09 +01003032 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003033 /* TODO user type plugins */
3034 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003035 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003036 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 +01003037 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003038 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003039 LY_CHECK_GOTO(ret, cleanup);
3040 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003041 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003042 /* remove the processed typedef contexts from the stack for circular check */
3043 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003044
Radek Krejcic5c27e52018-11-15 14:38:11 +01003045 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003046 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003047 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003048 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003049 /* TODO user type plugins */
3050 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003051 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003052 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 +01003053 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003054 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003055 /* no specific restriction in leaf's type definition, copy from the base */
3056 free(*type);
3057 (*type) = base;
3058 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003059 }
Radek Krejcia1911222019-07-22 17:24:50 +02003060 if (dflt && !(*type)->dflt) {
3061 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003062 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003063 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3064 (*type)->dflt->realtype = (*type);
3065 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3066 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003067 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003068 if (err) {
3069 ly_err_print(err);
3070 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3071 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3072 ly_err_free(err);
3073 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003074 if (ret == LY_EINCOMPLETE) {
3075 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003076 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003077
3078 /* but in general result is so far ok */
3079 ret = LY_SUCCESS;
3080 }
Radek Krejcia1911222019-07-22 17:24:50 +02003081 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003082 }
Radek Krejci19a96102018-11-15 13:38:09 +01003083
Radek Krejci0935f412019-08-20 16:15:18 +02003084 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003085
3086cleanup:
3087 ly_set_erase(&tpdf_chain, free);
3088 return ret;
3089}
3090
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003091/**
3092 * @brief Compile status information of the given node.
3093 *
3094 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3095 * has the status correctly set during the compilation.
3096 *
3097 * @param[in] ctx Compile context
3098 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3099 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3100 * the compatibility with the parent's status value.
3101 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3102 * @return LY_ERR value.
3103 */
3104static LY_ERR
3105lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3106{
3107 /* status - it is not inherited by specification, but it does not make sense to have
3108 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3109 if (!((*node_flags) & LYS_STATUS_MASK)) {
3110 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3111 if ((parent_flags & 0x3) != 0x3) {
3112 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3113 * combination of bits (0x3) which marks the uses_status value */
3114 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3115 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3116 }
3117 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3118 } else {
3119 (*node_flags) |= LYS_STATUS_CURR;
3120 }
3121 } else if (parent_flags & LYS_STATUS_MASK) {
3122 /* check status compatibility with the parent */
3123 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3124 if ((*node_flags) & LYS_STATUS_CURR) {
3125 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3126 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3127 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3128 } else { /* LYS_STATUS_DEPRC */
3129 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3130 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3131 }
3132 return LY_EVALID;
3133 }
3134 }
3135 return LY_SUCCESS;
3136}
3137
Radek Krejci8cce8532019-03-05 11:27:45 +01003138/**
3139 * @brief Check uniqness of the node/action/notification name.
3140 *
3141 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3142 * structures, but they share the namespace so we need to check their name collisions.
3143 *
3144 * @param[in] ctx Compile context.
3145 * @param[in] children List (linked list) of data nodes to go through.
3146 * @param[in] actions List (sized array) of actions or RPCs to go through.
3147 * @param[in] notifs List (sized array) of Notifications to go through.
3148 * @param[in] name Name of the item to find in the given lists.
3149 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3150 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3151 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3152 */
3153static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02003154lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions,
3155 const struct lysc_notif *notifs, const char *name, void *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003156{
3157 const struct lysc_node *iter;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003158 LY_ARRAY_SIZE_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003159
3160 LY_LIST_FOR(children, iter) {
3161 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3162 goto error;
3163 }
3164 }
3165 LY_ARRAY_FOR(actions, u) {
3166 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3167 goto error;
3168 }
3169 }
3170 LY_ARRAY_FOR(notifs, u) {
3171 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3172 goto error;
3173 }
3174 }
3175 return LY_SUCCESS;
3176error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003177 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003178 return LY_EEXIST;
3179}
3180
Radek Krejciec4da802019-05-02 13:02:41 +02003181static 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 +01003182
Radek Krejcia3045382018-11-22 14:30:31 +01003183/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003184 * @brief Compile parsed RPC/action schema node information.
3185 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003186 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003187 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003188 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3189 * @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).
3190 * Zero means no uses, non-zero value with no status bit set mean the default status.
3191 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3192 */
3193static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003194lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003195 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3196{
3197 LY_ERR ret = LY_SUCCESS;
3198 struct lysp_node *child_p;
3199 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003200 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003201
Radek Krejci327de162019-06-14 12:52:07 +02003202 lysc_update_path(ctx, parent, action_p->name);
3203
Radek Krejci8cce8532019-03-05 11:27:45 +01003204 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3205 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3206 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3207 action_p->name, action)) {
3208 return LY_EVALID;
3209 }
3210
Radek Krejciec4da802019-05-02 13:02:41 +02003211 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003212 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003213 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003214 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003215 return LY_EVALID;
3216 }
3217
Michal Vasko1bf09392020-03-27 12:38:10 +01003218 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003219 action->module = ctx->mod;
3220 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003221 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003222 action->sp = action_p;
3223 }
3224 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3225
3226 /* status - it is not inherited by specification, but it does not make sense to have
3227 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003228 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003229
3230 DUP_STRING(ctx->ctx, action_p->name, action->name);
3231 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3232 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003233 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003234 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003235
3236 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003237 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003238 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003239 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 +02003240 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003241 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003242 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003243 }
Radek Krejci327de162019-06-14 12:52:07 +02003244 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003245 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003246
3247 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003248 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003249 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003250 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 +02003251 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003252 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003253 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003254 }
Radek Krejci327de162019-06-14 12:52:07 +02003255 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003256 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003257
3258 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3259 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003260 ly_set_add(&ctx->xpath, action, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003261 }
3262
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003263cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003264 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003265 return ret;
3266}
3267
3268/**
Radek Krejci43981a32019-04-12 09:44:11 +02003269 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003270 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003271 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003272 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3273 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3274 * @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 +02003275 * Zero means no uses, non-zero value with no status bit set mean the default status.
3276 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3277 */
3278static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003279lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003280 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3281{
3282 LY_ERR ret = LY_SUCCESS;
3283 struct lysp_node *child_p;
3284 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003285 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003286
Radek Krejci327de162019-06-14 12:52:07 +02003287 lysc_update_path(ctx, parent, notif_p->name);
3288
Radek Krejcifc11bd72019-04-11 16:00:05 +02003289 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3290 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3291 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3292 notif_p->name, notif)) {
3293 return LY_EVALID;
3294 }
3295
Radek Krejciec4da802019-05-02 13:02:41 +02003296 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003297 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3298 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003299 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003300 return LY_EVALID;
3301 }
3302
3303 notif->nodetype = LYS_NOTIF;
3304 notif->module = ctx->mod;
3305 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003306 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003307 notif->sp = notif_p;
3308 }
3309 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3310
3311 /* status - it is not inherited by specification, but it does not make sense to have
3312 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3313 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3314
3315 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3316 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3317 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003318 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003319 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003320 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3321 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003322 ly_set_add(&ctx->xpath, notif, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003323 }
Radek Krejci0935f412019-08-20 16:15:18 +02003324 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003325
Radek Krejciec4da802019-05-02 13:02:41 +02003326 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003327 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003328 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003329 }
3330
Radek Krejci327de162019-06-14 12:52:07 +02003331 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003332cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003333 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003334 return ret;
3335}
3336
3337/**
Radek Krejcia3045382018-11-22 14:30:31 +01003338 * @brief Compile parsed container node information.
3339 * @param[in] ctx Compile context
3340 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003341 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3342 * is enriched with the container-specific information.
3343 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3344 */
Radek Krejci19a96102018-11-15 13:38:09 +01003345static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003346lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003347{
3348 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3349 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3350 struct lysp_node *child_p;
3351 unsigned int u;
3352 LY_ERR ret = LY_SUCCESS;
3353
Radek Krejcife909632019-02-12 15:34:42 +01003354 if (cont_p->presence) {
3355 cont->flags |= LYS_PRESENCE;
3356 }
3357
Radek Krejci19a96102018-11-15 13:38:09 +01003358 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003359 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003360 }
3361
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003362 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003363 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3364 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003365 ly_set_add(&ctx->xpath, cont, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003366 }
Radek Krejciec4da802019-05-02 13:02:41 +02003367 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3368 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003369
3370done:
3371 return ret;
3372}
3373
Radek Krejci33f72892019-02-21 10:36:58 +01003374/*
3375 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3376 * @param[in] ctx Compile context.
3377 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3378 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003379 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3380 * @return LY_ERR value.
3381 */
3382static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003383lys_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 +01003384{
Radek Krejci33f72892019-02-21 10:36:58 +01003385 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3386
Radek Krejciec4da802019-05-02 13:02:41 +02003387 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 +01003388 leaf->units ? NULL : &leaf->units));
3389 if (leaf->nodetype == LYS_LEAFLIST) {
3390 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003391 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3392 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003393 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003394 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3395 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3396 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3397 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003398 LY_ARRAY_INCREMENT(llist->dflts);
3399 }
3400 } else {
3401 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003402 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003403 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3404 leaf->dflt->realtype = leaf->type->dflt->realtype;
3405 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3406 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003407 }
3408 }
3409 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3410 /* 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 +02003411 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003412 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003413 LY_ARRAY_SIZE_TYPE u;
Radek Krejci33f72892019-02-21 10:36:58 +01003414 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3415 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3416 /* 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 +02003417 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003418 }
3419 }
3420 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003421 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3422 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3423 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3424 return LY_EVALID;
3425 }
3426 }
3427
Radek Krejci33f72892019-02-21 10:36:58 +01003428 return LY_SUCCESS;
3429}
3430
Radek Krejcia3045382018-11-22 14:30:31 +01003431/**
3432 * @brief Compile parsed leaf node information.
3433 * @param[in] ctx Compile context
3434 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003435 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3436 * is enriched with the leaf-specific information.
3437 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3438 */
Radek Krejci19a96102018-11-15 13:38:09 +01003439static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003440lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003441{
3442 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3443 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3444 unsigned int u;
3445 LY_ERR ret = LY_SUCCESS;
3446
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003447 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003448 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3449 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003450 ly_set_add(&ctx->xpath, leaf, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003451 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003452 if (leaf_p->units) {
3453 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3454 leaf->flags |= LYS_SET_UNITS;
3455 }
Radek Krejcia1911222019-07-22 17:24:50 +02003456
3457 /* the dflt member is just filled to avoid getting the default value from the type */
3458 leaf->dflt = (void*)leaf_p->dflt;
3459 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003460 if (ret) {
3461 leaf->dflt = NULL;
3462 return ret;
3463 }
Radek Krejcia1911222019-07-22 17:24:50 +02003464
Radek Krejciccd20f12019-02-15 14:12:27 +01003465 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003466 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003467 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003468 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3469 leaf->dflt->realtype = leaf->type;
3470 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3471 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003472 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003473 leaf->dflt->realtype->refcount++;
3474 if (err) {
3475 ly_err_print(err);
3476 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3477 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3478 ly_err_free(err);
3479 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003480 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003481 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003482 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003483
3484 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003485 ret = LY_SUCCESS;
3486 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003487 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003488 leaf->flags |= LYS_SET_DFLT;
3489 }
Radek Krejci43699232018-11-23 14:59:46 +01003490
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003491
Radek Krejci19a96102018-11-15 13:38:09 +01003492done:
3493 return ret;
3494}
3495
Radek Krejcia3045382018-11-22 14:30:31 +01003496/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003497 * @brief Compile parsed leaf-list node information.
3498 * @param[in] ctx Compile context
3499 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003500 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3501 * is enriched with the leaf-list-specific information.
3502 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3503 */
3504static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003505lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003506{
3507 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3508 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003509 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003510 LY_ERR ret = LY_SUCCESS;
3511
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003512 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003513 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3514 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003515 ly_set_add(&ctx->xpath, llist, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003516 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003517 if (llist_p->units) {
3518 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3519 llist->flags |= LYS_SET_UNITS;
3520 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003521
Radek Krejcia1911222019-07-22 17:24:50 +02003522 /* the dflts member is just filled to avoid getting the default value from the type */
3523 llist->dflts = (void*)llist_p->dflts;
3524 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Michal Vasko6a044b22020-01-15 12:25:39 +01003525 if (ret != LY_SUCCESS) {
3526 /* make sure the defaults are freed correctly */
3527 if (llist_p->dflts) {
3528 llist->dflts = NULL;
3529 }
3530 return ret;
3531 }
3532
Radek Krejci0e5d8382018-11-28 16:37:53 +01003533 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003534 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003535 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003536 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3537 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003538 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003539 LY_ARRAY_INCREMENT(llist->dflts_mods);
3540 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003541 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003542 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3543 llist->dflts[u]->realtype = llist->type;
3544 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3545 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003546 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003547 llist->dflts[u]->realtype->refcount++;
3548 if (err) {
3549 ly_err_print(err);
3550 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3551 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3552 ly_err_free(err);
3553 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003554 if (ret == LY_EINCOMPLETE) {
3555 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003556 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003557
3558 /* but in general result is so far ok */
3559 ret = LY_SUCCESS;
3560 }
Radek Krejcia1911222019-07-22 17:24:50 +02003561 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003562 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003563 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003564 }
Radek Krejcia1911222019-07-22 17:24:50 +02003565 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3566 /* configuration data values must be unique - so check the default values */
3567 LY_ARRAY_FOR(llist->dflts, u) {
3568 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3569 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3570 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003571 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 +02003572 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3573 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3574 if (dynamic) {
3575 free((char*)val);
3576 }
3577 return LY_EVALID;
3578 }
3579 }
3580 }
3581 }
3582
3583 /* 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 +01003584
3585 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003586 if (llist->min) {
3587 llist->flags |= LYS_MAND_TRUE;
3588 }
Radek Krejcib7408632018-11-28 17:12:11 +01003589 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003590
Radek Krejci0e5d8382018-11-28 16:37:53 +01003591done:
3592 return ret;
3593}
3594
3595/**
Radek Krejci7af64242019-02-18 13:07:53 +01003596 * @brief Compile information about list's uniques.
3597 * @param[in] ctx Compile context.
3598 * @param[in] context_module Module where the prefixes are going to be resolved.
3599 * @param[in] uniques Sized array list of unique statements.
3600 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3601 * @return LY_ERR value.
3602 */
3603static LY_ERR
3604lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3605{
3606 LY_ERR ret = LY_SUCCESS;
3607 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003608 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003609 const char *keystr, *delim;
3610 size_t len;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003611 LY_ARRAY_SIZE_TYPE v;
Radek Krejci7af64242019-02-18 13:07:53 +01003612 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003613 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003614
3615 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3616 config = -1;
3617 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3618 keystr = uniques[v];
3619 while (keystr) {
3620 delim = strpbrk(keystr, " \t\n");
3621 if (delim) {
3622 len = delim - keystr;
3623 while (isspace(*delim)) {
3624 ++delim;
3625 }
3626 } else {
3627 len = strlen(keystr);
3628 }
3629
3630 /* unique node must be present */
3631 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003632 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3633 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003634 if (ret != LY_SUCCESS) {
3635 if (ret == LY_EDENIED) {
3636 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003637 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003638 len, keystr, lys_nodetype2str((*key)->nodetype));
3639 }
3640 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003641 } else if (flags) {
3642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3643 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003644 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003645 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003646 }
3647
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003648
Radek Krejci7af64242019-02-18 13:07:53 +01003649 /* all referenced leafs must be of the same config type */
3650 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3651 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003652 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003653 return LY_EVALID;
3654 } else if ((*key)->flags & LYS_CONFIG_W) {
3655 config = 1;
3656 } else { /* LYS_CONFIG_R */
3657 config = 0;
3658 }
3659
Michal Vasko14654712020-02-06 08:35:21 +01003660 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3661 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3662 if (parent->nodetype == LYS_LIST) {
3663 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3664 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3665 return LY_EVALID;
3666 }
3667 }
3668
Radek Krejci7af64242019-02-18 13:07:53 +01003669 /* check status */
3670 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3671 (*key)->flags, (*key)->module, (*key)->name));
3672
3673 /* mark leaf as unique */
3674 (*key)->flags |= LYS_UNIQUE;
3675
3676 /* next unique value in line */
3677 keystr = delim;
3678 }
3679 /* next unique definition */
3680 }
3681
3682 return LY_SUCCESS;
3683}
3684
3685/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003686 * @brief Compile parsed list node information.
3687 * @param[in] ctx Compile context
3688 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003689 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3690 * is enriched with the list-specific information.
3691 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3692 */
3693static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003694lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003695{
3696 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3697 struct lysc_node_list *list = (struct lysc_node_list*)node;
3698 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003699 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003700 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003701 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003702 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003703 LY_ERR ret = LY_SUCCESS;
3704
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003705 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003706 if (list->min) {
3707 list->flags |= LYS_MAND_TRUE;
3708 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003709 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3710
3711 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003712 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003713 }
3714
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003715 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003716 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3717 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003718 ly_set_add(&ctx->xpath, list, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003719 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003720
3721 /* keys */
3722 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3723 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3724 return LY_EVALID;
3725 }
3726
3727 /* find all the keys (must be direct children) */
3728 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003729 if (!keystr) {
3730 /* keyless list */
3731 list->flags |= LYS_KEYLESS;
3732 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003733 while (keystr) {
3734 delim = strpbrk(keystr, " \t\n");
3735 if (delim) {
3736 len = delim - keystr;
3737 while (isspace(*delim)) {
3738 ++delim;
3739 }
3740 } else {
3741 len = strlen(keystr);
3742 }
3743
3744 /* key node must be present */
Michal Vaskoe444f752020-02-10 12:20:06 +01003745 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 +02003746 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3748 "The list's key \"%.*s\" not found.", len, keystr);
3749 return LY_EVALID;
3750 }
3751 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003752 if (key->flags & LYS_KEY) {
3753 /* the node was already marked as a key */
3754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3755 "Duplicated key identifier \"%.*s\".", len, keystr);
3756 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003757 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003758
3759 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003760 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003761 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003762 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3763 return LY_EVALID;
3764 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003765 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003766 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003767 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003768 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003769 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003770 return LY_EVALID;
3771 }
3772 } else {
3773 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003774 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003775 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003776 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003777 return LY_EVALID;
3778 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003779 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003781 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003782 return LY_EVALID;
3783 }
3784 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003785
3786 /* check status */
3787 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003788 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003789
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003790 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003791 if (key->dflt) {
3792 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3793 lysc_type_free(ctx->ctx, key->dflt->realtype);
3794 free(key->dflt);
3795 key->dflt = NULL;
3796 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003797 }
3798 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003799 key->flags |= LYS_KEY;
3800
3801 /* move it to the correct position */
3802 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3803 /* fix links in closest previous siblings of the key */
3804 if (key->next) {
3805 key->next->prev = key->prev;
3806 } else {
3807 /* last child */
3808 list->child->prev = key->prev;
3809 }
3810 if (key->prev->next) {
3811 key->prev->next = key->next;
3812 }
3813 /* fix links in the key */
3814 if (prev_key) {
3815 key->prev = (struct lysc_node*)prev_key;
3816 key->next = prev_key->next;
3817 } else {
3818 key->prev = list->child->prev;
3819 key->next = list->child;
3820 }
3821 /* fix links in closes future siblings of the key */
3822 if (prev_key) {
3823 if (prev_key->next) {
3824 prev_key->next->prev = (struct lysc_node*)key;
3825 } else {
3826 list->child->prev = (struct lysc_node*)key;
3827 }
3828 prev_key->next = (struct lysc_node*)key;
3829 } else {
3830 list->child->prev = (struct lysc_node*)key;
3831 }
3832 /* fix links in parent */
3833 if (!key->prev->next) {
3834 list->child = (struct lysc_node*)key;
3835 }
3836 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003837
3838 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003839 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003840 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003841 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003842 }
3843
3844 /* uniques */
3845 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003846 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003847 }
3848
Radek Krejciec4da802019-05-02 13:02:41 +02003849 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3850 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003851
3852done:
3853 return ret;
3854}
3855
Radek Krejcib56c7502019-02-13 14:19:54 +01003856/**
3857 * @brief Do some checks and set the default choice's case.
3858 *
3859 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3860 *
3861 * @param[in] ctx Compile context.
3862 * @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,
3863 * not the reference to the imported module.
3864 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3865 * @return LY_ERR value.
3866 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003867static LY_ERR
3868lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3869{
3870 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3871 const char *prefix = NULL, *name;
3872 size_t prefix_len = 0;
3873
3874 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3875 name = strchr(dflt, ':');
3876 if (name) {
3877 prefix = dflt;
3878 prefix_len = name - prefix;
3879 ++name;
3880 } else {
3881 name = dflt;
3882 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003883 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003884 /* prefixed default case make sense only for the prefix of the schema itself */
3885 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3886 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3887 prefix_len, prefix);
3888 return LY_EVALID;
3889 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003890 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 +01003891 if (!ch->dflt) {
3892 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3893 "Default case \"%s\" not found.", dflt);
3894 return LY_EVALID;
3895 }
3896 /* no mandatory nodes directly under the default case */
3897 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003898 if (iter->parent != (struct lysc_node*)ch->dflt) {
3899 break;
3900 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003901 if (iter->flags & LYS_MAND_TRUE) {
3902 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3903 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3904 return LY_EVALID;
3905 }
3906 }
Radek Krejci01342af2019-01-03 15:18:08 +01003907 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003908 return LY_SUCCESS;
3909}
3910
Radek Krejciccd20f12019-02-15 14:12:27 +01003911static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003912lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003913{
3914 struct lys_module *mod;
3915 const char *prefix = NULL, *name;
3916 size_t prefix_len = 0;
3917 struct lysc_node_case *cs;
3918 struct lysc_node *node;
3919
3920 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3921 name = strchr(dflt, ':');
3922 if (name) {
3923 prefix = dflt;
3924 prefix_len = name - prefix;
3925 ++name;
3926 } else {
3927 name = dflt;
3928 }
3929 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3930 if (prefix) {
3931 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3932 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003933 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3934 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003935 return LY_EVALID;
3936 }
3937 } else {
3938 mod = ctx->mod;
3939 }
3940 /* get the default case */
Michal Vaskoe444f752020-02-10 12:20:06 +01003941 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 +01003942 if (!cs) {
3943 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003944 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003945 return LY_EVALID;
3946 }
3947
3948 /* check that there is no mandatory node */
3949 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003950 if (node->parent != (struct lysc_node*)cs) {
3951 break;
3952 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003953 if (node->flags & LYS_MAND_TRUE) {
3954 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003955 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3956 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003957 return LY_EVALID;
3958 }
3959 }
3960
3961 /* set the default case in choice */
3962 ch->dflt = cs;
3963 cs->flags |= LYS_SET_DFLT;
3964
3965 return LY_SUCCESS;
3966}
3967
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003968/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003969 * @brief Compile parsed choice node information.
3970 * @param[in] ctx Compile context
3971 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003972 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003973 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003974 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3975 */
3976static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003977lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003978{
3979 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3980 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3981 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01003982 LY_ERR ret = LY_SUCCESS;
3983
Radek Krejci056d0a82018-12-06 16:57:25 +01003984 LY_LIST_FOR(ch_p->child, child_p) {
3985 if (child_p->nodetype == LYS_CASE) {
3986 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003987 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003988 }
3989 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003990 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003991 }
3992 }
3993
3994 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003995 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003996 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003997 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003998
Radek Krejci9800fb82018-12-13 14:26:23 +01003999 return ret;
4000}
4001
4002/**
4003 * @brief Compile parsed anydata or anyxml node information.
4004 * @param[in] ctx Compile context
4005 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004006 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4007 * is enriched with the any-specific information.
4008 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4009 */
4010static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004011lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004012{
4013 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4014 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4015 unsigned int u;
4016 LY_ERR ret = LY_SUCCESS;
4017
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004018 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004019 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4020 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004021 ly_set_add(&ctx->xpath, any, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004022 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004023
4024 if (any->flags & LYS_CONFIG_W) {
4025 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004026 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004027 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004028done:
4029 return ret;
4030}
4031
Radek Krejcib56c7502019-02-13 14:19:54 +01004032/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004033 * @brief Connect the node into the siblings list and check its name uniqueness.
4034 *
4035 * @param[in] ctx Compile context
4036 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4037 * the choice itself is expected instead of a specific case node.
4038 * @param[in] node Schema node to connect into the list.
4039 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004040 * 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 +01004041 */
4042static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004043lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004044{
4045 struct lysc_node **children;
4046
4047 if (node->nodetype == LYS_CASE) {
4048 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4049 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004050 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004051 }
4052 if (children) {
4053 if (!(*children)) {
4054 /* first child */
4055 *children = node;
4056 } else if (*children != node) {
4057 /* by the condition in previous branch we cover the choice/case children
4058 * - the children list is shared by the choice and the the first case, in addition
4059 * the first child of each case must be referenced from the case node. So the node is
4060 * actually always already inserted in case it is the first children - so here such
4061 * a situation actually corresponds to the first branch */
4062 /* insert at the end of the parent's children list */
4063 (*children)->prev->next = node;
4064 node->prev = (*children)->prev;
4065 (*children)->prev = node;
4066
4067 /* check the name uniqueness */
4068 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4069 lysc_node_notifs(parent), node->name, node)) {
4070 return LY_EEXIST;
4071 }
4072 }
4073 }
4074 return LY_SUCCESS;
4075}
4076
Radek Krejci95710c92019-02-11 15:49:55 +01004077/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004078 * @brief Prepare the case structure in choice node for the new data node.
4079 *
4080 * 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
4081 * created in the choice when the first child was processed.
4082 *
4083 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004084 * @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,
4085 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004086 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4087 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4088 * @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,
4089 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004090 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004091static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004092lys_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 +01004093{
4094 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004095 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004096 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004097 unsigned int u;
4098 LY_ERR ret;
4099
Radek Krejci95710c92019-02-11 15:49:55 +01004100#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004101 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004102 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004103 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4104 return NULL; \
4105 } \
4106 }
4107
Radek Krejci95710c92019-02-11 15:49:55 +01004108 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4109 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004110
4111 /* we have to add an implicit case node into the parent choice */
4112 cs = calloc(1, sizeof(struct lysc_node_case));
4113 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004114 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004115 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004116 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004117 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4118 /* the case is already present since the child is not its first children */
4119 return (struct lysc_node_case*)ch->cases->prev;
4120 }
Radek Krejci95710c92019-02-11 15:49:55 +01004121 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004122
4123 /* explicit parent case is not present (this is its first child) */
4124 cs = calloc(1, sizeof(struct lysc_node_case));
4125 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004126 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004127 cs->flags = LYS_STATUS_MASK & node_p->flags;
4128 cs->sp = node_p;
4129
Radek Krejcib1b59152019-01-07 13:21:56 +01004130 /* 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 +02004131 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004132
4133 if (node_p->when) {
4134 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004135 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004136 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004137
4138 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4139 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004140 ly_set_add(&ctx->xpath, cs, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004141 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004142 }
Radek Krejciec4da802019-05-02 13:02:41 +02004143 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004144 } else {
4145 LOGINT(ctx->ctx);
4146 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004147 }
4148 cs->module = ctx->mod;
4149 cs->prev = (struct lysc_node*)cs;
4150 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004151 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004152 cs->child = child;
4153
4154 return cs;
4155error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004156 if (cs) {
4157 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4158 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004159 return NULL;
4160
4161#undef UNIQUE_CHECK
4162}
4163
Radek Krejcib56c7502019-02-13 14:19:54 +01004164/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004165 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004166 *
4167 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004168 * @param[in] node Target node where the config is supposed to be changed.
4169 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004170 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4171 * 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 +01004172 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4173 * @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 +01004174 * @return LY_ERR value.
4175 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004176static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004177lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004178 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004179{
4180 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004181 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004182
4183 if (config == (node->flags & LYS_CONFIG_MASK)) {
4184 /* nothing to do */
4185 return LY_SUCCESS;
4186 }
4187
4188 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004189 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004190 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4191 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004192 "Invalid %s of config - configuration node cannot be child of any state data node.",
4193 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004194 return LY_EVALID;
4195 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004196 node->flags |= LYS_SET_CONFIG;
4197 } else {
4198 if (node->flags & LYS_SET_CONFIG) {
4199 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4200 /* setting config flags, but have node with explicit config true */
4201 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004202 "Invalid %s of config - configuration node cannot be child of any state data node.",
4203 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004204 return LY_EVALID;
4205 }
4206 /* do not change config on nodes where the config is explicitely set, this does not apply to
4207 * nodes, which are being changed explicitly (targets of refine or deviation) */
4208 return LY_SUCCESS;
4209 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004210 }
4211 node->flags &= ~LYS_CONFIG_MASK;
4212 node->flags |= config;
4213
4214 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004215 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004216 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004217 }
4218
Radek Krejci76b3e962018-12-14 17:01:25 +01004219 return LY_SUCCESS;
4220}
4221
Radek Krejcib56c7502019-02-13 14:19:54 +01004222/**
4223 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4224 *
4225 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4226 * the flag to such parents from a mandatory children.
4227 *
4228 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4229 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4230 * (mandatory children was removed).
4231 */
Radek Krejcife909632019-02-12 15:34:42 +01004232void
4233lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4234{
4235 struct lysc_node *iter;
4236
4237 if (add) { /* set flag */
4238 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4239 parent = parent->parent) {
4240 parent->flags |= LYS_MAND_TRUE;
4241 }
4242 } else { /* unset flag */
4243 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004244 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004245 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004246 /* there is another mandatory node */
4247 return;
4248 }
4249 }
4250 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4251 parent->flags &= ~LYS_MAND_TRUE;
4252 }
4253 }
4254}
4255
Radek Krejci056d0a82018-12-06 16:57:25 +01004256/**
Radek Krejci3641f562019-02-13 15:38:40 +01004257 * @brief Internal sorting process for the lys_compile_augment_sort().
4258 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4259 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4260 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4261 */
4262static void
4263lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4264{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004265 LY_ARRAY_SIZE_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004266 size_t len;
4267
4268 len = strlen(aug_p->nodeid);
4269 LY_ARRAY_FOR(result, v) {
4270 if (strlen(result[v]->nodeid) <= len) {
4271 continue;
4272 }
4273 if (v < LY_ARRAY_SIZE(result)) {
4274 /* move the rest of array */
4275 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4276 break;
4277 }
4278 }
4279 result[v] = aug_p;
4280 LY_ARRAY_INCREMENT(result);
4281}
4282
4283/**
4284 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4285 *
4286 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4287 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4288 *
4289 * @param[in] ctx Compile context.
4290 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4291 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4292 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4293 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4294 * @return LY_ERR value.
4295 */
4296LY_ERR
4297lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4298{
4299 struct lysp_augment **result = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004300 LY_ARRAY_SIZE_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004301
4302 assert(augments);
4303
4304 /* get count of the augments in module and all its submodules */
4305 if (aug_p) {
4306 count += LY_ARRAY_SIZE(aug_p);
4307 }
4308 LY_ARRAY_FOR(inc_p, u) {
4309 if (inc_p[u].submodule->augments) {
4310 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4311 }
4312 }
4313
4314 if (!count) {
4315 *augments = NULL;
4316 return LY_SUCCESS;
4317 }
4318 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4319
4320 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4321 * together, so there can be even /z/y betwwen them. */
4322 LY_ARRAY_FOR(aug_p, u) {
4323 lys_compile_augment_sort_(&aug_p[u], result);
4324 }
4325 LY_ARRAY_FOR(inc_p, u) {
4326 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4327 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4328 }
4329 }
4330
4331 *augments = result;
4332 return LY_SUCCESS;
4333}
4334
4335/**
4336 * @brief Compile the parsed augment connecting it into its target.
4337 *
4338 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4339 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4340 * are already implemented and compiled.
4341 *
4342 * @param[in] ctx Compile context.
4343 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004344 * @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
4345 * children in case of the augmenting uses data.
4346 * @return LY_SUCCESS on success.
4347 * @return LY_EVALID on failure.
4348 */
4349LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004350lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004351{
4352 LY_ERR ret = LY_SUCCESS;
4353 struct lysp_node *node_p, *case_node_p;
4354 struct lysc_node *target; /* target target of the augment */
4355 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004356 struct lysc_when **when, *when_shared;
4357 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004358 uint16_t flags = 0;
4359 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004360 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004361
Radek Krejci327de162019-06-14 12:52:07 +02004362 lysc_update_path(ctx, NULL, "{augment}");
4363 lysc_update_path(ctx, NULL, aug_p->nodeid);
4364
Radek Krejci7af64242019-02-18 13:07:53 +01004365 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004366 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004367 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004368 if (ret != LY_SUCCESS) {
4369 if (ret == LY_EDENIED) {
4370 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4371 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4372 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4373 }
4374 return LY_EVALID;
4375 }
4376
4377 /* check for mandatory nodes
4378 * - new cases augmenting some choice can have mandatory nodes
4379 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4380 */
Radek Krejci733988a2019-02-15 15:12:44 +01004381 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004382 allow_mandatory = 1;
4383 }
4384
4385 when_shared = NULL;
4386 LY_LIST_FOR(aug_p->child, node_p) {
4387 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4388 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004389 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004390 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4391 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004392 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004393 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4394 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004395 return LY_EVALID;
4396 }
4397
4398 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004399 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004400 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004401 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004402 } else {
4403 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004404 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004405 }
4406 }
Radek Krejciec4da802019-05-02 13:02:41 +02004407 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004408
Radek Krejcife13da42019-02-15 14:51:01 +01004409 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4410 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004411 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004412 /* 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 +01004413 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 +01004414 } else if (target->nodetype == LYS_CHOICE) {
4415 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4416 node = ((struct lysc_node_choice*)target)->cases->prev;
4417 } else {
4418 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004419 node = (struct lysc_node*)lysc_node_children(target, flags);
4420 if (!node) {
4421 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4422 break;
4423 }
4424 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004425 }
4426
Radek Krejci733988a2019-02-15 15:12:44 +01004427 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004428 node->flags &= ~LYS_MAND_TRUE;
4429 lys_compile_mandatory_parents(target, 0);
4430 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004431 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004432 return LY_EVALID;
4433 }
4434
4435 /* pass augment's when to all the children */
4436 if (aug_p->when) {
4437 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4438 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004439 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004440 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004441
4442 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4443 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004444 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004445 }
4446
Radek Krejci3641f562019-02-13 15:38:40 +01004447 when_shared = *when;
4448 } else {
4449 ++when_shared->refcount;
4450 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004451
4452 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4453 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004454 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004455 }
Radek Krejci3641f562019-02-13 15:38:40 +01004456 }
4457 }
4458 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004459
Radek Krejciec4da802019-05-02 13:02:41 +02004460 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004461 switch (target->nodetype) {
4462 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004463 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004464 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004465 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004466 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004467 break;
4468 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004469 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004470 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004471 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004472 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004473 break;
4474 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004475 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004476 if (aug_p->actions) {
4477 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004478 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4479 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004480 return LY_EVALID;
4481 }
4482 if (aug_p->notifs) {
4483 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004484 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004485 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004486 return LY_EVALID;
4487 }
4488 }
Radek Krejci3641f562019-02-13 15:38:40 +01004489
Radek Krejci327de162019-06-14 12:52:07 +02004490 lysc_update_path(ctx, NULL, NULL);
4491 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004492error:
Radek Krejciec4da802019-05-02 13:02:41 +02004493 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004494 return ret;
4495}
4496
4497/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004498 * @brief Apply refined or deviated mandatory flag to the target node.
4499 *
4500 * @param[in] ctx Compile context.
4501 * @param[in] node Target node where the mandatory property is supposed to be changed.
4502 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004503 * @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 +01004504 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4505 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4506 * 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 +01004507 * @return LY_ERR value.
4508 */
4509static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004510lys_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 +01004511{
4512 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4513 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004514 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4515 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004516 return LY_EVALID;
4517 }
4518
4519 if (mandatory_flag & LYS_MAND_TRUE) {
4520 /* check if node has default value */
4521 if (node->nodetype & LYS_LEAF) {
4522 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004523 if (refine_flag) {
4524 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004525 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004526 return LY_EVALID;
4527 }
Radek Krejcia1911222019-07-22 17:24:50 +02004528 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004529 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004530 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004531
4532 /* update the list of incomplete default values if needed */
4533 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4534
Radek Krejcia1911222019-07-22 17:24:50 +02004535 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4536 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4537 free(leaf->dflt);
4538 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004539 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004540 }
4541 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004542 if (refine_flag) {
4543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004544 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004545 return LY_EVALID;
4546 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004547 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004548 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004549 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 +01004550 return LY_EVALID;
4551 }
4552
4553 node->flags &= ~LYS_MAND_FALSE;
4554 node->flags |= LYS_MAND_TRUE;
4555 lys_compile_mandatory_parents(node->parent, 1);
4556 } else {
4557 /* make mandatory false */
4558 node->flags &= ~LYS_MAND_TRUE;
4559 node->flags |= LYS_MAND_FALSE;
4560 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004561 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 +01004562 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004563 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004564 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004565 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4566 leaf->dflt->realtype = leaf->type->dflt->realtype;
4567 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4568 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004569 }
4570 }
4571 return LY_SUCCESS;
4572}
4573
4574/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004575 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4576 * If present, also apply uses's modificators.
4577 *
4578 * @param[in] ctx Compile context
4579 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004580 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4581 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4582 * the compile context.
4583 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4584 */
4585static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004586lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004587{
4588 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004589 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004590 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004591 struct lysc_node_container context_node_fake =
4592 {.nodetype = LYS_CONTAINER,
4593 .module = ctx->mod,
4594 .flags = parent ? parent->flags : 0,
4595 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004596 .prev = (struct lysc_node*)&context_node_fake,
4597 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004598 struct lysp_grp *grp = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004599 LY_ARRAY_SIZE_TYPE u, v;
4600 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004601 int found;
4602 const char *id, *name, *prefix;
4603 size_t prefix_len, name_len;
4604 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004605 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004606 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004607 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004608 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004609 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004610 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004611 struct lysp_augment **augments = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004612 LY_ARRAY_SIZE_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004613 struct lysc_notif **notifs = NULL;
4614 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004615
4616 /* search for the grouping definition */
4617 found = 0;
4618 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004619 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004620 if (prefix) {
4621 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4622 if (!mod) {
4623 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004624 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004625 return LY_EVALID;
4626 }
4627 } else {
4628 mod = ctx->mod_def;
4629 }
4630 if (mod == ctx->mod_def) {
4631 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004632 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004633 LY_ARRAY_FOR(grp, u) {
4634 if (!strcmp(grp[u].name, name)) {
4635 grp = &grp[u];
4636 found = 1;
4637 break;
4638 }
4639 }
4640 }
4641 }
4642 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004643 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004644 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004645 if (grp) {
4646 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4647 if (!strcmp(grp[u].name, name)) {
4648 grp = &grp[u];
4649 found = 1;
4650 }
4651 }
4652 }
4653 if (!found && mod->parsed->includes) {
4654 /* ... and all the submodules */
4655 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4656 grp = mod->parsed->includes[u].submodule->groupings;
4657 if (grp) {
4658 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4659 if (!strcmp(grp[v].name, name)) {
4660 grp = &grp[v];
4661 found = 1;
4662 }
4663 }
4664 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004665 }
4666 }
4667 }
4668 if (!found) {
4669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4670 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4671 return LY_EVALID;
4672 }
4673
4674 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4675 grp_stack_count = ctx->groupings.count;
4676 ly_set_add(&ctx->groupings, (void*)grp, 0);
4677 if (grp_stack_count == ctx->groupings.count) {
4678 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4680 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4681 return LY_EVALID;
4682 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004683 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4684 /* remember that the grouping is instantiated to avoid its standalone validation */
4685 grp->flags |= LYS_USED_GRP;
4686 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004687
4688 /* switch context's mod_def */
4689 mod_old = ctx->mod_def;
4690 ctx->mod_def = mod;
4691
4692 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004693 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 +01004694
Radek Krejcifc11bd72019-04-11 16:00:05 +02004695 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004696 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004697 /* 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 +02004698 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 +01004699
4700 /* some preparation for applying refines */
4701 if (grp->data == node_p) {
4702 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004703 if (parent) {
4704 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4705 } else if (ctx->mod->compiled->data) {
4706 child = ctx->mod->compiled->data;
4707 } else {
4708 child = NULL;
4709 }
4710 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004711 }
4712 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004713 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004714 LY_LIST_FOR(context_node_fake.child, child) {
4715 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004716
Radek Krejcifc11bd72019-04-11 16:00:05 +02004717 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004718 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004719 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004720 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004721 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4722
4723 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4724 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004725 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004726 }
4727
Radek Krejci00b874b2019-02-12 10:54:50 +01004728 when_shared = *when;
4729 } else {
4730 ++when_shared->refcount;
4731 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004732
4733 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4734 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004735 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004736 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004737 }
4738 }
Radek Krejci01342af2019-01-03 15:18:08 +01004739 }
4740 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004741 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004742 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004743 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004744 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 +01004745 }
4746
Radek Krejcifc11bd72019-04-11 16:00:05 +02004747 /* compile actions */
4748 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4749 if (actions) {
4750 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004751 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004752 if (*actions && (uses_p->augments || uses_p->refines)) {
4753 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4754 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4755 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4756 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4757 }
4758 }
4759
4760 /* compile notifications */
4761 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4762 if (notifs) {
4763 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004764 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004765 if (*notifs && (uses_p->augments || uses_p->refines)) {
4766 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4767 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4768 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4769 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4770 }
4771 }
4772
4773
Radek Krejci3641f562019-02-13 15:38:40 +01004774 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004775 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004776 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004777 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004778 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004779
Radek Krejcif0089082019-01-07 16:42:01 +01004780 /* reload previous context's mod_def */
4781 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004782 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004783
Radek Krejci76b3e962018-12-14 17:01:25 +01004784 /* apply refine */
4785 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004786 lysc_update_path(ctx, NULL, rfn->nodeid);
4787
Radek Krejci7af64242019-02-18 13:07:53 +01004788 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 +01004789 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004790 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004791 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004792
4793 /* default value */
4794 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004795 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004796 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004797 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_SIZE_TYPE" default values.",
Radek Krejci327de162019-06-14 12:52:07 +02004798 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004799 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004800 }
4801 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4802 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004803 "Invalid refine of default - %s cannot hold default value(s).",
4804 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004805 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004806 }
4807 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004808 struct ly_err_item *err = NULL;
4809 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4810 if (leaf->dflt) {
4811 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004812 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004813 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4814 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4815 } else {
4816 /* prepare a new one */
4817 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4818 leaf->dflt->realtype = leaf->type;
4819 }
4820 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004821 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004822 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4823 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004824 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004825 leaf->dflt->realtype->refcount++;
4826 if (err) {
4827 ly_err_print(err);
4828 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4829 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4830 ly_err_free(err);
4831 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004832 if (rc == LY_EINCOMPLETE) {
4833 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004834 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004835
4836 /* but in general result is so far ok */
4837 rc = LY_SUCCESS;
4838 }
Radek Krejcia1911222019-07-22 17:24:50 +02004839 LY_CHECK_GOTO(rc, cleanup);
4840 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004841 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004842 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4843
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004844 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004845 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4846 "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 +02004847 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004848 }
Radek Krejcia1911222019-07-22 17:24:50 +02004849
4850 /* remove previous set of default values */
4851 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004852 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004853 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4854 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4855 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004856 }
Radek Krejcia1911222019-07-22 17:24:50 +02004857 LY_ARRAY_FREE(llist->dflts);
4858 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004859 LY_ARRAY_FREE(llist->dflts_mods);
4860 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004861
4862 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004863 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02004864 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004865 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004866 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004867 LY_ARRAY_INCREMENT(llist->dflts_mods);
4868 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004869 LY_ARRAY_INCREMENT(llist->dflts);
4870 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4871 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004872 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004873 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004874 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004875 llist->dflts[u]->realtype->refcount++;
4876 if (err) {
4877 ly_err_print(err);
4878 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4879 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4880 ly_err_free(err);
4881 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004882 if (rc == LY_EINCOMPLETE) {
4883 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004884 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004885
4886 /* but in general result is so far ok */
4887 rc = LY_SUCCESS;
4888 }
4889 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004890 }
Radek Krejcia1911222019-07-22 17:24:50 +02004891 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004892 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004893 if (((struct lysc_node_choice*)node)->dflt) {
4894 /* unset LYS_SET_DFLT from the current default case */
4895 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4896 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004897 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 +01004898 }
4899 }
4900
Radek Krejci12fb9142019-01-08 09:45:30 +01004901 /* description */
4902 if (rfn->dsc) {
4903 FREE_STRING(ctx->ctx, node->dsc);
4904 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4905 }
4906
4907 /* reference */
4908 if (rfn->ref) {
4909 FREE_STRING(ctx->ctx, node->ref);
4910 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4911 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004912
4913 /* config */
4914 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004915 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004916 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004917 } else {
4918 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004919 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004920 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004921 }
4922
4923 /* mandatory */
4924 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004925 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004926 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004927
4928 /* presence */
4929 if (rfn->presence) {
4930 if (node->nodetype != LYS_CONTAINER) {
4931 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004932 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4933 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004934 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004935 }
4936 node->flags |= LYS_PRESENCE;
4937 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004938
4939 /* must */
4940 if (rfn->musts) {
4941 switch (node->nodetype) {
4942 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004943 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 +01004944 break;
4945 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004946 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 +01004947 break;
4948 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004949 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 +01004950 break;
4951 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004952 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 +01004953 break;
4954 case LYS_ANYXML:
4955 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004956 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 +01004957 break;
4958 default:
4959 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004960 "Invalid refine of must statement - %s cannot hold any must statement.",
4961 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004962 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004963 }
Michal Vasko004d3152020-06-11 19:59:22 +02004964 ly_set_add(&ctx->xpath, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01004965 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004966
4967 /* min/max-elements */
4968 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4969 switch (node->nodetype) {
4970 case LYS_LEAFLIST:
4971 if (rfn->flags & LYS_SET_MAX) {
4972 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4973 }
4974 if (rfn->flags & LYS_SET_MIN) {
4975 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004976 if (rfn->min) {
4977 node->flags |= LYS_MAND_TRUE;
4978 lys_compile_mandatory_parents(node->parent, 1);
4979 } else {
4980 node->flags &= ~LYS_MAND_TRUE;
4981 lys_compile_mandatory_parents(node->parent, 0);
4982 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004983 }
4984 break;
4985 case LYS_LIST:
4986 if (rfn->flags & LYS_SET_MAX) {
4987 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4988 }
4989 if (rfn->flags & LYS_SET_MIN) {
4990 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004991 if (rfn->min) {
4992 node->flags |= LYS_MAND_TRUE;
4993 lys_compile_mandatory_parents(node->parent, 1);
4994 } else {
4995 node->flags &= ~LYS_MAND_TRUE;
4996 lys_compile_mandatory_parents(node->parent, 0);
4997 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004998 }
4999 break;
5000 default:
5001 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005002 "Invalid refine of %s statement - %s cannot hold this statement.",
5003 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005004 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005005 }
5006 }
Radek Krejcif0089082019-01-07 16:42:01 +01005007
5008 /* if-feature */
5009 if (rfn->iffeatures) {
5010 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005011 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005012 }
Radek Krejci327de162019-06-14 12:52:07 +02005013
5014 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005015 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005016
Radek Krejcif2271f12019-01-07 16:42:23 +01005017 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005018 for (uint32_t i = 0; i < refined.count; ++i) {
5019 node = (struct lysc_node*)refined.objs[i];
5020 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02005021 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005022
5023 /* check possible conflict with default value (default added, mandatory left true) */
5024 if ((node->flags & LYS_MAND_TRUE) &&
5025 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5026 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5027 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005028 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005029 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005030 }
5031
5032 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5033 if (node->nodetype == LYS_LIST) {
5034 min = ((struct lysc_node_list*)node)->min;
5035 max = ((struct lysc_node_list*)node)->max;
5036 } else {
5037 min = ((struct lysc_node_leaflist*)node)->min;
5038 max = ((struct lysc_node_leaflist*)node)->max;
5039 }
5040 if (min > max) {
5041 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005042 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5043 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005044 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005045 }
5046 }
5047 }
5048
Radek Krejci327de162019-06-14 12:52:07 +02005049 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005050 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005051
5052cleanup:
5053 /* fix connection of the children nodes from fake context node back into the parent */
5054 if (context_node_fake.child) {
5055 context_node_fake.child->prev = child;
5056 }
5057 LY_LIST_FOR(context_node_fake.child, child) {
5058 child->parent = parent;
5059 }
5060
5061 if (uses_p->augments || uses_p->refines) {
5062 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005063 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005064 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5065 LY_ARRAY_FREE(context_node_fake.actions);
5066 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005067 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005068 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5069 LY_ARRAY_FREE(context_node_fake.notifs);
5070 }
5071 }
5072
Radek Krejcie86bf772018-12-14 11:39:53 +01005073 /* reload previous context's mod_def */
5074 ctx->mod_def = mod_old;
5075 /* remove the grouping from the stack for circular groupings dependency check */
5076 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5077 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005078 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005079 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005080
5081 return ret;
5082}
5083
Radek Krejci327de162019-06-14 12:52:07 +02005084static int
5085lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5086{
5087 struct lysp_node *iter;
5088 int len = 0;
5089
5090 *path = NULL;
5091 for (iter = node; iter && len >= 0; iter = iter->parent) {
5092 char *s = *path;
5093 char *id;
5094
5095 switch (iter->nodetype) {
5096 case LYS_USES:
5097 asprintf(&id, "{uses='%s'}", iter->name);
5098 break;
5099 case LYS_GROUPING:
5100 asprintf(&id, "{grouping='%s'}", iter->name);
5101 break;
5102 case LYS_AUGMENT:
5103 asprintf(&id, "{augment='%s'}", iter->name);
5104 break;
5105 default:
5106 id = strdup(iter->name);
5107 break;
5108 }
5109
5110 if (!iter->parent) {
5111 /* print prefix */
5112 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5113 } else {
5114 /* prefix is the same as in parent */
5115 len = asprintf(path, "/%s%s", id, s ? s : "");
5116 }
5117 free(s);
5118 free(id);
5119 }
5120
5121 if (len < 0) {
5122 free(*path);
5123 *path = NULL;
5124 } else if (len == 0) {
5125 *path = strdup("/");
5126 len = 1;
5127 }
5128 return len;
5129}
5130
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005131/**
5132 * @brief Validate groupings that were defined but not directly used in the schema itself.
5133 *
5134 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5135 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5136 */
5137static LY_ERR
5138lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5139{
5140 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005141 char *path;
5142 int len;
5143
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005144 struct lysp_node_uses fake_uses = {
5145 .parent = node_p,
5146 .nodetype = LYS_USES,
5147 .flags = 0, .next = NULL,
5148 .name = grp->name,
5149 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5150 .refines = NULL, .augments = NULL
5151 };
5152 struct lysc_node_container fake_container = {
5153 .nodetype = LYS_CONTAINER,
5154 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5155 .module = ctx->mod,
5156 .sp = NULL, .parent = NULL, .next = NULL,
5157 .prev = (struct lysc_node*)&fake_container,
5158 .name = "fake",
5159 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5160 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5161 };
5162
5163 if (grp->parent) {
5164 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5165 }
Radek Krejci327de162019-06-14 12:52:07 +02005166
5167 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5168 if (len < 0) {
5169 LOGMEM(ctx->ctx);
5170 return LY_EMEM;
5171 }
5172 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5173 ctx->path_len = (uint16_t)len;
5174 free(path);
5175
5176 lysc_update_path(ctx, NULL, "{grouping}");
5177 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005178 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005179 lysc_update_path(ctx, NULL, NULL);
5180 lysc_update_path(ctx, NULL, NULL);
5181
5182 ctx->path_len = 1;
5183 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005184
5185 /* cleanup */
5186 lysc_node_container_free(ctx->ctx, &fake_container);
5187
5188 return ret;
5189}
Radek Krejcife909632019-02-12 15:34:42 +01005190
Radek Krejcie86bf772018-12-14 11:39:53 +01005191/**
Radek Krejcia3045382018-11-22 14:30:31 +01005192 * @brief Compile parsed schema node information.
5193 * @param[in] ctx Compile context
5194 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005195 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5196 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5197 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005198 * @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).
5199 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005200 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5201 */
Radek Krejci19a96102018-11-15 13:38:09 +01005202static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005203lys_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 +01005204{
Radek Krejci1c54f462020-05-12 17:25:34 +02005205 LY_ERR ret = LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01005206 struct lysc_node *node;
5207 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005208 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005209 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005210 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005211
Radek Krejci327de162019-06-14 12:52:07 +02005212 if (node_p->nodetype != LYS_USES) {
5213 lysc_update_path(ctx, parent, node_p->name);
5214 } else {
5215 lysc_update_path(ctx, NULL, "{uses}");
5216 lysc_update_path(ctx, NULL, node_p->name);
5217 }
5218
Radek Krejci19a96102018-11-15 13:38:09 +01005219 switch (node_p->nodetype) {
5220 case LYS_CONTAINER:
5221 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5222 node_compile_spec = lys_compile_node_container;
5223 break;
5224 case LYS_LEAF:
5225 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5226 node_compile_spec = lys_compile_node_leaf;
5227 break;
5228 case LYS_LIST:
5229 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005230 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005231 break;
5232 case LYS_LEAFLIST:
5233 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005234 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005235 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005236 case LYS_CHOICE:
5237 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005238 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005239 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005240 case LYS_ANYXML:
5241 case LYS_ANYDATA:
5242 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005243 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005244 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005245 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005246 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5247 lysc_update_path(ctx, NULL, NULL);
5248 lysc_update_path(ctx, NULL, NULL);
5249 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005250 default:
5251 LOGINT(ctx->ctx);
5252 return LY_EINT;
5253 }
5254 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5255 node->nodetype = node_p->nodetype;
5256 node->module = ctx->mod;
5257 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005258 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005259
5260 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005261 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005262 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005263 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005264 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5265 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005266 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005267 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005268 node->flags |= LYS_CONFIG_R;
5269 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005270 /* config not explicitely set, inherit it from parent */
5271 if (parent) {
5272 node->flags |= parent->flags & LYS_CONFIG_MASK;
5273 } else {
5274 /* default is config true */
5275 node->flags |= LYS_CONFIG_W;
5276 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005277 } else {
5278 /* config set explicitely */
5279 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005280 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005281 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5282 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5283 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005284 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005285 goto error;
5286 }
Radek Krejci19a96102018-11-15 13:38:09 +01005287
Radek Krejcia6d57732018-11-29 13:40:37 +01005288 /* *list ordering */
5289 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5290 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005291 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005292 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5293 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005294 node->flags &= ~LYS_ORDBY_MASK;
5295 node->flags |= LYS_ORDBY_SYSTEM;
5296 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5297 /* default ordering is system */
5298 node->flags |= LYS_ORDBY_SYSTEM;
5299 }
5300 }
5301
Radek Krejci19a96102018-11-15 13:38:09 +01005302 /* status - it is not inherited by specification, but it does not make sense to have
5303 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005304 if (!parent || parent->nodetype != LYS_CHOICE) {
5305 /* in case of choice/case's children, postpone the check to the moment we know if
5306 * 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 +02005307 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 +01005308 }
5309
Radek Krejciec4da802019-05-02 13:02:41 +02005310 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005311 node->sp = node_p;
5312 }
5313 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005314 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5315 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005316 if (node_p->when) {
5317 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005318 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005319
5320 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5321 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02005322 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01005323 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005324 }
Radek Krejciec4da802019-05-02 13:02:41 +02005325 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005326
5327 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005328 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005329
Radek Krejci0935f412019-08-20 16:15:18 +02005330 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5331
Radek Krejcife909632019-02-12 15:34:42 +01005332 /* inherit LYS_MAND_TRUE in parent containers */
5333 if (node->flags & LYS_MAND_TRUE) {
5334 lys_compile_mandatory_parents(parent, 1);
5335 }
5336
Radek Krejci327de162019-06-14 12:52:07 +02005337 lysc_update_path(ctx, NULL, NULL);
5338
Radek Krejci19a96102018-11-15 13:38:09 +01005339 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005340 if (parent) {
5341 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005342 if (node_p->parent->nodetype == LYS_CASE) {
5343 lysc_update_path(ctx, parent, node_p->parent->name);
5344 } else {
5345 lysc_update_path(ctx, parent, node->name);
5346 }
Radek Krejciec4da802019-05-02 13:02:41 +02005347 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005348 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005349 if (uses_status) {
5350
5351 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005352 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005353 * it directly gets the same status flags as the choice;
5354 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005355 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005356 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005357 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005358 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005359 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005360 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005361 }
Radek Krejciec4da802019-05-02 13:02:41 +02005362 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 +02005363
5364 if (parent->nodetype == LYS_CHOICE) {
5365 lysc_update_path(ctx, NULL, NULL);
5366 }
Radek Krejci19a96102018-11-15 13:38:09 +01005367 } else {
5368 /* top-level element */
5369 if (!ctx->mod->compiled->data) {
5370 ctx->mod->compiled->data = node;
5371 } else {
5372 /* insert at the end of the module's top-level nodes list */
5373 ctx->mod->compiled->data->prev->next = node;
5374 node->prev = ctx->mod->compiled->data->prev;
5375 ctx->mod->compiled->data->prev = node;
5376 }
Radek Krejci327de162019-06-14 12:52:07 +02005377 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005378 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5379 ctx->mod->compiled->notifs, node->name, node)) {
5380 return LY_EVALID;
5381 }
Radek Krejci19a96102018-11-15 13:38:09 +01005382 }
Radek Krejci327de162019-06-14 12:52:07 +02005383 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005384
5385 return LY_SUCCESS;
5386
5387error:
5388 lysc_node_free(ctx->ctx, node);
5389 return ret;
5390}
5391
Radek Krejciccd20f12019-02-15 14:12:27 +01005392static void
5393lysc_disconnect(struct lysc_node *node)
5394{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005395 struct lysc_node *parent, *child, *prev = NULL, *next;
5396 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005397 int remove_cs = 0;
5398
5399 parent = node->parent;
5400
5401 /* parent's first child */
5402 if (!parent) {
5403 return;
5404 }
5405 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005406 cs = (struct lysc_node_case*)node;
5407 } else if (parent->nodetype == LYS_CASE) {
5408 /* disconnecting some node in a case */
5409 cs = (struct lysc_node_case*)parent;
5410 parent = cs->parent;
5411 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5412 if (child == node) {
5413 if (cs->child == child) {
5414 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5415 /* case with a single child -> remove also the case */
5416 child->parent = NULL;
5417 remove_cs = 1;
5418 } else {
5419 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005420 }
5421 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005422 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005423 }
5424 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005425 if (!remove_cs) {
5426 cs = NULL;
5427 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005428 } else if (lysc_node_children(parent, node->flags) == node) {
5429 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005430 }
5431
5432 if (cs) {
5433 if (remove_cs) {
5434 /* cs has only one child which is being also removed */
5435 lysc_disconnect((struct lysc_node*)cs);
5436 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5437 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005438 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5439 /* default case removed */
5440 ((struct lysc_node_choice*)parent)->dflt = NULL;
5441 }
5442 if (((struct lysc_node_choice*)parent)->cases == cs) {
5443 /* first case removed */
5444 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5445 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005446 if (cs->child) {
5447 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5448 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5449 prev = cs->child->prev;
5450 } /* else all the children are under a single case */
5451 LY_LIST_FOR_SAFE(cs->child, next, child) {
5452 if (child->parent != (struct lysc_node*)cs) {
5453 break;
5454 }
5455 lysc_node_free(node->module->ctx, child);
5456 }
5457 if (prev) {
5458 if (prev->next) {
5459 prev->next = child;
5460 }
5461 if (child) {
5462 child->prev = prev;
5463 } else {
5464 /* link from the first child under the cases */
5465 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5466 }
5467 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005468 }
5469 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005470 }
5471
5472 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005473 if (node->prev->next) {
5474 node->prev->next = node->next;
5475 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005476 if (node->next) {
5477 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005478 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005479 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005480 if (child) {
5481 child->prev = node->prev;
5482 }
5483 } else if (((struct lysc_node_choice*)parent)->cases) {
5484 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005485 }
5486}
5487
5488LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005489lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005490{
Radek Krejcia1911222019-07-22 17:24:50 +02005491 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005492 struct ly_set devs_p = {0};
5493 struct ly_set targets = {0};
5494 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005495 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005496 struct lysc_action *rpcs;
5497 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005498 struct lysp_deviation *dev;
5499 struct lysp_deviate *d, **dp_new;
5500 struct lysp_deviate_add *d_add;
5501 struct lysp_deviate_del *d_del;
5502 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005503 LY_ARRAY_SIZE_TYPE u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005504 struct lysc_deviation {
5505 const char *nodeid;
5506 struct lysc_node *target; /* target node of the deviation */
5507 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005508 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005509 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5510 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005511 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005512 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005513 const char *prefix, *name, *nodeid, *dflt;
Michal Vasko57c10cd2020-05-27 15:57:11 +02005514 struct lys_module *mod, **dev_mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005515 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005516 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005517
5518 /* get all deviations from the module and all its submodules ... */
5519 LY_ARRAY_FOR(mod_p->deviations, u) {
5520 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5521 }
5522 LY_ARRAY_FOR(mod_p->includes, v) {
5523 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5524 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5525 }
5526 }
5527 if (!devs_p.count) {
5528 /* nothing to do */
5529 return LY_SUCCESS;
5530 }
5531
Radek Krejci327de162019-06-14 12:52:07 +02005532 lysc_update_path(ctx, NULL, "{deviation}");
5533
Radek Krejciccd20f12019-02-15 14:12:27 +01005534 /* ... and group them by the target node */
5535 devs = calloc(devs_p.count, sizeof *devs);
5536 for (u = 0; u < devs_p.count; ++u) {
5537 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005538 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005539
5540 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005541 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5542 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01005543 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005544 /* move the target pointer to input/output to make them different from the action and
5545 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5546 * back to the RPC/action node due to a better compatibility and decision code in this function.
5547 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5548 if (flags & LYSC_OPT_RPC_INPUT) {
5549 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5550 flags |= LYSC_OPT_INTERNAL;
5551 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5552 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5553 flags |= LYSC_OPT_INTERNAL;
5554 }
5555 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005556 /* insert into the set of targets with duplicity detection */
5557 i = ly_set_add(&targets, target, 0);
5558 if (!devs[i]) {
5559 /* new record */
5560 devs[i] = calloc(1, sizeof **devs);
5561 devs[i]->target = target;
5562 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005563 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005564 }
5565 /* add deviates into the deviation's list of deviates */
5566 for (d = dev->deviates; d; d = d->next) {
5567 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5568 *dp_new = d;
5569 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5570 devs[i]->not_supported = 1;
5571 }
5572 }
Radek Krejci327de162019-06-14 12:52:07 +02005573
5574 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005575 }
5576
5577 /* MACROS for deviates checking */
5578#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5579 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005580 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 +01005581 goto cleanup; \
5582 }
5583
5584#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5585 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005586 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_SIZE_TYPE") %s properties.", \
Radek Krejci327de162019-06-14 12:52:07 +02005587 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005588 goto cleanup; \
5589 }
5590
Radek Krejcia1911222019-07-22 17:24:50 +02005591
Radek Krejciccd20f12019-02-15 14:12:27 +01005592#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005593 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5595 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5596 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5597 goto cleanup; \
5598 }
5599
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005600#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005601 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005602 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005603 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5604 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005606 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5607 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005608 goto cleanup; \
5609 }
5610
Radek Krejci551b12c2019-02-19 16:11:21 +01005611#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5612 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5613 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005614 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5615 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005616 goto cleanup; \
5617 }
5618
Radek Krejciccd20f12019-02-15 14:12:27 +01005619#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5620 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005621 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005622 goto cleanup; \
5623 }
5624
Radek Krejci551b12c2019-02-19 16:11:21 +01005625#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5626 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5627 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005628 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005629 goto cleanup; \
5630 }
5631
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005632#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5633 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5634 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5635 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5636 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 +01005637 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005638 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005639 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005640 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5641 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005642 goto cleanup; \
5643 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005644 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5645 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5646 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5647 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5648 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005649 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005650 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5651 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5652 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005653 }
5654
5655 /* apply deviations */
5656 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005657 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5658 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5659 struct ly_err_item *err = NULL;
5660
5661 dflt = NULL;
5662 changed_type = 0;
5663
Radek Krejci327de162019-06-14 12:52:07 +02005664 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5665
Radek Krejcif538ce52019-03-05 10:46:14 +01005666 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5667 /* fix the target pointer in case of RPC's/action's input/output */
5668 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5669 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5670 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5671 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5672 }
5673 }
5674
Radek Krejciccd20f12019-02-15 14:12:27 +01005675 /* not-supported */
5676 if (devs[u]->not_supported) {
5677 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005678 LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_SIZE_TYPE") deviates on node \"%s\" since the node is not-supported.",
Radek Krejciccd20f12019-02-15 14:12:27 +01005679 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5680 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005681
5682#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5683 if (devs[u]->target->parent) { \
5684 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5685 } else { \
5686 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5687 } \
5688 LY_ARRAY_FOR(ARRAY, x) { \
5689 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5690 } \
5691 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5692 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5693 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5694 LY_ARRAY_DECREMENT(ARRAY); \
5695 }
5696
Michal Vasko1bf09392020-03-27 12:38:10 +01005697 if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005698 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5699 /* remove RPC's/action's input */
5700 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5701 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005702 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5703 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005704 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5705 /* remove RPC's/action's output */
5706 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5707 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005708 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5709 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005710 } else {
5711 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005712 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005713 }
5714 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005715 /* remove Notification */
5716 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005717 } else {
5718 /* remove the target node */
5719 lysc_disconnect(devs[u]->target);
5720 lysc_node_free(ctx->ctx, devs[u]->target);
5721 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005722
Radek Krejci474f9b82019-07-24 11:36:37 +02005723 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5724 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005725 continue;
5726 }
5727
5728 /* list of deviates (not-supported is not present in the list) */
5729 LY_ARRAY_FOR(devs[u]->deviates, v) {
5730 d = devs[u]->deviates[v];
5731
5732 switch (d->mod) {
5733 case LYS_DEV_ADD:
5734 d_add = (struct lysp_deviate_add*)d;
5735 /* [units-stmt] */
5736 if (d_add->units) {
5737 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5738 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5739
5740 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5741 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5742 }
5743
5744 /* *must-stmt */
5745 if (d_add->musts) {
5746 switch (devs[u]->target->nodetype) {
5747 case LYS_CONTAINER:
5748 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005749 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5750 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005751 break;
5752 case LYS_LEAF:
5753 case LYS_LEAFLIST:
5754 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005755 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5756 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005757 break;
5758 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005759 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5760 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005761 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005762 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005763 case LYS_ACTION:
5764 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005765 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5766 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005767 break;
Michal Vaskocc048b22020-03-27 15:52:38 +01005768 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005769 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5770 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005771 break;
5772 }
5773 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005774 default:
5775 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005776 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005777 goto cleanup;
5778 }
Michal Vasko004d3152020-06-11 19:59:22 +02005779 ly_set_add(&ctx->xpath, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01005780 }
5781
5782 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005783 if (d_add->uniques) {
5784 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5785 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5786 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005787
5788 /* *default-stmt */
5789 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005790 switch (devs[u]->target->nodetype) {
5791 case LYS_LEAF:
5792 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005793 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 +02005794 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005795 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005796 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005797 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5798 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5799 } else {
5800 /* prepare new default value storage */
5801 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005802 }
Radek Krejcia1911222019-07-22 17:24:50 +02005803 dflt = d_add->dflts[0];
5804 /* parsing is done at the end after possible replace of the leaf's type */
5805
Radek Krejci551b12c2019-02-19 16:11:21 +01005806 /* mark the new default values as leaf's own */
5807 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005808 break;
5809 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005810 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005811 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005812 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005813 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005814 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5815 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5816 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005817 }
Radek Krejcia1911222019-07-22 17:24:50 +02005818 LY_ARRAY_FREE(llist->dflts);
5819 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005820 LY_ARRAY_FREE(llist->dflts_mods);
5821 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005822 }
5823 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005824 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005825 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5826 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01005827 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005828 LY_ARRAY_INCREMENT(llist->dflts_mods);
5829 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005830 LY_ARRAY_INCREMENT(llist->dflts);
5831 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5832 llist->dflts[x]->realtype = llist->type;
5833 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 +02005834 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005835 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005836 llist->dflts[x]->realtype->refcount++;
5837 if (err) {
5838 ly_err_print(err);
5839 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5840 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5841 d_add->dflts[x - y], err->msg);
5842 ly_err_free(err);
5843 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005844 if (rc == LY_EINCOMPLETE) {
5845 /* postpone default compilation when the tree is complete */
5846 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5847
5848 /* but in general result is so far ok */
5849 rc = LY_SUCCESS;
5850 }
Radek Krejcia1911222019-07-22 17:24:50 +02005851 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005852 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005853 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005854 devs[u]->target->flags |= LYS_SET_DFLT;
5855 break;
5856 case LYS_CHOICE:
5857 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5858 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5859 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5860 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005861 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 +01005862 goto cleanup;
5863 }
5864 break;
5865 default:
5866 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005867 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005868 goto cleanup;
5869 }
5870 }
5871
5872 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005873 if (d_add->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01005874 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005875 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005876 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5877 goto cleanup;
5878 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005879 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005880 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
Michal Vaskoa3881362020-01-21 15:57:35 +01005881 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005882 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005883 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5884 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005885 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5886 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005887 goto cleanup;
5888 }
Radek Krejci327de162019-06-14 12:52:07 +02005889 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005890 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005891
5892 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005893 if (d_add->flags & LYS_MAND_MASK) {
5894 if (devs[u]->target->flags & LYS_MAND_MASK) {
5895 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005896 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5897 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005898 goto cleanup;
5899 }
Radek Krejci327de162019-06-14 12:52:07 +02005900 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005901 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005902
5903 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005904 if (d_add->flags & LYS_SET_MIN) {
5905 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5906 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5907 /* change value */
5908 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5909 } else if (devs[u]->target->nodetype == LYS_LIST) {
5910 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5911 /* change value */
5912 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5913 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005914 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005915 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5916 goto cleanup;
5917 }
5918 if (d_add->min) {
5919 devs[u]->target->flags |= LYS_MAND_TRUE;
5920 }
5921 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005922
5923 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005924 if (d_add->flags & LYS_SET_MAX) {
5925 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5926 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5927 /* change value */
5928 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5929 } else if (devs[u]->target->nodetype == LYS_LIST) {
5930 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5931 /* change value */
5932 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5933 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005934 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005935 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5936 goto cleanup;
5937 }
5938 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005939
5940 break;
5941 case LYS_DEV_DELETE:
5942 d_del = (struct lysp_deviate_del*)d;
5943
5944 /* [units-stmt] */
5945 if (d_del->units) {
5946 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02005947 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
5948 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
5949 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5950 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
5951 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5952 goto cleanup;
5953 }
5954 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5955 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005956 }
5957
5958 /* *must-stmt */
5959 if (d_del->musts) {
5960 switch (devs[u]->target->nodetype) {
5961 case LYS_CONTAINER:
5962 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005963 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005964 break;
5965 case LYS_LEAF:
5966 case LYS_LEAFLIST:
5967 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005968 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005969 break;
5970 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005971 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005972 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005973 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005974 case LYS_ACTION:
5975 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5976 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5977 break;
5978 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5979 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5980 break;
5981 }
5982 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005983 default:
5984 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005985 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005986 goto cleanup;
5987 }
5988 }
5989
5990 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005991 if (d_del->uniques) {
5992 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5993 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5994 LY_ARRAY_FOR(d_del->uniques, x) {
5995 LY_ARRAY_FOR(list->uniques, z) {
5996 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5997 nodeid = strpbrk(name, " \t\n");
5998 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02005999 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006000 break;
6001 }
6002 while (isspace(*nodeid)) {
6003 ++nodeid;
6004 }
6005 } else {
6006 if (strcmp(name, list->uniques[z][y]->name)) {
6007 break;
6008 }
6009 }
6010 }
6011 if (!name) {
6012 /* complete match - remove the unique */
6013 LY_ARRAY_DECREMENT(list->uniques);
6014 LY_ARRAY_FREE(list->uniques[z]);
6015 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6016 --z;
6017 break;
6018 }
6019 }
6020 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6021 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006022 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6023 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006024 goto cleanup;
6025 }
6026 }
6027 if (!LY_ARRAY_SIZE(list->uniques)) {
6028 LY_ARRAY_FREE(list->uniques);
6029 list->uniques = NULL;
6030 }
6031 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006032
6033 /* *default-stmt */
6034 if (d_del->dflts) {
6035 switch (devs[u]->target->nodetype) {
6036 case LYS_LEAF:
6037 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6038 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6039 dflt, "deleting", "default", d_del->dflts[0]);
6040
Radek Krejcia1911222019-07-22 17:24:50 +02006041 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006042 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006043 if (strcmp(dflt, d_del->dflts[0])) {
6044 if (i) {
6045 free((char*)dflt);
6046 }
6047 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6048 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6049 d_del->dflts[0], dflt);
6050 goto cleanup;
6051 }
6052 if (i) {
6053 free((char*)dflt);
6054 }
6055 dflt = NULL;
6056
Radek Krejci474f9b82019-07-24 11:36:37 +02006057 /* update the list of incomplete default values if needed */
6058 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6059
Radek Krejcia1911222019-07-22 17:24:50 +02006060 /* remove the default specification */
6061 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6062 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6063 free(leaf->dflt);
6064 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006065 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006066 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006067 break;
6068 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006069 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6070 LY_ARRAY_FOR(d_del->dflts, x) {
6071 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006072 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 +02006073 if (!strcmp(dflt, d_del->dflts[x])) {
6074 if (i) {
6075 free((char*)dflt);
6076 }
6077 dflt = NULL;
6078 break;
6079 }
6080 if (i) {
6081 free((char*)dflt);
6082 }
6083 dflt = NULL;
6084 }
6085 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6086 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6087 "which does not match any of the target's property values.", d_del->dflts[x]);
6088 goto cleanup;
6089 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006090
6091 /* update the list of incomplete default values if needed */
6092 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6093
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006094 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006095 LY_ARRAY_DECREMENT(llist->dflts);
6096 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6097 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6098 free(llist->dflts[y]);
6099 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006100 memmove(&llist->dflts_mods[y], &llist->dflts_mods[y + 1], (LY_ARRAY_SIZE(llist->dflts_mods) - y) * (sizeof *llist->dflts_mods));
Radek Krejcia1911222019-07-22 17:24:50 +02006101 }
6102 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006103 LY_ARRAY_FREE(llist->dflts_mods);
6104 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006105 LY_ARRAY_FREE(llist->dflts);
6106 llist->dflts = NULL;
6107 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006108 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006109 break;
6110 case LYS_CHOICE:
6111 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6112 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6113 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006114 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006115 if (prefix) {
6116 /* use module prefixes from the deviation module to match the module of the default case */
6117 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6118 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006119 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6120 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006121 goto cleanup;
6122 }
6123 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6124 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006125 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6126 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006127 goto cleanup;
6128 }
6129 }
6130 /* else {
6131 * strictly, the default prefix would point to the deviation module, but the value should actually
6132 * match the default string in the original module (usually unprefixed), so in this case we do not check
6133 * the module of the default case, just matching its name */
6134 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6135 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006136 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6137 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006138 goto cleanup;
6139 }
6140 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6141 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6142 break;
6143 default:
6144 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006145 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006146 goto cleanup;
6147 }
6148 }
6149
6150 break;
6151 case LYS_DEV_REPLACE:
6152 d_rpl = (struct lysp_deviate_rpl*)d;
6153
6154 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006155 if (d_rpl->type) {
6156 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6157 /* type is mandatory, so checking for its presence is not necessary */
6158 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006159
6160 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6161 /* the target has default from the previous type - remove it */
6162 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006163 /* update the list of incomplete default values if needed */
6164 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6165
Radek Krejcia1911222019-07-22 17:24:50 +02006166 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6167 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6168 free(leaf->dflt);
6169 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006170 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006171 } else { /* LYS_LEAFLIST */
6172 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006173 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006174 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6175 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6176 free(llist->dflts[x]);
6177 }
6178 LY_ARRAY_FREE(llist->dflts);
6179 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006180 LY_ARRAY_FREE(llist->dflts_mods);
6181 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006182 }
6183 }
6184 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006185 /* there is no default value, do not set changed_type after type compilation
6186 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006187 changed_type = -1;
6188 }
Radek Krejciec4da802019-05-02 13:02:41 +02006189 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 +02006190 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006191 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006192
6193 /* [units-stmt] */
6194 if (d_rpl->units) {
6195 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6196 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6197 units, "replacing", "units", d_rpl->units);
6198
6199 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6200 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6201 }
6202
6203 /* [default-stmt] */
6204 if (d_rpl->dflt) {
6205 switch (devs[u]->target->nodetype) {
6206 case LYS_LEAF:
6207 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6208 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006209 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006210 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006211 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6212 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6213 dflt = d_rpl->dflt;
6214 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006215 break;
6216 case LYS_CHOICE:
6217 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006218 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 +01006219 goto cleanup;
6220 }
6221 break;
6222 default:
6223 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006224 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006225 goto cleanup;
6226 }
6227 }
6228
6229 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006230 if (d_rpl->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01006231 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006232 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006233 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6234 goto cleanup;
6235 }
6236 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006237 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006238 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6239 goto cleanup;
6240 }
Radek Krejci327de162019-06-14 12:52:07 +02006241 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006242 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006243
6244 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006245 if (d_rpl->flags & LYS_MAND_MASK) {
6246 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006247 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006248 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6249 goto cleanup;
6250 }
Radek Krejci327de162019-06-14 12:52:07 +02006251 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006252 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006253
6254 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006255 if (d_rpl->flags & LYS_SET_MIN) {
6256 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6257 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6258 /* change value */
6259 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6260 } else if (devs[u]->target->nodetype == LYS_LIST) {
6261 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6262 /* change value */
6263 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6264 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006265 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006266 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6267 goto cleanup;
6268 }
6269 if (d_rpl->min) {
6270 devs[u]->target->flags |= LYS_MAND_TRUE;
6271 }
6272 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006273
6274 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006275 if (d_rpl->flags & LYS_SET_MAX) {
6276 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6277 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6278 /* change value */
6279 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6280 } else if (devs[u]->target->nodetype == LYS_LIST) {
6281 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6282 /* change value */
6283 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6284 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006286 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6287 goto cleanup;
6288 }
6289 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006290
6291 break;
6292 default:
6293 LOGINT(ctx->ctx);
6294 goto cleanup;
6295 }
6296 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006297
Radek Krejci33f72892019-02-21 10:36:58 +01006298 /* final check when all deviations of a single target node are applied */
6299
Radek Krejci551b12c2019-02-19 16:11:21 +01006300 /* check min-max compatibility */
6301 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6302 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6303 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6304 } else if (devs[u]->target->nodetype == LYS_LIST) {
6305 min = ((struct lysc_node_list*)devs[u]->target)->min;
6306 max = ((struct lysc_node_list*)devs[u]->target)->max;
6307 } else {
6308 min = max = 0;
6309 }
6310 if (min > max) {
6311 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 +02006312 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006313 goto cleanup;
6314 }
6315
Radek Krejcia1911222019-07-22 17:24:50 +02006316 if (dflt) {
6317 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006318 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006319 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006320 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6321 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006322 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006323 leaf->dflt->realtype->refcount++;
6324 if (err) {
6325 ly_err_print(err);
6326 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6327 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6328 ly_err_free(err);
6329 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006330 if (rc == LY_EINCOMPLETE) {
6331 /* postpone default compilation when the tree is complete */
6332 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6333
6334 /* but in general result is so far ok */
6335 rc = LY_SUCCESS;
6336 }
Radek Krejcia1911222019-07-22 17:24:50 +02006337 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006338 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006339 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6340 int dynamic;
6341 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006342 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006343
6344 /* update the list of incomplete default values if needed */
6345 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6346
6347 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006348 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6349 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6350 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006351 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6352 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006353 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006354 leaf->dflt->realtype->refcount++;
6355 if (err) {
6356 ly_err_print(err);
6357 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6358 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6359 ly_err_free(err);
6360 }
6361 if (dynamic) {
6362 free((void*)dflt);
6363 }
Radek Krejcia1911222019-07-22 17:24:50 +02006364 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006365 if (rc == LY_EINCOMPLETE) {
6366 /* postpone default compilation when the tree is complete */
6367 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6368
6369 /* but in general result is so far ok */
6370 rc = LY_SUCCESS;
6371 }
6372 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006373 } else { /* LYS_LEAFLIST */
6374 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006375 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 +02006376 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6377 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6378 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006379 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6380 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006381 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6382 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006383 llist->dflts[x]->realtype->refcount++;
6384 if (err) {
6385 ly_err_print(err);
6386 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6387 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6388 dflt, err->msg);
6389 ly_err_free(err);
6390 }
6391 if (dynamic) {
6392 free((void*)dflt);
6393 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006394 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006395 if (rc == LY_EINCOMPLETE) {
6396 /* postpone default compilation when the tree is complete */
6397 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6398
6399 /* but in general result is so far ok */
6400 rc = LY_SUCCESS;
6401 }
Radek Krejcia1911222019-07-22 17:24:50 +02006402 LY_CHECK_GOTO(rc, cleanup);
6403 }
6404 }
6405 }
6406
Radek Krejci551b12c2019-02-19 16:11:21 +01006407 /* check mandatory - default compatibility */
6408 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6409 && (devs[u]->target->flags & LYS_SET_DFLT)
6410 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6411 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006412 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006413 goto cleanup;
6414 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6415 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6416 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006417 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 +01006418 goto cleanup;
6419 }
6420 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6421 /* mandatory node under a default case */
6422 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006423 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6424 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006425 goto cleanup;
6426 }
Radek Krejci33f72892019-02-21 10:36:58 +01006427
Michal Vasko57c10cd2020-05-27 15:57:11 +02006428 /* add this module into the target module deviated_by */
6429 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup);
6430 *dev_mod = mod_p->mod;
6431
Radek Krejci327de162019-06-14 12:52:07 +02006432 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006433 }
6434
Radek Krejci327de162019-06-14 12:52:07 +02006435 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006436 ret = LY_SUCCESS;
6437
6438cleanup:
6439 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6440 LY_ARRAY_FREE(devs[u]->deviates);
6441 free(devs[u]);
6442 }
6443 free(devs);
6444 ly_set_erase(&targets, NULL);
6445 ly_set_erase(&devs_p, NULL);
6446
6447 return ret;
6448}
6449
Radek Krejcib56c7502019-02-13 14:19:54 +01006450/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006451 * @brief Compile the given YANG submodule into the main module.
6452 * @param[in] ctx Compile context
6453 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006454 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6455 */
6456LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006457lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006458{
6459 unsigned int u;
6460 LY_ERR ret = LY_SUCCESS;
6461 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006462 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006463 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006464 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006465
Radek Krejci0af46292019-01-11 16:02:31 +01006466 if (!mainmod->mod->off_features) {
6467 /* features are compiled directly into the compiled module structure,
6468 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6469 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006470 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6471 LY_CHECK_GOTO(ret, error);
6472 }
Radek Krejci0af46292019-01-11 16:02:31 +01006473
Radek Krejci327de162019-06-14 12:52:07 +02006474 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006475 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006476 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006477
Radek Krejci474f9b82019-07-24 11:36:37 +02006478 /* data nodes */
6479 LY_LIST_FOR(submod->data, node_p) {
6480 ret = lys_compile_node(ctx, node_p, NULL, 0);
6481 LY_CHECK_GOTO(ret, error);
6482 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006483
Radek Krejciec4da802019-05-02 13:02:41 +02006484 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6485 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006486
Radek Krejcid05cbd92018-12-05 14:26:40 +01006487error:
6488 return ret;
6489}
6490
Radek Krejci335332a2019-09-05 13:03:35 +02006491static void *
6492lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6493{
6494 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6495 if (substmts[u].stmt == stmt) {
6496 return substmts[u].storage;
6497 }
6498 }
6499 return NULL;
6500}
6501
6502LY_ERR
6503lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6504{
6505 LY_ERR ret = LY_EVALID, r;
6506 unsigned int u;
6507 struct lysp_stmt *stmt;
6508 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006509
6510 /* check for invalid substatements */
6511 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006512 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6513 continue;
6514 }
Radek Krejci335332a2019-09-05 13:03:35 +02006515 for (u = 0; substmts[u].stmt; ++u) {
6516 if (substmts[u].stmt == stmt->kw) {
6517 break;
6518 }
6519 }
6520 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006521 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6522 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006523 goto cleanup;
6524 }
Radek Krejci335332a2019-09-05 13:03:35 +02006525 }
6526
Radek Krejciad5963b2019-09-06 16:03:05 +02006527 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6528
Radek Krejci335332a2019-09-05 13:03:35 +02006529 /* keep order of the processing the same as the order in the defined substmts,
6530 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6531 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006532 int stmt_present = 0;
6533
Radek Krejci335332a2019-09-05 13:03:35 +02006534 for (stmt = ext->child; stmt; stmt = stmt->next) {
6535 if (substmts[u].stmt != stmt->kw) {
6536 continue;
6537 }
6538
Radek Krejciad5963b2019-09-06 16:03:05 +02006539 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006540 if (substmts[u].storage) {
6541 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006542 case LY_STMT_STATUS:
6543 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6544 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6545 break;
6546 case LY_STMT_UNITS: {
6547 const char **units;
6548
6549 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6550 /* single item */
6551 if (*((const char **)substmts[u].storage)) {
6552 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6553 goto cleanup;
6554 }
6555 units = (const char **)substmts[u].storage;
6556 } else {
6557 /* sized array */
6558 const char ***units_array = (const char ***)substmts[u].storage;
6559 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6560 }
6561 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6562 break;
6563 }
Radek Krejci335332a2019-09-05 13:03:35 +02006564 case LY_STMT_TYPE: {
6565 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6566 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6567
6568 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6569 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006570 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006571 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6572 goto cleanup;
6573 }
6574 compiled = substmts[u].storage;
6575 } else {
6576 /* sized array */
6577 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6578 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6579 compiled = (void*)type;
6580 }
6581
Radek Krejciad5963b2019-09-06 16:03:05 +02006582 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006583 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6584 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006585 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6586 lysp_type_free(ctx->ctx, parsed);
6587 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006588 break;
6589 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006590 case LY_STMT_IF_FEATURE: {
6591 struct lysc_iffeature *iff = NULL;
6592
6593 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6594 /* single item */
6595 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6597 goto cleanup;
6598 }
6599 iff = (struct lysc_iffeature*)substmts[u].storage;
6600 } else {
6601 /* sized array */
6602 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6603 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6604 }
6605 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6606 break;
6607 }
6608 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6609 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006610 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006611 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.",
6612 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006613 goto cleanup;
6614 }
6615 }
Radek Krejci335332a2019-09-05 13:03:35 +02006616 }
Radek Krejci335332a2019-09-05 13:03:35 +02006617
Radek Krejciad5963b2019-09-06 16:03:05 +02006618 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6619 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6620 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6621 goto cleanup;
6622 }
Radek Krejci335332a2019-09-05 13:03:35 +02006623 }
6624
6625 ret = LY_SUCCESS;
6626
6627cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006628 return ret;
6629}
6630
Michal Vasko175012e2019-11-06 15:49:14 +01006631/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006632 * @brief Check when for cyclic dependencies.
6633 * @param[in] set Set with all the referenced nodes.
6634 * @param[in] node Node whose "when" referenced nodes are in @p set.
6635 * @return LY_ERR value
6636 */
6637static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006638lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006639{
6640 struct lyxp_set tmp_set;
6641 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006642 uint32_t i, j;
6643 LY_ARRAY_SIZE_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006644 int idx;
6645 struct lysc_when *when;
6646 LY_ERR ret = LY_SUCCESS;
6647
6648 memset(&tmp_set, 0, sizeof tmp_set);
6649
6650 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006651 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006652 xp_scnode = &set->val.scnodes[i];
6653
Michal Vasko5c4e5892019-11-14 12:31:38 +01006654 if (xp_scnode->in_ctx != -1) {
6655 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006656 xp_scnode->in_ctx = 1;
6657 }
6658 }
6659
6660 for (i = 0; i < set->used; ++i) {
6661 xp_scnode = &set->val.scnodes[i];
6662 if (xp_scnode->in_ctx != 1) {
6663 /* already checked */
6664 continue;
6665 }
6666
Michal Vasko1bf09392020-03-27 12:38:10 +01006667 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006668 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006669 /* no when to check */
6670 xp_scnode->in_ctx = 0;
6671 continue;
6672 }
6673
6674 node = xp_scnode->scnode;
6675 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006676 LY_ARRAY_FOR(node->when, u) {
6677 when = node->when[u];
Michal Vasko52927e22020-03-16 17:26:14 +01006678 ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006679 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6680 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006681 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006682 goto cleanup;
6683 }
6684
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006685 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006686 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006687 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006688 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006689 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 +01006690 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006691 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 +01006692 ret = LY_EVALID;
6693 goto cleanup;
6694 }
6695
6696 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006697 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006698 } else {
6699 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006700 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006701 }
6702 }
6703
6704 /* merge this set into the global when set */
6705 lyxp_set_scnode_merge(set, &tmp_set);
6706 }
6707
6708 /* check when of non-data parents as well */
6709 node = node->parent;
6710 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6711
Michal Vasko251f56e2019-11-14 16:06:47 +01006712 /* this node when was checked (xp_scnode could have been reallocd) */
6713 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006714 }
6715
6716cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006717 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006718 return ret;
6719}
6720
6721/**
Michal Vasko175012e2019-11-06 15:49:14 +01006722 * @brief Check when/must expressions of a node on a compiled schema tree.
6723 * @param[in] ctx Compile context.
6724 * @param[in] node Node to check.
6725 * @return LY_ERR value
6726 */
6727static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006728lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006729{
Michal Vasko175012e2019-11-06 15:49:14 +01006730 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006731 uint32_t i;
6732 LY_ARRAY_SIZE_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01006733 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006734 struct lysc_when **when = NULL;
6735 struct lysc_must *musts = NULL;
6736 LY_ERR ret = LY_SUCCESS;
6737
6738 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006739 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko175012e2019-11-06 15:49:14 +01006740
6741 switch (node->nodetype) {
6742 case LYS_CONTAINER:
6743 when = ((struct lysc_node_container *)node)->when;
6744 musts = ((struct lysc_node_container *)node)->musts;
6745 break;
6746 case LYS_CHOICE:
6747 when = ((struct lysc_node_choice *)node)->when;
6748 break;
6749 case LYS_LEAF:
6750 when = ((struct lysc_node_leaf *)node)->when;
6751 musts = ((struct lysc_node_leaf *)node)->musts;
6752 break;
6753 case LYS_LEAFLIST:
6754 when = ((struct lysc_node_leaflist *)node)->when;
6755 musts = ((struct lysc_node_leaflist *)node)->musts;
6756 break;
6757 case LYS_LIST:
6758 when = ((struct lysc_node_list *)node)->when;
6759 musts = ((struct lysc_node_list *)node)->musts;
6760 break;
6761 case LYS_ANYXML:
6762 case LYS_ANYDATA:
6763 when = ((struct lysc_node_anydata *)node)->when;
6764 musts = ((struct lysc_node_anydata *)node)->musts;
6765 break;
6766 case LYS_CASE:
6767 when = ((struct lysc_node_case *)node)->when;
6768 break;
6769 case LYS_NOTIF:
6770 musts = ((struct lysc_notif *)node)->musts;
6771 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006772 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006773 case LYS_ACTION:
6774 /* first process input musts */
6775 musts = ((struct lysc_action *)node)->input.musts;
6776 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006777 default:
6778 /* nothing to check */
6779 break;
6780 }
6781
Michal Vasko175012e2019-11-06 15:49:14 +01006782 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006783 LY_ARRAY_FOR(when, u) {
Michal Vasko004d3152020-06-11 19:59:22 +02006784 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 +02006785 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006786 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006787 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006788 goto cleanup;
6789 }
6790
Michal Vaskodc052f32019-11-07 11:11:38 +01006791 ctx->path[0] = '\0';
6792 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006793 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006794 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006795 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6796 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006797
Michal Vaskoecd62de2019-11-13 12:35:11 +01006798 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006799 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006800 schema->name);
6801 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006802
6803 /* check dummy node accessing */
6804 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006805 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006806 ret = LY_EVALID;
6807 goto cleanup;
6808 }
Michal Vasko175012e2019-11-06 15:49:14 +01006809 }
6810 }
6811
Michal Vaskoecd62de2019-11-13 12:35:11 +01006812 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006813 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006814 LY_CHECK_GOTO(ret, cleanup);
6815
Michal Vaskod3678892020-05-21 10:06:58 +02006816 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006817 }
6818
Michal Vasko5d8756a2019-11-07 15:21:00 +01006819check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006820 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006821 LY_ARRAY_FOR(musts, u) {
6822 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 +01006823 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006824 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006825 goto cleanup;
6826 }
6827
Michal Vaskodc052f32019-11-07 11:11:38 +01006828 ctx->path[0] = '\0';
6829 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006830 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006831 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006832 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006833 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006834 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6835 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006836 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006837 }
6838 }
6839
Michal Vaskod3678892020-05-21 10:06:58 +02006840 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006841 }
6842
Michal Vasko1bf09392020-03-27 12:38:10 +01006843 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006844 /* now check output musts */
6845 input_done = 1;
6846 musts = ((struct lysc_action *)node)->output.musts;
6847 opts = LYXP_SCNODE_OUTPUT;
6848 goto check_musts;
6849 }
6850
Michal Vasko175012e2019-11-06 15:49:14 +01006851cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006852 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006853 return ret;
6854}
6855
Michal Vasko8d544252020-03-02 10:19:52 +01006856static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006857lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6858{
6859 const struct lysc_node *target = NULL;
6860 struct ly_path *p;
6861 struct lysc_type *type;
6862
6863 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6864
6865 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02006866 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
6867 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
6868 lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02006869
6870 /* get the target node */
6871 target = p[LY_ARRAY_SIZE(p) - 1].node;
6872 ly_path_free(node->module->ctx, p);
6873
6874 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
6875 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6876 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
6877 lref->path->expr, lys_nodetype2str(target->nodetype));
6878 return LY_EVALID;
6879 }
6880
6881 /* check status */
6882 ctx->path[0] = '\0';
6883 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6884 ctx->path_len = strlen(ctx->path);
6885 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
6886 return LY_EVALID;
6887 }
6888 ctx->path_len = 1;
6889 ctx->path[1] = '\0';
6890
6891 /* check config */
6892 if (lref->require_instance && (node->flags & LYS_CONFIG_W)) {
6893 if (target->flags & LYS_CONFIG_R) {
6894 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
6895 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
6896 return LY_EVALID;
6897 }
6898 }
6899
6900 /* store the target's type and check for circular chain of leafrefs */
6901 lref->realtype = ((struct lysc_node_leaf *)target)->type;
6902 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
6903 if (type == (struct lysc_type *)lref) {
6904 /* circular chain detected */
6905 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6906 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
6907 return LY_EVALID;
6908 }
6909 }
6910
6911 /* check if leafref and its target are under common if-features */
6912 if (lys_compile_leafref_features_validate(node, target)) {
6913 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6914 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
6915 " features applicable to the leafref itself.", lref->path->expr);
6916 return LY_EVALID;
6917 }
6918
6919 return LY_SUCCESS;
6920}
6921
6922static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01006923lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
6924{
6925 struct lysc_ext_instance *ext;
6926 struct lysp_ext_instance *ext_p = NULL;
6927 struct lysp_stmt *stmt;
6928 const struct lys_module *ext_mod;
6929 LY_ERR ret = LY_SUCCESS;
6930
6931 /* create the parsed extension instance manually */
6932 ext_p = calloc(1, sizeof *ext_p);
6933 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
6934 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
6935 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
6936 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
6937 ext_p->insubstmt_index = 0;
6938
6939 stmt = calloc(1, sizeof *ext_p->child);
6940 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
6941 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
6942 stmt->kw = LY_STMT_TYPE;
6943 ext_p->child = stmt;
6944
6945 /* allocate new extension instance */
6946 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
6947
6948 /* manually get extension definition module */
6949 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
6950
6951 /* compile the extension instance */
6952 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
6953
6954cleanup:
6955 lysp_ext_instance_free(ctx->ctx, ext_p);
6956 free(ext_p);
6957 return ret;
6958}
6959
Michal Vasko004d3152020-06-11 19:59:22 +02006960static LY_ERR
6961lys_compile_unres(struct lysc_ctx *ctx)
6962{
6963 struct lysc_node *node;
6964 struct lysc_type *type, *typeiter;
6965 struct lysc_type_leafref *lref;
6966 LY_ARRAY_SIZE_TYPE v;
6967 uint32_t i;
6968
6969 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6970 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6971 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
6972 for (i = 0; i < ctx->leafrefs.count; ++i) {
6973 node = ctx->leafrefs.objs[i];
6974 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6975 type = ((struct lysc_node_leaf *)node)->type;
6976 if (type->basetype == LY_TYPE_LEAFREF) {
6977 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
6978 } else if (type->basetype == LY_TYPE_UNION) {
6979 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
6980 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6981 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
6982 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
6983 }
6984 }
6985 }
6986 }
6987 for (i = 0; i < ctx->leafrefs.count; ++i) {
6988 /* store pointer to the real type */
6989 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
6990 if (type->basetype == LY_TYPE_LEAFREF) {
6991 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6992 typeiter->basetype == LY_TYPE_LEAFREF;
6993 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6994 ((struct lysc_type_leafref*)type)->realtype = typeiter;
6995 } else if (type->basetype == LY_TYPE_UNION) {
6996 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6997 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6998 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6999 typeiter->basetype == LY_TYPE_LEAFREF;
7000 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7001 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7002 }
7003 }
7004 }
7005 }
7006
7007 /* check xpath */
7008 for (i = 0; i < ctx->xpath.count; ++i) {
7009 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7010 }
7011
7012 /* finish incomplete default values compilation */
7013 for (i = 0; i < ctx->dflts.count; ++i) {
7014 struct ly_err_item *err = NULL;
7015 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
7016 LY_ERR ret;
7017 ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
7018 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7019 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7020 if (err) {
7021 ly_err_print(err);
7022 ctx->path[0] = '\0';
7023 lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7024 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7025 "Invalid default - value does not fit the type (%s).", err->msg);
7026 ly_err_free(err);
7027 }
7028 LY_CHECK_RET(ret);
7029 }
7030
7031 return LY_SUCCESS;
7032}
7033
Radek Krejci19a96102018-11-15 13:38:09 +01007034LY_ERR
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007035lys_compile(struct lys_module **mod, int options)
Radek Krejci19a96102018-11-15 13:38:09 +01007036{
7037 struct lysc_ctx ctx = {0};
7038 struct lysc_module *mod_c;
7039 struct lysp_module *sp;
7040 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007041 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007042 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007043 struct lys_module *m;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007044 LY_ARRAY_SIZE_TYPE u, v;
7045 uint32_t i;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007046 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007047
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007048 LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007049
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007050 if (!(*mod)->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007051 /* just imported modules are not compiled */
7052 return LY_SUCCESS;
7053 }
7054
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007055 sp = (*mod)->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007056
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007057 ctx.ctx = (*mod)->ctx;
7058 ctx.mod = *mod;
7059 ctx.mod_def = *mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007060 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007061 ctx.path_len = 1;
7062 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007063
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007064 (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
7065 LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
7066 mod_c->mod = *mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007067
Radek Krejciec4da802019-05-02 13:02:41 +02007068 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007069 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007070 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007071 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7072 }
Radek Krejci0935f412019-08-20 16:15:18 +02007073
7074 /* features */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007075 if ((*mod)->off_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007076 /* there is already precompiled array of features */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007077 mod_c->features = (*mod)->off_features;
7078 (*mod)->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007079 } else {
7080 /* features are compiled directly into the compiled module structure,
7081 * 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 +02007082 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007083 LY_CHECK_GOTO(ret, error);
7084 }
7085 /* finish feature compilation, not only for the main module, but also for the submodules.
7086 * Due to possible forward references, it must be done when all the features (including submodules)
7087 * are present. */
7088 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007089 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007090 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7091 }
Radek Krejci327de162019-06-14 12:52:07 +02007092 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007093 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007094 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007095 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007096 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007097 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7098 }
Radek Krejci327de162019-06-14 12:52:07 +02007099 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007100 }
Radek Krejci327de162019-06-14 12:52:07 +02007101 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007102
Radek Krejci0935f412019-08-20 16:15:18 +02007103 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02007104 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02007105 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007106 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007107 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007108 }
Radek Krejci327de162019-06-14 12:52:07 +02007109 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007110
Radek Krejci95710c92019-02-11 15:49:55 +01007111 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007112 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007113 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007114 }
Radek Krejci95710c92019-02-11 15:49:55 +01007115
Radek Krejciec4da802019-05-02 13:02:41 +02007116 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7117 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007118
Radek Krejci95710c92019-02-11 15:49:55 +01007119 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007120 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007121 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007122 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007123 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007124
Radek Krejci474f9b82019-07-24 11:36:37 +02007125 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007126 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007127
Radek Krejci0935f412019-08-20 16:15:18 +02007128 /* extension instances TODO cover extension instances from submodules */
7129 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007130
Michal Vasko004d3152020-06-11 19:59:22 +02007131 /* finish compilation for all unresolved items in the context */
7132 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007133
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007134 /* validate non-instantiated groupings from the parsed schema,
7135 * without it we would accept even the schemas with invalid grouping specification */
7136 ctx.options |= LYSC_OPT_GROUPING;
7137 LY_ARRAY_FOR(sp->groupings, u) {
7138 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007139 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007140 }
7141 }
7142 LY_LIST_FOR(sp->data, node_p) {
7143 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7144 LY_ARRAY_FOR(grps, u) {
7145 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007146 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007147 }
7148 }
7149 }
7150
Radek Krejci474f9b82019-07-24 11:36:37 +02007151 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7152 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7153 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7154 }
7155
Michal Vasko8d544252020-03-02 10:19:52 +01007156#if 0
7157 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7158 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7159 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7160 * the anotation definitions available in the internal schema structure. */
7161 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7162 if (lyp_add_ietf_netconf_annotations(mod)) {
7163 lys_free(mod, NULL, 1, 1);
7164 return NULL;
7165 }
7166 }
7167#endif
7168
7169 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007170 if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
7171 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007172 }
7173
Radek Krejci1c0c3442019-07-23 16:08:47 +02007174 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007175 ly_set_erase(&ctx.xpath, NULL);
7176 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007177 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007178 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007179 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007180
Radek Krejciec4da802019-05-02 13:02:41 +02007181 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007182 lysp_module_free((*mod)->parsed);
7183 (*mod)->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007184 }
7185
Radek Krejciec4da802019-05-02 13:02:41 +02007186 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007187 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007188 for (i = 0; i < ctx.ctx->list.count; ++i) {
7189 m = ctx.ctx->list.objs[i];
Radek Krejci95710c92019-02-11 15:49:55 +01007190 if (m->implemented == 2) {
7191 m->implemented = 1;
7192 }
7193 }
7194 }
7195
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007196 (*mod)->compiled = mod_c;
Radek Krejci19a96102018-11-15 13:38:09 +01007197 return LY_SUCCESS;
7198
7199error:
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007200 lys_feature_precompile_revert(&ctx, *mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007201 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007202 ly_set_erase(&ctx.xpath, NULL);
7203 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007204 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007205 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007206 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007207 lysc_module_free(mod_c, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007208 (*mod)->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007209
7210 /* revert compilation of modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007211 for (i = 0; i < ctx.ctx->list.count; ++i) {
7212 m = ctx.ctx->list.objs[i];
Michal Vasko2947ce12019-12-16 10:37:19 +01007213 if ((m->implemented == 2) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007214 /* revert features list to the precompiled state */
7215 lys_feature_precompile_revert(&ctx, m);
7216 /* mark module as imported-only / not-implemented */
7217 m->implemented = 0;
7218 /* free the compiled version of the module */
7219 lysc_module_free(m->compiled, NULL);
7220 m->compiled = NULL;
7221 }
7222 }
7223
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007224 /* remove the module itself from the context and free it */
7225 ly_set_rm(&ctx.ctx->list, *mod, NULL);
7226 lys_module_free(*mod, NULL);
7227 *mod = NULL;
7228
Radek Krejci19a96102018-11-15 13:38:09 +01007229 return ret;
7230}