blob: 4767760682d1c477d2edd9aa156f77cc4d159341 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejci19a96102018-11-15 13:38:09 +010016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020026#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "dict.h"
29#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020030#include "path.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020031#include "parser.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020032#include "parser_schema.h"
Radek Krejci0935f412019-08-20 16:15:18 +020033#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020035#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020039#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010040#include "tree_schema_internal.h"
41#include "xpath.h"
42
Michal Vasko5fe75f12020-03-02 13:52:37 +010043static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
44 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
45
Radek Krejci19a96102018-11-15 13:38:09 +010046/**
47 * @brief Duplicate string into dictionary
48 * @param[in] CTX libyang context of the dictionary.
49 * @param[in] ORIG String to duplicate.
50 * @param[out] DUP Where to store the result.
51 */
52#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
53
Radek Krejciec4da802019-05-02 13:02:41 +020054#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010055 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020056 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
57 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
58 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci19a96102018-11-15 13:38:09 +010059 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020060 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010061 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
62 } \
63 }
64
Radek Krejciec4da802019-05-02 13:02:41 +020065#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010066 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020067 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
68 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
69 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010070 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020071 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010072 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
73 } \
74 }
75
Radek Krejci0935f412019-08-20 16:15:18 +020076#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
77 if (EXTS_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020078 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
79 for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \
Radek Krejci0935f412019-08-20 16:15:18 +020080 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010081 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \
Radek Krejci0935f412019-08-20 16:15:18 +020082 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
83 } \
84 }
85
Radek Krejciec4da802019-05-02 13:02:41 +020086#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010087 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020088 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
89 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
90 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010091 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020092 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010093 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
94 } \
95 }
96
Radek Krejciec4da802019-05-02 13:02:41 +020097#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010098 if (MEMBER_P) { \
99 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
100 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200101 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100102 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
103 }
104
Radek Krejciec4da802019-05-02 13:02:41 +0200105#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100106 if (MEMBER_P) { \
107 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200108 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100109 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200110 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100111 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
112 }
113
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100114#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100115 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200116 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100117 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100118 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
119 return LY_EVALID; \
120 } \
121 } \
122 }
123
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100124#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
125 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200126 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100127 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
128 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
129 return LY_EVALID; \
130 } \
131 } \
132 }
133
134struct lysc_ext *
135lysc_ext_dup(struct lysc_ext *orig)
136{
137 ++orig->refcount;
138 return orig;
139}
140
Radek Krejci19a96102018-11-15 13:38:09 +0100141static struct lysc_ext_instance *
142lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
143{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100144 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100145 (void) ctx;
146 (void) orig;
147 return NULL;
148}
149
Radek Krejcib56c7502019-02-13 14:19:54 +0100150/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200151 * @brief Add record into the compile context's list of incomplete default values.
152 * @param[in] ctx Compile context with the incomplete default values list.
153 * @param[in] context_node Context schema node to store in the record.
154 * @param[in] dflt Incomplete default value to store in the record.
155 * @param[in] dflt_mod Module of the default value definition to store in the record.
156 * @return LY_EMEM in case of memory allocation failure.
157 * @return LY_SUCCESS
158 */
159static LY_ERR
160lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
161{
162 struct lysc_incomplete_dflt *r;
163 r = malloc(sizeof *r);
164 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
165 r->context_node = context_node;
166 r->dflt = dflt;
167 r->dflt_mod = dflt_mod;
168 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
169
170 return LY_SUCCESS;
171}
172
173/**
174 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
175 * @param[in] ctx Compile context with the incomplete default values list.
176 * @param[in] dflt Incomplete default values identifying the record to remove.
177 */
178static void
179lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
180{
181 unsigned int u;
182 struct lysc_incomplete_dflt *r;
183
184 for (u = 0; u < ctx->dflts.count; ++u) {
185 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
186 if (r->dflt == dflt) {
187 free(ctx->dflts.objs[u]);
188 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
189 --ctx->dflts.count;
190 return;
191 }
192 }
193}
194
Radek Krejci0e59c312019-08-15 15:34:15 +0200195void
Radek Krejci327de162019-06-14 12:52:07 +0200196lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
197{
198 int len;
199 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
200
201 if (!name) {
202 /* removing last path segment */
203 if (ctx->path[ctx->path_len - 1] == '}') {
204 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
205 if (ctx->path[ctx->path_len] == '=') {
206 ctx->path[ctx->path_len++] = '}';
207 } else {
208 /* not a top-level special tag, remove also preceiding '/' */
209 goto remove_nodelevel;
210 }
211 } else {
212remove_nodelevel:
213 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
214 if (ctx->path_len == 0) {
215 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200216 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200217 }
218 }
219 /* set new terminating NULL-byte */
220 ctx->path[ctx->path_len] = '\0';
221 } else {
222 if (ctx->path_len > 1) {
223 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
224 /* extension of the special tag */
225 nextlevel = 2;
226 --ctx->path_len;
227 } else {
228 /* there is already some path, so add next level */
229 nextlevel = 1;
230 }
231 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
232
233 if (nextlevel != 2) {
234 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
235 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200236 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200237 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200238 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200239 }
240 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200241 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200242 }
Radek Krejciacc79042019-07-25 14:14:57 +0200243 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
244 /* output truncated */
245 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
246 } else {
247 ctx->path_len += len;
248 }
Radek Krejci327de162019-06-14 12:52:07 +0200249 }
250}
251
252/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100253 * @brief Duplicate the compiled pattern structure.
254 *
255 * Instead of duplicating memory, the reference counter in the @p orig is increased.
256 *
257 * @param[in] orig The pattern structure to duplicate.
258 * @return The duplicated structure to use.
259 */
Radek Krejci19a96102018-11-15 13:38:09 +0100260static struct lysc_pattern*
261lysc_pattern_dup(struct lysc_pattern *orig)
262{
263 ++orig->refcount;
264 return orig;
265}
266
Radek Krejcib56c7502019-02-13 14:19:54 +0100267/**
268 * @brief Duplicate the array of compiled patterns.
269 *
270 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
271 *
272 * @param[in] ctx Libyang context for logging.
273 * @param[in] orig The patterns sized array to duplicate.
274 * @return New sized array as a copy of @p orig.
275 * @return NULL in case of memory allocation error.
276 */
Radek Krejci19a96102018-11-15 13:38:09 +0100277static struct lysc_pattern**
278lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
279{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100280 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200281 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100282
Radek Krejcib56c7502019-02-13 14:19:54 +0100283 assert(orig);
284
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200285 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100286 LY_ARRAY_FOR(orig, u) {
287 dup[u] = lysc_pattern_dup(orig[u]);
288 LY_ARRAY_INCREMENT(dup);
289 }
290 return dup;
291}
292
Radek Krejcib56c7502019-02-13 14:19:54 +0100293/**
294 * @brief Duplicate compiled range structure.
295 *
296 * @param[in] ctx Libyang context for logging.
297 * @param[in] orig The range structure to be duplicated.
298 * @return New compiled range structure as a copy of @p orig.
299 * @return NULL in case of memory allocation error.
300 */
Radek Krejci19a96102018-11-15 13:38:09 +0100301struct lysc_range*
302lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
303{
304 struct lysc_range *dup;
305 LY_ERR ret;
306
Radek Krejcib56c7502019-02-13 14:19:54 +0100307 assert(orig);
308
Radek Krejci19a96102018-11-15 13:38:09 +0100309 dup = calloc(1, sizeof *dup);
310 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
311 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200312 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
313 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
314 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100315 }
316 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
317 DUP_STRING(ctx, orig->emsg, dup->emsg);
318 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
319
320 return dup;
321cleanup:
322 free(dup);
323 (void) ret; /* set but not used due to the return type */
324 return NULL;
325}
326
Radek Krejcib56c7502019-02-13 14:19:54 +0100327/**
328 * @brief Stack for processing if-feature expressions.
329 */
Radek Krejci19a96102018-11-15 13:38:09 +0100330struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100331 int size; /**< number of items in the stack */
332 int index; /**< first empty item */
333 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100334};
335
Radek Krejcib56c7502019-02-13 14:19:54 +0100336/**
337 * @brief Add @ref ifftokens into the stack.
338 * @param[in] stack The if-feature stack to use.
339 * @param[in] value One of the @ref ifftokens to store in the stack.
340 * @return LY_EMEM in case of memory allocation error
341 * @return LY_ESUCCESS if the value successfully stored.
342 */
Radek Krejci19a96102018-11-15 13:38:09 +0100343static LY_ERR
344iff_stack_push(struct iff_stack *stack, uint8_t value)
345{
346 if (stack->index == stack->size) {
347 stack->size += 4;
348 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
349 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
350 }
351 stack->stack[stack->index++] = value;
352 return LY_SUCCESS;
353}
354
Radek Krejcib56c7502019-02-13 14:19:54 +0100355/**
356 * @brief Get (and remove) the last item form the stack.
357 * @param[in] stack The if-feature stack to use.
358 * @return The value from the top of the stack.
359 */
Radek Krejci19a96102018-11-15 13:38:09 +0100360static uint8_t
361iff_stack_pop(struct iff_stack *stack)
362{
Radek Krejcib56c7502019-02-13 14:19:54 +0100363 assert(stack && stack->index);
364
Radek Krejci19a96102018-11-15 13:38:09 +0100365 stack->index--;
366 return stack->stack[stack->index];
367}
368
Radek Krejcib56c7502019-02-13 14:19:54 +0100369/**
370 * @brief Clean up the stack.
371 * @param[in] stack The if-feature stack to use.
372 */
Radek Krejci19a96102018-11-15 13:38:09 +0100373static void
374iff_stack_clean(struct iff_stack *stack)
375{
376 stack->size = 0;
377 free(stack->stack);
378}
379
Radek Krejcib56c7502019-02-13 14:19:54 +0100380/**
381 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
382 * (libyang format of the if-feature expression).
383 * @param[in,out] list The 2bits array to modify.
384 * @param[in] op The operand (@ref ifftokens) to store.
385 * @param[in] pos Position (0-based) where to store the given @p op.
386 */
Radek Krejci19a96102018-11-15 13:38:09 +0100387static void
388iff_setop(uint8_t *list, uint8_t op, int pos)
389{
390 uint8_t *item;
391 uint8_t mask = 3;
392
393 assert(pos >= 0);
394 assert(op <= 3); /* max 2 bits */
395
396 item = &list[pos / 4];
397 mask = mask << 2 * (pos % 4);
398 *item = (*item) & ~mask;
399 *item = (*item) | (op << 2 * (pos % 4));
400}
401
Radek Krejcib56c7502019-02-13 14:19:54 +0100402#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
403#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100404
Radek Krejci0af46292019-01-11 16:02:31 +0100405/**
406 * @brief Find a feature of the given name and referenced in the given module.
407 *
408 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
409 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
410 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
411 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
412 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
413 * assigned till that time will be still valid.
414 *
415 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
416 * @param[in] name Name of the feature including possible prefix.
417 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
418 * @return Pointer to the feature structure if found, NULL otherwise.
419 */
Radek Krejci19a96102018-11-15 13:38:09 +0100420static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100421lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100422{
423 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200424 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0af46292019-01-11 16:02:31 +0100425 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100426
427 for (i = 0; i < len; ++i) {
428 if (name[i] == ':') {
429 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100430 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100431 LY_CHECK_RET(!mod, NULL);
432
433 name = &name[i + 1];
434 len = len - i - 1;
435 }
436 }
437
438 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100439 if (mod->implemented) {
440 /* module is implemented so there is already the compiled schema */
441 flist = mod->compiled->features;
442 } else {
Michal Vasko33ff9422020-07-03 09:50:39 +0200443 flist = mod->dis_features;
Radek Krejci0af46292019-01-11 16:02:31 +0100444 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200445 LY_ARRAY_FOR(flist, u) {
446 f = &flist[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200447 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100448 return f;
449 }
450 }
451
452 return NULL;
453}
454
Michal Vasko8d544252020-03-02 10:19:52 +0100455/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100456 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
457 */
458static LY_ERR
459lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
460{
461 LY_ERR ret = LY_SUCCESS;
462
463 if (!ext_p->compiled) {
464 lysc_update_path(ctx, NULL, "{extension}");
465 lysc_update_path(ctx, NULL, ext_p->name);
466
467 /* compile the extension definition */
468 ext_p->compiled = calloc(1, sizeof **ext);
469 ext_p->compiled->refcount = 1;
470 DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name);
471 DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument);
472 ext_p->compiled->module = (struct lys_module *)ext_mod;
473 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
474
475 lysc_update_path(ctx, NULL, NULL);
476 lysc_update_path(ctx, NULL, NULL);
477
478 /* find extension definition plugin */
479 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
480 }
481
482 *ext = lysc_ext_dup(ext_p->compiled);
483
484done:
485 return ret;
486}
487
488/**
Michal Vasko8d544252020-03-02 10:19:52 +0100489 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
490 *
491 * @param[in] ctx Compilation context.
492 * @param[in] ext_p Parsed extension instance.
493 * @param[in,out] ext Prepared compiled extension instance.
494 * @param[in] parent Extension instance parent.
495 * @param[in] parent_type Extension instance parent type.
496 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
497 */
Radek Krejci19a96102018-11-15 13:38:09 +0100498static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100499lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
500 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100501{
Radek Krejci7c960162019-09-18 14:16:12 +0200502 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100503 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200504 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200505 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200506 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100507
508 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
509 ext->insubstmt = ext_p->insubstmt;
510 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200511 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200512 ext->parent = parent;
513 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100514
Radek Krejcif56e2a42019-09-09 14:15:25 +0200515 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200516
Radek Krejci19a96102018-11-15 13:38:09 +0100517 /* get module where the extension definition should be placed */
Radek Krejci7c960162019-09-18 14:16:12 +0200518 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u);
519 if (ext_p->yin) {
520 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
521 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
522 * YANG (import) prefix must be done here. */
523 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
524 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
525 u = 0;
526 } else if (ctx->mod_def->compiled) {
Radek Krejci7c960162019-09-18 14:16:12 +0200527 LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) {
528 if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) {
529 char *s;
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
Michal Vasko33ff9422020-07-03 09:50:39 +0200951LY_ERR
952lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
953 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +0100954{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200955 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +0200956 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +0100957 LY_ERR ret = LY_SUCCESS;
958
Michal Vasko33ff9422020-07-03 09:50:39 +0200959 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +0200960
Michal Vasko33ff9422020-07-03 09:50:39 +0200961 if (!ctx_sc) {
962 context.ctx = ctx;
963 context.mod = module;
964 context.path_len = 1;
965 context.path[0] = '/';
966 ctx_sc = &context;
967 }
Radek Krejci19a96102018-11-15 13:38:09 +0100968
Michal Vasko33ff9422020-07-03 09:50:39 +0200969 if (!identities_p) {
970 return LY_SUCCESS;
971 }
972 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200973 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +0200974 }
975
976 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200977 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +0200978 LY_ARRAY_FOR(identities_p, u) {
979 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
980
981 LY_ARRAY_INCREMENT(*identities);
982 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
983 DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name);
984 DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc);
985 DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref);
986 (*identities)[offset + u].module = ctx_sc->mod;
987 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
988 lys_compile_iffeature, ret, done);
989 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
990 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
991 LYEXT_PAR_IDENT, ret, done);
992 (*identities)[offset + u].flags = identities_p[u].flags;
993
994 lysc_update_path(ctx_sc, NULL, NULL);
995 }
996 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100997done:
998 return ret;
999}
1000
Radek Krejcib56c7502019-02-13 14:19:54 +01001001/**
1002 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1003 *
1004 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1005 *
1006 * @param[in] ctx Compile context for logging.
1007 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1008 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1009 * @return LY_SUCCESS if everything is ok.
1010 * @return LY_EVALID if the identity is derived from itself.
1011 */
Radek Krejci38222632019-02-12 16:55:05 +01001012static LY_ERR
1013lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1014{
1015 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001016 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001017 struct ly_set recursion = {0};
1018 struct lysc_ident *drv;
1019
1020 if (!derived) {
1021 return LY_SUCCESS;
1022 }
1023
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001024 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001025 if (ident == derived[u]) {
1026 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1027 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1028 goto cleanup;
1029 }
1030 ly_set_add(&recursion, derived[u], 0);
1031 }
1032
1033 for (v = 0; v < recursion.count; ++v) {
1034 drv = recursion.objs[v];
1035 if (!drv->derived) {
1036 continue;
1037 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001038 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001039 if (ident == drv->derived[u]) {
1040 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1041 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1042 goto cleanup;
1043 }
1044 ly_set_add(&recursion, drv->derived[u], 0);
1045 }
1046 }
1047 ret = LY_SUCCESS;
1048
1049cleanup:
1050 ly_set_erase(&recursion, NULL);
1051 return ret;
1052}
1053
Radek Krejcia3045382018-11-22 14:30:31 +01001054/**
1055 * @brief Find and process the referenced base identities from another identity or identityref
1056 *
Radek Krejciaca74032019-06-04 08:53:06 +02001057 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001058 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1059 * to distinguish these two use cases.
1060 *
1061 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1062 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001063 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001064 * @param[in] bases Array of bases of identityref to fill in.
1065 * @return LY_ERR value.
1066 */
Radek Krejci19a96102018-11-15 13:38:09 +01001067static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001068lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
1069 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001070{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001071 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001072 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001073 struct lys_module *mod;
Michal Vasko33ff9422020-07-03 09:50:39 +02001074 struct lysc_ident **idref, *identities;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001075
1076 assert(ident || bases);
1077
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001078 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001079 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1080 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1081 return LY_EVALID;
1082 }
1083
Michal Vasko33ff9422020-07-03 09:50:39 +02001084 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001085 s = strchr(bases_p[u], ':');
1086 if (s) {
1087 /* prefixed identity */
1088 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001089 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001090 } else {
1091 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001092 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001093 }
1094 if (!mod) {
1095 if (ident) {
1096 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1097 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1098 } else {
1099 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1100 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1101 }
1102 return LY_EVALID;
1103 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001104
Radek Krejci555cb5b2018-11-16 14:54:33 +01001105 idref = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02001106 if (mod->compiled) {
1107 identities = mod->compiled->identities;
1108 } else {
1109 identities = mod->dis_identities;
1110 }
1111 LY_ARRAY_FOR(identities, v) {
1112 if (!strcmp(name, identities[v].name)) {
1113 if (ident) {
1114 if (ident == &identities[v]) {
1115 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1116 "Identity \"%s\" is derived from itself.", ident->name);
1117 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001118 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001119 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived));
1120 /* we have match! store the backlink */
1121 LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM);
1122 *idref = ident;
1123 } else {
1124 /* we have match! store the found identity */
1125 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
1126 *idref = &identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001127 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001128 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001129 }
1130 }
1131 if (!idref || !(*idref)) {
1132 if (ident) {
1133 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1134 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1135 } else {
1136 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1137 "Unable to find base (%s) of identityref.", bases_p[u]);
1138 }
1139 return LY_EVALID;
1140 }
1141 }
1142 return LY_SUCCESS;
1143}
1144
Radek Krejcia3045382018-11-22 14:30:31 +01001145/**
1146 * @brief For the given array of identities, set the backlinks from all their base identities.
1147 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1148 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1149 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1150 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1151 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001152static LY_ERR
1153lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1154{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001155 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001156
Michal Vasko33ff9422020-07-03 09:50:39 +02001157 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001158 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001159 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001160 continue;
1161 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001162 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001163 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 +02001164 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001165 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001166 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001167 return LY_SUCCESS;
1168}
1169
Radek Krejci0af46292019-01-11 16:02:31 +01001170LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001171lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
1172 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001173{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001174 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001175 struct lysc_ctx context = {0};
1176
Radek Krejci327de162019-06-14 12:52:07 +02001177 assert(ctx_sc || ctx);
1178
1179 if (!ctx_sc) {
1180 context.ctx = ctx;
1181 context.mod = module;
1182 context.path_len = 1;
1183 context.path[0] = '/';
1184 ctx_sc = &context;
1185 }
Radek Krejci0af46292019-01-11 16:02:31 +01001186
1187 if (!features_p) {
1188 return LY_SUCCESS;
1189 }
1190 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001191 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001192 }
1193
Radek Krejci327de162019-06-14 12:52:07 +02001194 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001195 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001196 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001197 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1198
Radek Krejci0af46292019-01-11 16:02:31 +01001199 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001200 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci327de162019-06-14 12:52:07 +02001201 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1202 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1203 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001204 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001205 (*features)[offset + u].module = ctx_sc->mod;
1206
1207 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001208 }
Radek Krejci327de162019-06-14 12:52:07 +02001209 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001210
1211 return LY_SUCCESS;
1212}
1213
Radek Krejcia3045382018-11-22 14:30:31 +01001214/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001215 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001216 *
1217 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1218 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001219 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001220 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1221 * being currently processed).
1222 * @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 +01001223 * @return LY_SUCCESS if everything is ok.
1224 * @return LY_EVALID if the feature references indirectly itself.
1225 */
1226static LY_ERR
1227lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1228{
1229 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001230 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001231 struct ly_set recursion = {0};
1232 struct lysc_feature *drv;
1233
1234 if (!depfeatures) {
1235 return LY_SUCCESS;
1236 }
1237
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001238 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001239 if (feature == depfeatures[u]) {
1240 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1241 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1242 goto cleanup;
1243 }
1244 ly_set_add(&recursion, depfeatures[u], 0);
1245 }
1246
1247 for (v = 0; v < recursion.count; ++v) {
1248 drv = recursion.objs[v];
1249 if (!drv->depfeatures) {
1250 continue;
1251 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001252 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001253 if (feature == drv->depfeatures[u]) {
1254 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1255 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1256 goto cleanup;
1257 }
1258 ly_set_add(&recursion, drv->depfeatures[u], 0);
1259 }
1260 }
1261 ret = LY_SUCCESS;
1262
1263cleanup:
1264 ly_set_erase(&recursion, NULL);
1265 return ret;
1266}
1267
1268/**
Radek Krejci0af46292019-01-11 16:02:31 +01001269 * @brief Create pre-compiled features array.
1270 *
1271 * See lys_feature_precompile() for more details.
1272 *
Radek Krejcia3045382018-11-22 14:30:31 +01001273 * @param[in] ctx Compile context.
1274 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001275 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001276 * @return LY_ERR value.
1277 */
Radek Krejci19a96102018-11-15 13:38:09 +01001278static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001279lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001280{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001281 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001282 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001283 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001284
Radek Krejci327de162019-06-14 12:52:07 +02001285
Radek Krejci0af46292019-01-11 16:02:31 +01001286 /* find the preprecompiled feature */
1287 LY_ARRAY_FOR(features, x) {
1288 if (strcmp(features[x].name, feature_p->name)) {
1289 continue;
1290 }
1291 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001292 lysc_update_path(ctx, NULL, "{feature}");
1293 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001294
Radek Krejci0af46292019-01-11 16:02:31 +01001295 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001296 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001297 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001298 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001299 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001300 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001301 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001302 /* check for circular dependency - direct reference first,... */
1303 if (feature == feature->iffeatures[u].features[v]) {
1304 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1305 "Feature \"%s\" is referenced from itself.", feature->name);
1306 return LY_EVALID;
1307 }
1308 /* ... and indirect circular reference */
1309 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1310
Radek Krejci0af46292019-01-11 16:02:31 +01001311 /* add itself into the dependants list */
1312 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1313 *df = feature;
1314 }
Radek Krejci19a96102018-11-15 13:38:09 +01001315 }
Radek Krejci19a96102018-11-15 13:38:09 +01001316 }
1317 }
Radek Krejci327de162019-06-14 12:52:07 +02001318 lysc_update_path(ctx, NULL, NULL);
1319 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001320 done:
1321 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001322 }
Radek Krejci0af46292019-01-11 16:02:31 +01001323
1324 LOGINT(ctx->ctx);
1325 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001326}
1327
Radek Krejcib56c7502019-02-13 14:19:54 +01001328/**
1329 * @brief Revert compiled list of features back to the precompiled state.
1330 *
1331 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
Michal Vasko33ff9422020-07-03 09:50:39 +02001332 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001333 *
1334 * @param[in] ctx Compilation context.
1335 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
Michal Vasko33ff9422020-07-03 09:50:39 +02001336 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001337 */
Radek Krejci95710c92019-02-11 15:49:55 +01001338static void
1339lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1340{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001341 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001342
Michal Vasko33ff9422020-07-03 09:50:39 +02001343 /* keep the dis_features list until the complete lys_module is freed */
1344 mod->dis_features = mod->compiled->features;
Radek Krejci95710c92019-02-11 15:49:55 +01001345 mod->compiled->features = NULL;
1346
Michal Vasko33ff9422020-07-03 09:50:39 +02001347 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001348 * which may points into the data being freed here */
Michal Vasko33ff9422020-07-03 09:50:39 +02001349 LY_ARRAY_FOR(mod->dis_features, u) {
1350 LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) {
1351 lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001352 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001353 LY_ARRAY_FREE(mod->dis_features[u].iffeatures);
1354 mod->dis_features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001355
Michal Vasko33ff9422020-07-03 09:50:39 +02001356 LY_ARRAY_FOR(mod->dis_features[u].exts, v) {
1357 lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001358 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001359 LY_ARRAY_FREE(mod->dis_features[u].exts);
1360 mod->dis_features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001361 }
1362}
1363
Radek Krejcia3045382018-11-22 14:30:31 +01001364/**
1365 * @brief Validate and normalize numeric value from a range definition.
1366 * @param[in] ctx Compile context.
1367 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1368 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1369 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1370 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1371 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1372 * @param[in] value String value of the range boundary.
1373 * @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.
1374 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1375 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1376 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001377LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001378range_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 +01001379{
Radek Krejci6cba4292018-11-15 17:33:29 +01001380 size_t fraction = 0, size;
1381
Radek Krejci19a96102018-11-15 13:38:09 +01001382 *len = 0;
1383
1384 assert(value);
1385 /* parse value */
1386 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1387 return LY_EVALID;
1388 }
1389
1390 if ((value[*len] == '-') || (value[*len] == '+')) {
1391 ++(*len);
1392 }
1393
1394 while (isdigit(value[*len])) {
1395 ++(*len);
1396 }
1397
1398 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001399 if (basetype == LY_TYPE_DEC64) {
1400 goto decimal;
1401 } else {
1402 *valcopy = strndup(value, *len);
1403 return LY_SUCCESS;
1404 }
Radek Krejci19a96102018-11-15 13:38:09 +01001405 }
1406 fraction = *len;
1407
1408 ++(*len);
1409 while (isdigit(value[*len])) {
1410 ++(*len);
1411 }
1412
Radek Krejci6cba4292018-11-15 17:33:29 +01001413 if (basetype == LY_TYPE_DEC64) {
1414decimal:
1415 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001416 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001417 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1418 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1419 *len, value, frdigits);
1420 return LY_EINVAL;
1421 }
1422 if (fraction) {
1423 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1424 } else {
1425 size = (*len) + frdigits + 1;
1426 }
1427 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001428 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1429
Radek Krejci6cba4292018-11-15 17:33:29 +01001430 (*valcopy)[size - 1] = '\0';
1431 if (fraction) {
1432 memcpy(&(*valcopy)[0], &value[0], fraction);
1433 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1434 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1435 } else {
1436 memcpy(&(*valcopy)[0], &value[0], *len);
1437 memset(&(*valcopy)[*len], '0', frdigits);
1438 }
Radek Krejci19a96102018-11-15 13:38:09 +01001439 }
1440 return LY_SUCCESS;
1441}
1442
Radek Krejcia3045382018-11-22 14:30:31 +01001443/**
1444 * @brief Check that values in range are in ascendant order.
1445 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001446 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1447 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001448 * @param[in] value Current value to check.
1449 * @param[in] prev_value The last seen value.
1450 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1451 */
Radek Krejci19a96102018-11-15 13:38:09 +01001452static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001453range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001454{
1455 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001456 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001457 return LY_EEXIST;
1458 }
1459 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001460 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001461 return LY_EEXIST;
1462 }
1463 }
1464 return LY_SUCCESS;
1465}
1466
Radek Krejcia3045382018-11-22 14:30:31 +01001467/**
1468 * @brief Set min/max value of the range part.
1469 * @param[in] ctx Compile context.
1470 * @param[in] part Range part structure to fill.
1471 * @param[in] max Flag to distinguish if storing min or max value.
1472 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1473 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1474 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1475 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1476 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001477 * @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 +01001478 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1479 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1480 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1481 * frdigits value), LY_EMEM.
1482 */
Radek Krejci19a96102018-11-15 13:38:09 +01001483static LY_ERR
1484range_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 +01001485 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001486{
1487 LY_ERR ret = LY_SUCCESS;
1488 char *valcopy = NULL;
1489 size_t len;
1490
1491 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001492 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001493 LY_CHECK_GOTO(ret, finalize);
1494 }
1495 if (!valcopy && base_range) {
1496 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001497 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001498 } else {
1499 part->min_64 = base_range->parts[0].min_64;
1500 }
1501 if (!first) {
1502 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1503 }
1504 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001505 }
1506
1507 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001508 case LY_TYPE_INT8: /* range */
1509 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001510 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 +01001511 } else if (max) {
1512 part->max_64 = INT64_C(127);
1513 } else {
1514 part->min_64 = INT64_C(-128);
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_INT16: /* range */
1521 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001522 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 +01001523 } else if (max) {
1524 part->max_64 = INT64_C(32767);
1525 } else {
1526 part->min_64 = INT64_C(-32768);
1527 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001528 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001529 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001530 }
1531 break;
1532 case LY_TYPE_INT32: /* range */
1533 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001534 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 +01001535 } else if (max) {
1536 part->max_64 = INT64_C(2147483647);
1537 } else {
1538 part->min_64 = INT64_C(-2147483648);
1539 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001540 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001541 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001542 }
1543 break;
1544 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001545 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001546 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001547 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001548 max ? &part->max_64 : &part->min_64);
1549 } else if (max) {
1550 part->max_64 = INT64_C(9223372036854775807);
1551 } else {
1552 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
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(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001556 }
1557 break;
1558 case LY_TYPE_UINT8: /* range */
1559 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001560 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 +01001561 } else if (max) {
1562 part->max_u64 = UINT64_C(255);
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_UINT16: /* range */
1571 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001572 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 +01001573 } else if (max) {
1574 part->max_u64 = UINT64_C(65535);
1575 } else {
1576 part->min_u64 = UINT64_C(0);
1577 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001578 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001579 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001580 }
1581 break;
1582 case LY_TYPE_UINT32: /* range */
1583 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001584 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 +01001585 } else if (max) {
1586 part->max_u64 = UINT64_C(4294967295);
1587 } else {
1588 part->min_u64 = UINT64_C(0);
1589 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001590 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001591 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001592 }
1593 break;
1594 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001595 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001596 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001597 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001598 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 +01001599 } else if (max) {
1600 part->max_u64 = UINT64_C(18446744073709551615);
1601 } else {
1602 part->min_u64 = UINT64_C(0);
1603 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001604 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001605 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001606 }
1607 break;
1608 default:
1609 LOGINT(ctx->ctx);
1610 ret = LY_EINT;
1611 }
1612
Radek Krejci5969f272018-11-23 10:03:58 +01001613finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001614 if (ret == LY_EDENIED) {
1615 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1616 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1617 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1618 } else if (ret == LY_EVALID) {
1619 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1620 "Invalid %s restriction - invalid value \"%s\".",
1621 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1622 } else if (ret == LY_EEXIST) {
1623 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1624 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001625 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001626 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001627 } else if (!ret && value) {
1628 *value = *value + len;
1629 }
1630 free(valcopy);
1631 return ret;
1632}
1633
Radek Krejcia3045382018-11-22 14:30:31 +01001634/**
1635 * @brief Compile the parsed range restriction.
1636 * @param[in] ctx Compile context.
1637 * @param[in] range_p Parsed range structure to compile.
1638 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1639 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1640 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1641 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1642 * range restriction must be more restrictive than the base_range.
1643 * @param[in,out] range Pointer to the created current range structure.
1644 * @return LY_ERR value.
1645 */
Radek Krejci19a96102018-11-15 13:38:09 +01001646static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001647lys_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 +01001648 struct lysc_range *base_range, struct lysc_range **range)
1649{
1650 LY_ERR ret = LY_EVALID;
1651 const char *expr;
1652 struct lysc_range_part *parts = NULL, *part;
1653 int range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001654 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001655
1656 assert(range);
1657 assert(range_p);
1658
1659 expr = range_p->arg;
1660 while(1) {
1661 if (isspace(*expr)) {
1662 ++expr;
1663 } else if (*expr == '\0') {
1664 if (range_expected) {
1665 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1666 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1667 length_restr ? "length" : "range", range_p->arg);
1668 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001669 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1671 "Invalid %s restriction - unexpected end of the expression (%s).",
1672 length_restr ? "length" : "range", range_p->arg);
1673 goto cleanup;
1674 }
1675 parts_done++;
1676 break;
1677 } else if (!strncmp(expr, "min", 3)) {
1678 if (parts) {
1679 /* min cannot be used elsewhere than in the first part */
1680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1681 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1682 expr - range_p->arg, range_p->arg);
1683 goto cleanup;
1684 }
1685 expr += 3;
1686
1687 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001688 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 +01001689 part->max_64 = part->min_64;
1690 } else if (*expr == '|') {
1691 if (!parts || range_expected) {
1692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1693 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1694 goto cleanup;
1695 }
1696 expr++;
1697 parts_done++;
1698 /* process next part of the expression */
1699 } else if (!strncmp(expr, "..", 2)) {
1700 expr += 2;
1701 while (isspace(*expr)) {
1702 expr++;
1703 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001704 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001705 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1706 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1707 goto cleanup;
1708 }
1709 /* continue expecting the upper boundary */
1710 range_expected = 1;
1711 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1712 /* number */
1713 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001714 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001715 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 +01001716 range_expected = 0;
1717 } else {
1718 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001719 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001720 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001721 part->max_64 = part->min_64;
1722 }
1723
1724 /* continue with possible another expression part */
1725 } else if (!strncmp(expr, "max", 3)) {
1726 expr += 3;
1727 while (isspace(*expr)) {
1728 expr++;
1729 }
1730 if (*expr != '\0') {
1731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1732 length_restr ? "length" : "range", expr);
1733 goto cleanup;
1734 }
1735 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001736 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001737 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 +01001738 range_expected = 0;
1739 } else {
1740 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001741 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001742 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001743 part->min_64 = part->max_64;
1744 }
1745 } else {
1746 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1747 length_restr ? "length" : "range", expr);
1748 goto cleanup;
1749 }
1750 }
1751
1752 /* check with the previous range/length restriction */
1753 if (base_range) {
1754 switch (basetype) {
1755 case LY_TYPE_BINARY:
1756 case LY_TYPE_UINT8:
1757 case LY_TYPE_UINT16:
1758 case LY_TYPE_UINT32:
1759 case LY_TYPE_UINT64:
1760 case LY_TYPE_STRING:
1761 uns = 1;
1762 break;
1763 case LY_TYPE_DEC64:
1764 case LY_TYPE_INT8:
1765 case LY_TYPE_INT16:
1766 case LY_TYPE_INT32:
1767 case LY_TYPE_INT64:
1768 uns = 0;
1769 break;
1770 default:
1771 LOGINT(ctx->ctx);
1772 ret = LY_EINT;
1773 goto cleanup;
1774 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001775 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001776 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1777 goto baseerror;
1778 }
1779 /* current lower bound is not lower than the base */
1780 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1781 /* base has single value */
1782 if (base_range->parts[v].min_64 == parts[u].min_64) {
1783 /* both lower bounds are the same */
1784 if (parts[u].min_64 != parts[u].max_64) {
1785 /* current continues with a range */
1786 goto baseerror;
1787 } else {
1788 /* equal single values, move both forward */
1789 ++v;
1790 continue;
1791 }
1792 } else {
1793 /* base is single value lower than current range, so the
1794 * value from base range is removed in the current,
1795 * move only base and repeat checking */
1796 ++v;
1797 --u;
1798 continue;
1799 }
1800 } else {
1801 /* base is the range */
1802 if (parts[u].min_64 == parts[u].max_64) {
1803 /* current is a single value */
1804 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1805 /* current is behind the base range, so base range is omitted,
1806 * move the base and keep the current for further check */
1807 ++v;
1808 --u;
1809 } /* else it is within the base range, so move the current, but keep the base */
1810 continue;
1811 } else {
1812 /* both are ranges - check the higher bound, the lower was already checked */
1813 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1814 /* higher bound is higher than the current higher bound */
1815 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1816 /* but the current lower bound is also higher, so the base range is omitted,
1817 * continue with the same current, but move the base */
1818 --u;
1819 ++v;
1820 continue;
1821 }
1822 /* current range starts within the base range but end behind it */
1823 goto baseerror;
1824 } else {
1825 /* current range is smaller than the base,
1826 * move current, but stay with the base */
1827 continue;
1828 }
1829 }
1830 }
1831 }
1832 if (u != parts_done) {
1833baseerror:
1834 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1835 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1836 length_restr ? "length" : "range", range_p->arg);
1837 goto cleanup;
1838 }
1839 }
1840
1841 if (!(*range)) {
1842 *range = calloc(1, sizeof **range);
1843 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1844 }
1845
Radek Krejcic8b31002019-01-08 10:24:45 +01001846 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001847 if (range_p->eapptag) {
1848 lydict_remove(ctx->ctx, (*range)->eapptag);
1849 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1850 }
1851 if (range_p->emsg) {
1852 lydict_remove(ctx->ctx, (*range)->emsg);
1853 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1854 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001855 if (range_p->dsc) {
1856 lydict_remove(ctx->ctx, (*range)->dsc);
1857 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1858 }
1859 if (range_p->ref) {
1860 lydict_remove(ctx->ctx, (*range)->ref);
1861 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1862 }
Radek Krejci19a96102018-11-15 13:38:09 +01001863 /* extensions are taken only from the last range by the caller */
1864
1865 (*range)->parts = parts;
1866 parts = NULL;
1867 ret = LY_SUCCESS;
1868cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001869 LY_ARRAY_FREE(parts);
1870
1871 return ret;
1872}
1873
1874/**
1875 * @brief Checks pattern syntax.
1876 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001877 * @param[in] ctx Context.
1878 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001879 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001880 * @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 +01001881 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001882 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001883LY_ERR
1884lys_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 +01001885{
Michal Vasko40a00082020-05-27 15:20:01 +02001886 int idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001887 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001888 int err_code;
1889 const char *orig_ptr;
1890 PCRE2_SIZE err_offset;
1891 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001892#define URANGE_LEN 19
1893 char *ublock2urange[][2] = {
1894 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1895 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1896 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1897 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1898 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1899 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1900 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1901 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1902 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1903 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1904 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1905 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1906 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1907 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1908 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1909 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1910 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1911 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1912 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1913 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1914 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1915 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1916 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1917 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1918 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1919 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1920 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1921 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1922 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1923 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1924 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1925 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1926 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1927 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1928 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1929 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1930 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1931 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1932 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1933 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1934 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1935 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1936 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1937 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1938 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1939 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1940 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1941 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1942 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1943 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1944 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1945 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1946 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1947 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1948 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1949 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1950 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1951 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1952 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1953 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1954 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1955 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1956 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1957 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1958 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1959 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1960 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1961 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1962 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1963 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1964 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1965 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1966 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1967 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1968 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1969 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1970 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1971 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1972 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1973 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1974 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1975 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1976 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1977 {NULL, NULL}
1978 };
1979
1980 /* adjust the expression to a Perl equivalent
1981 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1982
Michal Vasko40a00082020-05-27 15:20:01 +02001983 /* allocate space for the transformed pattern */
1984 size = strlen(pattern) + 1;
1985 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001986 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001987 perl_regex[0] = '\0';
1988
Michal Vasko40a00082020-05-27 15:20:01 +02001989 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1990 brack = 0;
1991 idx = 0;
1992 orig_ptr = pattern;
1993 while (orig_ptr[0]) {
1994 switch (orig_ptr[0]) {
1995 case '$':
1996 case '^':
1997 if (!brack) {
1998 /* make space for the extra character */
1999 ++size;
2000 perl_regex = ly_realloc(perl_regex, size);
2001 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002002
Michal Vasko40a00082020-05-27 15:20:01 +02002003 /* print escape slash */
2004 perl_regex[idx] = '\\';
2005 ++idx;
2006 }
2007 break;
2008 case '[':
2009 /* must not be escaped */
2010 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2011 ++brack;
2012 }
2013 break;
2014 case ']':
2015 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2016 /* pattern was checked and compiled already */
2017 assert(brack);
2018 --brack;
2019 }
2020 break;
2021 default:
2022 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002023 }
Michal Vasko40a00082020-05-27 15:20:01 +02002024
2025 /* copy char */
2026 perl_regex[idx] = orig_ptr[0];
2027
2028 ++idx;
2029 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002030 }
Michal Vasko40a00082020-05-27 15:20:01 +02002031 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002032
2033 /* substitute Unicode Character Blocks with exact Character Ranges */
2034 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2035 start = ptr - perl_regex;
2036
2037 ptr = strchr(ptr, '}');
2038 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002040 pattern, perl_regex + start + 2, "unterminated character property");
2041 free(perl_regex);
2042 return LY_EVALID;
2043 }
2044 end = (ptr - perl_regex) + 1;
2045
2046 /* need more space */
2047 if (end - start < URANGE_LEN) {
2048 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002049 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002050 }
2051
2052 /* find our range */
2053 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2054 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2055 break;
2056 }
2057 }
2058 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002059 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002060 pattern, perl_regex + start + 5, "unknown block name");
2061 free(perl_regex);
2062 return LY_EVALID;
2063 }
2064
2065 /* 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 +02002066 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002067 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002068 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002069 }
2070 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002071 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002072 }
2073 }
Michal Vasko40a00082020-05-27 15:20:01 +02002074 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002075 /* skip brackets */
2076 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2077 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2078 } else {
2079 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2080 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2081 }
2082 }
2083
2084 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002085 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2086 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002087 &err_code, &err_offset, NULL);
2088 if (!code_local) {
2089 PCRE2_UCHAR err_msg[256] = {0};
2090 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002091 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002092 free(perl_regex);
2093 return LY_EVALID;
2094 }
2095 free(perl_regex);
2096
Radek Krejci54579462019-04-30 12:47:06 +02002097 if (code) {
2098 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002099 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002100 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002101 }
2102
2103 return LY_SUCCESS;
2104
2105#undef URANGE_LEN
2106}
2107
Radek Krejcia3045382018-11-22 14:30:31 +01002108/**
2109 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2110 * @param[in] ctx Compile context.
2111 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002112 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2113 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2114 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2115 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2116 */
Radek Krejci19a96102018-11-15 13:38:09 +01002117static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002118lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002119 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2120{
2121 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002122 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002123 LY_ERR ret = LY_SUCCESS;
2124
2125 /* first, copy the patterns from the base type */
2126 if (base_patterns) {
2127 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2128 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2129 }
2130
2131 LY_ARRAY_FOR(patterns_p, u) {
2132 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2133 *pattern = calloc(1, sizeof **pattern);
2134 ++(*pattern)->refcount;
2135
Michal Vasko03ff5a72019-09-11 13:49:33 +02002136 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002137 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002138
2139 if (patterns_p[u].arg[0] == 0x15) {
2140 (*pattern)->inverted = 1;
2141 }
Radek Krejci54579462019-04-30 12:47:06 +02002142 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002143 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2144 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002145 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2146 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002147 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002148 }
2149done:
2150 return ret;
2151}
2152
Radek Krejcia3045382018-11-22 14:30:31 +01002153/**
2154 * @brief map of the possible restrictions combination for the specific built-in type.
2155 */
Radek Krejci19a96102018-11-15 13:38:09 +01002156static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2157 0 /* LY_TYPE_UNKNOWN */,
2158 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002159 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2160 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2161 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2162 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2163 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002164 LYS_SET_BIT /* LY_TYPE_BITS */,
2165 0 /* LY_TYPE_BOOL */,
2166 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2167 0 /* LY_TYPE_EMPTY */,
2168 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2169 LYS_SET_BASE /* LY_TYPE_IDENT */,
2170 LYS_SET_REQINST /* LY_TYPE_INST */,
2171 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002172 LYS_SET_TYPE /* LY_TYPE_UNION */,
2173 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002174 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002175 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002176 LYS_SET_RANGE /* LY_TYPE_INT64 */
2177};
2178
2179/**
2180 * @brief stringification of the YANG built-in data types
2181 */
2182const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2183 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2184 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002185};
2186
Radek Krejcia3045382018-11-22 14:30:31 +01002187/**
2188 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2189 * @param[in] ctx Compile context.
2190 * @param[in] enums_p Array of the parsed enum structures to compile.
2191 * @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 +01002192 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2193 * @param[out] enums Newly created array of the compiled enums information for the current type.
2194 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2195 */
Radek Krejci19a96102018-11-15 13:38:09 +01002196static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002197lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002198 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002199{
2200 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002201 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002202 int32_t value = 0;
2203 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002204 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002205
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002206 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002207 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2208 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2209 return LY_EVALID;
2210 }
2211
2212 LY_ARRAY_FOR(enums_p, u) {
2213 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2214 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002215 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2216 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002217 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002218 if (base_enums) {
2219 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2220 LY_ARRAY_FOR(base_enums, v) {
2221 if (!strcmp(e->name, base_enums[v].name)) {
2222 break;
2223 }
2224 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002225 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002226 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2227 "Invalid %s - derived type adds new item \"%s\".",
2228 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2229 return LY_EVALID;
2230 }
2231 match = v;
2232 }
2233
2234 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002235 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002236 if (enums_p[u].flags & LYS_SET_VALUE) {
2237 e->value = (int32_t)enums_p[u].value;
2238 if (!u || e->value >= value) {
2239 value = e->value + 1;
2240 }
2241 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002242 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002243 if (e->value == (*enums)[v].value) {
2244 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2245 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2246 e->value, e->name, (*enums)[v].name);
2247 return LY_EVALID;
2248 }
2249 }
2250 } else if (base_enums) {
2251 /* inherit the assigned value */
2252 e->value = base_enums[match].value;
2253 if (!u || e->value >= value) {
2254 value = e->value + 1;
2255 }
2256 } else {
2257 /* assign value automatically */
2258 if (u && value == INT32_MIN) {
2259 /* counter overflow */
2260 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2261 "Invalid enumeration - it is not possible to auto-assign enum value for "
2262 "\"%s\" since the highest value is already 2147483647.", e->name);
2263 return LY_EVALID;
2264 }
2265 e->value = value++;
2266 }
2267 } else { /* LY_TYPE_BITS */
2268 if (enums_p[u].flags & LYS_SET_VALUE) {
2269 e->value = (int32_t)enums_p[u].value;
2270 if (!u || (uint32_t)e->value >= position) {
2271 position = (uint32_t)e->value + 1;
2272 }
2273 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002274 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002275 if (e->value == (*enums)[v].value) {
2276 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2277 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2278 (uint32_t)e->value, e->name, (*enums)[v].name);
2279 return LY_EVALID;
2280 }
2281 }
2282 } else if (base_enums) {
2283 /* inherit the assigned value */
2284 e->value = base_enums[match].value;
2285 if (!u || (uint32_t)e->value >= position) {
2286 position = (uint32_t)e->value + 1;
2287 }
2288 } else {
2289 /* assign value automatically */
2290 if (u && position == 0) {
2291 /* counter overflow */
2292 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2293 "Invalid bits - it is not possible to auto-assign bit position for "
2294 "\"%s\" since the highest value is already 4294967295.", e->name);
2295 return LY_EVALID;
2296 }
2297 e->value = position++;
2298 }
2299 }
2300
2301 if (base_enums) {
2302 /* the assigned values must not change from the derived type */
2303 if (e->value != base_enums[match].value) {
2304 if (basetype == LY_TYPE_ENUM) {
2305 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2306 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2307 e->name, base_enums[match].value, e->value);
2308 } else {
2309 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2310 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2311 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2312 }
2313 return LY_EVALID;
2314 }
2315 }
2316
Radek Krejciec4da802019-05-02 13:02:41 +02002317 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002318 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 +01002319
2320 if (basetype == LY_TYPE_BITS) {
2321 /* keep bits ordered by position */
2322 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2323 if (v != u) {
2324 memcpy(&storage, e, sizeof *e);
2325 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2326 memcpy(&(*enums)[v], &storage, sizeof storage);
2327 }
2328 }
2329 }
2330
2331done:
2332 return ret;
2333}
2334
Radek Krejcia3045382018-11-22 14:30:31 +01002335/**
2336 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2337 *
2338 * path-arg = absolute-path / relative-path
2339 * absolute-path = 1*("/" (node-identifier *path-predicate))
2340 * relative-path = 1*(".." "/") descendant-path
2341 *
2342 * @param[in,out] path Path to parse.
2343 * @param[out] prefix Prefix of the token, NULL if there is not any.
2344 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2345 * @param[out] name Name of the token.
2346 * @param[out] nam_len Length of the name.
2347 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2348 * must not be changed between consecutive calls. -1 if the
2349 * path is absolute.
2350 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2351 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2352 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002353LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002354lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2355 int *parent_times, int *has_predicate)
2356{
2357 int par_times = 0;
2358
2359 assert(path && *path);
2360 assert(parent_times);
2361 assert(prefix);
2362 assert(prefix_len);
2363 assert(name);
2364 assert(name_len);
2365 assert(has_predicate);
2366
2367 *prefix = NULL;
2368 *prefix_len = 0;
2369 *name = NULL;
2370 *name_len = 0;
2371 *has_predicate = 0;
2372
2373 if (!*parent_times) {
2374 if (!strncmp(*path, "..", 2)) {
2375 *path += 2;
2376 ++par_times;
2377 while (!strncmp(*path, "/..", 3)) {
2378 *path += 3;
2379 ++par_times;
2380 }
2381 }
2382 if (par_times) {
2383 *parent_times = par_times;
2384 } else {
2385 *parent_times = -1;
2386 }
2387 }
2388
2389 if (**path != '/') {
2390 return LY_EINVAL;
2391 }
2392 /* skip '/' */
2393 ++(*path);
2394
2395 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002396 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002397
2398 if ((**path == '/' && (*path)[1]) || !**path) {
2399 /* path continues by another token or this is the last token */
2400 return LY_SUCCESS;
2401 } else if ((*path)[0] != '[') {
2402 /* unexpected character */
2403 return LY_EINVAL;
2404 } else {
2405 /* predicate starting with [ */
2406 *has_predicate = 1;
2407 return LY_SUCCESS;
2408 }
2409}
2410
2411/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002412 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2413 *
2414 * The set of features used for target must be a subset of features used for the leafref.
2415 * This is not a perfect, we should compare the truth tables but it could require too much resources
2416 * and RFC 7950 does not require it explicitely, so we simplify that.
2417 *
2418 * @param[in] refnode The leafref node.
2419 * @param[in] target Tha target node of the leafref.
2420 * @return LY_SUCCESS or LY_EVALID;
2421 */
2422static LY_ERR
2423lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2424{
2425 LY_ERR ret = LY_EVALID;
2426 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002427 LY_ARRAY_COUNT_TYPE u, v, count;
Radek Krejci58d171e2018-11-23 13:50:55 +01002428 struct ly_set features = {0};
2429
2430 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002431 if (iter->iffeatures) {
2432 LY_ARRAY_FOR(iter->iffeatures, u) {
2433 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2434 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002435 }
2436 }
2437 }
2438 }
2439
2440 /* we should have, in features set, a superset of features applicable to the target node.
2441 * So when adding features applicable to the target into the features set, we should not be
2442 * able to actually add any new feature, otherwise it is not a subset of features applicable
2443 * to the leafref itself. */
2444 count = features.count;
2445 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002446 if (iter->iffeatures) {
2447 LY_ARRAY_FOR(iter->iffeatures, u) {
2448 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2449 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002450 /* new feature was added (or LY_EMEM) */
2451 goto cleanup;
2452 }
2453 }
2454 }
2455 }
2456 }
2457 ret = LY_SUCCESS;
2458
2459cleanup:
2460 ly_set_erase(&features, NULL);
2461 return ret;
2462}
2463
Michal Vasko004d3152020-06-11 19:59:22 +02002464static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2465 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2466 struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002467
Radek Krejcia3045382018-11-22 14:30:31 +01002468/**
2469 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2470 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002471 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2472 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2473 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2474 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002475 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002476 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002477 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002478 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2479 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2480 * @param[out] type Newly created type structure with the filled information about the type.
2481 * @return LY_ERR value.
2482 */
Radek Krejci19a96102018-11-15 13:38:09 +01002483static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002484lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2485 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2486 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2487 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002488{
2489 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002490 struct lysc_type_bin *bin;
2491 struct lysc_type_num *num;
2492 struct lysc_type_str *str;
2493 struct lysc_type_bits *bits;
2494 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002495 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002496 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002497 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002498 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002499
2500 switch (basetype) {
2501 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002502 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002503
2504 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002505 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002506 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2507 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002508 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002509 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 +01002510 }
2511 }
2512
2513 if (tpdfname) {
2514 type_p->compiled = *type;
2515 *type = calloc(1, sizeof(struct lysc_type_bin));
2516 }
2517 break;
2518 case LY_TYPE_BITS:
2519 /* RFC 7950 9.7 - bits */
2520 bits = (struct lysc_type_bits*)(*type);
2521 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002522 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002523 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2524 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002525 }
2526
Radek Krejci555cb5b2018-11-16 14:54:33 +01002527 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002528 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002529 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002530 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002531 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002532 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002533 free(*type);
2534 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002535 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002536 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002537 }
2538
2539 if (tpdfname) {
2540 type_p->compiled = *type;
2541 *type = calloc(1, sizeof(struct lysc_type_bits));
2542 }
2543 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002544 case LY_TYPE_DEC64:
2545 dec = (struct lysc_type_dec*)(*type);
2546
2547 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002548 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002549 if (!type_p->fraction_digits) {
2550 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002552 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002553 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002554 free(*type);
2555 *type = NULL;
2556 }
2557 return LY_EVALID;
2558 }
2559 } else if (type_p->fraction_digits) {
2560 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002561 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002562 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2563 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002564 tpdfname);
2565 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002566 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2567 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002568 free(*type);
2569 *type = NULL;
2570 }
2571 return LY_EVALID;
2572 }
2573 dec->fraction_digits = type_p->fraction_digits;
2574
2575 /* RFC 7950 9.2.4 - range */
2576 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002577 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2578 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002579 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002580 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 +01002581 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002582 }
2583
2584 if (tpdfname) {
2585 type_p->compiled = *type;
2586 *type = calloc(1, sizeof(struct lysc_type_dec));
2587 }
2588 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002589 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002590 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002591
2592 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002593 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002594 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2595 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002596 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002597 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 +01002598 }
2599 } else if (base && ((struct lysc_type_str*)base)->length) {
2600 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2601 }
2602
2603 /* RFC 7950 9.4.5 - pattern */
2604 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002605 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002606 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002607 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2608 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2609 }
2610
2611 if (tpdfname) {
2612 type_p->compiled = *type;
2613 *type = calloc(1, sizeof(struct lysc_type_str));
2614 }
2615 break;
2616 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002617 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002618
2619 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002620 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002621 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002622 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002623 }
2624
Radek Krejci555cb5b2018-11-16 14:54:33 +01002625 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002626 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002627 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002628 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002629 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002630 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002631 free(*type);
2632 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002633 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002634 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002635 }
2636
2637 if (tpdfname) {
2638 type_p->compiled = *type;
2639 *type = calloc(1, sizeof(struct lysc_type_enum));
2640 }
2641 break;
2642 case LY_TYPE_INT8:
2643 case LY_TYPE_UINT8:
2644 case LY_TYPE_INT16:
2645 case LY_TYPE_UINT16:
2646 case LY_TYPE_INT32:
2647 case LY_TYPE_UINT32:
2648 case LY_TYPE_INT64:
2649 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002650 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002651
2652 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002653 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002654 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2655 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002656 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002657 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 +01002658 }
2659 }
2660
2661 if (tpdfname) {
2662 type_p->compiled = *type;
2663 *type = calloc(1, sizeof(struct lysc_type_num));
2664 }
2665 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002666 case LY_TYPE_IDENT:
2667 idref = (struct lysc_type_identityref*)(*type);
2668
2669 /* RFC 7950 9.10.2 - base */
2670 if (type_p->bases) {
2671 if (base) {
2672 /* only the directly derived identityrefs can contain base specification */
2673 if (tpdfname) {
2674 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002675 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002676 tpdfname);
2677 } else {
2678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002679 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002680 free(*type);
2681 *type = NULL;
2682 }
2683 return LY_EVALID;
2684 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002685 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002686 }
2687
2688 if (!base && !type_p->flags) {
2689 /* type derived from identityref built-in type must contain at least one base */
2690 if (tpdfname) {
2691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2692 } else {
2693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2694 free(*type);
2695 *type = NULL;
2696 }
2697 return LY_EVALID;
2698 }
2699
2700 if (tpdfname) {
2701 type_p->compiled = *type;
2702 *type = calloc(1, sizeof(struct lysc_type_identityref));
2703 }
2704 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002705 case LY_TYPE_LEAFREF:
Michal Vasko004d3152020-06-11 19:59:22 +02002706 lref = (struct lysc_type_leafref*)*type;
2707
Radek Krejcia3045382018-11-22 14:30:31 +01002708 /* RFC 7950 9.9.3 - require-instance */
2709 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002710 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002711 if (tpdfname) {
2712 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2713 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2714 } else {
2715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2716 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2717 free(*type);
2718 *type = NULL;
2719 }
2720 return LY_EVALID;
2721 }
Michal Vasko004d3152020-06-11 19:59:22 +02002722 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002723 } else if (base) {
2724 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002725 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002726 } else {
2727 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002728 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002729 }
2730 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002731 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2732 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002733 } else if (base) {
Michal Vasko004d3152020-06-11 19:59:22 +02002734 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path);
2735 lref->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002736 } else if (tpdfname) {
2737 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2738 return LY_EVALID;
2739 } else {
2740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2741 free(*type);
2742 *type = NULL;
2743 return LY_EVALID;
2744 }
2745 if (tpdfname) {
2746 type_p->compiled = *type;
2747 *type = calloc(1, sizeof(struct lysc_type_leafref));
2748 }
2749 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002750 case LY_TYPE_INST:
2751 /* RFC 7950 9.9.3 - require-instance */
2752 if (type_p->flags & LYS_SET_REQINST) {
2753 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2754 } else {
2755 /* default is true */
2756 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2757 }
2758
2759 if (tpdfname) {
2760 type_p->compiled = *type;
2761 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2762 }
2763 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002764 case LY_TYPE_UNION:
2765 un = (struct lysc_type_union*)(*type);
2766
2767 /* RFC 7950 7.4 - type */
2768 if (type_p->types) {
2769 if (base) {
2770 /* only the directly derived union can contain types specification */
2771 if (tpdfname) {
2772 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2773 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2774 tpdfname);
2775 } else {
2776 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2777 "Invalid type substatement for the type not directly derived from union built-in type.");
2778 free(*type);
2779 *type = NULL;
2780 }
2781 return LY_EVALID;
2782 }
2783 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002784 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2785 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002786 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
2787 &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002788 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2789 /* add space for additional types from the union subtype */
2790 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002791 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t*)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1,
Radek Krejcic4fa0292020-05-14 10:54:49 +02002792 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002793
2794 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002795 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002796 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2797 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2798 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2799 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 +02002800 lref = (struct lysc_type_leafref *)un->types[u + additional];
2801
2802 lref->basetype = LY_TYPE_LEAFREF;
2803 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path);
2804 lref->refcount = 1;
2805 lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2806 lref->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002807 /* TODO extensions */
2808
2809 } else {
2810 un->types[u + additional] = un_aux->types[v];
2811 ++un_aux->types[v]->refcount;
2812 }
2813 ++additional;
2814 LY_ARRAY_INCREMENT(un->types);
2815 }
2816 /* compensate u increment in main loop */
2817 --additional;
2818
2819 /* free the replaced union subtype */
2820 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2821 } else {
2822 LY_ARRAY_INCREMENT(un->types);
2823 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002824 }
2825 }
2826
2827 if (!base && !type_p->flags) {
2828 /* type derived from union built-in type must contain at least one type */
2829 if (tpdfname) {
2830 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2831 } else {
2832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2833 free(*type);
2834 *type = NULL;
2835 }
2836 return LY_EVALID;
2837 }
2838
2839 if (tpdfname) {
2840 type_p->compiled = *type;
2841 *type = calloc(1, sizeof(struct lysc_type_union));
2842 }
2843 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002844 case LY_TYPE_BOOL:
2845 case LY_TYPE_EMPTY:
2846 case LY_TYPE_UNKNOWN: /* just to complete switch */
2847 break;
2848 }
2849 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2850done:
2851 return ret;
2852}
2853
Radek Krejcia3045382018-11-22 14:30:31 +01002854/**
2855 * @brief Compile information about the leaf/leaf-list's type.
2856 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002857 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2858 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2859 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2860 * @param[in] context_name Name of the context node or referencing typedef for logging.
2861 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002862 * @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 +01002863 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002864 * @return LY_ERR value.
2865 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002866static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002867lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2868 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2869 struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002870{
2871 LY_ERR ret = LY_SUCCESS;
2872 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002873 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002874 struct type_context {
2875 const struct lysp_tpdf *tpdf;
2876 struct lysp_node *node;
2877 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002878 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002879 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002880 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002881 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002882 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002883 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002884
2885 (*type) = NULL;
2886
2887 tctx = calloc(1, sizeof *tctx);
2888 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002889 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002890 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2891 ret == LY_SUCCESS;
2892 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2893 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2894 if (basetype) {
2895 break;
2896 }
2897
2898 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002899 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2900 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002901 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2902
Radek Krejcicdfecd92018-11-26 11:27:32 +01002903 if (units && !*units) {
2904 /* inherit units */
2905 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2906 }
Radek Krejci01342af2019-01-03 15:18:08 +01002907 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002908 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002909 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02002910 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002911 }
Radek Krejci01342af2019-01-03 15:18:08 +01002912 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002913 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2914 break;
2915 }
2916
Radek Krejci19a96102018-11-15 13:38:09 +01002917 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002918 /* it is not necessary to continue, the rest of the chain was already compiled,
2919 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002920 basetype = tctx->tpdf->type.compiled->basetype;
2921 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002922 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002923 dummyloops = 1;
2924 goto preparenext;
2925 } else {
2926 tctx = NULL;
2927 break;
2928 }
Radek Krejci19a96102018-11-15 13:38:09 +01002929 }
2930
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002931 /* circular typedef reference detection */
2932 for (u = 0; u < tpdf_chain.count; u++) {
2933 /* local part */
2934 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2935 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2936 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2937 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2938 free(tctx);
2939 ret = LY_EVALID;
2940 goto cleanup;
2941 }
2942 }
2943 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2944 /* global part for unions corner case */
2945 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2946 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2947 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2948 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2949 free(tctx);
2950 ret = LY_EVALID;
2951 goto cleanup;
2952 }
2953 }
2954
Radek Krejci19a96102018-11-15 13:38:09 +01002955 /* store information for the following processing */
2956 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2957
Radek Krejcicdfecd92018-11-26 11:27:32 +01002958preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002959 /* prepare next loop */
2960 tctx_prev = tctx;
2961 tctx = calloc(1, sizeof *tctx);
2962 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2963 }
2964 free(tctx);
2965
2966 /* allocate type according to the basetype */
2967 switch (basetype) {
2968 case LY_TYPE_BINARY:
2969 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002970 break;
2971 case LY_TYPE_BITS:
2972 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002973 break;
2974 case LY_TYPE_BOOL:
2975 case LY_TYPE_EMPTY:
2976 *type = calloc(1, sizeof(struct lysc_type));
2977 break;
2978 case LY_TYPE_DEC64:
2979 *type = calloc(1, sizeof(struct lysc_type_dec));
2980 break;
2981 case LY_TYPE_ENUM:
2982 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002983 break;
2984 case LY_TYPE_IDENT:
2985 *type = calloc(1, sizeof(struct lysc_type_identityref));
2986 break;
2987 case LY_TYPE_INST:
2988 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2989 break;
2990 case LY_TYPE_LEAFREF:
2991 *type = calloc(1, sizeof(struct lysc_type_leafref));
2992 break;
2993 case LY_TYPE_STRING:
2994 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002995 break;
2996 case LY_TYPE_UNION:
2997 *type = calloc(1, sizeof(struct lysc_type_union));
2998 break;
2999 case LY_TYPE_INT8:
3000 case LY_TYPE_UINT8:
3001 case LY_TYPE_INT16:
3002 case LY_TYPE_UINT16:
3003 case LY_TYPE_INT32:
3004 case LY_TYPE_UINT32:
3005 case LY_TYPE_INT64:
3006 case LY_TYPE_UINT64:
3007 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003008 break;
3009 case LY_TYPE_UNKNOWN:
3010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3011 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3012 ret = LY_EVALID;
3013 goto cleanup;
3014 }
3015 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003016 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003017 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3018 ly_data_type2str[basetype]);
3019 free(*type);
3020 (*type) = NULL;
3021 ret = LY_EVALID;
3022 goto cleanup;
3023 }
3024
3025 /* get restrictions from the referred typedefs */
3026 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3027 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003028
3029 /* remember the typedef context for circular check */
3030 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3031
Radek Krejci43699232018-11-23 14:59:46 +01003032 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003033 base = tctx->tpdf->type.compiled;
3034 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003035 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003036 /* no change, just use the type information from the base */
3037 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3038 ++base->refcount;
3039 continue;
3040 }
3041
3042 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003043 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3044 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3045 tctx->tpdf->name, ly_data_type2str[basetype]);
3046 ret = LY_EVALID;
3047 goto cleanup;
3048 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3049 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3050 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3051 tctx->tpdf->name, tctx->tpdf->dflt);
3052 ret = LY_EVALID;
3053 goto cleanup;
3054 }
3055
Radek Krejci19a96102018-11-15 13:38:09 +01003056 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003057 /* TODO user type plugins */
3058 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003059 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003060 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 +01003061 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003062 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003063 LY_CHECK_GOTO(ret, cleanup);
3064 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003065 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003066 /* remove the processed typedef contexts from the stack for circular check */
3067 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003068
Radek Krejcic5c27e52018-11-15 14:38:11 +01003069 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003070 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003071 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003072 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003073 /* TODO user type plugins */
3074 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003075 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003076 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 +01003077 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003078 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003079 /* no specific restriction in leaf's type definition, copy from the base */
3080 free(*type);
3081 (*type) = base;
3082 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003083 }
Radek Krejcia1911222019-07-22 17:24:50 +02003084 if (dflt && !(*type)->dflt) {
3085 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003086 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003087 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3088 (*type)->dflt->realtype = (*type);
3089 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3090 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003091 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003092 if (err) {
3093 ly_err_print(err);
3094 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3095 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3096 ly_err_free(err);
3097 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003098 if (ret == LY_EINCOMPLETE) {
3099 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003100 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003101
3102 /* but in general result is so far ok */
3103 ret = LY_SUCCESS;
3104 }
Radek Krejcia1911222019-07-22 17:24:50 +02003105 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003106 }
Radek Krejci19a96102018-11-15 13:38:09 +01003107
Radek Krejci0935f412019-08-20 16:15:18 +02003108 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003109
3110cleanup:
3111 ly_set_erase(&tpdf_chain, free);
3112 return ret;
3113}
3114
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003115/**
3116 * @brief Compile status information of the given node.
3117 *
3118 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3119 * has the status correctly set during the compilation.
3120 *
3121 * @param[in] ctx Compile context
3122 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3123 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3124 * the compatibility with the parent's status value.
3125 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3126 * @return LY_ERR value.
3127 */
3128static LY_ERR
3129lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3130{
3131 /* status - it is not inherited by specification, but it does not make sense to have
3132 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3133 if (!((*node_flags) & LYS_STATUS_MASK)) {
3134 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3135 if ((parent_flags & 0x3) != 0x3) {
3136 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3137 * combination of bits (0x3) which marks the uses_status value */
3138 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3139 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3140 }
3141 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3142 } else {
3143 (*node_flags) |= LYS_STATUS_CURR;
3144 }
3145 } else if (parent_flags & LYS_STATUS_MASK) {
3146 /* check status compatibility with the parent */
3147 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3148 if ((*node_flags) & LYS_STATUS_CURR) {
3149 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3150 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3151 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3152 } else { /* LYS_STATUS_DEPRC */
3153 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3154 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3155 }
3156 return LY_EVALID;
3157 }
3158 }
3159 return LY_SUCCESS;
3160}
3161
Radek Krejci8cce8532019-03-05 11:27:45 +01003162/**
3163 * @brief Check uniqness of the node/action/notification name.
3164 *
3165 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3166 * structures, but they share the namespace so we need to check their name collisions.
3167 *
3168 * @param[in] ctx Compile context.
3169 * @param[in] children List (linked list) of data nodes to go through.
3170 * @param[in] actions List (sized array) of actions or RPCs to go through.
3171 * @param[in] notifs List (sized array) of Notifications to go through.
3172 * @param[in] name Name of the item to find in the given lists.
3173 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3174 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3175 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3176 */
3177static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02003178lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions,
3179 const struct lysc_notif *notifs, const char *name, void *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003180{
3181 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003182 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003183
3184 LY_LIST_FOR(children, iter) {
3185 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3186 goto error;
3187 }
3188 }
3189 LY_ARRAY_FOR(actions, u) {
3190 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3191 goto error;
3192 }
3193 }
3194 LY_ARRAY_FOR(notifs, u) {
3195 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3196 goto error;
3197 }
3198 }
3199 return LY_SUCCESS;
3200error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003201 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003202 return LY_EEXIST;
3203}
3204
Radek Krejciec4da802019-05-02 13:02:41 +02003205static 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 +01003206
Radek Krejcia3045382018-11-22 14:30:31 +01003207/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003208 * @brief Compile parsed RPC/action schema node information.
3209 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003210 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003211 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003212 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3213 * @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).
3214 * Zero means no uses, non-zero value with no status bit set mean the default status.
3215 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3216 */
3217static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003218lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003219 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3220{
3221 LY_ERR ret = LY_SUCCESS;
3222 struct lysp_node *child_p;
3223 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003224 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003225
Radek Krejci327de162019-06-14 12:52:07 +02003226 lysc_update_path(ctx, parent, action_p->name);
3227
Radek Krejci8cce8532019-03-05 11:27:45 +01003228 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3229 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3230 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3231 action_p->name, action)) {
3232 return LY_EVALID;
3233 }
3234
Radek Krejciec4da802019-05-02 13:02:41 +02003235 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003236 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003237 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003238 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003239 return LY_EVALID;
3240 }
3241
Michal Vasko1bf09392020-03-27 12:38:10 +01003242 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003243 action->module = ctx->mod;
3244 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003245 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003246 action->sp = action_p;
3247 }
3248 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3249
3250 /* status - it is not inherited by specification, but it does not make sense to have
3251 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003252 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003253
3254 DUP_STRING(ctx->ctx, action_p->name, action->name);
3255 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3256 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003257 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003258 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003259
3260 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003261 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003262 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003263 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 +02003264 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003265 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003266 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003267 }
Radek Krejci327de162019-06-14 12:52:07 +02003268 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003269 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003270
3271 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003272 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003273 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003274 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 +02003275 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003276 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003277 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003278 }
Radek Krejci327de162019-06-14 12:52:07 +02003279 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003280 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003281
3282 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3283 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003284 ly_set_add(&ctx->xpath, action, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003285 }
3286
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003287cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003288 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003289 return ret;
3290}
3291
3292/**
Radek Krejci43981a32019-04-12 09:44:11 +02003293 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003294 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003295 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003296 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3297 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3298 * @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 +02003299 * Zero means no uses, non-zero value with no status bit set mean the default status.
3300 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3301 */
3302static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003303lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003304 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3305{
3306 LY_ERR ret = LY_SUCCESS;
3307 struct lysp_node *child_p;
3308 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003309 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003310
Radek Krejci327de162019-06-14 12:52:07 +02003311 lysc_update_path(ctx, parent, notif_p->name);
3312
Radek Krejcifc11bd72019-04-11 16:00:05 +02003313 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3314 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3315 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3316 notif_p->name, notif)) {
3317 return LY_EVALID;
3318 }
3319
Radek Krejciec4da802019-05-02 13:02:41 +02003320 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003321 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3322 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003323 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003324 return LY_EVALID;
3325 }
3326
3327 notif->nodetype = LYS_NOTIF;
3328 notif->module = ctx->mod;
3329 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003330 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003331 notif->sp = notif_p;
3332 }
3333 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3334
3335 /* status - it is not inherited by specification, but it does not make sense to have
3336 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3337 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3338
3339 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3340 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3341 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003342 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003343 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003344 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3345 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003346 ly_set_add(&ctx->xpath, notif, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003347 }
Radek Krejci0935f412019-08-20 16:15:18 +02003348 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003349
Radek Krejciec4da802019-05-02 13:02:41 +02003350 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003351 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003352 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003353 }
3354
Radek Krejci327de162019-06-14 12:52:07 +02003355 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003356cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003357 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003358 return ret;
3359}
3360
3361/**
Radek Krejcia3045382018-11-22 14:30:31 +01003362 * @brief Compile parsed container node information.
3363 * @param[in] ctx Compile context
3364 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003365 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3366 * is enriched with the container-specific information.
3367 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3368 */
Radek Krejci19a96102018-11-15 13:38:09 +01003369static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003370lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003371{
3372 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3373 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3374 struct lysp_node *child_p;
3375 unsigned int u;
3376 LY_ERR ret = LY_SUCCESS;
3377
Radek Krejcife909632019-02-12 15:34:42 +01003378 if (cont_p->presence) {
3379 cont->flags |= LYS_PRESENCE;
3380 }
3381
Radek Krejci19a96102018-11-15 13:38:09 +01003382 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003383 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003384 }
3385
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003386 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003387 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3388 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003389 ly_set_add(&ctx->xpath, cont, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003390 }
Radek Krejciec4da802019-05-02 13:02:41 +02003391 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3392 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003393
3394done:
3395 return ret;
3396}
3397
Radek Krejci33f72892019-02-21 10:36:58 +01003398/*
3399 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3400 * @param[in] ctx Compile context.
3401 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3402 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003403 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3404 * @return LY_ERR value.
3405 */
3406static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003407lys_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 +01003408{
Radek Krejci33f72892019-02-21 10:36:58 +01003409 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3410
Radek Krejciec4da802019-05-02 13:02:41 +02003411 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 +01003412 leaf->units ? NULL : &leaf->units));
3413 if (leaf->nodetype == LYS_LEAFLIST) {
3414 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003415 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3416 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003417 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003418 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3419 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3420 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3421 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003422 LY_ARRAY_INCREMENT(llist->dflts);
3423 }
3424 } else {
3425 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003426 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003427 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3428 leaf->dflt->realtype = leaf->type->dflt->realtype;
3429 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3430 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003431 }
3432 }
3433 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3434 /* 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 +02003435 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003436 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003437 LY_ARRAY_COUNT_TYPE u;
Radek Krejci33f72892019-02-21 10:36:58 +01003438 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3439 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3440 /* 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 +02003441 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003442 }
3443 }
3444 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003445 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3446 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3447 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3448 return LY_EVALID;
3449 }
3450 }
3451
Radek Krejci33f72892019-02-21 10:36:58 +01003452 return LY_SUCCESS;
3453}
3454
Radek Krejcia3045382018-11-22 14:30:31 +01003455/**
3456 * @brief Compile parsed leaf node information.
3457 * @param[in] ctx Compile context
3458 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003459 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3460 * is enriched with the leaf-specific information.
3461 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3462 */
Radek Krejci19a96102018-11-15 13:38:09 +01003463static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003464lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003465{
3466 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3467 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3468 unsigned int u;
3469 LY_ERR ret = LY_SUCCESS;
3470
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003471 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003472 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3473 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003474 ly_set_add(&ctx->xpath, leaf, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003475 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003476 if (leaf_p->units) {
3477 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3478 leaf->flags |= LYS_SET_UNITS;
3479 }
Radek Krejcia1911222019-07-22 17:24:50 +02003480
3481 /* the dflt member is just filled to avoid getting the default value from the type */
3482 leaf->dflt = (void*)leaf_p->dflt;
3483 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003484 if (ret) {
3485 leaf->dflt = NULL;
3486 return ret;
3487 }
Radek Krejcia1911222019-07-22 17:24:50 +02003488
Radek Krejciccd20f12019-02-15 14:12:27 +01003489 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003490 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003491 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003492 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3493 leaf->dflt->realtype = leaf->type;
3494 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3495 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003496 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003497 leaf->dflt->realtype->refcount++;
3498 if (err) {
3499 ly_err_print(err);
3500 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3501 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3502 ly_err_free(err);
3503 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003504 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003505 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003506 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003507
3508 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003509 ret = LY_SUCCESS;
3510 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003511 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003512 leaf->flags |= LYS_SET_DFLT;
3513 }
Radek Krejci43699232018-11-23 14:59:46 +01003514
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003515
Radek Krejci19a96102018-11-15 13:38:09 +01003516done:
3517 return ret;
3518}
3519
Radek Krejcia3045382018-11-22 14:30:31 +01003520/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003521 * @brief Compile parsed leaf-list node information.
3522 * @param[in] ctx Compile context
3523 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003524 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3525 * is enriched with the leaf-list-specific information.
3526 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3527 */
3528static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003529lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003530{
3531 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3532 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003533 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003534 LY_ERR ret = LY_SUCCESS;
3535
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003536 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003537 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3538 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003539 ly_set_add(&ctx->xpath, llist, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003540 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003541 if (llist_p->units) {
3542 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3543 llist->flags |= LYS_SET_UNITS;
3544 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003545
Radek Krejcia1911222019-07-22 17:24:50 +02003546 /* the dflts member is just filled to avoid getting the default value from the type */
3547 llist->dflts = (void*)llist_p->dflts;
3548 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Michal Vasko6a044b22020-01-15 12:25:39 +01003549 if (ret != LY_SUCCESS) {
3550 /* make sure the defaults are freed correctly */
3551 if (llist_p->dflts) {
3552 llist->dflts = NULL;
3553 }
3554 return ret;
3555 }
3556
Radek Krejci0e5d8382018-11-28 16:37:53 +01003557 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003558 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003559 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
3560 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003561 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003562 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003563 LY_ARRAY_INCREMENT(llist->dflts_mods);
3564 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003565 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003566 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3567 llist->dflts[u]->realtype = llist->type;
3568 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3569 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003570 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003571 llist->dflts[u]->realtype->refcount++;
3572 if (err) {
3573 ly_err_print(err);
3574 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3575 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3576 ly_err_free(err);
3577 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003578 if (ret == LY_EINCOMPLETE) {
3579 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003580 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003581
3582 /* but in general result is so far ok */
3583 ret = LY_SUCCESS;
3584 }
Radek Krejcia1911222019-07-22 17:24:50 +02003585 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003586 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003587 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003588 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003589 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02003590 /* configuration data values must be unique - so check the default values */
3591 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003592 for (v = u + 1; v < LY_ARRAY_COUNT(llist->dflts); ++v) {
Radek Krejcia1911222019-07-22 17:24:50 +02003593 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3594 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003595 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 +02003596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3597 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3598 if (dynamic) {
3599 free((char*)val);
3600 }
3601 return LY_EVALID;
3602 }
3603 }
3604 }
3605 }
3606
3607 /* 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 +01003608
3609 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003610 if (llist->min) {
3611 llist->flags |= LYS_MAND_TRUE;
3612 }
Radek Krejcib7408632018-11-28 17:12:11 +01003613 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003614
Radek Krejci0e5d8382018-11-28 16:37:53 +01003615done:
3616 return ret;
3617}
3618
3619/**
Radek Krejci7af64242019-02-18 13:07:53 +01003620 * @brief Compile information about list's uniques.
3621 * @param[in] ctx Compile context.
3622 * @param[in] context_module Module where the prefixes are going to be resolved.
3623 * @param[in] uniques Sized array list of unique statements.
3624 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3625 * @return LY_ERR value.
3626 */
3627static LY_ERR
3628lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3629{
3630 LY_ERR ret = LY_SUCCESS;
3631 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003632 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003633 const char *keystr, *delim;
3634 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003635 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7af64242019-02-18 13:07:53 +01003636 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003637 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003638
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003639 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003640 config = -1;
3641 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3642 keystr = uniques[v];
3643 while (keystr) {
3644 delim = strpbrk(keystr, " \t\n");
3645 if (delim) {
3646 len = delim - keystr;
3647 while (isspace(*delim)) {
3648 ++delim;
3649 }
3650 } else {
3651 len = strlen(keystr);
3652 }
3653
3654 /* unique node must be present */
3655 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003656 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3657 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003658 if (ret != LY_SUCCESS) {
3659 if (ret == LY_EDENIED) {
3660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003661 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003662 len, keystr, lys_nodetype2str((*key)->nodetype));
3663 }
3664 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003665 } else if (flags) {
3666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3667 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003668 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003669 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003670 }
3671
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003672
Radek Krejci7af64242019-02-18 13:07:53 +01003673 /* all referenced leafs must be of the same config type */
3674 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003676 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003677 return LY_EVALID;
3678 } else if ((*key)->flags & LYS_CONFIG_W) {
3679 config = 1;
3680 } else { /* LYS_CONFIG_R */
3681 config = 0;
3682 }
3683
Michal Vasko14654712020-02-06 08:35:21 +01003684 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3685 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3686 if (parent->nodetype == LYS_LIST) {
3687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3688 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3689 return LY_EVALID;
3690 }
3691 }
3692
Radek Krejci7af64242019-02-18 13:07:53 +01003693 /* check status */
3694 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3695 (*key)->flags, (*key)->module, (*key)->name));
3696
3697 /* mark leaf as unique */
3698 (*key)->flags |= LYS_UNIQUE;
3699
3700 /* next unique value in line */
3701 keystr = delim;
3702 }
3703 /* next unique definition */
3704 }
3705
3706 return LY_SUCCESS;
3707}
3708
3709/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003710 * @brief Compile parsed list node information.
3711 * @param[in] ctx Compile context
3712 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003713 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3714 * is enriched with the list-specific information.
3715 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3716 */
3717static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003718lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003719{
3720 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3721 struct lysc_node_list *list = (struct lysc_node_list*)node;
3722 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003723 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003724 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003725 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003726 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003727 LY_ERR ret = LY_SUCCESS;
3728
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003729 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003730 if (list->min) {
3731 list->flags |= LYS_MAND_TRUE;
3732 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003733 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3734
3735 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003736 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003737 }
3738
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003739 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003740 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3741 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003742 ly_set_add(&ctx->xpath, list, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003743 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003744
3745 /* keys */
3746 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3748 return LY_EVALID;
3749 }
3750
3751 /* find all the keys (must be direct children) */
3752 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003753 if (!keystr) {
3754 /* keyless list */
3755 list->flags |= LYS_KEYLESS;
3756 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003757 while (keystr) {
3758 delim = strpbrk(keystr, " \t\n");
3759 if (delim) {
3760 len = delim - keystr;
3761 while (isspace(*delim)) {
3762 ++delim;
3763 }
3764 } else {
3765 len = strlen(keystr);
3766 }
3767
3768 /* key node must be present */
Michal Vaskoe444f752020-02-10 12:20:06 +01003769 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 +02003770 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3772 "The list's key \"%.*s\" not found.", len, keystr);
3773 return LY_EVALID;
3774 }
3775 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003776 if (key->flags & LYS_KEY) {
3777 /* the node was already marked as a key */
3778 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3779 "Duplicated key identifier \"%.*s\".", len, keystr);
3780 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003781 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003782
3783 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003784 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003785 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003786 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3787 return LY_EVALID;
3788 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003789 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003790 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003791 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003792 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003793 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003794 return LY_EVALID;
3795 }
3796 } else {
3797 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003798 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003799 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003800 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003801 return LY_EVALID;
3802 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003803 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003804 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003805 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003806 return LY_EVALID;
3807 }
3808 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003809
3810 /* check status */
3811 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003812 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003813
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003814 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003815 if (key->dflt) {
3816 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3817 lysc_type_free(ctx->ctx, key->dflt->realtype);
3818 free(key->dflt);
3819 key->dflt = NULL;
3820 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003821 }
3822 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003823 key->flags |= LYS_KEY;
3824
3825 /* move it to the correct position */
3826 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3827 /* fix links in closest previous siblings of the key */
3828 if (key->next) {
3829 key->next->prev = key->prev;
3830 } else {
3831 /* last child */
3832 list->child->prev = key->prev;
3833 }
3834 if (key->prev->next) {
3835 key->prev->next = key->next;
3836 }
3837 /* fix links in the key */
3838 if (prev_key) {
3839 key->prev = (struct lysc_node*)prev_key;
3840 key->next = prev_key->next;
3841 } else {
3842 key->prev = list->child->prev;
3843 key->next = list->child;
3844 }
3845 /* fix links in closes future siblings of the key */
3846 if (prev_key) {
3847 if (prev_key->next) {
3848 prev_key->next->prev = (struct lysc_node*)key;
3849 } else {
3850 list->child->prev = (struct lysc_node*)key;
3851 }
3852 prev_key->next = (struct lysc_node*)key;
3853 } else {
3854 list->child->prev = (struct lysc_node*)key;
3855 }
3856 /* fix links in parent */
3857 if (!key->prev->next) {
3858 list->child = (struct lysc_node*)key;
3859 }
3860 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003861
3862 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003863 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003864 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003865 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003866 }
3867
3868 /* uniques */
3869 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003870 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003871 }
3872
Radek Krejciec4da802019-05-02 13:02:41 +02003873 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3874 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003875
3876done:
3877 return ret;
3878}
3879
Radek Krejcib56c7502019-02-13 14:19:54 +01003880/**
3881 * @brief Do some checks and set the default choice's case.
3882 *
3883 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3884 *
3885 * @param[in] ctx Compile context.
3886 * @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,
3887 * not the reference to the imported module.
3888 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3889 * @return LY_ERR value.
3890 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003891static LY_ERR
3892lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3893{
3894 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3895 const char *prefix = NULL, *name;
3896 size_t prefix_len = 0;
3897
3898 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3899 name = strchr(dflt, ':');
3900 if (name) {
3901 prefix = dflt;
3902 prefix_len = name - prefix;
3903 ++name;
3904 } else {
3905 name = dflt;
3906 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003907 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003908 /* prefixed default case make sense only for the prefix of the schema itself */
3909 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3910 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3911 prefix_len, prefix);
3912 return LY_EVALID;
3913 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003914 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 +01003915 if (!ch->dflt) {
3916 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3917 "Default case \"%s\" not found.", dflt);
3918 return LY_EVALID;
3919 }
3920 /* no mandatory nodes directly under the default case */
3921 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003922 if (iter->parent != (struct lysc_node*)ch->dflt) {
3923 break;
3924 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003925 if (iter->flags & LYS_MAND_TRUE) {
3926 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3927 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3928 return LY_EVALID;
3929 }
3930 }
Radek Krejci01342af2019-01-03 15:18:08 +01003931 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003932 return LY_SUCCESS;
3933}
3934
Radek Krejciccd20f12019-02-15 14:12:27 +01003935static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003936lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003937{
3938 struct lys_module *mod;
3939 const char *prefix = NULL, *name;
3940 size_t prefix_len = 0;
3941 struct lysc_node_case *cs;
3942 struct lysc_node *node;
3943
3944 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3945 name = strchr(dflt, ':');
3946 if (name) {
3947 prefix = dflt;
3948 prefix_len = name - prefix;
3949 ++name;
3950 } else {
3951 name = dflt;
3952 }
3953 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3954 if (prefix) {
3955 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3956 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003957 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3958 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003959 return LY_EVALID;
3960 }
3961 } else {
3962 mod = ctx->mod;
3963 }
3964 /* get the default case */
Michal Vaskoe444f752020-02-10 12:20:06 +01003965 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 +01003966 if (!cs) {
3967 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003968 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003969 return LY_EVALID;
3970 }
3971
3972 /* check that there is no mandatory node */
3973 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003974 if (node->parent != (struct lysc_node*)cs) {
3975 break;
3976 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003977 if (node->flags & LYS_MAND_TRUE) {
3978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003979 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3980 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003981 return LY_EVALID;
3982 }
3983 }
3984
3985 /* set the default case in choice */
3986 ch->dflt = cs;
3987 cs->flags |= LYS_SET_DFLT;
3988
3989 return LY_SUCCESS;
3990}
3991
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003992/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003993 * @brief Compile parsed choice node information.
3994 * @param[in] ctx Compile context
3995 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003996 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003997 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003998 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3999 */
4000static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004001lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004002{
4003 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4004 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4005 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004006 LY_ERR ret = LY_SUCCESS;
4007
Radek Krejci056d0a82018-12-06 16:57:25 +01004008 LY_LIST_FOR(ch_p->child, child_p) {
4009 if (child_p->nodetype == LYS_CASE) {
4010 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004011 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004012 }
4013 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004014 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004015 }
4016 }
4017
4018 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004019 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004020 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004021 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004022
Radek Krejci9800fb82018-12-13 14:26:23 +01004023 return ret;
4024}
4025
4026/**
4027 * @brief Compile parsed anydata or anyxml node information.
4028 * @param[in] ctx Compile context
4029 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004030 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4031 * is enriched with the any-specific information.
4032 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4033 */
4034static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004035lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004036{
4037 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4038 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4039 unsigned int u;
4040 LY_ERR ret = LY_SUCCESS;
4041
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004042 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004043 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4044 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004045 ly_set_add(&ctx->xpath, any, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004046 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004047
4048 if (any->flags & LYS_CONFIG_W) {
4049 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004050 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004051 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004052done:
4053 return ret;
4054}
4055
Radek Krejcib56c7502019-02-13 14:19:54 +01004056/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004057 * @brief Connect the node into the siblings list and check its name uniqueness.
4058 *
4059 * @param[in] ctx Compile context
4060 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4061 * the choice itself is expected instead of a specific case node.
4062 * @param[in] node Schema node to connect into the list.
4063 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004064 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
Radek Krejci056d0a82018-12-06 16:57:25 +01004065 */
4066static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004067lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004068{
4069 struct lysc_node **children;
4070
4071 if (node->nodetype == LYS_CASE) {
4072 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4073 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004074 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004075 }
4076 if (children) {
4077 if (!(*children)) {
4078 /* first child */
4079 *children = node;
4080 } else if (*children != node) {
4081 /* by the condition in previous branch we cover the choice/case children
4082 * - the children list is shared by the choice and the the first case, in addition
4083 * the first child of each case must be referenced from the case node. So the node is
4084 * actually always already inserted in case it is the first children - so here such
4085 * a situation actually corresponds to the first branch */
4086 /* insert at the end of the parent's children list */
4087 (*children)->prev->next = node;
4088 node->prev = (*children)->prev;
4089 (*children)->prev = node;
4090
4091 /* check the name uniqueness */
4092 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4093 lysc_node_notifs(parent), node->name, node)) {
4094 return LY_EEXIST;
4095 }
4096 }
4097 }
4098 return LY_SUCCESS;
4099}
4100
Radek Krejci95710c92019-02-11 15:49:55 +01004101/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004102 * @brief Prepare the case structure in choice node for the new data node.
4103 *
4104 * 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
4105 * created in the choice when the first child was processed.
4106 *
4107 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004108 * @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,
4109 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004110 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4111 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4112 * @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,
4113 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004114 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004115static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004116lys_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 +01004117{
4118 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004119 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004120 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004121 unsigned int u;
4122 LY_ERR ret;
4123
Radek Krejci95710c92019-02-11 15:49:55 +01004124#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004125 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004126 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004127 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4128 return NULL; \
4129 } \
4130 }
4131
Radek Krejci95710c92019-02-11 15:49:55 +01004132 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4133 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004134
4135 /* we have to add an implicit case node into the parent choice */
4136 cs = calloc(1, sizeof(struct lysc_node_case));
4137 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004138 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004139 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004140 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004141 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4142 /* the case is already present since the child is not its first children */
4143 return (struct lysc_node_case*)ch->cases->prev;
4144 }
Radek Krejci95710c92019-02-11 15:49:55 +01004145 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004146
4147 /* explicit parent case is not present (this is its first child) */
4148 cs = calloc(1, sizeof(struct lysc_node_case));
4149 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004150 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004151 cs->flags = LYS_STATUS_MASK & node_p->flags;
4152 cs->sp = node_p;
4153
Radek Krejcib1b59152019-01-07 13:21:56 +01004154 /* 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 +02004155 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004156
4157 if (node_p->when) {
4158 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004159 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004160 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004161
4162 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4163 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004164 ly_set_add(&ctx->xpath, cs, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004165 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004166 }
Radek Krejciec4da802019-05-02 13:02:41 +02004167 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004168 } else {
4169 LOGINT(ctx->ctx);
4170 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004171 }
4172 cs->module = ctx->mod;
4173 cs->prev = (struct lysc_node*)cs;
4174 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004175 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004176 cs->child = child;
4177
4178 return cs;
4179error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004180 if (cs) {
4181 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4182 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004183 return NULL;
4184
4185#undef UNIQUE_CHECK
4186}
4187
Radek Krejcib56c7502019-02-13 14:19:54 +01004188/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004189 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004190 *
4191 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004192 * @param[in] node Target node where the config is supposed to be changed.
4193 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004194 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4195 * 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 +01004196 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4197 * @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 +01004198 * @return LY_ERR value.
4199 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004200static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004201lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004202 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004203{
4204 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004205 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004206
4207 if (config == (node->flags & LYS_CONFIG_MASK)) {
4208 /* nothing to do */
4209 return LY_SUCCESS;
4210 }
4211
4212 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004213 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004214 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4215 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004216 "Invalid %s of config - configuration node cannot be child of any state data node.",
4217 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004218 return LY_EVALID;
4219 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004220 node->flags |= LYS_SET_CONFIG;
4221 } else {
4222 if (node->flags & LYS_SET_CONFIG) {
4223 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4224 /* setting config flags, but have node with explicit config true */
4225 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004226 "Invalid %s of config - configuration node cannot be child of any state data node.",
4227 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004228 return LY_EVALID;
4229 }
4230 /* do not change config on nodes where the config is explicitely set, this does not apply to
4231 * nodes, which are being changed explicitly (targets of refine or deviation) */
4232 return LY_SUCCESS;
4233 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004234 }
4235 node->flags &= ~LYS_CONFIG_MASK;
4236 node->flags |= config;
4237
4238 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004239 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004240 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004241 }
4242
Radek Krejci76b3e962018-12-14 17:01:25 +01004243 return LY_SUCCESS;
4244}
4245
Radek Krejcib56c7502019-02-13 14:19:54 +01004246/**
4247 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4248 *
4249 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4250 * the flag to such parents from a mandatory children.
4251 *
4252 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4253 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4254 * (mandatory children was removed).
4255 */
Radek Krejcife909632019-02-12 15:34:42 +01004256void
4257lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4258{
4259 struct lysc_node *iter;
4260
4261 if (add) { /* set flag */
4262 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4263 parent = parent->parent) {
4264 parent->flags |= LYS_MAND_TRUE;
4265 }
4266 } else { /* unset flag */
4267 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004268 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004269 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004270 /* there is another mandatory node */
4271 return;
4272 }
4273 }
4274 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4275 parent->flags &= ~LYS_MAND_TRUE;
4276 }
4277 }
4278}
4279
Radek Krejci056d0a82018-12-06 16:57:25 +01004280/**
Radek Krejci3641f562019-02-13 15:38:40 +01004281 * @brief Internal sorting process for the lys_compile_augment_sort().
4282 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4283 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4284 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4285 */
4286static void
4287lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4288{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004289 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004290 size_t len;
4291
4292 len = strlen(aug_p->nodeid);
4293 LY_ARRAY_FOR(result, v) {
4294 if (strlen(result[v]->nodeid) <= len) {
4295 continue;
4296 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004297 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004298 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004299 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004300 break;
4301 }
4302 }
4303 result[v] = aug_p;
4304 LY_ARRAY_INCREMENT(result);
4305}
4306
4307/**
4308 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4309 *
4310 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4311 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4312 *
4313 * @param[in] ctx Compile context.
4314 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4315 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4316 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4317 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4318 * @return LY_ERR value.
4319 */
4320LY_ERR
4321lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4322{
4323 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004324 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004325
4326 assert(augments);
4327
4328 /* get count of the augments in module and all its submodules */
4329 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004330 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004331 }
4332 LY_ARRAY_FOR(inc_p, u) {
4333 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004334 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004335 }
4336 }
4337
4338 if (!count) {
4339 *augments = NULL;
4340 return LY_SUCCESS;
4341 }
4342 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4343
4344 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4345 * together, so there can be even /z/y betwwen them. */
4346 LY_ARRAY_FOR(aug_p, u) {
4347 lys_compile_augment_sort_(&aug_p[u], result);
4348 }
4349 LY_ARRAY_FOR(inc_p, u) {
4350 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4351 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4352 }
4353 }
4354
4355 *augments = result;
4356 return LY_SUCCESS;
4357}
4358
4359/**
4360 * @brief Compile the parsed augment connecting it into its target.
4361 *
4362 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4363 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4364 * are already implemented and compiled.
4365 *
4366 * @param[in] ctx Compile context.
4367 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004368 * @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
4369 * children in case of the augmenting uses data.
4370 * @return LY_SUCCESS on success.
4371 * @return LY_EVALID on failure.
4372 */
4373LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004374lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004375{
Michal Vaskoe6143202020-07-03 13:02:08 +02004376 LY_ERR ret = LY_SUCCESS, rc;
Radek Krejci3641f562019-02-13 15:38:40 +01004377 struct lysp_node *node_p, *case_node_p;
4378 struct lysc_node *target; /* target target of the augment */
4379 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004380 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004381 struct lys_module **aug_mod;
Radek Krejci3641f562019-02-13 15:38:40 +01004382 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004383 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004384 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejciec4da802019-05-02 13:02:41 +02004385 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004386
Radek Krejci327de162019-06-14 12:52:07 +02004387 lysc_update_path(ctx, NULL, "{augment}");
4388 lysc_update_path(ctx, NULL, aug_p->nodeid);
4389
Radek Krejci7af64242019-02-18 13:07:53 +01004390 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004391 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004392 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004393 if (ret != LY_SUCCESS) {
4394 if (ret == LY_EDENIED) {
4395 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4396 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4397 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4398 }
4399 return LY_EVALID;
4400 }
4401
4402 /* check for mandatory nodes
4403 * - new cases augmenting some choice can have mandatory nodes
4404 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4405 */
Radek Krejci733988a2019-02-15 15:12:44 +01004406 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004407 allow_mandatory = 1;
4408 }
4409
4410 when_shared = NULL;
4411 LY_LIST_FOR(aug_p->child, node_p) {
4412 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4413 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004414 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004415 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4416 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004417 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004418 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4419 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004420 return LY_EVALID;
4421 }
4422
4423 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004424 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004425 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004426 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004427 } else {
4428 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004429 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004430 }
4431 }
Radek Krejciec4da802019-05-02 13:02:41 +02004432 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004433
Radek Krejcife13da42019-02-15 14:51:01 +01004434 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4435 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004436 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004437 /* 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 +01004438 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 +01004439 } else if (target->nodetype == LYS_CHOICE) {
4440 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4441 node = ((struct lysc_node_choice*)target)->cases->prev;
4442 } else {
4443 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004444 node = (struct lysc_node*)lysc_node_children(target, flags);
4445 if (!node) {
4446 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4447 break;
4448 }
4449 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004450 }
4451
Radek Krejci733988a2019-02-15 15:12:44 +01004452 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004453 node->flags &= ~LYS_MAND_TRUE;
4454 lys_compile_mandatory_parents(target, 0);
4455 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004456 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004457 return LY_EVALID;
4458 }
4459
4460 /* pass augment's when to all the children */
4461 if (aug_p->when) {
4462 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4463 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004464 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004465 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004466
4467 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4468 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004469 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004470 }
4471
Radek Krejci3641f562019-02-13 15:38:40 +01004472 when_shared = *when;
4473 } else {
4474 ++when_shared->refcount;
4475 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004476
4477 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4478 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004479 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004480 }
Radek Krejci3641f562019-02-13 15:38:40 +01004481 }
4482 }
4483 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004484
Radek Krejciec4da802019-05-02 13:02:41 +02004485 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004486 switch (target->nodetype) {
4487 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004488 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004489 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004490 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004491 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004492 break;
4493 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004494 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004495 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004496 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004497 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004498 break;
4499 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004500 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004501 if (aug_p->actions) {
4502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004503 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4504 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004505 return LY_EVALID;
4506 }
4507 if (aug_p->notifs) {
4508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004509 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004510 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004511 return LY_EVALID;
4512 }
4513 }
Radek Krejci3641f562019-02-13 15:38:40 +01004514
Michal Vaskoe6143202020-07-03 13:02:08 +02004515 /* add this module into the target module augmented_by, if not there already */
4516 rc = LY_SUCCESS;
4517 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4518 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4519 rc = LY_EEXIST;
4520 break;
4521 }
4522 }
4523 if (!rc) {
4524 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4525 *aug_mod = ctx->mod;
4526 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004527
Radek Krejci327de162019-06-14 12:52:07 +02004528 lysc_update_path(ctx, NULL, NULL);
4529 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004530
Radek Krejci3641f562019-02-13 15:38:40 +01004531error:
Radek Krejciec4da802019-05-02 13:02:41 +02004532 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004533 return ret;
4534}
4535
4536/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004537 * @brief Apply refined or deviated mandatory flag to the target node.
4538 *
4539 * @param[in] ctx Compile context.
4540 * @param[in] node Target node where the mandatory property is supposed to be changed.
4541 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004542 * @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 +01004543 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4544 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4545 * 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 +01004546 * @return LY_ERR value.
4547 */
4548static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004549lys_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 +01004550{
4551 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4552 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004553 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4554 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004555 return LY_EVALID;
4556 }
4557
4558 if (mandatory_flag & LYS_MAND_TRUE) {
4559 /* check if node has default value */
4560 if (node->nodetype & LYS_LEAF) {
4561 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004562 if (refine_flag) {
4563 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004564 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004565 return LY_EVALID;
4566 }
Radek Krejcia1911222019-07-22 17:24:50 +02004567 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004568 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004569 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004570
4571 /* update the list of incomplete default values if needed */
4572 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4573
Radek Krejcia1911222019-07-22 17:24:50 +02004574 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4575 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4576 free(leaf->dflt);
4577 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004578 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004579 }
4580 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004581 if (refine_flag) {
4582 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004583 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004584 return LY_EVALID;
4585 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004586 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004587 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004588 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 +01004589 return LY_EVALID;
4590 }
4591
4592 node->flags &= ~LYS_MAND_FALSE;
4593 node->flags |= LYS_MAND_TRUE;
4594 lys_compile_mandatory_parents(node->parent, 1);
4595 } else {
4596 /* make mandatory false */
4597 node->flags &= ~LYS_MAND_TRUE;
4598 node->flags |= LYS_MAND_FALSE;
4599 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004600 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 +01004601 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004602 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004603 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004604 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4605 leaf->dflt->realtype = leaf->type->dflt->realtype;
4606 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4607 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004608 }
4609 }
4610 return LY_SUCCESS;
4611}
4612
4613/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004614 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4615 * If present, also apply uses's modificators.
4616 *
4617 * @param[in] ctx Compile context
4618 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004619 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4620 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4621 * the compile context.
4622 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4623 */
4624static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004625lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004626{
4627 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004628 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004629 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004630 struct lysc_node_container context_node_fake =
4631 {.nodetype = LYS_CONTAINER,
4632 .module = ctx->mod,
4633 .flags = parent ? parent->flags : 0,
4634 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004635 .prev = (struct lysc_node*)&context_node_fake,
4636 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004637 struct lysp_grp *grp = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004638 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004639 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004640 int found;
4641 const char *id, *name, *prefix;
4642 size_t prefix_len, name_len;
4643 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004644 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004645 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004646 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004647 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004648 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004649 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004650 struct lysp_augment **augments = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004651 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004652 struct lysc_notif **notifs = NULL;
4653 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004654
4655 /* search for the grouping definition */
4656 found = 0;
4657 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004658 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004659 if (prefix) {
4660 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4661 if (!mod) {
4662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004663 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004664 return LY_EVALID;
4665 }
4666 } else {
4667 mod = ctx->mod_def;
4668 }
4669 if (mod == ctx->mod_def) {
4670 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004671 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004672 LY_ARRAY_FOR(grp, u) {
4673 if (!strcmp(grp[u].name, name)) {
4674 grp = &grp[u];
4675 found = 1;
4676 break;
4677 }
4678 }
4679 }
4680 }
4681 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004682 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004683 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004684 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004685 for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004686 if (!strcmp(grp[u].name, name)) {
4687 grp = &grp[u];
4688 found = 1;
4689 }
4690 }
4691 }
4692 if (!found && mod->parsed->includes) {
4693 /* ... and all the submodules */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004694 for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004695 grp = mod->parsed->includes[u].submodule->groupings;
4696 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004697 for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004698 if (!strcmp(grp[v].name, name)) {
4699 grp = &grp[v];
4700 found = 1;
4701 }
4702 }
4703 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004704 }
4705 }
4706 }
4707 if (!found) {
4708 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4709 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4710 return LY_EVALID;
4711 }
4712
4713 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4714 grp_stack_count = ctx->groupings.count;
4715 ly_set_add(&ctx->groupings, (void*)grp, 0);
4716 if (grp_stack_count == ctx->groupings.count) {
4717 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4718 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4719 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4720 return LY_EVALID;
4721 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004722 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4723 /* remember that the grouping is instantiated to avoid its standalone validation */
4724 grp->flags |= LYS_USED_GRP;
4725 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004726
4727 /* switch context's mod_def */
4728 mod_old = ctx->mod_def;
4729 ctx->mod_def = mod;
4730
4731 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004732 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 +01004733
Radek Krejcifc11bd72019-04-11 16:00:05 +02004734 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004735 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004736 /* 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 +02004737 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 +01004738
4739 /* some preparation for applying refines */
4740 if (grp->data == node_p) {
4741 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004742 if (parent) {
4743 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4744 } else if (ctx->mod->compiled->data) {
4745 child = ctx->mod->compiled->data;
4746 } else {
4747 child = NULL;
4748 }
4749 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004750 }
4751 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004752 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004753 LY_LIST_FOR(context_node_fake.child, child) {
4754 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004755
Radek Krejcifc11bd72019-04-11 16:00:05 +02004756 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004757 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004758 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004759 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004760 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4761
4762 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4763 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004764 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004765 }
4766
Radek Krejci00b874b2019-02-12 10:54:50 +01004767 when_shared = *when;
4768 } else {
4769 ++when_shared->refcount;
4770 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004771
4772 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4773 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004774 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004775 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004776 }
4777 }
Radek Krejci01342af2019-01-03 15:18:08 +01004778 }
4779 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004780 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004781 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004782 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004783 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 +01004784 }
4785
Radek Krejcifc11bd72019-04-11 16:00:05 +02004786 /* compile actions */
4787 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4788 if (actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004789 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004790 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004791 if (*actions && (uses_p->augments || uses_p->refines)) {
4792 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004793 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
4794 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
4795 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004796 }
4797 }
4798
4799 /* compile notifications */
4800 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4801 if (notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004802 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004803 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004804 if (*notifs && (uses_p->augments || uses_p->refines)) {
4805 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004806 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
4807 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
4808 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004809 }
4810 }
4811
4812
Radek Krejci3641f562019-02-13 15:38:40 +01004813 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004814 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004815 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004816 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004817 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004818
Radek Krejcif0089082019-01-07 16:42:01 +01004819 /* reload previous context's mod_def */
4820 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004821 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004822
Radek Krejci76b3e962018-12-14 17:01:25 +01004823 /* apply refine */
4824 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004825 lysc_update_path(ctx, NULL, rfn->nodeid);
4826
Radek Krejci7af64242019-02-18 13:07:53 +01004827 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 +01004828 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004829 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004830 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004831
4832 /* default value */
4833 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004834 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004836 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.",
4837 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004838 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004839 }
4840 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4841 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004842 "Invalid refine of default - %s cannot hold default value(s).",
4843 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004844 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004845 }
4846 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004847 struct ly_err_item *err = NULL;
4848 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4849 if (leaf->dflt) {
4850 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004851 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004852 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4853 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4854 } else {
4855 /* prepare a new one */
4856 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4857 leaf->dflt->realtype = leaf->type;
4858 }
4859 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004860 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004861 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4862 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004863 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004864 leaf->dflt->realtype->refcount++;
4865 if (err) {
4866 ly_err_print(err);
4867 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4868 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4869 ly_err_free(err);
4870 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004871 if (rc == LY_EINCOMPLETE) {
4872 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004873 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004874
4875 /* but in general result is so far ok */
4876 rc = LY_SUCCESS;
4877 }
Radek Krejcia1911222019-07-22 17:24:50 +02004878 LY_CHECK_GOTO(rc, cleanup);
4879 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004880 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004881 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4882
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004883 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004884 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4885 "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 +02004886 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004887 }
Radek Krejcia1911222019-07-22 17:24:50 +02004888
4889 /* remove previous set of default values */
4890 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004891 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004892 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4893 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4894 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004895 }
Radek Krejcia1911222019-07-22 17:24:50 +02004896 LY_ARRAY_FREE(llist->dflts);
4897 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004898 LY_ARRAY_FREE(llist->dflts_mods);
4899 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004900
4901 /* create the new set of the default values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004902 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
4903 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004904 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004905 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004906 LY_ARRAY_INCREMENT(llist->dflts_mods);
4907 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004908 LY_ARRAY_INCREMENT(llist->dflts);
4909 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4910 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004911 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004912 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004913 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004914 llist->dflts[u]->realtype->refcount++;
4915 if (err) {
4916 ly_err_print(err);
4917 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4918 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4919 ly_err_free(err);
4920 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004921 if (rc == LY_EINCOMPLETE) {
4922 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004923 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004924
4925 /* but in general result is so far ok */
4926 rc = LY_SUCCESS;
4927 }
4928 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004929 }
Radek Krejcia1911222019-07-22 17:24:50 +02004930 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004931 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004932 if (((struct lysc_node_choice*)node)->dflt) {
4933 /* unset LYS_SET_DFLT from the current default case */
4934 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4935 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004936 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 +01004937 }
4938 }
4939
Radek Krejci12fb9142019-01-08 09:45:30 +01004940 /* description */
4941 if (rfn->dsc) {
4942 FREE_STRING(ctx->ctx, node->dsc);
4943 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4944 }
4945
4946 /* reference */
4947 if (rfn->ref) {
4948 FREE_STRING(ctx->ctx, node->ref);
4949 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4950 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004951
4952 /* config */
4953 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004954 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004955 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004956 } else {
4957 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004958 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004959 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004960 }
4961
4962 /* mandatory */
4963 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004964 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004965 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004966
4967 /* presence */
4968 if (rfn->presence) {
4969 if (node->nodetype != LYS_CONTAINER) {
4970 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004971 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4972 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004973 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004974 }
4975 node->flags |= LYS_PRESENCE;
4976 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004977
4978 /* must */
4979 if (rfn->musts) {
4980 switch (node->nodetype) {
4981 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004982 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 +01004983 break;
4984 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004985 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 +01004986 break;
4987 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004988 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 +01004989 break;
4990 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004991 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 +01004992 break;
4993 case LYS_ANYXML:
4994 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004995 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 +01004996 break;
4997 default:
4998 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004999 "Invalid refine of must statement - %s cannot hold any must statement.",
5000 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005001 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005002 }
Michal Vasko004d3152020-06-11 19:59:22 +02005003 ly_set_add(&ctx->xpath, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01005004 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005005
5006 /* min/max-elements */
5007 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5008 switch (node->nodetype) {
5009 case LYS_LEAFLIST:
5010 if (rfn->flags & LYS_SET_MAX) {
5011 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5012 }
5013 if (rfn->flags & LYS_SET_MIN) {
5014 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005015 if (rfn->min) {
5016 node->flags |= LYS_MAND_TRUE;
5017 lys_compile_mandatory_parents(node->parent, 1);
5018 } else {
5019 node->flags &= ~LYS_MAND_TRUE;
5020 lys_compile_mandatory_parents(node->parent, 0);
5021 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005022 }
5023 break;
5024 case LYS_LIST:
5025 if (rfn->flags & LYS_SET_MAX) {
5026 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5027 }
5028 if (rfn->flags & LYS_SET_MIN) {
5029 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005030 if (rfn->min) {
5031 node->flags |= LYS_MAND_TRUE;
5032 lys_compile_mandatory_parents(node->parent, 1);
5033 } else {
5034 node->flags &= ~LYS_MAND_TRUE;
5035 lys_compile_mandatory_parents(node->parent, 0);
5036 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005037 }
5038 break;
5039 default:
5040 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005041 "Invalid refine of %s statement - %s cannot hold this statement.",
5042 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005043 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005044 }
5045 }
Radek Krejcif0089082019-01-07 16:42:01 +01005046
5047 /* if-feature */
5048 if (rfn->iffeatures) {
5049 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005050 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005051 }
Radek Krejci327de162019-06-14 12:52:07 +02005052
5053 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005054 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005055
Radek Krejcif2271f12019-01-07 16:42:23 +01005056 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005057 for (uint32_t i = 0; i < refined.count; ++i) {
5058 node = (struct lysc_node*)refined.objs[i];
5059 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02005060 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005061
5062 /* check possible conflict with default value (default added, mandatory left true) */
5063 if ((node->flags & LYS_MAND_TRUE) &&
5064 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5065 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5066 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005067 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005068 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005069 }
5070
5071 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5072 if (node->nodetype == LYS_LIST) {
5073 min = ((struct lysc_node_list*)node)->min;
5074 max = ((struct lysc_node_list*)node)->max;
5075 } else {
5076 min = ((struct lysc_node_leaflist*)node)->min;
5077 max = ((struct lysc_node_leaflist*)node)->max;
5078 }
5079 if (min > max) {
5080 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005081 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5082 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005083 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005084 }
5085 }
5086 }
5087
Radek Krejci327de162019-06-14 12:52:07 +02005088 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005089 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005090
5091cleanup:
5092 /* fix connection of the children nodes from fake context node back into the parent */
5093 if (context_node_fake.child) {
5094 context_node_fake.child->prev = child;
5095 }
5096 LY_LIST_FOR(context_node_fake.child, child) {
5097 child->parent = parent;
5098 }
5099
5100 if (uses_p->augments || uses_p->refines) {
5101 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005102 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005103 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005104 LY_ARRAY_FREE(context_node_fake.actions);
5105 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005106 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005107 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005108 LY_ARRAY_FREE(context_node_fake.notifs);
5109 }
5110 }
5111
Radek Krejcie86bf772018-12-14 11:39:53 +01005112 /* reload previous context's mod_def */
5113 ctx->mod_def = mod_old;
5114 /* remove the grouping from the stack for circular groupings dependency check */
5115 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5116 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005117 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005118 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005119
5120 return ret;
5121}
5122
Radek Krejci327de162019-06-14 12:52:07 +02005123static int
5124lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5125{
5126 struct lysp_node *iter;
5127 int len = 0;
5128
5129 *path = NULL;
5130 for (iter = node; iter && len >= 0; iter = iter->parent) {
5131 char *s = *path;
5132 char *id;
5133
5134 switch (iter->nodetype) {
5135 case LYS_USES:
5136 asprintf(&id, "{uses='%s'}", iter->name);
5137 break;
5138 case LYS_GROUPING:
5139 asprintf(&id, "{grouping='%s'}", iter->name);
5140 break;
5141 case LYS_AUGMENT:
5142 asprintf(&id, "{augment='%s'}", iter->name);
5143 break;
5144 default:
5145 id = strdup(iter->name);
5146 break;
5147 }
5148
5149 if (!iter->parent) {
5150 /* print prefix */
5151 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5152 } else {
5153 /* prefix is the same as in parent */
5154 len = asprintf(path, "/%s%s", id, s ? s : "");
5155 }
5156 free(s);
5157 free(id);
5158 }
5159
5160 if (len < 0) {
5161 free(*path);
5162 *path = NULL;
5163 } else if (len == 0) {
5164 *path = strdup("/");
5165 len = 1;
5166 }
5167 return len;
5168}
5169
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005170/**
5171 * @brief Validate groupings that were defined but not directly used in the schema itself.
5172 *
5173 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5174 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5175 */
5176static LY_ERR
5177lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5178{
5179 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005180 char *path;
5181 int len;
5182
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005183 struct lysp_node_uses fake_uses = {
5184 .parent = node_p,
5185 .nodetype = LYS_USES,
5186 .flags = 0, .next = NULL,
5187 .name = grp->name,
5188 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5189 .refines = NULL, .augments = NULL
5190 };
5191 struct lysc_node_container fake_container = {
5192 .nodetype = LYS_CONTAINER,
5193 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5194 .module = ctx->mod,
5195 .sp = NULL, .parent = NULL, .next = NULL,
5196 .prev = (struct lysc_node*)&fake_container,
5197 .name = "fake",
5198 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5199 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5200 };
5201
5202 if (grp->parent) {
5203 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5204 }
Radek Krejci327de162019-06-14 12:52:07 +02005205
5206 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5207 if (len < 0) {
5208 LOGMEM(ctx->ctx);
5209 return LY_EMEM;
5210 }
5211 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5212 ctx->path_len = (uint16_t)len;
5213 free(path);
5214
5215 lysc_update_path(ctx, NULL, "{grouping}");
5216 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005217 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005218 lysc_update_path(ctx, NULL, NULL);
5219 lysc_update_path(ctx, NULL, NULL);
5220
5221 ctx->path_len = 1;
5222 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005223
5224 /* cleanup */
5225 lysc_node_container_free(ctx->ctx, &fake_container);
5226
5227 return ret;
5228}
Radek Krejcife909632019-02-12 15:34:42 +01005229
Radek Krejcie86bf772018-12-14 11:39:53 +01005230/**
Radek Krejcia3045382018-11-22 14:30:31 +01005231 * @brief Compile parsed schema node information.
5232 * @param[in] ctx Compile context
5233 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005234 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5235 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5236 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005237 * @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).
5238 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005239 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5240 */
Radek Krejci19a96102018-11-15 13:38:09 +01005241static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005242lys_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 +01005243{
Radek Krejci1c54f462020-05-12 17:25:34 +02005244 LY_ERR ret = LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01005245 struct lysc_node *node;
5246 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005247 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005248 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005249 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005250
Radek Krejci327de162019-06-14 12:52:07 +02005251 if (node_p->nodetype != LYS_USES) {
5252 lysc_update_path(ctx, parent, node_p->name);
5253 } else {
5254 lysc_update_path(ctx, NULL, "{uses}");
5255 lysc_update_path(ctx, NULL, node_p->name);
5256 }
5257
Radek Krejci19a96102018-11-15 13:38:09 +01005258 switch (node_p->nodetype) {
5259 case LYS_CONTAINER:
5260 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5261 node_compile_spec = lys_compile_node_container;
5262 break;
5263 case LYS_LEAF:
5264 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5265 node_compile_spec = lys_compile_node_leaf;
5266 break;
5267 case LYS_LIST:
5268 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005269 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005270 break;
5271 case LYS_LEAFLIST:
5272 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005273 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005274 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005275 case LYS_CHOICE:
5276 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005277 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005278 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005279 case LYS_ANYXML:
5280 case LYS_ANYDATA:
5281 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005282 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005283 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005284 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005285 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5286 lysc_update_path(ctx, NULL, NULL);
5287 lysc_update_path(ctx, NULL, NULL);
5288 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005289 default:
5290 LOGINT(ctx->ctx);
5291 return LY_EINT;
5292 }
5293 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5294 node->nodetype = node_p->nodetype;
5295 node->module = ctx->mod;
5296 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005297 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005298
5299 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005300 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005301 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005302 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005303 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5304 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005305 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005306 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005307 node->flags |= LYS_CONFIG_R;
5308 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005309 /* config not explicitely set, inherit it from parent */
5310 if (parent) {
5311 node->flags |= parent->flags & LYS_CONFIG_MASK;
5312 } else {
5313 /* default is config true */
5314 node->flags |= LYS_CONFIG_W;
5315 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005316 } else {
5317 /* config set explicitely */
5318 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005319 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005320 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5321 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5322 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005323 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005324 goto error;
5325 }
Radek Krejci19a96102018-11-15 13:38:09 +01005326
Radek Krejcia6d57732018-11-29 13:40:37 +01005327 /* *list ordering */
5328 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5329 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005330 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005331 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5332 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005333 node->flags &= ~LYS_ORDBY_MASK;
5334 node->flags |= LYS_ORDBY_SYSTEM;
5335 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5336 /* default ordering is system */
5337 node->flags |= LYS_ORDBY_SYSTEM;
5338 }
5339 }
5340
Radek Krejci19a96102018-11-15 13:38:09 +01005341 /* status - it is not inherited by specification, but it does not make sense to have
5342 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005343 if (!parent || parent->nodetype != LYS_CHOICE) {
5344 /* in case of choice/case's children, postpone the check to the moment we know if
5345 * 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 +02005346 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 +01005347 }
5348
Radek Krejciec4da802019-05-02 13:02:41 +02005349 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005350 node->sp = node_p;
5351 }
5352 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005353 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5354 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005355 if (node_p->when) {
5356 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005357 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005358
5359 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5360 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02005361 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01005362 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005363 }
Radek Krejciec4da802019-05-02 13:02:41 +02005364 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005365
5366 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005367 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005368
Radek Krejci0935f412019-08-20 16:15:18 +02005369 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5370
Radek Krejcife909632019-02-12 15:34:42 +01005371 /* inherit LYS_MAND_TRUE in parent containers */
5372 if (node->flags & LYS_MAND_TRUE) {
5373 lys_compile_mandatory_parents(parent, 1);
5374 }
5375
Radek Krejci327de162019-06-14 12:52:07 +02005376 lysc_update_path(ctx, NULL, NULL);
5377
Radek Krejci19a96102018-11-15 13:38:09 +01005378 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005379 if (parent) {
5380 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005381 if (node_p->parent->nodetype == LYS_CASE) {
5382 lysc_update_path(ctx, parent, node_p->parent->name);
5383 } else {
5384 lysc_update_path(ctx, parent, node->name);
5385 }
Radek Krejciec4da802019-05-02 13:02:41 +02005386 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005387 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005388 if (uses_status) {
5389
5390 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005391 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005392 * it directly gets the same status flags as the choice;
5393 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005394 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005395 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005396 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005397 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005398 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005399 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005400 }
Radek Krejciec4da802019-05-02 13:02:41 +02005401 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 +02005402
5403 if (parent->nodetype == LYS_CHOICE) {
5404 lysc_update_path(ctx, NULL, NULL);
5405 }
Radek Krejci19a96102018-11-15 13:38:09 +01005406 } else {
5407 /* top-level element */
5408 if (!ctx->mod->compiled->data) {
5409 ctx->mod->compiled->data = node;
5410 } else {
5411 /* insert at the end of the module's top-level nodes list */
5412 ctx->mod->compiled->data->prev->next = node;
5413 node->prev = ctx->mod->compiled->data->prev;
5414 ctx->mod->compiled->data->prev = node;
5415 }
Radek Krejci327de162019-06-14 12:52:07 +02005416 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005417 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5418 ctx->mod->compiled->notifs, node->name, node)) {
5419 return LY_EVALID;
5420 }
Radek Krejci19a96102018-11-15 13:38:09 +01005421 }
Radek Krejci327de162019-06-14 12:52:07 +02005422 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005423
5424 return LY_SUCCESS;
5425
5426error:
5427 lysc_node_free(ctx->ctx, node);
5428 return ret;
5429}
5430
Radek Krejciccd20f12019-02-15 14:12:27 +01005431static void
5432lysc_disconnect(struct lysc_node *node)
5433{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005434 struct lysc_node *parent, *child, *prev = NULL, *next;
5435 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005436 int remove_cs = 0;
5437
5438 parent = node->parent;
5439
5440 /* parent's first child */
5441 if (!parent) {
5442 return;
5443 }
5444 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005445 cs = (struct lysc_node_case*)node;
5446 } else if (parent->nodetype == LYS_CASE) {
5447 /* disconnecting some node in a case */
5448 cs = (struct lysc_node_case*)parent;
5449 parent = cs->parent;
5450 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5451 if (child == node) {
5452 if (cs->child == child) {
5453 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5454 /* case with a single child -> remove also the case */
5455 child->parent = NULL;
5456 remove_cs = 1;
5457 } else {
5458 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005459 }
5460 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005461 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005462 }
5463 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005464 if (!remove_cs) {
5465 cs = NULL;
5466 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005467 } else if (lysc_node_children(parent, node->flags) == node) {
5468 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005469 }
5470
5471 if (cs) {
5472 if (remove_cs) {
5473 /* cs has only one child which is being also removed */
5474 lysc_disconnect((struct lysc_node*)cs);
5475 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5476 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005477 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5478 /* default case removed */
5479 ((struct lysc_node_choice*)parent)->dflt = NULL;
5480 }
5481 if (((struct lysc_node_choice*)parent)->cases == cs) {
5482 /* first case removed */
5483 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5484 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005485 if (cs->child) {
5486 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5487 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5488 prev = cs->child->prev;
5489 } /* else all the children are under a single case */
5490 LY_LIST_FOR_SAFE(cs->child, next, child) {
5491 if (child->parent != (struct lysc_node*)cs) {
5492 break;
5493 }
5494 lysc_node_free(node->module->ctx, child);
5495 }
5496 if (prev) {
5497 if (prev->next) {
5498 prev->next = child;
5499 }
5500 if (child) {
5501 child->prev = prev;
5502 } else {
5503 /* link from the first child under the cases */
5504 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5505 }
5506 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005507 }
5508 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005509 }
5510
5511 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005512 if (node->prev->next) {
5513 node->prev->next = node->next;
5514 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005515 if (node->next) {
5516 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005517 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005518 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005519 if (child) {
5520 child->prev = node->prev;
5521 }
5522 } else if (((struct lysc_node_choice*)parent)->cases) {
5523 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005524 }
5525}
5526
5527LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005528lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005529{
Radek Krejcia1911222019-07-22 17:24:50 +02005530 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005531 struct ly_set devs_p = {0};
5532 struct ly_set targets = {0};
5533 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005534 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005535 struct lysc_action *rpcs;
5536 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005537 struct lysp_deviation *dev;
5538 struct lysp_deviate *d, **dp_new;
5539 struct lysp_deviate_add *d_add;
5540 struct lysp_deviate_del *d_del;
5541 struct lysp_deviate_rpl *d_rpl;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005542 LY_ARRAY_COUNT_TYPE u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005543 struct lysc_deviation {
5544 const char *nodeid;
5545 struct lysc_node *target; /* target node of the deviation */
5546 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005547 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005548 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5549 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005550 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005551 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005552 const char *prefix, *name, *nodeid, *dflt;
Michal Vasko57c10cd2020-05-27 15:57:11 +02005553 struct lys_module *mod, **dev_mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005554 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005555 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005556
5557 /* get all deviations from the module and all its submodules ... */
5558 LY_ARRAY_FOR(mod_p->deviations, u) {
5559 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5560 }
5561 LY_ARRAY_FOR(mod_p->includes, v) {
5562 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5563 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5564 }
5565 }
5566 if (!devs_p.count) {
5567 /* nothing to do */
5568 return LY_SUCCESS;
5569 }
5570
Radek Krejci327de162019-06-14 12:52:07 +02005571 lysc_update_path(ctx, NULL, "{deviation}");
5572
Radek Krejciccd20f12019-02-15 14:12:27 +01005573 /* ... and group them by the target node */
5574 devs = calloc(devs_p.count, sizeof *devs);
5575 for (u = 0; u < devs_p.count; ++u) {
5576 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005577 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005578
5579 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005580 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5581 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01005582 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005583 /* move the target pointer to input/output to make them different from the action and
5584 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5585 * back to the RPC/action node due to a better compatibility and decision code in this function.
5586 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5587 if (flags & LYSC_OPT_RPC_INPUT) {
5588 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5589 flags |= LYSC_OPT_INTERNAL;
5590 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5591 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5592 flags |= LYSC_OPT_INTERNAL;
5593 }
5594 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005595 /* insert into the set of targets with duplicity detection */
5596 i = ly_set_add(&targets, target, 0);
5597 if (!devs[i]) {
5598 /* new record */
5599 devs[i] = calloc(1, sizeof **devs);
5600 devs[i]->target = target;
5601 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005602 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005603 }
5604 /* add deviates into the deviation's list of deviates */
5605 for (d = dev->deviates; d; d = d->next) {
5606 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5607 *dp_new = d;
5608 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5609 devs[i]->not_supported = 1;
5610 }
5611 }
Radek Krejci327de162019-06-14 12:52:07 +02005612
5613 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005614 }
5615
5616 /* MACROS for deviates checking */
5617#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5618 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005619 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 +01005620 goto cleanup; \
5621 }
5622
5623#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005624 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5626 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005627 goto cleanup; \
5628 }
5629
Radek Krejcia1911222019-07-22 17:24:50 +02005630
Radek Krejciccd20f12019-02-15 14:12:27 +01005631#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005632 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5633 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5634 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5635 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5636 goto cleanup; \
5637 }
5638
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005639#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005640 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005641 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005642 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5643 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005645 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5646 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005647 goto cleanup; \
5648 }
5649
Radek Krejci551b12c2019-02-19 16:11:21 +01005650#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5651 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5652 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005653 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5654 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005655 goto cleanup; \
5656 }
5657
Radek Krejciccd20f12019-02-15 14:12:27 +01005658#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5659 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005661 goto cleanup; \
5662 }
5663
Radek Krejci551b12c2019-02-19 16:11:21 +01005664#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5665 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005667 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005668 goto cleanup; \
5669 }
5670
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005671#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5672 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5673 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5674 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5675 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 +01005676 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005677 if (y == LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005679 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5680 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005681 goto cleanup; \
5682 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005683 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5684 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5685 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5686 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005687 (LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005688 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005689 if (!LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005690 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5691 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005692 }
5693
5694 /* apply deviations */
5695 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005696 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5697 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5698 struct ly_err_item *err = NULL;
5699
5700 dflt = NULL;
5701 changed_type = 0;
5702
Radek Krejci327de162019-06-14 12:52:07 +02005703 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5704
Radek Krejcif538ce52019-03-05 10:46:14 +01005705 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5706 /* fix the target pointer in case of RPC's/action's input/output */
5707 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5708 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5709 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5710 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5711 }
5712 }
5713
Radek Krejciccd20f12019-02-15 14:12:27 +01005714 /* not-supported */
5715 if (devs[u]->not_supported) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005716 if (LY_ARRAY_COUNT(devs[u]->deviates) > 1) {
5717 LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.",
5718 LY_ARRAY_COUNT(devs[u]->deviates), devs[u]->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005719 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005720
5721#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5722 if (devs[u]->target->parent) { \
5723 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5724 } else { \
5725 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5726 } \
5727 LY_ARRAY_FOR(ARRAY, x) { \
5728 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5729 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005730 if (x < LY_ARRAY_COUNT(ARRAY)) { \
Radek Krejcifc11bd72019-04-11 16:00:05 +02005731 FREEFUNC(ctx->ctx, &ARRAY[x]); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005732 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
Radek Krejcifc11bd72019-04-11 16:00:05 +02005733 LY_ARRAY_DECREMENT(ARRAY); \
5734 }
5735
Michal Vasko1bf09392020-03-27 12:38:10 +01005736 if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005737 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5738 /* remove RPC's/action's input */
5739 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5740 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005741 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5742 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005743 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5744 /* remove RPC's/action's output */
5745 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5746 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005747 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5748 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005749 } else {
5750 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005751 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005752 }
5753 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005754 /* remove Notification */
5755 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005756 } else {
5757 /* remove the target node */
5758 lysc_disconnect(devs[u]->target);
5759 lysc_node_free(ctx->ctx, devs[u]->target);
5760 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005761
Radek Krejci474f9b82019-07-24 11:36:37 +02005762 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5763 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005764 continue;
5765 }
5766
5767 /* list of deviates (not-supported is not present in the list) */
5768 LY_ARRAY_FOR(devs[u]->deviates, v) {
5769 d = devs[u]->deviates[v];
5770
5771 switch (d->mod) {
5772 case LYS_DEV_ADD:
5773 d_add = (struct lysp_deviate_add*)d;
5774 /* [units-stmt] */
5775 if (d_add->units) {
5776 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5777 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5778
5779 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5780 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5781 }
5782
5783 /* *must-stmt */
5784 if (d_add->musts) {
5785 switch (devs[u]->target->nodetype) {
5786 case LYS_CONTAINER:
5787 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005788 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5789 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005790 break;
5791 case LYS_LEAF:
5792 case LYS_LEAFLIST:
5793 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005794 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5795 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005796 break;
5797 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005798 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5799 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005800 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005801 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005802 case LYS_ACTION:
5803 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005804 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5805 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005806 break;
Michal Vaskocc048b22020-03-27 15:52:38 +01005807 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005808 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5809 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005810 break;
5811 }
5812 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005813 default:
5814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005815 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005816 goto cleanup;
5817 }
Michal Vasko004d3152020-06-11 19:59:22 +02005818 ly_set_add(&ctx->xpath, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01005819 }
5820
5821 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005822 if (d_add->uniques) {
5823 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5824 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5825 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005826
5827 /* *default-stmt */
5828 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005829 switch (devs[u]->target->nodetype) {
5830 case LYS_LEAF:
5831 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005832 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 +02005833 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005834 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005835 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005836 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5837 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5838 } else {
5839 /* prepare new default value storage */
5840 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005841 }
Radek Krejcia1911222019-07-22 17:24:50 +02005842 dflt = d_add->dflts[0];
5843 /* parsing is done at the end after possible replace of the leaf's type */
5844
Radek Krejci551b12c2019-02-19 16:11:21 +01005845 /* mark the new default values as leaf's own */
5846 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005847 break;
5848 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005849 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005850 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005851 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005852 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005853 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5854 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5855 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005856 }
Radek Krejcia1911222019-07-22 17:24:50 +02005857 LY_ARRAY_FREE(llist->dflts);
5858 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005859 LY_ARRAY_FREE(llist->dflts_mods);
5860 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005861 }
5862 /* add new default value(s) */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005863 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup);
5864 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup);
5865 for (x = y = LY_ARRAY_COUNT(llist->dflts);
5866 x < LY_ARRAY_COUNT(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005867 LY_ARRAY_INCREMENT(llist->dflts_mods);
5868 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005869 LY_ARRAY_INCREMENT(llist->dflts);
5870 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5871 llist->dflts[x]->realtype = llist->type;
5872 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 +02005873 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005874 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005875 llist->dflts[x]->realtype->refcount++;
5876 if (err) {
5877 ly_err_print(err);
5878 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5879 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5880 d_add->dflts[x - y], err->msg);
5881 ly_err_free(err);
5882 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005883 if (rc == LY_EINCOMPLETE) {
5884 /* postpone default compilation when the tree is complete */
5885 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5886
5887 /* but in general result is so far ok */
5888 rc = LY_SUCCESS;
5889 }
Radek Krejcia1911222019-07-22 17:24:50 +02005890 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005891 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005892 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005893 devs[u]->target->flags |= LYS_SET_DFLT;
5894 break;
5895 case LYS_CHOICE:
5896 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5897 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5898 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5899 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005900 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 +01005901 goto cleanup;
5902 }
5903 break;
5904 default:
5905 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005906 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005907 goto cleanup;
5908 }
5909 }
5910
5911 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005912 if (d_add->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01005913 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005914 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005915 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5916 goto cleanup;
5917 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005918 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005919 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
Michal Vaskoa3881362020-01-21 15:57:35 +01005920 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005921 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005922 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5923 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005924 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5925 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005926 goto cleanup;
5927 }
Radek Krejci327de162019-06-14 12:52:07 +02005928 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005929 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005930
5931 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005932 if (d_add->flags & LYS_MAND_MASK) {
5933 if (devs[u]->target->flags & LYS_MAND_MASK) {
5934 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005935 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5936 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005937 goto cleanup;
5938 }
Radek Krejci327de162019-06-14 12:52:07 +02005939 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005940 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005941
5942 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005943 if (d_add->flags & LYS_SET_MIN) {
5944 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5945 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5946 /* change value */
5947 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5948 } else if (devs[u]->target->nodetype == LYS_LIST) {
5949 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5950 /* change value */
5951 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5952 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005953 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005954 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5955 goto cleanup;
5956 }
5957 if (d_add->min) {
5958 devs[u]->target->flags |= LYS_MAND_TRUE;
5959 }
5960 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005961
5962 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005963 if (d_add->flags & LYS_SET_MAX) {
5964 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5965 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5966 /* change value */
5967 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5968 } else if (devs[u]->target->nodetype == LYS_LIST) {
5969 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5970 /* change value */
5971 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5972 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005973 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005974 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5975 goto cleanup;
5976 }
5977 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005978
5979 break;
5980 case LYS_DEV_DELETE:
5981 d_del = (struct lysp_deviate_del*)d;
5982
5983 /* [units-stmt] */
5984 if (d_del->units) {
5985 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02005986 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
5987 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
5988 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5989 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
5990 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5991 goto cleanup;
5992 }
5993 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5994 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005995 }
5996
5997 /* *must-stmt */
5998 if (d_del->musts) {
5999 switch (devs[u]->target->nodetype) {
6000 case LYS_CONTAINER:
6001 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006002 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006003 break;
6004 case LYS_LEAF:
6005 case LYS_LEAFLIST:
6006 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006007 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006008 break;
6009 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006010 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006011 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006012 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006013 case LYS_ACTION:
6014 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6015 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6016 break;
6017 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6018 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6019 break;
6020 }
6021 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006022 default:
6023 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006024 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006025 goto cleanup;
6026 }
6027 }
6028
6029 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006030 if (d_del->uniques) {
6031 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6032 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6033 LY_ARRAY_FOR(d_del->uniques, x) {
6034 LY_ARRAY_FOR(list->uniques, z) {
6035 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6036 nodeid = strpbrk(name, " \t\n");
6037 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02006038 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006039 break;
6040 }
6041 while (isspace(*nodeid)) {
6042 ++nodeid;
6043 }
6044 } else {
6045 if (strcmp(name, list->uniques[z][y]->name)) {
6046 break;
6047 }
6048 }
6049 }
6050 if (!name) {
6051 /* complete match - remove the unique */
6052 LY_ARRAY_DECREMENT(list->uniques);
6053 LY_ARRAY_FREE(list->uniques[z]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006054 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
Radek Krejci7af64242019-02-18 13:07:53 +01006055 --z;
6056 break;
6057 }
6058 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006059 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006060 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006061 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6062 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006063 goto cleanup;
6064 }
6065 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006066 if (!LY_ARRAY_COUNT(list->uniques)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006067 LY_ARRAY_FREE(list->uniques);
6068 list->uniques = NULL;
6069 }
6070 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006071
6072 /* *default-stmt */
6073 if (d_del->dflts) {
6074 switch (devs[u]->target->nodetype) {
6075 case LYS_LEAF:
6076 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6077 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6078 dflt, "deleting", "default", d_del->dflts[0]);
6079
Radek Krejcia1911222019-07-22 17:24:50 +02006080 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006081 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006082 if (strcmp(dflt, d_del->dflts[0])) {
6083 if (i) {
6084 free((char*)dflt);
6085 }
6086 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6087 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6088 d_del->dflts[0], dflt);
6089 goto cleanup;
6090 }
6091 if (i) {
6092 free((char*)dflt);
6093 }
6094 dflt = NULL;
6095
Radek Krejci474f9b82019-07-24 11:36:37 +02006096 /* update the list of incomplete default values if needed */
6097 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6098
Radek Krejcia1911222019-07-22 17:24:50 +02006099 /* remove the default specification */
6100 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6101 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6102 free(leaf->dflt);
6103 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006104 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006105 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006106 break;
6107 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006108 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6109 LY_ARRAY_FOR(d_del->dflts, x) {
6110 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006111 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 +02006112 if (!strcmp(dflt, d_del->dflts[x])) {
6113 if (i) {
6114 free((char*)dflt);
6115 }
6116 dflt = NULL;
6117 break;
6118 }
6119 if (i) {
6120 free((char*)dflt);
6121 }
6122 dflt = NULL;
6123 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006124 if (y == LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02006125 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6126 "which does not match any of the target's property values.", d_del->dflts[x]);
6127 goto cleanup;
6128 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006129
6130 /* update the list of incomplete default values if needed */
6131 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6132
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006133 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006134 LY_ARRAY_DECREMENT(llist->dflts);
6135 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6136 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6137 free(llist->dflts[y]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006138 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
6139 memmove(&llist->dflts_mods[y], &llist->dflts_mods[y + 1], (LY_ARRAY_COUNT(llist->dflts_mods) - y) * (sizeof *llist->dflts_mods));
Radek Krejcia1911222019-07-22 17:24:50 +02006140 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006141 if (!LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006142 LY_ARRAY_FREE(llist->dflts_mods);
6143 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006144 LY_ARRAY_FREE(llist->dflts);
6145 llist->dflts = NULL;
6146 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006147 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006148 break;
6149 case LYS_CHOICE:
6150 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6151 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6152 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006153 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006154 if (prefix) {
6155 /* use module prefixes from the deviation module to match the module of the default case */
6156 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6157 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006158 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6159 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006160 goto cleanup;
6161 }
6162 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6163 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006164 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6165 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006166 goto cleanup;
6167 }
6168 }
6169 /* else {
6170 * strictly, the default prefix would point to the deviation module, but the value should actually
6171 * match the default string in the original module (usually unprefixed), so in this case we do not check
6172 * the module of the default case, just matching its name */
6173 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6174 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006175 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6176 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006177 goto cleanup;
6178 }
6179 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6180 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6181 break;
6182 default:
6183 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006184 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006185 goto cleanup;
6186 }
6187 }
6188
6189 break;
6190 case LYS_DEV_REPLACE:
6191 d_rpl = (struct lysp_deviate_rpl*)d;
6192
6193 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006194 if (d_rpl->type) {
6195 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6196 /* type is mandatory, so checking for its presence is not necessary */
6197 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006198
6199 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6200 /* the target has default from the previous type - remove it */
6201 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006202 /* update the list of incomplete default values if needed */
6203 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6204
Radek Krejcia1911222019-07-22 17:24:50 +02006205 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6206 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6207 free(leaf->dflt);
6208 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006209 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006210 } else { /* LYS_LEAFLIST */
6211 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006212 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006213 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6214 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6215 free(llist->dflts[x]);
6216 }
6217 LY_ARRAY_FREE(llist->dflts);
6218 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006219 LY_ARRAY_FREE(llist->dflts_mods);
6220 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006221 }
6222 }
6223 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006224 /* there is no default value, do not set changed_type after type compilation
6225 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006226 changed_type = -1;
6227 }
Radek Krejciec4da802019-05-02 13:02:41 +02006228 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 +02006229 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006230 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006231
6232 /* [units-stmt] */
6233 if (d_rpl->units) {
6234 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6235 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6236 units, "replacing", "units", d_rpl->units);
6237
6238 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6239 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6240 }
6241
6242 /* [default-stmt] */
6243 if (d_rpl->dflt) {
6244 switch (devs[u]->target->nodetype) {
6245 case LYS_LEAF:
6246 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6247 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006248 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006249 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006250 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6251 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6252 dflt = d_rpl->dflt;
6253 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006254 break;
6255 case LYS_CHOICE:
6256 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006257 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 +01006258 goto cleanup;
6259 }
6260 break;
6261 default:
6262 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006263 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006264 goto cleanup;
6265 }
6266 }
6267
6268 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006269 if (d_rpl->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01006270 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006271 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006272 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6273 goto cleanup;
6274 }
6275 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006276 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006277 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6278 goto cleanup;
6279 }
Radek Krejci327de162019-06-14 12:52:07 +02006280 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006281 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006282
6283 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006284 if (d_rpl->flags & LYS_MAND_MASK) {
6285 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006286 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006287 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6288 goto cleanup;
6289 }
Radek Krejci327de162019-06-14 12:52:07 +02006290 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006291 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006292
6293 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006294 if (d_rpl->flags & LYS_SET_MIN) {
6295 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6296 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6297 /* change value */
6298 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6299 } else if (devs[u]->target->nodetype == LYS_LIST) {
6300 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6301 /* change value */
6302 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6303 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006304 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006305 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6306 goto cleanup;
6307 }
6308 if (d_rpl->min) {
6309 devs[u]->target->flags |= LYS_MAND_TRUE;
6310 }
6311 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006312
6313 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006314 if (d_rpl->flags & LYS_SET_MAX) {
6315 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6316 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6317 /* change value */
6318 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6319 } else if (devs[u]->target->nodetype == LYS_LIST) {
6320 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6321 /* change value */
6322 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6323 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006324 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006325 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6326 goto cleanup;
6327 }
6328 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006329
6330 break;
6331 default:
6332 LOGINT(ctx->ctx);
6333 goto cleanup;
6334 }
6335 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006336
Radek Krejci33f72892019-02-21 10:36:58 +01006337 /* final check when all deviations of a single target node are applied */
6338
Radek Krejci551b12c2019-02-19 16:11:21 +01006339 /* check min-max compatibility */
6340 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6341 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6342 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6343 } else if (devs[u]->target->nodetype == LYS_LIST) {
6344 min = ((struct lysc_node_list*)devs[u]->target)->min;
6345 max = ((struct lysc_node_list*)devs[u]->target)->max;
6346 } else {
6347 min = max = 0;
6348 }
6349 if (min > max) {
6350 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 +02006351 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006352 goto cleanup;
6353 }
6354
Radek Krejcia1911222019-07-22 17:24:50 +02006355 if (dflt) {
6356 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006357 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006358 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006359 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6360 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006361 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006362 leaf->dflt->realtype->refcount++;
6363 if (err) {
6364 ly_err_print(err);
6365 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6366 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6367 ly_err_free(err);
6368 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006369 if (rc == LY_EINCOMPLETE) {
6370 /* postpone default compilation when the tree is complete */
6371 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6372
6373 /* but in general result is so far ok */
6374 rc = LY_SUCCESS;
6375 }
Radek Krejcia1911222019-07-22 17:24:50 +02006376 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006377 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006378 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6379 int dynamic;
6380 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006381 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006382
6383 /* update the list of incomplete default values if needed */
6384 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6385
6386 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006387 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6388 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6389 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006390 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6391 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006392 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006393 leaf->dflt->realtype->refcount++;
6394 if (err) {
6395 ly_err_print(err);
6396 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6397 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6398 ly_err_free(err);
6399 }
6400 if (dynamic) {
6401 free((void*)dflt);
6402 }
Radek Krejcia1911222019-07-22 17:24:50 +02006403 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006404 if (rc == LY_EINCOMPLETE) {
6405 /* postpone default compilation when the tree is complete */
6406 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6407
6408 /* but in general result is so far ok */
6409 rc = LY_SUCCESS;
6410 }
6411 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006412 } else { /* LYS_LEAFLIST */
6413 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006414 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 +02006415 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6416 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6417 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006418 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6419 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006420 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6421 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006422 llist->dflts[x]->realtype->refcount++;
6423 if (err) {
6424 ly_err_print(err);
6425 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6426 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6427 dflt, err->msg);
6428 ly_err_free(err);
6429 }
6430 if (dynamic) {
6431 free((void*)dflt);
6432 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006433 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006434 if (rc == LY_EINCOMPLETE) {
6435 /* postpone default compilation when the tree is complete */
6436 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6437
6438 /* but in general result is so far ok */
6439 rc = LY_SUCCESS;
6440 }
Radek Krejcia1911222019-07-22 17:24:50 +02006441 LY_CHECK_GOTO(rc, cleanup);
6442 }
6443 }
6444 }
6445
Radek Krejci551b12c2019-02-19 16:11:21 +01006446 /* check mandatory - default compatibility */
6447 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6448 && (devs[u]->target->flags & LYS_SET_DFLT)
6449 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6450 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006451 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006452 goto cleanup;
6453 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6454 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6455 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006456 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 +01006457 goto cleanup;
6458 }
6459 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6460 /* mandatory node under a default case */
6461 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006462 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6463 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006464 goto cleanup;
6465 }
Radek Krejci33f72892019-02-21 10:36:58 +01006466
Michal Vaskoe6143202020-07-03 13:02:08 +02006467 /* add this module into the target module deviated_by, if not there already */
6468 rc = LY_SUCCESS;
6469 LY_ARRAY_FOR(devs[u]->target->module->compiled->deviated_by, v) {
6470 if (devs[u]->target->module->compiled->deviated_by[v] == mod_p->mod) {
6471 rc = LY_EEXIST;
6472 break;
6473 }
6474 }
6475 if (!rc) {
6476 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup);
6477 *dev_mod = mod_p->mod;
6478 }
Michal Vasko57c10cd2020-05-27 15:57:11 +02006479
Radek Krejci327de162019-06-14 12:52:07 +02006480 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006481 }
6482
Radek Krejci327de162019-06-14 12:52:07 +02006483 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006484 ret = LY_SUCCESS;
6485
6486cleanup:
6487 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6488 LY_ARRAY_FREE(devs[u]->deviates);
6489 free(devs[u]);
6490 }
6491 free(devs);
6492 ly_set_erase(&targets, NULL);
6493 ly_set_erase(&devs_p, NULL);
6494
6495 return ret;
6496}
6497
Radek Krejcib56c7502019-02-13 14:19:54 +01006498/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006499 * @brief Compile the given YANG submodule into the main module.
6500 * @param[in] ctx Compile context
6501 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006502 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6503 */
6504LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006505lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006506{
6507 unsigned int u;
6508 LY_ERR ret = LY_SUCCESS;
6509 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006510 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006511 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006512 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006513
Michal Vasko33ff9422020-07-03 09:50:39 +02006514 if (!mainmod->mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01006515 /* features are compiled directly into the compiled module structure,
6516 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6517 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006518 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6519 LY_CHECK_GOTO(ret, error);
6520 }
Radek Krejci0af46292019-01-11 16:02:31 +01006521
Michal Vasko33ff9422020-07-03 09:50:39 +02006522 if (!mainmod->mod->dis_identities) {
6523 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities);
6524 LY_CHECK_GOTO(ret, error);
6525 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01006526
Radek Krejci474f9b82019-07-24 11:36:37 +02006527 /* data nodes */
6528 LY_LIST_FOR(submod->data, node_p) {
6529 ret = lys_compile_node(ctx, node_p, NULL, 0);
6530 LY_CHECK_GOTO(ret, error);
6531 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006532
Radek Krejciec4da802019-05-02 13:02:41 +02006533 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6534 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006535
Radek Krejcid05cbd92018-12-05 14:26:40 +01006536error:
6537 return ret;
6538}
6539
Radek Krejci335332a2019-09-05 13:03:35 +02006540static void *
6541lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6542{
6543 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6544 if (substmts[u].stmt == stmt) {
6545 return substmts[u].storage;
6546 }
6547 }
6548 return NULL;
6549}
6550
6551LY_ERR
6552lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6553{
6554 LY_ERR ret = LY_EVALID, r;
6555 unsigned int u;
6556 struct lysp_stmt *stmt;
6557 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006558
6559 /* check for invalid substatements */
6560 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006561 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6562 continue;
6563 }
Radek Krejci335332a2019-09-05 13:03:35 +02006564 for (u = 0; substmts[u].stmt; ++u) {
6565 if (substmts[u].stmt == stmt->kw) {
6566 break;
6567 }
6568 }
6569 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006570 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6571 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006572 goto cleanup;
6573 }
Radek Krejci335332a2019-09-05 13:03:35 +02006574 }
6575
Radek Krejciad5963b2019-09-06 16:03:05 +02006576 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6577
Radek Krejci335332a2019-09-05 13:03:35 +02006578 /* keep order of the processing the same as the order in the defined substmts,
6579 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6580 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006581 int stmt_present = 0;
6582
Radek Krejci335332a2019-09-05 13:03:35 +02006583 for (stmt = ext->child; stmt; stmt = stmt->next) {
6584 if (substmts[u].stmt != stmt->kw) {
6585 continue;
6586 }
6587
Radek Krejciad5963b2019-09-06 16:03:05 +02006588 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006589 if (substmts[u].storage) {
6590 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006591 case LY_STMT_STATUS:
6592 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6593 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6594 break;
6595 case LY_STMT_UNITS: {
6596 const char **units;
6597
6598 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6599 /* single item */
6600 if (*((const char **)substmts[u].storage)) {
6601 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6602 goto cleanup;
6603 }
6604 units = (const char **)substmts[u].storage;
6605 } else {
6606 /* sized array */
6607 const char ***units_array = (const char ***)substmts[u].storage;
6608 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6609 }
6610 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6611 break;
6612 }
Radek Krejci335332a2019-09-05 13:03:35 +02006613 case LY_STMT_TYPE: {
6614 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6615 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6616
6617 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6618 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006619 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006620 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6621 goto cleanup;
6622 }
6623 compiled = substmts[u].storage;
6624 } else {
6625 /* sized array */
6626 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6627 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6628 compiled = (void*)type;
6629 }
6630
Radek Krejciad5963b2019-09-06 16:03:05 +02006631 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006632 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6633 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006634 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6635 lysp_type_free(ctx->ctx, parsed);
6636 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006637 break;
6638 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006639 case LY_STMT_IF_FEATURE: {
6640 struct lysc_iffeature *iff = NULL;
6641
6642 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6643 /* single item */
6644 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6645 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6646 goto cleanup;
6647 }
6648 iff = (struct lysc_iffeature*)substmts[u].storage;
6649 } else {
6650 /* sized array */
6651 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6652 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6653 }
6654 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6655 break;
6656 }
6657 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6658 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006659 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006660 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.",
6661 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006662 goto cleanup;
6663 }
6664 }
Radek Krejci335332a2019-09-05 13:03:35 +02006665 }
Radek Krejci335332a2019-09-05 13:03:35 +02006666
Radek Krejciad5963b2019-09-06 16:03:05 +02006667 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6668 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6669 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6670 goto cleanup;
6671 }
Radek Krejci335332a2019-09-05 13:03:35 +02006672 }
6673
6674 ret = LY_SUCCESS;
6675
6676cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006677 return ret;
6678}
6679
Michal Vasko175012e2019-11-06 15:49:14 +01006680/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006681 * @brief Check when for cyclic dependencies.
6682 * @param[in] set Set with all the referenced nodes.
6683 * @param[in] node Node whose "when" referenced nodes are in @p set.
6684 * @return LY_ERR value
6685 */
6686static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006687lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006688{
6689 struct lyxp_set tmp_set;
6690 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006691 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006692 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006693 int idx;
6694 struct lysc_when *when;
6695 LY_ERR ret = LY_SUCCESS;
6696
6697 memset(&tmp_set, 0, sizeof tmp_set);
6698
6699 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006700 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006701 xp_scnode = &set->val.scnodes[i];
6702
Michal Vasko5c4e5892019-11-14 12:31:38 +01006703 if (xp_scnode->in_ctx != -1) {
6704 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006705 xp_scnode->in_ctx = 1;
6706 }
6707 }
6708
6709 for (i = 0; i < set->used; ++i) {
6710 xp_scnode = &set->val.scnodes[i];
6711 if (xp_scnode->in_ctx != 1) {
6712 /* already checked */
6713 continue;
6714 }
6715
Michal Vasko1bf09392020-03-27 12:38:10 +01006716 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006717 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006718 /* no when to check */
6719 xp_scnode->in_ctx = 0;
6720 continue;
6721 }
6722
6723 node = xp_scnode->scnode;
6724 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006725 LY_ARRAY_FOR(node->when, u) {
6726 when = node->when[u];
Michal Vasko52927e22020-03-16 17:26:14 +01006727 ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006728 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6729 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006730 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006731 goto cleanup;
6732 }
6733
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006734 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006735 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006736 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006737 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006738 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 +01006739 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006740 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 +01006741 ret = LY_EVALID;
6742 goto cleanup;
6743 }
6744
6745 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006746 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006747 } else {
6748 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006749 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006750 }
6751 }
6752
6753 /* merge this set into the global when set */
6754 lyxp_set_scnode_merge(set, &tmp_set);
6755 }
6756
6757 /* check when of non-data parents as well */
6758 node = node->parent;
6759 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6760
Michal Vasko251f56e2019-11-14 16:06:47 +01006761 /* this node when was checked (xp_scnode could have been reallocd) */
6762 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006763 }
6764
6765cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006766 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006767 return ret;
6768}
6769
6770/**
Michal Vasko175012e2019-11-06 15:49:14 +01006771 * @brief Check when/must expressions of a node on a compiled schema tree.
6772 * @param[in] ctx Compile context.
6773 * @param[in] node Node to check.
6774 * @return LY_ERR value
6775 */
6776static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006777lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006778{
Michal Vasko175012e2019-11-06 15:49:14 +01006779 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006780 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006781 LY_ARRAY_COUNT_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01006782 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006783 struct lysc_when **when = NULL;
6784 struct lysc_must *musts = NULL;
6785 LY_ERR ret = LY_SUCCESS;
6786
6787 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006788 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko175012e2019-11-06 15:49:14 +01006789
6790 switch (node->nodetype) {
6791 case LYS_CONTAINER:
6792 when = ((struct lysc_node_container *)node)->when;
6793 musts = ((struct lysc_node_container *)node)->musts;
6794 break;
6795 case LYS_CHOICE:
6796 when = ((struct lysc_node_choice *)node)->when;
6797 break;
6798 case LYS_LEAF:
6799 when = ((struct lysc_node_leaf *)node)->when;
6800 musts = ((struct lysc_node_leaf *)node)->musts;
6801 break;
6802 case LYS_LEAFLIST:
6803 when = ((struct lysc_node_leaflist *)node)->when;
6804 musts = ((struct lysc_node_leaflist *)node)->musts;
6805 break;
6806 case LYS_LIST:
6807 when = ((struct lysc_node_list *)node)->when;
6808 musts = ((struct lysc_node_list *)node)->musts;
6809 break;
6810 case LYS_ANYXML:
6811 case LYS_ANYDATA:
6812 when = ((struct lysc_node_anydata *)node)->when;
6813 musts = ((struct lysc_node_anydata *)node)->musts;
6814 break;
6815 case LYS_CASE:
6816 when = ((struct lysc_node_case *)node)->when;
6817 break;
6818 case LYS_NOTIF:
6819 musts = ((struct lysc_notif *)node)->musts;
6820 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006821 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006822 case LYS_ACTION:
6823 /* first process input musts */
6824 musts = ((struct lysc_action *)node)->input.musts;
6825 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006826 default:
6827 /* nothing to check */
6828 break;
6829 }
6830
Michal Vasko175012e2019-11-06 15:49:14 +01006831 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006832 LY_ARRAY_FOR(when, u) {
Michal Vasko004d3152020-06-11 19:59:22 +02006833 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 +02006834 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006835 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006836 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006837 goto cleanup;
6838 }
6839
Michal Vaskodc052f32019-11-07 11:11:38 +01006840 ctx->path[0] = '\0';
6841 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006842 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006843 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006844 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6845 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006846
Michal Vaskoecd62de2019-11-13 12:35:11 +01006847 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006848 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006849 schema->name);
6850 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006851
6852 /* check dummy node accessing */
6853 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006854 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006855 ret = LY_EVALID;
6856 goto cleanup;
6857 }
Michal Vasko175012e2019-11-06 15:49:14 +01006858 }
6859 }
6860
Michal Vaskoecd62de2019-11-13 12:35:11 +01006861 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006862 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006863 LY_CHECK_GOTO(ret, cleanup);
6864
Michal Vaskod3678892020-05-21 10:06:58 +02006865 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006866 }
6867
Michal Vasko5d8756a2019-11-07 15:21:00 +01006868check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006869 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006870 LY_ARRAY_FOR(musts, u) {
6871 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 +01006872 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006873 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006874 goto cleanup;
6875 }
6876
Michal Vaskodc052f32019-11-07 11:11:38 +01006877 ctx->path[0] = '\0';
6878 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006879 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006880 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006881 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006882 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006883 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6884 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006885 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006886 }
6887 }
6888
Michal Vaskod3678892020-05-21 10:06:58 +02006889 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006890 }
6891
Michal Vasko1bf09392020-03-27 12:38:10 +01006892 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006893 /* now check output musts */
6894 input_done = 1;
6895 musts = ((struct lysc_action *)node)->output.musts;
6896 opts = LYXP_SCNODE_OUTPUT;
6897 goto check_musts;
6898 }
6899
Michal Vasko175012e2019-11-06 15:49:14 +01006900cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006901 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006902 return ret;
6903}
6904
Michal Vasko8d544252020-03-02 10:19:52 +01006905static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006906lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6907{
6908 const struct lysc_node *target = NULL;
6909 struct ly_path *p;
6910 struct lysc_type *type;
6911
6912 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6913
6914 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02006915 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
6916 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
6917 lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02006918
6919 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006920 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02006921 ly_path_free(node->module->ctx, p);
6922
6923 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
6924 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6925 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
6926 lref->path->expr, lys_nodetype2str(target->nodetype));
6927 return LY_EVALID;
6928 }
6929
6930 /* check status */
6931 ctx->path[0] = '\0';
6932 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6933 ctx->path_len = strlen(ctx->path);
6934 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
6935 return LY_EVALID;
6936 }
6937 ctx->path_len = 1;
6938 ctx->path[1] = '\0';
6939
6940 /* check config */
6941 if (lref->require_instance && (node->flags & LYS_CONFIG_W)) {
6942 if (target->flags & LYS_CONFIG_R) {
6943 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
6944 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
6945 return LY_EVALID;
6946 }
6947 }
6948
6949 /* store the target's type and check for circular chain of leafrefs */
6950 lref->realtype = ((struct lysc_node_leaf *)target)->type;
6951 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
6952 if (type == (struct lysc_type *)lref) {
6953 /* circular chain detected */
6954 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6955 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
6956 return LY_EVALID;
6957 }
6958 }
6959
6960 /* check if leafref and its target are under common if-features */
6961 if (lys_compile_leafref_features_validate(node, target)) {
6962 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6963 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
6964 " features applicable to the leafref itself.", lref->path->expr);
6965 return LY_EVALID;
6966 }
6967
6968 return LY_SUCCESS;
6969}
6970
6971static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01006972lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
6973{
6974 struct lysc_ext_instance *ext;
6975 struct lysp_ext_instance *ext_p = NULL;
6976 struct lysp_stmt *stmt;
6977 const struct lys_module *ext_mod;
6978 LY_ERR ret = LY_SUCCESS;
6979
6980 /* create the parsed extension instance manually */
6981 ext_p = calloc(1, sizeof *ext_p);
6982 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
6983 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
6984 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
6985 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
6986 ext_p->insubstmt_index = 0;
6987
6988 stmt = calloc(1, sizeof *ext_p->child);
6989 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
6990 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
6991 stmt->kw = LY_STMT_TYPE;
6992 ext_p->child = stmt;
6993
6994 /* allocate new extension instance */
6995 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
6996
6997 /* manually get extension definition module */
6998 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
6999
7000 /* compile the extension instance */
7001 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7002
7003cleanup:
7004 lysp_ext_instance_free(ctx->ctx, ext_p);
7005 free(ext_p);
7006 return ret;
7007}
7008
Michal Vasko004d3152020-06-11 19:59:22 +02007009static LY_ERR
7010lys_compile_unres(struct lysc_ctx *ctx)
7011{
7012 struct lysc_node *node;
7013 struct lysc_type *type, *typeiter;
7014 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007015 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007016 uint32_t i;
7017
7018 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7019 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7020 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7021 for (i = 0; i < ctx->leafrefs.count; ++i) {
7022 node = ctx->leafrefs.objs[i];
7023 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7024 type = ((struct lysc_node_leaf *)node)->type;
7025 if (type->basetype == LY_TYPE_LEAFREF) {
7026 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7027 } else if (type->basetype == LY_TYPE_UNION) {
7028 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7029 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7030 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7031 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7032 }
7033 }
7034 }
7035 }
7036 for (i = 0; i < ctx->leafrefs.count; ++i) {
7037 /* store pointer to the real type */
7038 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7039 if (type->basetype == LY_TYPE_LEAFREF) {
7040 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7041 typeiter->basetype == LY_TYPE_LEAFREF;
7042 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7043 ((struct lysc_type_leafref*)type)->realtype = typeiter;
7044 } else if (type->basetype == LY_TYPE_UNION) {
7045 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7046 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7047 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7048 typeiter->basetype == LY_TYPE_LEAFREF;
7049 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7050 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7051 }
7052 }
7053 }
7054 }
7055
7056 /* check xpath */
7057 for (i = 0; i < ctx->xpath.count; ++i) {
7058 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7059 }
7060
7061 /* finish incomplete default values compilation */
7062 for (i = 0; i < ctx->dflts.count; ++i) {
7063 struct ly_err_item *err = NULL;
7064 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
7065 LY_ERR ret;
7066 ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
7067 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7068 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7069 if (err) {
7070 ly_err_print(err);
7071 ctx->path[0] = '\0';
7072 lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7073 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7074 "Invalid default - value does not fit the type (%s).", err->msg);
7075 ly_err_free(err);
7076 }
7077 LY_CHECK_RET(ret);
7078 }
7079
7080 return LY_SUCCESS;
7081}
7082
Radek Krejci19a96102018-11-15 13:38:09 +01007083LY_ERR
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007084lys_compile(struct lys_module **mod, int options)
Radek Krejci19a96102018-11-15 13:38:09 +01007085{
7086 struct lysc_ctx ctx = {0};
7087 struct lysc_module *mod_c;
7088 struct lysp_module *sp;
7089 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007090 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007091 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007092 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007093 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007094 uint32_t i;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007095 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007096
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007097 LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007098
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007099 if (!(*mod)->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007100 /* just imported modules are not compiled */
7101 return LY_SUCCESS;
7102 }
7103
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007104 sp = (*mod)->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007105
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007106 ctx.ctx = (*mod)->ctx;
7107 ctx.mod = *mod;
7108 ctx.mod_def = *mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007109 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007110 ctx.path_len = 1;
7111 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007112
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007113 (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
7114 LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
7115 mod_c->mod = *mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007116
Radek Krejciec4da802019-05-02 13:02:41 +02007117 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007118 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007119 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007120 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7121 }
Radek Krejci0935f412019-08-20 16:15:18 +02007122
7123 /* features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007124 if ((*mod)->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007125 /* there is already precompiled array of features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007126 mod_c->features = (*mod)->dis_features;
7127 (*mod)->dis_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007128 } else {
7129 /* features are compiled directly into the compiled module structure,
7130 * 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 +02007131 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007132 LY_CHECK_GOTO(ret, error);
7133 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007134
Radek Krejci0af46292019-01-11 16:02:31 +01007135 /* finish feature compilation, not only for the main module, but also for the submodules.
7136 * Due to possible forward references, it must be done when all the features (including submodules)
7137 * are present. */
7138 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007139 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007140 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7141 }
Radek Krejci327de162019-06-14 12:52:07 +02007142 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007143 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007144 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007145 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007146 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007147 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7148 }
Radek Krejci327de162019-06-14 12:52:07 +02007149 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007150 }
Radek Krejci327de162019-06-14 12:52:07 +02007151 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007152
Michal Vasko33ff9422020-07-03 09:50:39 +02007153 /* identities, work similarly to features with the precompilation */
7154 if ((*mod)->dis_identities) {
7155 mod_c->identities = (*mod)->dis_identities;
7156 (*mod)->dis_identities = NULL;
7157 } else {
7158 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
7159 LY_CHECK_GOTO(ret, error);
7160 }
Radek Krejci19a96102018-11-15 13:38:09 +01007161 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007162 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007163 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007164 lysc_update_path(&ctx, NULL, "{submodule}");
7165 LY_ARRAY_FOR(sp->includes, v) {
7166 if (sp->includes[v].submodule->identities) {
7167 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7168 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities);
7169 LY_CHECK_GOTO(ret, error);
7170 lysc_update_path(&ctx, NULL, NULL);
7171 }
7172 }
Radek Krejci327de162019-06-14 12:52:07 +02007173 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007174
Radek Krejci95710c92019-02-11 15:49:55 +01007175 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007176 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007177 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007178 }
Radek Krejci95710c92019-02-11 15:49:55 +01007179
Radek Krejciec4da802019-05-02 13:02:41 +02007180 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7181 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007182
Radek Krejci95710c92019-02-11 15:49:55 +01007183 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007184 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007185 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007186 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007187 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007188
Radek Krejci474f9b82019-07-24 11:36:37 +02007189 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007190 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007191
Radek Krejci0935f412019-08-20 16:15:18 +02007192 /* extension instances TODO cover extension instances from submodules */
7193 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007194
Michal Vasko004d3152020-06-11 19:59:22 +02007195 /* finish compilation for all unresolved items in the context */
7196 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007197
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007198 /* validate non-instantiated groupings from the parsed schema,
7199 * without it we would accept even the schemas with invalid grouping specification */
7200 ctx.options |= LYSC_OPT_GROUPING;
7201 LY_ARRAY_FOR(sp->groupings, u) {
7202 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007203 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007204 }
7205 }
7206 LY_LIST_FOR(sp->data, node_p) {
7207 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7208 LY_ARRAY_FOR(grps, u) {
7209 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007210 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007211 }
7212 }
7213 }
7214
Radek Krejci474f9b82019-07-24 11:36:37 +02007215 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7216 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7217 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7218 }
7219
Michal Vasko8d544252020-03-02 10:19:52 +01007220#if 0
7221 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7222 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7223 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7224 * the anotation definitions available in the internal schema structure. */
7225 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7226 if (lyp_add_ietf_netconf_annotations(mod)) {
7227 lys_free(mod, NULL, 1, 1);
7228 return NULL;
7229 }
7230 }
7231#endif
7232
7233 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007234 if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
7235 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007236 }
7237
Radek Krejci1c0c3442019-07-23 16:08:47 +02007238 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007239 ly_set_erase(&ctx.xpath, NULL);
7240 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007241 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007242 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007243 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007244
Radek Krejciec4da802019-05-02 13:02:41 +02007245 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007246 lysp_module_free((*mod)->parsed);
7247 (*mod)->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007248 }
7249
Radek Krejciec4da802019-05-02 13:02:41 +02007250 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007251 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007252 for (i = 0; i < ctx.ctx->list.count; ++i) {
7253 m = ctx.ctx->list.objs[i];
Radek Krejci95710c92019-02-11 15:49:55 +01007254 if (m->implemented == 2) {
7255 m->implemented = 1;
7256 }
7257 }
7258 }
7259
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007260 (*mod)->compiled = mod_c;
Radek Krejci19a96102018-11-15 13:38:09 +01007261 return LY_SUCCESS;
7262
7263error:
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007264 lys_feature_precompile_revert(&ctx, *mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007265 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007266 ly_set_erase(&ctx.xpath, NULL);
7267 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007268 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007269 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007270 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007271 lysc_module_free(mod_c, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007272 (*mod)->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007273
7274 /* revert compilation of modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007275 for (i = 0; i < ctx.ctx->list.count; ++i) {
7276 m = ctx.ctx->list.objs[i];
Michal Vasko2947ce12019-12-16 10:37:19 +01007277 if ((m->implemented == 2) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007278 /* revert features list to the precompiled state */
7279 lys_feature_precompile_revert(&ctx, m);
7280 /* mark module as imported-only / not-implemented */
7281 m->implemented = 0;
7282 /* free the compiled version of the module */
7283 lysc_module_free(m->compiled, NULL);
7284 m->compiled = NULL;
7285 }
7286 }
7287
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007288 /* remove the module itself from the context and free it */
7289 ly_set_rm(&ctx.ctx->list, *mod, NULL);
7290 lys_module_free(*mod, NULL);
7291 *mod = NULL;
7292
Radek Krejci19a96102018-11-15 13:38:09 +01007293 return ret;
7294}