blob: c5e56a57df1bd80fdd92a9f6f54e46419766c62e [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;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004357 struct lys_module **aug_mod;
Radek Krejci3641f562019-02-13 15:38:40 +01004358 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004359 uint16_t flags = 0;
4360 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004361 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004362
Radek Krejci327de162019-06-14 12:52:07 +02004363 lysc_update_path(ctx, NULL, "{augment}");
4364 lysc_update_path(ctx, NULL, aug_p->nodeid);
4365
Radek Krejci7af64242019-02-18 13:07:53 +01004366 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004367 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004368 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004369 if (ret != LY_SUCCESS) {
4370 if (ret == LY_EDENIED) {
4371 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4372 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4373 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4374 }
4375 return LY_EVALID;
4376 }
4377
4378 /* check for mandatory nodes
4379 * - new cases augmenting some choice can have mandatory nodes
4380 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4381 */
Radek Krejci733988a2019-02-15 15:12:44 +01004382 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004383 allow_mandatory = 1;
4384 }
4385
4386 when_shared = NULL;
4387 LY_LIST_FOR(aug_p->child, node_p) {
4388 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4389 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004390 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004391 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4392 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004393 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004394 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4395 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004396 return LY_EVALID;
4397 }
4398
4399 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004400 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004401 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004402 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004403 } else {
4404 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004405 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004406 }
4407 }
Radek Krejciec4da802019-05-02 13:02:41 +02004408 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004409
Radek Krejcife13da42019-02-15 14:51:01 +01004410 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4411 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004412 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004413 /* 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 +01004414 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 +01004415 } else if (target->nodetype == LYS_CHOICE) {
4416 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4417 node = ((struct lysc_node_choice*)target)->cases->prev;
4418 } else {
4419 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004420 node = (struct lysc_node*)lysc_node_children(target, flags);
4421 if (!node) {
4422 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4423 break;
4424 }
4425 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004426 }
4427
Radek Krejci733988a2019-02-15 15:12:44 +01004428 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004429 node->flags &= ~LYS_MAND_TRUE;
4430 lys_compile_mandatory_parents(target, 0);
4431 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004432 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004433 return LY_EVALID;
4434 }
4435
4436 /* pass augment's when to all the children */
4437 if (aug_p->when) {
4438 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4439 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004440 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004441 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004442
4443 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4444 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004445 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004446 }
4447
Radek Krejci3641f562019-02-13 15:38:40 +01004448 when_shared = *when;
4449 } else {
4450 ++when_shared->refcount;
4451 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004452
4453 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4454 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004455 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004456 }
Radek Krejci3641f562019-02-13 15:38:40 +01004457 }
4458 }
4459 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004460
Radek Krejciec4da802019-05-02 13:02:41 +02004461 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004462 switch (target->nodetype) {
4463 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004464 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004465 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004466 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004467 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004468 break;
4469 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004470 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004471 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004472 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004473 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004474 break;
4475 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004476 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004477 if (aug_p->actions) {
4478 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004479 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4480 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004481 return LY_EVALID;
4482 }
4483 if (aug_p->notifs) {
4484 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004485 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004486 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004487 return LY_EVALID;
4488 }
4489 }
Radek Krejci3641f562019-02-13 15:38:40 +01004490
Michal Vaskoa3af5982020-06-29 11:51:37 +02004491 /* add this module into the target module augmented_by */
4492 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4493 *aug_mod = ctx->mod;
4494
Radek Krejci327de162019-06-14 12:52:07 +02004495 lysc_update_path(ctx, NULL, NULL);
4496 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004497
Radek Krejci3641f562019-02-13 15:38:40 +01004498error:
Radek Krejciec4da802019-05-02 13:02:41 +02004499 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004500 return ret;
4501}
4502
4503/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004504 * @brief Apply refined or deviated mandatory flag to the target node.
4505 *
4506 * @param[in] ctx Compile context.
4507 * @param[in] node Target node where the mandatory property is supposed to be changed.
4508 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004509 * @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 +01004510 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4511 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4512 * 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 +01004513 * @return LY_ERR value.
4514 */
4515static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004516lys_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 +01004517{
4518 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4519 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004520 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4521 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004522 return LY_EVALID;
4523 }
4524
4525 if (mandatory_flag & LYS_MAND_TRUE) {
4526 /* check if node has default value */
4527 if (node->nodetype & LYS_LEAF) {
4528 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004529 if (refine_flag) {
4530 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004531 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004532 return LY_EVALID;
4533 }
Radek Krejcia1911222019-07-22 17:24:50 +02004534 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004535 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004536 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004537
4538 /* update the list of incomplete default values if needed */
4539 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4540
Radek Krejcia1911222019-07-22 17:24:50 +02004541 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4542 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4543 free(leaf->dflt);
4544 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004545 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004546 }
4547 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004548 if (refine_flag) {
4549 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004550 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004551 return LY_EVALID;
4552 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004553 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004554 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004555 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 +01004556 return LY_EVALID;
4557 }
4558
4559 node->flags &= ~LYS_MAND_FALSE;
4560 node->flags |= LYS_MAND_TRUE;
4561 lys_compile_mandatory_parents(node->parent, 1);
4562 } else {
4563 /* make mandatory false */
4564 node->flags &= ~LYS_MAND_TRUE;
4565 node->flags |= LYS_MAND_FALSE;
4566 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004567 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 +01004568 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004569 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004570 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004571 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4572 leaf->dflt->realtype = leaf->type->dflt->realtype;
4573 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4574 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004575 }
4576 }
4577 return LY_SUCCESS;
4578}
4579
4580/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004581 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4582 * If present, also apply uses's modificators.
4583 *
4584 * @param[in] ctx Compile context
4585 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004586 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4587 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4588 * the compile context.
4589 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4590 */
4591static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004592lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004593{
4594 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004595 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004596 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004597 struct lysc_node_container context_node_fake =
4598 {.nodetype = LYS_CONTAINER,
4599 .module = ctx->mod,
4600 .flags = parent ? parent->flags : 0,
4601 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004602 .prev = (struct lysc_node*)&context_node_fake,
4603 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004604 struct lysp_grp *grp = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004605 LY_ARRAY_SIZE_TYPE u, v;
4606 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004607 int found;
4608 const char *id, *name, *prefix;
4609 size_t prefix_len, name_len;
4610 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004611 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004612 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004613 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004614 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004615 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004616 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004617 struct lysp_augment **augments = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004618 LY_ARRAY_SIZE_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004619 struct lysc_notif **notifs = NULL;
4620 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004621
4622 /* search for the grouping definition */
4623 found = 0;
4624 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004625 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004626 if (prefix) {
4627 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4628 if (!mod) {
4629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004630 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004631 return LY_EVALID;
4632 }
4633 } else {
4634 mod = ctx->mod_def;
4635 }
4636 if (mod == ctx->mod_def) {
4637 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004638 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004639 LY_ARRAY_FOR(grp, u) {
4640 if (!strcmp(grp[u].name, name)) {
4641 grp = &grp[u];
4642 found = 1;
4643 break;
4644 }
4645 }
4646 }
4647 }
4648 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004649 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004650 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004651 if (grp) {
4652 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4653 if (!strcmp(grp[u].name, name)) {
4654 grp = &grp[u];
4655 found = 1;
4656 }
4657 }
4658 }
4659 if (!found && mod->parsed->includes) {
4660 /* ... and all the submodules */
4661 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4662 grp = mod->parsed->includes[u].submodule->groupings;
4663 if (grp) {
4664 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4665 if (!strcmp(grp[v].name, name)) {
4666 grp = &grp[v];
4667 found = 1;
4668 }
4669 }
4670 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004671 }
4672 }
4673 }
4674 if (!found) {
4675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4676 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4677 return LY_EVALID;
4678 }
4679
4680 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4681 grp_stack_count = ctx->groupings.count;
4682 ly_set_add(&ctx->groupings, (void*)grp, 0);
4683 if (grp_stack_count == ctx->groupings.count) {
4684 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4685 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4686 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4687 return LY_EVALID;
4688 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004689 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4690 /* remember that the grouping is instantiated to avoid its standalone validation */
4691 grp->flags |= LYS_USED_GRP;
4692 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004693
4694 /* switch context's mod_def */
4695 mod_old = ctx->mod_def;
4696 ctx->mod_def = mod;
4697
4698 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004699 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 +01004700
Radek Krejcifc11bd72019-04-11 16:00:05 +02004701 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004702 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004703 /* 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 +02004704 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 +01004705
4706 /* some preparation for applying refines */
4707 if (grp->data == node_p) {
4708 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004709 if (parent) {
4710 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4711 } else if (ctx->mod->compiled->data) {
4712 child = ctx->mod->compiled->data;
4713 } else {
4714 child = NULL;
4715 }
4716 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004717 }
4718 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004719 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004720 LY_LIST_FOR(context_node_fake.child, child) {
4721 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004722
Radek Krejcifc11bd72019-04-11 16:00:05 +02004723 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004724 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004725 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004726 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004727 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4728
4729 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4730 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004731 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004732 }
4733
Radek Krejci00b874b2019-02-12 10:54:50 +01004734 when_shared = *when;
4735 } else {
4736 ++when_shared->refcount;
4737 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004738
4739 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4740 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004741 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004742 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004743 }
4744 }
Radek Krejci01342af2019-01-03 15:18:08 +01004745 }
4746 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004747 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004748 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004749 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004750 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 +01004751 }
4752
Radek Krejcifc11bd72019-04-11 16:00:05 +02004753 /* compile actions */
4754 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4755 if (actions) {
4756 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004757 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004758 if (*actions && (uses_p->augments || uses_p->refines)) {
4759 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4760 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4761 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4762 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4763 }
4764 }
4765
4766 /* compile notifications */
4767 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4768 if (notifs) {
4769 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004770 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004771 if (*notifs && (uses_p->augments || uses_p->refines)) {
4772 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4773 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4774 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4775 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4776 }
4777 }
4778
4779
Radek Krejci3641f562019-02-13 15:38:40 +01004780 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004781 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004782 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004783 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004784 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004785
Radek Krejcif0089082019-01-07 16:42:01 +01004786 /* reload previous context's mod_def */
4787 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004788 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004789
Radek Krejci76b3e962018-12-14 17:01:25 +01004790 /* apply refine */
4791 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004792 lysc_update_path(ctx, NULL, rfn->nodeid);
4793
Radek Krejci7af64242019-02-18 13:07:53 +01004794 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 +01004795 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004796 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004797 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004798
4799 /* default value */
4800 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004801 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004802 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004803 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_SIZE_TYPE" default values.",
Radek Krejci327de162019-06-14 12:52:07 +02004804 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004805 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004806 }
4807 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4808 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004809 "Invalid refine of default - %s cannot hold default value(s).",
4810 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004811 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004812 }
4813 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004814 struct ly_err_item *err = NULL;
4815 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4816 if (leaf->dflt) {
4817 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004818 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004819 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4820 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4821 } else {
4822 /* prepare a new one */
4823 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4824 leaf->dflt->realtype = leaf->type;
4825 }
4826 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004827 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004828 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4829 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004830 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004831 leaf->dflt->realtype->refcount++;
4832 if (err) {
4833 ly_err_print(err);
4834 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4835 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4836 ly_err_free(err);
4837 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004838 if (rc == LY_EINCOMPLETE) {
4839 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004840 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004841
4842 /* but in general result is so far ok */
4843 rc = LY_SUCCESS;
4844 }
Radek Krejcia1911222019-07-22 17:24:50 +02004845 LY_CHECK_GOTO(rc, cleanup);
4846 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004847 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004848 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4849
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004850 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004851 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4852 "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 +02004853 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004854 }
Radek Krejcia1911222019-07-22 17:24:50 +02004855
4856 /* remove previous set of default values */
4857 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004858 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004859 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4860 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4861 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004862 }
Radek Krejcia1911222019-07-22 17:24:50 +02004863 LY_ARRAY_FREE(llist->dflts);
4864 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004865 LY_ARRAY_FREE(llist->dflts_mods);
4866 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004867
4868 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004869 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02004870 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004871 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004872 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004873 LY_ARRAY_INCREMENT(llist->dflts_mods);
4874 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004875 LY_ARRAY_INCREMENT(llist->dflts);
4876 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4877 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004878 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004879 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004880 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004881 llist->dflts[u]->realtype->refcount++;
4882 if (err) {
4883 ly_err_print(err);
4884 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4885 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4886 ly_err_free(err);
4887 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004888 if (rc == LY_EINCOMPLETE) {
4889 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004890 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004891
4892 /* but in general result is so far ok */
4893 rc = LY_SUCCESS;
4894 }
4895 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004896 }
Radek Krejcia1911222019-07-22 17:24:50 +02004897 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004898 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004899 if (((struct lysc_node_choice*)node)->dflt) {
4900 /* unset LYS_SET_DFLT from the current default case */
4901 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4902 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004903 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 +01004904 }
4905 }
4906
Radek Krejci12fb9142019-01-08 09:45:30 +01004907 /* description */
4908 if (rfn->dsc) {
4909 FREE_STRING(ctx->ctx, node->dsc);
4910 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4911 }
4912
4913 /* reference */
4914 if (rfn->ref) {
4915 FREE_STRING(ctx->ctx, node->ref);
4916 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4917 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004918
4919 /* config */
4920 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004921 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004922 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004923 } else {
4924 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004925 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004926 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004927 }
4928
4929 /* mandatory */
4930 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004931 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004932 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004933
4934 /* presence */
4935 if (rfn->presence) {
4936 if (node->nodetype != LYS_CONTAINER) {
4937 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004938 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4939 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004940 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004941 }
4942 node->flags |= LYS_PRESENCE;
4943 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004944
4945 /* must */
4946 if (rfn->musts) {
4947 switch (node->nodetype) {
4948 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004949 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 +01004950 break;
4951 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004952 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 +01004953 break;
4954 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004955 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 +01004956 break;
4957 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004958 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 +01004959 break;
4960 case LYS_ANYXML:
4961 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004962 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 +01004963 break;
4964 default:
4965 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004966 "Invalid refine of must statement - %s cannot hold any must statement.",
4967 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004968 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004969 }
Michal Vasko004d3152020-06-11 19:59:22 +02004970 ly_set_add(&ctx->xpath, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01004971 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004972
4973 /* min/max-elements */
4974 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4975 switch (node->nodetype) {
4976 case LYS_LEAFLIST:
4977 if (rfn->flags & LYS_SET_MAX) {
4978 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4979 }
4980 if (rfn->flags & LYS_SET_MIN) {
4981 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004982 if (rfn->min) {
4983 node->flags |= LYS_MAND_TRUE;
4984 lys_compile_mandatory_parents(node->parent, 1);
4985 } else {
4986 node->flags &= ~LYS_MAND_TRUE;
4987 lys_compile_mandatory_parents(node->parent, 0);
4988 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004989 }
4990 break;
4991 case LYS_LIST:
4992 if (rfn->flags & LYS_SET_MAX) {
4993 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4994 }
4995 if (rfn->flags & LYS_SET_MIN) {
4996 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004997 if (rfn->min) {
4998 node->flags |= LYS_MAND_TRUE;
4999 lys_compile_mandatory_parents(node->parent, 1);
5000 } else {
5001 node->flags &= ~LYS_MAND_TRUE;
5002 lys_compile_mandatory_parents(node->parent, 0);
5003 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005004 }
5005 break;
5006 default:
5007 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005008 "Invalid refine of %s statement - %s cannot hold this statement.",
5009 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005010 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005011 }
5012 }
Radek Krejcif0089082019-01-07 16:42:01 +01005013
5014 /* if-feature */
5015 if (rfn->iffeatures) {
5016 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005017 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005018 }
Radek Krejci327de162019-06-14 12:52:07 +02005019
5020 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005021 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005022
Radek Krejcif2271f12019-01-07 16:42:23 +01005023 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005024 for (uint32_t i = 0; i < refined.count; ++i) {
5025 node = (struct lysc_node*)refined.objs[i];
5026 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02005027 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005028
5029 /* check possible conflict with default value (default added, mandatory left true) */
5030 if ((node->flags & LYS_MAND_TRUE) &&
5031 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5032 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5033 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005034 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005035 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005036 }
5037
5038 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5039 if (node->nodetype == LYS_LIST) {
5040 min = ((struct lysc_node_list*)node)->min;
5041 max = ((struct lysc_node_list*)node)->max;
5042 } else {
5043 min = ((struct lysc_node_leaflist*)node)->min;
5044 max = ((struct lysc_node_leaflist*)node)->max;
5045 }
5046 if (min > max) {
5047 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005048 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5049 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005050 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005051 }
5052 }
5053 }
5054
Radek Krejci327de162019-06-14 12:52:07 +02005055 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005056 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005057
5058cleanup:
5059 /* fix connection of the children nodes from fake context node back into the parent */
5060 if (context_node_fake.child) {
5061 context_node_fake.child->prev = child;
5062 }
5063 LY_LIST_FOR(context_node_fake.child, child) {
5064 child->parent = parent;
5065 }
5066
5067 if (uses_p->augments || uses_p->refines) {
5068 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005069 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005070 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5071 LY_ARRAY_FREE(context_node_fake.actions);
5072 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005073 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005074 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5075 LY_ARRAY_FREE(context_node_fake.notifs);
5076 }
5077 }
5078
Radek Krejcie86bf772018-12-14 11:39:53 +01005079 /* reload previous context's mod_def */
5080 ctx->mod_def = mod_old;
5081 /* remove the grouping from the stack for circular groupings dependency check */
5082 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5083 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005084 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005085 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005086
5087 return ret;
5088}
5089
Radek Krejci327de162019-06-14 12:52:07 +02005090static int
5091lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5092{
5093 struct lysp_node *iter;
5094 int len = 0;
5095
5096 *path = NULL;
5097 for (iter = node; iter && len >= 0; iter = iter->parent) {
5098 char *s = *path;
5099 char *id;
5100
5101 switch (iter->nodetype) {
5102 case LYS_USES:
5103 asprintf(&id, "{uses='%s'}", iter->name);
5104 break;
5105 case LYS_GROUPING:
5106 asprintf(&id, "{grouping='%s'}", iter->name);
5107 break;
5108 case LYS_AUGMENT:
5109 asprintf(&id, "{augment='%s'}", iter->name);
5110 break;
5111 default:
5112 id = strdup(iter->name);
5113 break;
5114 }
5115
5116 if (!iter->parent) {
5117 /* print prefix */
5118 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5119 } else {
5120 /* prefix is the same as in parent */
5121 len = asprintf(path, "/%s%s", id, s ? s : "");
5122 }
5123 free(s);
5124 free(id);
5125 }
5126
5127 if (len < 0) {
5128 free(*path);
5129 *path = NULL;
5130 } else if (len == 0) {
5131 *path = strdup("/");
5132 len = 1;
5133 }
5134 return len;
5135}
5136
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005137/**
5138 * @brief Validate groupings that were defined but not directly used in the schema itself.
5139 *
5140 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5141 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5142 */
5143static LY_ERR
5144lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5145{
5146 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005147 char *path;
5148 int len;
5149
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005150 struct lysp_node_uses fake_uses = {
5151 .parent = node_p,
5152 .nodetype = LYS_USES,
5153 .flags = 0, .next = NULL,
5154 .name = grp->name,
5155 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5156 .refines = NULL, .augments = NULL
5157 };
5158 struct lysc_node_container fake_container = {
5159 .nodetype = LYS_CONTAINER,
5160 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5161 .module = ctx->mod,
5162 .sp = NULL, .parent = NULL, .next = NULL,
5163 .prev = (struct lysc_node*)&fake_container,
5164 .name = "fake",
5165 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5166 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5167 };
5168
5169 if (grp->parent) {
5170 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5171 }
Radek Krejci327de162019-06-14 12:52:07 +02005172
5173 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5174 if (len < 0) {
5175 LOGMEM(ctx->ctx);
5176 return LY_EMEM;
5177 }
5178 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5179 ctx->path_len = (uint16_t)len;
5180 free(path);
5181
5182 lysc_update_path(ctx, NULL, "{grouping}");
5183 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005184 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005185 lysc_update_path(ctx, NULL, NULL);
5186 lysc_update_path(ctx, NULL, NULL);
5187
5188 ctx->path_len = 1;
5189 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005190
5191 /* cleanup */
5192 lysc_node_container_free(ctx->ctx, &fake_container);
5193
5194 return ret;
5195}
Radek Krejcife909632019-02-12 15:34:42 +01005196
Radek Krejcie86bf772018-12-14 11:39:53 +01005197/**
Radek Krejcia3045382018-11-22 14:30:31 +01005198 * @brief Compile parsed schema node information.
5199 * @param[in] ctx Compile context
5200 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005201 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5202 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5203 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005204 * @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).
5205 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005206 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5207 */
Radek Krejci19a96102018-11-15 13:38:09 +01005208static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005209lys_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 +01005210{
Radek Krejci1c54f462020-05-12 17:25:34 +02005211 LY_ERR ret = LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01005212 struct lysc_node *node;
5213 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005214 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005215 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005216 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005217
Radek Krejci327de162019-06-14 12:52:07 +02005218 if (node_p->nodetype != LYS_USES) {
5219 lysc_update_path(ctx, parent, node_p->name);
5220 } else {
5221 lysc_update_path(ctx, NULL, "{uses}");
5222 lysc_update_path(ctx, NULL, node_p->name);
5223 }
5224
Radek Krejci19a96102018-11-15 13:38:09 +01005225 switch (node_p->nodetype) {
5226 case LYS_CONTAINER:
5227 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5228 node_compile_spec = lys_compile_node_container;
5229 break;
5230 case LYS_LEAF:
5231 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5232 node_compile_spec = lys_compile_node_leaf;
5233 break;
5234 case LYS_LIST:
5235 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005236 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005237 break;
5238 case LYS_LEAFLIST:
5239 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005240 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005241 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005242 case LYS_CHOICE:
5243 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005244 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005245 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005246 case LYS_ANYXML:
5247 case LYS_ANYDATA:
5248 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005249 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005250 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005251 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005252 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5253 lysc_update_path(ctx, NULL, NULL);
5254 lysc_update_path(ctx, NULL, NULL);
5255 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005256 default:
5257 LOGINT(ctx->ctx);
5258 return LY_EINT;
5259 }
5260 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5261 node->nodetype = node_p->nodetype;
5262 node->module = ctx->mod;
5263 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005264 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005265
5266 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005267 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005268 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005269 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005270 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5271 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005272 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005273 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005274 node->flags |= LYS_CONFIG_R;
5275 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005276 /* config not explicitely set, inherit it from parent */
5277 if (parent) {
5278 node->flags |= parent->flags & LYS_CONFIG_MASK;
5279 } else {
5280 /* default is config true */
5281 node->flags |= LYS_CONFIG_W;
5282 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005283 } else {
5284 /* config set explicitely */
5285 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005286 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005287 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5289 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005290 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005291 goto error;
5292 }
Radek Krejci19a96102018-11-15 13:38:09 +01005293
Radek Krejcia6d57732018-11-29 13:40:37 +01005294 /* *list ordering */
5295 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5296 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005297 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005298 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5299 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005300 node->flags &= ~LYS_ORDBY_MASK;
5301 node->flags |= LYS_ORDBY_SYSTEM;
5302 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5303 /* default ordering is system */
5304 node->flags |= LYS_ORDBY_SYSTEM;
5305 }
5306 }
5307
Radek Krejci19a96102018-11-15 13:38:09 +01005308 /* status - it is not inherited by specification, but it does not make sense to have
5309 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005310 if (!parent || parent->nodetype != LYS_CHOICE) {
5311 /* in case of choice/case's children, postpone the check to the moment we know if
5312 * 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 +02005313 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 +01005314 }
5315
Radek Krejciec4da802019-05-02 13:02:41 +02005316 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005317 node->sp = node_p;
5318 }
5319 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005320 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5321 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005322 if (node_p->when) {
5323 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005324 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005325
5326 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5327 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02005328 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01005329 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005330 }
Radek Krejciec4da802019-05-02 13:02:41 +02005331 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005332
5333 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005334 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005335
Radek Krejci0935f412019-08-20 16:15:18 +02005336 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5337
Radek Krejcife909632019-02-12 15:34:42 +01005338 /* inherit LYS_MAND_TRUE in parent containers */
5339 if (node->flags & LYS_MAND_TRUE) {
5340 lys_compile_mandatory_parents(parent, 1);
5341 }
5342
Radek Krejci327de162019-06-14 12:52:07 +02005343 lysc_update_path(ctx, NULL, NULL);
5344
Radek Krejci19a96102018-11-15 13:38:09 +01005345 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005346 if (parent) {
5347 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005348 if (node_p->parent->nodetype == LYS_CASE) {
5349 lysc_update_path(ctx, parent, node_p->parent->name);
5350 } else {
5351 lysc_update_path(ctx, parent, node->name);
5352 }
Radek Krejciec4da802019-05-02 13:02:41 +02005353 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005354 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005355 if (uses_status) {
5356
5357 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005358 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005359 * it directly gets the same status flags as the choice;
5360 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005361 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005362 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005363 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005364 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005365 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005366 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005367 }
Radek Krejciec4da802019-05-02 13:02:41 +02005368 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 +02005369
5370 if (parent->nodetype == LYS_CHOICE) {
5371 lysc_update_path(ctx, NULL, NULL);
5372 }
Radek Krejci19a96102018-11-15 13:38:09 +01005373 } else {
5374 /* top-level element */
5375 if (!ctx->mod->compiled->data) {
5376 ctx->mod->compiled->data = node;
5377 } else {
5378 /* insert at the end of the module's top-level nodes list */
5379 ctx->mod->compiled->data->prev->next = node;
5380 node->prev = ctx->mod->compiled->data->prev;
5381 ctx->mod->compiled->data->prev = node;
5382 }
Radek Krejci327de162019-06-14 12:52:07 +02005383 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005384 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5385 ctx->mod->compiled->notifs, node->name, node)) {
5386 return LY_EVALID;
5387 }
Radek Krejci19a96102018-11-15 13:38:09 +01005388 }
Radek Krejci327de162019-06-14 12:52:07 +02005389 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005390
5391 return LY_SUCCESS;
5392
5393error:
5394 lysc_node_free(ctx->ctx, node);
5395 return ret;
5396}
5397
Radek Krejciccd20f12019-02-15 14:12:27 +01005398static void
5399lysc_disconnect(struct lysc_node *node)
5400{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005401 struct lysc_node *parent, *child, *prev = NULL, *next;
5402 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005403 int remove_cs = 0;
5404
5405 parent = node->parent;
5406
5407 /* parent's first child */
5408 if (!parent) {
5409 return;
5410 }
5411 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005412 cs = (struct lysc_node_case*)node;
5413 } else if (parent->nodetype == LYS_CASE) {
5414 /* disconnecting some node in a case */
5415 cs = (struct lysc_node_case*)parent;
5416 parent = cs->parent;
5417 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5418 if (child == node) {
5419 if (cs->child == child) {
5420 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5421 /* case with a single child -> remove also the case */
5422 child->parent = NULL;
5423 remove_cs = 1;
5424 } else {
5425 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005426 }
5427 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005428 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005429 }
5430 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005431 if (!remove_cs) {
5432 cs = NULL;
5433 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005434 } else if (lysc_node_children(parent, node->flags) == node) {
5435 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005436 }
5437
5438 if (cs) {
5439 if (remove_cs) {
5440 /* cs has only one child which is being also removed */
5441 lysc_disconnect((struct lysc_node*)cs);
5442 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5443 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005444 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5445 /* default case removed */
5446 ((struct lysc_node_choice*)parent)->dflt = NULL;
5447 }
5448 if (((struct lysc_node_choice*)parent)->cases == cs) {
5449 /* first case removed */
5450 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5451 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005452 if (cs->child) {
5453 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5454 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5455 prev = cs->child->prev;
5456 } /* else all the children are under a single case */
5457 LY_LIST_FOR_SAFE(cs->child, next, child) {
5458 if (child->parent != (struct lysc_node*)cs) {
5459 break;
5460 }
5461 lysc_node_free(node->module->ctx, child);
5462 }
5463 if (prev) {
5464 if (prev->next) {
5465 prev->next = child;
5466 }
5467 if (child) {
5468 child->prev = prev;
5469 } else {
5470 /* link from the first child under the cases */
5471 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5472 }
5473 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005474 }
5475 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005476 }
5477
5478 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005479 if (node->prev->next) {
5480 node->prev->next = node->next;
5481 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005482 if (node->next) {
5483 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005484 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005485 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005486 if (child) {
5487 child->prev = node->prev;
5488 }
5489 } else if (((struct lysc_node_choice*)parent)->cases) {
5490 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005491 }
5492}
5493
5494LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005495lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005496{
Radek Krejcia1911222019-07-22 17:24:50 +02005497 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005498 struct ly_set devs_p = {0};
5499 struct ly_set targets = {0};
5500 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005501 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005502 struct lysc_action *rpcs;
5503 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005504 struct lysp_deviation *dev;
5505 struct lysp_deviate *d, **dp_new;
5506 struct lysp_deviate_add *d_add;
5507 struct lysp_deviate_del *d_del;
5508 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005509 LY_ARRAY_SIZE_TYPE u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005510 struct lysc_deviation {
5511 const char *nodeid;
5512 struct lysc_node *target; /* target node of the deviation */
5513 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005514 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005515 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5516 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005517 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005518 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005519 const char *prefix, *name, *nodeid, *dflt;
Michal Vasko57c10cd2020-05-27 15:57:11 +02005520 struct lys_module *mod, **dev_mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005521 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005522 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005523
5524 /* get all deviations from the module and all its submodules ... */
5525 LY_ARRAY_FOR(mod_p->deviations, u) {
5526 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5527 }
5528 LY_ARRAY_FOR(mod_p->includes, v) {
5529 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5530 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5531 }
5532 }
5533 if (!devs_p.count) {
5534 /* nothing to do */
5535 return LY_SUCCESS;
5536 }
5537
Radek Krejci327de162019-06-14 12:52:07 +02005538 lysc_update_path(ctx, NULL, "{deviation}");
5539
Radek Krejciccd20f12019-02-15 14:12:27 +01005540 /* ... and group them by the target node */
5541 devs = calloc(devs_p.count, sizeof *devs);
5542 for (u = 0; u < devs_p.count; ++u) {
5543 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005544 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005545
5546 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005547 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5548 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01005549 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005550 /* move the target pointer to input/output to make them different from the action and
5551 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5552 * back to the RPC/action node due to a better compatibility and decision code in this function.
5553 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5554 if (flags & LYSC_OPT_RPC_INPUT) {
5555 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5556 flags |= LYSC_OPT_INTERNAL;
5557 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5558 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5559 flags |= LYSC_OPT_INTERNAL;
5560 }
5561 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005562 /* insert into the set of targets with duplicity detection */
5563 i = ly_set_add(&targets, target, 0);
5564 if (!devs[i]) {
5565 /* new record */
5566 devs[i] = calloc(1, sizeof **devs);
5567 devs[i]->target = target;
5568 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005569 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005570 }
5571 /* add deviates into the deviation's list of deviates */
5572 for (d = dev->deviates; d; d = d->next) {
5573 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5574 *dp_new = d;
5575 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5576 devs[i]->not_supported = 1;
5577 }
5578 }
Radek Krejci327de162019-06-14 12:52:07 +02005579
5580 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005581 }
5582
5583 /* MACROS for deviates checking */
5584#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5585 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005586 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 +01005587 goto cleanup; \
5588 }
5589
5590#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5591 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005592 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 +02005593 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005594 goto cleanup; \
5595 }
5596
Radek Krejcia1911222019-07-22 17:24:50 +02005597
Radek Krejciccd20f12019-02-15 14:12:27 +01005598#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005599 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5600 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5601 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5602 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5603 goto cleanup; \
5604 }
5605
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005606#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005607 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005608 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005609 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5610 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005611 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005612 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5613 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005614 goto cleanup; \
5615 }
5616
Radek Krejci551b12c2019-02-19 16:11:21 +01005617#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5618 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5619 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005620 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5621 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005622 goto cleanup; \
5623 }
5624
Radek Krejciccd20f12019-02-15 14:12:27 +01005625#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5626 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005627 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005628 goto cleanup; \
5629 }
5630
Radek Krejci551b12c2019-02-19 16:11:21 +01005631#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5632 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5633 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005634 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005635 goto cleanup; \
5636 }
5637
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005638#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5639 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5640 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5641 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5642 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 +01005643 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005644 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005645 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005646 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5647 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005648 goto cleanup; \
5649 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005650 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5651 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5652 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5653 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5654 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005655 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005656 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5657 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5658 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005659 }
5660
5661 /* apply deviations */
5662 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005663 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5664 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5665 struct ly_err_item *err = NULL;
5666
5667 dflt = NULL;
5668 changed_type = 0;
5669
Radek Krejci327de162019-06-14 12:52:07 +02005670 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5671
Radek Krejcif538ce52019-03-05 10:46:14 +01005672 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5673 /* fix the target pointer in case of RPC's/action's input/output */
5674 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5675 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5676 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5677 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5678 }
5679 }
5680
Radek Krejciccd20f12019-02-15 14:12:27 +01005681 /* not-supported */
5682 if (devs[u]->not_supported) {
5683 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005684 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 +01005685 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5686 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005687
5688#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5689 if (devs[u]->target->parent) { \
5690 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5691 } else { \
5692 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5693 } \
5694 LY_ARRAY_FOR(ARRAY, x) { \
5695 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5696 } \
5697 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5698 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5699 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5700 LY_ARRAY_DECREMENT(ARRAY); \
5701 }
5702
Michal Vasko1bf09392020-03-27 12:38:10 +01005703 if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005704 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5705 /* remove RPC's/action's input */
5706 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5707 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005708 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5709 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005710 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5711 /* remove RPC's/action's output */
5712 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5713 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005714 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5715 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005716 } else {
5717 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005718 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005719 }
5720 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005721 /* remove Notification */
5722 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005723 } else {
5724 /* remove the target node */
5725 lysc_disconnect(devs[u]->target);
5726 lysc_node_free(ctx->ctx, devs[u]->target);
5727 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005728
Radek Krejci474f9b82019-07-24 11:36:37 +02005729 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5730 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005731 continue;
5732 }
5733
5734 /* list of deviates (not-supported is not present in the list) */
5735 LY_ARRAY_FOR(devs[u]->deviates, v) {
5736 d = devs[u]->deviates[v];
5737
5738 switch (d->mod) {
5739 case LYS_DEV_ADD:
5740 d_add = (struct lysp_deviate_add*)d;
5741 /* [units-stmt] */
5742 if (d_add->units) {
5743 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5744 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5745
5746 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5747 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5748 }
5749
5750 /* *must-stmt */
5751 if (d_add->musts) {
5752 switch (devs[u]->target->nodetype) {
5753 case LYS_CONTAINER:
5754 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005755 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5756 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005757 break;
5758 case LYS_LEAF:
5759 case LYS_LEAFLIST:
5760 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005761 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5762 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005763 break;
5764 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005765 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5766 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005767 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005768 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005769 case LYS_ACTION:
5770 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005771 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5772 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005773 break;
Michal Vaskocc048b22020-03-27 15:52:38 +01005774 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005775 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5776 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005777 break;
5778 }
5779 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005780 default:
5781 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005782 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005783 goto cleanup;
5784 }
Michal Vasko004d3152020-06-11 19:59:22 +02005785 ly_set_add(&ctx->xpath, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01005786 }
5787
5788 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005789 if (d_add->uniques) {
5790 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5791 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5792 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005793
5794 /* *default-stmt */
5795 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005796 switch (devs[u]->target->nodetype) {
5797 case LYS_LEAF:
5798 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005799 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 +02005800 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005801 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005802 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005803 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5804 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5805 } else {
5806 /* prepare new default value storage */
5807 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005808 }
Radek Krejcia1911222019-07-22 17:24:50 +02005809 dflt = d_add->dflts[0];
5810 /* parsing is done at the end after possible replace of the leaf's type */
5811
Radek Krejci551b12c2019-02-19 16:11:21 +01005812 /* mark the new default values as leaf's own */
5813 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005814 break;
5815 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005816 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005817 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005818 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005819 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005820 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5821 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5822 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005823 }
Radek Krejcia1911222019-07-22 17:24:50 +02005824 LY_ARRAY_FREE(llist->dflts);
5825 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005826 LY_ARRAY_FREE(llist->dflts_mods);
5827 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005828 }
5829 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005830 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005831 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5832 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01005833 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005834 LY_ARRAY_INCREMENT(llist->dflts_mods);
5835 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005836 LY_ARRAY_INCREMENT(llist->dflts);
5837 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5838 llist->dflts[x]->realtype = llist->type;
5839 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 +02005840 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005841 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005842 llist->dflts[x]->realtype->refcount++;
5843 if (err) {
5844 ly_err_print(err);
5845 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5846 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5847 d_add->dflts[x - y], err->msg);
5848 ly_err_free(err);
5849 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005850 if (rc == LY_EINCOMPLETE) {
5851 /* postpone default compilation when the tree is complete */
5852 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5853
5854 /* but in general result is so far ok */
5855 rc = LY_SUCCESS;
5856 }
Radek Krejcia1911222019-07-22 17:24:50 +02005857 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005858 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005859 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005860 devs[u]->target->flags |= LYS_SET_DFLT;
5861 break;
5862 case LYS_CHOICE:
5863 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5864 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5865 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5866 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005867 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 +01005868 goto cleanup;
5869 }
5870 break;
5871 default:
5872 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005873 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005874 goto cleanup;
5875 }
5876 }
5877
5878 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005879 if (d_add->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01005880 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005882 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5883 goto cleanup;
5884 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005885 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005886 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
Michal Vaskoa3881362020-01-21 15:57:35 +01005887 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005888 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005889 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005891 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5892 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005893 goto cleanup;
5894 }
Radek Krejci327de162019-06-14 12:52:07 +02005895 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005896 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005897
5898 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005899 if (d_add->flags & LYS_MAND_MASK) {
5900 if (devs[u]->target->flags & LYS_MAND_MASK) {
5901 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005902 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5903 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005904 goto cleanup;
5905 }
Radek Krejci327de162019-06-14 12:52:07 +02005906 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005907 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005908
5909 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005910 if (d_add->flags & LYS_SET_MIN) {
5911 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5912 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5913 /* change value */
5914 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5915 } else if (devs[u]->target->nodetype == LYS_LIST) {
5916 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5917 /* change value */
5918 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5919 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005920 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005921 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5922 goto cleanup;
5923 }
5924 if (d_add->min) {
5925 devs[u]->target->flags |= LYS_MAND_TRUE;
5926 }
5927 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005928
5929 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005930 if (d_add->flags & LYS_SET_MAX) {
5931 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5932 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5933 /* change value */
5934 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5935 } else if (devs[u]->target->nodetype == LYS_LIST) {
5936 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5937 /* change value */
5938 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5939 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005940 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005941 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5942 goto cleanup;
5943 }
5944 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005945
5946 break;
5947 case LYS_DEV_DELETE:
5948 d_del = (struct lysp_deviate_del*)d;
5949
5950 /* [units-stmt] */
5951 if (d_del->units) {
5952 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02005953 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
5954 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
5955 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5956 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
5957 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5958 goto cleanup;
5959 }
5960 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5961 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005962 }
5963
5964 /* *must-stmt */
5965 if (d_del->musts) {
5966 switch (devs[u]->target->nodetype) {
5967 case LYS_CONTAINER:
5968 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005969 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005970 break;
5971 case LYS_LEAF:
5972 case LYS_LEAFLIST:
5973 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005974 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005975 break;
5976 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005977 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005978 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005979 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005980 case LYS_ACTION:
5981 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5982 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5983 break;
5984 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5985 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5986 break;
5987 }
5988 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005989 default:
5990 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005991 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005992 goto cleanup;
5993 }
5994 }
5995
5996 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005997 if (d_del->uniques) {
5998 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5999 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6000 LY_ARRAY_FOR(d_del->uniques, x) {
6001 LY_ARRAY_FOR(list->uniques, z) {
6002 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6003 nodeid = strpbrk(name, " \t\n");
6004 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02006005 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006006 break;
6007 }
6008 while (isspace(*nodeid)) {
6009 ++nodeid;
6010 }
6011 } else {
6012 if (strcmp(name, list->uniques[z][y]->name)) {
6013 break;
6014 }
6015 }
6016 }
6017 if (!name) {
6018 /* complete match - remove the unique */
6019 LY_ARRAY_DECREMENT(list->uniques);
6020 LY_ARRAY_FREE(list->uniques[z]);
6021 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6022 --z;
6023 break;
6024 }
6025 }
6026 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6027 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006028 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6029 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006030 goto cleanup;
6031 }
6032 }
6033 if (!LY_ARRAY_SIZE(list->uniques)) {
6034 LY_ARRAY_FREE(list->uniques);
6035 list->uniques = NULL;
6036 }
6037 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006038
6039 /* *default-stmt */
6040 if (d_del->dflts) {
6041 switch (devs[u]->target->nodetype) {
6042 case LYS_LEAF:
6043 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6044 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6045 dflt, "deleting", "default", d_del->dflts[0]);
6046
Radek Krejcia1911222019-07-22 17:24:50 +02006047 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006048 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006049 if (strcmp(dflt, d_del->dflts[0])) {
6050 if (i) {
6051 free((char*)dflt);
6052 }
6053 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6054 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6055 d_del->dflts[0], dflt);
6056 goto cleanup;
6057 }
6058 if (i) {
6059 free((char*)dflt);
6060 }
6061 dflt = NULL;
6062
Radek Krejci474f9b82019-07-24 11:36:37 +02006063 /* update the list of incomplete default values if needed */
6064 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6065
Radek Krejcia1911222019-07-22 17:24:50 +02006066 /* remove the default specification */
6067 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6068 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6069 free(leaf->dflt);
6070 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006071 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006072 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006073 break;
6074 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006075 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6076 LY_ARRAY_FOR(d_del->dflts, x) {
6077 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006078 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 +02006079 if (!strcmp(dflt, d_del->dflts[x])) {
6080 if (i) {
6081 free((char*)dflt);
6082 }
6083 dflt = NULL;
6084 break;
6085 }
6086 if (i) {
6087 free((char*)dflt);
6088 }
6089 dflt = NULL;
6090 }
6091 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6092 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6093 "which does not match any of the target's property values.", d_del->dflts[x]);
6094 goto cleanup;
6095 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006096
6097 /* update the list of incomplete default values if needed */
6098 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6099
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006100 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006101 LY_ARRAY_DECREMENT(llist->dflts);
6102 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6103 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6104 free(llist->dflts[y]);
6105 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006106 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 +02006107 }
6108 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006109 LY_ARRAY_FREE(llist->dflts_mods);
6110 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006111 LY_ARRAY_FREE(llist->dflts);
6112 llist->dflts = NULL;
6113 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006114 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006115 break;
6116 case LYS_CHOICE:
6117 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6118 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6119 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006120 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006121 if (prefix) {
6122 /* use module prefixes from the deviation module to match the module of the default case */
6123 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
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 any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006127 goto cleanup;
6128 }
6129 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6130 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006131 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6132 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006133 goto cleanup;
6134 }
6135 }
6136 /* else {
6137 * strictly, the default prefix would point to the deviation module, but the value should actually
6138 * match the default string in the original module (usually unprefixed), so in this case we do not check
6139 * the module of the default case, just matching its name */
6140 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6141 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006142 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6143 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006144 goto cleanup;
6145 }
6146 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6147 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6148 break;
6149 default:
6150 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006151 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006152 goto cleanup;
6153 }
6154 }
6155
6156 break;
6157 case LYS_DEV_REPLACE:
6158 d_rpl = (struct lysp_deviate_rpl*)d;
6159
6160 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006161 if (d_rpl->type) {
6162 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6163 /* type is mandatory, so checking for its presence is not necessary */
6164 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006165
6166 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6167 /* the target has default from the previous type - remove it */
6168 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006169 /* update the list of incomplete default values if needed */
6170 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6171
Radek Krejcia1911222019-07-22 17:24:50 +02006172 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6173 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6174 free(leaf->dflt);
6175 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006176 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006177 } else { /* LYS_LEAFLIST */
6178 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006179 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006180 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6181 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6182 free(llist->dflts[x]);
6183 }
6184 LY_ARRAY_FREE(llist->dflts);
6185 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006186 LY_ARRAY_FREE(llist->dflts_mods);
6187 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006188 }
6189 }
6190 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006191 /* there is no default value, do not set changed_type after type compilation
6192 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006193 changed_type = -1;
6194 }
Radek Krejciec4da802019-05-02 13:02:41 +02006195 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 +02006196 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006197 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006198
6199 /* [units-stmt] */
6200 if (d_rpl->units) {
6201 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6202 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6203 units, "replacing", "units", d_rpl->units);
6204
6205 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6206 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6207 }
6208
6209 /* [default-stmt] */
6210 if (d_rpl->dflt) {
6211 switch (devs[u]->target->nodetype) {
6212 case LYS_LEAF:
6213 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6214 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006215 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006216 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006217 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6218 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6219 dflt = d_rpl->dflt;
6220 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006221 break;
6222 case LYS_CHOICE:
6223 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006224 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 +01006225 goto cleanup;
6226 }
6227 break;
6228 default:
6229 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006230 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006231 goto cleanup;
6232 }
6233 }
6234
6235 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006236 if (d_rpl->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01006237 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006238 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006239 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6240 goto cleanup;
6241 }
6242 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006244 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6245 goto cleanup;
6246 }
Radek Krejci327de162019-06-14 12:52:07 +02006247 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006248 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006249
6250 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006251 if (d_rpl->flags & LYS_MAND_MASK) {
6252 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006253 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006254 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6255 goto cleanup;
6256 }
Radek Krejci327de162019-06-14 12:52:07 +02006257 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006258 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006259
6260 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006261 if (d_rpl->flags & LYS_SET_MIN) {
6262 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6263 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6264 /* change value */
6265 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6266 } else if (devs[u]->target->nodetype == LYS_LIST) {
6267 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6268 /* change value */
6269 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6270 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006271 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006272 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6273 goto cleanup;
6274 }
6275 if (d_rpl->min) {
6276 devs[u]->target->flags |= LYS_MAND_TRUE;
6277 }
6278 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006279
6280 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006281 if (d_rpl->flags & LYS_SET_MAX) {
6282 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6283 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6284 /* change value */
6285 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6286 } else if (devs[u]->target->nodetype == LYS_LIST) {
6287 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6288 /* change value */
6289 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6290 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006291 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006292 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6293 goto cleanup;
6294 }
6295 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006296
6297 break;
6298 default:
6299 LOGINT(ctx->ctx);
6300 goto cleanup;
6301 }
6302 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006303
Radek Krejci33f72892019-02-21 10:36:58 +01006304 /* final check when all deviations of a single target node are applied */
6305
Radek Krejci551b12c2019-02-19 16:11:21 +01006306 /* check min-max compatibility */
6307 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6308 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6309 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6310 } else if (devs[u]->target->nodetype == LYS_LIST) {
6311 min = ((struct lysc_node_list*)devs[u]->target)->min;
6312 max = ((struct lysc_node_list*)devs[u]->target)->max;
6313 } else {
6314 min = max = 0;
6315 }
6316 if (min > max) {
6317 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 +02006318 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006319 goto cleanup;
6320 }
6321
Radek Krejcia1911222019-07-22 17:24:50 +02006322 if (dflt) {
6323 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006324 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006325 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006326 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6327 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006328 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006329 leaf->dflt->realtype->refcount++;
6330 if (err) {
6331 ly_err_print(err);
6332 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6333 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6334 ly_err_free(err);
6335 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006336 if (rc == LY_EINCOMPLETE) {
6337 /* postpone default compilation when the tree is complete */
6338 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6339
6340 /* but in general result is so far ok */
6341 rc = LY_SUCCESS;
6342 }
Radek Krejcia1911222019-07-22 17:24:50 +02006343 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006344 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006345 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6346 int dynamic;
6347 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006348 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006349
6350 /* update the list of incomplete default values if needed */
6351 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6352
6353 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006354 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6355 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6356 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006357 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6358 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006359 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006360 leaf->dflt->realtype->refcount++;
6361 if (err) {
6362 ly_err_print(err);
6363 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6364 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6365 ly_err_free(err);
6366 }
6367 if (dynamic) {
6368 free((void*)dflt);
6369 }
Radek Krejcia1911222019-07-22 17:24:50 +02006370 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006371 if (rc == LY_EINCOMPLETE) {
6372 /* postpone default compilation when the tree is complete */
6373 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6374
6375 /* but in general result is so far ok */
6376 rc = LY_SUCCESS;
6377 }
6378 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006379 } else { /* LYS_LEAFLIST */
6380 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006381 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 +02006382 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6383 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6384 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006385 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6386 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006387 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6388 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006389 llist->dflts[x]->realtype->refcount++;
6390 if (err) {
6391 ly_err_print(err);
6392 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6393 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6394 dflt, err->msg);
6395 ly_err_free(err);
6396 }
6397 if (dynamic) {
6398 free((void*)dflt);
6399 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006400 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006401 if (rc == LY_EINCOMPLETE) {
6402 /* postpone default compilation when the tree is complete */
6403 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6404
6405 /* but in general result is so far ok */
6406 rc = LY_SUCCESS;
6407 }
Radek Krejcia1911222019-07-22 17:24:50 +02006408 LY_CHECK_GOTO(rc, cleanup);
6409 }
6410 }
6411 }
6412
Radek Krejci551b12c2019-02-19 16:11:21 +01006413 /* check mandatory - default compatibility */
6414 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6415 && (devs[u]->target->flags & LYS_SET_DFLT)
6416 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6417 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006418 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006419 goto cleanup;
6420 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6421 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6422 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006423 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 +01006424 goto cleanup;
6425 }
6426 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6427 /* mandatory node under a default case */
6428 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006429 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6430 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006431 goto cleanup;
6432 }
Radek Krejci33f72892019-02-21 10:36:58 +01006433
Michal Vasko57c10cd2020-05-27 15:57:11 +02006434 /* add this module into the target module deviated_by */
6435 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup);
6436 *dev_mod = mod_p->mod;
6437
Radek Krejci327de162019-06-14 12:52:07 +02006438 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006439 }
6440
Radek Krejci327de162019-06-14 12:52:07 +02006441 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006442 ret = LY_SUCCESS;
6443
6444cleanup:
6445 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6446 LY_ARRAY_FREE(devs[u]->deviates);
6447 free(devs[u]);
6448 }
6449 free(devs);
6450 ly_set_erase(&targets, NULL);
6451 ly_set_erase(&devs_p, NULL);
6452
6453 return ret;
6454}
6455
Radek Krejcib56c7502019-02-13 14:19:54 +01006456/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006457 * @brief Compile the given YANG submodule into the main module.
6458 * @param[in] ctx Compile context
6459 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006460 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6461 */
6462LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006463lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006464{
6465 unsigned int u;
6466 LY_ERR ret = LY_SUCCESS;
6467 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006468 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006469 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006470 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006471
Radek Krejci0af46292019-01-11 16:02:31 +01006472 if (!mainmod->mod->off_features) {
6473 /* features are compiled directly into the compiled module structure,
6474 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6475 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006476 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6477 LY_CHECK_GOTO(ret, error);
6478 }
Radek Krejci0af46292019-01-11 16:02:31 +01006479
Radek Krejci327de162019-06-14 12:52:07 +02006480 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006481 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006482 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006483
Radek Krejci474f9b82019-07-24 11:36:37 +02006484 /* data nodes */
6485 LY_LIST_FOR(submod->data, node_p) {
6486 ret = lys_compile_node(ctx, node_p, NULL, 0);
6487 LY_CHECK_GOTO(ret, error);
6488 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006489
Radek Krejciec4da802019-05-02 13:02:41 +02006490 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6491 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006492
Radek Krejcid05cbd92018-12-05 14:26:40 +01006493error:
6494 return ret;
6495}
6496
Radek Krejci335332a2019-09-05 13:03:35 +02006497static void *
6498lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6499{
6500 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6501 if (substmts[u].stmt == stmt) {
6502 return substmts[u].storage;
6503 }
6504 }
6505 return NULL;
6506}
6507
6508LY_ERR
6509lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6510{
6511 LY_ERR ret = LY_EVALID, r;
6512 unsigned int u;
6513 struct lysp_stmt *stmt;
6514 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006515
6516 /* check for invalid substatements */
6517 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006518 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6519 continue;
6520 }
Radek Krejci335332a2019-09-05 13:03:35 +02006521 for (u = 0; substmts[u].stmt; ++u) {
6522 if (substmts[u].stmt == stmt->kw) {
6523 break;
6524 }
6525 }
6526 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006527 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6528 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006529 goto cleanup;
6530 }
Radek Krejci335332a2019-09-05 13:03:35 +02006531 }
6532
Radek Krejciad5963b2019-09-06 16:03:05 +02006533 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6534
Radek Krejci335332a2019-09-05 13:03:35 +02006535 /* keep order of the processing the same as the order in the defined substmts,
6536 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6537 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006538 int stmt_present = 0;
6539
Radek Krejci335332a2019-09-05 13:03:35 +02006540 for (stmt = ext->child; stmt; stmt = stmt->next) {
6541 if (substmts[u].stmt != stmt->kw) {
6542 continue;
6543 }
6544
Radek Krejciad5963b2019-09-06 16:03:05 +02006545 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006546 if (substmts[u].storage) {
6547 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006548 case LY_STMT_STATUS:
6549 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6550 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6551 break;
6552 case LY_STMT_UNITS: {
6553 const char **units;
6554
6555 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6556 /* single item */
6557 if (*((const char **)substmts[u].storage)) {
6558 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6559 goto cleanup;
6560 }
6561 units = (const char **)substmts[u].storage;
6562 } else {
6563 /* sized array */
6564 const char ***units_array = (const char ***)substmts[u].storage;
6565 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6566 }
6567 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6568 break;
6569 }
Radek Krejci335332a2019-09-05 13:03:35 +02006570 case LY_STMT_TYPE: {
6571 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6572 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6573
6574 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6575 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006576 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006577 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6578 goto cleanup;
6579 }
6580 compiled = substmts[u].storage;
6581 } else {
6582 /* sized array */
6583 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6584 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6585 compiled = (void*)type;
6586 }
6587
Radek Krejciad5963b2019-09-06 16:03:05 +02006588 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006589 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6590 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006591 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6592 lysp_type_free(ctx->ctx, parsed);
6593 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006594 break;
6595 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006596 case LY_STMT_IF_FEATURE: {
6597 struct lysc_iffeature *iff = NULL;
6598
6599 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6600 /* single item */
6601 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6602 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6603 goto cleanup;
6604 }
6605 iff = (struct lysc_iffeature*)substmts[u].storage;
6606 } else {
6607 /* sized array */
6608 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6609 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6610 }
6611 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6612 break;
6613 }
6614 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6615 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006616 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006617 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.",
6618 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006619 goto cleanup;
6620 }
6621 }
Radek Krejci335332a2019-09-05 13:03:35 +02006622 }
Radek Krejci335332a2019-09-05 13:03:35 +02006623
Radek Krejciad5963b2019-09-06 16:03:05 +02006624 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6626 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6627 goto cleanup;
6628 }
Radek Krejci335332a2019-09-05 13:03:35 +02006629 }
6630
6631 ret = LY_SUCCESS;
6632
6633cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006634 return ret;
6635}
6636
Michal Vasko175012e2019-11-06 15:49:14 +01006637/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006638 * @brief Check when for cyclic dependencies.
6639 * @param[in] set Set with all the referenced nodes.
6640 * @param[in] node Node whose "when" referenced nodes are in @p set.
6641 * @return LY_ERR value
6642 */
6643static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006644lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006645{
6646 struct lyxp_set tmp_set;
6647 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006648 uint32_t i, j;
6649 LY_ARRAY_SIZE_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006650 int idx;
6651 struct lysc_when *when;
6652 LY_ERR ret = LY_SUCCESS;
6653
6654 memset(&tmp_set, 0, sizeof tmp_set);
6655
6656 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006657 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006658 xp_scnode = &set->val.scnodes[i];
6659
Michal Vasko5c4e5892019-11-14 12:31:38 +01006660 if (xp_scnode->in_ctx != -1) {
6661 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006662 xp_scnode->in_ctx = 1;
6663 }
6664 }
6665
6666 for (i = 0; i < set->used; ++i) {
6667 xp_scnode = &set->val.scnodes[i];
6668 if (xp_scnode->in_ctx != 1) {
6669 /* already checked */
6670 continue;
6671 }
6672
Michal Vasko1bf09392020-03-27 12:38:10 +01006673 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006674 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006675 /* no when to check */
6676 xp_scnode->in_ctx = 0;
6677 continue;
6678 }
6679
6680 node = xp_scnode->scnode;
6681 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006682 LY_ARRAY_FOR(node->when, u) {
6683 when = node->when[u];
Michal Vasko52927e22020-03-16 17:26:14 +01006684 ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006685 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6686 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006687 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006688 goto cleanup;
6689 }
6690
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006691 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006692 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006693 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006694 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006695 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 +01006696 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006697 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 +01006698 ret = LY_EVALID;
6699 goto cleanup;
6700 }
6701
6702 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006703 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006704 } else {
6705 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006706 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006707 }
6708 }
6709
6710 /* merge this set into the global when set */
6711 lyxp_set_scnode_merge(set, &tmp_set);
6712 }
6713
6714 /* check when of non-data parents as well */
6715 node = node->parent;
6716 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6717
Michal Vasko251f56e2019-11-14 16:06:47 +01006718 /* this node when was checked (xp_scnode could have been reallocd) */
6719 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006720 }
6721
6722cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006723 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006724 return ret;
6725}
6726
6727/**
Michal Vasko175012e2019-11-06 15:49:14 +01006728 * @brief Check when/must expressions of a node on a compiled schema tree.
6729 * @param[in] ctx Compile context.
6730 * @param[in] node Node to check.
6731 * @return LY_ERR value
6732 */
6733static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006734lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006735{
Michal Vasko175012e2019-11-06 15:49:14 +01006736 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006737 uint32_t i;
6738 LY_ARRAY_SIZE_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01006739 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006740 struct lysc_when **when = NULL;
6741 struct lysc_must *musts = NULL;
6742 LY_ERR ret = LY_SUCCESS;
6743
6744 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006745 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko175012e2019-11-06 15:49:14 +01006746
6747 switch (node->nodetype) {
6748 case LYS_CONTAINER:
6749 when = ((struct lysc_node_container *)node)->when;
6750 musts = ((struct lysc_node_container *)node)->musts;
6751 break;
6752 case LYS_CHOICE:
6753 when = ((struct lysc_node_choice *)node)->when;
6754 break;
6755 case LYS_LEAF:
6756 when = ((struct lysc_node_leaf *)node)->when;
6757 musts = ((struct lysc_node_leaf *)node)->musts;
6758 break;
6759 case LYS_LEAFLIST:
6760 when = ((struct lysc_node_leaflist *)node)->when;
6761 musts = ((struct lysc_node_leaflist *)node)->musts;
6762 break;
6763 case LYS_LIST:
6764 when = ((struct lysc_node_list *)node)->when;
6765 musts = ((struct lysc_node_list *)node)->musts;
6766 break;
6767 case LYS_ANYXML:
6768 case LYS_ANYDATA:
6769 when = ((struct lysc_node_anydata *)node)->when;
6770 musts = ((struct lysc_node_anydata *)node)->musts;
6771 break;
6772 case LYS_CASE:
6773 when = ((struct lysc_node_case *)node)->when;
6774 break;
6775 case LYS_NOTIF:
6776 musts = ((struct lysc_notif *)node)->musts;
6777 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006778 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006779 case LYS_ACTION:
6780 /* first process input musts */
6781 musts = ((struct lysc_action *)node)->input.musts;
6782 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006783 default:
6784 /* nothing to check */
6785 break;
6786 }
6787
Michal Vasko175012e2019-11-06 15:49:14 +01006788 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006789 LY_ARRAY_FOR(when, u) {
Michal Vasko004d3152020-06-11 19:59:22 +02006790 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 +02006791 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006792 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006793 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006794 goto cleanup;
6795 }
6796
Michal Vaskodc052f32019-11-07 11:11:38 +01006797 ctx->path[0] = '\0';
6798 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006799 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006800 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006801 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6802 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006803
Michal Vaskoecd62de2019-11-13 12:35:11 +01006804 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006805 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006806 schema->name);
6807 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006808
6809 /* check dummy node accessing */
6810 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006811 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006812 ret = LY_EVALID;
6813 goto cleanup;
6814 }
Michal Vasko175012e2019-11-06 15:49:14 +01006815 }
6816 }
6817
Michal Vaskoecd62de2019-11-13 12:35:11 +01006818 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006819 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006820 LY_CHECK_GOTO(ret, cleanup);
6821
Michal Vaskod3678892020-05-21 10:06:58 +02006822 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006823 }
6824
Michal Vasko5d8756a2019-11-07 15:21:00 +01006825check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006826 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006827 LY_ARRAY_FOR(musts, u) {
6828 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 +01006829 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006830 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006831 goto cleanup;
6832 }
6833
Michal Vaskodc052f32019-11-07 11:11:38 +01006834 ctx->path[0] = '\0';
6835 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006836 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006837 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006838 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006839 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006840 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6841 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006842 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006843 }
6844 }
6845
Michal Vaskod3678892020-05-21 10:06:58 +02006846 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006847 }
6848
Michal Vasko1bf09392020-03-27 12:38:10 +01006849 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006850 /* now check output musts */
6851 input_done = 1;
6852 musts = ((struct lysc_action *)node)->output.musts;
6853 opts = LYXP_SCNODE_OUTPUT;
6854 goto check_musts;
6855 }
6856
Michal Vasko175012e2019-11-06 15:49:14 +01006857cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006858 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006859 return ret;
6860}
6861
Michal Vasko8d544252020-03-02 10:19:52 +01006862static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006863lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6864{
6865 const struct lysc_node *target = NULL;
6866 struct ly_path *p;
6867 struct lysc_type *type;
6868
6869 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6870
6871 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02006872 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
6873 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
6874 lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02006875
6876 /* get the target node */
6877 target = p[LY_ARRAY_SIZE(p) - 1].node;
6878 ly_path_free(node->module->ctx, p);
6879
6880 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
6881 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6882 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
6883 lref->path->expr, lys_nodetype2str(target->nodetype));
6884 return LY_EVALID;
6885 }
6886
6887 /* check status */
6888 ctx->path[0] = '\0';
6889 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6890 ctx->path_len = strlen(ctx->path);
6891 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
6892 return LY_EVALID;
6893 }
6894 ctx->path_len = 1;
6895 ctx->path[1] = '\0';
6896
6897 /* check config */
6898 if (lref->require_instance && (node->flags & LYS_CONFIG_W)) {
6899 if (target->flags & LYS_CONFIG_R) {
6900 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
6901 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
6902 return LY_EVALID;
6903 }
6904 }
6905
6906 /* store the target's type and check for circular chain of leafrefs */
6907 lref->realtype = ((struct lysc_node_leaf *)target)->type;
6908 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
6909 if (type == (struct lysc_type *)lref) {
6910 /* circular chain detected */
6911 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6912 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
6913 return LY_EVALID;
6914 }
6915 }
6916
6917 /* check if leafref and its target are under common if-features */
6918 if (lys_compile_leafref_features_validate(node, target)) {
6919 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6920 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
6921 " features applicable to the leafref itself.", lref->path->expr);
6922 return LY_EVALID;
6923 }
6924
6925 return LY_SUCCESS;
6926}
6927
6928static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01006929lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
6930{
6931 struct lysc_ext_instance *ext;
6932 struct lysp_ext_instance *ext_p = NULL;
6933 struct lysp_stmt *stmt;
6934 const struct lys_module *ext_mod;
6935 LY_ERR ret = LY_SUCCESS;
6936
6937 /* create the parsed extension instance manually */
6938 ext_p = calloc(1, sizeof *ext_p);
6939 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
6940 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
6941 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
6942 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
6943 ext_p->insubstmt_index = 0;
6944
6945 stmt = calloc(1, sizeof *ext_p->child);
6946 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
6947 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
6948 stmt->kw = LY_STMT_TYPE;
6949 ext_p->child = stmt;
6950
6951 /* allocate new extension instance */
6952 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
6953
6954 /* manually get extension definition module */
6955 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
6956
6957 /* compile the extension instance */
6958 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
6959
6960cleanup:
6961 lysp_ext_instance_free(ctx->ctx, ext_p);
6962 free(ext_p);
6963 return ret;
6964}
6965
Michal Vasko004d3152020-06-11 19:59:22 +02006966static LY_ERR
6967lys_compile_unres(struct lysc_ctx *ctx)
6968{
6969 struct lysc_node *node;
6970 struct lysc_type *type, *typeiter;
6971 struct lysc_type_leafref *lref;
6972 LY_ARRAY_SIZE_TYPE v;
6973 uint32_t i;
6974
6975 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6976 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6977 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
6978 for (i = 0; i < ctx->leafrefs.count; ++i) {
6979 node = ctx->leafrefs.objs[i];
6980 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6981 type = ((struct lysc_node_leaf *)node)->type;
6982 if (type->basetype == LY_TYPE_LEAFREF) {
6983 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
6984 } else if (type->basetype == LY_TYPE_UNION) {
6985 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
6986 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6987 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
6988 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
6989 }
6990 }
6991 }
6992 }
6993 for (i = 0; i < ctx->leafrefs.count; ++i) {
6994 /* store pointer to the real type */
6995 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
6996 if (type->basetype == LY_TYPE_LEAFREF) {
6997 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6998 typeiter->basetype == LY_TYPE_LEAFREF;
6999 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7000 ((struct lysc_type_leafref*)type)->realtype = typeiter;
7001 } else if (type->basetype == LY_TYPE_UNION) {
7002 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7003 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7004 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7005 typeiter->basetype == LY_TYPE_LEAFREF;
7006 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7007 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7008 }
7009 }
7010 }
7011 }
7012
7013 /* check xpath */
7014 for (i = 0; i < ctx->xpath.count; ++i) {
7015 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7016 }
7017
7018 /* finish incomplete default values compilation */
7019 for (i = 0; i < ctx->dflts.count; ++i) {
7020 struct ly_err_item *err = NULL;
7021 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
7022 LY_ERR ret;
7023 ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
7024 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7025 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7026 if (err) {
7027 ly_err_print(err);
7028 ctx->path[0] = '\0';
7029 lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7030 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7031 "Invalid default - value does not fit the type (%s).", err->msg);
7032 ly_err_free(err);
7033 }
7034 LY_CHECK_RET(ret);
7035 }
7036
7037 return LY_SUCCESS;
7038}
7039
Radek Krejci19a96102018-11-15 13:38:09 +01007040LY_ERR
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007041lys_compile(struct lys_module **mod, int options)
Radek Krejci19a96102018-11-15 13:38:09 +01007042{
7043 struct lysc_ctx ctx = {0};
7044 struct lysc_module *mod_c;
7045 struct lysp_module *sp;
7046 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007047 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007048 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007049 struct lys_module *m;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007050 LY_ARRAY_SIZE_TYPE u, v;
7051 uint32_t i;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007052 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007053
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007054 LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007055
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007056 if (!(*mod)->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007057 /* just imported modules are not compiled */
7058 return LY_SUCCESS;
7059 }
7060
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007061 sp = (*mod)->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007062
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007063 ctx.ctx = (*mod)->ctx;
7064 ctx.mod = *mod;
7065 ctx.mod_def = *mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007066 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007067 ctx.path_len = 1;
7068 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007069
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007070 (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
7071 LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
7072 mod_c->mod = *mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007073
Radek Krejciec4da802019-05-02 13:02:41 +02007074 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007075 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007076 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007077 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7078 }
Radek Krejci0935f412019-08-20 16:15:18 +02007079
7080 /* features */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007081 if ((*mod)->off_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007082 /* there is already precompiled array of features */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007083 mod_c->features = (*mod)->off_features;
7084 (*mod)->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007085 } else {
7086 /* features are compiled directly into the compiled module structure,
7087 * 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 +02007088 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007089 LY_CHECK_GOTO(ret, error);
7090 }
7091 /* finish feature compilation, not only for the main module, but also for the submodules.
7092 * Due to possible forward references, it must be done when all the features (including submodules)
7093 * are present. */
7094 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007095 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007096 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7097 }
Radek Krejci327de162019-06-14 12:52:07 +02007098 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007099 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007100 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007101 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007102 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007103 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7104 }
Radek Krejci327de162019-06-14 12:52:07 +02007105 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007106 }
Radek Krejci327de162019-06-14 12:52:07 +02007107 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007108
Radek Krejci0935f412019-08-20 16:15:18 +02007109 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02007110 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02007111 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007112 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007113 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007114 }
Radek Krejci327de162019-06-14 12:52:07 +02007115 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007116
Radek Krejci95710c92019-02-11 15:49:55 +01007117 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007118 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007119 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007120 }
Radek Krejci95710c92019-02-11 15:49:55 +01007121
Radek Krejciec4da802019-05-02 13:02:41 +02007122 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7123 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007124
Radek Krejci95710c92019-02-11 15:49:55 +01007125 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007126 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007127 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007128 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007129 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007130
Radek Krejci474f9b82019-07-24 11:36:37 +02007131 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007132 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007133
Radek Krejci0935f412019-08-20 16:15:18 +02007134 /* extension instances TODO cover extension instances from submodules */
7135 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007136
Michal Vasko004d3152020-06-11 19:59:22 +02007137 /* finish compilation for all unresolved items in the context */
7138 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007139
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007140 /* validate non-instantiated groupings from the parsed schema,
7141 * without it we would accept even the schemas with invalid grouping specification */
7142 ctx.options |= LYSC_OPT_GROUPING;
7143 LY_ARRAY_FOR(sp->groupings, u) {
7144 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007145 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007146 }
7147 }
7148 LY_LIST_FOR(sp->data, node_p) {
7149 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7150 LY_ARRAY_FOR(grps, u) {
7151 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007152 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007153 }
7154 }
7155 }
7156
Radek Krejci474f9b82019-07-24 11:36:37 +02007157 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7158 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7159 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7160 }
7161
Michal Vasko8d544252020-03-02 10:19:52 +01007162#if 0
7163 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7164 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7165 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7166 * the anotation definitions available in the internal schema structure. */
7167 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7168 if (lyp_add_ietf_netconf_annotations(mod)) {
7169 lys_free(mod, NULL, 1, 1);
7170 return NULL;
7171 }
7172 }
7173#endif
7174
7175 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007176 if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
7177 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007178 }
7179
Radek Krejci1c0c3442019-07-23 16:08:47 +02007180 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007181 ly_set_erase(&ctx.xpath, NULL);
7182 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007183 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007184 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007185 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007186
Radek Krejciec4da802019-05-02 13:02:41 +02007187 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007188 lysp_module_free((*mod)->parsed);
7189 (*mod)->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007190 }
7191
Radek Krejciec4da802019-05-02 13:02:41 +02007192 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007193 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007194 for (i = 0; i < ctx.ctx->list.count; ++i) {
7195 m = ctx.ctx->list.objs[i];
Radek Krejci95710c92019-02-11 15:49:55 +01007196 if (m->implemented == 2) {
7197 m->implemented = 1;
7198 }
7199 }
7200 }
7201
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007202 (*mod)->compiled = mod_c;
Radek Krejci19a96102018-11-15 13:38:09 +01007203 return LY_SUCCESS;
7204
7205error:
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007206 lys_feature_precompile_revert(&ctx, *mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007207 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007208 ly_set_erase(&ctx.xpath, NULL);
7209 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007210 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007211 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007212 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007213 lysc_module_free(mod_c, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007214 (*mod)->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007215
7216 /* revert compilation of modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007217 for (i = 0; i < ctx.ctx->list.count; ++i) {
7218 m = ctx.ctx->list.objs[i];
Michal Vasko2947ce12019-12-16 10:37:19 +01007219 if ((m->implemented == 2) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007220 /* revert features list to the precompiled state */
7221 lys_feature_precompile_revert(&ctx, m);
7222 /* mark module as imported-only / not-implemented */
7223 m->implemented = 0;
7224 /* free the compiled version of the module */
7225 lysc_module_free(m->compiled, NULL);
7226 m->compiled = NULL;
7227 }
7228 }
7229
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007230 /* remove the module itself from the context and free it */
7231 ly_set_rm(&ctx.ctx->list, *mod, NULL);
7232 lys_module_free(*mod, NULL);
7233 *mod = NULL;
7234
Radek Krejci19a96102018-11-15 13:38:09 +01007235 return ret;
7236}