blob: 1b99187f123ececd418302d4394971270d5ef6fd [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejci19a96102018-11-15 13:38:09 +010016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020026#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "dict.h"
29#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020030#include "path.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020031#include "parser.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020032#include "parser_schema.h"
Radek Krejci0935f412019-08-20 16:15:18 +020033#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020035#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020039#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010040#include "tree_schema_internal.h"
41#include "xpath.h"
42
Michal Vasko5fe75f12020-03-02 13:52:37 +010043static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
44 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
45
Radek Krejci19a96102018-11-15 13:38:09 +010046/**
47 * @brief Duplicate string into dictionary
48 * @param[in] CTX libyang context of the dictionary.
49 * @param[in] ORIG String to duplicate.
50 * @param[out] DUP Where to store the result.
51 */
52#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
53
Radek Krejciec4da802019-05-02 13:02:41 +020054#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010055 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020056 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
57 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
58 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci19a96102018-11-15 13:38:09 +010059 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020060 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010061 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
62 } \
63 }
64
Radek Krejciec4da802019-05-02 13:02:41 +020065#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010066 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020067 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
68 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
69 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010070 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020071 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010072 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
73 } \
74 }
75
Radek Krejci0935f412019-08-20 16:15:18 +020076#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
77 if (EXTS_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020078 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
79 for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \
Radek Krejci0935f412019-08-20 16:15:18 +020080 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010081 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \
Radek Krejci0935f412019-08-20 16:15:18 +020082 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
83 } \
84 }
85
Radek Krejciec4da802019-05-02 13:02:41 +020086#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010087 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020088 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
89 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
90 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010091 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020092 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010093 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
94 } \
95 }
96
Radek Krejciec4da802019-05-02 13:02:41 +020097#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010098 if (MEMBER_P) { \
99 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
100 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200101 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100102 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
103 }
104
Radek Krejciec4da802019-05-02 13:02:41 +0200105#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100106 if (MEMBER_P) { \
107 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200108 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100109 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200110 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100111 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
112 }
113
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100114#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100115 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200116 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100117 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100118 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
119 return LY_EVALID; \
120 } \
121 } \
122 }
123
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100124#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
125 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200126 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100127 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
128 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
129 return LY_EVALID; \
130 } \
131 } \
132 }
133
134struct lysc_ext *
135lysc_ext_dup(struct lysc_ext *orig)
136{
137 ++orig->refcount;
138 return orig;
139}
140
Radek Krejci19a96102018-11-15 13:38:09 +0100141static struct lysc_ext_instance *
142lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
143{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100144 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100145 (void) ctx;
146 (void) orig;
147 return NULL;
148}
149
Radek Krejcib56c7502019-02-13 14:19:54 +0100150/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200151 * @brief Add record into the compile context's list of incomplete default values.
152 * @param[in] ctx Compile context with the incomplete default values list.
153 * @param[in] context_node Context schema node to store in the record.
154 * @param[in] dflt Incomplete default value to store in the record.
155 * @param[in] dflt_mod Module of the default value definition to store in the record.
156 * @return LY_EMEM in case of memory allocation failure.
157 * @return LY_SUCCESS
158 */
159static LY_ERR
160lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
161{
162 struct lysc_incomplete_dflt *r;
163 r = malloc(sizeof *r);
164 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
165 r->context_node = context_node;
166 r->dflt = dflt;
167 r->dflt_mod = dflt_mod;
168 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
169
170 return LY_SUCCESS;
171}
172
173/**
174 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
175 * @param[in] ctx Compile context with the incomplete default values list.
176 * @param[in] dflt Incomplete default values identifying the record to remove.
177 */
178static void
179lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
180{
181 unsigned int u;
182 struct lysc_incomplete_dflt *r;
183
184 for (u = 0; u < ctx->dflts.count; ++u) {
185 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
186 if (r->dflt == dflt) {
187 free(ctx->dflts.objs[u]);
188 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
189 --ctx->dflts.count;
190 return;
191 }
192 }
193}
194
Radek Krejci0e59c312019-08-15 15:34:15 +0200195void
Radek Krejci327de162019-06-14 12:52:07 +0200196lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
197{
198 int len;
199 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
200
201 if (!name) {
202 /* removing last path segment */
203 if (ctx->path[ctx->path_len - 1] == '}') {
204 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
205 if (ctx->path[ctx->path_len] == '=') {
206 ctx->path[ctx->path_len++] = '}';
207 } else {
208 /* not a top-level special tag, remove also preceiding '/' */
209 goto remove_nodelevel;
210 }
211 } else {
212remove_nodelevel:
213 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
214 if (ctx->path_len == 0) {
215 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200216 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200217 }
218 }
219 /* set new terminating NULL-byte */
220 ctx->path[ctx->path_len] = '\0';
221 } else {
222 if (ctx->path_len > 1) {
223 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
224 /* extension of the special tag */
225 nextlevel = 2;
226 --ctx->path_len;
227 } else {
228 /* there is already some path, so add next level */
229 nextlevel = 1;
230 }
231 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
232
233 if (nextlevel != 2) {
234 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
235 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200236 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200237 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200238 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200239 }
240 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200241 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200242 }
Radek Krejciacc79042019-07-25 14:14:57 +0200243 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
244 /* output truncated */
245 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
246 } else {
247 ctx->path_len += len;
248 }
Radek Krejci327de162019-06-14 12:52:07 +0200249 }
250}
251
252/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100253 * @brief Duplicate the compiled pattern structure.
254 *
255 * Instead of duplicating memory, the reference counter in the @p orig is increased.
256 *
257 * @param[in] orig The pattern structure to duplicate.
258 * @return The duplicated structure to use.
259 */
Radek Krejci19a96102018-11-15 13:38:09 +0100260static struct lysc_pattern*
261lysc_pattern_dup(struct lysc_pattern *orig)
262{
263 ++orig->refcount;
264 return orig;
265}
266
Radek Krejcib56c7502019-02-13 14:19:54 +0100267/**
268 * @brief Duplicate the array of compiled patterns.
269 *
270 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
271 *
272 * @param[in] ctx Libyang context for logging.
273 * @param[in] orig The patterns sized array to duplicate.
274 * @return New sized array as a copy of @p orig.
275 * @return NULL in case of memory allocation error.
276 */
Radek Krejci19a96102018-11-15 13:38:09 +0100277static struct lysc_pattern**
278lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
279{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100280 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200281 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100282
Radek Krejcib56c7502019-02-13 14:19:54 +0100283 assert(orig);
284
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200285 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100286 LY_ARRAY_FOR(orig, u) {
287 dup[u] = lysc_pattern_dup(orig[u]);
288 LY_ARRAY_INCREMENT(dup);
289 }
290 return dup;
291}
292
Radek Krejcib56c7502019-02-13 14:19:54 +0100293/**
294 * @brief Duplicate compiled range structure.
295 *
296 * @param[in] ctx Libyang context for logging.
297 * @param[in] orig The range structure to be duplicated.
298 * @return New compiled range structure as a copy of @p orig.
299 * @return NULL in case of memory allocation error.
300 */
Radek Krejci19a96102018-11-15 13:38:09 +0100301struct lysc_range*
302lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
303{
304 struct lysc_range *dup;
305 LY_ERR ret;
306
Radek Krejcib56c7502019-02-13 14:19:54 +0100307 assert(orig);
308
Radek Krejci19a96102018-11-15 13:38:09 +0100309 dup = calloc(1, sizeof *dup);
310 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
311 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200312 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
313 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
314 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100315 }
316 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
317 DUP_STRING(ctx, orig->emsg, dup->emsg);
318 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
319
320 return dup;
321cleanup:
322 free(dup);
323 (void) ret; /* set but not used due to the return type */
324 return NULL;
325}
326
Radek Krejcib56c7502019-02-13 14:19:54 +0100327/**
328 * @brief Stack for processing if-feature expressions.
329 */
Radek Krejci19a96102018-11-15 13:38:09 +0100330struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100331 int size; /**< number of items in the stack */
332 int index; /**< first empty item */
333 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100334};
335
Radek Krejcib56c7502019-02-13 14:19:54 +0100336/**
337 * @brief Add @ref ifftokens into the stack.
338 * @param[in] stack The if-feature stack to use.
339 * @param[in] value One of the @ref ifftokens to store in the stack.
340 * @return LY_EMEM in case of memory allocation error
341 * @return LY_ESUCCESS if the value successfully stored.
342 */
Radek Krejci19a96102018-11-15 13:38:09 +0100343static LY_ERR
344iff_stack_push(struct iff_stack *stack, uint8_t value)
345{
346 if (stack->index == stack->size) {
347 stack->size += 4;
348 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
349 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
350 }
351 stack->stack[stack->index++] = value;
352 return LY_SUCCESS;
353}
354
Radek Krejcib56c7502019-02-13 14:19:54 +0100355/**
356 * @brief Get (and remove) the last item form the stack.
357 * @param[in] stack The if-feature stack to use.
358 * @return The value from the top of the stack.
359 */
Radek Krejci19a96102018-11-15 13:38:09 +0100360static uint8_t
361iff_stack_pop(struct iff_stack *stack)
362{
Radek Krejcib56c7502019-02-13 14:19:54 +0100363 assert(stack && stack->index);
364
Radek Krejci19a96102018-11-15 13:38:09 +0100365 stack->index--;
366 return stack->stack[stack->index];
367}
368
Radek Krejcib56c7502019-02-13 14:19:54 +0100369/**
370 * @brief Clean up the stack.
371 * @param[in] stack The if-feature stack to use.
372 */
Radek Krejci19a96102018-11-15 13:38:09 +0100373static void
374iff_stack_clean(struct iff_stack *stack)
375{
376 stack->size = 0;
377 free(stack->stack);
378}
379
Radek Krejcib56c7502019-02-13 14:19:54 +0100380/**
381 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
382 * (libyang format of the if-feature expression).
383 * @param[in,out] list The 2bits array to modify.
384 * @param[in] op The operand (@ref ifftokens) to store.
385 * @param[in] pos Position (0-based) where to store the given @p op.
386 */
Radek Krejci19a96102018-11-15 13:38:09 +0100387static void
388iff_setop(uint8_t *list, uint8_t op, int pos)
389{
390 uint8_t *item;
391 uint8_t mask = 3;
392
393 assert(pos >= 0);
394 assert(op <= 3); /* max 2 bits */
395
396 item = &list[pos / 4];
397 mask = mask << 2 * (pos % 4);
398 *item = (*item) & ~mask;
399 *item = (*item) | (op << 2 * (pos % 4));
400}
401
Radek Krejcib56c7502019-02-13 14:19:54 +0100402#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
403#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100404
Radek Krejci0af46292019-01-11 16:02:31 +0100405/**
406 * @brief Find a feature of the given name and referenced in the given module.
407 *
408 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
409 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
410 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
411 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
412 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
413 * assigned till that time will be still valid.
414 *
415 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
416 * @param[in] name Name of the feature including possible prefix.
417 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
418 * @return Pointer to the feature structure if found, NULL otherwise.
419 */
Radek Krejci19a96102018-11-15 13:38:09 +0100420static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100421lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100422{
423 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200424 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0af46292019-01-11 16:02:31 +0100425 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100426
427 for (i = 0; i < len; ++i) {
428 if (name[i] == ':') {
429 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100430 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100431 LY_CHECK_RET(!mod, NULL);
432
433 name = &name[i + 1];
434 len = len - i - 1;
435 }
436 }
437
438 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100439 if (mod->implemented) {
440 /* module is implemented so there is already the compiled schema */
441 flist = mod->compiled->features;
442 } else {
Michal Vasko33ff9422020-07-03 09:50:39 +0200443 flist = mod->dis_features;
Radek Krejci0af46292019-01-11 16:02:31 +0100444 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200445 LY_ARRAY_FOR(flist, u) {
446 f = &flist[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200447 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100448 return f;
449 }
450 }
451
452 return NULL;
453}
454
Michal Vasko8d544252020-03-02 10:19:52 +0100455/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100456 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
457 */
458static LY_ERR
459lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
460{
461 LY_ERR ret = LY_SUCCESS;
462
463 if (!ext_p->compiled) {
464 lysc_update_path(ctx, NULL, "{extension}");
465 lysc_update_path(ctx, NULL, ext_p->name);
466
467 /* compile the extension definition */
468 ext_p->compiled = calloc(1, sizeof **ext);
469 ext_p->compiled->refcount = 1;
470 DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name);
471 DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument);
472 ext_p->compiled->module = (struct lys_module *)ext_mod;
473 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
474
475 lysc_update_path(ctx, NULL, NULL);
476 lysc_update_path(ctx, NULL, NULL);
477
478 /* find extension definition plugin */
479 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
480 }
481
482 *ext = lysc_ext_dup(ext_p->compiled);
483
484done:
485 return ret;
486}
487
488/**
Michal Vasko8d544252020-03-02 10:19:52 +0100489 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
490 *
491 * @param[in] ctx Compilation context.
492 * @param[in] ext_p Parsed extension instance.
493 * @param[in,out] ext Prepared compiled extension instance.
494 * @param[in] parent Extension instance parent.
495 * @param[in] parent_type Extension instance parent type.
496 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
497 */
Radek Krejci19a96102018-11-15 13:38:09 +0100498static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100499lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
500 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100501{
Radek Krejci7c960162019-09-18 14:16:12 +0200502 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100503 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200504 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200505 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200506 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100507
508 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
509 ext->insubstmt = ext_p->insubstmt;
510 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200511 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200512 ext->parent = parent;
513 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100514
Radek Krejcif56e2a42019-09-09 14:15:25 +0200515 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200516
Radek Krejci19a96102018-11-15 13:38:09 +0100517 /* get module where the extension definition should be placed */
Radek Krejci7c960162019-09-18 14:16:12 +0200518 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u);
519 if (ext_p->yin) {
520 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
521 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
522 * YANG (import) prefix must be done here. */
523 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
524 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
525 u = 0;
526 } else if (ctx->mod_def->compiled) {
Radek Krejci7c960162019-09-18 14:16:12 +0200527 LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) {
528 if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) {
529 char *s;
Radek Krejci200f1062020-07-11 22:51:03 +0200530 LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]) == -1,
531 ret = LY_EMEM, cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200532 prefixed_name = lydict_insert_zc(ctx->ctx, s);
533 u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */
534 break;
535 }
536 }
537 }
538 if (!prefixed_name) {
539 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
540 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
541 goto cleanup;
542 }
543 } else {
544 prefixed_name = ext_p->name;
545 }
546 lysc_update_path(ctx, NULL, prefixed_name);
547
Michal Vasko8d544252020-03-02 10:19:52 +0100548 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200549 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100550 if (!ext_mod) {
551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
552 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
553 goto cleanup;
554 } else if (!ext_mod->parsed->extensions) {
555 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
556 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
557 prefixed_name, ext_mod->name);
558 goto cleanup;
559 }
Radek Krejci7c960162019-09-18 14:16:12 +0200560 }
561 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200562
Michal Vasko5fe75f12020-03-02 13:52:37 +0100563 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200564 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
565 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100566 /* compile extension definition and assign it */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200567 LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def));
Radek Krejci19a96102018-11-15 13:38:09 +0100568 break;
569 }
570 }
Radek Krejci7c960162019-09-18 14:16:12 +0200571 if (!ext->def) {
572 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
573 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
574 goto cleanup;
575 }
Radek Krejci0935f412019-08-20 16:15:18 +0200576
Radek Krejcif56e2a42019-09-09 14:15:25 +0200577 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
578 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
579 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200580 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200581 /* Schema was parsed from YIN and an argument is expected, ... */
582 struct lysp_stmt *stmt = NULL;
583
584 if (ext->def->flags & LYS_YINELEM_TRUE) {
585 /* ... argument was the first XML child element */
586 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
587 /* TODO check namespace of the statement */
588 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
589 stmt = ext_p->child;
590 }
591 }
592 } else {
593 /* ... argument was one of the XML attributes which are represented as child stmt
594 * with LYS_YIN_ATTR flag */
595 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
596 if (!strcmp(stmt->stmt, ext->def->argument)) {
597 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200598 break;
599 }
600 }
601 }
602 if (!stmt) {
603 /* missing extension's argument */
604 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200605 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
606 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200607
608 }
609 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
610 stmt->flags |= LYS_YIN_ARGUMENT;
611 }
Radek Krejci7c960162019-09-18 14:16:12 +0200612 if (prefixed_name != ext_p->name) {
613 lydict_remove(ctx->ctx, ext_p->name);
614 ext_p->name = prefixed_name;
615 if (!ext_p->argument && ext->argument) {
616 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
617 }
618 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200619
Radek Krejci0935f412019-08-20 16:15:18 +0200620 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200621 if (ext->argument) {
622 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
623 }
Radek Krejci7c960162019-09-18 14:16:12 +0200624 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200625 if (ext->argument) {
626 lysc_update_path(ctx, NULL, NULL);
627 }
Radek Krejci0935f412019-08-20 16:15:18 +0200628 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200629 ext_p->compiled = ext;
630
Radek Krejci7c960162019-09-18 14:16:12 +0200631 ret = LY_SUCCESS;
632cleanup:
633 if (prefixed_name && prefixed_name != ext_p->name) {
634 lydict_remove(ctx->ctx, prefixed_name);
635 }
636
Radek Krejcif56e2a42019-09-09 14:15:25 +0200637 lysc_update_path(ctx, NULL, NULL);
638 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200639
Radek Krejci7c960162019-09-18 14:16:12 +0200640 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200641}
642
643/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100644 * @brief Compile information from the if-feature statement
645 * @param[in] ctx Compile context.
646 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
Radek Krejcib56c7502019-02-13 14:19:54 +0100647 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
648 * @return LY_ERR value.
649 */
Radek Krejci19a96102018-11-15 13:38:09 +0100650static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200651lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100652{
653 const char *c = *value;
654 int r, rc = EXIT_FAILURE;
655 int i, j, last_not, checkversion = 0;
656 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
657 uint8_t op;
658 struct iff_stack stack = {0, 0, NULL};
659 struct lysc_feature *f;
660
661 assert(c);
662
663 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
664 for (i = j = last_not = 0; c[i]; i++) {
665 if (c[i] == '(') {
666 j++;
667 checkversion = 1;
668 continue;
669 } else if (c[i] == ')') {
670 j--;
671 continue;
672 } else if (isspace(c[i])) {
673 checkversion = 1;
674 continue;
675 }
676
677 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200678 int sp;
679 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
680 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
682 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
683 return LY_EVALID;
684 } else if (!isspace(c[i + r])) {
685 /* feature name starting with the not/and/or */
686 last_not = 0;
687 f_size++;
688 } else if (c[i] == 'n') { /* not operation */
689 if (last_not) {
690 /* double not */
691 expr_size = expr_size - 2;
692 last_not = 0;
693 } else {
694 last_not = 1;
695 }
696 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200697 if (f_exp != f_size) {
698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
699 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
700 return LY_EVALID;
701 }
Radek Krejci19a96102018-11-15 13:38:09 +0100702 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200703
Radek Krejci19a96102018-11-15 13:38:09 +0100704 /* not a not operation */
705 last_not = 0;
706 }
707 i += r;
708 } else {
709 f_size++;
710 last_not = 0;
711 }
712 expr_size++;
713
714 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200715 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100716 i--;
717 break;
718 }
719 i++;
720 }
721 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200722 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100723 /* not matching count of ( and ) */
724 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
725 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
726 return LY_EVALID;
727 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200728 if (f_exp != f_size) {
729 /* features do not match the needed arguments for the logical operations */
730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
731 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
732 "the required number of operands for the operations.", *value);
733 return LY_EVALID;
734 }
Radek Krejci19a96102018-11-15 13:38:09 +0100735
736 if (checkversion || expr_size > 1) {
737 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100738 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100739 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
740 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
741 return LY_EVALID;
742 }
743 }
744
745 /* allocate the memory */
746 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
747 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
748 stack.stack = malloc(expr_size * sizeof *stack.stack);
749 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
750
751 stack.size = expr_size;
752 f_size--; expr_size--; /* used as indexes from now */
753
754 for (i--; i >= 0; i--) {
755 if (c[i] == ')') {
756 /* push it on stack */
757 iff_stack_push(&stack, LYS_IFF_RP);
758 continue;
759 } else if (c[i] == '(') {
760 /* pop from the stack into result all operators until ) */
761 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
762 iff_setop(iff->expr, op, expr_size--);
763 }
764 continue;
765 } else if (isspace(c[i])) {
766 continue;
767 }
768
769 /* end of operator or operand -> find beginning and get what is it */
770 j = i + 1;
771 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
772 i--;
773 }
774 i++; /* go back by one step */
775
776 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
777 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
778 /* double not */
779 iff_stack_pop(&stack);
780 } else {
781 /* not has the highest priority, so do not pop from the stack
782 * as in case of AND and OR */
783 iff_stack_push(&stack, LYS_IFF_NOT);
784 }
785 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
786 /* as for OR - pop from the stack all operators with the same or higher
787 * priority and store them to the result, then push the AND to the stack */
788 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
789 op = iff_stack_pop(&stack);
790 iff_setop(iff->expr, op, expr_size--);
791 }
792 iff_stack_push(&stack, LYS_IFF_AND);
793 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
794 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
795 op = iff_stack_pop(&stack);
796 iff_setop(iff->expr, op, expr_size--);
797 }
798 iff_stack_push(&stack, LYS_IFF_OR);
799 } else {
800 /* feature name, length is j - i */
801
802 /* add it to the expression */
803 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
804
805 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100806 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
807 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
808 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
809 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100810 iff->features[f_size] = f;
811 LY_ARRAY_INCREMENT(iff->features);
812 f_size--;
813 }
814 }
815 while (stack.index) {
816 op = iff_stack_pop(&stack);
817 iff_setop(iff->expr, op, expr_size--);
818 }
819
820 if (++expr_size || ++f_size) {
821 /* not all expected operators and operands found */
822 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
823 "Invalid value \"%s\" of if-feature - processing error.", *value);
824 rc = LY_EINT;
825 } else {
826 rc = LY_SUCCESS;
827 }
828
829error:
830 /* cleanup */
831 iff_stack_clean(&stack);
832
833 return rc;
834}
835
Radek Krejcib56c7502019-02-13 14:19:54 +0100836/**
Michal Vasko175012e2019-11-06 15:49:14 +0100837 * @brief Get the XPath context node for the given schema node.
838 * @param[in] start The schema node where the XPath expression appears.
839 * @return The context node to evaluate XPath expression in given schema node.
840 * @return NULL in case the context node is the root node.
841 */
842static struct lysc_node *
843lysc_xpath_context(struct lysc_node *start)
844{
Michal Vasko1bf09392020-03-27 12:38:10 +0100845 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF));
Michal Vasko175012e2019-11-06 15:49:14 +0100846 start = start->parent);
847 return start;
848}
849
850/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100851 * @brief Compile information from the when statement
852 * @param[in] ctx Compile context.
853 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100854 * @param[in] flags Flags of the node with the "when" defiition.
855 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100856 * @param[out] when Pointer where to store pointer to the created compiled when structure.
857 * @return LY_ERR value.
858 */
Radek Krejci19a96102018-11-15 13:38:09 +0100859static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100860lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100861{
Radek Krejci19a96102018-11-15 13:38:09 +0100862 LY_ERR ret = LY_SUCCESS;
863
Radek Krejci00b874b2019-02-12 10:54:50 +0100864 *when = calloc(1, sizeof **when);
865 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200866 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200867 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100868 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100869 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
870 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
871 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200872 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100873 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100874
875done:
876 return ret;
877}
878
Radek Krejcib56c7502019-02-13 14:19:54 +0100879/**
880 * @brief Compile information from the must statement
881 * @param[in] ctx Compile context.
882 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100883 * @param[in,out] must Prepared (empty) compiled must structure to fill.
884 * @return LY_ERR value.
885 */
Radek Krejci19a96102018-11-15 13:38:09 +0100886static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200887lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100888{
Radek Krejci19a96102018-11-15 13:38:09 +0100889 LY_ERR ret = LY_SUCCESS;
890
Michal Vasko004d3152020-06-11 19:59:22 +0200891 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100892 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200893 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100894 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
895 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100896 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
897 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200898 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100899
900done:
901 return ret;
902}
903
Radek Krejcib56c7502019-02-13 14:19:54 +0100904/**
905 * @brief Compile information from the import statement
906 * @param[in] ctx Compile context.
907 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100908 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
909 * @return LY_ERR value.
910 */
Radek Krejci19a96102018-11-15 13:38:09 +0100911static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200912lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100913{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200914 const struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100915 LY_ERR ret = LY_SUCCESS;
916
917 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200918 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100919 imp->module = imp_p->module;
920
Radek Krejci7f2a5362018-11-28 13:05:37 +0100921 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
922 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100923 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100924 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100925 /* try to use filepath if present */
926 if (imp->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200927 struct ly_in *in;
928 if (ly_in_new_filepath(imp->module->filepath, 0, &in)) {
929 LOGINT(ctx->ctx);
930 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200931 LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)],
932 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200933 if (mod != imp->module) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200934 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
935 imp->module->filepath, imp->module->name);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200936 mod = NULL;
937 }
Radek Krejci19a96102018-11-15 13:38:09 +0100938 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200939 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100940 }
941 if (!mod) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200942 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, (struct lys_module **)&mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100943 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 +0100944 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100945 return LY_ENOTFOUND;
946 }
947 }
Radek Krejci19a96102018-11-15 13:38:09 +0100948 }
949
950done:
951 return ret;
952}
953
Michal Vasko33ff9422020-07-03 09:50:39 +0200954LY_ERR
955lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
956 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +0100957{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200958 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +0200959 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +0100960 LY_ERR ret = LY_SUCCESS;
961
Michal Vasko33ff9422020-07-03 09:50:39 +0200962 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +0200963
Michal Vasko33ff9422020-07-03 09:50:39 +0200964 if (!ctx_sc) {
965 context.ctx = ctx;
966 context.mod = module;
967 context.path_len = 1;
968 context.path[0] = '/';
969 ctx_sc = &context;
970 }
Radek Krejci19a96102018-11-15 13:38:09 +0100971
Michal Vasko33ff9422020-07-03 09:50:39 +0200972 if (!identities_p) {
973 return LY_SUCCESS;
974 }
975 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200976 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +0200977 }
978
979 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200980 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +0200981 LY_ARRAY_FOR(identities_p, u) {
982 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
983
984 LY_ARRAY_INCREMENT(*identities);
985 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
986 DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name);
987 DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc);
988 DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref);
989 (*identities)[offset + u].module = ctx_sc->mod;
990 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
991 lys_compile_iffeature, ret, done);
992 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
993 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
994 LYEXT_PAR_IDENT, ret, done);
995 (*identities)[offset + u].flags = identities_p[u].flags;
996
997 lysc_update_path(ctx_sc, NULL, NULL);
998 }
999 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001000done:
1001 return ret;
1002}
1003
Radek Krejcib56c7502019-02-13 14:19:54 +01001004/**
1005 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1006 *
1007 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1008 *
1009 * @param[in] ctx Compile context for logging.
1010 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1011 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1012 * @return LY_SUCCESS if everything is ok.
1013 * @return LY_EVALID if the identity is derived from itself.
1014 */
Radek Krejci38222632019-02-12 16:55:05 +01001015static LY_ERR
1016lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1017{
1018 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001019 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001020 struct ly_set recursion = {0};
1021 struct lysc_ident *drv;
1022
1023 if (!derived) {
1024 return LY_SUCCESS;
1025 }
1026
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001027 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001028 if (ident == derived[u]) {
1029 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1030 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1031 goto cleanup;
1032 }
1033 ly_set_add(&recursion, derived[u], 0);
1034 }
1035
1036 for (v = 0; v < recursion.count; ++v) {
1037 drv = recursion.objs[v];
1038 if (!drv->derived) {
1039 continue;
1040 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001041 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001042 if (ident == drv->derived[u]) {
1043 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1044 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1045 goto cleanup;
1046 }
1047 ly_set_add(&recursion, drv->derived[u], 0);
1048 }
1049 }
1050 ret = LY_SUCCESS;
1051
1052cleanup:
1053 ly_set_erase(&recursion, NULL);
1054 return ret;
1055}
1056
Radek Krejcia3045382018-11-22 14:30:31 +01001057/**
1058 * @brief Find and process the referenced base identities from another identity or identityref
1059 *
Radek Krejciaca74032019-06-04 08:53:06 +02001060 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001061 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1062 * to distinguish these two use cases.
1063 *
1064 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1065 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001066 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001067 * @param[in] bases Array of bases of identityref to fill in.
1068 * @return LY_ERR value.
1069 */
Radek Krejci19a96102018-11-15 13:38:09 +01001070static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001071lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
1072 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001073{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001074 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001075 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001076 struct lys_module *mod;
Michal Vasko33ff9422020-07-03 09:50:39 +02001077 struct lysc_ident **idref, *identities;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001078
1079 assert(ident || bases);
1080
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001081 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001082 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1083 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1084 return LY_EVALID;
1085 }
1086
Michal Vasko33ff9422020-07-03 09:50:39 +02001087 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001088 s = strchr(bases_p[u], ':');
1089 if (s) {
1090 /* prefixed identity */
1091 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001092 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001093 } else {
1094 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001095 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001096 }
1097 if (!mod) {
1098 if (ident) {
1099 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1100 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1101 } else {
1102 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1103 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1104 }
1105 return LY_EVALID;
1106 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001107
Radek Krejci555cb5b2018-11-16 14:54:33 +01001108 idref = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02001109 if (mod->compiled) {
1110 identities = mod->compiled->identities;
1111 } else {
1112 identities = mod->dis_identities;
1113 }
1114 LY_ARRAY_FOR(identities, v) {
1115 if (!strcmp(name, identities[v].name)) {
1116 if (ident) {
1117 if (ident == &identities[v]) {
1118 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1119 "Identity \"%s\" is derived from itself.", ident->name);
1120 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001121 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001122 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived));
1123 /* we have match! store the backlink */
1124 LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM);
1125 *idref = ident;
1126 } else {
1127 /* we have match! store the found identity */
1128 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
1129 *idref = &identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001130 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001131 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001132 }
1133 }
1134 if (!idref || !(*idref)) {
1135 if (ident) {
1136 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1137 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1138 } else {
1139 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1140 "Unable to find base (%s) of identityref.", bases_p[u]);
1141 }
1142 return LY_EVALID;
1143 }
1144 }
1145 return LY_SUCCESS;
1146}
1147
Radek Krejcia3045382018-11-22 14:30:31 +01001148/**
1149 * @brief For the given array of identities, set the backlinks from all their base identities.
1150 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1151 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1152 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1153 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1154 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001155static LY_ERR
1156lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1157{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001158 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001159
Michal Vasko33ff9422020-07-03 09:50:39 +02001160 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001161 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001162 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001163 continue;
1164 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001165 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001166 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 +02001167 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001168 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001169 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001170 return LY_SUCCESS;
1171}
1172
Radek Krejci0af46292019-01-11 16:02:31 +01001173LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001174lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
1175 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001176{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001177 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001178 struct lysc_ctx context = {0};
1179
Radek Krejci327de162019-06-14 12:52:07 +02001180 assert(ctx_sc || ctx);
1181
1182 if (!ctx_sc) {
1183 context.ctx = ctx;
1184 context.mod = module;
1185 context.path_len = 1;
1186 context.path[0] = '/';
1187 ctx_sc = &context;
1188 }
Radek Krejci0af46292019-01-11 16:02:31 +01001189
1190 if (!features_p) {
1191 return LY_SUCCESS;
1192 }
1193 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001194 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001195 }
1196
Radek Krejci327de162019-06-14 12:52:07 +02001197 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001198 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001199 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001200 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1201
Radek Krejci0af46292019-01-11 16:02:31 +01001202 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001203 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci327de162019-06-14 12:52:07 +02001204 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1205 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1206 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001207 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001208 (*features)[offset + u].module = ctx_sc->mod;
1209
1210 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001211 }
Radek Krejci327de162019-06-14 12:52:07 +02001212 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001213
1214 return LY_SUCCESS;
1215}
1216
Radek Krejcia3045382018-11-22 14:30:31 +01001217/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001218 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001219 *
1220 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1221 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001222 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001223 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1224 * being currently processed).
1225 * @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 +01001226 * @return LY_SUCCESS if everything is ok.
1227 * @return LY_EVALID if the feature references indirectly itself.
1228 */
1229static LY_ERR
1230lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1231{
1232 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001233 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001234 struct ly_set recursion = {0};
1235 struct lysc_feature *drv;
1236
1237 if (!depfeatures) {
1238 return LY_SUCCESS;
1239 }
1240
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001241 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001242 if (feature == depfeatures[u]) {
1243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1244 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1245 goto cleanup;
1246 }
1247 ly_set_add(&recursion, depfeatures[u], 0);
1248 }
1249
1250 for (v = 0; v < recursion.count; ++v) {
1251 drv = recursion.objs[v];
1252 if (!drv->depfeatures) {
1253 continue;
1254 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001255 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001256 if (feature == drv->depfeatures[u]) {
1257 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1258 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1259 goto cleanup;
1260 }
1261 ly_set_add(&recursion, drv->depfeatures[u], 0);
1262 }
1263 }
1264 ret = LY_SUCCESS;
1265
1266cleanup:
1267 ly_set_erase(&recursion, NULL);
1268 return ret;
1269}
1270
1271/**
Radek Krejci0af46292019-01-11 16:02:31 +01001272 * @brief Create pre-compiled features array.
1273 *
1274 * See lys_feature_precompile() for more details.
1275 *
Radek Krejcia3045382018-11-22 14:30:31 +01001276 * @param[in] ctx Compile context.
1277 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001278 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001279 * @return LY_ERR value.
1280 */
Radek Krejci19a96102018-11-15 13:38:09 +01001281static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001282lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001283{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001284 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001285 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001286 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001287
Radek Krejci327de162019-06-14 12:52:07 +02001288
Radek Krejci0af46292019-01-11 16:02:31 +01001289 /* find the preprecompiled feature */
1290 LY_ARRAY_FOR(features, x) {
1291 if (strcmp(features[x].name, feature_p->name)) {
1292 continue;
1293 }
1294 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001295 lysc_update_path(ctx, NULL, "{feature}");
1296 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001297
Radek Krejci0af46292019-01-11 16:02:31 +01001298 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001299 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001300 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001301 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001302 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001303 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001304 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001305 /* check for circular dependency - direct reference first,... */
1306 if (feature == feature->iffeatures[u].features[v]) {
1307 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1308 "Feature \"%s\" is referenced from itself.", feature->name);
1309 return LY_EVALID;
1310 }
1311 /* ... and indirect circular reference */
1312 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1313
Radek Krejci0af46292019-01-11 16:02:31 +01001314 /* add itself into the dependants list */
1315 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1316 *df = feature;
1317 }
Radek Krejci19a96102018-11-15 13:38:09 +01001318 }
Radek Krejci19a96102018-11-15 13:38:09 +01001319 }
1320 }
Radek Krejci327de162019-06-14 12:52:07 +02001321 lysc_update_path(ctx, NULL, NULL);
1322 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001323 done:
1324 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001325 }
Radek Krejci0af46292019-01-11 16:02:31 +01001326
1327 LOGINT(ctx->ctx);
1328 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001329}
1330
Radek Krejcib56c7502019-02-13 14:19:54 +01001331/**
1332 * @brief Revert compiled list of features back to the precompiled state.
1333 *
1334 * 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 +02001335 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001336 *
1337 * @param[in] ctx Compilation context.
1338 * @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 +02001339 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001340 */
Radek Krejci95710c92019-02-11 15:49:55 +01001341static void
1342lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1343{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001344 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001345
Michal Vasko33ff9422020-07-03 09:50:39 +02001346 /* keep the dis_features list until the complete lys_module is freed */
1347 mod->dis_features = mod->compiled->features;
Radek Krejci95710c92019-02-11 15:49:55 +01001348 mod->compiled->features = NULL;
1349
Michal Vasko33ff9422020-07-03 09:50:39 +02001350 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001351 * which may points into the data being freed here */
Michal Vasko33ff9422020-07-03 09:50:39 +02001352 LY_ARRAY_FOR(mod->dis_features, u) {
1353 LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) {
1354 lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001355 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001356 LY_ARRAY_FREE(mod->dis_features[u].iffeatures);
1357 mod->dis_features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001358
Michal Vasko33ff9422020-07-03 09:50:39 +02001359 LY_ARRAY_FOR(mod->dis_features[u].exts, v) {
1360 lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001361 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001362 LY_ARRAY_FREE(mod->dis_features[u].exts);
1363 mod->dis_features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001364 }
1365}
1366
Radek Krejcia3045382018-11-22 14:30:31 +01001367/**
1368 * @brief Validate and normalize numeric value from a range definition.
1369 * @param[in] ctx Compile context.
1370 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1371 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1372 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1373 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1374 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1375 * @param[in] value String value of the range boundary.
1376 * @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.
1377 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1378 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1379 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001380LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001381range_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 +01001382{
Radek Krejci6cba4292018-11-15 17:33:29 +01001383 size_t fraction = 0, size;
1384
Radek Krejci19a96102018-11-15 13:38:09 +01001385 *len = 0;
1386
1387 assert(value);
1388 /* parse value */
1389 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1390 return LY_EVALID;
1391 }
1392
1393 if ((value[*len] == '-') || (value[*len] == '+')) {
1394 ++(*len);
1395 }
1396
1397 while (isdigit(value[*len])) {
1398 ++(*len);
1399 }
1400
1401 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001402 if (basetype == LY_TYPE_DEC64) {
1403 goto decimal;
1404 } else {
1405 *valcopy = strndup(value, *len);
1406 return LY_SUCCESS;
1407 }
Radek Krejci19a96102018-11-15 13:38:09 +01001408 }
1409 fraction = *len;
1410
1411 ++(*len);
1412 while (isdigit(value[*len])) {
1413 ++(*len);
1414 }
1415
Radek Krejci6cba4292018-11-15 17:33:29 +01001416 if (basetype == LY_TYPE_DEC64) {
1417decimal:
1418 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001419 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001420 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1421 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1422 *len, value, frdigits);
1423 return LY_EINVAL;
1424 }
1425 if (fraction) {
1426 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1427 } else {
1428 size = (*len) + frdigits + 1;
1429 }
1430 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001431 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1432
Radek Krejci6cba4292018-11-15 17:33:29 +01001433 (*valcopy)[size - 1] = '\0';
1434 if (fraction) {
1435 memcpy(&(*valcopy)[0], &value[0], fraction);
1436 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1437 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1438 } else {
1439 memcpy(&(*valcopy)[0], &value[0], *len);
1440 memset(&(*valcopy)[*len], '0', frdigits);
1441 }
Radek Krejci19a96102018-11-15 13:38:09 +01001442 }
1443 return LY_SUCCESS;
1444}
1445
Radek Krejcia3045382018-11-22 14:30:31 +01001446/**
1447 * @brief Check that values in range are in ascendant order.
1448 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001449 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1450 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001451 * @param[in] value Current value to check.
1452 * @param[in] prev_value The last seen value.
1453 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1454 */
Radek Krejci19a96102018-11-15 13:38:09 +01001455static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001456range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001457{
1458 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001459 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001460 return LY_EEXIST;
1461 }
1462 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001463 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001464 return LY_EEXIST;
1465 }
1466 }
1467 return LY_SUCCESS;
1468}
1469
Radek Krejcia3045382018-11-22 14:30:31 +01001470/**
1471 * @brief Set min/max value of the range part.
1472 * @param[in] ctx Compile context.
1473 * @param[in] part Range part structure to fill.
1474 * @param[in] max Flag to distinguish if storing min or max value.
1475 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1476 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1477 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1478 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1479 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001480 * @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 +01001481 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1482 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1483 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1484 * frdigits value), LY_EMEM.
1485 */
Radek Krejci19a96102018-11-15 13:38:09 +01001486static LY_ERR
1487range_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 +01001488 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001489{
1490 LY_ERR ret = LY_SUCCESS;
1491 char *valcopy = NULL;
1492 size_t len;
1493
1494 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001495 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001496 LY_CHECK_GOTO(ret, finalize);
1497 }
1498 if (!valcopy && base_range) {
1499 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001500 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001501 } else {
1502 part->min_64 = base_range->parts[0].min_64;
1503 }
1504 if (!first) {
1505 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1506 }
1507 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001508 }
1509
1510 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001511 case LY_TYPE_INT8: /* range */
1512 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001513 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 +01001514 } else if (max) {
1515 part->max_64 = INT64_C(127);
1516 } else {
1517 part->min_64 = INT64_C(-128);
1518 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001519 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001520 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001521 }
1522 break;
1523 case LY_TYPE_INT16: /* range */
1524 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001525 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 +01001526 } else if (max) {
1527 part->max_64 = INT64_C(32767);
1528 } else {
1529 part->min_64 = INT64_C(-32768);
1530 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001531 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001532 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001533 }
1534 break;
1535 case LY_TYPE_INT32: /* range */
1536 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001537 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 +01001538 } else if (max) {
1539 part->max_64 = INT64_C(2147483647);
1540 } else {
1541 part->min_64 = INT64_C(-2147483648);
1542 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001543 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001544 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001545 }
1546 break;
1547 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001548 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001549 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001550 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001551 max ? &part->max_64 : &part->min_64);
1552 } else if (max) {
1553 part->max_64 = INT64_C(9223372036854775807);
1554 } else {
1555 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1556 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001557 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001558 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001559 }
1560 break;
1561 case LY_TYPE_UINT8: /* range */
1562 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001563 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 +01001564 } else if (max) {
1565 part->max_u64 = UINT64_C(255);
1566 } else {
1567 part->min_u64 = UINT64_C(0);
1568 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001569 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001570 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001571 }
1572 break;
1573 case LY_TYPE_UINT16: /* range */
1574 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001575 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 +01001576 } else if (max) {
1577 part->max_u64 = UINT64_C(65535);
1578 } else {
1579 part->min_u64 = UINT64_C(0);
1580 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001581 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001582 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001583 }
1584 break;
1585 case LY_TYPE_UINT32: /* range */
1586 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001587 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 +01001588 } else if (max) {
1589 part->max_u64 = UINT64_C(4294967295);
1590 } else {
1591 part->min_u64 = UINT64_C(0);
1592 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001593 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001594 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001595 }
1596 break;
1597 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001598 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001599 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001600 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001601 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 +01001602 } else if (max) {
1603 part->max_u64 = UINT64_C(18446744073709551615);
1604 } else {
1605 part->min_u64 = UINT64_C(0);
1606 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001607 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001608 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001609 }
1610 break;
1611 default:
1612 LOGINT(ctx->ctx);
1613 ret = LY_EINT;
1614 }
1615
Radek Krejci5969f272018-11-23 10:03:58 +01001616finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001617 if (ret == LY_EDENIED) {
1618 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1619 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1620 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1621 } else if (ret == LY_EVALID) {
1622 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1623 "Invalid %s restriction - invalid value \"%s\".",
1624 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1625 } else if (ret == LY_EEXIST) {
1626 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1627 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001628 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001629 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001630 } else if (!ret && value) {
1631 *value = *value + len;
1632 }
1633 free(valcopy);
1634 return ret;
1635}
1636
Radek Krejcia3045382018-11-22 14:30:31 +01001637/**
1638 * @brief Compile the parsed range restriction.
1639 * @param[in] ctx Compile context.
1640 * @param[in] range_p Parsed range structure to compile.
1641 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1642 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1643 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1644 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1645 * range restriction must be more restrictive than the base_range.
1646 * @param[in,out] range Pointer to the created current range structure.
1647 * @return LY_ERR value.
1648 */
Radek Krejci19a96102018-11-15 13:38:09 +01001649static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001650lys_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 +01001651 struct lysc_range *base_range, struct lysc_range **range)
1652{
1653 LY_ERR ret = LY_EVALID;
1654 const char *expr;
1655 struct lysc_range_part *parts = NULL, *part;
1656 int range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001657 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001658
1659 assert(range);
1660 assert(range_p);
1661
1662 expr = range_p->arg;
1663 while(1) {
1664 if (isspace(*expr)) {
1665 ++expr;
1666 } else if (*expr == '\0') {
1667 if (range_expected) {
1668 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1669 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1670 length_restr ? "length" : "range", range_p->arg);
1671 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001672 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1674 "Invalid %s restriction - unexpected end of the expression (%s).",
1675 length_restr ? "length" : "range", range_p->arg);
1676 goto cleanup;
1677 }
1678 parts_done++;
1679 break;
1680 } else if (!strncmp(expr, "min", 3)) {
1681 if (parts) {
1682 /* min cannot be used elsewhere than in the first part */
1683 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1684 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1685 expr - range_p->arg, range_p->arg);
1686 goto cleanup;
1687 }
1688 expr += 3;
1689
1690 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001691 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 +01001692 part->max_64 = part->min_64;
1693 } else if (*expr == '|') {
1694 if (!parts || range_expected) {
1695 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1696 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1697 goto cleanup;
1698 }
1699 expr++;
1700 parts_done++;
1701 /* process next part of the expression */
1702 } else if (!strncmp(expr, "..", 2)) {
1703 expr += 2;
1704 while (isspace(*expr)) {
1705 expr++;
1706 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001707 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001708 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1709 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1710 goto cleanup;
1711 }
1712 /* continue expecting the upper boundary */
1713 range_expected = 1;
1714 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1715 /* number */
1716 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001717 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001718 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 +01001719 range_expected = 0;
1720 } else {
1721 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001722 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 +01001723 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001724 part->max_64 = part->min_64;
1725 }
1726
1727 /* continue with possible another expression part */
1728 } else if (!strncmp(expr, "max", 3)) {
1729 expr += 3;
1730 while (isspace(*expr)) {
1731 expr++;
1732 }
1733 if (*expr != '\0') {
1734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1735 length_restr ? "length" : "range", expr);
1736 goto cleanup;
1737 }
1738 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001739 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001740 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 +01001741 range_expected = 0;
1742 } else {
1743 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001744 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 +01001745 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001746 part->min_64 = part->max_64;
1747 }
1748 } else {
1749 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1750 length_restr ? "length" : "range", expr);
1751 goto cleanup;
1752 }
1753 }
1754
1755 /* check with the previous range/length restriction */
1756 if (base_range) {
1757 switch (basetype) {
1758 case LY_TYPE_BINARY:
1759 case LY_TYPE_UINT8:
1760 case LY_TYPE_UINT16:
1761 case LY_TYPE_UINT32:
1762 case LY_TYPE_UINT64:
1763 case LY_TYPE_STRING:
1764 uns = 1;
1765 break;
1766 case LY_TYPE_DEC64:
1767 case LY_TYPE_INT8:
1768 case LY_TYPE_INT16:
1769 case LY_TYPE_INT32:
1770 case LY_TYPE_INT64:
1771 uns = 0;
1772 break;
1773 default:
1774 LOGINT(ctx->ctx);
1775 ret = LY_EINT;
1776 goto cleanup;
1777 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001778 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001779 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1780 goto baseerror;
1781 }
1782 /* current lower bound is not lower than the base */
1783 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1784 /* base has single value */
1785 if (base_range->parts[v].min_64 == parts[u].min_64) {
1786 /* both lower bounds are the same */
1787 if (parts[u].min_64 != parts[u].max_64) {
1788 /* current continues with a range */
1789 goto baseerror;
1790 } else {
1791 /* equal single values, move both forward */
1792 ++v;
1793 continue;
1794 }
1795 } else {
1796 /* base is single value lower than current range, so the
1797 * value from base range is removed in the current,
1798 * move only base and repeat checking */
1799 ++v;
1800 --u;
1801 continue;
1802 }
1803 } else {
1804 /* base is the range */
1805 if (parts[u].min_64 == parts[u].max_64) {
1806 /* current is a single value */
1807 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1808 /* current is behind the base range, so base range is omitted,
1809 * move the base and keep the current for further check */
1810 ++v;
1811 --u;
1812 } /* else it is within the base range, so move the current, but keep the base */
1813 continue;
1814 } else {
1815 /* both are ranges - check the higher bound, the lower was already checked */
1816 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1817 /* higher bound is higher than the current higher bound */
1818 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1819 /* but the current lower bound is also higher, so the base range is omitted,
1820 * continue with the same current, but move the base */
1821 --u;
1822 ++v;
1823 continue;
1824 }
1825 /* current range starts within the base range but end behind it */
1826 goto baseerror;
1827 } else {
1828 /* current range is smaller than the base,
1829 * move current, but stay with the base */
1830 continue;
1831 }
1832 }
1833 }
1834 }
1835 if (u != parts_done) {
1836baseerror:
1837 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1838 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1839 length_restr ? "length" : "range", range_p->arg);
1840 goto cleanup;
1841 }
1842 }
1843
1844 if (!(*range)) {
1845 *range = calloc(1, sizeof **range);
1846 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1847 }
1848
Radek Krejcic8b31002019-01-08 10:24:45 +01001849 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001850 if (range_p->eapptag) {
1851 lydict_remove(ctx->ctx, (*range)->eapptag);
1852 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1853 }
1854 if (range_p->emsg) {
1855 lydict_remove(ctx->ctx, (*range)->emsg);
1856 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1857 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001858 if (range_p->dsc) {
1859 lydict_remove(ctx->ctx, (*range)->dsc);
1860 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1861 }
1862 if (range_p->ref) {
1863 lydict_remove(ctx->ctx, (*range)->ref);
1864 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1865 }
Radek Krejci19a96102018-11-15 13:38:09 +01001866 /* extensions are taken only from the last range by the caller */
1867
1868 (*range)->parts = parts;
1869 parts = NULL;
1870 ret = LY_SUCCESS;
1871cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001872 LY_ARRAY_FREE(parts);
1873
1874 return ret;
1875}
1876
1877/**
1878 * @brief Checks pattern syntax.
1879 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001880 * @param[in] ctx Context.
1881 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001882 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001883 * @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 +01001884 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001885 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001886LY_ERR
1887lys_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 +01001888{
Michal Vasko40a00082020-05-27 15:20:01 +02001889 int idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001890 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001891 int err_code;
1892 const char *orig_ptr;
1893 PCRE2_SIZE err_offset;
1894 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001895#define URANGE_LEN 19
1896 char *ublock2urange[][2] = {
1897 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1898 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1899 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1900 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1901 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1902 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1903 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1904 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1905 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1906 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1907 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1908 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1909 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1910 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1911 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1912 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1913 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1914 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1915 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1916 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1917 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1918 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1919 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1920 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1921 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1922 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1923 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1924 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1925 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1926 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1927 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1928 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1929 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1930 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1931 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1932 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1933 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1934 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1935 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1936 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1937 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1938 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1939 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1940 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1941 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1942 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1943 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1944 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1945 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1946 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1947 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1948 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1949 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1950 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1951 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1952 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1953 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1954 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1955 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1956 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1957 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1958 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1959 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1960 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1961 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1962 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1963 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1964 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1965 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1966 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1967 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1968 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1969 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1970 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1971 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1972 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1973 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1974 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1975 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1976 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1977 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1978 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1979 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1980 {NULL, NULL}
1981 };
1982
1983 /* adjust the expression to a Perl equivalent
1984 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1985
Michal Vasko40a00082020-05-27 15:20:01 +02001986 /* allocate space for the transformed pattern */
1987 size = strlen(pattern) + 1;
1988 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001990 perl_regex[0] = '\0';
1991
Michal Vasko40a00082020-05-27 15:20:01 +02001992 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1993 brack = 0;
1994 idx = 0;
1995 orig_ptr = pattern;
1996 while (orig_ptr[0]) {
1997 switch (orig_ptr[0]) {
1998 case '$':
1999 case '^':
2000 if (!brack) {
2001 /* make space for the extra character */
2002 ++size;
2003 perl_regex = ly_realloc(perl_regex, size);
2004 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002005
Michal Vasko40a00082020-05-27 15:20:01 +02002006 /* print escape slash */
2007 perl_regex[idx] = '\\';
2008 ++idx;
2009 }
2010 break;
2011 case '[':
2012 /* must not be escaped */
2013 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2014 ++brack;
2015 }
2016 break;
2017 case ']':
2018 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2019 /* pattern was checked and compiled already */
2020 assert(brack);
2021 --brack;
2022 }
2023 break;
2024 default:
2025 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002026 }
Michal Vasko40a00082020-05-27 15:20:01 +02002027
2028 /* copy char */
2029 perl_regex[idx] = orig_ptr[0];
2030
2031 ++idx;
2032 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002033 }
Michal Vasko40a00082020-05-27 15:20:01 +02002034 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002035
2036 /* substitute Unicode Character Blocks with exact Character Ranges */
2037 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2038 start = ptr - perl_regex;
2039
2040 ptr = strchr(ptr, '}');
2041 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002042 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002043 pattern, perl_regex + start + 2, "unterminated character property");
2044 free(perl_regex);
2045 return LY_EVALID;
2046 }
2047 end = (ptr - perl_regex) + 1;
2048
2049 /* need more space */
2050 if (end - start < URANGE_LEN) {
2051 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002052 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002053 }
2054
2055 /* find our range */
2056 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2057 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2058 break;
2059 }
2060 }
2061 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002062 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002063 pattern, perl_regex + start + 5, "unknown block name");
2064 free(perl_regex);
2065 return LY_EVALID;
2066 }
2067
2068 /* 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 +02002069 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002070 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 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002074 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002075 }
2076 }
Michal Vasko40a00082020-05-27 15:20:01 +02002077 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002078 /* skip brackets */
2079 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2080 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2081 } else {
2082 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2083 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2084 }
2085 }
2086
2087 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002088 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2089 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002090 &err_code, &err_offset, NULL);
2091 if (!code_local) {
2092 PCRE2_UCHAR err_msg[256] = {0};
2093 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002095 free(perl_regex);
2096 return LY_EVALID;
2097 }
2098 free(perl_regex);
2099
Radek Krejci54579462019-04-30 12:47:06 +02002100 if (code) {
2101 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002102 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002103 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002104 }
2105
2106 return LY_SUCCESS;
2107
2108#undef URANGE_LEN
2109}
2110
Radek Krejcia3045382018-11-22 14:30:31 +01002111/**
2112 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2113 * @param[in] ctx Compile context.
2114 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002115 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2116 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2117 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2118 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2119 */
Radek Krejci19a96102018-11-15 13:38:09 +01002120static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002121lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002122 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2123{
2124 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002125 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002126 LY_ERR ret = LY_SUCCESS;
2127
2128 /* first, copy the patterns from the base type */
2129 if (base_patterns) {
2130 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2131 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2132 }
2133
2134 LY_ARRAY_FOR(patterns_p, u) {
2135 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2136 *pattern = calloc(1, sizeof **pattern);
2137 ++(*pattern)->refcount;
2138
Michal Vasko03ff5a72019-09-11 13:49:33 +02002139 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002140 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002141
2142 if (patterns_p[u].arg[0] == 0x15) {
2143 (*pattern)->inverted = 1;
2144 }
Radek Krejci54579462019-04-30 12:47:06 +02002145 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002146 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2147 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002148 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2149 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002150 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002151 }
2152done:
2153 return ret;
2154}
2155
Radek Krejcia3045382018-11-22 14:30:31 +01002156/**
2157 * @brief map of the possible restrictions combination for the specific built-in type.
2158 */
Radek Krejci19a96102018-11-15 13:38:09 +01002159static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2160 0 /* LY_TYPE_UNKNOWN */,
2161 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002162 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2163 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2164 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2165 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2166 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002167 LYS_SET_BIT /* LY_TYPE_BITS */,
2168 0 /* LY_TYPE_BOOL */,
2169 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2170 0 /* LY_TYPE_EMPTY */,
2171 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2172 LYS_SET_BASE /* LY_TYPE_IDENT */,
2173 LYS_SET_REQINST /* LY_TYPE_INST */,
2174 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002175 LYS_SET_TYPE /* LY_TYPE_UNION */,
2176 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002177 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002178 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002179 LYS_SET_RANGE /* LY_TYPE_INT64 */
2180};
2181
2182/**
2183 * @brief stringification of the YANG built-in data types
2184 */
2185const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2186 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2187 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002188};
2189
Radek Krejcia3045382018-11-22 14:30:31 +01002190/**
2191 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2192 * @param[in] ctx Compile context.
2193 * @param[in] enums_p Array of the parsed enum structures to compile.
2194 * @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 +01002195 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2196 * @param[out] enums Newly created array of the compiled enums information for the current type.
2197 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2198 */
Radek Krejci19a96102018-11-15 13:38:09 +01002199static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002200lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002201 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002202{
2203 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002204 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002205 int32_t value = 0;
2206 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002207 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002208
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002209 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002210 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2211 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2212 return LY_EVALID;
2213 }
2214
2215 LY_ARRAY_FOR(enums_p, u) {
2216 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2217 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002218 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2219 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002220 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002221 if (base_enums) {
2222 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2223 LY_ARRAY_FOR(base_enums, v) {
2224 if (!strcmp(e->name, base_enums[v].name)) {
2225 break;
2226 }
2227 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002228 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002229 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2230 "Invalid %s - derived type adds new item \"%s\".",
2231 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2232 return LY_EVALID;
2233 }
2234 match = v;
2235 }
2236
2237 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002238 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002239 if (enums_p[u].flags & LYS_SET_VALUE) {
2240 e->value = (int32_t)enums_p[u].value;
2241 if (!u || e->value >= value) {
2242 value = e->value + 1;
2243 }
2244 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002245 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002246 if (e->value == (*enums)[v].value) {
2247 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2248 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2249 e->value, e->name, (*enums)[v].name);
2250 return LY_EVALID;
2251 }
2252 }
2253 } else if (base_enums) {
2254 /* inherit the assigned value */
2255 e->value = base_enums[match].value;
2256 if (!u || e->value >= value) {
2257 value = e->value + 1;
2258 }
2259 } else {
2260 /* assign value automatically */
2261 if (u && value == INT32_MIN) {
2262 /* counter overflow */
2263 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2264 "Invalid enumeration - it is not possible to auto-assign enum value for "
2265 "\"%s\" since the highest value is already 2147483647.", e->name);
2266 return LY_EVALID;
2267 }
2268 e->value = value++;
2269 }
2270 } else { /* LY_TYPE_BITS */
2271 if (enums_p[u].flags & LYS_SET_VALUE) {
2272 e->value = (int32_t)enums_p[u].value;
2273 if (!u || (uint32_t)e->value >= position) {
2274 position = (uint32_t)e->value + 1;
2275 }
2276 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002277 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002278 if (e->value == (*enums)[v].value) {
2279 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2280 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2281 (uint32_t)e->value, e->name, (*enums)[v].name);
2282 return LY_EVALID;
2283 }
2284 }
2285 } else if (base_enums) {
2286 /* inherit the assigned value */
2287 e->value = base_enums[match].value;
2288 if (!u || (uint32_t)e->value >= position) {
2289 position = (uint32_t)e->value + 1;
2290 }
2291 } else {
2292 /* assign value automatically */
2293 if (u && position == 0) {
2294 /* counter overflow */
2295 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2296 "Invalid bits - it is not possible to auto-assign bit position for "
2297 "\"%s\" since the highest value is already 4294967295.", e->name);
2298 return LY_EVALID;
2299 }
2300 e->value = position++;
2301 }
2302 }
2303
2304 if (base_enums) {
2305 /* the assigned values must not change from the derived type */
2306 if (e->value != base_enums[match].value) {
2307 if (basetype == LY_TYPE_ENUM) {
2308 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2309 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2310 e->name, base_enums[match].value, e->value);
2311 } else {
2312 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2313 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2314 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2315 }
2316 return LY_EVALID;
2317 }
2318 }
2319
Radek Krejciec4da802019-05-02 13:02:41 +02002320 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002321 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 +01002322
2323 if (basetype == LY_TYPE_BITS) {
2324 /* keep bits ordered by position */
2325 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2326 if (v != u) {
2327 memcpy(&storage, e, sizeof *e);
2328 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2329 memcpy(&(*enums)[v], &storage, sizeof storage);
2330 }
2331 }
2332 }
2333
2334done:
2335 return ret;
2336}
2337
Radek Krejcia3045382018-11-22 14:30:31 +01002338/**
2339 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2340 *
2341 * path-arg = absolute-path / relative-path
2342 * absolute-path = 1*("/" (node-identifier *path-predicate))
2343 * relative-path = 1*(".." "/") descendant-path
2344 *
2345 * @param[in,out] path Path to parse.
2346 * @param[out] prefix Prefix of the token, NULL if there is not any.
2347 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2348 * @param[out] name Name of the token.
2349 * @param[out] nam_len Length of the name.
2350 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2351 * must not be changed between consecutive calls. -1 if the
2352 * path is absolute.
2353 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2354 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2355 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002356LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002357lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2358 int *parent_times, int *has_predicate)
2359{
2360 int par_times = 0;
2361
2362 assert(path && *path);
2363 assert(parent_times);
2364 assert(prefix);
2365 assert(prefix_len);
2366 assert(name);
2367 assert(name_len);
2368 assert(has_predicate);
2369
2370 *prefix = NULL;
2371 *prefix_len = 0;
2372 *name = NULL;
2373 *name_len = 0;
2374 *has_predicate = 0;
2375
2376 if (!*parent_times) {
2377 if (!strncmp(*path, "..", 2)) {
2378 *path += 2;
2379 ++par_times;
2380 while (!strncmp(*path, "/..", 3)) {
2381 *path += 3;
2382 ++par_times;
2383 }
2384 }
2385 if (par_times) {
2386 *parent_times = par_times;
2387 } else {
2388 *parent_times = -1;
2389 }
2390 }
2391
2392 if (**path != '/') {
2393 return LY_EINVAL;
2394 }
2395 /* skip '/' */
2396 ++(*path);
2397
2398 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002399 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002400
2401 if ((**path == '/' && (*path)[1]) || !**path) {
2402 /* path continues by another token or this is the last token */
2403 return LY_SUCCESS;
2404 } else if ((*path)[0] != '[') {
2405 /* unexpected character */
2406 return LY_EINVAL;
2407 } else {
2408 /* predicate starting with [ */
2409 *has_predicate = 1;
2410 return LY_SUCCESS;
2411 }
2412}
2413
2414/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002415 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2416 *
2417 * The set of features used for target must be a subset of features used for the leafref.
2418 * This is not a perfect, we should compare the truth tables but it could require too much resources
2419 * and RFC 7950 does not require it explicitely, so we simplify that.
2420 *
2421 * @param[in] refnode The leafref node.
2422 * @param[in] target Tha target node of the leafref.
2423 * @return LY_SUCCESS or LY_EVALID;
2424 */
2425static LY_ERR
2426lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2427{
2428 LY_ERR ret = LY_EVALID;
2429 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002430 LY_ARRAY_COUNT_TYPE u, v, count;
Radek Krejci58d171e2018-11-23 13:50:55 +01002431 struct ly_set features = {0};
2432
2433 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002434 if (iter->iffeatures) {
2435 LY_ARRAY_FOR(iter->iffeatures, u) {
2436 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2437 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002438 }
2439 }
2440 }
2441 }
2442
2443 /* we should have, in features set, a superset of features applicable to the target node.
2444 * So when adding features applicable to the target into the features set, we should not be
2445 * able to actually add any new feature, otherwise it is not a subset of features applicable
2446 * to the leafref itself. */
2447 count = features.count;
2448 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002449 if (iter->iffeatures) {
2450 LY_ARRAY_FOR(iter->iffeatures, u) {
2451 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2452 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002453 /* new feature was added (or LY_EMEM) */
2454 goto cleanup;
2455 }
2456 }
2457 }
2458 }
2459 }
2460 ret = LY_SUCCESS;
2461
2462cleanup:
2463 ly_set_erase(&features, NULL);
2464 return ret;
2465}
2466
Michal Vasko004d3152020-06-11 19:59:22 +02002467static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2468 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2469 struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002470
Radek Krejcia3045382018-11-22 14:30:31 +01002471/**
2472 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2473 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002474 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2475 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2476 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2477 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002478 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002479 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002480 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002481 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2482 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2483 * @param[out] type Newly created type structure with the filled information about the type.
2484 * @return LY_ERR value.
2485 */
Radek Krejci19a96102018-11-15 13:38:09 +01002486static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002487lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2488 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2489 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2490 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002491{
2492 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002493 struct lysc_type_bin *bin;
2494 struct lysc_type_num *num;
2495 struct lysc_type_str *str;
2496 struct lysc_type_bits *bits;
2497 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002498 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002499 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002500 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002501 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002502
2503 switch (basetype) {
2504 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002505 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002506
2507 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002508 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002509 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2510 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002511 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002512 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 +01002513 }
2514 }
2515
2516 if (tpdfname) {
2517 type_p->compiled = *type;
2518 *type = calloc(1, sizeof(struct lysc_type_bin));
2519 }
2520 break;
2521 case LY_TYPE_BITS:
2522 /* RFC 7950 9.7 - bits */
2523 bits = (struct lysc_type_bits*)(*type);
2524 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002525 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002526 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2527 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002528 }
2529
Radek Krejci555cb5b2018-11-16 14:54:33 +01002530 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002531 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002532 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002533 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002534 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002536 free(*type);
2537 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002538 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002539 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002540 }
2541
2542 if (tpdfname) {
2543 type_p->compiled = *type;
2544 *type = calloc(1, sizeof(struct lysc_type_bits));
2545 }
2546 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002547 case LY_TYPE_DEC64:
2548 dec = (struct lysc_type_dec*)(*type);
2549
2550 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002551 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002552 if (!type_p->fraction_digits) {
2553 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002555 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002556 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002557 free(*type);
2558 *type = NULL;
2559 }
2560 return LY_EVALID;
2561 }
2562 } else if (type_p->fraction_digits) {
2563 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002564 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002565 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2566 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002567 tpdfname);
2568 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002569 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2570 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002571 free(*type);
2572 *type = NULL;
2573 }
2574 return LY_EVALID;
2575 }
2576 dec->fraction_digits = type_p->fraction_digits;
2577
2578 /* RFC 7950 9.2.4 - range */
2579 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002580 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2581 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002582 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002583 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 +01002584 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002585 }
2586
2587 if (tpdfname) {
2588 type_p->compiled = *type;
2589 *type = calloc(1, sizeof(struct lysc_type_dec));
2590 }
2591 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002592 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002593 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002594
2595 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002596 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002597 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2598 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002599 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002600 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 +01002601 }
2602 } else if (base && ((struct lysc_type_str*)base)->length) {
2603 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2604 }
2605
2606 /* RFC 7950 9.4.5 - pattern */
2607 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002608 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002609 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002610 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2611 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2612 }
2613
2614 if (tpdfname) {
2615 type_p->compiled = *type;
2616 *type = calloc(1, sizeof(struct lysc_type_str));
2617 }
2618 break;
2619 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002620 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002621
2622 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002623 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002624 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002625 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002626 }
2627
Radek Krejci555cb5b2018-11-16 14:54:33 +01002628 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002629 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002630 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002631 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002632 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002633 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002634 free(*type);
2635 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002636 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002637 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002638 }
2639
2640 if (tpdfname) {
2641 type_p->compiled = *type;
2642 *type = calloc(1, sizeof(struct lysc_type_enum));
2643 }
2644 break;
2645 case LY_TYPE_INT8:
2646 case LY_TYPE_UINT8:
2647 case LY_TYPE_INT16:
2648 case LY_TYPE_UINT16:
2649 case LY_TYPE_INT32:
2650 case LY_TYPE_UINT32:
2651 case LY_TYPE_INT64:
2652 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002653 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002654
2655 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002656 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002657 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2658 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002659 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002660 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 +01002661 }
2662 }
2663
2664 if (tpdfname) {
2665 type_p->compiled = *type;
2666 *type = calloc(1, sizeof(struct lysc_type_num));
2667 }
2668 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002669 case LY_TYPE_IDENT:
2670 idref = (struct lysc_type_identityref*)(*type);
2671
2672 /* RFC 7950 9.10.2 - base */
2673 if (type_p->bases) {
2674 if (base) {
2675 /* only the directly derived identityrefs can contain base specification */
2676 if (tpdfname) {
2677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002678 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002679 tpdfname);
2680 } else {
2681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002682 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002683 free(*type);
2684 *type = NULL;
2685 }
2686 return LY_EVALID;
2687 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002688 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002689 }
2690
2691 if (!base && !type_p->flags) {
2692 /* type derived from identityref built-in type must contain at least one base */
2693 if (tpdfname) {
2694 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2695 } else {
2696 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2697 free(*type);
2698 *type = NULL;
2699 }
2700 return LY_EVALID;
2701 }
2702
2703 if (tpdfname) {
2704 type_p->compiled = *type;
2705 *type = calloc(1, sizeof(struct lysc_type_identityref));
2706 }
2707 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002708 case LY_TYPE_LEAFREF:
Michal Vasko004d3152020-06-11 19:59:22 +02002709 lref = (struct lysc_type_leafref*)*type;
2710
Radek Krejcia3045382018-11-22 14:30:31 +01002711 /* RFC 7950 9.9.3 - require-instance */
2712 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002713 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002714 if (tpdfname) {
2715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2716 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2717 } else {
2718 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2719 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2720 free(*type);
2721 *type = NULL;
2722 }
2723 return LY_EVALID;
2724 }
Michal Vasko004d3152020-06-11 19:59:22 +02002725 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002726 } else if (base) {
2727 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002728 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002729 } else {
2730 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002731 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002732 }
2733 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002734 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2735 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002736 } else if (base) {
Michal Vasko004d3152020-06-11 19:59:22 +02002737 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path);
2738 lref->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002739 } else if (tpdfname) {
2740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2741 return LY_EVALID;
2742 } else {
2743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2744 free(*type);
2745 *type = NULL;
2746 return LY_EVALID;
2747 }
2748 if (tpdfname) {
2749 type_p->compiled = *type;
2750 *type = calloc(1, sizeof(struct lysc_type_leafref));
2751 }
2752 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002753 case LY_TYPE_INST:
2754 /* RFC 7950 9.9.3 - require-instance */
2755 if (type_p->flags & LYS_SET_REQINST) {
2756 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2757 } else {
2758 /* default is true */
2759 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2760 }
2761
2762 if (tpdfname) {
2763 type_p->compiled = *type;
2764 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2765 }
2766 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002767 case LY_TYPE_UNION:
2768 un = (struct lysc_type_union*)(*type);
2769
2770 /* RFC 7950 7.4 - type */
2771 if (type_p->types) {
2772 if (base) {
2773 /* only the directly derived union can contain types specification */
2774 if (tpdfname) {
2775 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2776 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2777 tpdfname);
2778 } else {
2779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2780 "Invalid type substatement for the type not directly derived from union built-in type.");
2781 free(*type);
2782 *type = NULL;
2783 }
2784 return LY_EVALID;
2785 }
2786 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002787 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2788 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002789 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
2790 &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002791 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2792 /* add space for additional types from the union subtype */
2793 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002794 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 +02002795 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002796
2797 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002798 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002799 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2800 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2801 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2802 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 +02002803 lref = (struct lysc_type_leafref *)un->types[u + additional];
2804
2805 lref->basetype = LY_TYPE_LEAFREF;
2806 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path);
2807 lref->refcount = 1;
2808 lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2809 lref->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002810 /* TODO extensions */
2811
2812 } else {
2813 un->types[u + additional] = un_aux->types[v];
2814 ++un_aux->types[v]->refcount;
2815 }
2816 ++additional;
2817 LY_ARRAY_INCREMENT(un->types);
2818 }
2819 /* compensate u increment in main loop */
2820 --additional;
2821
2822 /* free the replaced union subtype */
2823 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2824 } else {
2825 LY_ARRAY_INCREMENT(un->types);
2826 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002827 }
2828 }
2829
2830 if (!base && !type_p->flags) {
2831 /* type derived from union built-in type must contain at least one type */
2832 if (tpdfname) {
2833 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2834 } else {
2835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2836 free(*type);
2837 *type = NULL;
2838 }
2839 return LY_EVALID;
2840 }
2841
2842 if (tpdfname) {
2843 type_p->compiled = *type;
2844 *type = calloc(1, sizeof(struct lysc_type_union));
2845 }
2846 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002847 case LY_TYPE_BOOL:
2848 case LY_TYPE_EMPTY:
2849 case LY_TYPE_UNKNOWN: /* just to complete switch */
2850 break;
2851 }
2852 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2853done:
2854 return ret;
2855}
2856
Radek Krejcia3045382018-11-22 14:30:31 +01002857/**
2858 * @brief Compile information about the leaf/leaf-list's type.
2859 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002860 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2861 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2862 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2863 * @param[in] context_name Name of the context node or referencing typedef for logging.
2864 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002865 * @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 +01002866 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002867 * @return LY_ERR value.
2868 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002869static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002870lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2871 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2872 struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002873{
2874 LY_ERR ret = LY_SUCCESS;
2875 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002876 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002877 struct type_context {
2878 const struct lysp_tpdf *tpdf;
2879 struct lysp_node *node;
2880 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002881 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002882 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002883 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002884 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002885 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002886 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002887
2888 (*type) = NULL;
2889
2890 tctx = calloc(1, sizeof *tctx);
2891 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002892 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002893 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2894 ret == LY_SUCCESS;
2895 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2896 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2897 if (basetype) {
2898 break;
2899 }
2900
2901 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002902 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2903 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002904 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2905
Radek Krejcicdfecd92018-11-26 11:27:32 +01002906 if (units && !*units) {
2907 /* inherit units */
2908 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2909 }
Radek Krejci01342af2019-01-03 15:18:08 +01002910 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002911 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002912 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02002913 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002914 }
Radek Krejci01342af2019-01-03 15:18:08 +01002915 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002916 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2917 break;
2918 }
2919
Radek Krejci19a96102018-11-15 13:38:09 +01002920 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002921 /* it is not necessary to continue, the rest of the chain was already compiled,
2922 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002923 basetype = tctx->tpdf->type.compiled->basetype;
2924 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002925 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002926 dummyloops = 1;
2927 goto preparenext;
2928 } else {
2929 tctx = NULL;
2930 break;
2931 }
Radek Krejci19a96102018-11-15 13:38:09 +01002932 }
2933
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002934 /* circular typedef reference detection */
2935 for (u = 0; u < tpdf_chain.count; u++) {
2936 /* local part */
2937 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2938 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2939 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2940 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2941 free(tctx);
2942 ret = LY_EVALID;
2943 goto cleanup;
2944 }
2945 }
2946 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2947 /* global part for unions corner case */
2948 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2949 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2950 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2951 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2952 free(tctx);
2953 ret = LY_EVALID;
2954 goto cleanup;
2955 }
2956 }
2957
Radek Krejci19a96102018-11-15 13:38:09 +01002958 /* store information for the following processing */
2959 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2960
Radek Krejcicdfecd92018-11-26 11:27:32 +01002961preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002962 /* prepare next loop */
2963 tctx_prev = tctx;
2964 tctx = calloc(1, sizeof *tctx);
2965 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2966 }
2967 free(tctx);
2968
2969 /* allocate type according to the basetype */
2970 switch (basetype) {
2971 case LY_TYPE_BINARY:
2972 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002973 break;
2974 case LY_TYPE_BITS:
2975 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002976 break;
2977 case LY_TYPE_BOOL:
2978 case LY_TYPE_EMPTY:
2979 *type = calloc(1, sizeof(struct lysc_type));
2980 break;
2981 case LY_TYPE_DEC64:
2982 *type = calloc(1, sizeof(struct lysc_type_dec));
2983 break;
2984 case LY_TYPE_ENUM:
2985 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002986 break;
2987 case LY_TYPE_IDENT:
2988 *type = calloc(1, sizeof(struct lysc_type_identityref));
2989 break;
2990 case LY_TYPE_INST:
2991 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2992 break;
2993 case LY_TYPE_LEAFREF:
2994 *type = calloc(1, sizeof(struct lysc_type_leafref));
2995 break;
2996 case LY_TYPE_STRING:
2997 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002998 break;
2999 case LY_TYPE_UNION:
3000 *type = calloc(1, sizeof(struct lysc_type_union));
3001 break;
3002 case LY_TYPE_INT8:
3003 case LY_TYPE_UINT8:
3004 case LY_TYPE_INT16:
3005 case LY_TYPE_UINT16:
3006 case LY_TYPE_INT32:
3007 case LY_TYPE_UINT32:
3008 case LY_TYPE_INT64:
3009 case LY_TYPE_UINT64:
3010 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003011 break;
3012 case LY_TYPE_UNKNOWN:
3013 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3014 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3015 ret = LY_EVALID;
3016 goto cleanup;
3017 }
3018 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003019 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3021 ly_data_type2str[basetype]);
3022 free(*type);
3023 (*type) = NULL;
3024 ret = LY_EVALID;
3025 goto cleanup;
3026 }
3027
3028 /* get restrictions from the referred typedefs */
3029 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3030 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003031
3032 /* remember the typedef context for circular check */
3033 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3034
Radek Krejci43699232018-11-23 14:59:46 +01003035 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003036 base = tctx->tpdf->type.compiled;
3037 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003038 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003039 /* no change, just use the type information from the base */
3040 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3041 ++base->refcount;
3042 continue;
3043 }
3044
3045 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003046 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3047 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3048 tctx->tpdf->name, ly_data_type2str[basetype]);
3049 ret = LY_EVALID;
3050 goto cleanup;
3051 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3052 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3053 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3054 tctx->tpdf->name, tctx->tpdf->dflt);
3055 ret = LY_EVALID;
3056 goto cleanup;
3057 }
3058
Radek Krejci19a96102018-11-15 13:38:09 +01003059 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003060 /* TODO user type plugins */
3061 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003062 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003063 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 +01003064 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003065 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003066 LY_CHECK_GOTO(ret, cleanup);
3067 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003068 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003069 /* remove the processed typedef contexts from the stack for circular check */
3070 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003071
Radek Krejcic5c27e52018-11-15 14:38:11 +01003072 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003073 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003074 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003075 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003076 /* TODO user type plugins */
3077 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003078 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003079 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 +01003080 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003081 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003082 /* no specific restriction in leaf's type definition, copy from the base */
3083 free(*type);
3084 (*type) = base;
3085 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003086 }
Radek Krejcia1911222019-07-22 17:24:50 +02003087 if (dflt && !(*type)->dflt) {
3088 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003089 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003090 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3091 (*type)->dflt->realtype = (*type);
3092 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3093 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003094 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003095 if (err) {
3096 ly_err_print(err);
3097 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3098 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3099 ly_err_free(err);
3100 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003101 if (ret == LY_EINCOMPLETE) {
3102 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003103 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003104
3105 /* but in general result is so far ok */
3106 ret = LY_SUCCESS;
3107 }
Radek Krejcia1911222019-07-22 17:24:50 +02003108 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003109 }
Radek Krejci19a96102018-11-15 13:38:09 +01003110
Radek Krejci0935f412019-08-20 16:15:18 +02003111 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003112
3113cleanup:
3114 ly_set_erase(&tpdf_chain, free);
3115 return ret;
3116}
3117
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003118/**
3119 * @brief Compile status information of the given node.
3120 *
3121 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3122 * has the status correctly set during the compilation.
3123 *
3124 * @param[in] ctx Compile context
3125 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3126 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3127 * the compatibility with the parent's status value.
3128 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3129 * @return LY_ERR value.
3130 */
3131static LY_ERR
3132lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3133{
3134 /* status - it is not inherited by specification, but it does not make sense to have
3135 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3136 if (!((*node_flags) & LYS_STATUS_MASK)) {
3137 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3138 if ((parent_flags & 0x3) != 0x3) {
3139 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3140 * combination of bits (0x3) which marks the uses_status value */
3141 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3142 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3143 }
3144 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3145 } else {
3146 (*node_flags) |= LYS_STATUS_CURR;
3147 }
3148 } else if (parent_flags & LYS_STATUS_MASK) {
3149 /* check status compatibility with the parent */
3150 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3151 if ((*node_flags) & LYS_STATUS_CURR) {
3152 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3153 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3154 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3155 } else { /* LYS_STATUS_DEPRC */
3156 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3157 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3158 }
3159 return LY_EVALID;
3160 }
3161 }
3162 return LY_SUCCESS;
3163}
3164
Radek Krejci8cce8532019-03-05 11:27:45 +01003165/**
3166 * @brief Check uniqness of the node/action/notification name.
3167 *
3168 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3169 * structures, but they share the namespace so we need to check their name collisions.
3170 *
3171 * @param[in] ctx Compile context.
3172 * @param[in] children List (linked list) of data nodes to go through.
3173 * @param[in] actions List (sized array) of actions or RPCs to go through.
3174 * @param[in] notifs List (sized array) of Notifications to go through.
3175 * @param[in] name Name of the item to find in the given lists.
3176 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3177 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3178 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3179 */
3180static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02003181lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions,
3182 const struct lysc_notif *notifs, const char *name, void *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003183{
3184 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003185 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003186
3187 LY_LIST_FOR(children, iter) {
3188 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3189 goto error;
3190 }
3191 }
3192 LY_ARRAY_FOR(actions, u) {
3193 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3194 goto error;
3195 }
3196 }
3197 LY_ARRAY_FOR(notifs, u) {
3198 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3199 goto error;
3200 }
3201 }
3202 return LY_SUCCESS;
3203error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003204 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003205 return LY_EEXIST;
3206}
3207
Radek Krejciec4da802019-05-02 13:02:41 +02003208static 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 +01003209
Radek Krejcia3045382018-11-22 14:30:31 +01003210/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003211 * @brief Compile parsed RPC/action schema node information.
3212 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003213 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003214 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003215 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3216 * @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).
3217 * Zero means no uses, non-zero value with no status bit set mean the default status.
3218 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3219 */
3220static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003221lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003222 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3223{
3224 LY_ERR ret = LY_SUCCESS;
3225 struct lysp_node *child_p;
3226 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003227 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003228
Radek Krejci327de162019-06-14 12:52:07 +02003229 lysc_update_path(ctx, parent, action_p->name);
3230
Radek Krejci8cce8532019-03-05 11:27:45 +01003231 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3232 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3233 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3234 action_p->name, action)) {
3235 return LY_EVALID;
3236 }
3237
Radek Krejciec4da802019-05-02 13:02:41 +02003238 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003239 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003240 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003241 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003242 return LY_EVALID;
3243 }
3244
Michal Vasko1bf09392020-03-27 12:38:10 +01003245 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003246 action->module = ctx->mod;
3247 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003248 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003249 action->sp = action_p;
3250 }
3251 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3252
3253 /* status - it is not inherited by specification, but it does not make sense to have
3254 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003255 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003256
3257 DUP_STRING(ctx->ctx, action_p->name, action->name);
3258 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3259 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003260 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003261 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003262
3263 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003264 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003265 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003266 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 +02003267 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003268 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003269 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003270 }
Radek Krejci327de162019-06-14 12:52:07 +02003271 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003272 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003273
3274 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003275 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003276 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003277 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 +02003278 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003279 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003280 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003281 }
Radek Krejci327de162019-06-14 12:52:07 +02003282 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003283 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003284
3285 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3286 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003287 ly_set_add(&ctx->xpath, action, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003288 }
3289
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003290cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003291 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003292 return ret;
3293}
3294
3295/**
Radek Krejci43981a32019-04-12 09:44:11 +02003296 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003297 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003298 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003299 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3300 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3301 * @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 +02003302 * Zero means no uses, non-zero value with no status bit set mean the default status.
3303 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3304 */
3305static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003306lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003307 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3308{
3309 LY_ERR ret = LY_SUCCESS;
3310 struct lysp_node *child_p;
3311 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003312 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003313
Radek Krejci327de162019-06-14 12:52:07 +02003314 lysc_update_path(ctx, parent, notif_p->name);
3315
Radek Krejcifc11bd72019-04-11 16:00:05 +02003316 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3317 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3318 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3319 notif_p->name, notif)) {
3320 return LY_EVALID;
3321 }
3322
Radek Krejciec4da802019-05-02 13:02:41 +02003323 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003324 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3325 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003326 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003327 return LY_EVALID;
3328 }
3329
3330 notif->nodetype = LYS_NOTIF;
3331 notif->module = ctx->mod;
3332 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003333 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003334 notif->sp = notif_p;
3335 }
3336 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3337
3338 /* status - it is not inherited by specification, but it does not make sense to have
3339 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3340 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3341
3342 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3343 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3344 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003345 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003346 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003347 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3348 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003349 ly_set_add(&ctx->xpath, notif, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003350 }
Radek Krejci0935f412019-08-20 16:15:18 +02003351 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003352
Radek Krejciec4da802019-05-02 13:02:41 +02003353 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003354 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003355 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003356 }
3357
Radek Krejci327de162019-06-14 12:52:07 +02003358 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003359cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003360 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003361 return ret;
3362}
3363
3364/**
Radek Krejcia3045382018-11-22 14:30:31 +01003365 * @brief Compile parsed container node information.
3366 * @param[in] ctx Compile context
3367 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003368 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3369 * is enriched with the container-specific information.
3370 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3371 */
Radek Krejci19a96102018-11-15 13:38:09 +01003372static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003373lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003374{
3375 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3376 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3377 struct lysp_node *child_p;
3378 unsigned int u;
3379 LY_ERR ret = LY_SUCCESS;
3380
Radek Krejcife909632019-02-12 15:34:42 +01003381 if (cont_p->presence) {
3382 cont->flags |= LYS_PRESENCE;
3383 }
3384
Radek Krejci19a96102018-11-15 13:38:09 +01003385 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003386 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003387 }
3388
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003389 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003390 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3391 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003392 ly_set_add(&ctx->xpath, cont, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003393 }
Radek Krejciec4da802019-05-02 13:02:41 +02003394 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3395 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003396
3397done:
3398 return ret;
3399}
3400
Radek Krejci33f72892019-02-21 10:36:58 +01003401/*
3402 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3403 * @param[in] ctx Compile context.
3404 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3405 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003406 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3407 * @return LY_ERR value.
3408 */
3409static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003410lys_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 +01003411{
Radek Krejci33f72892019-02-21 10:36:58 +01003412 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3413
Radek Krejciec4da802019-05-02 13:02:41 +02003414 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 +01003415 leaf->units ? NULL : &leaf->units));
3416 if (leaf->nodetype == LYS_LEAFLIST) {
3417 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003418 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3419 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003420 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003421 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3422 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3423 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3424 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003425 LY_ARRAY_INCREMENT(llist->dflts);
3426 }
3427 } else {
3428 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003429 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003430 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3431 leaf->dflt->realtype = leaf->type->dflt->realtype;
3432 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3433 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003434 }
3435 }
3436 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3437 /* 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 +02003438 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003439 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003440 LY_ARRAY_COUNT_TYPE u;
Radek Krejci33f72892019-02-21 10:36:58 +01003441 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3442 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3443 /* 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 +02003444 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003445 }
3446 }
3447 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003448 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3449 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3450 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3451 return LY_EVALID;
3452 }
3453 }
3454
Radek Krejci33f72892019-02-21 10:36:58 +01003455 return LY_SUCCESS;
3456}
3457
Radek Krejcia3045382018-11-22 14:30:31 +01003458/**
3459 * @brief Compile parsed leaf node information.
3460 * @param[in] ctx Compile context
3461 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003462 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3463 * is enriched with the leaf-specific information.
3464 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3465 */
Radek Krejci19a96102018-11-15 13:38:09 +01003466static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003467lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003468{
3469 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3470 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3471 unsigned int u;
3472 LY_ERR ret = LY_SUCCESS;
3473
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003474 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003475 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3476 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003477 ly_set_add(&ctx->xpath, leaf, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003478 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003479 if (leaf_p->units) {
3480 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3481 leaf->flags |= LYS_SET_UNITS;
3482 }
Radek Krejcia1911222019-07-22 17:24:50 +02003483
3484 /* the dflt member is just filled to avoid getting the default value from the type */
3485 leaf->dflt = (void*)leaf_p->dflt;
3486 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003487 if (ret) {
3488 leaf->dflt = NULL;
3489 return ret;
3490 }
Radek Krejcia1911222019-07-22 17:24:50 +02003491
Radek Krejciccd20f12019-02-15 14:12:27 +01003492 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003493 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003494 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003495 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3496 leaf->dflt->realtype = leaf->type;
3497 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3498 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003499 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003500 leaf->dflt->realtype->refcount++;
3501 if (err) {
3502 ly_err_print(err);
3503 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3504 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3505 ly_err_free(err);
3506 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003507 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003508 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003509 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003510
3511 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003512 ret = LY_SUCCESS;
3513 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003514 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003515 leaf->flags |= LYS_SET_DFLT;
3516 }
Radek Krejci43699232018-11-23 14:59:46 +01003517
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003518
Radek Krejci19a96102018-11-15 13:38:09 +01003519done:
3520 return ret;
3521}
3522
Radek Krejcia3045382018-11-22 14:30:31 +01003523/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003524 * @brief Compile parsed leaf-list node information.
3525 * @param[in] ctx Compile context
3526 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003527 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3528 * is enriched with the leaf-list-specific information.
3529 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3530 */
3531static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003532lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003533{
3534 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3535 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003536 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003537 LY_ERR ret = LY_SUCCESS;
3538
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003539 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003540 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3541 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003542 ly_set_add(&ctx->xpath, llist, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003543 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003544 if (llist_p->units) {
3545 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3546 llist->flags |= LYS_SET_UNITS;
3547 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003548
Radek Krejcia1911222019-07-22 17:24:50 +02003549 /* the dflts member is just filled to avoid getting the default value from the type */
3550 llist->dflts = (void*)llist_p->dflts;
3551 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Michal Vasko6a044b22020-01-15 12:25:39 +01003552 if (ret != LY_SUCCESS) {
3553 /* make sure the defaults are freed correctly */
3554 if (llist_p->dflts) {
3555 llist->dflts = NULL;
3556 }
3557 return ret;
3558 }
3559
Radek Krejci0e5d8382018-11-28 16:37:53 +01003560 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003561 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003562 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
3563 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003564 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003565 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003566 LY_ARRAY_INCREMENT(llist->dflts_mods);
3567 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003568 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003569 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3570 llist->dflts[u]->realtype = llist->type;
3571 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3572 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003573 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003574 llist->dflts[u]->realtype->refcount++;
3575 if (err) {
3576 ly_err_print(err);
3577 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3578 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3579 ly_err_free(err);
3580 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003581 if (ret == LY_EINCOMPLETE) {
3582 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003583 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003584
3585 /* but in general result is so far ok */
3586 ret = LY_SUCCESS;
3587 }
Radek Krejcia1911222019-07-22 17:24:50 +02003588 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003589 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003590 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003591 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003592 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02003593 /* configuration data values must be unique - so check the default values */
3594 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003595 for (v = u + 1; v < LY_ARRAY_COUNT(llist->dflts); ++v) {
Radek Krejcia1911222019-07-22 17:24:50 +02003596 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3597 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003598 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 +02003599 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3600 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3601 if (dynamic) {
3602 free((char*)val);
3603 }
3604 return LY_EVALID;
3605 }
3606 }
3607 }
3608 }
3609
3610 /* 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 +01003611
3612 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003613 if (llist->min) {
3614 llist->flags |= LYS_MAND_TRUE;
3615 }
Radek Krejcib7408632018-11-28 17:12:11 +01003616 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003617
Radek Krejci0e5d8382018-11-28 16:37:53 +01003618done:
3619 return ret;
3620}
3621
3622/**
Radek Krejci7af64242019-02-18 13:07:53 +01003623 * @brief Compile information about list's uniques.
3624 * @param[in] ctx Compile context.
3625 * @param[in] context_module Module where the prefixes are going to be resolved.
3626 * @param[in] uniques Sized array list of unique statements.
3627 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3628 * @return LY_ERR value.
3629 */
3630static LY_ERR
3631lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3632{
3633 LY_ERR ret = LY_SUCCESS;
3634 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003635 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003636 const char *keystr, *delim;
3637 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003638 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7af64242019-02-18 13:07:53 +01003639 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003640 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003641
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003642 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003643 config = -1;
3644 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3645 keystr = uniques[v];
3646 while (keystr) {
3647 delim = strpbrk(keystr, " \t\n");
3648 if (delim) {
3649 len = delim - keystr;
3650 while (isspace(*delim)) {
3651 ++delim;
3652 }
3653 } else {
3654 len = strlen(keystr);
3655 }
3656
3657 /* unique node must be present */
3658 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003659 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3660 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003661 if (ret != LY_SUCCESS) {
3662 if (ret == LY_EDENIED) {
3663 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003664 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003665 len, keystr, lys_nodetype2str((*key)->nodetype));
3666 }
3667 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003668 } else if (flags) {
3669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3670 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003671 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003672 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003673 }
3674
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003675
Radek Krejci7af64242019-02-18 13:07:53 +01003676 /* all referenced leafs must be of the same config type */
3677 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003679 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003680 return LY_EVALID;
3681 } else if ((*key)->flags & LYS_CONFIG_W) {
3682 config = 1;
3683 } else { /* LYS_CONFIG_R */
3684 config = 0;
3685 }
3686
Michal Vasko14654712020-02-06 08:35:21 +01003687 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3688 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3689 if (parent->nodetype == LYS_LIST) {
3690 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3691 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3692 return LY_EVALID;
3693 }
3694 }
3695
Radek Krejci7af64242019-02-18 13:07:53 +01003696 /* check status */
3697 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3698 (*key)->flags, (*key)->module, (*key)->name));
3699
3700 /* mark leaf as unique */
3701 (*key)->flags |= LYS_UNIQUE;
3702
3703 /* next unique value in line */
3704 keystr = delim;
3705 }
3706 /* next unique definition */
3707 }
3708
3709 return LY_SUCCESS;
3710}
3711
3712/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003713 * @brief Compile parsed list node information.
3714 * @param[in] ctx Compile context
3715 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003716 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3717 * is enriched with the list-specific information.
3718 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3719 */
3720static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003721lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003722{
3723 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3724 struct lysc_node_list *list = (struct lysc_node_list*)node;
3725 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003726 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003727 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003728 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003729 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003730 LY_ERR ret = LY_SUCCESS;
3731
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003732 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003733 if (list->min) {
3734 list->flags |= LYS_MAND_TRUE;
3735 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003736 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3737
3738 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003739 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003740 }
3741
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003742 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003743 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3744 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003745 ly_set_add(&ctx->xpath, list, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003746 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003747
3748 /* keys */
3749 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3750 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3751 return LY_EVALID;
3752 }
3753
3754 /* find all the keys (must be direct children) */
3755 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003756 if (!keystr) {
3757 /* keyless list */
3758 list->flags |= LYS_KEYLESS;
3759 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003760 while (keystr) {
3761 delim = strpbrk(keystr, " \t\n");
3762 if (delim) {
3763 len = delim - keystr;
3764 while (isspace(*delim)) {
3765 ++delim;
3766 }
3767 } else {
3768 len = strlen(keystr);
3769 }
3770
3771 /* key node must be present */
Michal Vaskoe444f752020-02-10 12:20:06 +01003772 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 +02003773 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003774 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3775 "The list's key \"%.*s\" not found.", len, keystr);
3776 return LY_EVALID;
3777 }
3778 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003779 if (key->flags & LYS_KEY) {
3780 /* the node was already marked as a key */
3781 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3782 "Duplicated key identifier \"%.*s\".", len, keystr);
3783 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003784 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003785
3786 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003787 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003788 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3790 return LY_EVALID;
3791 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003792 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003793 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003794 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003795 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003796 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003797 return LY_EVALID;
3798 }
3799 } else {
3800 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003801 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003802 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003803 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003804 return LY_EVALID;
3805 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003806 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003807 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003808 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003809 return LY_EVALID;
3810 }
3811 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003812
3813 /* check status */
3814 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003815 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003816
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003817 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003818 if (key->dflt) {
3819 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3820 lysc_type_free(ctx->ctx, key->dflt->realtype);
3821 free(key->dflt);
3822 key->dflt = NULL;
3823 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003824 }
3825 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003826 key->flags |= LYS_KEY;
3827
3828 /* move it to the correct position */
3829 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3830 /* fix links in closest previous siblings of the key */
3831 if (key->next) {
3832 key->next->prev = key->prev;
3833 } else {
3834 /* last child */
3835 list->child->prev = key->prev;
3836 }
3837 if (key->prev->next) {
3838 key->prev->next = key->next;
3839 }
3840 /* fix links in the key */
3841 if (prev_key) {
3842 key->prev = (struct lysc_node*)prev_key;
3843 key->next = prev_key->next;
3844 } else {
3845 key->prev = list->child->prev;
3846 key->next = list->child;
3847 }
3848 /* fix links in closes future siblings of the key */
3849 if (prev_key) {
3850 if (prev_key->next) {
3851 prev_key->next->prev = (struct lysc_node*)key;
3852 } else {
3853 list->child->prev = (struct lysc_node*)key;
3854 }
3855 prev_key->next = (struct lysc_node*)key;
3856 } else {
3857 list->child->prev = (struct lysc_node*)key;
3858 }
3859 /* fix links in parent */
3860 if (!key->prev->next) {
3861 list->child = (struct lysc_node*)key;
3862 }
3863 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003864
3865 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003866 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003867 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003868 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003869 }
3870
3871 /* uniques */
3872 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003873 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003874 }
3875
Radek Krejciec4da802019-05-02 13:02:41 +02003876 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3877 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003878
3879done:
3880 return ret;
3881}
3882
Radek Krejcib56c7502019-02-13 14:19:54 +01003883/**
3884 * @brief Do some checks and set the default choice's case.
3885 *
3886 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3887 *
3888 * @param[in] ctx Compile context.
3889 * @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,
3890 * not the reference to the imported module.
3891 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3892 * @return LY_ERR value.
3893 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003894static LY_ERR
3895lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3896{
3897 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3898 const char *prefix = NULL, *name;
3899 size_t prefix_len = 0;
3900
3901 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3902 name = strchr(dflt, ':');
3903 if (name) {
3904 prefix = dflt;
3905 prefix_len = name - prefix;
3906 ++name;
3907 } else {
3908 name = dflt;
3909 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003910 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003911 /* prefixed default case make sense only for the prefix of the schema itself */
3912 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3913 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3914 prefix_len, prefix);
3915 return LY_EVALID;
3916 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003917 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 +01003918 if (!ch->dflt) {
3919 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3920 "Default case \"%s\" not found.", dflt);
3921 return LY_EVALID;
3922 }
3923 /* no mandatory nodes directly under the default case */
3924 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003925 if (iter->parent != (struct lysc_node*)ch->dflt) {
3926 break;
3927 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003928 if (iter->flags & LYS_MAND_TRUE) {
3929 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3930 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3931 return LY_EVALID;
3932 }
3933 }
Radek Krejci01342af2019-01-03 15:18:08 +01003934 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003935 return LY_SUCCESS;
3936}
3937
Radek Krejciccd20f12019-02-15 14:12:27 +01003938static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003939lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003940{
3941 struct lys_module *mod;
3942 const char *prefix = NULL, *name;
3943 size_t prefix_len = 0;
3944 struct lysc_node_case *cs;
3945 struct lysc_node *node;
3946
3947 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3948 name = strchr(dflt, ':');
3949 if (name) {
3950 prefix = dflt;
3951 prefix_len = name - prefix;
3952 ++name;
3953 } else {
3954 name = dflt;
3955 }
3956 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3957 if (prefix) {
3958 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3959 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003960 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3961 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003962 return LY_EVALID;
3963 }
3964 } else {
3965 mod = ctx->mod;
3966 }
3967 /* get the default case */
Michal Vaskoe444f752020-02-10 12:20:06 +01003968 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 +01003969 if (!cs) {
3970 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003971 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003972 return LY_EVALID;
3973 }
3974
3975 /* check that there is no mandatory node */
3976 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003977 if (node->parent != (struct lysc_node*)cs) {
3978 break;
3979 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003980 if (node->flags & LYS_MAND_TRUE) {
3981 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003982 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3983 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003984 return LY_EVALID;
3985 }
3986 }
3987
3988 /* set the default case in choice */
3989 ch->dflt = cs;
3990 cs->flags |= LYS_SET_DFLT;
3991
3992 return LY_SUCCESS;
3993}
3994
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003995/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003996 * @brief Compile parsed choice node information.
3997 * @param[in] ctx Compile context
3998 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003999 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004000 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004001 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4002 */
4003static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004004lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004005{
4006 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4007 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4008 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004009 LY_ERR ret = LY_SUCCESS;
4010
Radek Krejci056d0a82018-12-06 16:57:25 +01004011 LY_LIST_FOR(ch_p->child, child_p) {
4012 if (child_p->nodetype == LYS_CASE) {
4013 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004014 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004015 }
4016 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004017 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004018 }
4019 }
4020
4021 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004022 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004023 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004024 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004025
Radek Krejci9800fb82018-12-13 14:26:23 +01004026 return ret;
4027}
4028
4029/**
4030 * @brief Compile parsed anydata or anyxml node information.
4031 * @param[in] ctx Compile context
4032 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004033 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4034 * is enriched with the any-specific information.
4035 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4036 */
4037static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004038lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004039{
4040 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4041 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4042 unsigned int u;
4043 LY_ERR ret = LY_SUCCESS;
4044
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004045 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004046 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4047 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004048 ly_set_add(&ctx->xpath, any, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004049 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004050
4051 if (any->flags & LYS_CONFIG_W) {
4052 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004053 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004054 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004055done:
4056 return ret;
4057}
4058
Radek Krejcib56c7502019-02-13 14:19:54 +01004059/**
Michal Vasko795b3752020-07-13 15:24:27 +02004060 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
4061 * keep specific order of augments targetting the same node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004062 *
4063 * @param[in] ctx Compile context
4064 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4065 * the choice itself is expected instead of a specific case node.
4066 * @param[in] node Schema node to connect into the list.
4067 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004068 * 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 +01004069 */
4070static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004071lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004072{
Michal Vasko795b3752020-07-13 15:24:27 +02004073 struct lysc_node **children, *start, *end;
4074 const struct lys_module *mod;
Radek Krejci056d0a82018-12-06 16:57:25 +01004075
4076 if (node->nodetype == LYS_CASE) {
4077 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4078 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004079 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004080 }
4081 if (children) {
4082 if (!(*children)) {
4083 /* first child */
4084 *children = node;
4085 } else if (*children != node) {
4086 /* by the condition in previous branch we cover the choice/case children
4087 * - the children list is shared by the choice and the the first case, in addition
4088 * the first child of each case must be referenced from the case node. So the node is
4089 * actually always already inserted in case it is the first children - so here such
4090 * a situation actually corresponds to the first branch */
Michal Vasko795b3752020-07-13 15:24:27 +02004091 if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module)
4092 && (strcmp((*children)->prev->module->name, node->module->name) > 0)) {
4093 /* some augments are already connected and we are connecting new ones,
4094 * keep module name order and insert the node into the children list */
4095 end = (*children);
4096 do {
4097 end = end->prev;
4098 mod = end->module;
4099 while (end->prev->module == mod) {
4100 end = end->prev;
4101 }
4102 } while ((end->prev->module != (*children)->module) && (end->prev->module != node->module)
4103 && (strcmp(mod->name, node->module->name) > 0));
4104
4105 /* we have the last existing node after our node, easily get the first before and connect it */
4106 start = end->prev;
4107 start->next = node;
4108 node->next = end;
4109 end->prev = node;
4110 node->prev = start;
4111 } else {
4112 /* insert at the end of the parent's children list */
4113 (*children)->prev->next = node;
4114 node->prev = (*children)->prev;
4115 (*children)->prev = node;
4116 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004117
4118 /* check the name uniqueness */
4119 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4120 lysc_node_notifs(parent), node->name, node)) {
4121 return LY_EEXIST;
4122 }
4123 }
4124 }
4125 return LY_SUCCESS;
4126}
4127
Radek Krejci95710c92019-02-11 15:49:55 +01004128/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004129 * @brief Prepare the case structure in choice node for the new data node.
4130 *
4131 * 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
4132 * created in the choice when the first child was processed.
4133 *
4134 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004135 * @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,
4136 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004137 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4138 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4139 * @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,
4140 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004141 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004142static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004143lys_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 +01004144{
4145 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004146 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004147 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004148 unsigned int u;
4149 LY_ERR ret;
4150
Radek Krejci95710c92019-02-11 15:49:55 +01004151#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004152 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004153 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4155 return NULL; \
4156 } \
4157 }
4158
Radek Krejci95710c92019-02-11 15:49:55 +01004159 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4160 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004161
4162 /* we have to add an implicit case node into the parent choice */
4163 cs = calloc(1, sizeof(struct lysc_node_case));
4164 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004165 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004166 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004167 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004168 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4169 /* the case is already present since the child is not its first children */
4170 return (struct lysc_node_case*)ch->cases->prev;
4171 }
Radek Krejci95710c92019-02-11 15:49:55 +01004172 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004173
4174 /* explicit parent case is not present (this is its first child) */
4175 cs = calloc(1, sizeof(struct lysc_node_case));
4176 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004177 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004178 cs->flags = LYS_STATUS_MASK & node_p->flags;
4179 cs->sp = node_p;
4180
Radek Krejcib1b59152019-01-07 13:21:56 +01004181 /* 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 +02004182 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004183
4184 if (node_p->when) {
4185 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004186 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004187 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004188
4189 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4190 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004191 ly_set_add(&ctx->xpath, cs, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004192 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004193 }
Radek Krejciec4da802019-05-02 13:02:41 +02004194 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004195 } else {
4196 LOGINT(ctx->ctx);
4197 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004198 }
4199 cs->module = ctx->mod;
4200 cs->prev = (struct lysc_node*)cs;
4201 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004202 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004203 cs->child = child;
4204
4205 return cs;
4206error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004207 if (cs) {
4208 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4209 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004210 return NULL;
4211
4212#undef UNIQUE_CHECK
4213}
4214
Radek Krejcib56c7502019-02-13 14:19:54 +01004215/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004216 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004217 *
4218 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004219 * @param[in] node Target node where the config is supposed to be changed.
4220 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004221 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4222 * 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 +01004223 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4224 * @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 +01004225 * @return LY_ERR value.
4226 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004227static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004228lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004229 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004230{
4231 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004232 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004233
4234 if (config == (node->flags & LYS_CONFIG_MASK)) {
4235 /* nothing to do */
4236 return LY_SUCCESS;
4237 }
4238
4239 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004240 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004241 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004243 "Invalid %s of config - configuration node cannot be child of any state data node.",
4244 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004245 return LY_EVALID;
4246 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004247 node->flags |= LYS_SET_CONFIG;
4248 } else {
4249 if (node->flags & LYS_SET_CONFIG) {
4250 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4251 /* setting config flags, but have node with explicit config true */
4252 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004253 "Invalid %s of config - configuration node cannot be child of any state data node.",
4254 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004255 return LY_EVALID;
4256 }
4257 /* do not change config on nodes where the config is explicitely set, this does not apply to
4258 * nodes, which are being changed explicitly (targets of refine or deviation) */
4259 return LY_SUCCESS;
4260 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004261 }
4262 node->flags &= ~LYS_CONFIG_MASK;
4263 node->flags |= config;
4264
4265 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004266 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004267 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004268 }
4269
Radek Krejci76b3e962018-12-14 17:01:25 +01004270 return LY_SUCCESS;
4271}
4272
Radek Krejcib56c7502019-02-13 14:19:54 +01004273/**
4274 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4275 *
4276 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4277 * the flag to such parents from a mandatory children.
4278 *
4279 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4280 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4281 * (mandatory children was removed).
4282 */
Radek Krejcife909632019-02-12 15:34:42 +01004283void
4284lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4285{
4286 struct lysc_node *iter;
4287
4288 if (add) { /* set flag */
4289 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4290 parent = parent->parent) {
4291 parent->flags |= LYS_MAND_TRUE;
4292 }
4293 } else { /* unset flag */
4294 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004295 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004296 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004297 /* there is another mandatory node */
4298 return;
4299 }
4300 }
4301 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4302 parent->flags &= ~LYS_MAND_TRUE;
4303 }
4304 }
4305}
4306
Radek Krejci056d0a82018-12-06 16:57:25 +01004307/**
Radek Krejci3641f562019-02-13 15:38:40 +01004308 * @brief Internal sorting process for the lys_compile_augment_sort().
4309 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4310 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4311 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4312 */
4313static void
4314lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4315{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004316 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004317 size_t len;
4318
4319 len = strlen(aug_p->nodeid);
4320 LY_ARRAY_FOR(result, v) {
4321 if (strlen(result[v]->nodeid) <= len) {
4322 continue;
4323 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004324 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004325 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004326 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004327 break;
4328 }
4329 }
4330 result[v] = aug_p;
4331 LY_ARRAY_INCREMENT(result);
4332}
4333
4334/**
4335 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4336 *
4337 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4338 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4339 *
4340 * @param[in] ctx Compile context.
4341 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4342 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4343 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4344 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4345 * @return LY_ERR value.
4346 */
4347LY_ERR
4348lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4349{
4350 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004351 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004352
4353 assert(augments);
4354
4355 /* get count of the augments in module and all its submodules */
4356 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004357 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004358 }
4359 LY_ARRAY_FOR(inc_p, u) {
4360 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004361 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004362 }
4363 }
4364
4365 if (!count) {
4366 *augments = NULL;
4367 return LY_SUCCESS;
4368 }
4369 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4370
4371 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4372 * together, so there can be even /z/y betwwen them. */
4373 LY_ARRAY_FOR(aug_p, u) {
4374 lys_compile_augment_sort_(&aug_p[u], result);
4375 }
4376 LY_ARRAY_FOR(inc_p, u) {
4377 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4378 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4379 }
4380 }
4381
4382 *augments = result;
4383 return LY_SUCCESS;
4384}
4385
4386/**
4387 * @brief Compile the parsed augment connecting it into its target.
4388 *
4389 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4390 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4391 * are already implemented and compiled.
4392 *
4393 * @param[in] ctx Compile context.
4394 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004395 * @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
4396 * children in case of the augmenting uses data.
4397 * @return LY_SUCCESS on success.
4398 * @return LY_EVALID on failure.
4399 */
4400LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004401lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004402{
Michal Vaskoe6143202020-07-03 13:02:08 +02004403 LY_ERR ret = LY_SUCCESS, rc;
Radek Krejci3641f562019-02-13 15:38:40 +01004404 struct lysp_node *node_p, *case_node_p;
4405 struct lysc_node *target; /* target target of the augment */
4406 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004407 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004408 struct lys_module **aug_mod;
Radek Krejci3641f562019-02-13 15:38:40 +01004409 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004410 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004411 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejciec4da802019-05-02 13:02:41 +02004412 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004413
Radek Krejci327de162019-06-14 12:52:07 +02004414 lysc_update_path(ctx, NULL, "{augment}");
4415 lysc_update_path(ctx, NULL, aug_p->nodeid);
4416
Radek Krejci7af64242019-02-18 13:07:53 +01004417 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004418 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004419 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004420 if (ret != LY_SUCCESS) {
4421 if (ret == LY_EDENIED) {
4422 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4423 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4424 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4425 }
4426 return LY_EVALID;
4427 }
4428
4429 /* check for mandatory nodes
4430 * - new cases augmenting some choice can have mandatory nodes
4431 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4432 */
Radek Krejci733988a2019-02-15 15:12:44 +01004433 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004434 allow_mandatory = 1;
4435 }
4436
4437 when_shared = NULL;
4438 LY_LIST_FOR(aug_p->child, node_p) {
4439 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4440 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004441 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004442 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4443 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004444 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004445 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4446 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004447 return LY_EVALID;
4448 }
4449
4450 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004451 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004452 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004453 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004454 } else {
4455 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004456 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004457 }
4458 }
Radek Krejciec4da802019-05-02 13:02:41 +02004459 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004460
Radek Krejcife13da42019-02-15 14:51:01 +01004461 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4462 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004463 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004464 /* 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 +01004465 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 +01004466 } else if (target->nodetype == LYS_CHOICE) {
4467 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4468 node = ((struct lysc_node_choice*)target)->cases->prev;
4469 } else {
4470 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004471 node = (struct lysc_node*)lysc_node_children(target, flags);
4472 if (!node) {
4473 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4474 break;
4475 }
4476 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004477 }
4478
Radek Krejci733988a2019-02-15 15:12:44 +01004479 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004480 node->flags &= ~LYS_MAND_TRUE;
4481 lys_compile_mandatory_parents(target, 0);
4482 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004483 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004484 return LY_EVALID;
4485 }
4486
4487 /* pass augment's when to all the children */
4488 if (aug_p->when) {
4489 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4490 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004491 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004492 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004493
4494 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4495 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004496 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004497 }
4498
Radek Krejci3641f562019-02-13 15:38:40 +01004499 when_shared = *when;
4500 } else {
4501 ++when_shared->refcount;
4502 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004503
4504 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4505 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004506 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004507 }
Radek Krejci3641f562019-02-13 15:38:40 +01004508 }
4509 }
4510 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004511
Radek Krejciec4da802019-05-02 13:02:41 +02004512 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004513 switch (target->nodetype) {
4514 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004515 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004516 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004517 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004518 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004519 break;
4520 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004521 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004522 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004523 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004524 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004525 break;
4526 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004527 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004528 if (aug_p->actions) {
4529 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004530 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4531 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004532 return LY_EVALID;
4533 }
4534 if (aug_p->notifs) {
4535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004536 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004537 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004538 return LY_EVALID;
4539 }
4540 }
Radek Krejci3641f562019-02-13 15:38:40 +01004541
Michal Vaskoe6143202020-07-03 13:02:08 +02004542 /* add this module into the target module augmented_by, if not there already */
4543 rc = LY_SUCCESS;
4544 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4545 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4546 rc = LY_EEXIST;
4547 break;
4548 }
4549 }
4550 if (!rc) {
4551 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4552 *aug_mod = ctx->mod;
4553 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004554
Radek Krejci327de162019-06-14 12:52:07 +02004555 lysc_update_path(ctx, NULL, NULL);
4556 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004557
Radek Krejci3641f562019-02-13 15:38:40 +01004558error:
Radek Krejciec4da802019-05-02 13:02:41 +02004559 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004560 return ret;
4561}
4562
4563/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004564 * @brief Apply refined or deviated mandatory flag to the target node.
4565 *
4566 * @param[in] ctx Compile context.
4567 * @param[in] node Target node where the mandatory property is supposed to be changed.
4568 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004569 * @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 +01004570 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4571 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4572 * 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 +01004573 * @return LY_ERR value.
4574 */
4575static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004576lys_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 +01004577{
4578 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4579 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004580 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4581 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004582 return LY_EVALID;
4583 }
4584
4585 if (mandatory_flag & LYS_MAND_TRUE) {
4586 /* check if node has default value */
4587 if (node->nodetype & LYS_LEAF) {
4588 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004589 if (refine_flag) {
4590 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004591 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004592 return LY_EVALID;
4593 }
Radek Krejcia1911222019-07-22 17:24:50 +02004594 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004595 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004596 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004597
4598 /* update the list of incomplete default values if needed */
4599 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4600
Radek Krejcia1911222019-07-22 17:24:50 +02004601 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4602 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4603 free(leaf->dflt);
4604 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004605 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004606 }
4607 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004608 if (refine_flag) {
4609 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004610 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004611 return LY_EVALID;
4612 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004613 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004614 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004615 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 +01004616 return LY_EVALID;
4617 }
4618
4619 node->flags &= ~LYS_MAND_FALSE;
4620 node->flags |= LYS_MAND_TRUE;
4621 lys_compile_mandatory_parents(node->parent, 1);
4622 } else {
4623 /* make mandatory false */
4624 node->flags &= ~LYS_MAND_TRUE;
4625 node->flags |= LYS_MAND_FALSE;
4626 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004627 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 +01004628 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004629 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004630 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004631 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4632 leaf->dflt->realtype = leaf->type->dflt->realtype;
4633 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4634 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004635 }
4636 }
4637 return LY_SUCCESS;
4638}
4639
4640/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004641 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4642 * If present, also apply uses's modificators.
4643 *
4644 * @param[in] ctx Compile context
4645 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004646 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4647 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4648 * the compile context.
4649 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4650 */
4651static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004652lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004653{
4654 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004655 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004656 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004657 struct lysc_node_container context_node_fake =
4658 {.nodetype = LYS_CONTAINER,
4659 .module = ctx->mod,
4660 .flags = parent ? parent->flags : 0,
4661 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004662 .prev = (struct lysc_node*)&context_node_fake,
4663 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004664 struct lysp_grp *grp = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004665 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004666 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004667 int found;
4668 const char *id, *name, *prefix;
4669 size_t prefix_len, name_len;
4670 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004671 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004672 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004673 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004674 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004675 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004676 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004677 struct lysp_augment **augments = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004678 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004679 struct lysc_notif **notifs = NULL;
4680 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004681
4682 /* search for the grouping definition */
4683 found = 0;
4684 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004685 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004686 if (prefix) {
4687 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4688 if (!mod) {
4689 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004690 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004691 return LY_EVALID;
4692 }
4693 } else {
4694 mod = ctx->mod_def;
4695 }
4696 if (mod == ctx->mod_def) {
4697 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004698 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004699 LY_ARRAY_FOR(grp, u) {
4700 if (!strcmp(grp[u].name, name)) {
4701 grp = &grp[u];
4702 found = 1;
4703 break;
4704 }
4705 }
4706 }
4707 }
4708 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004709 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004710 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004711 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004712 for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004713 if (!strcmp(grp[u].name, name)) {
4714 grp = &grp[u];
4715 found = 1;
4716 }
4717 }
4718 }
4719 if (!found && mod->parsed->includes) {
4720 /* ... and all the submodules */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004721 for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004722 grp = mod->parsed->includes[u].submodule->groupings;
4723 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004724 for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004725 if (!strcmp(grp[v].name, name)) {
4726 grp = &grp[v];
4727 found = 1;
4728 }
4729 }
4730 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004731 }
4732 }
4733 }
4734 if (!found) {
4735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4736 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4737 return LY_EVALID;
4738 }
4739
4740 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4741 grp_stack_count = ctx->groupings.count;
4742 ly_set_add(&ctx->groupings, (void*)grp, 0);
4743 if (grp_stack_count == ctx->groupings.count) {
4744 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4745 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4746 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4747 return LY_EVALID;
4748 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004749 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4750 /* remember that the grouping is instantiated to avoid its standalone validation */
4751 grp->flags |= LYS_USED_GRP;
4752 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004753
4754 /* switch context's mod_def */
4755 mod_old = ctx->mod_def;
4756 ctx->mod_def = mod;
4757
4758 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004759 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 +01004760
Radek Krejcifc11bd72019-04-11 16:00:05 +02004761 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004762 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004763 /* 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 +02004764 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 +01004765
4766 /* some preparation for applying refines */
4767 if (grp->data == node_p) {
4768 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004769 if (parent) {
4770 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4771 } else if (ctx->mod->compiled->data) {
4772 child = ctx->mod->compiled->data;
4773 } else {
4774 child = NULL;
4775 }
4776 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004777 }
4778 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004779 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004780 LY_LIST_FOR(context_node_fake.child, child) {
4781 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004782
Radek Krejcifc11bd72019-04-11 16:00:05 +02004783 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004784 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004785 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004786 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004787 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4788
4789 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4790 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004791 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004792 }
4793
Radek Krejci00b874b2019-02-12 10:54:50 +01004794 when_shared = *when;
4795 } else {
4796 ++when_shared->refcount;
4797 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004798
4799 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4800 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004801 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004802 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004803 }
4804 }
Radek Krejci01342af2019-01-03 15:18:08 +01004805 }
4806 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004807 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004808 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004809 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004810 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 +01004811 }
4812
Radek Krejcifc11bd72019-04-11 16:00:05 +02004813 /* compile actions */
4814 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4815 if (actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004816 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004817 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004818 if (*actions && (uses_p->augments || uses_p->refines)) {
4819 /* 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 +02004820 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
4821 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
4822 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004823 }
4824 }
4825
4826 /* compile notifications */
4827 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4828 if (notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004829 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004830 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004831 if (*notifs && (uses_p->augments || uses_p->refines)) {
4832 /* 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 +02004833 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
4834 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
4835 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004836 }
4837 }
4838
4839
Radek Krejci3641f562019-02-13 15:38:40 +01004840 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004841 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004842 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004843 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004844 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004845
Radek Krejcif0089082019-01-07 16:42:01 +01004846 /* reload previous context's mod_def */
4847 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004848 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004849
Radek Krejci76b3e962018-12-14 17:01:25 +01004850 /* apply refine */
4851 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004852 lysc_update_path(ctx, NULL, rfn->nodeid);
4853
Radek Krejci7af64242019-02-18 13:07:53 +01004854 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 +01004855 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004856 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004857 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004858
4859 /* default value */
4860 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004861 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004862 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004863 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.",
4864 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004865 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004866 }
4867 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4868 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004869 "Invalid refine of default - %s cannot hold default value(s).",
4870 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004871 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004872 }
4873 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004874 struct ly_err_item *err = NULL;
4875 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4876 if (leaf->dflt) {
4877 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004878 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004879 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4880 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4881 } else {
4882 /* prepare a new one */
4883 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4884 leaf->dflt->realtype = leaf->type;
4885 }
4886 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004887 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004888 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4889 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004890 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004891 leaf->dflt->realtype->refcount++;
4892 if (err) {
4893 ly_err_print(err);
4894 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4895 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4896 ly_err_free(err);
4897 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004898 if (rc == LY_EINCOMPLETE) {
4899 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004900 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004901
4902 /* but in general result is so far ok */
4903 rc = LY_SUCCESS;
4904 }
Radek Krejcia1911222019-07-22 17:24:50 +02004905 LY_CHECK_GOTO(rc, cleanup);
4906 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004907 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004908 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4909
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004910 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4912 "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 +02004913 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004914 }
Radek Krejcia1911222019-07-22 17:24:50 +02004915
4916 /* remove previous set of default values */
4917 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004918 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004919 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4920 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4921 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004922 }
Radek Krejcia1911222019-07-22 17:24:50 +02004923 LY_ARRAY_FREE(llist->dflts);
4924 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004925 LY_ARRAY_FREE(llist->dflts_mods);
4926 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004927
4928 /* create the new set of the default values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004929 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
4930 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004931 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004932 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004933 LY_ARRAY_INCREMENT(llist->dflts_mods);
4934 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004935 LY_ARRAY_INCREMENT(llist->dflts);
4936 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4937 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004938 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004939 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004940 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004941 llist->dflts[u]->realtype->refcount++;
4942 if (err) {
4943 ly_err_print(err);
4944 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4945 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4946 ly_err_free(err);
4947 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004948 if (rc == LY_EINCOMPLETE) {
4949 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004950 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004951
4952 /* but in general result is so far ok */
4953 rc = LY_SUCCESS;
4954 }
4955 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004956 }
Radek Krejcia1911222019-07-22 17:24:50 +02004957 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004958 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004959 if (((struct lysc_node_choice*)node)->dflt) {
4960 /* unset LYS_SET_DFLT from the current default case */
4961 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4962 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004963 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 +01004964 }
4965 }
4966
Radek Krejci12fb9142019-01-08 09:45:30 +01004967 /* description */
4968 if (rfn->dsc) {
4969 FREE_STRING(ctx->ctx, node->dsc);
4970 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4971 }
4972
4973 /* reference */
4974 if (rfn->ref) {
4975 FREE_STRING(ctx->ctx, node->ref);
4976 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4977 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004978
4979 /* config */
4980 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004981 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004982 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004983 } else {
4984 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004985 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004986 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004987 }
4988
4989 /* mandatory */
4990 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004991 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004992 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004993
4994 /* presence */
4995 if (rfn->presence) {
4996 if (node->nodetype != LYS_CONTAINER) {
4997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004998 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4999 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005000 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005001 }
5002 node->flags |= LYS_PRESENCE;
5003 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005004
5005 /* must */
5006 if (rfn->musts) {
5007 switch (node->nodetype) {
5008 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005009 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 +01005010 break;
5011 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005012 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 +01005013 break;
5014 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005015 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 +01005016 break;
5017 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005018 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 +01005019 break;
5020 case LYS_ANYXML:
5021 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005022 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 +01005023 break;
5024 default:
5025 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005026 "Invalid refine of must statement - %s cannot hold any must statement.",
5027 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005028 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005029 }
Michal Vasko004d3152020-06-11 19:59:22 +02005030 ly_set_add(&ctx->xpath, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01005031 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005032
5033 /* min/max-elements */
5034 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5035 switch (node->nodetype) {
5036 case LYS_LEAFLIST:
5037 if (rfn->flags & LYS_SET_MAX) {
5038 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5039 }
5040 if (rfn->flags & LYS_SET_MIN) {
5041 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005042 if (rfn->min) {
5043 node->flags |= LYS_MAND_TRUE;
5044 lys_compile_mandatory_parents(node->parent, 1);
5045 } else {
5046 node->flags &= ~LYS_MAND_TRUE;
5047 lys_compile_mandatory_parents(node->parent, 0);
5048 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005049 }
5050 break;
5051 case LYS_LIST:
5052 if (rfn->flags & LYS_SET_MAX) {
5053 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5054 }
5055 if (rfn->flags & LYS_SET_MIN) {
5056 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005057 if (rfn->min) {
5058 node->flags |= LYS_MAND_TRUE;
5059 lys_compile_mandatory_parents(node->parent, 1);
5060 } else {
5061 node->flags &= ~LYS_MAND_TRUE;
5062 lys_compile_mandatory_parents(node->parent, 0);
5063 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005064 }
5065 break;
5066 default:
5067 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005068 "Invalid refine of %s statement - %s cannot hold this statement.",
5069 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005070 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005071 }
5072 }
Radek Krejcif0089082019-01-07 16:42:01 +01005073
5074 /* if-feature */
5075 if (rfn->iffeatures) {
5076 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005077 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005078 }
Radek Krejci327de162019-06-14 12:52:07 +02005079
5080 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005081 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005082
Radek Krejcif2271f12019-01-07 16:42:23 +01005083 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005084 for (uint32_t i = 0; i < refined.count; ++i) {
5085 node = (struct lysc_node*)refined.objs[i];
5086 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02005087 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005088
5089 /* check possible conflict with default value (default added, mandatory left true) */
5090 if ((node->flags & LYS_MAND_TRUE) &&
5091 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5092 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5093 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005094 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005095 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005096 }
5097
5098 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5099 if (node->nodetype == LYS_LIST) {
5100 min = ((struct lysc_node_list*)node)->min;
5101 max = ((struct lysc_node_list*)node)->max;
5102 } else {
5103 min = ((struct lysc_node_leaflist*)node)->min;
5104 max = ((struct lysc_node_leaflist*)node)->max;
5105 }
5106 if (min > max) {
5107 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005108 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5109 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005110 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005111 }
5112 }
5113 }
5114
Radek Krejci327de162019-06-14 12:52:07 +02005115 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005116 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005117
5118cleanup:
5119 /* fix connection of the children nodes from fake context node back into the parent */
5120 if (context_node_fake.child) {
5121 context_node_fake.child->prev = child;
5122 }
5123 LY_LIST_FOR(context_node_fake.child, child) {
5124 child->parent = parent;
5125 }
5126
5127 if (uses_p->augments || uses_p->refines) {
5128 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005129 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005130 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005131 LY_ARRAY_FREE(context_node_fake.actions);
5132 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005133 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005134 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005135 LY_ARRAY_FREE(context_node_fake.notifs);
5136 }
5137 }
5138
Radek Krejcie86bf772018-12-14 11:39:53 +01005139 /* reload previous context's mod_def */
5140 ctx->mod_def = mod_old;
5141 /* remove the grouping from the stack for circular groupings dependency check */
5142 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5143 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005144 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005145 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005146
5147 return ret;
5148}
5149
Radek Krejci327de162019-06-14 12:52:07 +02005150static int
5151lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5152{
5153 struct lysp_node *iter;
5154 int len = 0;
5155
5156 *path = NULL;
5157 for (iter = node; iter && len >= 0; iter = iter->parent) {
5158 char *s = *path;
5159 char *id;
5160
5161 switch (iter->nodetype) {
5162 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005163 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005164 break;
5165 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005166 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005167 break;
5168 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005169 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005170 break;
5171 default:
5172 id = strdup(iter->name);
5173 break;
5174 }
5175
5176 if (!iter->parent) {
5177 /* print prefix */
5178 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5179 } else {
5180 /* prefix is the same as in parent */
5181 len = asprintf(path, "/%s%s", id, s ? s : "");
5182 }
5183 free(s);
5184 free(id);
5185 }
5186
5187 if (len < 0) {
5188 free(*path);
5189 *path = NULL;
5190 } else if (len == 0) {
5191 *path = strdup("/");
5192 len = 1;
5193 }
5194 return len;
5195}
5196
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005197/**
5198 * @brief Validate groupings that were defined but not directly used in the schema itself.
5199 *
5200 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5201 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5202 */
5203static LY_ERR
5204lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5205{
5206 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005207 char *path;
5208 int len;
5209
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005210 struct lysp_node_uses fake_uses = {
5211 .parent = node_p,
5212 .nodetype = LYS_USES,
5213 .flags = 0, .next = NULL,
5214 .name = grp->name,
5215 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5216 .refines = NULL, .augments = NULL
5217 };
5218 struct lysc_node_container fake_container = {
5219 .nodetype = LYS_CONTAINER,
5220 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5221 .module = ctx->mod,
5222 .sp = NULL, .parent = NULL, .next = NULL,
5223 .prev = (struct lysc_node*)&fake_container,
5224 .name = "fake",
5225 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5226 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5227 };
5228
5229 if (grp->parent) {
5230 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5231 }
Radek Krejci327de162019-06-14 12:52:07 +02005232
5233 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5234 if (len < 0) {
5235 LOGMEM(ctx->ctx);
5236 return LY_EMEM;
5237 }
5238 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5239 ctx->path_len = (uint16_t)len;
5240 free(path);
5241
5242 lysc_update_path(ctx, NULL, "{grouping}");
5243 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005244 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005245 lysc_update_path(ctx, NULL, NULL);
5246 lysc_update_path(ctx, NULL, NULL);
5247
5248 ctx->path_len = 1;
5249 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005250
5251 /* cleanup */
5252 lysc_node_container_free(ctx->ctx, &fake_container);
5253
5254 return ret;
5255}
Radek Krejcife909632019-02-12 15:34:42 +01005256
Radek Krejcie86bf772018-12-14 11:39:53 +01005257/**
Radek Krejcia3045382018-11-22 14:30:31 +01005258 * @brief Compile parsed schema node information.
5259 * @param[in] ctx Compile context
5260 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005261 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5262 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5263 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005264 * @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).
5265 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005266 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5267 */
Radek Krejci19a96102018-11-15 13:38:09 +01005268static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005269lys_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 +01005270{
Radek Krejci1c54f462020-05-12 17:25:34 +02005271 LY_ERR ret = LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01005272 struct lysc_node *node;
5273 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005274 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005275 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005276 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005277
Radek Krejci327de162019-06-14 12:52:07 +02005278 if (node_p->nodetype != LYS_USES) {
5279 lysc_update_path(ctx, parent, node_p->name);
5280 } else {
5281 lysc_update_path(ctx, NULL, "{uses}");
5282 lysc_update_path(ctx, NULL, node_p->name);
5283 }
5284
Radek Krejci19a96102018-11-15 13:38:09 +01005285 switch (node_p->nodetype) {
5286 case LYS_CONTAINER:
5287 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5288 node_compile_spec = lys_compile_node_container;
5289 break;
5290 case LYS_LEAF:
5291 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5292 node_compile_spec = lys_compile_node_leaf;
5293 break;
5294 case LYS_LIST:
5295 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005296 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005297 break;
5298 case LYS_LEAFLIST:
5299 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005300 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005301 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005302 case LYS_CHOICE:
5303 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005304 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005305 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005306 case LYS_ANYXML:
5307 case LYS_ANYDATA:
5308 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005309 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005310 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005311 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005312 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5313 lysc_update_path(ctx, NULL, NULL);
5314 lysc_update_path(ctx, NULL, NULL);
5315 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005316 default:
5317 LOGINT(ctx->ctx);
5318 return LY_EINT;
5319 }
5320 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5321 node->nodetype = node_p->nodetype;
5322 node->module = ctx->mod;
5323 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005324 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005325
5326 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005327 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005328 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005329 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005330 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5331 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005332 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005333 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005334 node->flags |= LYS_CONFIG_R;
5335 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005336 /* config not explicitely set, inherit it from parent */
5337 if (parent) {
5338 node->flags |= parent->flags & LYS_CONFIG_MASK;
5339 } else {
5340 /* default is config true */
5341 node->flags |= LYS_CONFIG_W;
5342 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005343 } else {
5344 /* config set explicitely */
5345 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005346 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005347 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5348 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5349 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005350 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005351 goto error;
5352 }
Radek Krejci19a96102018-11-15 13:38:09 +01005353
Radek Krejcia6d57732018-11-29 13:40:37 +01005354 /* *list ordering */
5355 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5356 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005357 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005358 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5359 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005360 node->flags &= ~LYS_ORDBY_MASK;
5361 node->flags |= LYS_ORDBY_SYSTEM;
5362 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5363 /* default ordering is system */
5364 node->flags |= LYS_ORDBY_SYSTEM;
5365 }
5366 }
5367
Radek Krejci19a96102018-11-15 13:38:09 +01005368 /* status - it is not inherited by specification, but it does not make sense to have
5369 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005370 if (!parent || parent->nodetype != LYS_CHOICE) {
5371 /* in case of choice/case's children, postpone the check to the moment we know if
5372 * 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 +02005373 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 +01005374 }
5375
Radek Krejciec4da802019-05-02 13:02:41 +02005376 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005377 node->sp = node_p;
5378 }
5379 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005380 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5381 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005382 if (node_p->when) {
5383 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005384 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005385
5386 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5387 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02005388 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01005389 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005390 }
Radek Krejciec4da802019-05-02 13:02:41 +02005391 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005392
5393 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005394 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005395
Radek Krejci0935f412019-08-20 16:15:18 +02005396 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5397
Radek Krejcife909632019-02-12 15:34:42 +01005398 /* inherit LYS_MAND_TRUE in parent containers */
5399 if (node->flags & LYS_MAND_TRUE) {
5400 lys_compile_mandatory_parents(parent, 1);
5401 }
5402
Radek Krejci327de162019-06-14 12:52:07 +02005403 lysc_update_path(ctx, NULL, NULL);
5404
Radek Krejci19a96102018-11-15 13:38:09 +01005405 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005406 if (parent) {
5407 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005408 if (node_p->parent->nodetype == LYS_CASE) {
5409 lysc_update_path(ctx, parent, node_p->parent->name);
5410 } else {
5411 lysc_update_path(ctx, parent, node->name);
5412 }
Radek Krejciec4da802019-05-02 13:02:41 +02005413 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005414 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005415 if (uses_status) {
5416
5417 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005418 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005419 * it directly gets the same status flags as the choice;
5420 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005421 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005422 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005423 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005424 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005425 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005426 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005427 }
Radek Krejciec4da802019-05-02 13:02:41 +02005428 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 +02005429
5430 if (parent->nodetype == LYS_CHOICE) {
5431 lysc_update_path(ctx, NULL, NULL);
5432 }
Radek Krejci19a96102018-11-15 13:38:09 +01005433 } else {
5434 /* top-level element */
5435 if (!ctx->mod->compiled->data) {
5436 ctx->mod->compiled->data = node;
5437 } else {
5438 /* insert at the end of the module's top-level nodes list */
5439 ctx->mod->compiled->data->prev->next = node;
5440 node->prev = ctx->mod->compiled->data->prev;
5441 ctx->mod->compiled->data->prev = node;
5442 }
Radek Krejci327de162019-06-14 12:52:07 +02005443 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005444 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5445 ctx->mod->compiled->notifs, node->name, node)) {
5446 return LY_EVALID;
5447 }
Radek Krejci19a96102018-11-15 13:38:09 +01005448 }
Radek Krejci327de162019-06-14 12:52:07 +02005449 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005450
5451 return LY_SUCCESS;
5452
5453error:
5454 lysc_node_free(ctx->ctx, node);
5455 return ret;
5456}
5457
Radek Krejciccd20f12019-02-15 14:12:27 +01005458static void
5459lysc_disconnect(struct lysc_node *node)
5460{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005461 struct lysc_node *parent, *child, *prev = NULL, *next;
5462 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005463 int remove_cs = 0;
5464
5465 parent = node->parent;
5466
5467 /* parent's first child */
5468 if (!parent) {
5469 return;
5470 }
5471 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005472 cs = (struct lysc_node_case*)node;
5473 } else if (parent->nodetype == LYS_CASE) {
5474 /* disconnecting some node in a case */
5475 cs = (struct lysc_node_case*)parent;
5476 parent = cs->parent;
5477 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5478 if (child == node) {
5479 if (cs->child == child) {
5480 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5481 /* case with a single child -> remove also the case */
5482 child->parent = NULL;
5483 remove_cs = 1;
5484 } else {
5485 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005486 }
5487 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005488 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005489 }
5490 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005491 if (!remove_cs) {
5492 cs = NULL;
5493 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005494 } else if (lysc_node_children(parent, node->flags) == node) {
5495 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005496 }
5497
5498 if (cs) {
5499 if (remove_cs) {
5500 /* cs has only one child which is being also removed */
5501 lysc_disconnect((struct lysc_node*)cs);
5502 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5503 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005504 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5505 /* default case removed */
5506 ((struct lysc_node_choice*)parent)->dflt = NULL;
5507 }
5508 if (((struct lysc_node_choice*)parent)->cases == cs) {
5509 /* first case removed */
5510 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5511 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005512 if (cs->child) {
5513 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5514 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5515 prev = cs->child->prev;
5516 } /* else all the children are under a single case */
5517 LY_LIST_FOR_SAFE(cs->child, next, child) {
5518 if (child->parent != (struct lysc_node*)cs) {
5519 break;
5520 }
5521 lysc_node_free(node->module->ctx, child);
5522 }
5523 if (prev) {
5524 if (prev->next) {
5525 prev->next = child;
5526 }
5527 if (child) {
5528 child->prev = prev;
5529 } else {
5530 /* link from the first child under the cases */
5531 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5532 }
5533 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005534 }
5535 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005536 }
5537
5538 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005539 if (node->prev->next) {
5540 node->prev->next = node->next;
5541 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005542 if (node->next) {
5543 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005544 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005545 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005546 if (child) {
5547 child->prev = node->prev;
5548 }
5549 } else if (((struct lysc_node_choice*)parent)->cases) {
5550 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005551 }
5552}
5553
5554LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005555lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005556{
Radek Krejcia1911222019-07-22 17:24:50 +02005557 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005558 struct ly_set devs_p = {0};
5559 struct ly_set targets = {0};
5560 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005561 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005562 struct lysc_action *rpcs;
5563 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005564 struct lysp_deviation *dev;
5565 struct lysp_deviate *d, **dp_new;
5566 struct lysp_deviate_add *d_add;
5567 struct lysp_deviate_del *d_del;
5568 struct lysp_deviate_rpl *d_rpl;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005569 LY_ARRAY_COUNT_TYPE u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005570 struct lysc_deviation {
5571 const char *nodeid;
5572 struct lysc_node *target; /* target node of the deviation */
5573 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005574 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005575 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5576 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005577 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005578 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005579 const char *prefix, *name, *nodeid, *dflt;
Michal Vasko57c10cd2020-05-27 15:57:11 +02005580 struct lys_module *mod, **dev_mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005581 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005582 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005583
5584 /* get all deviations from the module and all its submodules ... */
5585 LY_ARRAY_FOR(mod_p->deviations, u) {
5586 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5587 }
5588 LY_ARRAY_FOR(mod_p->includes, v) {
5589 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5590 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5591 }
5592 }
5593 if (!devs_p.count) {
5594 /* nothing to do */
5595 return LY_SUCCESS;
5596 }
5597
Radek Krejci327de162019-06-14 12:52:07 +02005598 lysc_update_path(ctx, NULL, "{deviation}");
5599
Radek Krejciccd20f12019-02-15 14:12:27 +01005600 /* ... and group them by the target node */
5601 devs = calloc(devs_p.count, sizeof *devs);
5602 for (u = 0; u < devs_p.count; ++u) {
5603 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005604 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005605
5606 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005607 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5608 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01005609 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005610 /* move the target pointer to input/output to make them different from the action and
5611 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5612 * back to the RPC/action node due to a better compatibility and decision code in this function.
5613 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5614 if (flags & LYSC_OPT_RPC_INPUT) {
5615 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5616 flags |= LYSC_OPT_INTERNAL;
5617 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5618 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5619 flags |= LYSC_OPT_INTERNAL;
5620 }
5621 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005622 /* insert into the set of targets with duplicity detection */
5623 i = ly_set_add(&targets, target, 0);
5624 if (!devs[i]) {
5625 /* new record */
5626 devs[i] = calloc(1, sizeof **devs);
5627 devs[i]->target = target;
5628 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005629 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005630 }
5631 /* add deviates into the deviation's list of deviates */
5632 for (d = dev->deviates; d; d = d->next) {
5633 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5634 *dp_new = d;
5635 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5636 devs[i]->not_supported = 1;
5637 }
5638 }
Radek Krejci327de162019-06-14 12:52:07 +02005639
5640 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005641 }
5642
5643 /* MACROS for deviates checking */
5644#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5645 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005646 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 +01005647 goto cleanup; \
5648 }
5649
5650#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005651 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5652 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5653 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005654 goto cleanup; \
5655 }
5656
Radek Krejcia1911222019-07-22 17:24:50 +02005657
Radek Krejciccd20f12019-02-15 14:12:27 +01005658#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005659 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5661 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5662 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5663 goto cleanup; \
5664 }
5665
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005666#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005667 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005668 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005669 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5670 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005672 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5673 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005674 goto cleanup; \
5675 }
5676
Radek Krejci551b12c2019-02-19 16:11:21 +01005677#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5678 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005680 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5681 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005682 goto cleanup; \
5683 }
5684
Radek Krejciccd20f12019-02-15 14:12:27 +01005685#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5686 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005688 goto cleanup; \
5689 }
5690
Radek Krejci551b12c2019-02-19 16:11:21 +01005691#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5692 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005694 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005695 goto cleanup; \
5696 }
5697
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005698#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5699 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5700 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5701 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5702 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 +01005703 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005704 if (y == LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005705 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005706 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5707 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005708 goto cleanup; \
5709 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005710 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5711 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5712 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5713 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005714 (LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005715 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005716 if (!LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005717 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5718 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005719 }
5720
5721 /* apply deviations */
5722 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005723 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5724 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5725 struct ly_err_item *err = NULL;
5726
5727 dflt = NULL;
5728 changed_type = 0;
5729
Radek Krejci327de162019-06-14 12:52:07 +02005730 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5731
Radek Krejcif538ce52019-03-05 10:46:14 +01005732 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5733 /* fix the target pointer in case of RPC's/action's input/output */
5734 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5735 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5736 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5737 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5738 }
5739 }
5740
Radek Krejciccd20f12019-02-15 14:12:27 +01005741 /* not-supported */
5742 if (devs[u]->not_supported) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005743 if (LY_ARRAY_COUNT(devs[u]->deviates) > 1) {
5744 LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.",
5745 LY_ARRAY_COUNT(devs[u]->deviates), devs[u]->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005746 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005747
5748#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5749 if (devs[u]->target->parent) { \
5750 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5751 } else { \
5752 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5753 } \
5754 LY_ARRAY_FOR(ARRAY, x) { \
5755 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5756 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005757 if (x < LY_ARRAY_COUNT(ARRAY)) { \
Radek Krejcifc11bd72019-04-11 16:00:05 +02005758 FREEFUNC(ctx->ctx, &ARRAY[x]); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005759 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
Radek Krejcifc11bd72019-04-11 16:00:05 +02005760 LY_ARRAY_DECREMENT(ARRAY); \
5761 }
5762
Michal Vasko1bf09392020-03-27 12:38:10 +01005763 if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005764 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5765 /* remove RPC's/action's input */
5766 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5767 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005768 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5769 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005770 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5771 /* remove RPC's/action's output */
5772 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5773 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005774 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5775 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005776 } else {
5777 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005778 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005779 }
5780 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005781 /* remove Notification */
5782 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005783 } else {
5784 /* remove the target node */
5785 lysc_disconnect(devs[u]->target);
5786 lysc_node_free(ctx->ctx, devs[u]->target);
5787 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005788
Radek Krejci474f9b82019-07-24 11:36:37 +02005789 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5790 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005791 continue;
5792 }
5793
5794 /* list of deviates (not-supported is not present in the list) */
5795 LY_ARRAY_FOR(devs[u]->deviates, v) {
5796 d = devs[u]->deviates[v];
5797
5798 switch (d->mod) {
5799 case LYS_DEV_ADD:
5800 d_add = (struct lysp_deviate_add*)d;
5801 /* [units-stmt] */
5802 if (d_add->units) {
5803 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5804 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5805
5806 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5807 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5808 }
5809
5810 /* *must-stmt */
5811 if (d_add->musts) {
5812 switch (devs[u]->target->nodetype) {
5813 case LYS_CONTAINER:
5814 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005815 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5816 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005817 break;
5818 case LYS_LEAF:
5819 case LYS_LEAFLIST:
5820 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005821 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5822 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005823 break;
5824 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005825 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5826 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005827 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005828 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005829 case LYS_ACTION:
5830 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005831 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5832 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005833 break;
Michal Vaskocc048b22020-03-27 15:52:38 +01005834 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005835 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5836 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005837 break;
5838 }
5839 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005840 default:
5841 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005842 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005843 goto cleanup;
5844 }
Michal Vasko004d3152020-06-11 19:59:22 +02005845 ly_set_add(&ctx->xpath, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01005846 }
5847
5848 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005849 if (d_add->uniques) {
5850 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5851 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5852 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005853
5854 /* *default-stmt */
5855 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005856 switch (devs[u]->target->nodetype) {
5857 case LYS_LEAF:
5858 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005859 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 +02005860 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005861 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005862 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005863 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5864 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5865 } else {
5866 /* prepare new default value storage */
5867 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005868 }
Radek Krejcia1911222019-07-22 17:24:50 +02005869 dflt = d_add->dflts[0];
5870 /* parsing is done at the end after possible replace of the leaf's type */
5871
Radek Krejci551b12c2019-02-19 16:11:21 +01005872 /* mark the new default values as leaf's own */
5873 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005874 break;
5875 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005876 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005877 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005878 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005879 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005880 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5881 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5882 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005883 }
Radek Krejcia1911222019-07-22 17:24:50 +02005884 LY_ARRAY_FREE(llist->dflts);
5885 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005886 LY_ARRAY_FREE(llist->dflts_mods);
5887 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005888 }
5889 /* add new default value(s) */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005890 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup);
5891 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup);
5892 for (x = y = LY_ARRAY_COUNT(llist->dflts);
5893 x < LY_ARRAY_COUNT(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005894 LY_ARRAY_INCREMENT(llist->dflts_mods);
5895 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005896 LY_ARRAY_INCREMENT(llist->dflts);
5897 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5898 llist->dflts[x]->realtype = llist->type;
5899 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 +02005900 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005901 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005902 llist->dflts[x]->realtype->refcount++;
5903 if (err) {
5904 ly_err_print(err);
5905 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5906 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5907 d_add->dflts[x - y], err->msg);
5908 ly_err_free(err);
5909 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005910 if (rc == LY_EINCOMPLETE) {
5911 /* postpone default compilation when the tree is complete */
5912 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5913
5914 /* but in general result is so far ok */
5915 rc = LY_SUCCESS;
5916 }
Radek Krejcia1911222019-07-22 17:24:50 +02005917 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005918 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005919 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005920 devs[u]->target->flags |= LYS_SET_DFLT;
5921 break;
5922 case LYS_CHOICE:
5923 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5924 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5925 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5926 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005927 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 +01005928 goto cleanup;
5929 }
5930 break;
5931 default:
5932 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005933 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005934 goto cleanup;
5935 }
5936 }
5937
5938 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005939 if (d_add->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01005940 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005941 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005942 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5943 goto cleanup;
5944 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005945 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005946 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
Michal Vaskoa3881362020-01-21 15:57:35 +01005947 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005948 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005949 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5950 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005951 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5952 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005953 goto cleanup;
5954 }
Radek Krejci327de162019-06-14 12:52:07 +02005955 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005956 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005957
5958 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005959 if (d_add->flags & LYS_MAND_MASK) {
5960 if (devs[u]->target->flags & LYS_MAND_MASK) {
5961 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005962 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5963 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005964 goto cleanup;
5965 }
Radek Krejci327de162019-06-14 12:52:07 +02005966 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005967 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005968
5969 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005970 if (d_add->flags & LYS_SET_MIN) {
5971 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5972 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5973 /* change value */
5974 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5975 } else if (devs[u]->target->nodetype == LYS_LIST) {
5976 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5977 /* change value */
5978 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5979 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005980 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005981 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5982 goto cleanup;
5983 }
5984 if (d_add->min) {
5985 devs[u]->target->flags |= LYS_MAND_TRUE;
5986 }
5987 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005988
5989 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005990 if (d_add->flags & LYS_SET_MAX) {
5991 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5992 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5993 /* change value */
5994 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5995 } else if (devs[u]->target->nodetype == LYS_LIST) {
5996 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5997 /* change value */
5998 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5999 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006001 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6002 goto cleanup;
6003 }
6004 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006005
6006 break;
6007 case LYS_DEV_DELETE:
6008 d_del = (struct lysp_deviate_del*)d;
6009
6010 /* [units-stmt] */
6011 if (d_del->units) {
6012 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006013 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6014 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6015 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6016 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6017 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6018 goto cleanup;
6019 }
6020 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6021 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006022 }
6023
6024 /* *must-stmt */
6025 if (d_del->musts) {
6026 switch (devs[u]->target->nodetype) {
6027 case LYS_CONTAINER:
6028 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006029 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006030 break;
6031 case LYS_LEAF:
6032 case LYS_LEAFLIST:
6033 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006034 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006035 break;
6036 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006037 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006038 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006039 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006040 case LYS_ACTION:
6041 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6042 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6043 break;
6044 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6045 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6046 break;
6047 }
6048 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006049 default:
6050 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006051 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006052 goto cleanup;
6053 }
6054 }
6055
6056 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006057 if (d_del->uniques) {
6058 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6059 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6060 LY_ARRAY_FOR(d_del->uniques, x) {
6061 LY_ARRAY_FOR(list->uniques, z) {
6062 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6063 nodeid = strpbrk(name, " \t\n");
6064 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02006065 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006066 break;
6067 }
6068 while (isspace(*nodeid)) {
6069 ++nodeid;
6070 }
6071 } else {
6072 if (strcmp(name, list->uniques[z][y]->name)) {
6073 break;
6074 }
6075 }
6076 }
6077 if (!name) {
6078 /* complete match - remove the unique */
6079 LY_ARRAY_DECREMENT(list->uniques);
6080 LY_ARRAY_FREE(list->uniques[z]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006081 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
Radek Krejci7af64242019-02-18 13:07:53 +01006082 --z;
6083 break;
6084 }
6085 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006086 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006087 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006088 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6089 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006090 goto cleanup;
6091 }
6092 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006093 if (!LY_ARRAY_COUNT(list->uniques)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006094 LY_ARRAY_FREE(list->uniques);
6095 list->uniques = NULL;
6096 }
6097 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006098
6099 /* *default-stmt */
6100 if (d_del->dflts) {
6101 switch (devs[u]->target->nodetype) {
6102 case LYS_LEAF:
6103 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6104 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6105 dflt, "deleting", "default", d_del->dflts[0]);
6106
Radek Krejcia1911222019-07-22 17:24:50 +02006107 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006108 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006109 if (strcmp(dflt, d_del->dflts[0])) {
6110 if (i) {
6111 free((char*)dflt);
6112 }
6113 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6114 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6115 d_del->dflts[0], dflt);
6116 goto cleanup;
6117 }
6118 if (i) {
6119 free((char*)dflt);
6120 }
6121 dflt = NULL;
6122
Radek Krejci474f9b82019-07-24 11:36:37 +02006123 /* update the list of incomplete default values if needed */
6124 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6125
Radek Krejcia1911222019-07-22 17:24:50 +02006126 /* remove the default specification */
6127 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6128 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6129 free(leaf->dflt);
6130 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006131 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006132 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006133 break;
6134 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006135 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6136 LY_ARRAY_FOR(d_del->dflts, x) {
6137 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006138 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 +02006139 if (!strcmp(dflt, d_del->dflts[x])) {
6140 if (i) {
6141 free((char*)dflt);
6142 }
6143 dflt = NULL;
6144 break;
6145 }
6146 if (i) {
6147 free((char*)dflt);
6148 }
6149 dflt = NULL;
6150 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006151 if (y == LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02006152 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6153 "which does not match any of the target's property values.", d_del->dflts[x]);
6154 goto cleanup;
6155 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006156
6157 /* update the list of incomplete default values if needed */
6158 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6159
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006160 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006161 LY_ARRAY_DECREMENT(llist->dflts);
6162 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6163 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6164 free(llist->dflts[y]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006165 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
6166 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 +02006167 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006168 if (!LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006169 LY_ARRAY_FREE(llist->dflts_mods);
6170 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006171 LY_ARRAY_FREE(llist->dflts);
6172 llist->dflts = NULL;
6173 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006174 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006175 break;
6176 case LYS_CHOICE:
6177 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6178 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6179 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006180 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006181 if (prefix) {
6182 /* use module prefixes from the deviation module to match the module of the default case */
6183 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6184 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006185 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6186 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006187 goto cleanup;
6188 }
6189 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6190 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006191 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6192 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006193 goto cleanup;
6194 }
6195 }
6196 /* else {
6197 * strictly, the default prefix would point to the deviation module, but the value should actually
6198 * match the default string in the original module (usually unprefixed), so in this case we do not check
6199 * the module of the default case, just matching its name */
6200 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6201 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006202 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6203 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006204 goto cleanup;
6205 }
6206 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6207 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6208 break;
6209 default:
6210 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006211 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006212 goto cleanup;
6213 }
6214 }
6215
6216 break;
6217 case LYS_DEV_REPLACE:
6218 d_rpl = (struct lysp_deviate_rpl*)d;
6219
6220 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006221 if (d_rpl->type) {
6222 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6223 /* type is mandatory, so checking for its presence is not necessary */
6224 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006225
6226 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6227 /* the target has default from the previous type - remove it */
6228 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006229 /* update the list of incomplete default values if needed */
6230 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6231
Radek Krejcia1911222019-07-22 17:24:50 +02006232 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6233 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6234 free(leaf->dflt);
6235 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006236 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006237 } else { /* LYS_LEAFLIST */
6238 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006239 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006240 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6241 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6242 free(llist->dflts[x]);
6243 }
6244 LY_ARRAY_FREE(llist->dflts);
6245 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006246 LY_ARRAY_FREE(llist->dflts_mods);
6247 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006248 }
6249 }
6250 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006251 /* there is no default value, do not set changed_type after type compilation
6252 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006253 changed_type = -1;
6254 }
Radek Krejciec4da802019-05-02 13:02:41 +02006255 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 +02006256 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006257 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006258
6259 /* [units-stmt] */
6260 if (d_rpl->units) {
6261 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6262 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6263 units, "replacing", "units", d_rpl->units);
6264
6265 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6266 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6267 }
6268
6269 /* [default-stmt] */
6270 if (d_rpl->dflt) {
6271 switch (devs[u]->target->nodetype) {
6272 case LYS_LEAF:
6273 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6274 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006275 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006276 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006277 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6278 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6279 dflt = d_rpl->dflt;
6280 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006281 break;
6282 case LYS_CHOICE:
6283 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006284 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 +01006285 goto cleanup;
6286 }
6287 break;
6288 default:
6289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006290 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006291 goto cleanup;
6292 }
6293 }
6294
6295 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006296 if (d_rpl->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01006297 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006298 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006299 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6300 goto cleanup;
6301 }
6302 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006303 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006304 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6305 goto cleanup;
6306 }
Radek Krejci327de162019-06-14 12:52:07 +02006307 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006308 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006309
6310 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006311 if (d_rpl->flags & LYS_MAND_MASK) {
6312 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006313 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006314 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6315 goto cleanup;
6316 }
Radek Krejci327de162019-06-14 12:52:07 +02006317 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006318 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006319
6320 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006321 if (d_rpl->flags & LYS_SET_MIN) {
6322 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6323 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6324 /* change value */
6325 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6326 } else if (devs[u]->target->nodetype == LYS_LIST) {
6327 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6328 /* change value */
6329 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6330 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006331 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006332 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6333 goto cleanup;
6334 }
6335 if (d_rpl->min) {
6336 devs[u]->target->flags |= LYS_MAND_TRUE;
6337 }
6338 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006339
6340 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006341 if (d_rpl->flags & LYS_SET_MAX) {
6342 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6343 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6344 /* change value */
6345 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6346 } else if (devs[u]->target->nodetype == LYS_LIST) {
6347 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6348 /* change value */
6349 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6350 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006351 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006352 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6353 goto cleanup;
6354 }
6355 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006356
6357 break;
6358 default:
6359 LOGINT(ctx->ctx);
6360 goto cleanup;
6361 }
6362 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006363
Radek Krejci33f72892019-02-21 10:36:58 +01006364 /* final check when all deviations of a single target node are applied */
6365
Radek Krejci551b12c2019-02-19 16:11:21 +01006366 /* check min-max compatibility */
6367 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6368 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6369 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6370 } else if (devs[u]->target->nodetype == LYS_LIST) {
6371 min = ((struct lysc_node_list*)devs[u]->target)->min;
6372 max = ((struct lysc_node_list*)devs[u]->target)->max;
6373 } else {
6374 min = max = 0;
6375 }
6376 if (min > max) {
6377 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 +02006378 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006379 goto cleanup;
6380 }
6381
Radek Krejcia1911222019-07-22 17:24:50 +02006382 if (dflt) {
6383 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006384 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006385 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006386 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6387 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006388 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006389 leaf->dflt->realtype->refcount++;
6390 if (err) {
6391 ly_err_print(err);
6392 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6393 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6394 ly_err_free(err);
6395 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006396 if (rc == LY_EINCOMPLETE) {
6397 /* postpone default compilation when the tree is complete */
6398 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6399
6400 /* but in general result is so far ok */
6401 rc = LY_SUCCESS;
6402 }
Radek Krejcia1911222019-07-22 17:24:50 +02006403 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006404 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006405 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6406 int dynamic;
6407 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006408 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006409
6410 /* update the list of incomplete default values if needed */
6411 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6412
6413 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006414 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6415 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6416 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006417 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6418 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006419 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006420 leaf->dflt->realtype->refcount++;
6421 if (err) {
6422 ly_err_print(err);
6423 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6424 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6425 ly_err_free(err);
6426 }
6427 if (dynamic) {
6428 free((void*)dflt);
6429 }
Radek Krejcia1911222019-07-22 17:24:50 +02006430 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006431 if (rc == LY_EINCOMPLETE) {
6432 /* postpone default compilation when the tree is complete */
6433 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6434
6435 /* but in general result is so far ok */
6436 rc = LY_SUCCESS;
6437 }
6438 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006439 } else { /* LYS_LEAFLIST */
6440 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006441 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 +02006442 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6443 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6444 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006445 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6446 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006447 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6448 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006449 llist->dflts[x]->realtype->refcount++;
6450 if (err) {
6451 ly_err_print(err);
6452 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6453 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6454 dflt, err->msg);
6455 ly_err_free(err);
6456 }
6457 if (dynamic) {
6458 free((void*)dflt);
6459 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006460 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006461 if (rc == LY_EINCOMPLETE) {
6462 /* postpone default compilation when the tree is complete */
6463 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6464
6465 /* but in general result is so far ok */
6466 rc = LY_SUCCESS;
6467 }
Radek Krejcia1911222019-07-22 17:24:50 +02006468 LY_CHECK_GOTO(rc, cleanup);
6469 }
6470 }
6471 }
6472
Radek Krejci551b12c2019-02-19 16:11:21 +01006473 /* check mandatory - default compatibility */
6474 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6475 && (devs[u]->target->flags & LYS_SET_DFLT)
6476 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6477 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006478 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006479 goto cleanup;
6480 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6481 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6482 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006483 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 +01006484 goto cleanup;
6485 }
6486 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6487 /* mandatory node under a default case */
6488 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006489 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6490 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006491 goto cleanup;
6492 }
Radek Krejci33f72892019-02-21 10:36:58 +01006493
Michal Vaskoe6143202020-07-03 13:02:08 +02006494 /* add this module into the target module deviated_by, if not there already */
6495 rc = LY_SUCCESS;
6496 LY_ARRAY_FOR(devs[u]->target->module->compiled->deviated_by, v) {
6497 if (devs[u]->target->module->compiled->deviated_by[v] == mod_p->mod) {
6498 rc = LY_EEXIST;
6499 break;
6500 }
6501 }
6502 if (!rc) {
6503 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup);
6504 *dev_mod = mod_p->mod;
6505 }
Michal Vasko57c10cd2020-05-27 15:57:11 +02006506
Radek Krejci327de162019-06-14 12:52:07 +02006507 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006508 }
6509
Radek Krejci327de162019-06-14 12:52:07 +02006510 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006511 ret = LY_SUCCESS;
6512
6513cleanup:
6514 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6515 LY_ARRAY_FREE(devs[u]->deviates);
6516 free(devs[u]);
6517 }
6518 free(devs);
6519 ly_set_erase(&targets, NULL);
6520 ly_set_erase(&devs_p, NULL);
6521
6522 return ret;
6523}
6524
Radek Krejcib56c7502019-02-13 14:19:54 +01006525/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006526 * @brief Compile the given YANG submodule into the main module.
6527 * @param[in] ctx Compile context
6528 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006529 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6530 */
6531LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006532lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006533{
6534 unsigned int u;
6535 LY_ERR ret = LY_SUCCESS;
6536 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006537 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006538 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006539 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006540
Michal Vasko33ff9422020-07-03 09:50:39 +02006541 if (!mainmod->mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01006542 /* features are compiled directly into the compiled module structure,
6543 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6544 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006545 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6546 LY_CHECK_GOTO(ret, error);
6547 }
Radek Krejci0af46292019-01-11 16:02:31 +01006548
Michal Vasko33ff9422020-07-03 09:50:39 +02006549 if (!mainmod->mod->dis_identities) {
6550 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities);
6551 LY_CHECK_GOTO(ret, error);
6552 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01006553
Radek Krejci474f9b82019-07-24 11:36:37 +02006554 /* data nodes */
6555 LY_LIST_FOR(submod->data, node_p) {
6556 ret = lys_compile_node(ctx, node_p, NULL, 0);
6557 LY_CHECK_GOTO(ret, error);
6558 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006559
Radek Krejciec4da802019-05-02 13:02:41 +02006560 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6561 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006562
Radek Krejcid05cbd92018-12-05 14:26:40 +01006563error:
6564 return ret;
6565}
6566
Radek Krejci335332a2019-09-05 13:03:35 +02006567static void *
6568lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6569{
6570 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6571 if (substmts[u].stmt == stmt) {
6572 return substmts[u].storage;
6573 }
6574 }
6575 return NULL;
6576}
6577
6578LY_ERR
6579lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6580{
6581 LY_ERR ret = LY_EVALID, r;
6582 unsigned int u;
6583 struct lysp_stmt *stmt;
6584 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006585
6586 /* check for invalid substatements */
6587 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006588 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6589 continue;
6590 }
Radek Krejci335332a2019-09-05 13:03:35 +02006591 for (u = 0; substmts[u].stmt; ++u) {
6592 if (substmts[u].stmt == stmt->kw) {
6593 break;
6594 }
6595 }
6596 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006597 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6598 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006599 goto cleanup;
6600 }
Radek Krejci335332a2019-09-05 13:03:35 +02006601 }
6602
Radek Krejciad5963b2019-09-06 16:03:05 +02006603 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6604
Radek Krejci335332a2019-09-05 13:03:35 +02006605 /* keep order of the processing the same as the order in the defined substmts,
6606 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6607 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006608 int stmt_present = 0;
6609
Radek Krejci335332a2019-09-05 13:03:35 +02006610 for (stmt = ext->child; stmt; stmt = stmt->next) {
6611 if (substmts[u].stmt != stmt->kw) {
6612 continue;
6613 }
6614
Radek Krejciad5963b2019-09-06 16:03:05 +02006615 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006616 if (substmts[u].storage) {
6617 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006618 case LY_STMT_STATUS:
6619 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6620 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6621 break;
6622 case LY_STMT_UNITS: {
6623 const char **units;
6624
6625 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6626 /* single item */
6627 if (*((const char **)substmts[u].storage)) {
6628 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6629 goto cleanup;
6630 }
6631 units = (const char **)substmts[u].storage;
6632 } else {
6633 /* sized array */
6634 const char ***units_array = (const char ***)substmts[u].storage;
6635 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6636 }
6637 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6638 break;
6639 }
Radek Krejci335332a2019-09-05 13:03:35 +02006640 case LY_STMT_TYPE: {
6641 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6642 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6643
6644 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6645 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006646 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006647 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6648 goto cleanup;
6649 }
6650 compiled = substmts[u].storage;
6651 } else {
6652 /* sized array */
6653 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6654 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6655 compiled = (void*)type;
6656 }
6657
Radek Krejciad5963b2019-09-06 16:03:05 +02006658 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006659 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6660 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006661 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6662 lysp_type_free(ctx->ctx, parsed);
6663 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006664 break;
6665 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006666 case LY_STMT_IF_FEATURE: {
6667 struct lysc_iffeature *iff = NULL;
6668
6669 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6670 /* single item */
6671 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6673 goto cleanup;
6674 }
6675 iff = (struct lysc_iffeature*)substmts[u].storage;
6676 } else {
6677 /* sized array */
6678 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6679 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6680 }
6681 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6682 break;
6683 }
6684 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6685 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006686 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006687 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.",
6688 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006689 goto cleanup;
6690 }
6691 }
Radek Krejci335332a2019-09-05 13:03:35 +02006692 }
Radek Krejci335332a2019-09-05 13:03:35 +02006693
Radek Krejciad5963b2019-09-06 16:03:05 +02006694 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6695 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6696 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6697 goto cleanup;
6698 }
Radek Krejci335332a2019-09-05 13:03:35 +02006699 }
6700
6701 ret = LY_SUCCESS;
6702
6703cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006704 return ret;
6705}
6706
Michal Vasko175012e2019-11-06 15:49:14 +01006707/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006708 * @brief Check when for cyclic dependencies.
6709 * @param[in] set Set with all the referenced nodes.
6710 * @param[in] node Node whose "when" referenced nodes are in @p set.
6711 * @return LY_ERR value
6712 */
6713static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006714lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006715{
6716 struct lyxp_set tmp_set;
6717 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006718 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006719 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006720 int idx;
6721 struct lysc_when *when;
6722 LY_ERR ret = LY_SUCCESS;
6723
6724 memset(&tmp_set, 0, sizeof tmp_set);
6725
6726 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006727 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006728 xp_scnode = &set->val.scnodes[i];
6729
Michal Vasko5c4e5892019-11-14 12:31:38 +01006730 if (xp_scnode->in_ctx != -1) {
6731 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006732 xp_scnode->in_ctx = 1;
6733 }
6734 }
6735
6736 for (i = 0; i < set->used; ++i) {
6737 xp_scnode = &set->val.scnodes[i];
6738 if (xp_scnode->in_ctx != 1) {
6739 /* already checked */
6740 continue;
6741 }
6742
Michal Vasko1bf09392020-03-27 12:38:10 +01006743 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006744 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006745 /* no when to check */
6746 xp_scnode->in_ctx = 0;
6747 continue;
6748 }
6749
6750 node = xp_scnode->scnode;
6751 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006752 LY_ARRAY_FOR(node->when, u) {
6753 when = node->when[u];
Michal Vasko52927e22020-03-16 17:26:14 +01006754 ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006755 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6756 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006757 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006758 goto cleanup;
6759 }
6760
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006761 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006762 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006763 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006764 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006765 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 +01006766 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006767 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 +01006768 ret = LY_EVALID;
6769 goto cleanup;
6770 }
6771
6772 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006773 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006774 } else {
6775 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006776 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006777 }
6778 }
6779
6780 /* merge this set into the global when set */
6781 lyxp_set_scnode_merge(set, &tmp_set);
6782 }
6783
6784 /* check when of non-data parents as well */
6785 node = node->parent;
6786 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6787
Michal Vasko251f56e2019-11-14 16:06:47 +01006788 /* this node when was checked (xp_scnode could have been reallocd) */
6789 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006790 }
6791
6792cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006793 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006794 return ret;
6795}
6796
6797/**
Michal Vasko175012e2019-11-06 15:49:14 +01006798 * @brief Check when/must expressions of a node on a compiled schema tree.
6799 * @param[in] ctx Compile context.
6800 * @param[in] node Node to check.
6801 * @return LY_ERR value
6802 */
6803static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006804lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006805{
Michal Vasko175012e2019-11-06 15:49:14 +01006806 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006807 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006808 LY_ARRAY_COUNT_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01006809 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006810 struct lysc_when **when = NULL;
6811 struct lysc_must *musts = NULL;
6812 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b26e742020-07-17 15:02:10 +02006813 const struct lysc_node *op;
Michal Vasko175012e2019-11-06 15:49:14 +01006814
6815 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006816 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko6b26e742020-07-17 15:02:10 +02006817 if (node->flags & LYS_CONFIG_R) {
6818 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent);
6819 if (op) {
6820 /* we are actually in output */
6821 opts = LYXP_SCNODE_OUTPUT;
6822 }
6823 }
Michal Vasko175012e2019-11-06 15:49:14 +01006824
6825 switch (node->nodetype) {
6826 case LYS_CONTAINER:
6827 when = ((struct lysc_node_container *)node)->when;
6828 musts = ((struct lysc_node_container *)node)->musts;
6829 break;
6830 case LYS_CHOICE:
6831 when = ((struct lysc_node_choice *)node)->when;
6832 break;
6833 case LYS_LEAF:
6834 when = ((struct lysc_node_leaf *)node)->when;
6835 musts = ((struct lysc_node_leaf *)node)->musts;
6836 break;
6837 case LYS_LEAFLIST:
6838 when = ((struct lysc_node_leaflist *)node)->when;
6839 musts = ((struct lysc_node_leaflist *)node)->musts;
6840 break;
6841 case LYS_LIST:
6842 when = ((struct lysc_node_list *)node)->when;
6843 musts = ((struct lysc_node_list *)node)->musts;
6844 break;
6845 case LYS_ANYXML:
6846 case LYS_ANYDATA:
6847 when = ((struct lysc_node_anydata *)node)->when;
6848 musts = ((struct lysc_node_anydata *)node)->musts;
6849 break;
6850 case LYS_CASE:
6851 when = ((struct lysc_node_case *)node)->when;
6852 break;
6853 case LYS_NOTIF:
6854 musts = ((struct lysc_notif *)node)->musts;
6855 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006856 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006857 case LYS_ACTION:
6858 /* first process input musts */
6859 musts = ((struct lysc_action *)node)->input.musts;
6860 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006861 default:
6862 /* nothing to check */
6863 break;
6864 }
6865
Michal Vasko175012e2019-11-06 15:49:14 +01006866 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006867 LY_ARRAY_FOR(when, u) {
Michal Vasko004d3152020-06-11 19:59:22 +02006868 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 +02006869 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006870 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006871 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006872 goto cleanup;
6873 }
6874
Michal Vaskodc052f32019-11-07 11:11:38 +01006875 ctx->path[0] = '\0';
6876 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006877 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006878 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006879 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6880 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006881
Michal Vaskoecd62de2019-11-13 12:35:11 +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, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006884 schema->name);
6885 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006886
6887 /* check dummy node accessing */
6888 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006889 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006890 ret = LY_EVALID;
6891 goto cleanup;
6892 }
Michal Vasko175012e2019-11-06 15:49:14 +01006893 }
6894 }
6895
Michal Vaskoecd62de2019-11-13 12:35:11 +01006896 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006897 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006898 LY_CHECK_GOTO(ret, cleanup);
6899
Michal Vaskod3678892020-05-21 10:06:58 +02006900 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006901 }
6902
Michal Vasko5d8756a2019-11-07 15:21:00 +01006903check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006904 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006905 LY_ARRAY_FOR(musts, u) {
6906 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 +01006907 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006908 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006909 goto cleanup;
6910 }
6911
Michal Vaskodc052f32019-11-07 11:11:38 +01006912 ctx->path[0] = '\0';
6913 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006914 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006915 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006916 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006917 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006918 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6919 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006920 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006921 }
6922 }
6923
Michal Vaskod3678892020-05-21 10:06:58 +02006924 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006925 }
6926
Michal Vasko1bf09392020-03-27 12:38:10 +01006927 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006928 /* now check output musts */
6929 input_done = 1;
6930 musts = ((struct lysc_action *)node)->output.musts;
6931 opts = LYXP_SCNODE_OUTPUT;
6932 goto check_musts;
6933 }
6934
Michal Vasko175012e2019-11-06 15:49:14 +01006935cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006936 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006937 return ret;
6938}
6939
Michal Vasko8d544252020-03-02 10:19:52 +01006940static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006941lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6942{
Michal Vasko6b26e742020-07-17 15:02:10 +02006943 const struct lysc_node *target = NULL, *siter;
Michal Vasko004d3152020-06-11 19:59:22 +02006944 struct ly_path *p;
6945 struct lysc_type *type;
6946
6947 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6948
6949 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02006950 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
6951 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
6952 lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02006953
6954 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006955 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02006956 ly_path_free(node->module->ctx, p);
6957
6958 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
6959 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6960 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
6961 lref->path->expr, lys_nodetype2str(target->nodetype));
6962 return LY_EVALID;
6963 }
6964
6965 /* check status */
6966 ctx->path[0] = '\0';
6967 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6968 ctx->path_len = strlen(ctx->path);
6969 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
6970 return LY_EVALID;
6971 }
6972 ctx->path_len = 1;
6973 ctx->path[1] = '\0';
6974
6975 /* check config */
Michal Vasko6b26e742020-07-17 15:02:10 +02006976 if (lref->require_instance) {
6977 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent);
6978 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006979 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
6980 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
6981 return LY_EVALID;
6982 }
6983 }
6984
6985 /* store the target's type and check for circular chain of leafrefs */
6986 lref->realtype = ((struct lysc_node_leaf *)target)->type;
6987 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
6988 if (type == (struct lysc_type *)lref) {
6989 /* circular chain detected */
6990 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6991 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
6992 return LY_EVALID;
6993 }
6994 }
6995
6996 /* check if leafref and its target are under common if-features */
6997 if (lys_compile_leafref_features_validate(node, target)) {
6998 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6999 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
7000 " features applicable to the leafref itself.", lref->path->expr);
7001 return LY_EVALID;
7002 }
7003
7004 return LY_SUCCESS;
7005}
7006
7007static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01007008lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7009{
7010 struct lysc_ext_instance *ext;
7011 struct lysp_ext_instance *ext_p = NULL;
7012 struct lysp_stmt *stmt;
7013 const struct lys_module *ext_mod;
7014 LY_ERR ret = LY_SUCCESS;
7015
7016 /* create the parsed extension instance manually */
7017 ext_p = calloc(1, sizeof *ext_p);
7018 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
7019 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
7020 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
7021 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7022 ext_p->insubstmt_index = 0;
7023
7024 stmt = calloc(1, sizeof *ext_p->child);
7025 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
7026 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
7027 stmt->kw = LY_STMT_TYPE;
7028 ext_p->child = stmt;
7029
7030 /* allocate new extension instance */
7031 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7032
7033 /* manually get extension definition module */
7034 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7035
7036 /* compile the extension instance */
7037 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7038
7039cleanup:
7040 lysp_ext_instance_free(ctx->ctx, ext_p);
7041 free(ext_p);
7042 return ret;
7043}
7044
Michal Vasko004d3152020-06-11 19:59:22 +02007045static LY_ERR
7046lys_compile_unres(struct lysc_ctx *ctx)
7047{
7048 struct lysc_node *node;
7049 struct lysc_type *type, *typeiter;
7050 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007051 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007052 uint32_t i;
7053
7054 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7055 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7056 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7057 for (i = 0; i < ctx->leafrefs.count; ++i) {
7058 node = ctx->leafrefs.objs[i];
7059 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7060 type = ((struct lysc_node_leaf *)node)->type;
7061 if (type->basetype == LY_TYPE_LEAFREF) {
7062 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7063 } else if (type->basetype == LY_TYPE_UNION) {
7064 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7065 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7066 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7067 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7068 }
7069 }
7070 }
7071 }
7072 for (i = 0; i < ctx->leafrefs.count; ++i) {
7073 /* store pointer to the real type */
7074 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7075 if (type->basetype == LY_TYPE_LEAFREF) {
7076 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7077 typeiter->basetype == LY_TYPE_LEAFREF;
7078 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7079 ((struct lysc_type_leafref*)type)->realtype = typeiter;
7080 } else if (type->basetype == LY_TYPE_UNION) {
7081 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7082 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7083 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7084 typeiter->basetype == LY_TYPE_LEAFREF;
7085 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7086 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7087 }
7088 }
7089 }
7090 }
7091
7092 /* check xpath */
7093 for (i = 0; i < ctx->xpath.count; ++i) {
7094 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7095 }
7096
7097 /* finish incomplete default values compilation */
7098 for (i = 0; i < ctx->dflts.count; ++i) {
7099 struct ly_err_item *err = NULL;
7100 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
7101 LY_ERR ret;
7102 ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
7103 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7104 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7105 if (err) {
7106 ly_err_print(err);
7107 ctx->path[0] = '\0';
7108 lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7109 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7110 "Invalid default - value does not fit the type (%s).", err->msg);
7111 ly_err_free(err);
7112 }
7113 LY_CHECK_RET(ret);
7114 }
7115
7116 return LY_SUCCESS;
7117}
7118
Radek Krejci19a96102018-11-15 13:38:09 +01007119LY_ERR
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007120lys_compile(struct lys_module **mod, int options)
Radek Krejci19a96102018-11-15 13:38:09 +01007121{
7122 struct lysc_ctx ctx = {0};
7123 struct lysc_module *mod_c;
7124 struct lysp_module *sp;
7125 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007126 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007127 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007128 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007129 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007130 uint32_t i;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007131 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007132
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007133 LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007134
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007135 if (!(*mod)->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007136 /* just imported modules are not compiled */
7137 return LY_SUCCESS;
7138 }
7139
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007140 sp = (*mod)->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007141
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007142 ctx.ctx = (*mod)->ctx;
7143 ctx.mod = *mod;
7144 ctx.mod_def = *mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007145 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007146 ctx.path_len = 1;
7147 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007148
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007149 (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
7150 LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
7151 mod_c->mod = *mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007152
Radek Krejciec4da802019-05-02 13:02:41 +02007153 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007154 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007155 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007156 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7157 }
Radek Krejci0935f412019-08-20 16:15:18 +02007158
7159 /* features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007160 if ((*mod)->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007161 /* there is already precompiled array of features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007162 mod_c->features = (*mod)->dis_features;
7163 (*mod)->dis_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007164 } else {
7165 /* features are compiled directly into the compiled module structure,
7166 * 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 +02007167 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007168 LY_CHECK_GOTO(ret, error);
7169 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007170
Radek Krejci0af46292019-01-11 16:02:31 +01007171 /* finish feature compilation, not only for the main module, but also for the submodules.
7172 * Due to possible forward references, it must be done when all the features (including submodules)
7173 * are present. */
7174 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007175 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007176 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7177 }
Radek Krejci327de162019-06-14 12:52:07 +02007178 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007179 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007180 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007181 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007182 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007183 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7184 }
Radek Krejci327de162019-06-14 12:52:07 +02007185 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007186 }
Radek Krejci327de162019-06-14 12:52:07 +02007187 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007188
Michal Vasko33ff9422020-07-03 09:50:39 +02007189 /* identities, work similarly to features with the precompilation */
7190 if ((*mod)->dis_identities) {
7191 mod_c->identities = (*mod)->dis_identities;
7192 (*mod)->dis_identities = NULL;
7193 } else {
7194 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
7195 LY_CHECK_GOTO(ret, error);
7196 }
Radek Krejci19a96102018-11-15 13:38:09 +01007197 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007198 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007199 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007200 lysc_update_path(&ctx, NULL, "{submodule}");
7201 LY_ARRAY_FOR(sp->includes, v) {
7202 if (sp->includes[v].submodule->identities) {
7203 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7204 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities);
7205 LY_CHECK_GOTO(ret, error);
7206 lysc_update_path(&ctx, NULL, NULL);
7207 }
7208 }
Radek Krejci327de162019-06-14 12:52:07 +02007209 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007210
Radek Krejci95710c92019-02-11 15:49:55 +01007211 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007212 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007213 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007214 }
Radek Krejci95710c92019-02-11 15:49:55 +01007215
Radek Krejciec4da802019-05-02 13:02:41 +02007216 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7217 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007218
Radek Krejci95710c92019-02-11 15:49:55 +01007219 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007220 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007221 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007222 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007223 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007224
Radek Krejci474f9b82019-07-24 11:36:37 +02007225 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007226 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007227
Radek Krejci0935f412019-08-20 16:15:18 +02007228 /* extension instances TODO cover extension instances from submodules */
7229 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007230
Michal Vasko004d3152020-06-11 19:59:22 +02007231 /* finish compilation for all unresolved items in the context */
7232 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007233
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007234 /* validate non-instantiated groupings from the parsed schema,
7235 * without it we would accept even the schemas with invalid grouping specification */
7236 ctx.options |= LYSC_OPT_GROUPING;
7237 LY_ARRAY_FOR(sp->groupings, u) {
7238 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007239 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007240 }
7241 }
7242 LY_LIST_FOR(sp->data, node_p) {
7243 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7244 LY_ARRAY_FOR(grps, u) {
7245 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007246 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007247 }
7248 }
7249 }
7250
Radek Krejci474f9b82019-07-24 11:36:37 +02007251 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7252 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7253 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7254 }
7255
Michal Vasko8d544252020-03-02 10:19:52 +01007256#if 0
7257 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7258 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7259 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7260 * the anotation definitions available in the internal schema structure. */
7261 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7262 if (lyp_add_ietf_netconf_annotations(mod)) {
7263 lys_free(mod, NULL, 1, 1);
7264 return NULL;
7265 }
7266 }
7267#endif
7268
7269 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007270 if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
7271 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007272 }
7273
Radek Krejci1c0c3442019-07-23 16:08:47 +02007274 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007275 ly_set_erase(&ctx.xpath, NULL);
7276 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007277 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007278 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007279 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007280
Radek Krejciec4da802019-05-02 13:02:41 +02007281 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007282 lysp_module_free((*mod)->parsed);
7283 (*mod)->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007284 }
7285
Radek Krejciec4da802019-05-02 13:02:41 +02007286 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007287 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007288 for (i = 0; i < ctx.ctx->list.count; ++i) {
7289 m = ctx.ctx->list.objs[i];
Radek Krejci95710c92019-02-11 15:49:55 +01007290 if (m->implemented == 2) {
7291 m->implemented = 1;
7292 }
7293 }
7294 }
7295
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007296 (*mod)->compiled = mod_c;
Radek Krejci19a96102018-11-15 13:38:09 +01007297 return LY_SUCCESS;
7298
7299error:
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007300 lys_feature_precompile_revert(&ctx, *mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007301 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007302 ly_set_erase(&ctx.xpath, NULL);
7303 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007304 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007305 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007306 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007307 lysc_module_free(mod_c, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007308 (*mod)->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007309
7310 /* revert compilation of modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007311 for (i = 0; i < ctx.ctx->list.count; ++i) {
7312 m = ctx.ctx->list.objs[i];
Michal Vasko2947ce12019-12-16 10:37:19 +01007313 if ((m->implemented == 2) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007314 /* revert features list to the precompiled state */
7315 lys_feature_precompile_revert(&ctx, m);
7316 /* mark module as imported-only / not-implemented */
7317 m->implemented = 0;
7318 /* free the compiled version of the module */
7319 lysc_module_free(m->compiled, NULL);
7320 m->compiled = NULL;
7321 }
7322 }
7323
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007324 /* remove the module itself from the context and free it */
7325 ly_set_rm(&ctx.ctx->list, *mod, NULL);
7326 lys_module_free(*mod, NULL);
7327 *mod = NULL;
7328
Radek Krejci19a96102018-11-15 13:38:09 +01007329 return ret;
7330}