blob: 4417027b077b0990561c887b533ccdbea027340d [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejci19a96102018-11-15 13:38:09 +010016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020026#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "dict.h"
29#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020030#include "path.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020031#include "parser.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020032#include "parser_schema.h"
Radek Krejci0935f412019-08-20 16:15:18 +020033#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020035#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020039#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010040#include "tree_schema_internal.h"
41#include "xpath.h"
42
Michal Vasko5fe75f12020-03-02 13:52:37 +010043static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
44 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
45
Radek Krejci19a96102018-11-15 13:38:09 +010046/**
47 * @brief Duplicate string into dictionary
48 * @param[in] CTX libyang context of the dictionary.
49 * @param[in] ORIG String to duplicate.
50 * @param[out] DUP Where to store the result.
51 */
52#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
53
Radek Krejciec4da802019-05-02 13:02:41 +020054#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010055 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020056 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
57 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
58 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci19a96102018-11-15 13:38:09 +010059 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020060 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010061 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
62 } \
63 }
64
Radek Krejciec4da802019-05-02 13:02:41 +020065#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010066 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020067 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
68 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
69 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010070 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020071 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010072 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
73 } \
74 }
75
Radek Krejci0935f412019-08-20 16:15:18 +020076#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
77 if (EXTS_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020078 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
79 for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \
Radek Krejci0935f412019-08-20 16:15:18 +020080 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010081 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \
Radek Krejci0935f412019-08-20 16:15:18 +020082 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
83 } \
84 }
85
Radek Krejciec4da802019-05-02 13:02:41 +020086#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010087 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020088 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
89 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
90 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010091 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020092 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010093 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
94 } \
95 }
96
Radek Krejciec4da802019-05-02 13:02:41 +020097#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010098 if (MEMBER_P) { \
99 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
100 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200101 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100102 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
103 }
104
Radek Krejciec4da802019-05-02 13:02:41 +0200105#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100106 if (MEMBER_P) { \
107 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200108 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100109 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200110 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100111 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
112 }
113
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100114#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100115 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200116 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100117 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100118 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
119 return LY_EVALID; \
120 } \
121 } \
122 }
123
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100124#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
125 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200126 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100127 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
128 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
129 return LY_EVALID; \
130 } \
131 } \
132 }
133
134struct lysc_ext *
135lysc_ext_dup(struct lysc_ext *orig)
136{
137 ++orig->refcount;
138 return orig;
139}
140
Radek Krejci19a96102018-11-15 13:38:09 +0100141static struct lysc_ext_instance *
142lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
143{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100144 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100145 (void) ctx;
146 (void) orig;
147 return NULL;
148}
149
Radek Krejcib56c7502019-02-13 14:19:54 +0100150/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200151 * @brief Add record into the compile context's list of incomplete default values.
152 * @param[in] ctx Compile context with the incomplete default values list.
153 * @param[in] context_node Context schema node to store in the record.
154 * @param[in] dflt Incomplete default value to store in the record.
155 * @param[in] dflt_mod Module of the default value definition to store in the record.
156 * @return LY_EMEM in case of memory allocation failure.
157 * @return LY_SUCCESS
158 */
159static LY_ERR
160lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
161{
162 struct lysc_incomplete_dflt *r;
163 r = malloc(sizeof *r);
164 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
165 r->context_node = context_node;
166 r->dflt = dflt;
167 r->dflt_mod = dflt_mod;
168 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
169
170 return LY_SUCCESS;
171}
172
173/**
174 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
175 * @param[in] ctx Compile context with the incomplete default values list.
176 * @param[in] dflt Incomplete default values identifying the record to remove.
177 */
178static void
179lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
180{
181 unsigned int u;
182 struct lysc_incomplete_dflt *r;
183
184 for (u = 0; u < ctx->dflts.count; ++u) {
185 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
186 if (r->dflt == dflt) {
187 free(ctx->dflts.objs[u]);
188 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
189 --ctx->dflts.count;
190 return;
191 }
192 }
193}
194
Radek Krejci0e59c312019-08-15 15:34:15 +0200195void
Radek Krejci327de162019-06-14 12:52:07 +0200196lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
197{
198 int len;
199 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
200
201 if (!name) {
202 /* removing last path segment */
203 if (ctx->path[ctx->path_len - 1] == '}') {
204 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
205 if (ctx->path[ctx->path_len] == '=') {
206 ctx->path[ctx->path_len++] = '}';
207 } else {
208 /* not a top-level special tag, remove also preceiding '/' */
209 goto remove_nodelevel;
210 }
211 } else {
212remove_nodelevel:
213 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
214 if (ctx->path_len == 0) {
215 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200216 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200217 }
218 }
219 /* set new terminating NULL-byte */
220 ctx->path[ctx->path_len] = '\0';
221 } else {
222 if (ctx->path_len > 1) {
223 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
224 /* extension of the special tag */
225 nextlevel = 2;
226 --ctx->path_len;
227 } else {
228 /* there is already some path, so add next level */
229 nextlevel = 1;
230 }
231 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
232
233 if (nextlevel != 2) {
234 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
235 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200236 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200237 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200238 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200239 }
240 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200241 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200242 }
Radek Krejciacc79042019-07-25 14:14:57 +0200243 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
244 /* output truncated */
245 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
246 } else {
247 ctx->path_len += len;
248 }
Radek Krejci327de162019-06-14 12:52:07 +0200249 }
250}
251
252/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100253 * @brief Duplicate the compiled pattern structure.
254 *
255 * Instead of duplicating memory, the reference counter in the @p orig is increased.
256 *
257 * @param[in] orig The pattern structure to duplicate.
258 * @return The duplicated structure to use.
259 */
Radek Krejci19a96102018-11-15 13:38:09 +0100260static struct lysc_pattern*
261lysc_pattern_dup(struct lysc_pattern *orig)
262{
263 ++orig->refcount;
264 return orig;
265}
266
Radek Krejcib56c7502019-02-13 14:19:54 +0100267/**
268 * @brief Duplicate the array of compiled patterns.
269 *
270 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
271 *
272 * @param[in] ctx Libyang context for logging.
273 * @param[in] orig The patterns sized array to duplicate.
274 * @return New sized array as a copy of @p orig.
275 * @return NULL in case of memory allocation error.
276 */
Radek Krejci19a96102018-11-15 13:38:09 +0100277static struct lysc_pattern**
278lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
279{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100280 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200281 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100282
Radek Krejcib56c7502019-02-13 14:19:54 +0100283 assert(orig);
284
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200285 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100286 LY_ARRAY_FOR(orig, u) {
287 dup[u] = lysc_pattern_dup(orig[u]);
288 LY_ARRAY_INCREMENT(dup);
289 }
290 return dup;
291}
292
Radek Krejcib56c7502019-02-13 14:19:54 +0100293/**
294 * @brief Duplicate compiled range structure.
295 *
296 * @param[in] ctx Libyang context for logging.
297 * @param[in] orig The range structure to be duplicated.
298 * @return New compiled range structure as a copy of @p orig.
299 * @return NULL in case of memory allocation error.
300 */
Radek Krejci19a96102018-11-15 13:38:09 +0100301struct lysc_range*
302lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
303{
304 struct lysc_range *dup;
305 LY_ERR ret;
306
Radek Krejcib56c7502019-02-13 14:19:54 +0100307 assert(orig);
308
Radek Krejci19a96102018-11-15 13:38:09 +0100309 dup = calloc(1, sizeof *dup);
310 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
311 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200312 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
313 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
314 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100315 }
316 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
317 DUP_STRING(ctx, orig->emsg, dup->emsg);
318 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
319
320 return dup;
321cleanup:
322 free(dup);
323 (void) ret; /* set but not used due to the return type */
324 return NULL;
325}
326
Radek Krejcib56c7502019-02-13 14:19:54 +0100327/**
328 * @brief Stack for processing if-feature expressions.
329 */
Radek Krejci19a96102018-11-15 13:38:09 +0100330struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100331 int size; /**< number of items in the stack */
332 int index; /**< first empty item */
333 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100334};
335
Radek Krejcib56c7502019-02-13 14:19:54 +0100336/**
337 * @brief Add @ref ifftokens into the stack.
338 * @param[in] stack The if-feature stack to use.
339 * @param[in] value One of the @ref ifftokens to store in the stack.
340 * @return LY_EMEM in case of memory allocation error
341 * @return LY_ESUCCESS if the value successfully stored.
342 */
Radek Krejci19a96102018-11-15 13:38:09 +0100343static LY_ERR
344iff_stack_push(struct iff_stack *stack, uint8_t value)
345{
346 if (stack->index == stack->size) {
347 stack->size += 4;
348 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
349 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
350 }
351 stack->stack[stack->index++] = value;
352 return LY_SUCCESS;
353}
354
Radek Krejcib56c7502019-02-13 14:19:54 +0100355/**
356 * @brief Get (and remove) the last item form the stack.
357 * @param[in] stack The if-feature stack to use.
358 * @return The value from the top of the stack.
359 */
Radek Krejci19a96102018-11-15 13:38:09 +0100360static uint8_t
361iff_stack_pop(struct iff_stack *stack)
362{
Radek Krejcib56c7502019-02-13 14:19:54 +0100363 assert(stack && stack->index);
364
Radek Krejci19a96102018-11-15 13:38:09 +0100365 stack->index--;
366 return stack->stack[stack->index];
367}
368
Radek Krejcib56c7502019-02-13 14:19:54 +0100369/**
370 * @brief Clean up the stack.
371 * @param[in] stack The if-feature stack to use.
372 */
Radek Krejci19a96102018-11-15 13:38:09 +0100373static void
374iff_stack_clean(struct iff_stack *stack)
375{
376 stack->size = 0;
377 free(stack->stack);
378}
379
Radek Krejcib56c7502019-02-13 14:19:54 +0100380/**
381 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
382 * (libyang format of the if-feature expression).
383 * @param[in,out] list The 2bits array to modify.
384 * @param[in] op The operand (@ref ifftokens) to store.
385 * @param[in] pos Position (0-based) where to store the given @p op.
386 */
Radek Krejci19a96102018-11-15 13:38:09 +0100387static void
388iff_setop(uint8_t *list, uint8_t op, int pos)
389{
390 uint8_t *item;
391 uint8_t mask = 3;
392
393 assert(pos >= 0);
394 assert(op <= 3); /* max 2 bits */
395
396 item = &list[pos / 4];
397 mask = mask << 2 * (pos % 4);
398 *item = (*item) & ~mask;
399 *item = (*item) | (op << 2 * (pos % 4));
400}
401
Radek Krejcib56c7502019-02-13 14:19:54 +0100402#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
403#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100404
Radek Krejci0af46292019-01-11 16:02:31 +0100405/**
406 * @brief Find a feature of the given name and referenced in the given module.
407 *
408 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
409 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
410 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
411 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
412 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
413 * assigned till that time will be still valid.
414 *
415 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
416 * @param[in] name Name of the feature including possible prefix.
417 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
418 * @return Pointer to the feature structure if found, NULL otherwise.
419 */
Radek Krejci19a96102018-11-15 13:38:09 +0100420static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100421lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100422{
423 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200424 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0af46292019-01-11 16:02:31 +0100425 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100426
427 for (i = 0; i < len; ++i) {
428 if (name[i] == ':') {
429 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100430 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100431 LY_CHECK_RET(!mod, NULL);
432
433 name = &name[i + 1];
434 len = len - i - 1;
435 }
436 }
437
438 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100439 if (mod->implemented) {
440 /* module is implemented so there is already the compiled schema */
441 flist = mod->compiled->features;
442 } else {
Michal Vasko33ff9422020-07-03 09:50:39 +0200443 flist = mod->dis_features;
Radek Krejci0af46292019-01-11 16:02:31 +0100444 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200445 LY_ARRAY_FOR(flist, u) {
446 f = &flist[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200447 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100448 return f;
449 }
450 }
451
452 return NULL;
453}
454
Michal Vasko8d544252020-03-02 10:19:52 +0100455/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100456 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
457 */
458static LY_ERR
459lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
460{
461 LY_ERR ret = LY_SUCCESS;
462
463 if (!ext_p->compiled) {
464 lysc_update_path(ctx, NULL, "{extension}");
465 lysc_update_path(ctx, NULL, ext_p->name);
466
467 /* compile the extension definition */
468 ext_p->compiled = calloc(1, sizeof **ext);
469 ext_p->compiled->refcount = 1;
470 DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name);
471 DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument);
472 ext_p->compiled->module = (struct lys_module *)ext_mod;
473 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
474
475 lysc_update_path(ctx, NULL, NULL);
476 lysc_update_path(ctx, NULL, NULL);
477
478 /* find extension definition plugin */
479 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
480 }
481
482 *ext = lysc_ext_dup(ext_p->compiled);
483
484done:
485 return ret;
486}
487
488/**
Michal Vasko8d544252020-03-02 10:19:52 +0100489 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
490 *
491 * @param[in] ctx Compilation context.
492 * @param[in] ext_p Parsed extension instance.
493 * @param[in,out] ext Prepared compiled extension instance.
494 * @param[in] parent Extension instance parent.
495 * @param[in] parent_type Extension instance parent type.
496 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
497 */
Radek Krejci19a96102018-11-15 13:38:09 +0100498static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100499lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
500 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100501{
Radek Krejci7c960162019-09-18 14:16:12 +0200502 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100503 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200504 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200505 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200506 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100507
508 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
509 ext->insubstmt = ext_p->insubstmt;
510 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200511 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200512 ext->parent = parent;
513 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100514
Radek Krejcif56e2a42019-09-09 14:15:25 +0200515 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200516
Radek Krejci19a96102018-11-15 13:38:09 +0100517 /* get module where the extension definition should be placed */
Radek Krejci7c960162019-09-18 14:16:12 +0200518 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u);
519 if (ext_p->yin) {
520 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
521 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
522 * YANG (import) prefix must be done here. */
523 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
524 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
525 u = 0;
526 } else if (ctx->mod_def->compiled) {
Radek Krejci7c960162019-09-18 14:16:12 +0200527 LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) {
528 if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) {
529 char *s;
530 asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]);
531 prefixed_name = lydict_insert_zc(ctx->ctx, s);
532 u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */
533 break;
534 }
535 }
536 }
537 if (!prefixed_name) {
538 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
539 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
540 goto cleanup;
541 }
542 } else {
543 prefixed_name = ext_p->name;
544 }
545 lysc_update_path(ctx, NULL, prefixed_name);
546
Michal Vasko8d544252020-03-02 10:19:52 +0100547 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200548 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100549 if (!ext_mod) {
550 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
551 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
552 goto cleanup;
553 } else if (!ext_mod->parsed->extensions) {
554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
555 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
556 prefixed_name, ext_mod->name);
557 goto cleanup;
558 }
Radek Krejci7c960162019-09-18 14:16:12 +0200559 }
560 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200561
Michal Vasko5fe75f12020-03-02 13:52:37 +0100562 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200563 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
564 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100565 /* compile extension definition and assign it */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200566 LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def));
Radek Krejci19a96102018-11-15 13:38:09 +0100567 break;
568 }
569 }
Radek Krejci7c960162019-09-18 14:16:12 +0200570 if (!ext->def) {
571 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
572 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
573 goto cleanup;
574 }
Radek Krejci0935f412019-08-20 16:15:18 +0200575
Radek Krejcif56e2a42019-09-09 14:15:25 +0200576 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
577 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
578 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200579 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200580 /* Schema was parsed from YIN and an argument is expected, ... */
581 struct lysp_stmt *stmt = NULL;
582
583 if (ext->def->flags & LYS_YINELEM_TRUE) {
584 /* ... argument was the first XML child element */
585 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
586 /* TODO check namespace of the statement */
587 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
588 stmt = ext_p->child;
589 }
590 }
591 } else {
592 /* ... argument was one of the XML attributes which are represented as child stmt
593 * with LYS_YIN_ATTR flag */
594 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
595 if (!strcmp(stmt->stmt, ext->def->argument)) {
596 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200597 break;
598 }
599 }
600 }
601 if (!stmt) {
602 /* missing extension's argument */
603 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200604 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
605 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200606
607 }
608 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
609 stmt->flags |= LYS_YIN_ARGUMENT;
610 }
Radek Krejci7c960162019-09-18 14:16:12 +0200611 if (prefixed_name != ext_p->name) {
612 lydict_remove(ctx->ctx, ext_p->name);
613 ext_p->name = prefixed_name;
614 if (!ext_p->argument && ext->argument) {
615 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
616 }
617 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200618
Radek Krejci0935f412019-08-20 16:15:18 +0200619 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200620 if (ext->argument) {
621 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
622 }
Radek Krejci7c960162019-09-18 14:16:12 +0200623 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200624 if (ext->argument) {
625 lysc_update_path(ctx, NULL, NULL);
626 }
Radek Krejci0935f412019-08-20 16:15:18 +0200627 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200628 ext_p->compiled = ext;
629
Radek Krejci7c960162019-09-18 14:16:12 +0200630 ret = LY_SUCCESS;
631cleanup:
632 if (prefixed_name && prefixed_name != ext_p->name) {
633 lydict_remove(ctx->ctx, prefixed_name);
634 }
635
Radek Krejcif56e2a42019-09-09 14:15:25 +0200636 lysc_update_path(ctx, NULL, NULL);
637 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200638
Radek Krejci7c960162019-09-18 14:16:12 +0200639 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200640}
641
642/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100643 * @brief Compile information from the if-feature statement
644 * @param[in] ctx Compile context.
645 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
Radek Krejcib56c7502019-02-13 14:19:54 +0100646 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
647 * @return LY_ERR value.
648 */
Radek Krejci19a96102018-11-15 13:38:09 +0100649static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200650lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100651{
652 const char *c = *value;
653 int r, rc = EXIT_FAILURE;
654 int i, j, last_not, checkversion = 0;
655 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
656 uint8_t op;
657 struct iff_stack stack = {0, 0, NULL};
658 struct lysc_feature *f;
659
660 assert(c);
661
662 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
663 for (i = j = last_not = 0; c[i]; i++) {
664 if (c[i] == '(') {
665 j++;
666 checkversion = 1;
667 continue;
668 } else if (c[i] == ')') {
669 j--;
670 continue;
671 } else if (isspace(c[i])) {
672 checkversion = 1;
673 continue;
674 }
675
676 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200677 int sp;
678 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
679 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
681 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
682 return LY_EVALID;
683 } else if (!isspace(c[i + r])) {
684 /* feature name starting with the not/and/or */
685 last_not = 0;
686 f_size++;
687 } else if (c[i] == 'n') { /* not operation */
688 if (last_not) {
689 /* double not */
690 expr_size = expr_size - 2;
691 last_not = 0;
692 } else {
693 last_not = 1;
694 }
695 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200696 if (f_exp != f_size) {
697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
698 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
699 return LY_EVALID;
700 }
Radek Krejci19a96102018-11-15 13:38:09 +0100701 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200702
Radek Krejci19a96102018-11-15 13:38:09 +0100703 /* not a not operation */
704 last_not = 0;
705 }
706 i += r;
707 } else {
708 f_size++;
709 last_not = 0;
710 }
711 expr_size++;
712
713 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200714 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100715 i--;
716 break;
717 }
718 i++;
719 }
720 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200721 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100722 /* not matching count of ( and ) */
723 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
724 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
725 return LY_EVALID;
726 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200727 if (f_exp != f_size) {
728 /* features do not match the needed arguments for the logical operations */
729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
730 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
731 "the required number of operands for the operations.", *value);
732 return LY_EVALID;
733 }
Radek Krejci19a96102018-11-15 13:38:09 +0100734
735 if (checkversion || expr_size > 1) {
736 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100737 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100738 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
739 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
740 return LY_EVALID;
741 }
742 }
743
744 /* allocate the memory */
745 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
746 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
747 stack.stack = malloc(expr_size * sizeof *stack.stack);
748 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
749
750 stack.size = expr_size;
751 f_size--; expr_size--; /* used as indexes from now */
752
753 for (i--; i >= 0; i--) {
754 if (c[i] == ')') {
755 /* push it on stack */
756 iff_stack_push(&stack, LYS_IFF_RP);
757 continue;
758 } else if (c[i] == '(') {
759 /* pop from the stack into result all operators until ) */
760 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
761 iff_setop(iff->expr, op, expr_size--);
762 }
763 continue;
764 } else if (isspace(c[i])) {
765 continue;
766 }
767
768 /* end of operator or operand -> find beginning and get what is it */
769 j = i + 1;
770 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
771 i--;
772 }
773 i++; /* go back by one step */
774
775 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
776 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
777 /* double not */
778 iff_stack_pop(&stack);
779 } else {
780 /* not has the highest priority, so do not pop from the stack
781 * as in case of AND and OR */
782 iff_stack_push(&stack, LYS_IFF_NOT);
783 }
784 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
785 /* as for OR - pop from the stack all operators with the same or higher
786 * priority and store them to the result, then push the AND to the stack */
787 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
788 op = iff_stack_pop(&stack);
789 iff_setop(iff->expr, op, expr_size--);
790 }
791 iff_stack_push(&stack, LYS_IFF_AND);
792 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
793 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
794 op = iff_stack_pop(&stack);
795 iff_setop(iff->expr, op, expr_size--);
796 }
797 iff_stack_push(&stack, LYS_IFF_OR);
798 } else {
799 /* feature name, length is j - i */
800
801 /* add it to the expression */
802 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
803
804 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100805 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
806 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
807 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
808 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100809 iff->features[f_size] = f;
810 LY_ARRAY_INCREMENT(iff->features);
811 f_size--;
812 }
813 }
814 while (stack.index) {
815 op = iff_stack_pop(&stack);
816 iff_setop(iff->expr, op, expr_size--);
817 }
818
819 if (++expr_size || ++f_size) {
820 /* not all expected operators and operands found */
821 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
822 "Invalid value \"%s\" of if-feature - processing error.", *value);
823 rc = LY_EINT;
824 } else {
825 rc = LY_SUCCESS;
826 }
827
828error:
829 /* cleanup */
830 iff_stack_clean(&stack);
831
832 return rc;
833}
834
Radek Krejcib56c7502019-02-13 14:19:54 +0100835/**
Michal Vasko175012e2019-11-06 15:49:14 +0100836 * @brief Get the XPath context node for the given schema node.
837 * @param[in] start The schema node where the XPath expression appears.
838 * @return The context node to evaluate XPath expression in given schema node.
839 * @return NULL in case the context node is the root node.
840 */
841static struct lysc_node *
842lysc_xpath_context(struct lysc_node *start)
843{
Michal Vasko1bf09392020-03-27 12:38:10 +0100844 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF));
Michal Vasko175012e2019-11-06 15:49:14 +0100845 start = start->parent);
846 return start;
847}
848
849/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100850 * @brief Compile information from the when statement
851 * @param[in] ctx Compile context.
852 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100853 * @param[in] flags Flags of the node with the "when" defiition.
854 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100855 * @param[out] when Pointer where to store pointer to the created compiled when structure.
856 * @return LY_ERR value.
857 */
Radek Krejci19a96102018-11-15 13:38:09 +0100858static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100859lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100860{
Radek Krejci19a96102018-11-15 13:38:09 +0100861 LY_ERR ret = LY_SUCCESS;
862
Radek Krejci00b874b2019-02-12 10:54:50 +0100863 *when = calloc(1, sizeof **when);
864 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200865 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200866 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100867 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100868 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
869 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
870 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200871 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100872 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100873
874done:
875 return ret;
876}
877
Radek Krejcib56c7502019-02-13 14:19:54 +0100878/**
879 * @brief Compile information from the must statement
880 * @param[in] ctx Compile context.
881 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100882 * @param[in,out] must Prepared (empty) compiled must structure to fill.
883 * @return LY_ERR value.
884 */
Radek Krejci19a96102018-11-15 13:38:09 +0100885static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200886lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100887{
Radek Krejci19a96102018-11-15 13:38:09 +0100888 LY_ERR ret = LY_SUCCESS;
889
Michal Vasko004d3152020-06-11 19:59:22 +0200890 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100891 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200892 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100893 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
894 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100895 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
896 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200897 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100898
899done:
900 return ret;
901}
902
Radek Krejcib56c7502019-02-13 14:19:54 +0100903/**
904 * @brief Compile information from the import statement
905 * @param[in] ctx Compile context.
906 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100907 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
908 * @return LY_ERR value.
909 */
Radek Krejci19a96102018-11-15 13:38:09 +0100910static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200911lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100912{
Radek Krejci19a96102018-11-15 13:38:09 +0100913 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100914 LY_ERR ret = LY_SUCCESS;
915
916 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200917 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100918 imp->module = imp_p->module;
919
Radek Krejci7f2a5362018-11-28 13:05:37 +0100920 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
921 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100922 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100923 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100924 /* try to use filepath if present */
925 if (imp->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200926 struct ly_in *in;
927 if (ly_in_new_filepath(imp->module->filepath, 0, &in)) {
928 LOGINT(ctx->ctx);
929 } else {
930 mod = lys_parse(ctx->ctx, in, !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
931 if (mod != imp->module) {
932 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", imp->module->filepath, imp->module->name);
933 mod = NULL;
934 }
Radek Krejci19a96102018-11-15 13:38:09 +0100935 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200936 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100937 }
938 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100939 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100940 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100941 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100942 return LY_ENOTFOUND;
943 }
944 }
Radek Krejci19a96102018-11-15 13:38:09 +0100945 }
946
947done:
948 return ret;
949}
950
Michal Vasko33ff9422020-07-03 09:50:39 +0200951LY_ERR
952lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
953 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +0100954{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200955 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +0200956 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +0100957 LY_ERR ret = LY_SUCCESS;
958
Michal Vasko33ff9422020-07-03 09:50:39 +0200959 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +0200960
Michal Vasko33ff9422020-07-03 09:50:39 +0200961 if (!ctx_sc) {
962 context.ctx = ctx;
963 context.mod = module;
964 context.path_len = 1;
965 context.path[0] = '/';
966 ctx_sc = &context;
967 }
Radek Krejci19a96102018-11-15 13:38:09 +0100968
Michal Vasko33ff9422020-07-03 09:50:39 +0200969 if (!identities_p) {
970 return LY_SUCCESS;
971 }
972 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200973 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +0200974 }
975
976 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200977 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +0200978 LY_ARRAY_FOR(identities_p, u) {
979 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
980
981 LY_ARRAY_INCREMENT(*identities);
982 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
983 DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name);
984 DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc);
985 DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref);
986 (*identities)[offset + u].module = ctx_sc->mod;
987 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
988 lys_compile_iffeature, ret, done);
989 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
990 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
991 LYEXT_PAR_IDENT, ret, done);
992 (*identities)[offset + u].flags = identities_p[u].flags;
993
994 lysc_update_path(ctx_sc, NULL, NULL);
995 }
996 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100997done:
998 return ret;
999}
1000
Radek Krejcib56c7502019-02-13 14:19:54 +01001001/**
1002 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1003 *
1004 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1005 *
1006 * @param[in] ctx Compile context for logging.
1007 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1008 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1009 * @return LY_SUCCESS if everything is ok.
1010 * @return LY_EVALID if the identity is derived from itself.
1011 */
Radek Krejci38222632019-02-12 16:55:05 +01001012static LY_ERR
1013lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1014{
1015 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001016 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001017 struct ly_set recursion = {0};
1018 struct lysc_ident *drv;
1019
1020 if (!derived) {
1021 return LY_SUCCESS;
1022 }
1023
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001024 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001025 if (ident == derived[u]) {
1026 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1027 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1028 goto cleanup;
1029 }
1030 ly_set_add(&recursion, derived[u], 0);
1031 }
1032
1033 for (v = 0; v < recursion.count; ++v) {
1034 drv = recursion.objs[v];
1035 if (!drv->derived) {
1036 continue;
1037 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001038 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001039 if (ident == drv->derived[u]) {
1040 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1041 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1042 goto cleanup;
1043 }
1044 ly_set_add(&recursion, drv->derived[u], 0);
1045 }
1046 }
1047 ret = LY_SUCCESS;
1048
1049cleanup:
1050 ly_set_erase(&recursion, NULL);
1051 return ret;
1052}
1053
Radek Krejcia3045382018-11-22 14:30:31 +01001054/**
1055 * @brief Find and process the referenced base identities from another identity or identityref
1056 *
Radek Krejciaca74032019-06-04 08:53:06 +02001057 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001058 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1059 * to distinguish these two use cases.
1060 *
1061 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1062 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001063 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001064 * @param[in] bases Array of bases of identityref to fill in.
1065 * @return LY_ERR value.
1066 */
Radek Krejci19a96102018-11-15 13:38:09 +01001067static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001068lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
1069 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001070{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001071 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001072 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001073 struct lys_module *mod;
Michal Vasko33ff9422020-07-03 09:50:39 +02001074 struct lysc_ident **idref, *identities;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001075
1076 assert(ident || bases);
1077
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001078 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001079 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1080 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1081 return LY_EVALID;
1082 }
1083
Michal Vasko33ff9422020-07-03 09:50:39 +02001084 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001085 s = strchr(bases_p[u], ':');
1086 if (s) {
1087 /* prefixed identity */
1088 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001089 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001090 } else {
1091 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001092 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001093 }
1094 if (!mod) {
1095 if (ident) {
1096 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1097 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1098 } else {
1099 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1100 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1101 }
1102 return LY_EVALID;
1103 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001104
Radek Krejci555cb5b2018-11-16 14:54:33 +01001105 idref = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02001106 if (mod->compiled) {
1107 identities = mod->compiled->identities;
1108 } else {
1109 identities = mod->dis_identities;
1110 }
1111 LY_ARRAY_FOR(identities, v) {
1112 if (!strcmp(name, identities[v].name)) {
1113 if (ident) {
1114 if (ident == &identities[v]) {
1115 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1116 "Identity \"%s\" is derived from itself.", ident->name);
1117 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001118 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001119 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived));
1120 /* we have match! store the backlink */
1121 LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM);
1122 *idref = ident;
1123 } else {
1124 /* we have match! store the found identity */
1125 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
1126 *idref = &identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001127 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001128 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001129 }
1130 }
1131 if (!idref || !(*idref)) {
1132 if (ident) {
1133 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1134 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1135 } else {
1136 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1137 "Unable to find base (%s) of identityref.", bases_p[u]);
1138 }
1139 return LY_EVALID;
1140 }
1141 }
1142 return LY_SUCCESS;
1143}
1144
Radek Krejcia3045382018-11-22 14:30:31 +01001145/**
1146 * @brief For the given array of identities, set the backlinks from all their base identities.
1147 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1148 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1149 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1150 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1151 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001152static LY_ERR
1153lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1154{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001155 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001156
Michal Vasko33ff9422020-07-03 09:50:39 +02001157 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001158 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001159 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001160 continue;
1161 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001162 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001163 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents[u].module, idents_p[u].bases, &idents[u], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001164 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001165 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001166 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001167 return LY_SUCCESS;
1168}
1169
Radek Krejci0af46292019-01-11 16:02:31 +01001170LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001171lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
1172 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001173{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001174 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001175 struct lysc_ctx context = {0};
1176
Radek Krejci327de162019-06-14 12:52:07 +02001177 assert(ctx_sc || ctx);
1178
1179 if (!ctx_sc) {
1180 context.ctx = ctx;
1181 context.mod = module;
1182 context.path_len = 1;
1183 context.path[0] = '/';
1184 ctx_sc = &context;
1185 }
Radek Krejci0af46292019-01-11 16:02:31 +01001186
1187 if (!features_p) {
1188 return LY_SUCCESS;
1189 }
1190 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001191 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001192 }
1193
Radek Krejci327de162019-06-14 12:52:07 +02001194 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001195 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001196 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001197 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1198
Radek Krejci0af46292019-01-11 16:02:31 +01001199 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001200 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci327de162019-06-14 12:52:07 +02001201 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1202 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1203 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001204 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001205 (*features)[offset + u].module = ctx_sc->mod;
1206
1207 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001208 }
Radek Krejci327de162019-06-14 12:52:07 +02001209 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001210
1211 return LY_SUCCESS;
1212}
1213
Radek Krejcia3045382018-11-22 14:30:31 +01001214/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001215 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001216 *
1217 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1218 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001219 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001220 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1221 * being currently processed).
1222 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
Radek Krejci09a1fc52019-02-13 10:55:17 +01001223 * @return LY_SUCCESS if everything is ok.
1224 * @return LY_EVALID if the feature references indirectly itself.
1225 */
1226static LY_ERR
1227lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1228{
1229 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001230 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001231 struct ly_set recursion = {0};
1232 struct lysc_feature *drv;
1233
1234 if (!depfeatures) {
1235 return LY_SUCCESS;
1236 }
1237
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001238 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001239 if (feature == depfeatures[u]) {
1240 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1241 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1242 goto cleanup;
1243 }
1244 ly_set_add(&recursion, depfeatures[u], 0);
1245 }
1246
1247 for (v = 0; v < recursion.count; ++v) {
1248 drv = recursion.objs[v];
1249 if (!drv->depfeatures) {
1250 continue;
1251 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001252 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001253 if (feature == drv->depfeatures[u]) {
1254 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1255 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1256 goto cleanup;
1257 }
1258 ly_set_add(&recursion, drv->depfeatures[u], 0);
1259 }
1260 }
1261 ret = LY_SUCCESS;
1262
1263cleanup:
1264 ly_set_erase(&recursion, NULL);
1265 return ret;
1266}
1267
1268/**
Radek Krejci0af46292019-01-11 16:02:31 +01001269 * @brief Create pre-compiled features array.
1270 *
1271 * See lys_feature_precompile() for more details.
1272 *
Radek Krejcia3045382018-11-22 14:30:31 +01001273 * @param[in] ctx Compile context.
1274 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001275 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001276 * @return LY_ERR value.
1277 */
Radek Krejci19a96102018-11-15 13:38:09 +01001278static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001279lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001280{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001281 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001282 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001283 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001284
Radek Krejci327de162019-06-14 12:52:07 +02001285
Radek Krejci0af46292019-01-11 16:02:31 +01001286 /* find the preprecompiled feature */
1287 LY_ARRAY_FOR(features, x) {
1288 if (strcmp(features[x].name, feature_p->name)) {
1289 continue;
1290 }
1291 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001292 lysc_update_path(ctx, NULL, "{feature}");
1293 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001294
Radek Krejci0af46292019-01-11 16:02:31 +01001295 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001296 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001297 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001298 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001299 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001300 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001301 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001302 /* check for circular dependency - direct reference first,... */
1303 if (feature == feature->iffeatures[u].features[v]) {
1304 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1305 "Feature \"%s\" is referenced from itself.", feature->name);
1306 return LY_EVALID;
1307 }
1308 /* ... and indirect circular reference */
1309 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1310
Radek Krejci0af46292019-01-11 16:02:31 +01001311 /* add itself into the dependants list */
1312 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1313 *df = feature;
1314 }
Radek Krejci19a96102018-11-15 13:38:09 +01001315 }
Radek Krejci19a96102018-11-15 13:38:09 +01001316 }
1317 }
Radek Krejci327de162019-06-14 12:52:07 +02001318 lysc_update_path(ctx, NULL, NULL);
1319 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001320 done:
1321 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001322 }
Radek Krejci0af46292019-01-11 16:02:31 +01001323
1324 LOGINT(ctx->ctx);
1325 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001326}
1327
Radek Krejcib56c7502019-02-13 14:19:54 +01001328/**
1329 * @brief Revert compiled list of features back to the precompiled state.
1330 *
1331 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
Michal Vasko33ff9422020-07-03 09:50:39 +02001332 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001333 *
1334 * @param[in] ctx Compilation context.
1335 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
Michal Vasko33ff9422020-07-03 09:50:39 +02001336 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001337 */
Radek Krejci95710c92019-02-11 15:49:55 +01001338static void
1339lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1340{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001341 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001342
Michal Vasko33ff9422020-07-03 09:50:39 +02001343 /* keep the dis_features list until the complete lys_module is freed */
1344 mod->dis_features = mod->compiled->features;
Radek Krejci95710c92019-02-11 15:49:55 +01001345 mod->compiled->features = NULL;
1346
Michal Vasko33ff9422020-07-03 09:50:39 +02001347 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001348 * which may points into the data being freed here */
Michal Vasko33ff9422020-07-03 09:50:39 +02001349 LY_ARRAY_FOR(mod->dis_features, u) {
1350 LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) {
1351 lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001352 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001353 LY_ARRAY_FREE(mod->dis_features[u].iffeatures);
1354 mod->dis_features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001355
Michal Vasko33ff9422020-07-03 09:50:39 +02001356 LY_ARRAY_FOR(mod->dis_features[u].exts, v) {
1357 lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001358 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001359 LY_ARRAY_FREE(mod->dis_features[u].exts);
1360 mod->dis_features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001361 }
1362}
1363
Radek Krejcia3045382018-11-22 14:30:31 +01001364/**
1365 * @brief Validate and normalize numeric value from a range definition.
1366 * @param[in] ctx Compile context.
1367 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1368 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1369 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1370 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1371 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1372 * @param[in] value String value of the range boundary.
1373 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
1374 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1375 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1376 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001377LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001378range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy)
Radek Krejci19a96102018-11-15 13:38:09 +01001379{
Radek Krejci6cba4292018-11-15 17:33:29 +01001380 size_t fraction = 0, size;
1381
Radek Krejci19a96102018-11-15 13:38:09 +01001382 *len = 0;
1383
1384 assert(value);
1385 /* parse value */
1386 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1387 return LY_EVALID;
1388 }
1389
1390 if ((value[*len] == '-') || (value[*len] == '+')) {
1391 ++(*len);
1392 }
1393
1394 while (isdigit(value[*len])) {
1395 ++(*len);
1396 }
1397
1398 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001399 if (basetype == LY_TYPE_DEC64) {
1400 goto decimal;
1401 } else {
1402 *valcopy = strndup(value, *len);
1403 return LY_SUCCESS;
1404 }
Radek Krejci19a96102018-11-15 13:38:09 +01001405 }
1406 fraction = *len;
1407
1408 ++(*len);
1409 while (isdigit(value[*len])) {
1410 ++(*len);
1411 }
1412
Radek Krejci6cba4292018-11-15 17:33:29 +01001413 if (basetype == LY_TYPE_DEC64) {
1414decimal:
1415 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001416 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001417 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1418 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1419 *len, value, frdigits);
1420 return LY_EINVAL;
1421 }
1422 if (fraction) {
1423 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1424 } else {
1425 size = (*len) + frdigits + 1;
1426 }
1427 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001428 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1429
Radek Krejci6cba4292018-11-15 17:33:29 +01001430 (*valcopy)[size - 1] = '\0';
1431 if (fraction) {
1432 memcpy(&(*valcopy)[0], &value[0], fraction);
1433 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1434 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1435 } else {
1436 memcpy(&(*valcopy)[0], &value[0], *len);
1437 memset(&(*valcopy)[*len], '0', frdigits);
1438 }
Radek Krejci19a96102018-11-15 13:38:09 +01001439 }
1440 return LY_SUCCESS;
1441}
1442
Radek Krejcia3045382018-11-22 14:30:31 +01001443/**
1444 * @brief Check that values in range are in ascendant order.
1445 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001446 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1447 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001448 * @param[in] value Current value to check.
1449 * @param[in] prev_value The last seen value.
1450 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1451 */
Radek Krejci19a96102018-11-15 13:38:09 +01001452static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001453range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001454{
1455 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001456 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001457 return LY_EEXIST;
1458 }
1459 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001460 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001461 return LY_EEXIST;
1462 }
1463 }
1464 return LY_SUCCESS;
1465}
1466
Radek Krejcia3045382018-11-22 14:30:31 +01001467/**
1468 * @brief Set min/max value of the range part.
1469 * @param[in] ctx Compile context.
1470 * @param[in] part Range part structure to fill.
1471 * @param[in] max Flag to distinguish if storing min or max value.
1472 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1473 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1474 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1475 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1476 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001477 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
Radek Krejcia3045382018-11-22 14:30:31 +01001478 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1479 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1480 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1481 * frdigits value), LY_EMEM.
1482 */
Radek Krejci19a96102018-11-15 13:38:09 +01001483static LY_ERR
1484range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr,
Radek Krejci5969f272018-11-23 10:03:58 +01001485 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001486{
1487 LY_ERR ret = LY_SUCCESS;
1488 char *valcopy = NULL;
1489 size_t len;
1490
1491 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001492 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001493 LY_CHECK_GOTO(ret, finalize);
1494 }
1495 if (!valcopy && base_range) {
1496 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001497 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001498 } else {
1499 part->min_64 = base_range->parts[0].min_64;
1500 }
1501 if (!first) {
1502 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1503 }
1504 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001505 }
1506
1507 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001508 case LY_TYPE_INT8: /* range */
1509 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001510 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001511 } else if (max) {
1512 part->max_64 = INT64_C(127);
1513 } else {
1514 part->min_64 = INT64_C(-128);
1515 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001516 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001517 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001518 }
1519 break;
1520 case LY_TYPE_INT16: /* range */
1521 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001522 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001523 } else if (max) {
1524 part->max_64 = INT64_C(32767);
1525 } else {
1526 part->min_64 = INT64_C(-32768);
1527 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001528 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001529 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001530 }
1531 break;
1532 case LY_TYPE_INT32: /* range */
1533 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001534 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001535 } else if (max) {
1536 part->max_64 = INT64_C(2147483647);
1537 } else {
1538 part->min_64 = INT64_C(-2147483648);
1539 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001540 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001541 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001542 }
1543 break;
1544 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001545 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001546 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001547 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001548 max ? &part->max_64 : &part->min_64);
1549 } else if (max) {
1550 part->max_64 = INT64_C(9223372036854775807);
1551 } else {
1552 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1553 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001554 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001555 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001556 }
1557 break;
1558 case LY_TYPE_UINT8: /* range */
1559 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001560 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001561 } else if (max) {
1562 part->max_u64 = UINT64_C(255);
1563 } else {
1564 part->min_u64 = UINT64_C(0);
1565 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001566 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001567 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001568 }
1569 break;
1570 case LY_TYPE_UINT16: /* range */
1571 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001572 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001573 } else if (max) {
1574 part->max_u64 = UINT64_C(65535);
1575 } else {
1576 part->min_u64 = UINT64_C(0);
1577 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001578 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001579 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001580 }
1581 break;
1582 case LY_TYPE_UINT32: /* range */
1583 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001584 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001585 } else if (max) {
1586 part->max_u64 = UINT64_C(4294967295);
1587 } else {
1588 part->min_u64 = UINT64_C(0);
1589 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001590 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001591 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001592 }
1593 break;
1594 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001595 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001596 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001597 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001598 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001599 } else if (max) {
1600 part->max_u64 = UINT64_C(18446744073709551615);
1601 } else {
1602 part->min_u64 = UINT64_C(0);
1603 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001604 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001605 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001606 }
1607 break;
1608 default:
1609 LOGINT(ctx->ctx);
1610 ret = LY_EINT;
1611 }
1612
Radek Krejci5969f272018-11-23 10:03:58 +01001613finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001614 if (ret == LY_EDENIED) {
1615 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1616 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1617 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1618 } else if (ret == LY_EVALID) {
1619 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1620 "Invalid %s restriction - invalid value \"%s\".",
1621 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1622 } else if (ret == LY_EEXIST) {
1623 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1624 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001625 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001626 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001627 } else if (!ret && value) {
1628 *value = *value + len;
1629 }
1630 free(valcopy);
1631 return ret;
1632}
1633
Radek Krejcia3045382018-11-22 14:30:31 +01001634/**
1635 * @brief Compile the parsed range restriction.
1636 * @param[in] ctx Compile context.
1637 * @param[in] range_p Parsed range structure to compile.
1638 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1639 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1640 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1641 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1642 * range restriction must be more restrictive than the base_range.
1643 * @param[in,out] range Pointer to the created current range structure.
1644 * @return LY_ERR value.
1645 */
Radek Krejci19a96102018-11-15 13:38:09 +01001646static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001647lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits,
Radek Krejci19a96102018-11-15 13:38:09 +01001648 struct lysc_range *base_range, struct lysc_range **range)
1649{
1650 LY_ERR ret = LY_EVALID;
1651 const char *expr;
1652 struct lysc_range_part *parts = NULL, *part;
1653 int range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001654 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001655
1656 assert(range);
1657 assert(range_p);
1658
1659 expr = range_p->arg;
1660 while(1) {
1661 if (isspace(*expr)) {
1662 ++expr;
1663 } else if (*expr == '\0') {
1664 if (range_expected) {
1665 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1666 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1667 length_restr ? "length" : "range", range_p->arg);
1668 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001669 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1671 "Invalid %s restriction - unexpected end of the expression (%s).",
1672 length_restr ? "length" : "range", range_p->arg);
1673 goto cleanup;
1674 }
1675 parts_done++;
1676 break;
1677 } else if (!strncmp(expr, "min", 3)) {
1678 if (parts) {
1679 /* min cannot be used elsewhere than in the first part */
1680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1681 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1682 expr - range_p->arg, range_p->arg);
1683 goto cleanup;
1684 }
1685 expr += 3;
1686
1687 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001688 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001689 part->max_64 = part->min_64;
1690 } else if (*expr == '|') {
1691 if (!parts || range_expected) {
1692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1693 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1694 goto cleanup;
1695 }
1696 expr++;
1697 parts_done++;
1698 /* process next part of the expression */
1699 } else if (!strncmp(expr, "..", 2)) {
1700 expr += 2;
1701 while (isspace(*expr)) {
1702 expr++;
1703 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001704 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001705 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1706 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1707 goto cleanup;
1708 }
1709 /* continue expecting the upper boundary */
1710 range_expected = 1;
1711 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1712 /* number */
1713 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001714 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001715 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001716 range_expected = 0;
1717 } else {
1718 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001719 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001720 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001721 part->max_64 = part->min_64;
1722 }
1723
1724 /* continue with possible another expression part */
1725 } else if (!strncmp(expr, "max", 3)) {
1726 expr += 3;
1727 while (isspace(*expr)) {
1728 expr++;
1729 }
1730 if (*expr != '\0') {
1731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1732 length_restr ? "length" : "range", expr);
1733 goto cleanup;
1734 }
1735 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001736 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001737 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001738 range_expected = 0;
1739 } else {
1740 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001741 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001742 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001743 part->min_64 = part->max_64;
1744 }
1745 } else {
1746 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1747 length_restr ? "length" : "range", expr);
1748 goto cleanup;
1749 }
1750 }
1751
1752 /* check with the previous range/length restriction */
1753 if (base_range) {
1754 switch (basetype) {
1755 case LY_TYPE_BINARY:
1756 case LY_TYPE_UINT8:
1757 case LY_TYPE_UINT16:
1758 case LY_TYPE_UINT32:
1759 case LY_TYPE_UINT64:
1760 case LY_TYPE_STRING:
1761 uns = 1;
1762 break;
1763 case LY_TYPE_DEC64:
1764 case LY_TYPE_INT8:
1765 case LY_TYPE_INT16:
1766 case LY_TYPE_INT32:
1767 case LY_TYPE_INT64:
1768 uns = 0;
1769 break;
1770 default:
1771 LOGINT(ctx->ctx);
1772 ret = LY_EINT;
1773 goto cleanup;
1774 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001775 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001776 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1777 goto baseerror;
1778 }
1779 /* current lower bound is not lower than the base */
1780 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1781 /* base has single value */
1782 if (base_range->parts[v].min_64 == parts[u].min_64) {
1783 /* both lower bounds are the same */
1784 if (parts[u].min_64 != parts[u].max_64) {
1785 /* current continues with a range */
1786 goto baseerror;
1787 } else {
1788 /* equal single values, move both forward */
1789 ++v;
1790 continue;
1791 }
1792 } else {
1793 /* base is single value lower than current range, so the
1794 * value from base range is removed in the current,
1795 * move only base and repeat checking */
1796 ++v;
1797 --u;
1798 continue;
1799 }
1800 } else {
1801 /* base is the range */
1802 if (parts[u].min_64 == parts[u].max_64) {
1803 /* current is a single value */
1804 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1805 /* current is behind the base range, so base range is omitted,
1806 * move the base and keep the current for further check */
1807 ++v;
1808 --u;
1809 } /* else it is within the base range, so move the current, but keep the base */
1810 continue;
1811 } else {
1812 /* both are ranges - check the higher bound, the lower was already checked */
1813 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1814 /* higher bound is higher than the current higher bound */
1815 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1816 /* but the current lower bound is also higher, so the base range is omitted,
1817 * continue with the same current, but move the base */
1818 --u;
1819 ++v;
1820 continue;
1821 }
1822 /* current range starts within the base range but end behind it */
1823 goto baseerror;
1824 } else {
1825 /* current range is smaller than the base,
1826 * move current, but stay with the base */
1827 continue;
1828 }
1829 }
1830 }
1831 }
1832 if (u != parts_done) {
1833baseerror:
1834 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1835 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1836 length_restr ? "length" : "range", range_p->arg);
1837 goto cleanup;
1838 }
1839 }
1840
1841 if (!(*range)) {
1842 *range = calloc(1, sizeof **range);
1843 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1844 }
1845
Radek Krejcic8b31002019-01-08 10:24:45 +01001846 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001847 if (range_p->eapptag) {
1848 lydict_remove(ctx->ctx, (*range)->eapptag);
1849 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1850 }
1851 if (range_p->emsg) {
1852 lydict_remove(ctx->ctx, (*range)->emsg);
1853 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1854 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001855 if (range_p->dsc) {
1856 lydict_remove(ctx->ctx, (*range)->dsc);
1857 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1858 }
1859 if (range_p->ref) {
1860 lydict_remove(ctx->ctx, (*range)->ref);
1861 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1862 }
Radek Krejci19a96102018-11-15 13:38:09 +01001863 /* extensions are taken only from the last range by the caller */
1864
1865 (*range)->parts = parts;
1866 parts = NULL;
1867 ret = LY_SUCCESS;
1868cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001869 LY_ARRAY_FREE(parts);
1870
1871 return ret;
1872}
1873
1874/**
1875 * @brief Checks pattern syntax.
1876 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001877 * @param[in] ctx Context.
1878 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001879 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001880 * @param[in,out] pcre2_code Compiled PCRE2 pattern. If NULL, the compiled information used to validate pattern are freed.
Radek Krejcia3045382018-11-22 14:30:31 +01001881 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001882 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001883LY_ERR
1884lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001885{
Michal Vasko40a00082020-05-27 15:20:01 +02001886 int idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001887 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001888 int err_code;
1889 const char *orig_ptr;
1890 PCRE2_SIZE err_offset;
1891 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001892#define URANGE_LEN 19
1893 char *ublock2urange[][2] = {
1894 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1895 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1896 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1897 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1898 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1899 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1900 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1901 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1902 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1903 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1904 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1905 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1906 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1907 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1908 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1909 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1910 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1911 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1912 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1913 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1914 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1915 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1916 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1917 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1918 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1919 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1920 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1921 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1922 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1923 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1924 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1925 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1926 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1927 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1928 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1929 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1930 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1931 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1932 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1933 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1934 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1935 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1936 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1937 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1938 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1939 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1940 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1941 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1942 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1943 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1944 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1945 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1946 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1947 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1948 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1949 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1950 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1951 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1952 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1953 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1954 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1955 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1956 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1957 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1958 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1959 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1960 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1961 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1962 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1963 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1964 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1965 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1966 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1967 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1968 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1969 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1970 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1971 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1972 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1973 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1974 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1975 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1976 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1977 {NULL, NULL}
1978 };
1979
1980 /* adjust the expression to a Perl equivalent
1981 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1982
Michal Vasko40a00082020-05-27 15:20:01 +02001983 /* allocate space for the transformed pattern */
1984 size = strlen(pattern) + 1;
1985 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001986 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001987 perl_regex[0] = '\0';
1988
Michal Vasko40a00082020-05-27 15:20:01 +02001989 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1990 brack = 0;
1991 idx = 0;
1992 orig_ptr = pattern;
1993 while (orig_ptr[0]) {
1994 switch (orig_ptr[0]) {
1995 case '$':
1996 case '^':
1997 if (!brack) {
1998 /* make space for the extra character */
1999 ++size;
2000 perl_regex = ly_realloc(perl_regex, size);
2001 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002002
Michal Vasko40a00082020-05-27 15:20:01 +02002003 /* print escape slash */
2004 perl_regex[idx] = '\\';
2005 ++idx;
2006 }
2007 break;
2008 case '[':
2009 /* must not be escaped */
2010 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2011 ++brack;
2012 }
2013 break;
2014 case ']':
2015 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2016 /* pattern was checked and compiled already */
2017 assert(brack);
2018 --brack;
2019 }
2020 break;
2021 default:
2022 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002023 }
Michal Vasko40a00082020-05-27 15:20:01 +02002024
2025 /* copy char */
2026 perl_regex[idx] = orig_ptr[0];
2027
2028 ++idx;
2029 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002030 }
Michal Vasko40a00082020-05-27 15:20:01 +02002031 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002032
2033 /* substitute Unicode Character Blocks with exact Character Ranges */
2034 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2035 start = ptr - perl_regex;
2036
2037 ptr = strchr(ptr, '}');
2038 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002040 pattern, perl_regex + start + 2, "unterminated character property");
2041 free(perl_regex);
2042 return LY_EVALID;
2043 }
2044 end = (ptr - perl_regex) + 1;
2045
2046 /* need more space */
2047 if (end - start < URANGE_LEN) {
2048 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002049 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002050 }
2051
2052 /* find our range */
2053 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2054 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2055 break;
2056 }
2057 }
2058 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002059 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002060 pattern, perl_regex + start + 5, "unknown block name");
2061 free(perl_regex);
2062 return LY_EVALID;
2063 }
2064
2065 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
Michal Vasko40a00082020-05-27 15:20:01 +02002066 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002067 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002068 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002069 }
2070 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002071 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002072 }
2073 }
Michal Vasko40a00082020-05-27 15:20:01 +02002074 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002075 /* skip brackets */
2076 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2077 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2078 } else {
2079 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2080 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2081 }
2082 }
2083
2084 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002085 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2086 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002087 &err_code, &err_offset, NULL);
2088 if (!code_local) {
2089 PCRE2_UCHAR err_msg[256] = {0};
2090 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002091 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002092 free(perl_regex);
2093 return LY_EVALID;
2094 }
2095 free(perl_regex);
2096
Radek Krejci54579462019-04-30 12:47:06 +02002097 if (code) {
2098 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002099 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002100 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002101 }
2102
2103 return LY_SUCCESS;
2104
2105#undef URANGE_LEN
2106}
2107
Radek Krejcia3045382018-11-22 14:30:31 +01002108/**
2109 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2110 * @param[in] ctx Compile context.
2111 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002112 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2113 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2114 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2115 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2116 */
Radek Krejci19a96102018-11-15 13:38:09 +01002117static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002118lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002119 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2120{
2121 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002122 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002123 LY_ERR ret = LY_SUCCESS;
2124
2125 /* first, copy the patterns from the base type */
2126 if (base_patterns) {
2127 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2128 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2129 }
2130
2131 LY_ARRAY_FOR(patterns_p, u) {
2132 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2133 *pattern = calloc(1, sizeof **pattern);
2134 ++(*pattern)->refcount;
2135
Michal Vasko03ff5a72019-09-11 13:49:33 +02002136 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002137 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002138
2139 if (patterns_p[u].arg[0] == 0x15) {
2140 (*pattern)->inverted = 1;
2141 }
Radek Krejci54579462019-04-30 12:47:06 +02002142 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002143 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2144 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002145 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2146 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002147 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002148 }
2149done:
2150 return ret;
2151}
2152
Radek Krejcia3045382018-11-22 14:30:31 +01002153/**
2154 * @brief map of the possible restrictions combination for the specific built-in type.
2155 */
Radek Krejci19a96102018-11-15 13:38:09 +01002156static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2157 0 /* LY_TYPE_UNKNOWN */,
2158 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002159 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2160 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2161 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2162 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2163 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002164 LYS_SET_BIT /* LY_TYPE_BITS */,
2165 0 /* LY_TYPE_BOOL */,
2166 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2167 0 /* LY_TYPE_EMPTY */,
2168 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2169 LYS_SET_BASE /* LY_TYPE_IDENT */,
2170 LYS_SET_REQINST /* LY_TYPE_INST */,
2171 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002172 LYS_SET_TYPE /* LY_TYPE_UNION */,
2173 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002174 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002175 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002176 LYS_SET_RANGE /* LY_TYPE_INT64 */
2177};
2178
2179/**
2180 * @brief stringification of the YANG built-in data types
2181 */
2182const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2183 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2184 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002185};
2186
Radek Krejcia3045382018-11-22 14:30:31 +01002187/**
2188 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2189 * @param[in] ctx Compile context.
2190 * @param[in] enums_p Array of the parsed enum structures to compile.
2191 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
Radek Krejcia3045382018-11-22 14:30:31 +01002192 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2193 * @param[out] enums Newly created array of the compiled enums information for the current type.
2194 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2195 */
Radek Krejci19a96102018-11-15 13:38:09 +01002196static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002197lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002198 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002199{
2200 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002201 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002202 int32_t value = 0;
2203 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002204 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002205
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002206 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002207 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2208 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2209 return LY_EVALID;
2210 }
2211
2212 LY_ARRAY_FOR(enums_p, u) {
2213 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2214 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002215 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2216 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002217 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002218 if (base_enums) {
2219 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2220 LY_ARRAY_FOR(base_enums, v) {
2221 if (!strcmp(e->name, base_enums[v].name)) {
2222 break;
2223 }
2224 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002225 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002226 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2227 "Invalid %s - derived type adds new item \"%s\".",
2228 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2229 return LY_EVALID;
2230 }
2231 match = v;
2232 }
2233
2234 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002235 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002236 if (enums_p[u].flags & LYS_SET_VALUE) {
2237 e->value = (int32_t)enums_p[u].value;
2238 if (!u || e->value >= value) {
2239 value = e->value + 1;
2240 }
2241 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002242 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002243 if (e->value == (*enums)[v].value) {
2244 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2245 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2246 e->value, e->name, (*enums)[v].name);
2247 return LY_EVALID;
2248 }
2249 }
2250 } else if (base_enums) {
2251 /* inherit the assigned value */
2252 e->value = base_enums[match].value;
2253 if (!u || e->value >= value) {
2254 value = e->value + 1;
2255 }
2256 } else {
2257 /* assign value automatically */
2258 if (u && value == INT32_MIN) {
2259 /* counter overflow */
2260 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2261 "Invalid enumeration - it is not possible to auto-assign enum value for "
2262 "\"%s\" since the highest value is already 2147483647.", e->name);
2263 return LY_EVALID;
2264 }
2265 e->value = value++;
2266 }
2267 } else { /* LY_TYPE_BITS */
2268 if (enums_p[u].flags & LYS_SET_VALUE) {
2269 e->value = (int32_t)enums_p[u].value;
2270 if (!u || (uint32_t)e->value >= position) {
2271 position = (uint32_t)e->value + 1;
2272 }
2273 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002274 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002275 if (e->value == (*enums)[v].value) {
2276 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2277 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2278 (uint32_t)e->value, e->name, (*enums)[v].name);
2279 return LY_EVALID;
2280 }
2281 }
2282 } else if (base_enums) {
2283 /* inherit the assigned value */
2284 e->value = base_enums[match].value;
2285 if (!u || (uint32_t)e->value >= position) {
2286 position = (uint32_t)e->value + 1;
2287 }
2288 } else {
2289 /* assign value automatically */
2290 if (u && position == 0) {
2291 /* counter overflow */
2292 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2293 "Invalid bits - it is not possible to auto-assign bit position for "
2294 "\"%s\" since the highest value is already 4294967295.", e->name);
2295 return LY_EVALID;
2296 }
2297 e->value = position++;
2298 }
2299 }
2300
2301 if (base_enums) {
2302 /* the assigned values must not change from the derived type */
2303 if (e->value != base_enums[match].value) {
2304 if (basetype == LY_TYPE_ENUM) {
2305 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2306 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2307 e->name, base_enums[match].value, e->value);
2308 } else {
2309 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2310 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2311 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2312 }
2313 return LY_EVALID;
2314 }
2315 }
2316
Radek Krejciec4da802019-05-02 13:02:41 +02002317 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002318 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM : LYEXT_PAR_TYPE_BIT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002319
2320 if (basetype == LY_TYPE_BITS) {
2321 /* keep bits ordered by position */
2322 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2323 if (v != u) {
2324 memcpy(&storage, e, sizeof *e);
2325 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2326 memcpy(&(*enums)[v], &storage, sizeof storage);
2327 }
2328 }
2329 }
2330
2331done:
2332 return ret;
2333}
2334
Radek Krejcia3045382018-11-22 14:30:31 +01002335/**
2336 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2337 *
2338 * path-arg = absolute-path / relative-path
2339 * absolute-path = 1*("/" (node-identifier *path-predicate))
2340 * relative-path = 1*(".." "/") descendant-path
2341 *
2342 * @param[in,out] path Path to parse.
2343 * @param[out] prefix Prefix of the token, NULL if there is not any.
2344 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2345 * @param[out] name Name of the token.
2346 * @param[out] nam_len Length of the name.
2347 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2348 * must not be changed between consecutive calls. -1 if the
2349 * path is absolute.
2350 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2351 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2352 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002353LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002354lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2355 int *parent_times, int *has_predicate)
2356{
2357 int par_times = 0;
2358
2359 assert(path && *path);
2360 assert(parent_times);
2361 assert(prefix);
2362 assert(prefix_len);
2363 assert(name);
2364 assert(name_len);
2365 assert(has_predicate);
2366
2367 *prefix = NULL;
2368 *prefix_len = 0;
2369 *name = NULL;
2370 *name_len = 0;
2371 *has_predicate = 0;
2372
2373 if (!*parent_times) {
2374 if (!strncmp(*path, "..", 2)) {
2375 *path += 2;
2376 ++par_times;
2377 while (!strncmp(*path, "/..", 3)) {
2378 *path += 3;
2379 ++par_times;
2380 }
2381 }
2382 if (par_times) {
2383 *parent_times = par_times;
2384 } else {
2385 *parent_times = -1;
2386 }
2387 }
2388
2389 if (**path != '/') {
2390 return LY_EINVAL;
2391 }
2392 /* skip '/' */
2393 ++(*path);
2394
2395 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002396 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002397
2398 if ((**path == '/' && (*path)[1]) || !**path) {
2399 /* path continues by another token or this is the last token */
2400 return LY_SUCCESS;
2401 } else if ((*path)[0] != '[') {
2402 /* unexpected character */
2403 return LY_EINVAL;
2404 } else {
2405 /* predicate starting with [ */
2406 *has_predicate = 1;
2407 return LY_SUCCESS;
2408 }
2409}
2410
2411/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002412 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2413 *
2414 * The set of features used for target must be a subset of features used for the leafref.
2415 * This is not a perfect, we should compare the truth tables but it could require too much resources
2416 * and RFC 7950 does not require it explicitely, so we simplify that.
2417 *
2418 * @param[in] refnode The leafref node.
2419 * @param[in] target Tha target node of the leafref.
2420 * @return LY_SUCCESS or LY_EVALID;
2421 */
2422static LY_ERR
2423lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2424{
2425 LY_ERR ret = LY_EVALID;
2426 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002427 LY_ARRAY_COUNT_TYPE u, v, count;
Radek Krejci58d171e2018-11-23 13:50:55 +01002428 struct ly_set features = {0};
2429
2430 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002431 if (iter->iffeatures) {
2432 LY_ARRAY_FOR(iter->iffeatures, u) {
2433 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2434 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002435 }
2436 }
2437 }
2438 }
2439
2440 /* we should have, in features set, a superset of features applicable to the target node.
2441 * So when adding features applicable to the target into the features set, we should not be
2442 * able to actually add any new feature, otherwise it is not a subset of features applicable
2443 * to the leafref itself. */
2444 count = features.count;
2445 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002446 if (iter->iffeatures) {
2447 LY_ARRAY_FOR(iter->iffeatures, u) {
2448 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2449 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002450 /* new feature was added (or LY_EMEM) */
2451 goto cleanup;
2452 }
2453 }
2454 }
2455 }
2456 }
2457 ret = LY_SUCCESS;
2458
2459cleanup:
2460 ly_set_erase(&features, NULL);
2461 return ret;
2462}
2463
Michal Vasko004d3152020-06-11 19:59:22 +02002464static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2465 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2466 struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002467
Radek Krejcia3045382018-11-22 14:30:31 +01002468/**
2469 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2470 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002471 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2472 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2473 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2474 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002475 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002476 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002477 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002478 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2479 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2480 * @param[out] type Newly created type structure with the filled information about the type.
2481 * @return LY_ERR value.
2482 */
Radek Krejci19a96102018-11-15 13:38:09 +01002483static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002484lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2485 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2486 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2487 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002488{
2489 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002490 struct lysc_type_bin *bin;
2491 struct lysc_type_num *num;
2492 struct lysc_type_str *str;
2493 struct lysc_type_bits *bits;
2494 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002495 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002496 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002497 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002498 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002499
2500 switch (basetype) {
2501 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002502 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002503
2504 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002505 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002506 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2507 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002508 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002509 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002510 }
2511 }
2512
2513 if (tpdfname) {
2514 type_p->compiled = *type;
2515 *type = calloc(1, sizeof(struct lysc_type_bin));
2516 }
2517 break;
2518 case LY_TYPE_BITS:
2519 /* RFC 7950 9.7 - bits */
2520 bits = (struct lysc_type_bits*)(*type);
2521 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002522 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002523 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2524 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002525 }
2526
Radek Krejci555cb5b2018-11-16 14:54:33 +01002527 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002528 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002529 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002530 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002531 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002532 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002533 free(*type);
2534 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002535 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002536 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002537 }
2538
2539 if (tpdfname) {
2540 type_p->compiled = *type;
2541 *type = calloc(1, sizeof(struct lysc_type_bits));
2542 }
2543 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002544 case LY_TYPE_DEC64:
2545 dec = (struct lysc_type_dec*)(*type);
2546
2547 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002548 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002549 if (!type_p->fraction_digits) {
2550 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002552 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002553 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002554 free(*type);
2555 *type = NULL;
2556 }
2557 return LY_EVALID;
2558 }
2559 } else if (type_p->fraction_digits) {
2560 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002561 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002562 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2563 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002564 tpdfname);
2565 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002566 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2567 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002568 free(*type);
2569 *type = NULL;
2570 }
2571 return LY_EVALID;
2572 }
2573 dec->fraction_digits = type_p->fraction_digits;
2574
2575 /* RFC 7950 9.2.4 - range */
2576 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002577 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2578 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002579 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002580 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002581 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002582 }
2583
2584 if (tpdfname) {
2585 type_p->compiled = *type;
2586 *type = calloc(1, sizeof(struct lysc_type_dec));
2587 }
2588 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002589 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002590 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002591
2592 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002593 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002594 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2595 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002596 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002597 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002598 }
2599 } else if (base && ((struct lysc_type_str*)base)->length) {
2600 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2601 }
2602
2603 /* RFC 7950 9.4.5 - pattern */
2604 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002605 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002606 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002607 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2608 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2609 }
2610
2611 if (tpdfname) {
2612 type_p->compiled = *type;
2613 *type = calloc(1, sizeof(struct lysc_type_str));
2614 }
2615 break;
2616 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002617 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002618
2619 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002620 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002621 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002622 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002623 }
2624
Radek Krejci555cb5b2018-11-16 14:54:33 +01002625 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002626 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002627 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002628 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002629 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002630 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002631 free(*type);
2632 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002633 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002634 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002635 }
2636
2637 if (tpdfname) {
2638 type_p->compiled = *type;
2639 *type = calloc(1, sizeof(struct lysc_type_enum));
2640 }
2641 break;
2642 case LY_TYPE_INT8:
2643 case LY_TYPE_UINT8:
2644 case LY_TYPE_INT16:
2645 case LY_TYPE_UINT16:
2646 case LY_TYPE_INT32:
2647 case LY_TYPE_UINT32:
2648 case LY_TYPE_INT64:
2649 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002650 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002651
2652 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002653 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002654 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2655 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002656 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002657 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002658 }
2659 }
2660
2661 if (tpdfname) {
2662 type_p->compiled = *type;
2663 *type = calloc(1, sizeof(struct lysc_type_num));
2664 }
2665 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002666 case LY_TYPE_IDENT:
2667 idref = (struct lysc_type_identityref*)(*type);
2668
2669 /* RFC 7950 9.10.2 - base */
2670 if (type_p->bases) {
2671 if (base) {
2672 /* only the directly derived identityrefs can contain base specification */
2673 if (tpdfname) {
2674 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002675 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002676 tpdfname);
2677 } else {
2678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002679 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002680 free(*type);
2681 *type = NULL;
2682 }
2683 return LY_EVALID;
2684 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002685 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002686 }
2687
2688 if (!base && !type_p->flags) {
2689 /* type derived from identityref built-in type must contain at least one base */
2690 if (tpdfname) {
2691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2692 } else {
2693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2694 free(*type);
2695 *type = NULL;
2696 }
2697 return LY_EVALID;
2698 }
2699
2700 if (tpdfname) {
2701 type_p->compiled = *type;
2702 *type = calloc(1, sizeof(struct lysc_type_identityref));
2703 }
2704 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002705 case LY_TYPE_LEAFREF:
Michal Vasko004d3152020-06-11 19:59:22 +02002706 lref = (struct lysc_type_leafref*)*type;
2707
Radek Krejcia3045382018-11-22 14:30:31 +01002708 /* RFC 7950 9.9.3 - require-instance */
2709 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002710 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002711 if (tpdfname) {
2712 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2713 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2714 } else {
2715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2716 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2717 free(*type);
2718 *type = NULL;
2719 }
2720 return LY_EVALID;
2721 }
Michal Vasko004d3152020-06-11 19:59:22 +02002722 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002723 } else if (base) {
2724 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002725 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002726 } else {
2727 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002728 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002729 }
2730 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002731 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2732 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002733 } else if (base) {
Michal Vasko004d3152020-06-11 19:59:22 +02002734 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path);
2735 lref->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002736 } else if (tpdfname) {
2737 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2738 return LY_EVALID;
2739 } else {
2740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2741 free(*type);
2742 *type = NULL;
2743 return LY_EVALID;
2744 }
2745 if (tpdfname) {
2746 type_p->compiled = *type;
2747 *type = calloc(1, sizeof(struct lysc_type_leafref));
2748 }
2749 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002750 case LY_TYPE_INST:
2751 /* RFC 7950 9.9.3 - require-instance */
2752 if (type_p->flags & LYS_SET_REQINST) {
2753 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2754 } else {
2755 /* default is true */
2756 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2757 }
2758
2759 if (tpdfname) {
2760 type_p->compiled = *type;
2761 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2762 }
2763 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002764 case LY_TYPE_UNION:
2765 un = (struct lysc_type_union*)(*type);
2766
2767 /* RFC 7950 7.4 - type */
2768 if (type_p->types) {
2769 if (base) {
2770 /* only the directly derived union can contain types specification */
2771 if (tpdfname) {
2772 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2773 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2774 tpdfname);
2775 } else {
2776 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2777 "Invalid type substatement for the type not directly derived from union built-in type.");
2778 free(*type);
2779 *type = NULL;
2780 }
2781 return LY_EVALID;
2782 }
2783 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002784 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2785 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002786 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
2787 &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002788 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2789 /* add space for additional types from the union subtype */
2790 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002791 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t*)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1,
Radek Krejcic4fa0292020-05-14 10:54:49 +02002792 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002793
2794 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002795 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002796 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2797 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2798 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2799 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +02002800 lref = (struct lysc_type_leafref *)un->types[u + additional];
2801
2802 lref->basetype = LY_TYPE_LEAFREF;
2803 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path);
2804 lref->refcount = 1;
2805 lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2806 lref->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002807 /* TODO extensions */
2808
2809 } else {
2810 un->types[u + additional] = un_aux->types[v];
2811 ++un_aux->types[v]->refcount;
2812 }
2813 ++additional;
2814 LY_ARRAY_INCREMENT(un->types);
2815 }
2816 /* compensate u increment in main loop */
2817 --additional;
2818
2819 /* free the replaced union subtype */
2820 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2821 } else {
2822 LY_ARRAY_INCREMENT(un->types);
2823 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002824 }
2825 }
2826
2827 if (!base && !type_p->flags) {
2828 /* type derived from union built-in type must contain at least one type */
2829 if (tpdfname) {
2830 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2831 } else {
2832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2833 free(*type);
2834 *type = NULL;
2835 }
2836 return LY_EVALID;
2837 }
2838
2839 if (tpdfname) {
2840 type_p->compiled = *type;
2841 *type = calloc(1, sizeof(struct lysc_type_union));
2842 }
2843 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002844 case LY_TYPE_BOOL:
2845 case LY_TYPE_EMPTY:
2846 case LY_TYPE_UNKNOWN: /* just to complete switch */
2847 break;
2848 }
2849 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2850done:
2851 return ret;
2852}
2853
Radek Krejcia3045382018-11-22 14:30:31 +01002854/**
2855 * @brief Compile information about the leaf/leaf-list's type.
2856 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002857 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2858 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2859 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2860 * @param[in] context_name Name of the context node or referencing typedef for logging.
2861 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002862 * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002863 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002864 * @return LY_ERR value.
2865 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002866static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002867lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2868 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2869 struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002870{
2871 LY_ERR ret = LY_SUCCESS;
2872 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002873 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002874 struct type_context {
2875 const struct lysp_tpdf *tpdf;
2876 struct lysp_node *node;
2877 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002878 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002879 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002880 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002881 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002882 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002883 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002884
2885 (*type) = NULL;
2886
2887 tctx = calloc(1, sizeof *tctx);
2888 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002889 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002890 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2891 ret == LY_SUCCESS;
2892 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2893 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2894 if (basetype) {
2895 break;
2896 }
2897
2898 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002899 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2900 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002901 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2902
Radek Krejcicdfecd92018-11-26 11:27:32 +01002903 if (units && !*units) {
2904 /* inherit units */
2905 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2906 }
Radek Krejci01342af2019-01-03 15:18:08 +01002907 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002908 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002909 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02002910 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002911 }
Radek Krejci01342af2019-01-03 15:18:08 +01002912 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002913 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2914 break;
2915 }
2916
Radek Krejci19a96102018-11-15 13:38:09 +01002917 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002918 /* it is not necessary to continue, the rest of the chain was already compiled,
2919 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002920 basetype = tctx->tpdf->type.compiled->basetype;
2921 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002922 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002923 dummyloops = 1;
2924 goto preparenext;
2925 } else {
2926 tctx = NULL;
2927 break;
2928 }
Radek Krejci19a96102018-11-15 13:38:09 +01002929 }
2930
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002931 /* circular typedef reference detection */
2932 for (u = 0; u < tpdf_chain.count; u++) {
2933 /* local part */
2934 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2935 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2936 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2937 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2938 free(tctx);
2939 ret = LY_EVALID;
2940 goto cleanup;
2941 }
2942 }
2943 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2944 /* global part for unions corner case */
2945 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2946 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2947 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2948 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2949 free(tctx);
2950 ret = LY_EVALID;
2951 goto cleanup;
2952 }
2953 }
2954
Radek Krejci19a96102018-11-15 13:38:09 +01002955 /* store information for the following processing */
2956 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2957
Radek Krejcicdfecd92018-11-26 11:27:32 +01002958preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002959 /* prepare next loop */
2960 tctx_prev = tctx;
2961 tctx = calloc(1, sizeof *tctx);
2962 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2963 }
2964 free(tctx);
2965
2966 /* allocate type according to the basetype */
2967 switch (basetype) {
2968 case LY_TYPE_BINARY:
2969 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002970 break;
2971 case LY_TYPE_BITS:
2972 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002973 break;
2974 case LY_TYPE_BOOL:
2975 case LY_TYPE_EMPTY:
2976 *type = calloc(1, sizeof(struct lysc_type));
2977 break;
2978 case LY_TYPE_DEC64:
2979 *type = calloc(1, sizeof(struct lysc_type_dec));
2980 break;
2981 case LY_TYPE_ENUM:
2982 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002983 break;
2984 case LY_TYPE_IDENT:
2985 *type = calloc(1, sizeof(struct lysc_type_identityref));
2986 break;
2987 case LY_TYPE_INST:
2988 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2989 break;
2990 case LY_TYPE_LEAFREF:
2991 *type = calloc(1, sizeof(struct lysc_type_leafref));
2992 break;
2993 case LY_TYPE_STRING:
2994 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002995 break;
2996 case LY_TYPE_UNION:
2997 *type = calloc(1, sizeof(struct lysc_type_union));
2998 break;
2999 case LY_TYPE_INT8:
3000 case LY_TYPE_UINT8:
3001 case LY_TYPE_INT16:
3002 case LY_TYPE_UINT16:
3003 case LY_TYPE_INT32:
3004 case LY_TYPE_UINT32:
3005 case LY_TYPE_INT64:
3006 case LY_TYPE_UINT64:
3007 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003008 break;
3009 case LY_TYPE_UNKNOWN:
3010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3011 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3012 ret = LY_EVALID;
3013 goto cleanup;
3014 }
3015 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003016 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003017 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3018 ly_data_type2str[basetype]);
3019 free(*type);
3020 (*type) = NULL;
3021 ret = LY_EVALID;
3022 goto cleanup;
3023 }
3024
3025 /* get restrictions from the referred typedefs */
3026 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3027 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003028
3029 /* remember the typedef context for circular check */
3030 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3031
Radek Krejci43699232018-11-23 14:59:46 +01003032 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003033 base = tctx->tpdf->type.compiled;
3034 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003035 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003036 /* no change, just use the type information from the base */
3037 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3038 ++base->refcount;
3039 continue;
3040 }
3041
3042 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003043 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3044 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3045 tctx->tpdf->name, ly_data_type2str[basetype]);
3046 ret = LY_EVALID;
3047 goto cleanup;
3048 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3049 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3050 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3051 tctx->tpdf->name, tctx->tpdf->dflt);
3052 ret = LY_EVALID;
3053 goto cleanup;
3054 }
3055
Radek Krejci19a96102018-11-15 13:38:09 +01003056 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003057 /* TODO user type plugins */
3058 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003059 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003060 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, &((struct lysp_tpdf*)tctx->tpdf)->type,
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003061 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003062 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003063 LY_CHECK_GOTO(ret, cleanup);
3064 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003065 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003066 /* remove the processed typedef contexts from the stack for circular check */
3067 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003068
Radek Krejcic5c27e52018-11-15 14:38:11 +01003069 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003070 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003071 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003072 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003073 /* TODO user type plugins */
3074 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003075 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003076 ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003077 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003078 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003079 /* no specific restriction in leaf's type definition, copy from the base */
3080 free(*type);
3081 (*type) = base;
3082 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003083 }
Radek Krejcia1911222019-07-22 17:24:50 +02003084 if (dflt && !(*type)->dflt) {
3085 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003086 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003087 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3088 (*type)->dflt->realtype = (*type);
3089 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3090 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003091 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003092 if (err) {
3093 ly_err_print(err);
3094 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3095 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3096 ly_err_free(err);
3097 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003098 if (ret == LY_EINCOMPLETE) {
3099 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003100 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003101
3102 /* but in general result is so far ok */
3103 ret = LY_SUCCESS;
3104 }
Radek Krejcia1911222019-07-22 17:24:50 +02003105 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003106 }
Radek Krejci19a96102018-11-15 13:38:09 +01003107
Radek Krejci0935f412019-08-20 16:15:18 +02003108 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003109
3110cleanup:
3111 ly_set_erase(&tpdf_chain, free);
3112 return ret;
3113}
3114
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003115/**
3116 * @brief Compile status information of the given node.
3117 *
3118 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3119 * has the status correctly set during the compilation.
3120 *
3121 * @param[in] ctx Compile context
3122 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3123 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3124 * the compatibility with the parent's status value.
3125 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3126 * @return LY_ERR value.
3127 */
3128static LY_ERR
3129lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3130{
3131 /* status - it is not inherited by specification, but it does not make sense to have
3132 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3133 if (!((*node_flags) & LYS_STATUS_MASK)) {
3134 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3135 if ((parent_flags & 0x3) != 0x3) {
3136 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3137 * combination of bits (0x3) which marks the uses_status value */
3138 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3139 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3140 }
3141 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3142 } else {
3143 (*node_flags) |= LYS_STATUS_CURR;
3144 }
3145 } else if (parent_flags & LYS_STATUS_MASK) {
3146 /* check status compatibility with the parent */
3147 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3148 if ((*node_flags) & LYS_STATUS_CURR) {
3149 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3150 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3151 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3152 } else { /* LYS_STATUS_DEPRC */
3153 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3154 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3155 }
3156 return LY_EVALID;
3157 }
3158 }
3159 return LY_SUCCESS;
3160}
3161
Radek Krejci8cce8532019-03-05 11:27:45 +01003162/**
3163 * @brief Check uniqness of the node/action/notification name.
3164 *
3165 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3166 * structures, but they share the namespace so we need to check their name collisions.
3167 *
3168 * @param[in] ctx Compile context.
3169 * @param[in] children List (linked list) of data nodes to go through.
3170 * @param[in] actions List (sized array) of actions or RPCs to go through.
3171 * @param[in] notifs List (sized array) of Notifications to go through.
3172 * @param[in] name Name of the item to find in the given lists.
3173 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3174 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3175 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3176 */
3177static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02003178lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions,
3179 const struct lysc_notif *notifs, const char *name, void *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003180{
3181 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003182 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003183
3184 LY_LIST_FOR(children, iter) {
3185 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3186 goto error;
3187 }
3188 }
3189 LY_ARRAY_FOR(actions, u) {
3190 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3191 goto error;
3192 }
3193 }
3194 LY_ARRAY_FOR(notifs, u) {
3195 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3196 goto error;
3197 }
3198 }
3199 return LY_SUCCESS;
3200error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003201 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003202 return LY_EEXIST;
3203}
3204
Radek Krejciec4da802019-05-02 13:02:41 +02003205static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status);
Radek Krejci19a96102018-11-15 13:38:09 +01003206
Radek Krejcia3045382018-11-22 14:30:31 +01003207/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003208 * @brief Compile parsed RPC/action schema node information.
3209 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003210 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003211 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003212 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3213 * @param[in] uses_status If the RPC/action is being placed instead of uses, here we have the uses's status value (as node's flags).
3214 * Zero means no uses, non-zero value with no status bit set mean the default status.
3215 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3216 */
3217static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003218lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003219 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3220{
3221 LY_ERR ret = LY_SUCCESS;
3222 struct lysp_node *child_p;
3223 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003224 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003225
Radek Krejci327de162019-06-14 12:52:07 +02003226 lysc_update_path(ctx, parent, action_p->name);
3227
Radek Krejci8cce8532019-03-05 11:27:45 +01003228 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3229 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3230 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3231 action_p->name, action)) {
3232 return LY_EVALID;
3233 }
3234
Radek Krejciec4da802019-05-02 13:02:41 +02003235 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003236 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003237 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003238 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003239 return LY_EVALID;
3240 }
3241
Michal Vasko1bf09392020-03-27 12:38:10 +01003242 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003243 action->module = ctx->mod;
3244 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003245 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003246 action->sp = action_p;
3247 }
3248 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3249
3250 /* status - it is not inherited by specification, but it does not make sense to have
3251 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003252 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003253
3254 DUP_STRING(ctx->ctx, action_p->name, action->name);
3255 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3256 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003257 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003258 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003259
3260 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003261 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003262 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003263 COMPILE_EXTS_GOTO(ctx, action_p->input.exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003264 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003265 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003266 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003267 }
Radek Krejci327de162019-06-14 12:52:07 +02003268 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003269 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003270
3271 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003272 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003273 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003274 COMPILE_EXTS_GOTO(ctx, action_p->output.exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003275 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003276 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003277 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003278 }
Radek Krejci327de162019-06-14 12:52:07 +02003279 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003280 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003281
3282 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3283 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003284 ly_set_add(&ctx->xpath, action, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003285 }
3286
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003287cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003288 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003289 return ret;
3290}
3291
3292/**
Radek Krejci43981a32019-04-12 09:44:11 +02003293 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003294 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003295 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003296 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3297 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3298 * @param[in] uses_status If the Notification is being placed instead of uses, here we have the uses's status value (as node's flags).
Radek Krejcifc11bd72019-04-11 16:00:05 +02003299 * Zero means no uses, non-zero value with no status bit set mean the default status.
3300 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3301 */
3302static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003303lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003304 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3305{
3306 LY_ERR ret = LY_SUCCESS;
3307 struct lysp_node *child_p;
3308 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003309 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003310
Radek Krejci327de162019-06-14 12:52:07 +02003311 lysc_update_path(ctx, parent, notif_p->name);
3312
Radek Krejcifc11bd72019-04-11 16:00:05 +02003313 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3314 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3315 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3316 notif_p->name, notif)) {
3317 return LY_EVALID;
3318 }
3319
Radek Krejciec4da802019-05-02 13:02:41 +02003320 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003321 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3322 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003323 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003324 return LY_EVALID;
3325 }
3326
3327 notif->nodetype = LYS_NOTIF;
3328 notif->module = ctx->mod;
3329 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003330 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003331 notif->sp = notif_p;
3332 }
3333 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3334
3335 /* status - it is not inherited by specification, but it does not make sense to have
3336 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3337 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3338
3339 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3340 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3341 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003342 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003343 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003344 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3345 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003346 ly_set_add(&ctx->xpath, notif, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003347 }
Radek Krejci0935f412019-08-20 16:15:18 +02003348 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003349
Radek Krejciec4da802019-05-02 13:02:41 +02003350 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003351 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003352 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003353 }
3354
Radek Krejci327de162019-06-14 12:52:07 +02003355 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003356cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003357 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003358 return ret;
3359}
3360
3361/**
Radek Krejcia3045382018-11-22 14:30:31 +01003362 * @brief Compile parsed container node information.
3363 * @param[in] ctx Compile context
3364 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003365 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3366 * is enriched with the container-specific information.
3367 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3368 */
Radek Krejci19a96102018-11-15 13:38:09 +01003369static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003370lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003371{
3372 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3373 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3374 struct lysp_node *child_p;
3375 unsigned int u;
3376 LY_ERR ret = LY_SUCCESS;
3377
Radek Krejcife909632019-02-12 15:34:42 +01003378 if (cont_p->presence) {
3379 cont->flags |= LYS_PRESENCE;
3380 }
3381
Radek Krejci19a96102018-11-15 13:38:09 +01003382 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003383 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003384 }
3385
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003386 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003387 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3388 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003389 ly_set_add(&ctx->xpath, cont, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003390 }
Radek Krejciec4da802019-05-02 13:02:41 +02003391 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3392 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003393
3394done:
3395 return ret;
3396}
3397
Radek Krejci33f72892019-02-21 10:36:58 +01003398/*
3399 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3400 * @param[in] ctx Compile context.
3401 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3402 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003403 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3404 * @return LY_ERR value.
3405 */
3406static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003407lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, struct lysc_node_leaf *leaf)
Radek Krejci33f72892019-02-21 10:36:58 +01003408{
Radek Krejci33f72892019-02-21 10:36:58 +01003409 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3410
Radek Krejciec4da802019-05-02 13:02:41 +02003411 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, &leaf->type,
Radek Krejci33f72892019-02-21 10:36:58 +01003412 leaf->units ? NULL : &leaf->units));
3413 if (leaf->nodetype == LYS_LEAFLIST) {
3414 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003415 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3416 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003417 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003418 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3419 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3420 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3421 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003422 LY_ARRAY_INCREMENT(llist->dflts);
3423 }
3424 } else {
3425 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003426 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003427 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3428 leaf->dflt->realtype = leaf->type->dflt->realtype;
3429 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3430 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003431 }
3432 }
3433 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3434 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Michal Vasko004d3152020-06-11 19:59:22 +02003435 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003436 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003437 LY_ARRAY_COUNT_TYPE u;
Radek Krejci33f72892019-02-21 10:36:58 +01003438 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3439 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3440 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Michal Vasko004d3152020-06-11 19:59:22 +02003441 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003442 }
3443 }
3444 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003445 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3446 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3447 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3448 return LY_EVALID;
3449 }
3450 }
3451
Radek Krejci33f72892019-02-21 10:36:58 +01003452 return LY_SUCCESS;
3453}
3454
Radek Krejcia3045382018-11-22 14:30:31 +01003455/**
3456 * @brief Compile parsed leaf node information.
3457 * @param[in] ctx Compile context
3458 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003459 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3460 * is enriched with the leaf-specific information.
3461 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3462 */
Radek Krejci19a96102018-11-15 13:38:09 +01003463static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003464lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003465{
3466 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3467 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3468 unsigned int u;
3469 LY_ERR ret = LY_SUCCESS;
3470
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003471 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003472 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3473 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003474 ly_set_add(&ctx->xpath, leaf, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003475 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003476 if (leaf_p->units) {
3477 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3478 leaf->flags |= LYS_SET_UNITS;
3479 }
Radek Krejcia1911222019-07-22 17:24:50 +02003480
3481 /* the dflt member is just filled to avoid getting the default value from the type */
3482 leaf->dflt = (void*)leaf_p->dflt;
3483 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003484 if (ret) {
3485 leaf->dflt = NULL;
3486 return ret;
3487 }
Radek Krejcia1911222019-07-22 17:24:50 +02003488
Radek Krejciccd20f12019-02-15 14:12:27 +01003489 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003490 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003491 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003492 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3493 leaf->dflt->realtype = leaf->type;
3494 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3495 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003496 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003497 leaf->dflt->realtype->refcount++;
3498 if (err) {
3499 ly_err_print(err);
3500 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3501 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3502 ly_err_free(err);
3503 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003504 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003505 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003506 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003507
3508 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003509 ret = LY_SUCCESS;
3510 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003511 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003512 leaf->flags |= LYS_SET_DFLT;
3513 }
Radek Krejci43699232018-11-23 14:59:46 +01003514
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003515
Radek Krejci19a96102018-11-15 13:38:09 +01003516done:
3517 return ret;
3518}
3519
Radek Krejcia3045382018-11-22 14:30:31 +01003520/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003521 * @brief Compile parsed leaf-list node information.
3522 * @param[in] ctx Compile context
3523 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003524 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3525 * is enriched with the leaf-list-specific information.
3526 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3527 */
3528static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003529lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003530{
3531 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3532 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003533 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003534 LY_ERR ret = LY_SUCCESS;
3535
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003536 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003537 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3538 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003539 ly_set_add(&ctx->xpath, llist, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003540 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003541 if (llist_p->units) {
3542 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3543 llist->flags |= LYS_SET_UNITS;
3544 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003545
Radek Krejcia1911222019-07-22 17:24:50 +02003546 /* the dflts member is just filled to avoid getting the default value from the type */
3547 llist->dflts = (void*)llist_p->dflts;
3548 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Michal Vasko6a044b22020-01-15 12:25:39 +01003549 if (ret != LY_SUCCESS) {
3550 /* make sure the defaults are freed correctly */
3551 if (llist_p->dflts) {
3552 llist->dflts = NULL;
3553 }
3554 return ret;
3555 }
3556
Radek Krejci0e5d8382018-11-28 16:37:53 +01003557 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003558 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003559 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
3560 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003561 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003562 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003563 LY_ARRAY_INCREMENT(llist->dflts_mods);
3564 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003565 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003566 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3567 llist->dflts[u]->realtype = llist->type;
3568 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3569 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003570 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003571 llist->dflts[u]->realtype->refcount++;
3572 if (err) {
3573 ly_err_print(err);
3574 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3575 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3576 ly_err_free(err);
3577 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003578 if (ret == LY_EINCOMPLETE) {
3579 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003580 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003581
3582 /* but in general result is so far ok */
3583 ret = LY_SUCCESS;
3584 }
Radek Krejcia1911222019-07-22 17:24:50 +02003585 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003586 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003587 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003588 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003589 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02003590 /* configuration data values must be unique - so check the default values */
3591 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003592 for (v = u + 1; v < LY_ARRAY_COUNT(llist->dflts); ++v) {
Radek Krejcia1911222019-07-22 17:24:50 +02003593 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3594 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003595 const char *val = llist->type->plugin->print(llist->dflts[v], LYD_XML, lys_get_prefix, llist->dflts_mods[v], &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +02003596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3597 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3598 if (dynamic) {
3599 free((char*)val);
3600 }
3601 return LY_EVALID;
3602 }
3603 }
3604 }
3605 }
3606
3607 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
Radek Krejci0e5d8382018-11-28 16:37:53 +01003608
3609 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003610 if (llist->min) {
3611 llist->flags |= LYS_MAND_TRUE;
3612 }
Radek Krejcib7408632018-11-28 17:12:11 +01003613 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003614
Radek Krejci0e5d8382018-11-28 16:37:53 +01003615done:
3616 return ret;
3617}
3618
3619/**
Radek Krejci7af64242019-02-18 13:07:53 +01003620 * @brief Compile information about list's uniques.
3621 * @param[in] ctx Compile context.
3622 * @param[in] context_module Module where the prefixes are going to be resolved.
3623 * @param[in] uniques Sized array list of unique statements.
3624 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3625 * @return LY_ERR value.
3626 */
3627static LY_ERR
3628lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3629{
3630 LY_ERR ret = LY_SUCCESS;
3631 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003632 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003633 const char *keystr, *delim;
3634 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003635 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7af64242019-02-18 13:07:53 +01003636 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003637 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003638
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003639 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003640 config = -1;
3641 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3642 keystr = uniques[v];
3643 while (keystr) {
3644 delim = strpbrk(keystr, " \t\n");
3645 if (delim) {
3646 len = delim - keystr;
3647 while (isspace(*delim)) {
3648 ++delim;
3649 }
3650 } else {
3651 len = strlen(keystr);
3652 }
3653
3654 /* unique node must be present */
3655 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003656 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3657 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003658 if (ret != LY_SUCCESS) {
3659 if (ret == LY_EDENIED) {
3660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003661 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003662 len, keystr, lys_nodetype2str((*key)->nodetype));
3663 }
3664 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003665 } else if (flags) {
3666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3667 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003668 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003669 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003670 }
3671
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003672
Radek Krejci7af64242019-02-18 13:07:53 +01003673 /* all referenced leafs must be of the same config type */
3674 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003676 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003677 return LY_EVALID;
3678 } else if ((*key)->flags & LYS_CONFIG_W) {
3679 config = 1;
3680 } else { /* LYS_CONFIG_R */
3681 config = 0;
3682 }
3683
Michal Vasko14654712020-02-06 08:35:21 +01003684 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3685 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3686 if (parent->nodetype == LYS_LIST) {
3687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3688 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3689 return LY_EVALID;
3690 }
3691 }
3692
Radek Krejci7af64242019-02-18 13:07:53 +01003693 /* check status */
3694 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3695 (*key)->flags, (*key)->module, (*key)->name));
3696
3697 /* mark leaf as unique */
3698 (*key)->flags |= LYS_UNIQUE;
3699
3700 /* next unique value in line */
3701 keystr = delim;
3702 }
3703 /* next unique definition */
3704 }
3705
3706 return LY_SUCCESS;
3707}
3708
3709/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003710 * @brief Compile parsed list node information.
3711 * @param[in] ctx Compile context
3712 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003713 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3714 * is enriched with the list-specific information.
3715 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3716 */
3717static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003718lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003719{
3720 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3721 struct lysc_node_list *list = (struct lysc_node_list*)node;
3722 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003723 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003724 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003725 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003726 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003727 LY_ERR ret = LY_SUCCESS;
3728
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003729 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003730 if (list->min) {
3731 list->flags |= LYS_MAND_TRUE;
3732 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003733 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3734
3735 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003736 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003737 }
3738
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003739 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003740 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3741 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003742 ly_set_add(&ctx->xpath, list, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003743 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003744
3745 /* keys */
3746 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3748 return LY_EVALID;
3749 }
3750
3751 /* find all the keys (must be direct children) */
3752 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003753 if (!keystr) {
3754 /* keyless list */
3755 list->flags |= LYS_KEYLESS;
3756 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003757 while (keystr) {
3758 delim = strpbrk(keystr, " \t\n");
3759 if (delim) {
3760 len = delim - keystr;
3761 while (isspace(*delim)) {
3762 ++delim;
3763 }
3764 } else {
3765 len = strlen(keystr);
3766 }
3767
3768 /* key node must be present */
Michal Vaskoe444f752020-02-10 12:20:06 +01003769 key = (struct lysc_node_leaf*)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
Radek Krejci0fe9b512019-07-26 17:51:05 +02003770 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3772 "The list's key \"%.*s\" not found.", len, keystr);
3773 return LY_EVALID;
3774 }
3775 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003776 if (key->flags & LYS_KEY) {
3777 /* the node was already marked as a key */
3778 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3779 "Duplicated key identifier \"%.*s\".", len, keystr);
3780 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003781 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003782
3783 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003784 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003785 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003786 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3787 return LY_EVALID;
3788 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003789 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003790 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003791 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003792 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003793 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003794 return LY_EVALID;
3795 }
3796 } else {
3797 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003798 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003799 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003800 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003801 return LY_EVALID;
3802 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003803 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003804 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003805 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003806 return LY_EVALID;
3807 }
3808 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003809
3810 /* check status */
3811 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003812 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003813
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003814 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003815 if (key->dflt) {
3816 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3817 lysc_type_free(ctx->ctx, key->dflt->realtype);
3818 free(key->dflt);
3819 key->dflt = NULL;
3820 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003821 }
3822 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003823 key->flags |= LYS_KEY;
3824
3825 /* move it to the correct position */
3826 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3827 /* fix links in closest previous siblings of the key */
3828 if (key->next) {
3829 key->next->prev = key->prev;
3830 } else {
3831 /* last child */
3832 list->child->prev = key->prev;
3833 }
3834 if (key->prev->next) {
3835 key->prev->next = key->next;
3836 }
3837 /* fix links in the key */
3838 if (prev_key) {
3839 key->prev = (struct lysc_node*)prev_key;
3840 key->next = prev_key->next;
3841 } else {
3842 key->prev = list->child->prev;
3843 key->next = list->child;
3844 }
3845 /* fix links in closes future siblings of the key */
3846 if (prev_key) {
3847 if (prev_key->next) {
3848 prev_key->next->prev = (struct lysc_node*)key;
3849 } else {
3850 list->child->prev = (struct lysc_node*)key;
3851 }
3852 prev_key->next = (struct lysc_node*)key;
3853 } else {
3854 list->child->prev = (struct lysc_node*)key;
3855 }
3856 /* fix links in parent */
3857 if (!key->prev->next) {
3858 list->child = (struct lysc_node*)key;
3859 }
3860 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003861
3862 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003863 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003864 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003865 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003866 }
3867
3868 /* uniques */
3869 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003870 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003871 }
3872
Radek Krejciec4da802019-05-02 13:02:41 +02003873 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3874 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003875
3876done:
3877 return ret;
3878}
3879
Radek Krejcib56c7502019-02-13 14:19:54 +01003880/**
3881 * @brief Do some checks and set the default choice's case.
3882 *
3883 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3884 *
3885 * @param[in] ctx Compile context.
3886 * @param[in] dflt Name of the default branch. Can contain even the prefix, but it make sense only in case it is the prefix of the module itself,
3887 * not the reference to the imported module.
3888 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3889 * @return LY_ERR value.
3890 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003891static LY_ERR
3892lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3893{
3894 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3895 const char *prefix = NULL, *name;
3896 size_t prefix_len = 0;
3897
3898 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3899 name = strchr(dflt, ':');
3900 if (name) {
3901 prefix = dflt;
3902 prefix_len = name - prefix;
3903 ++name;
3904 } else {
3905 name = dflt;
3906 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003907 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003908 /* prefixed default case make sense only for the prefix of the schema itself */
3909 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3910 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3911 prefix_len, prefix);
3912 return LY_EVALID;
3913 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003914 ch->dflt = (struct lysc_node_case*)lys_find_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
Radek Krejci76b3e962018-12-14 17:01:25 +01003915 if (!ch->dflt) {
3916 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3917 "Default case \"%s\" not found.", dflt);
3918 return LY_EVALID;
3919 }
3920 /* no mandatory nodes directly under the default case */
3921 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003922 if (iter->parent != (struct lysc_node*)ch->dflt) {
3923 break;
3924 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003925 if (iter->flags & LYS_MAND_TRUE) {
3926 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3927 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3928 return LY_EVALID;
3929 }
3930 }
Radek Krejci01342af2019-01-03 15:18:08 +01003931 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003932 return LY_SUCCESS;
3933}
3934
Radek Krejciccd20f12019-02-15 14:12:27 +01003935static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003936lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003937{
3938 struct lys_module *mod;
3939 const char *prefix = NULL, *name;
3940 size_t prefix_len = 0;
3941 struct lysc_node_case *cs;
3942 struct lysc_node *node;
3943
3944 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3945 name = strchr(dflt, ':');
3946 if (name) {
3947 prefix = dflt;
3948 prefix_len = name - prefix;
3949 ++name;
3950 } else {
3951 name = dflt;
3952 }
3953 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3954 if (prefix) {
3955 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3956 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003957 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3958 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003959 return LY_EVALID;
3960 }
3961 } else {
3962 mod = ctx->mod;
3963 }
3964 /* get the default case */
Michal Vaskoe444f752020-02-10 12:20:06 +01003965 cs = (struct lysc_node_case*)lys_find_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
Radek Krejciccd20f12019-02-15 14:12:27 +01003966 if (!cs) {
3967 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003968 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003969 return LY_EVALID;
3970 }
3971
3972 /* check that there is no mandatory node */
3973 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003974 if (node->parent != (struct lysc_node*)cs) {
3975 break;
3976 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003977 if (node->flags & LYS_MAND_TRUE) {
3978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003979 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3980 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003981 return LY_EVALID;
3982 }
3983 }
3984
3985 /* set the default case in choice */
3986 ch->dflt = cs;
3987 cs->flags |= LYS_SET_DFLT;
3988
3989 return LY_SUCCESS;
3990}
3991
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003992/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003993 * @brief Compile parsed choice node information.
3994 * @param[in] ctx Compile context
3995 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003996 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003997 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003998 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3999 */
4000static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004001lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004002{
4003 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4004 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4005 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004006 LY_ERR ret = LY_SUCCESS;
4007
Radek Krejci056d0a82018-12-06 16:57:25 +01004008 LY_LIST_FOR(ch_p->child, child_p) {
4009 if (child_p->nodetype == LYS_CASE) {
4010 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004011 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004012 }
4013 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004014 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004015 }
4016 }
4017
4018 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004019 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004020 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004021 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004022
Radek Krejci9800fb82018-12-13 14:26:23 +01004023 return ret;
4024}
4025
4026/**
4027 * @brief Compile parsed anydata or anyxml node information.
4028 * @param[in] ctx Compile context
4029 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004030 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4031 * is enriched with the any-specific information.
4032 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4033 */
4034static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004035lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004036{
4037 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4038 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4039 unsigned int u;
4040 LY_ERR ret = LY_SUCCESS;
4041
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004042 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004043 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4044 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004045 ly_set_add(&ctx->xpath, any, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004046 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004047
4048 if (any->flags & LYS_CONFIG_W) {
4049 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004050 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004051 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004052done:
4053 return ret;
4054}
4055
Radek Krejcib56c7502019-02-13 14:19:54 +01004056/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004057 * @brief Connect the node into the siblings list and check its name uniqueness.
4058 *
4059 * @param[in] ctx Compile context
4060 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4061 * the choice itself is expected instead of a specific case node.
4062 * @param[in] node Schema node to connect into the list.
4063 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004064 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
Radek Krejci056d0a82018-12-06 16:57:25 +01004065 */
4066static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004067lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004068{
4069 struct lysc_node **children;
4070
4071 if (node->nodetype == LYS_CASE) {
4072 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4073 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004074 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004075 }
4076 if (children) {
4077 if (!(*children)) {
4078 /* first child */
4079 *children = node;
4080 } else if (*children != node) {
4081 /* by the condition in previous branch we cover the choice/case children
4082 * - the children list is shared by the choice and the the first case, in addition
4083 * the first child of each case must be referenced from the case node. So the node is
4084 * actually always already inserted in case it is the first children - so here such
4085 * a situation actually corresponds to the first branch */
4086 /* insert at the end of the parent's children list */
4087 (*children)->prev->next = node;
4088 node->prev = (*children)->prev;
4089 (*children)->prev = node;
4090
4091 /* check the name uniqueness */
4092 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4093 lysc_node_notifs(parent), node->name, node)) {
4094 return LY_EEXIST;
4095 }
4096 }
4097 }
4098 return LY_SUCCESS;
4099}
4100
Radek Krejci95710c92019-02-11 15:49:55 +01004101/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004102 * @brief Prepare the case structure in choice node for the new data node.
4103 *
4104 * It is able to handle implicit as well as explicit cases and the situation when the case has multiple data nodes and the case was already
4105 * created in the choice when the first child was processed.
4106 *
4107 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004108 * @param[in] node_p Node image from the parsed tree. If the case is explicit, it is the LYS_CASE node, but in case of implicit case,
4109 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004110 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4111 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4112 * @return The case structure where the child node belongs to, NULL in case of error. Note that the child is not connected into the siblings list,
4113 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004114 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004115static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004116lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node_choice *ch, struct lysc_node *child)
Radek Krejci056d0a82018-12-06 16:57:25 +01004117{
4118 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004119 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004120 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004121 unsigned int u;
4122 LY_ERR ret;
4123
Radek Krejci95710c92019-02-11 15:49:55 +01004124#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004125 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004126 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004127 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4128 return NULL; \
4129 } \
4130 }
4131
Radek Krejci95710c92019-02-11 15:49:55 +01004132 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4133 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004134
4135 /* we have to add an implicit case node into the parent choice */
4136 cs = calloc(1, sizeof(struct lysc_node_case));
4137 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004138 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004139 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004140 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004141 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4142 /* the case is already present since the child is not its first children */
4143 return (struct lysc_node_case*)ch->cases->prev;
4144 }
Radek Krejci95710c92019-02-11 15:49:55 +01004145 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004146
4147 /* explicit parent case is not present (this is its first child) */
4148 cs = calloc(1, sizeof(struct lysc_node_case));
4149 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004150 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004151 cs->flags = LYS_STATUS_MASK & node_p->flags;
4152 cs->sp = node_p;
4153
Radek Krejcib1b59152019-01-07 13:21:56 +01004154 /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */
Radek Krejcibae745f2019-04-09 16:28:00 +02004155 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004156
4157 if (node_p->when) {
4158 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004159 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004160 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004161
4162 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4163 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004164 ly_set_add(&ctx->xpath, cs, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004165 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004166 }
Radek Krejciec4da802019-05-02 13:02:41 +02004167 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004168 } else {
4169 LOGINT(ctx->ctx);
4170 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004171 }
4172 cs->module = ctx->mod;
4173 cs->prev = (struct lysc_node*)cs;
4174 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004175 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004176 cs->child = child;
4177
4178 return cs;
4179error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004180 if (cs) {
4181 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4182 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004183 return NULL;
4184
4185#undef UNIQUE_CHECK
4186}
4187
Radek Krejcib56c7502019-02-13 14:19:54 +01004188/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004189 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004190 *
4191 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004192 * @param[in] node Target node where the config is supposed to be changed.
4193 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004194 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4195 * done only on the node for which the refine was created. The function applies also recursively to apply the config change
Radek Krejci93dcc392019-02-19 10:43:38 +01004196 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4197 * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01004198 * @return LY_ERR value.
4199 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004200static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004201lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004202 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004203{
4204 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004205 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004206
4207 if (config == (node->flags & LYS_CONFIG_MASK)) {
4208 /* nothing to do */
4209 return LY_SUCCESS;
4210 }
4211
4212 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004213 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004214 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4215 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004216 "Invalid %s of config - configuration node cannot be child of any state data node.",
4217 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004218 return LY_EVALID;
4219 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004220 node->flags |= LYS_SET_CONFIG;
4221 } else {
4222 if (node->flags & LYS_SET_CONFIG) {
4223 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4224 /* setting config flags, but have node with explicit config true */
4225 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004226 "Invalid %s of config - configuration node cannot be child of any state data node.",
4227 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004228 return LY_EVALID;
4229 }
4230 /* do not change config on nodes where the config is explicitely set, this does not apply to
4231 * nodes, which are being changed explicitly (targets of refine or deviation) */
4232 return LY_SUCCESS;
4233 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004234 }
4235 node->flags &= ~LYS_CONFIG_MASK;
4236 node->flags |= config;
4237
4238 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004239 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004240 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004241 }
4242
Radek Krejci76b3e962018-12-14 17:01:25 +01004243 return LY_SUCCESS;
4244}
4245
Radek Krejcib56c7502019-02-13 14:19:54 +01004246/**
4247 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4248 *
4249 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4250 * the flag to such parents from a mandatory children.
4251 *
4252 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4253 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4254 * (mandatory children was removed).
4255 */
Radek Krejcife909632019-02-12 15:34:42 +01004256void
4257lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4258{
4259 struct lysc_node *iter;
4260
4261 if (add) { /* set flag */
4262 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4263 parent = parent->parent) {
4264 parent->flags |= LYS_MAND_TRUE;
4265 }
4266 } else { /* unset flag */
4267 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004268 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004269 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004270 /* there is another mandatory node */
4271 return;
4272 }
4273 }
4274 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4275 parent->flags &= ~LYS_MAND_TRUE;
4276 }
4277 }
4278}
4279
Radek Krejci056d0a82018-12-06 16:57:25 +01004280/**
Radek Krejci3641f562019-02-13 15:38:40 +01004281 * @brief Internal sorting process for the lys_compile_augment_sort().
4282 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4283 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4284 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4285 */
4286static void
4287lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4288{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004289 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004290 size_t len;
4291
4292 len = strlen(aug_p->nodeid);
4293 LY_ARRAY_FOR(result, v) {
4294 if (strlen(result[v]->nodeid) <= len) {
4295 continue;
4296 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004297 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004298 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004299 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004300 break;
4301 }
4302 }
4303 result[v] = aug_p;
4304 LY_ARRAY_INCREMENT(result);
4305}
4306
4307/**
4308 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4309 *
4310 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4311 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4312 *
4313 * @param[in] ctx Compile context.
4314 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4315 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4316 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4317 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4318 * @return LY_ERR value.
4319 */
4320LY_ERR
4321lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4322{
4323 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004324 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004325
4326 assert(augments);
4327
4328 /* get count of the augments in module and all its submodules */
4329 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004330 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004331 }
4332 LY_ARRAY_FOR(inc_p, u) {
4333 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004334 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004335 }
4336 }
4337
4338 if (!count) {
4339 *augments = NULL;
4340 return LY_SUCCESS;
4341 }
4342 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4343
4344 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4345 * together, so there can be even /z/y betwwen them. */
4346 LY_ARRAY_FOR(aug_p, u) {
4347 lys_compile_augment_sort_(&aug_p[u], result);
4348 }
4349 LY_ARRAY_FOR(inc_p, u) {
4350 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4351 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4352 }
4353 }
4354
4355 *augments = result;
4356 return LY_SUCCESS;
4357}
4358
4359/**
4360 * @brief Compile the parsed augment connecting it into its target.
4361 *
4362 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4363 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4364 * are already implemented and compiled.
4365 *
4366 * @param[in] ctx Compile context.
4367 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004368 * @param[in] parent Parent node to provide the augment's context. It is NULL for the top level augments and a node holding uses's
4369 * children in case of the augmenting uses data.
4370 * @return LY_SUCCESS on success.
4371 * @return LY_EVALID on failure.
4372 */
4373LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004374lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004375{
4376 LY_ERR ret = LY_SUCCESS;
4377 struct lysp_node *node_p, *case_node_p;
4378 struct lysc_node *target; /* target target of the augment */
4379 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004380 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004381 struct lys_module **aug_mod;
Radek Krejci3641f562019-02-13 15:38:40 +01004382 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004383 uint16_t flags = 0;
4384 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004385 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004386
Radek Krejci327de162019-06-14 12:52:07 +02004387 lysc_update_path(ctx, NULL, "{augment}");
4388 lysc_update_path(ctx, NULL, aug_p->nodeid);
4389
Radek Krejci7af64242019-02-18 13:07:53 +01004390 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004391 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004392 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004393 if (ret != LY_SUCCESS) {
4394 if (ret == LY_EDENIED) {
4395 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4396 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4397 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4398 }
4399 return LY_EVALID;
4400 }
4401
4402 /* check for mandatory nodes
4403 * - new cases augmenting some choice can have mandatory nodes
4404 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4405 */
Radek Krejci733988a2019-02-15 15:12:44 +01004406 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004407 allow_mandatory = 1;
4408 }
4409
4410 when_shared = NULL;
4411 LY_LIST_FOR(aug_p->child, node_p) {
4412 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4413 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004414 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004415 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4416 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004417 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004418 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4419 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004420 return LY_EVALID;
4421 }
4422
4423 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004424 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004425 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004426 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004427 } else {
4428 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004429 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004430 }
4431 }
Radek Krejciec4da802019-05-02 13:02:41 +02004432 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004433
Radek Krejcife13da42019-02-15 14:51:01 +01004434 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4435 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004436 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004437 /* the compiled node is the last child of the target (but it is a case, so we have to be careful and stop) */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004438 for (node = (struct lysc_node*)lysc_node_children(target, flags); node->next && node->next->parent == node->parent; node = node->next);
Radek Krejci3641f562019-02-13 15:38:40 +01004439 } else if (target->nodetype == LYS_CHOICE) {
4440 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4441 node = ((struct lysc_node_choice*)target)->cases->prev;
4442 } else {
4443 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004444 node = (struct lysc_node*)lysc_node_children(target, flags);
4445 if (!node) {
4446 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4447 break;
4448 }
4449 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004450 }
4451
Radek Krejci733988a2019-02-15 15:12:44 +01004452 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004453 node->flags &= ~LYS_MAND_TRUE;
4454 lys_compile_mandatory_parents(target, 0);
4455 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004456 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004457 return LY_EVALID;
4458 }
4459
4460 /* pass augment's when to all the children */
4461 if (aug_p->when) {
4462 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4463 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004464 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004465 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004466
4467 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4468 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004469 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004470 }
4471
Radek Krejci3641f562019-02-13 15:38:40 +01004472 when_shared = *when;
4473 } else {
4474 ++when_shared->refcount;
4475 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004476
4477 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4478 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004479 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004480 }
Radek Krejci3641f562019-02-13 15:38:40 +01004481 }
4482 }
4483 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004484
Radek Krejciec4da802019-05-02 13:02:41 +02004485 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004486 switch (target->nodetype) {
4487 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004488 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004489 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004490 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004491 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004492 break;
4493 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004494 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004495 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004496 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004497 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004498 break;
4499 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004500 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004501 if (aug_p->actions) {
4502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004503 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4504 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004505 return LY_EVALID;
4506 }
4507 if (aug_p->notifs) {
4508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004509 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004510 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004511 return LY_EVALID;
4512 }
4513 }
Radek Krejci3641f562019-02-13 15:38:40 +01004514
Michal Vaskoa3af5982020-06-29 11:51:37 +02004515 /* add this module into the target module augmented_by */
4516 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4517 *aug_mod = ctx->mod;
4518
Radek Krejci327de162019-06-14 12:52:07 +02004519 lysc_update_path(ctx, NULL, NULL);
4520 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004521
Radek Krejci3641f562019-02-13 15:38:40 +01004522error:
Radek Krejciec4da802019-05-02 13:02:41 +02004523 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004524 return ret;
4525}
4526
4527/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004528 * @brief Apply refined or deviated mandatory flag to the target node.
4529 *
4530 * @param[in] ctx Compile context.
4531 * @param[in] node Target node where the mandatory property is supposed to be changed.
4532 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004533 * @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 +01004534 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4535 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4536 * 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 +01004537 * @return LY_ERR value.
4538 */
4539static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004540lys_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 +01004541{
4542 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004544 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4545 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004546 return LY_EVALID;
4547 }
4548
4549 if (mandatory_flag & LYS_MAND_TRUE) {
4550 /* check if node has default value */
4551 if (node->nodetype & LYS_LEAF) {
4552 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004553 if (refine_flag) {
4554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004555 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004556 return LY_EVALID;
4557 }
Radek Krejcia1911222019-07-22 17:24:50 +02004558 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004559 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004560 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004561
4562 /* update the list of incomplete default values if needed */
4563 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4564
Radek Krejcia1911222019-07-22 17:24:50 +02004565 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4566 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4567 free(leaf->dflt);
4568 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004569 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004570 }
4571 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004572 if (refine_flag) {
4573 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004574 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004575 return LY_EVALID;
4576 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004577 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004578 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004579 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 +01004580 return LY_EVALID;
4581 }
4582
4583 node->flags &= ~LYS_MAND_FALSE;
4584 node->flags |= LYS_MAND_TRUE;
4585 lys_compile_mandatory_parents(node->parent, 1);
4586 } else {
4587 /* make mandatory false */
4588 node->flags &= ~LYS_MAND_TRUE;
4589 node->flags |= LYS_MAND_FALSE;
4590 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004591 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 +01004592 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004593 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004594 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004595 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4596 leaf->dflt->realtype = leaf->type->dflt->realtype;
4597 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4598 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004599 }
4600 }
4601 return LY_SUCCESS;
4602}
4603
4604/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004605 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4606 * If present, also apply uses's modificators.
4607 *
4608 * @param[in] ctx Compile context
4609 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004610 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4611 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4612 * the compile context.
4613 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4614 */
4615static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004616lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004617{
4618 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004619 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004620 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004621 struct lysc_node_container context_node_fake =
4622 {.nodetype = LYS_CONTAINER,
4623 .module = ctx->mod,
4624 .flags = parent ? parent->flags : 0,
4625 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004626 .prev = (struct lysc_node*)&context_node_fake,
4627 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004628 struct lysp_grp *grp = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004629 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004630 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004631 int found;
4632 const char *id, *name, *prefix;
4633 size_t prefix_len, name_len;
4634 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004635 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004636 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004637 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004638 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004639 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004640 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004641 struct lysp_augment **augments = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004642 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004643 struct lysc_notif **notifs = NULL;
4644 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004645
4646 /* search for the grouping definition */
4647 found = 0;
4648 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004649 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004650 if (prefix) {
4651 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4652 if (!mod) {
4653 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004654 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004655 return LY_EVALID;
4656 }
4657 } else {
4658 mod = ctx->mod_def;
4659 }
4660 if (mod == ctx->mod_def) {
4661 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004662 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004663 LY_ARRAY_FOR(grp, u) {
4664 if (!strcmp(grp[u].name, name)) {
4665 grp = &grp[u];
4666 found = 1;
4667 break;
4668 }
4669 }
4670 }
4671 }
4672 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004673 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004674 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004675 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004676 for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004677 if (!strcmp(grp[u].name, name)) {
4678 grp = &grp[u];
4679 found = 1;
4680 }
4681 }
4682 }
4683 if (!found && mod->parsed->includes) {
4684 /* ... and all the submodules */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004685 for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004686 grp = mod->parsed->includes[u].submodule->groupings;
4687 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004688 for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004689 if (!strcmp(grp[v].name, name)) {
4690 grp = &grp[v];
4691 found = 1;
4692 }
4693 }
4694 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004695 }
4696 }
4697 }
4698 if (!found) {
4699 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4700 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4701 return LY_EVALID;
4702 }
4703
4704 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4705 grp_stack_count = ctx->groupings.count;
4706 ly_set_add(&ctx->groupings, (void*)grp, 0);
4707 if (grp_stack_count == ctx->groupings.count) {
4708 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4709 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4710 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4711 return LY_EVALID;
4712 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004713 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4714 /* remember that the grouping is instantiated to avoid its standalone validation */
4715 grp->flags |= LYS_USED_GRP;
4716 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004717
4718 /* switch context's mod_def */
4719 mod_old = ctx->mod_def;
4720 ctx->mod_def = mod;
4721
4722 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004723 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 +01004724
Radek Krejcifc11bd72019-04-11 16:00:05 +02004725 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004726 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004727 /* 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 +02004728 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 +01004729
4730 /* some preparation for applying refines */
4731 if (grp->data == node_p) {
4732 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004733 if (parent) {
4734 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4735 } else if (ctx->mod->compiled->data) {
4736 child = ctx->mod->compiled->data;
4737 } else {
4738 child = NULL;
4739 }
4740 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004741 }
4742 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004743 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004744 LY_LIST_FOR(context_node_fake.child, child) {
4745 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004746
Radek Krejcifc11bd72019-04-11 16:00:05 +02004747 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004748 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004749 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004750 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004751 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4752
4753 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4754 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004755 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004756 }
4757
Radek Krejci00b874b2019-02-12 10:54:50 +01004758 when_shared = *when;
4759 } else {
4760 ++when_shared->refcount;
4761 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004762
4763 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4764 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004765 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004766 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004767 }
4768 }
Radek Krejci01342af2019-01-03 15:18:08 +01004769 }
4770 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004771 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004772 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004773 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004774 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 +01004775 }
4776
Radek Krejcifc11bd72019-04-11 16:00:05 +02004777 /* compile actions */
4778 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4779 if (actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004780 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004781 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004782 if (*actions && (uses_p->augments || uses_p->refines)) {
4783 /* 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 +02004784 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
4785 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
4786 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004787 }
4788 }
4789
4790 /* compile notifications */
4791 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4792 if (notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004793 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004794 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004795 if (*notifs && (uses_p->augments || uses_p->refines)) {
4796 /* 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 +02004797 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
4798 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
4799 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004800 }
4801 }
4802
4803
Radek Krejci3641f562019-02-13 15:38:40 +01004804 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004805 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004806 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004807 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004808 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004809
Radek Krejcif0089082019-01-07 16:42:01 +01004810 /* reload previous context's mod_def */
4811 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004812 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004813
Radek Krejci76b3e962018-12-14 17:01:25 +01004814 /* apply refine */
4815 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004816 lysc_update_path(ctx, NULL, rfn->nodeid);
4817
Radek Krejci7af64242019-02-18 13:07:53 +01004818 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 +01004819 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004820 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004821 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004822
4823 /* default value */
4824 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004825 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004826 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004827 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.",
4828 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004829 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004830 }
4831 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004833 "Invalid refine of default - %s cannot hold default value(s).",
4834 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004835 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004836 }
4837 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004838 struct ly_err_item *err = NULL;
4839 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4840 if (leaf->dflt) {
4841 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004842 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004843 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4844 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4845 } else {
4846 /* prepare a new one */
4847 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4848 leaf->dflt->realtype = leaf->type;
4849 }
4850 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004851 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004852 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4853 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004854 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004855 leaf->dflt->realtype->refcount++;
4856 if (err) {
4857 ly_err_print(err);
4858 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4859 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4860 ly_err_free(err);
4861 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004862 if (rc == LY_EINCOMPLETE) {
4863 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004864 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004865
4866 /* but in general result is so far ok */
4867 rc = LY_SUCCESS;
4868 }
Radek Krejcia1911222019-07-22 17:24:50 +02004869 LY_CHECK_GOTO(rc, cleanup);
4870 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004871 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004872 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4873
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004874 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004875 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4876 "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 +02004877 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004878 }
Radek Krejcia1911222019-07-22 17:24:50 +02004879
4880 /* remove previous set of default values */
4881 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004882 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004883 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4884 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4885 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004886 }
Radek Krejcia1911222019-07-22 17:24:50 +02004887 LY_ARRAY_FREE(llist->dflts);
4888 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004889 LY_ARRAY_FREE(llist->dflts_mods);
4890 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004891
4892 /* create the new set of the default values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004893 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
4894 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004895 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004896 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004897 LY_ARRAY_INCREMENT(llist->dflts_mods);
4898 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004899 LY_ARRAY_INCREMENT(llist->dflts);
4900 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4901 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004902 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004903 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004904 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004905 llist->dflts[u]->realtype->refcount++;
4906 if (err) {
4907 ly_err_print(err);
4908 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4909 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4910 ly_err_free(err);
4911 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004912 if (rc == LY_EINCOMPLETE) {
4913 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004914 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004915
4916 /* but in general result is so far ok */
4917 rc = LY_SUCCESS;
4918 }
4919 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004920 }
Radek Krejcia1911222019-07-22 17:24:50 +02004921 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004922 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004923 if (((struct lysc_node_choice*)node)->dflt) {
4924 /* unset LYS_SET_DFLT from the current default case */
4925 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4926 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004927 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 +01004928 }
4929 }
4930
Radek Krejci12fb9142019-01-08 09:45:30 +01004931 /* description */
4932 if (rfn->dsc) {
4933 FREE_STRING(ctx->ctx, node->dsc);
4934 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4935 }
4936
4937 /* reference */
4938 if (rfn->ref) {
4939 FREE_STRING(ctx->ctx, node->ref);
4940 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4941 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004942
4943 /* config */
4944 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004945 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004946 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004947 } else {
4948 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004949 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004950 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004951 }
4952
4953 /* mandatory */
4954 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004955 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004956 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004957
4958 /* presence */
4959 if (rfn->presence) {
4960 if (node->nodetype != LYS_CONTAINER) {
4961 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004962 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4963 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004964 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004965 }
4966 node->flags |= LYS_PRESENCE;
4967 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004968
4969 /* must */
4970 if (rfn->musts) {
4971 switch (node->nodetype) {
4972 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004973 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 +01004974 break;
4975 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004976 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 +01004977 break;
4978 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004979 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 +01004980 break;
4981 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004982 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 +01004983 break;
4984 case LYS_ANYXML:
4985 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004986 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 +01004987 break;
4988 default:
4989 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004990 "Invalid refine of must statement - %s cannot hold any must statement.",
4991 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004992 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004993 }
Michal Vasko004d3152020-06-11 19:59:22 +02004994 ly_set_add(&ctx->xpath, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01004995 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004996
4997 /* min/max-elements */
4998 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4999 switch (node->nodetype) {
5000 case LYS_LEAFLIST:
5001 if (rfn->flags & LYS_SET_MAX) {
5002 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5003 }
5004 if (rfn->flags & LYS_SET_MIN) {
5005 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005006 if (rfn->min) {
5007 node->flags |= LYS_MAND_TRUE;
5008 lys_compile_mandatory_parents(node->parent, 1);
5009 } else {
5010 node->flags &= ~LYS_MAND_TRUE;
5011 lys_compile_mandatory_parents(node->parent, 0);
5012 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005013 }
5014 break;
5015 case LYS_LIST:
5016 if (rfn->flags & LYS_SET_MAX) {
5017 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5018 }
5019 if (rfn->flags & LYS_SET_MIN) {
5020 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005021 if (rfn->min) {
5022 node->flags |= LYS_MAND_TRUE;
5023 lys_compile_mandatory_parents(node->parent, 1);
5024 } else {
5025 node->flags &= ~LYS_MAND_TRUE;
5026 lys_compile_mandatory_parents(node->parent, 0);
5027 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005028 }
5029 break;
5030 default:
5031 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005032 "Invalid refine of %s statement - %s cannot hold this statement.",
5033 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005034 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005035 }
5036 }
Radek Krejcif0089082019-01-07 16:42:01 +01005037
5038 /* if-feature */
5039 if (rfn->iffeatures) {
5040 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005041 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005042 }
Radek Krejci327de162019-06-14 12:52:07 +02005043
5044 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005045 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005046
Radek Krejcif2271f12019-01-07 16:42:23 +01005047 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005048 for (uint32_t i = 0; i < refined.count; ++i) {
5049 node = (struct lysc_node*)refined.objs[i];
5050 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02005051 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005052
5053 /* check possible conflict with default value (default added, mandatory left true) */
5054 if ((node->flags & LYS_MAND_TRUE) &&
5055 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5056 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5057 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005058 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005059 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005060 }
5061
5062 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5063 if (node->nodetype == LYS_LIST) {
5064 min = ((struct lysc_node_list*)node)->min;
5065 max = ((struct lysc_node_list*)node)->max;
5066 } else {
5067 min = ((struct lysc_node_leaflist*)node)->min;
5068 max = ((struct lysc_node_leaflist*)node)->max;
5069 }
5070 if (min > max) {
5071 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005072 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5073 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005074 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005075 }
5076 }
5077 }
5078
Radek Krejci327de162019-06-14 12:52:07 +02005079 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005080 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005081
5082cleanup:
5083 /* fix connection of the children nodes from fake context node back into the parent */
5084 if (context_node_fake.child) {
5085 context_node_fake.child->prev = child;
5086 }
5087 LY_LIST_FOR(context_node_fake.child, child) {
5088 child->parent = parent;
5089 }
5090
5091 if (uses_p->augments || uses_p->refines) {
5092 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005093 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005094 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005095 LY_ARRAY_FREE(context_node_fake.actions);
5096 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005097 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005098 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005099 LY_ARRAY_FREE(context_node_fake.notifs);
5100 }
5101 }
5102
Radek Krejcie86bf772018-12-14 11:39:53 +01005103 /* reload previous context's mod_def */
5104 ctx->mod_def = mod_old;
5105 /* remove the grouping from the stack for circular groupings dependency check */
5106 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5107 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005108 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005109 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005110
5111 return ret;
5112}
5113
Radek Krejci327de162019-06-14 12:52:07 +02005114static int
5115lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5116{
5117 struct lysp_node *iter;
5118 int len = 0;
5119
5120 *path = NULL;
5121 for (iter = node; iter && len >= 0; iter = iter->parent) {
5122 char *s = *path;
5123 char *id;
5124
5125 switch (iter->nodetype) {
5126 case LYS_USES:
5127 asprintf(&id, "{uses='%s'}", iter->name);
5128 break;
5129 case LYS_GROUPING:
5130 asprintf(&id, "{grouping='%s'}", iter->name);
5131 break;
5132 case LYS_AUGMENT:
5133 asprintf(&id, "{augment='%s'}", iter->name);
5134 break;
5135 default:
5136 id = strdup(iter->name);
5137 break;
5138 }
5139
5140 if (!iter->parent) {
5141 /* print prefix */
5142 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5143 } else {
5144 /* prefix is the same as in parent */
5145 len = asprintf(path, "/%s%s", id, s ? s : "");
5146 }
5147 free(s);
5148 free(id);
5149 }
5150
5151 if (len < 0) {
5152 free(*path);
5153 *path = NULL;
5154 } else if (len == 0) {
5155 *path = strdup("/");
5156 len = 1;
5157 }
5158 return len;
5159}
5160
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005161/**
5162 * @brief Validate groupings that were defined but not directly used in the schema itself.
5163 *
5164 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5165 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5166 */
5167static LY_ERR
5168lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5169{
5170 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005171 char *path;
5172 int len;
5173
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005174 struct lysp_node_uses fake_uses = {
5175 .parent = node_p,
5176 .nodetype = LYS_USES,
5177 .flags = 0, .next = NULL,
5178 .name = grp->name,
5179 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5180 .refines = NULL, .augments = NULL
5181 };
5182 struct lysc_node_container fake_container = {
5183 .nodetype = LYS_CONTAINER,
5184 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5185 .module = ctx->mod,
5186 .sp = NULL, .parent = NULL, .next = NULL,
5187 .prev = (struct lysc_node*)&fake_container,
5188 .name = "fake",
5189 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5190 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5191 };
5192
5193 if (grp->parent) {
5194 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5195 }
Radek Krejci327de162019-06-14 12:52:07 +02005196
5197 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5198 if (len < 0) {
5199 LOGMEM(ctx->ctx);
5200 return LY_EMEM;
5201 }
5202 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5203 ctx->path_len = (uint16_t)len;
5204 free(path);
5205
5206 lysc_update_path(ctx, NULL, "{grouping}");
5207 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005208 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005209 lysc_update_path(ctx, NULL, NULL);
5210 lysc_update_path(ctx, NULL, NULL);
5211
5212 ctx->path_len = 1;
5213 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005214
5215 /* cleanup */
5216 lysc_node_container_free(ctx->ctx, &fake_container);
5217
5218 return ret;
5219}
Radek Krejcife909632019-02-12 15:34:42 +01005220
Radek Krejcie86bf772018-12-14 11:39:53 +01005221/**
Radek Krejcia3045382018-11-22 14:30:31 +01005222 * @brief Compile parsed schema node information.
5223 * @param[in] ctx Compile context
5224 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005225 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5226 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5227 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005228 * @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).
5229 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005230 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5231 */
Radek Krejci19a96102018-11-15 13:38:09 +01005232static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005233lys_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 +01005234{
Radek Krejci1c54f462020-05-12 17:25:34 +02005235 LY_ERR ret = LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01005236 struct lysc_node *node;
5237 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005238 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005239 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005240 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005241
Radek Krejci327de162019-06-14 12:52:07 +02005242 if (node_p->nodetype != LYS_USES) {
5243 lysc_update_path(ctx, parent, node_p->name);
5244 } else {
5245 lysc_update_path(ctx, NULL, "{uses}");
5246 lysc_update_path(ctx, NULL, node_p->name);
5247 }
5248
Radek Krejci19a96102018-11-15 13:38:09 +01005249 switch (node_p->nodetype) {
5250 case LYS_CONTAINER:
5251 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5252 node_compile_spec = lys_compile_node_container;
5253 break;
5254 case LYS_LEAF:
5255 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5256 node_compile_spec = lys_compile_node_leaf;
5257 break;
5258 case LYS_LIST:
5259 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005260 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005261 break;
5262 case LYS_LEAFLIST:
5263 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005264 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005265 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005266 case LYS_CHOICE:
5267 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005268 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005269 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005270 case LYS_ANYXML:
5271 case LYS_ANYDATA:
5272 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005273 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005274 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005275 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005276 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5277 lysc_update_path(ctx, NULL, NULL);
5278 lysc_update_path(ctx, NULL, NULL);
5279 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005280 default:
5281 LOGINT(ctx->ctx);
5282 return LY_EINT;
5283 }
5284 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5285 node->nodetype = node_p->nodetype;
5286 node->module = ctx->mod;
5287 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005288 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005289
5290 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005291 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005292 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005293 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005294 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5295 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005296 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005297 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005298 node->flags |= LYS_CONFIG_R;
5299 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005300 /* config not explicitely set, inherit it from parent */
5301 if (parent) {
5302 node->flags |= parent->flags & LYS_CONFIG_MASK;
5303 } else {
5304 /* default is config true */
5305 node->flags |= LYS_CONFIG_W;
5306 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005307 } else {
5308 /* config set explicitely */
5309 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005310 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005311 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5312 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5313 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005314 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005315 goto error;
5316 }
Radek Krejci19a96102018-11-15 13:38:09 +01005317
Radek Krejcia6d57732018-11-29 13:40:37 +01005318 /* *list ordering */
5319 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5320 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005321 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005322 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5323 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005324 node->flags &= ~LYS_ORDBY_MASK;
5325 node->flags |= LYS_ORDBY_SYSTEM;
5326 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5327 /* default ordering is system */
5328 node->flags |= LYS_ORDBY_SYSTEM;
5329 }
5330 }
5331
Radek Krejci19a96102018-11-15 13:38:09 +01005332 /* status - it is not inherited by specification, but it does not make sense to have
5333 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005334 if (!parent || parent->nodetype != LYS_CHOICE) {
5335 /* in case of choice/case's children, postpone the check to the moment we know if
5336 * 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 +02005337 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 +01005338 }
5339
Radek Krejciec4da802019-05-02 13:02:41 +02005340 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005341 node->sp = node_p;
5342 }
5343 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005344 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5345 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005346 if (node_p->when) {
5347 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005348 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005349
5350 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5351 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02005352 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01005353 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005354 }
Radek Krejciec4da802019-05-02 13:02:41 +02005355 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005356
5357 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005358 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005359
Radek Krejci0935f412019-08-20 16:15:18 +02005360 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5361
Radek Krejcife909632019-02-12 15:34:42 +01005362 /* inherit LYS_MAND_TRUE in parent containers */
5363 if (node->flags & LYS_MAND_TRUE) {
5364 lys_compile_mandatory_parents(parent, 1);
5365 }
5366
Radek Krejci327de162019-06-14 12:52:07 +02005367 lysc_update_path(ctx, NULL, NULL);
5368
Radek Krejci19a96102018-11-15 13:38:09 +01005369 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005370 if (parent) {
5371 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005372 if (node_p->parent->nodetype == LYS_CASE) {
5373 lysc_update_path(ctx, parent, node_p->parent->name);
5374 } else {
5375 lysc_update_path(ctx, parent, node->name);
5376 }
Radek Krejciec4da802019-05-02 13:02:41 +02005377 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005378 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005379 if (uses_status) {
5380
5381 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005382 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005383 * it directly gets the same status flags as the choice;
5384 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005385 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005386 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005387 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005388 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005389 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005390 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005391 }
Radek Krejciec4da802019-05-02 13:02:41 +02005392 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 +02005393
5394 if (parent->nodetype == LYS_CHOICE) {
5395 lysc_update_path(ctx, NULL, NULL);
5396 }
Radek Krejci19a96102018-11-15 13:38:09 +01005397 } else {
5398 /* top-level element */
5399 if (!ctx->mod->compiled->data) {
5400 ctx->mod->compiled->data = node;
5401 } else {
5402 /* insert at the end of the module's top-level nodes list */
5403 ctx->mod->compiled->data->prev->next = node;
5404 node->prev = ctx->mod->compiled->data->prev;
5405 ctx->mod->compiled->data->prev = node;
5406 }
Radek Krejci327de162019-06-14 12:52:07 +02005407 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005408 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5409 ctx->mod->compiled->notifs, node->name, node)) {
5410 return LY_EVALID;
5411 }
Radek Krejci19a96102018-11-15 13:38:09 +01005412 }
Radek Krejci327de162019-06-14 12:52:07 +02005413 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005414
5415 return LY_SUCCESS;
5416
5417error:
5418 lysc_node_free(ctx->ctx, node);
5419 return ret;
5420}
5421
Radek Krejciccd20f12019-02-15 14:12:27 +01005422static void
5423lysc_disconnect(struct lysc_node *node)
5424{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005425 struct lysc_node *parent, *child, *prev = NULL, *next;
5426 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005427 int remove_cs = 0;
5428
5429 parent = node->parent;
5430
5431 /* parent's first child */
5432 if (!parent) {
5433 return;
5434 }
5435 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005436 cs = (struct lysc_node_case*)node;
5437 } else if (parent->nodetype == LYS_CASE) {
5438 /* disconnecting some node in a case */
5439 cs = (struct lysc_node_case*)parent;
5440 parent = cs->parent;
5441 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5442 if (child == node) {
5443 if (cs->child == child) {
5444 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5445 /* case with a single child -> remove also the case */
5446 child->parent = NULL;
5447 remove_cs = 1;
5448 } else {
5449 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005450 }
5451 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005452 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005453 }
5454 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005455 if (!remove_cs) {
5456 cs = NULL;
5457 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005458 } else if (lysc_node_children(parent, node->flags) == node) {
5459 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005460 }
5461
5462 if (cs) {
5463 if (remove_cs) {
5464 /* cs has only one child which is being also removed */
5465 lysc_disconnect((struct lysc_node*)cs);
5466 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5467 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005468 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5469 /* default case removed */
5470 ((struct lysc_node_choice*)parent)->dflt = NULL;
5471 }
5472 if (((struct lysc_node_choice*)parent)->cases == cs) {
5473 /* first case removed */
5474 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5475 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005476 if (cs->child) {
5477 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5478 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5479 prev = cs->child->prev;
5480 } /* else all the children are under a single case */
5481 LY_LIST_FOR_SAFE(cs->child, next, child) {
5482 if (child->parent != (struct lysc_node*)cs) {
5483 break;
5484 }
5485 lysc_node_free(node->module->ctx, child);
5486 }
5487 if (prev) {
5488 if (prev->next) {
5489 prev->next = child;
5490 }
5491 if (child) {
5492 child->prev = prev;
5493 } else {
5494 /* link from the first child under the cases */
5495 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5496 }
5497 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005498 }
5499 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005500 }
5501
5502 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005503 if (node->prev->next) {
5504 node->prev->next = node->next;
5505 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005506 if (node->next) {
5507 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005508 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005509 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005510 if (child) {
5511 child->prev = node->prev;
5512 }
5513 } else if (((struct lysc_node_choice*)parent)->cases) {
5514 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005515 }
5516}
5517
5518LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005519lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005520{
Radek Krejcia1911222019-07-22 17:24:50 +02005521 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005522 struct ly_set devs_p = {0};
5523 struct ly_set targets = {0};
5524 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005525 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005526 struct lysc_action *rpcs;
5527 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005528 struct lysp_deviation *dev;
5529 struct lysp_deviate *d, **dp_new;
5530 struct lysp_deviate_add *d_add;
5531 struct lysp_deviate_del *d_del;
5532 struct lysp_deviate_rpl *d_rpl;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005533 LY_ARRAY_COUNT_TYPE u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005534 struct lysc_deviation {
5535 const char *nodeid;
5536 struct lysc_node *target; /* target node of the deviation */
5537 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005538 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005539 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5540 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005541 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005542 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005543 const char *prefix, *name, *nodeid, *dflt;
Michal Vasko57c10cd2020-05-27 15:57:11 +02005544 struct lys_module *mod, **dev_mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005545 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005546 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005547
5548 /* get all deviations from the module and all its submodules ... */
5549 LY_ARRAY_FOR(mod_p->deviations, u) {
5550 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5551 }
5552 LY_ARRAY_FOR(mod_p->includes, v) {
5553 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5554 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5555 }
5556 }
5557 if (!devs_p.count) {
5558 /* nothing to do */
5559 return LY_SUCCESS;
5560 }
5561
Radek Krejci327de162019-06-14 12:52:07 +02005562 lysc_update_path(ctx, NULL, "{deviation}");
5563
Radek Krejciccd20f12019-02-15 14:12:27 +01005564 /* ... and group them by the target node */
5565 devs = calloc(devs_p.count, sizeof *devs);
5566 for (u = 0; u < devs_p.count; ++u) {
5567 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005568 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005569
5570 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005571 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5572 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01005573 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005574 /* move the target pointer to input/output to make them different from the action and
5575 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5576 * back to the RPC/action node due to a better compatibility and decision code in this function.
5577 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5578 if (flags & LYSC_OPT_RPC_INPUT) {
5579 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5580 flags |= LYSC_OPT_INTERNAL;
5581 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5582 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5583 flags |= LYSC_OPT_INTERNAL;
5584 }
5585 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005586 /* insert into the set of targets with duplicity detection */
5587 i = ly_set_add(&targets, target, 0);
5588 if (!devs[i]) {
5589 /* new record */
5590 devs[i] = calloc(1, sizeof **devs);
5591 devs[i]->target = target;
5592 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005593 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005594 }
5595 /* add deviates into the deviation's list of deviates */
5596 for (d = dev->deviates; d; d = d->next) {
5597 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5598 *dp_new = d;
5599 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5600 devs[i]->not_supported = 1;
5601 }
5602 }
Radek Krejci327de162019-06-14 12:52:07 +02005603
5604 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005605 }
5606
5607 /* MACROS for deviates checking */
5608#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5609 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005610 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 +01005611 goto cleanup; \
5612 }
5613
5614#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005615 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5616 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5617 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005618 goto cleanup; \
5619 }
5620
Radek Krejcia1911222019-07-22 17:24:50 +02005621
Radek Krejciccd20f12019-02-15 14:12:27 +01005622#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005623 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5624 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5625 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5626 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5627 goto cleanup; \
5628 }
5629
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005630#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005631 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005632 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005633 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5634 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005635 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005636 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5637 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005638 goto cleanup; \
5639 }
5640
Radek Krejci551b12c2019-02-19 16:11:21 +01005641#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5642 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005644 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5645 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005646 goto cleanup; \
5647 }
5648
Radek Krejciccd20f12019-02-15 14:12:27 +01005649#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5650 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005651 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005652 goto cleanup; \
5653 }
5654
Radek Krejci551b12c2019-02-19 16:11:21 +01005655#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5656 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5657 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005658 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005659 goto cleanup; \
5660 }
5661
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005662#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5663 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5664 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5665 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5666 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 +01005667 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005668 if (y == LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005670 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5671 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005672 goto cleanup; \
5673 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005674 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5675 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5676 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5677 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005678 (LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005679 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005680 if (!LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005681 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5682 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005683 }
5684
5685 /* apply deviations */
5686 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005687 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5688 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5689 struct ly_err_item *err = NULL;
5690
5691 dflt = NULL;
5692 changed_type = 0;
5693
Radek Krejci327de162019-06-14 12:52:07 +02005694 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5695
Radek Krejcif538ce52019-03-05 10:46:14 +01005696 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5697 /* fix the target pointer in case of RPC's/action's input/output */
5698 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5699 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5700 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5701 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5702 }
5703 }
5704
Radek Krejciccd20f12019-02-15 14:12:27 +01005705 /* not-supported */
5706 if (devs[u]->not_supported) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005707 if (LY_ARRAY_COUNT(devs[u]->deviates) > 1) {
5708 LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.",
5709 LY_ARRAY_COUNT(devs[u]->deviates), devs[u]->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005710 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005711
5712#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5713 if (devs[u]->target->parent) { \
5714 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5715 } else { \
5716 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5717 } \
5718 LY_ARRAY_FOR(ARRAY, x) { \
5719 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5720 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005721 if (x < LY_ARRAY_COUNT(ARRAY)) { \
Radek Krejcifc11bd72019-04-11 16:00:05 +02005722 FREEFUNC(ctx->ctx, &ARRAY[x]); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005723 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
Radek Krejcifc11bd72019-04-11 16:00:05 +02005724 LY_ARRAY_DECREMENT(ARRAY); \
5725 }
5726
Michal Vasko1bf09392020-03-27 12:38:10 +01005727 if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005728 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5729 /* remove RPC's/action's input */
5730 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5731 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005732 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5733 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005734 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5735 /* remove RPC's/action's output */
5736 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5737 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005738 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5739 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005740 } else {
5741 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005742 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005743 }
5744 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005745 /* remove Notification */
5746 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005747 } else {
5748 /* remove the target node */
5749 lysc_disconnect(devs[u]->target);
5750 lysc_node_free(ctx->ctx, devs[u]->target);
5751 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005752
Radek Krejci474f9b82019-07-24 11:36:37 +02005753 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5754 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005755 continue;
5756 }
5757
5758 /* list of deviates (not-supported is not present in the list) */
5759 LY_ARRAY_FOR(devs[u]->deviates, v) {
5760 d = devs[u]->deviates[v];
5761
5762 switch (d->mod) {
5763 case LYS_DEV_ADD:
5764 d_add = (struct lysp_deviate_add*)d;
5765 /* [units-stmt] */
5766 if (d_add->units) {
5767 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5768 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5769
5770 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5771 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5772 }
5773
5774 /* *must-stmt */
5775 if (d_add->musts) {
5776 switch (devs[u]->target->nodetype) {
5777 case LYS_CONTAINER:
5778 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005779 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5780 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005781 break;
5782 case LYS_LEAF:
5783 case LYS_LEAFLIST:
5784 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005785 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5786 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005787 break;
5788 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005789 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5790 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005791 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005792 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005793 case LYS_ACTION:
5794 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005795 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5796 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005797 break;
Michal Vaskocc048b22020-03-27 15:52:38 +01005798 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005799 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5800 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005801 break;
5802 }
5803 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005804 default:
5805 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005806 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005807 goto cleanup;
5808 }
Michal Vasko004d3152020-06-11 19:59:22 +02005809 ly_set_add(&ctx->xpath, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01005810 }
5811
5812 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005813 if (d_add->uniques) {
5814 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5815 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5816 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005817
5818 /* *default-stmt */
5819 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005820 switch (devs[u]->target->nodetype) {
5821 case LYS_LEAF:
5822 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005823 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 +02005824 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005825 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005826 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005827 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5828 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5829 } else {
5830 /* prepare new default value storage */
5831 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005832 }
Radek Krejcia1911222019-07-22 17:24:50 +02005833 dflt = d_add->dflts[0];
5834 /* parsing is done at the end after possible replace of the leaf's type */
5835
Radek Krejci551b12c2019-02-19 16:11:21 +01005836 /* mark the new default values as leaf's own */
5837 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005838 break;
5839 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005840 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005841 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005842 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005843 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005844 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5845 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5846 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005847 }
Radek Krejcia1911222019-07-22 17:24:50 +02005848 LY_ARRAY_FREE(llist->dflts);
5849 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005850 LY_ARRAY_FREE(llist->dflts_mods);
5851 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005852 }
5853 /* add new default value(s) */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005854 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup);
5855 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup);
5856 for (x = y = LY_ARRAY_COUNT(llist->dflts);
5857 x < LY_ARRAY_COUNT(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005858 LY_ARRAY_INCREMENT(llist->dflts_mods);
5859 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005860 LY_ARRAY_INCREMENT(llist->dflts);
5861 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5862 llist->dflts[x]->realtype = llist->type;
5863 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 +02005864 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005865 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005866 llist->dflts[x]->realtype->refcount++;
5867 if (err) {
5868 ly_err_print(err);
5869 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5870 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5871 d_add->dflts[x - y], err->msg);
5872 ly_err_free(err);
5873 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005874 if (rc == LY_EINCOMPLETE) {
5875 /* postpone default compilation when the tree is complete */
5876 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5877
5878 /* but in general result is so far ok */
5879 rc = LY_SUCCESS;
5880 }
Radek Krejcia1911222019-07-22 17:24:50 +02005881 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005882 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005883 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005884 devs[u]->target->flags |= LYS_SET_DFLT;
5885 break;
5886 case LYS_CHOICE:
5887 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5888 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5889 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5890 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005891 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 +01005892 goto cleanup;
5893 }
5894 break;
5895 default:
5896 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005897 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005898 goto cleanup;
5899 }
5900 }
5901
5902 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005903 if (d_add->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01005904 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005905 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005906 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5907 goto cleanup;
5908 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005909 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005910 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
Michal Vaskoa3881362020-01-21 15:57:35 +01005911 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005912 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005913 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5914 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005915 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5916 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005917 goto cleanup;
5918 }
Radek Krejci327de162019-06-14 12:52:07 +02005919 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005920 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005921
5922 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005923 if (d_add->flags & LYS_MAND_MASK) {
5924 if (devs[u]->target->flags & LYS_MAND_MASK) {
5925 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005926 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5927 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005928 goto cleanup;
5929 }
Radek Krejci327de162019-06-14 12:52:07 +02005930 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005931 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005932
5933 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005934 if (d_add->flags & LYS_SET_MIN) {
5935 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5936 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5937 /* change value */
5938 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5939 } else if (devs[u]->target->nodetype == LYS_LIST) {
5940 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5941 /* change value */
5942 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5943 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005944 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005945 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5946 goto cleanup;
5947 }
5948 if (d_add->min) {
5949 devs[u]->target->flags |= LYS_MAND_TRUE;
5950 }
5951 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005952
5953 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005954 if (d_add->flags & LYS_SET_MAX) {
5955 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5956 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5957 /* change value */
5958 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5959 } else if (devs[u]->target->nodetype == LYS_LIST) {
5960 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5961 /* change value */
5962 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5963 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005964 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005965 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5966 goto cleanup;
5967 }
5968 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005969
5970 break;
5971 case LYS_DEV_DELETE:
5972 d_del = (struct lysp_deviate_del*)d;
5973
5974 /* [units-stmt] */
5975 if (d_del->units) {
5976 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02005977 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
5978 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
5979 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5980 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
5981 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5982 goto cleanup;
5983 }
5984 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5985 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005986 }
5987
5988 /* *must-stmt */
5989 if (d_del->musts) {
5990 switch (devs[u]->target->nodetype) {
5991 case LYS_CONTAINER:
5992 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005993 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005994 break;
5995 case LYS_LEAF:
5996 case LYS_LEAFLIST:
5997 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005998 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005999 break;
6000 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006001 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006002 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006003 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006004 case LYS_ACTION:
6005 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6006 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6007 break;
6008 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6009 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6010 break;
6011 }
6012 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006013 default:
6014 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006015 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006016 goto cleanup;
6017 }
6018 }
6019
6020 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006021 if (d_del->uniques) {
6022 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6023 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6024 LY_ARRAY_FOR(d_del->uniques, x) {
6025 LY_ARRAY_FOR(list->uniques, z) {
6026 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6027 nodeid = strpbrk(name, " \t\n");
6028 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02006029 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006030 break;
6031 }
6032 while (isspace(*nodeid)) {
6033 ++nodeid;
6034 }
6035 } else {
6036 if (strcmp(name, list->uniques[z][y]->name)) {
6037 break;
6038 }
6039 }
6040 }
6041 if (!name) {
6042 /* complete match - remove the unique */
6043 LY_ARRAY_DECREMENT(list->uniques);
6044 LY_ARRAY_FREE(list->uniques[z]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006045 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
Radek Krejci7af64242019-02-18 13:07:53 +01006046 --z;
6047 break;
6048 }
6049 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006050 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006051 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006052 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6053 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006054 goto cleanup;
6055 }
6056 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006057 if (!LY_ARRAY_COUNT(list->uniques)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006058 LY_ARRAY_FREE(list->uniques);
6059 list->uniques = NULL;
6060 }
6061 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006062
6063 /* *default-stmt */
6064 if (d_del->dflts) {
6065 switch (devs[u]->target->nodetype) {
6066 case LYS_LEAF:
6067 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6068 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6069 dflt, "deleting", "default", d_del->dflts[0]);
6070
Radek Krejcia1911222019-07-22 17:24:50 +02006071 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006072 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006073 if (strcmp(dflt, d_del->dflts[0])) {
6074 if (i) {
6075 free((char*)dflt);
6076 }
6077 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6078 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6079 d_del->dflts[0], dflt);
6080 goto cleanup;
6081 }
6082 if (i) {
6083 free((char*)dflt);
6084 }
6085 dflt = NULL;
6086
Radek Krejci474f9b82019-07-24 11:36:37 +02006087 /* update the list of incomplete default values if needed */
6088 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6089
Radek Krejcia1911222019-07-22 17:24:50 +02006090 /* remove the default specification */
6091 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6092 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6093 free(leaf->dflt);
6094 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006095 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006096 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006097 break;
6098 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006099 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6100 LY_ARRAY_FOR(d_del->dflts, x) {
6101 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006102 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 +02006103 if (!strcmp(dflt, d_del->dflts[x])) {
6104 if (i) {
6105 free((char*)dflt);
6106 }
6107 dflt = NULL;
6108 break;
6109 }
6110 if (i) {
6111 free((char*)dflt);
6112 }
6113 dflt = NULL;
6114 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006115 if (y == LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02006116 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6117 "which does not match any of the target's property values.", d_del->dflts[x]);
6118 goto cleanup;
6119 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006120
6121 /* update the list of incomplete default values if needed */
6122 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6123
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006124 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006125 LY_ARRAY_DECREMENT(llist->dflts);
6126 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6127 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6128 free(llist->dflts[y]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006129 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
6130 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 +02006131 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006132 if (!LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006133 LY_ARRAY_FREE(llist->dflts_mods);
6134 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006135 LY_ARRAY_FREE(llist->dflts);
6136 llist->dflts = NULL;
6137 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006138 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006139 break;
6140 case LYS_CHOICE:
6141 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6142 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6143 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006144 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006145 if (prefix) {
6146 /* use module prefixes from the deviation module to match the module of the default case */
6147 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6148 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006149 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6150 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006151 goto cleanup;
6152 }
6153 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6154 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006155 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6156 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006157 goto cleanup;
6158 }
6159 }
6160 /* else {
6161 * strictly, the default prefix would point to the deviation module, but the value should actually
6162 * match the default string in the original module (usually unprefixed), so in this case we do not check
6163 * the module of the default case, just matching its name */
6164 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6165 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006166 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6167 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006168 goto cleanup;
6169 }
6170 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6171 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6172 break;
6173 default:
6174 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006175 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006176 goto cleanup;
6177 }
6178 }
6179
6180 break;
6181 case LYS_DEV_REPLACE:
6182 d_rpl = (struct lysp_deviate_rpl*)d;
6183
6184 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006185 if (d_rpl->type) {
6186 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6187 /* type is mandatory, so checking for its presence is not necessary */
6188 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006189
6190 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6191 /* the target has default from the previous type - remove it */
6192 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006193 /* update the list of incomplete default values if needed */
6194 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6195
Radek Krejcia1911222019-07-22 17:24:50 +02006196 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6197 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6198 free(leaf->dflt);
6199 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006200 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006201 } else { /* LYS_LEAFLIST */
6202 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006203 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006204 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6205 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6206 free(llist->dflts[x]);
6207 }
6208 LY_ARRAY_FREE(llist->dflts);
6209 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006210 LY_ARRAY_FREE(llist->dflts_mods);
6211 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006212 }
6213 }
6214 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006215 /* there is no default value, do not set changed_type after type compilation
6216 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006217 changed_type = -1;
6218 }
Radek Krejciec4da802019-05-02 13:02:41 +02006219 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 +02006220 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006221 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006222
6223 /* [units-stmt] */
6224 if (d_rpl->units) {
6225 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6226 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6227 units, "replacing", "units", d_rpl->units);
6228
6229 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6230 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6231 }
6232
6233 /* [default-stmt] */
6234 if (d_rpl->dflt) {
6235 switch (devs[u]->target->nodetype) {
6236 case LYS_LEAF:
6237 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6238 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006239 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006240 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006241 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6242 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6243 dflt = d_rpl->dflt;
6244 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006245 break;
6246 case LYS_CHOICE:
6247 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006248 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 +01006249 goto cleanup;
6250 }
6251 break;
6252 default:
6253 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006254 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006255 goto cleanup;
6256 }
6257 }
6258
6259 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006260 if (d_rpl->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01006261 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006262 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006263 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6264 goto cleanup;
6265 }
6266 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006267 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006268 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6269 goto cleanup;
6270 }
Radek Krejci327de162019-06-14 12:52:07 +02006271 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006272 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006273
6274 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006275 if (d_rpl->flags & LYS_MAND_MASK) {
6276 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006277 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006278 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6279 goto cleanup;
6280 }
Radek Krejci327de162019-06-14 12:52:07 +02006281 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006282 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006283
6284 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006285 if (d_rpl->flags & LYS_SET_MIN) {
6286 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6287 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6288 /* change value */
6289 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6290 } else if (devs[u]->target->nodetype == LYS_LIST) {
6291 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6292 /* change value */
6293 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6294 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006295 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006296 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6297 goto cleanup;
6298 }
6299 if (d_rpl->min) {
6300 devs[u]->target->flags |= LYS_MAND_TRUE;
6301 }
6302 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006303
6304 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006305 if (d_rpl->flags & LYS_SET_MAX) {
6306 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6307 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6308 /* change value */
6309 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6310 } else if (devs[u]->target->nodetype == LYS_LIST) {
6311 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6312 /* change value */
6313 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6314 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006315 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006316 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6317 goto cleanup;
6318 }
6319 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006320
6321 break;
6322 default:
6323 LOGINT(ctx->ctx);
6324 goto cleanup;
6325 }
6326 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006327
Radek Krejci33f72892019-02-21 10:36:58 +01006328 /* final check when all deviations of a single target node are applied */
6329
Radek Krejci551b12c2019-02-19 16:11:21 +01006330 /* check min-max compatibility */
6331 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6332 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6333 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6334 } else if (devs[u]->target->nodetype == LYS_LIST) {
6335 min = ((struct lysc_node_list*)devs[u]->target)->min;
6336 max = ((struct lysc_node_list*)devs[u]->target)->max;
6337 } else {
6338 min = max = 0;
6339 }
6340 if (min > max) {
6341 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 +02006342 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006343 goto cleanup;
6344 }
6345
Radek Krejcia1911222019-07-22 17:24:50 +02006346 if (dflt) {
6347 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006348 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006349 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006350 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6351 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006352 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006353 leaf->dflt->realtype->refcount++;
6354 if (err) {
6355 ly_err_print(err);
6356 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6357 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6358 ly_err_free(err);
6359 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006360 if (rc == LY_EINCOMPLETE) {
6361 /* postpone default compilation when the tree is complete */
6362 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6363
6364 /* but in general result is so far ok */
6365 rc = LY_SUCCESS;
6366 }
Radek Krejcia1911222019-07-22 17:24:50 +02006367 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006368 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006369 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6370 int dynamic;
6371 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006372 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006373
6374 /* update the list of incomplete default values if needed */
6375 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6376
6377 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006378 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6379 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6380 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006381 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6382 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006383 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006384 leaf->dflt->realtype->refcount++;
6385 if (err) {
6386 ly_err_print(err);
6387 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6388 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6389 ly_err_free(err);
6390 }
6391 if (dynamic) {
6392 free((void*)dflt);
6393 }
Radek Krejcia1911222019-07-22 17:24:50 +02006394 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006395 if (rc == LY_EINCOMPLETE) {
6396 /* postpone default compilation when the tree is complete */
6397 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6398
6399 /* but in general result is so far ok */
6400 rc = LY_SUCCESS;
6401 }
6402 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006403 } else { /* LYS_LEAFLIST */
6404 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006405 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 +02006406 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6407 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6408 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006409 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6410 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006411 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6412 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006413 llist->dflts[x]->realtype->refcount++;
6414 if (err) {
6415 ly_err_print(err);
6416 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6417 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6418 dflt, err->msg);
6419 ly_err_free(err);
6420 }
6421 if (dynamic) {
6422 free((void*)dflt);
6423 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006424 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006425 if (rc == LY_EINCOMPLETE) {
6426 /* postpone default compilation when the tree is complete */
6427 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6428
6429 /* but in general result is so far ok */
6430 rc = LY_SUCCESS;
6431 }
Radek Krejcia1911222019-07-22 17:24:50 +02006432 LY_CHECK_GOTO(rc, cleanup);
6433 }
6434 }
6435 }
6436
Radek Krejci551b12c2019-02-19 16:11:21 +01006437 /* check mandatory - default compatibility */
6438 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6439 && (devs[u]->target->flags & LYS_SET_DFLT)
6440 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6441 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006442 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006443 goto cleanup;
6444 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6445 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6446 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006447 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 +01006448 goto cleanup;
6449 }
6450 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6451 /* mandatory node under a default case */
6452 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006453 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6454 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006455 goto cleanup;
6456 }
Radek Krejci33f72892019-02-21 10:36:58 +01006457
Michal Vasko57c10cd2020-05-27 15:57:11 +02006458 /* add this module into the target module deviated_by */
6459 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup);
6460 *dev_mod = mod_p->mod;
6461
Radek Krejci327de162019-06-14 12:52:07 +02006462 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006463 }
6464
Radek Krejci327de162019-06-14 12:52:07 +02006465 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006466 ret = LY_SUCCESS;
6467
6468cleanup:
6469 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6470 LY_ARRAY_FREE(devs[u]->deviates);
6471 free(devs[u]);
6472 }
6473 free(devs);
6474 ly_set_erase(&targets, NULL);
6475 ly_set_erase(&devs_p, NULL);
6476
6477 return ret;
6478}
6479
Radek Krejcib56c7502019-02-13 14:19:54 +01006480/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006481 * @brief Compile the given YANG submodule into the main module.
6482 * @param[in] ctx Compile context
6483 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006484 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6485 */
6486LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006487lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006488{
6489 unsigned int u;
6490 LY_ERR ret = LY_SUCCESS;
6491 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006492 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006493 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006494 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006495
Michal Vasko33ff9422020-07-03 09:50:39 +02006496 if (!mainmod->mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01006497 /* features are compiled directly into the compiled module structure,
6498 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6499 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006500 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6501 LY_CHECK_GOTO(ret, error);
6502 }
Radek Krejci0af46292019-01-11 16:02:31 +01006503
Michal Vasko33ff9422020-07-03 09:50:39 +02006504 if (!mainmod->mod->dis_identities) {
6505 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities);
6506 LY_CHECK_GOTO(ret, error);
6507 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01006508
Radek Krejci474f9b82019-07-24 11:36:37 +02006509 /* data nodes */
6510 LY_LIST_FOR(submod->data, node_p) {
6511 ret = lys_compile_node(ctx, node_p, NULL, 0);
6512 LY_CHECK_GOTO(ret, error);
6513 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006514
Radek Krejciec4da802019-05-02 13:02:41 +02006515 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6516 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006517
Radek Krejcid05cbd92018-12-05 14:26:40 +01006518error:
6519 return ret;
6520}
6521
Radek Krejci335332a2019-09-05 13:03:35 +02006522static void *
6523lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6524{
6525 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6526 if (substmts[u].stmt == stmt) {
6527 return substmts[u].storage;
6528 }
6529 }
6530 return NULL;
6531}
6532
6533LY_ERR
6534lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6535{
6536 LY_ERR ret = LY_EVALID, r;
6537 unsigned int u;
6538 struct lysp_stmt *stmt;
6539 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006540
6541 /* check for invalid substatements */
6542 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006543 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6544 continue;
6545 }
Radek Krejci335332a2019-09-05 13:03:35 +02006546 for (u = 0; substmts[u].stmt; ++u) {
6547 if (substmts[u].stmt == stmt->kw) {
6548 break;
6549 }
6550 }
6551 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006552 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6553 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006554 goto cleanup;
6555 }
Radek Krejci335332a2019-09-05 13:03:35 +02006556 }
6557
Radek Krejciad5963b2019-09-06 16:03:05 +02006558 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6559
Radek Krejci335332a2019-09-05 13:03:35 +02006560 /* keep order of the processing the same as the order in the defined substmts,
6561 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6562 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006563 int stmt_present = 0;
6564
Radek Krejci335332a2019-09-05 13:03:35 +02006565 for (stmt = ext->child; stmt; stmt = stmt->next) {
6566 if (substmts[u].stmt != stmt->kw) {
6567 continue;
6568 }
6569
Radek Krejciad5963b2019-09-06 16:03:05 +02006570 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006571 if (substmts[u].storage) {
6572 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006573 case LY_STMT_STATUS:
6574 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6575 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6576 break;
6577 case LY_STMT_UNITS: {
6578 const char **units;
6579
6580 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6581 /* single item */
6582 if (*((const char **)substmts[u].storage)) {
6583 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6584 goto cleanup;
6585 }
6586 units = (const char **)substmts[u].storage;
6587 } else {
6588 /* sized array */
6589 const char ***units_array = (const char ***)substmts[u].storage;
6590 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6591 }
6592 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6593 break;
6594 }
Radek Krejci335332a2019-09-05 13:03:35 +02006595 case LY_STMT_TYPE: {
6596 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6597 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6598
6599 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6600 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006601 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006602 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6603 goto cleanup;
6604 }
6605 compiled = substmts[u].storage;
6606 } else {
6607 /* sized array */
6608 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6609 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6610 compiled = (void*)type;
6611 }
6612
Radek Krejciad5963b2019-09-06 16:03:05 +02006613 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006614 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6615 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006616 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6617 lysp_type_free(ctx->ctx, parsed);
6618 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006619 break;
6620 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006621 case LY_STMT_IF_FEATURE: {
6622 struct lysc_iffeature *iff = NULL;
6623
6624 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6625 /* single item */
6626 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6627 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6628 goto cleanup;
6629 }
6630 iff = (struct lysc_iffeature*)substmts[u].storage;
6631 } else {
6632 /* sized array */
6633 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6634 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6635 }
6636 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6637 break;
6638 }
6639 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6640 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006641 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006642 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.",
6643 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006644 goto cleanup;
6645 }
6646 }
Radek Krejci335332a2019-09-05 13:03:35 +02006647 }
Radek Krejci335332a2019-09-05 13:03:35 +02006648
Radek Krejciad5963b2019-09-06 16:03:05 +02006649 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6650 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6651 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6652 goto cleanup;
6653 }
Radek Krejci335332a2019-09-05 13:03:35 +02006654 }
6655
6656 ret = LY_SUCCESS;
6657
6658cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006659 return ret;
6660}
6661
Michal Vasko175012e2019-11-06 15:49:14 +01006662/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006663 * @brief Check when for cyclic dependencies.
6664 * @param[in] set Set with all the referenced nodes.
6665 * @param[in] node Node whose "when" referenced nodes are in @p set.
6666 * @return LY_ERR value
6667 */
6668static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006669lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006670{
6671 struct lyxp_set tmp_set;
6672 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006673 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006674 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006675 int idx;
6676 struct lysc_when *when;
6677 LY_ERR ret = LY_SUCCESS;
6678
6679 memset(&tmp_set, 0, sizeof tmp_set);
6680
6681 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006682 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006683 xp_scnode = &set->val.scnodes[i];
6684
Michal Vasko5c4e5892019-11-14 12:31:38 +01006685 if (xp_scnode->in_ctx != -1) {
6686 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006687 xp_scnode->in_ctx = 1;
6688 }
6689 }
6690
6691 for (i = 0; i < set->used; ++i) {
6692 xp_scnode = &set->val.scnodes[i];
6693 if (xp_scnode->in_ctx != 1) {
6694 /* already checked */
6695 continue;
6696 }
6697
Michal Vasko1bf09392020-03-27 12:38:10 +01006698 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006699 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006700 /* no when to check */
6701 xp_scnode->in_ctx = 0;
6702 continue;
6703 }
6704
6705 node = xp_scnode->scnode;
6706 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006707 LY_ARRAY_FOR(node->when, u) {
6708 when = node->when[u];
Michal Vasko52927e22020-03-16 17:26:14 +01006709 ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006710 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6711 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006712 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006713 goto cleanup;
6714 }
6715
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006716 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006717 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006718 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006719 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006720 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 +01006721 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006722 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 +01006723 ret = LY_EVALID;
6724 goto cleanup;
6725 }
6726
6727 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006728 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006729 } else {
6730 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006731 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006732 }
6733 }
6734
6735 /* merge this set into the global when set */
6736 lyxp_set_scnode_merge(set, &tmp_set);
6737 }
6738
6739 /* check when of non-data parents as well */
6740 node = node->parent;
6741 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6742
Michal Vasko251f56e2019-11-14 16:06:47 +01006743 /* this node when was checked (xp_scnode could have been reallocd) */
6744 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006745 }
6746
6747cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006748 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006749 return ret;
6750}
6751
6752/**
Michal Vasko175012e2019-11-06 15:49:14 +01006753 * @brief Check when/must expressions of a node on a compiled schema tree.
6754 * @param[in] ctx Compile context.
6755 * @param[in] node Node to check.
6756 * @return LY_ERR value
6757 */
6758static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006759lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006760{
Michal Vasko175012e2019-11-06 15:49:14 +01006761 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006762 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006763 LY_ARRAY_COUNT_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01006764 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006765 struct lysc_when **when = NULL;
6766 struct lysc_must *musts = NULL;
6767 LY_ERR ret = LY_SUCCESS;
6768
6769 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006770 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko175012e2019-11-06 15:49:14 +01006771
6772 switch (node->nodetype) {
6773 case LYS_CONTAINER:
6774 when = ((struct lysc_node_container *)node)->when;
6775 musts = ((struct lysc_node_container *)node)->musts;
6776 break;
6777 case LYS_CHOICE:
6778 when = ((struct lysc_node_choice *)node)->when;
6779 break;
6780 case LYS_LEAF:
6781 when = ((struct lysc_node_leaf *)node)->when;
6782 musts = ((struct lysc_node_leaf *)node)->musts;
6783 break;
6784 case LYS_LEAFLIST:
6785 when = ((struct lysc_node_leaflist *)node)->when;
6786 musts = ((struct lysc_node_leaflist *)node)->musts;
6787 break;
6788 case LYS_LIST:
6789 when = ((struct lysc_node_list *)node)->when;
6790 musts = ((struct lysc_node_list *)node)->musts;
6791 break;
6792 case LYS_ANYXML:
6793 case LYS_ANYDATA:
6794 when = ((struct lysc_node_anydata *)node)->when;
6795 musts = ((struct lysc_node_anydata *)node)->musts;
6796 break;
6797 case LYS_CASE:
6798 when = ((struct lysc_node_case *)node)->when;
6799 break;
6800 case LYS_NOTIF:
6801 musts = ((struct lysc_notif *)node)->musts;
6802 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006803 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006804 case LYS_ACTION:
6805 /* first process input musts */
6806 musts = ((struct lysc_action *)node)->input.musts;
6807 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006808 default:
6809 /* nothing to check */
6810 break;
6811 }
6812
Michal Vasko175012e2019-11-06 15:49:14 +01006813 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006814 LY_ARRAY_FOR(when, u) {
Michal Vasko004d3152020-06-11 19:59:22 +02006815 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 +02006816 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006817 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006818 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006819 goto cleanup;
6820 }
6821
Michal Vaskodc052f32019-11-07 11:11:38 +01006822 ctx->path[0] = '\0';
6823 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006824 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006825 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006826 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6827 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006828
Michal Vaskoecd62de2019-11-13 12:35:11 +01006829 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006830 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006831 schema->name);
6832 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006833
6834 /* check dummy node accessing */
6835 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006836 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006837 ret = LY_EVALID;
6838 goto cleanup;
6839 }
Michal Vasko175012e2019-11-06 15:49:14 +01006840 }
6841 }
6842
Michal Vaskoecd62de2019-11-13 12:35:11 +01006843 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006844 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006845 LY_CHECK_GOTO(ret, cleanup);
6846
Michal Vaskod3678892020-05-21 10:06:58 +02006847 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006848 }
6849
Michal Vasko5d8756a2019-11-07 15:21:00 +01006850check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006851 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006852 LY_ARRAY_FOR(musts, u) {
6853 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 +01006854 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006855 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006856 goto cleanup;
6857 }
6858
Michal Vaskodc052f32019-11-07 11:11:38 +01006859 ctx->path[0] = '\0';
6860 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006861 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006862 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006863 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006864 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006865 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6866 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006867 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006868 }
6869 }
6870
Michal Vaskod3678892020-05-21 10:06:58 +02006871 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006872 }
6873
Michal Vasko1bf09392020-03-27 12:38:10 +01006874 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006875 /* now check output musts */
6876 input_done = 1;
6877 musts = ((struct lysc_action *)node)->output.musts;
6878 opts = LYXP_SCNODE_OUTPUT;
6879 goto check_musts;
6880 }
6881
Michal Vasko175012e2019-11-06 15:49:14 +01006882cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006883 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006884 return ret;
6885}
6886
Michal Vasko8d544252020-03-02 10:19:52 +01006887static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006888lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6889{
6890 const struct lysc_node *target = NULL;
6891 struct ly_path *p;
6892 struct lysc_type *type;
6893
6894 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6895
6896 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02006897 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
6898 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
6899 lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02006900
6901 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006902 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02006903 ly_path_free(node->module->ctx, p);
6904
6905 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
6906 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6907 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
6908 lref->path->expr, lys_nodetype2str(target->nodetype));
6909 return LY_EVALID;
6910 }
6911
6912 /* check status */
6913 ctx->path[0] = '\0';
6914 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6915 ctx->path_len = strlen(ctx->path);
6916 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
6917 return LY_EVALID;
6918 }
6919 ctx->path_len = 1;
6920 ctx->path[1] = '\0';
6921
6922 /* check config */
6923 if (lref->require_instance && (node->flags & LYS_CONFIG_W)) {
6924 if (target->flags & LYS_CONFIG_R) {
6925 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
6926 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
6927 return LY_EVALID;
6928 }
6929 }
6930
6931 /* store the target's type and check for circular chain of leafrefs */
6932 lref->realtype = ((struct lysc_node_leaf *)target)->type;
6933 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
6934 if (type == (struct lysc_type *)lref) {
6935 /* circular chain detected */
6936 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6937 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
6938 return LY_EVALID;
6939 }
6940 }
6941
6942 /* check if leafref and its target are under common if-features */
6943 if (lys_compile_leafref_features_validate(node, target)) {
6944 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6945 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
6946 " features applicable to the leafref itself.", lref->path->expr);
6947 return LY_EVALID;
6948 }
6949
6950 return LY_SUCCESS;
6951}
6952
6953static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01006954lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
6955{
6956 struct lysc_ext_instance *ext;
6957 struct lysp_ext_instance *ext_p = NULL;
6958 struct lysp_stmt *stmt;
6959 const struct lys_module *ext_mod;
6960 LY_ERR ret = LY_SUCCESS;
6961
6962 /* create the parsed extension instance manually */
6963 ext_p = calloc(1, sizeof *ext_p);
6964 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
6965 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
6966 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
6967 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
6968 ext_p->insubstmt_index = 0;
6969
6970 stmt = calloc(1, sizeof *ext_p->child);
6971 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
6972 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
6973 stmt->kw = LY_STMT_TYPE;
6974 ext_p->child = stmt;
6975
6976 /* allocate new extension instance */
6977 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
6978
6979 /* manually get extension definition module */
6980 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
6981
6982 /* compile the extension instance */
6983 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
6984
6985cleanup:
6986 lysp_ext_instance_free(ctx->ctx, ext_p);
6987 free(ext_p);
6988 return ret;
6989}
6990
Michal Vasko004d3152020-06-11 19:59:22 +02006991static LY_ERR
6992lys_compile_unres(struct lysc_ctx *ctx)
6993{
6994 struct lysc_node *node;
6995 struct lysc_type *type, *typeiter;
6996 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006997 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02006998 uint32_t i;
6999
7000 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7001 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7002 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7003 for (i = 0; i < ctx->leafrefs.count; ++i) {
7004 node = ctx->leafrefs.objs[i];
7005 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7006 type = ((struct lysc_node_leaf *)node)->type;
7007 if (type->basetype == LY_TYPE_LEAFREF) {
7008 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7009 } else if (type->basetype == LY_TYPE_UNION) {
7010 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7011 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7012 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7013 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7014 }
7015 }
7016 }
7017 }
7018 for (i = 0; i < ctx->leafrefs.count; ++i) {
7019 /* store pointer to the real type */
7020 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7021 if (type->basetype == LY_TYPE_LEAFREF) {
7022 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7023 typeiter->basetype == LY_TYPE_LEAFREF;
7024 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7025 ((struct lysc_type_leafref*)type)->realtype = typeiter;
7026 } else if (type->basetype == LY_TYPE_UNION) {
7027 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7028 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7029 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7030 typeiter->basetype == LY_TYPE_LEAFREF;
7031 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7032 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7033 }
7034 }
7035 }
7036 }
7037
7038 /* check xpath */
7039 for (i = 0; i < ctx->xpath.count; ++i) {
7040 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7041 }
7042
7043 /* finish incomplete default values compilation */
7044 for (i = 0; i < ctx->dflts.count; ++i) {
7045 struct ly_err_item *err = NULL;
7046 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
7047 LY_ERR ret;
7048 ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
7049 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7050 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7051 if (err) {
7052 ly_err_print(err);
7053 ctx->path[0] = '\0';
7054 lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7055 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7056 "Invalid default - value does not fit the type (%s).", err->msg);
7057 ly_err_free(err);
7058 }
7059 LY_CHECK_RET(ret);
7060 }
7061
7062 return LY_SUCCESS;
7063}
7064
Radek Krejci19a96102018-11-15 13:38:09 +01007065LY_ERR
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007066lys_compile(struct lys_module **mod, int options)
Radek Krejci19a96102018-11-15 13:38:09 +01007067{
7068 struct lysc_ctx ctx = {0};
7069 struct lysc_module *mod_c;
7070 struct lysp_module *sp;
7071 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007072 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007073 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007074 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007075 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007076 uint32_t i;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007077 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007078
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007079 LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007080
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007081 if (!(*mod)->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007082 /* just imported modules are not compiled */
7083 return LY_SUCCESS;
7084 }
7085
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007086 sp = (*mod)->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007087
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007088 ctx.ctx = (*mod)->ctx;
7089 ctx.mod = *mod;
7090 ctx.mod_def = *mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007091 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007092 ctx.path_len = 1;
7093 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007094
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007095 (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
7096 LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
7097 mod_c->mod = *mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007098
Radek Krejciec4da802019-05-02 13:02:41 +02007099 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007100 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007101 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007102 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7103 }
Radek Krejci0935f412019-08-20 16:15:18 +02007104
7105 /* features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007106 if ((*mod)->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007107 /* there is already precompiled array of features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007108 mod_c->features = (*mod)->dis_features;
7109 (*mod)->dis_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007110 } else {
7111 /* features are compiled directly into the compiled module structure,
7112 * 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 +02007113 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007114 LY_CHECK_GOTO(ret, error);
7115 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007116
Radek Krejci0af46292019-01-11 16:02:31 +01007117 /* finish feature compilation, not only for the main module, but also for the submodules.
7118 * Due to possible forward references, it must be done when all the features (including submodules)
7119 * are present. */
7120 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007121 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007122 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7123 }
Radek Krejci327de162019-06-14 12:52:07 +02007124 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007125 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007126 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007127 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007128 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007129 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7130 }
Radek Krejci327de162019-06-14 12:52:07 +02007131 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007132 }
Radek Krejci327de162019-06-14 12:52:07 +02007133 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007134
Michal Vasko33ff9422020-07-03 09:50:39 +02007135 /* identities, work similarly to features with the precompilation */
7136 if ((*mod)->dis_identities) {
7137 mod_c->identities = (*mod)->dis_identities;
7138 (*mod)->dis_identities = NULL;
7139 } else {
7140 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
7141 LY_CHECK_GOTO(ret, error);
7142 }
Radek Krejci19a96102018-11-15 13:38:09 +01007143 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007144 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007145 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007146 lysc_update_path(&ctx, NULL, "{submodule}");
7147 LY_ARRAY_FOR(sp->includes, v) {
7148 if (sp->includes[v].submodule->identities) {
7149 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7150 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities);
7151 LY_CHECK_GOTO(ret, error);
7152 lysc_update_path(&ctx, NULL, NULL);
7153 }
7154 }
Radek Krejci327de162019-06-14 12:52:07 +02007155 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007156
Radek Krejci95710c92019-02-11 15:49:55 +01007157 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007158 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007159 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007160 }
Radek Krejci95710c92019-02-11 15:49:55 +01007161
Radek Krejciec4da802019-05-02 13:02:41 +02007162 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7163 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007164
Radek Krejci95710c92019-02-11 15:49:55 +01007165 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007166 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007167 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007168 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007169 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007170
Radek Krejci474f9b82019-07-24 11:36:37 +02007171 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007172 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007173
Radek Krejci0935f412019-08-20 16:15:18 +02007174 /* extension instances TODO cover extension instances from submodules */
7175 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007176
Michal Vasko004d3152020-06-11 19:59:22 +02007177 /* finish compilation for all unresolved items in the context */
7178 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007179
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007180 /* validate non-instantiated groupings from the parsed schema,
7181 * without it we would accept even the schemas with invalid grouping specification */
7182 ctx.options |= LYSC_OPT_GROUPING;
7183 LY_ARRAY_FOR(sp->groupings, u) {
7184 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007185 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007186 }
7187 }
7188 LY_LIST_FOR(sp->data, node_p) {
7189 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7190 LY_ARRAY_FOR(grps, u) {
7191 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007192 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007193 }
7194 }
7195 }
7196
Radek Krejci474f9b82019-07-24 11:36:37 +02007197 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7198 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7199 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7200 }
7201
Michal Vasko8d544252020-03-02 10:19:52 +01007202#if 0
7203 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7204 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7205 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7206 * the anotation definitions available in the internal schema structure. */
7207 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7208 if (lyp_add_ietf_netconf_annotations(mod)) {
7209 lys_free(mod, NULL, 1, 1);
7210 return NULL;
7211 }
7212 }
7213#endif
7214
7215 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007216 if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
7217 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007218 }
7219
Radek Krejci1c0c3442019-07-23 16:08:47 +02007220 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007221 ly_set_erase(&ctx.xpath, NULL);
7222 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007223 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007224 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007225 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007226
Radek Krejciec4da802019-05-02 13:02:41 +02007227 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007228 lysp_module_free((*mod)->parsed);
7229 (*mod)->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007230 }
7231
Radek Krejciec4da802019-05-02 13:02:41 +02007232 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007233 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007234 for (i = 0; i < ctx.ctx->list.count; ++i) {
7235 m = ctx.ctx->list.objs[i];
Radek Krejci95710c92019-02-11 15:49:55 +01007236 if (m->implemented == 2) {
7237 m->implemented = 1;
7238 }
7239 }
7240 }
7241
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007242 (*mod)->compiled = mod_c;
Radek Krejci19a96102018-11-15 13:38:09 +01007243 return LY_SUCCESS;
7244
7245error:
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007246 lys_feature_precompile_revert(&ctx, *mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007247 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007248 ly_set_erase(&ctx.xpath, NULL);
7249 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007250 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007251 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007252 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007253 lysc_module_free(mod_c, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007254 (*mod)->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007255
7256 /* revert compilation of modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007257 for (i = 0; i < ctx.ctx->list.count; ++i) {
7258 m = ctx.ctx->list.objs[i];
Michal Vasko2947ce12019-12-16 10:37:19 +01007259 if ((m->implemented == 2) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007260 /* revert features list to the precompiled state */
7261 lys_feature_precompile_revert(&ctx, m);
7262 /* mark module as imported-only / not-implemented */
7263 m->implemented = 0;
7264 /* free the compiled version of the module */
7265 lysc_module_free(m->compiled, NULL);
7266 m->compiled = NULL;
7267 }
7268 }
7269
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007270 /* remove the module itself from the context and free it */
7271 ly_set_rm(&ctx.ctx->list, *mod, NULL);
7272 lys_module_free(*mod, NULL);
7273 *mod = NULL;
7274
Radek Krejci19a96102018-11-15 13:38:09 +01007275 return ret;
7276}