blob: d62473bbb0adf8889367b38b58c64b18f2049081 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejci19a96102018-11-15 13:38:09 +010016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020026#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "dict.h"
29#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020030#include "path.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020031#include "parser.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020032#include "parser_schema.h"
Radek Krejci0935f412019-08-20 16:15:18 +020033#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020035#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020039#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010040#include "tree_schema_internal.h"
41#include "xpath.h"
42
Michal Vasko5fe75f12020-03-02 13:52:37 +010043static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
44 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
45
Radek Krejci19a96102018-11-15 13:38:09 +010046/**
47 * @brief Duplicate string into dictionary
48 * @param[in] CTX libyang context of the dictionary.
49 * @param[in] ORIG String to duplicate.
50 * @param[out] DUP Where to store the result.
51 */
52#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
53
Radek Krejciec4da802019-05-02 13:02:41 +020054#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010055 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020056 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
57 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
58 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci19a96102018-11-15 13:38:09 +010059 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020060 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010061 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
62 } \
63 }
64
Radek Krejciec4da802019-05-02 13:02:41 +020065#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010066 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020067 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
68 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
69 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010070 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020071 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010072 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
73 } \
74 }
75
Radek Krejci0935f412019-08-20 16:15:18 +020076#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
77 if (EXTS_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020078 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
79 for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \
Radek Krejci0935f412019-08-20 16:15:18 +020080 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010081 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \
Radek Krejci0935f412019-08-20 16:15:18 +020082 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
83 } \
84 }
85
Radek Krejciec4da802019-05-02 13:02:41 +020086#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010087 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020088 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
89 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
90 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010091 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020092 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010093 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
94 } \
95 }
96
Radek Krejciec4da802019-05-02 13:02:41 +020097#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010098 if (MEMBER_P) { \
99 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
100 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200101 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100102 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
103 }
104
Radek Krejciec4da802019-05-02 13:02:41 +0200105#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100106 if (MEMBER_P) { \
107 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200108 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100109 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200110 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100111 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
112 }
113
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100114#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100115 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200116 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100117 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100118 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
119 return LY_EVALID; \
120 } \
121 } \
122 }
123
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100124#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
125 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200126 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100127 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
128 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
129 return LY_EVALID; \
130 } \
131 } \
132 }
133
134struct lysc_ext *
135lysc_ext_dup(struct lysc_ext *orig)
136{
137 ++orig->refcount;
138 return orig;
139}
140
Radek Krejci19a96102018-11-15 13:38:09 +0100141static struct lysc_ext_instance *
142lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
143{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100144 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100145 (void) ctx;
146 (void) orig;
147 return NULL;
148}
149
Radek Krejcib56c7502019-02-13 14:19:54 +0100150/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200151 * @brief Add record into the compile context's list of incomplete default values.
152 * @param[in] ctx Compile context with the incomplete default values list.
153 * @param[in] context_node Context schema node to store in the record.
154 * @param[in] dflt Incomplete default value to store in the record.
155 * @param[in] dflt_mod Module of the default value definition to store in the record.
156 * @return LY_EMEM in case of memory allocation failure.
157 * @return LY_SUCCESS
158 */
159static LY_ERR
160lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
161{
162 struct lysc_incomplete_dflt *r;
163 r = malloc(sizeof *r);
164 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
165 r->context_node = context_node;
166 r->dflt = dflt;
167 r->dflt_mod = dflt_mod;
168 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
169
170 return LY_SUCCESS;
171}
172
173/**
174 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
175 * @param[in] ctx Compile context with the incomplete default values list.
176 * @param[in] dflt Incomplete default values identifying the record to remove.
177 */
178static void
179lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
180{
181 unsigned int u;
182 struct lysc_incomplete_dflt *r;
183
184 for (u = 0; u < ctx->dflts.count; ++u) {
185 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
186 if (r->dflt == dflt) {
187 free(ctx->dflts.objs[u]);
188 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
189 --ctx->dflts.count;
190 return;
191 }
192 }
193}
194
Radek Krejci0e59c312019-08-15 15:34:15 +0200195void
Radek Krejci327de162019-06-14 12:52:07 +0200196lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
197{
198 int len;
199 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
200
201 if (!name) {
202 /* removing last path segment */
203 if (ctx->path[ctx->path_len - 1] == '}') {
204 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
205 if (ctx->path[ctx->path_len] == '=') {
206 ctx->path[ctx->path_len++] = '}';
207 } else {
208 /* not a top-level special tag, remove also preceiding '/' */
209 goto remove_nodelevel;
210 }
211 } else {
212remove_nodelevel:
213 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
214 if (ctx->path_len == 0) {
215 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200216 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200217 }
218 }
219 /* set new terminating NULL-byte */
220 ctx->path[ctx->path_len] = '\0';
221 } else {
222 if (ctx->path_len > 1) {
223 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
224 /* extension of the special tag */
225 nextlevel = 2;
226 --ctx->path_len;
227 } else {
228 /* there is already some path, so add next level */
229 nextlevel = 1;
230 }
231 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
232
233 if (nextlevel != 2) {
234 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
235 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200236 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200237 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200238 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200239 }
240 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200241 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200242 }
Radek Krejciacc79042019-07-25 14:14:57 +0200243 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
244 /* output truncated */
245 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
246 } else {
247 ctx->path_len += len;
248 }
Radek Krejci327de162019-06-14 12:52:07 +0200249 }
250}
251
252/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100253 * @brief Duplicate the compiled pattern structure.
254 *
255 * Instead of duplicating memory, the reference counter in the @p orig is increased.
256 *
257 * @param[in] orig The pattern structure to duplicate.
258 * @return The duplicated structure to use.
259 */
Radek Krejci19a96102018-11-15 13:38:09 +0100260static struct lysc_pattern*
261lysc_pattern_dup(struct lysc_pattern *orig)
262{
263 ++orig->refcount;
264 return orig;
265}
266
Radek Krejcib56c7502019-02-13 14:19:54 +0100267/**
268 * @brief Duplicate the array of compiled patterns.
269 *
270 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
271 *
272 * @param[in] ctx Libyang context for logging.
273 * @param[in] orig The patterns sized array to duplicate.
274 * @return New sized array as a copy of @p orig.
275 * @return NULL in case of memory allocation error.
276 */
Radek Krejci19a96102018-11-15 13:38:09 +0100277static struct lysc_pattern**
278lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
279{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100280 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200281 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100282
Radek Krejcib56c7502019-02-13 14:19:54 +0100283 assert(orig);
284
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200285 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100286 LY_ARRAY_FOR(orig, u) {
287 dup[u] = lysc_pattern_dup(orig[u]);
288 LY_ARRAY_INCREMENT(dup);
289 }
290 return dup;
291}
292
Radek Krejcib56c7502019-02-13 14:19:54 +0100293/**
294 * @brief Duplicate compiled range structure.
295 *
296 * @param[in] ctx Libyang context for logging.
297 * @param[in] orig The range structure to be duplicated.
298 * @return New compiled range structure as a copy of @p orig.
299 * @return NULL in case of memory allocation error.
300 */
Radek Krejci19a96102018-11-15 13:38:09 +0100301struct lysc_range*
302lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
303{
304 struct lysc_range *dup;
305 LY_ERR ret;
306
Radek Krejcib56c7502019-02-13 14:19:54 +0100307 assert(orig);
308
Radek Krejci19a96102018-11-15 13:38:09 +0100309 dup = calloc(1, sizeof *dup);
310 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
311 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200312 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
313 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
314 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100315 }
316 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
317 DUP_STRING(ctx, orig->emsg, dup->emsg);
318 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
319
320 return dup;
321cleanup:
322 free(dup);
323 (void) ret; /* set but not used due to the return type */
324 return NULL;
325}
326
Radek Krejcib56c7502019-02-13 14:19:54 +0100327/**
328 * @brief Stack for processing if-feature expressions.
329 */
Radek Krejci19a96102018-11-15 13:38:09 +0100330struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100331 int size; /**< number of items in the stack */
332 int index; /**< first empty item */
333 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100334};
335
Radek Krejcib56c7502019-02-13 14:19:54 +0100336/**
337 * @brief Add @ref ifftokens into the stack.
338 * @param[in] stack The if-feature stack to use.
339 * @param[in] value One of the @ref ifftokens to store in the stack.
340 * @return LY_EMEM in case of memory allocation error
341 * @return LY_ESUCCESS if the value successfully stored.
342 */
Radek Krejci19a96102018-11-15 13:38:09 +0100343static LY_ERR
344iff_stack_push(struct iff_stack *stack, uint8_t value)
345{
346 if (stack->index == stack->size) {
347 stack->size += 4;
348 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
349 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
350 }
351 stack->stack[stack->index++] = value;
352 return LY_SUCCESS;
353}
354
Radek Krejcib56c7502019-02-13 14:19:54 +0100355/**
356 * @brief Get (and remove) the last item form the stack.
357 * @param[in] stack The if-feature stack to use.
358 * @return The value from the top of the stack.
359 */
Radek Krejci19a96102018-11-15 13:38:09 +0100360static uint8_t
361iff_stack_pop(struct iff_stack *stack)
362{
Radek Krejcib56c7502019-02-13 14:19:54 +0100363 assert(stack && stack->index);
364
Radek Krejci19a96102018-11-15 13:38:09 +0100365 stack->index--;
366 return stack->stack[stack->index];
367}
368
Radek Krejcib56c7502019-02-13 14:19:54 +0100369/**
370 * @brief Clean up the stack.
371 * @param[in] stack The if-feature stack to use.
372 */
Radek Krejci19a96102018-11-15 13:38:09 +0100373static void
374iff_stack_clean(struct iff_stack *stack)
375{
376 stack->size = 0;
377 free(stack->stack);
378}
379
Radek Krejcib56c7502019-02-13 14:19:54 +0100380/**
381 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
382 * (libyang format of the if-feature expression).
383 * @param[in,out] list The 2bits array to modify.
384 * @param[in] op The operand (@ref ifftokens) to store.
385 * @param[in] pos Position (0-based) where to store the given @p op.
386 */
Radek Krejci19a96102018-11-15 13:38:09 +0100387static void
388iff_setop(uint8_t *list, uint8_t op, int pos)
389{
390 uint8_t *item;
391 uint8_t mask = 3;
392
393 assert(pos >= 0);
394 assert(op <= 3); /* max 2 bits */
395
396 item = &list[pos / 4];
397 mask = mask << 2 * (pos % 4);
398 *item = (*item) & ~mask;
399 *item = (*item) | (op << 2 * (pos % 4));
400}
401
Radek Krejcib56c7502019-02-13 14:19:54 +0100402#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
403#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100404
Radek Krejci0af46292019-01-11 16:02:31 +0100405/**
406 * @brief Find a feature of the given name and referenced in the given module.
407 *
408 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
409 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
410 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
411 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
412 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
413 * assigned till that time will be still valid.
414 *
415 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
416 * @param[in] name Name of the feature including possible prefix.
417 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
418 * @return Pointer to the feature structure if found, NULL otherwise.
419 */
Radek Krejci19a96102018-11-15 13:38:09 +0100420static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100421lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100422{
423 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200424 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0af46292019-01-11 16:02:31 +0100425 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100426
427 for (i = 0; i < len; ++i) {
428 if (name[i] == ':') {
429 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100430 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100431 LY_CHECK_RET(!mod, NULL);
432
433 name = &name[i + 1];
434 len = len - i - 1;
435 }
436 }
437
438 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100439 if (mod->implemented) {
440 /* module is implemented so there is already the compiled schema */
441 flist = mod->compiled->features;
442 } else {
Michal Vasko33ff9422020-07-03 09:50:39 +0200443 flist = mod->dis_features;
Radek Krejci0af46292019-01-11 16:02:31 +0100444 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200445 LY_ARRAY_FOR(flist, u) {
446 f = &flist[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200447 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100448 return f;
449 }
450 }
451
452 return NULL;
453}
454
Michal Vasko8d544252020-03-02 10:19:52 +0100455/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100456 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
457 */
458static LY_ERR
459lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
460{
461 LY_ERR ret = LY_SUCCESS;
462
463 if (!ext_p->compiled) {
464 lysc_update_path(ctx, NULL, "{extension}");
465 lysc_update_path(ctx, NULL, ext_p->name);
466
467 /* compile the extension definition */
468 ext_p->compiled = calloc(1, sizeof **ext);
469 ext_p->compiled->refcount = 1;
470 DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name);
471 DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument);
472 ext_p->compiled->module = (struct lys_module *)ext_mod;
473 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
474
475 lysc_update_path(ctx, NULL, NULL);
476 lysc_update_path(ctx, NULL, NULL);
477
478 /* find extension definition plugin */
479 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
480 }
481
482 *ext = lysc_ext_dup(ext_p->compiled);
483
484done:
485 return ret;
486}
487
488/**
Michal Vasko8d544252020-03-02 10:19:52 +0100489 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
490 *
491 * @param[in] ctx Compilation context.
492 * @param[in] ext_p Parsed extension instance.
493 * @param[in,out] ext Prepared compiled extension instance.
494 * @param[in] parent Extension instance parent.
495 * @param[in] parent_type Extension instance parent type.
496 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
497 */
Radek Krejci19a96102018-11-15 13:38:09 +0100498static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100499lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
500 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100501{
Radek Krejci7c960162019-09-18 14:16:12 +0200502 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100503 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200504 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200505 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200506 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100507
508 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
509 ext->insubstmt = ext_p->insubstmt;
510 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200511 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200512 ext->parent = parent;
513 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100514
Radek Krejcif56e2a42019-09-09 14:15:25 +0200515 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200516
Radek Krejci19a96102018-11-15 13:38:09 +0100517 /* get module where the extension definition should be placed */
Radek Krejci7c960162019-09-18 14:16:12 +0200518 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u);
519 if (ext_p->yin) {
520 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
521 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
522 * YANG (import) prefix must be done here. */
523 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
524 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
525 u = 0;
526 } else if (ctx->mod_def->compiled) {
Radek Krejci7c960162019-09-18 14:16:12 +0200527 LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) {
528 if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) {
529 char *s;
Radek Krejci200f1062020-07-11 22:51:03 +0200530 LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]) == -1,
531 ret = LY_EMEM, cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200532 prefixed_name = lydict_insert_zc(ctx->ctx, s);
533 u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */
534 break;
535 }
536 }
537 }
538 if (!prefixed_name) {
539 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
540 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
541 goto cleanup;
542 }
543 } else {
544 prefixed_name = ext_p->name;
545 }
546 lysc_update_path(ctx, NULL, prefixed_name);
547
Michal Vasko8d544252020-03-02 10:19:52 +0100548 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200549 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100550 if (!ext_mod) {
551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
552 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
553 goto cleanup;
554 } else if (!ext_mod->parsed->extensions) {
555 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
556 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
557 prefixed_name, ext_mod->name);
558 goto cleanup;
559 }
Radek Krejci7c960162019-09-18 14:16:12 +0200560 }
561 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200562
Michal Vasko5fe75f12020-03-02 13:52:37 +0100563 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200564 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
565 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100566 /* compile extension definition and assign it */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200567 LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def));
Radek Krejci19a96102018-11-15 13:38:09 +0100568 break;
569 }
570 }
Radek Krejci7c960162019-09-18 14:16:12 +0200571 if (!ext->def) {
572 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
573 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
574 goto cleanup;
575 }
Radek Krejci0935f412019-08-20 16:15:18 +0200576
Radek Krejcif56e2a42019-09-09 14:15:25 +0200577 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
578 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
579 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200580 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200581 /* Schema was parsed from YIN and an argument is expected, ... */
582 struct lysp_stmt *stmt = NULL;
583
584 if (ext->def->flags & LYS_YINELEM_TRUE) {
585 /* ... argument was the first XML child element */
586 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
587 /* TODO check namespace of the statement */
588 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
589 stmt = ext_p->child;
590 }
591 }
592 } else {
593 /* ... argument was one of the XML attributes which are represented as child stmt
594 * with LYS_YIN_ATTR flag */
595 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
596 if (!strcmp(stmt->stmt, ext->def->argument)) {
597 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200598 break;
599 }
600 }
601 }
602 if (!stmt) {
603 /* missing extension's argument */
604 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200605 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
606 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200607
608 }
609 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
610 stmt->flags |= LYS_YIN_ARGUMENT;
611 }
Radek Krejci7c960162019-09-18 14:16:12 +0200612 if (prefixed_name != ext_p->name) {
613 lydict_remove(ctx->ctx, ext_p->name);
614 ext_p->name = prefixed_name;
615 if (!ext_p->argument && ext->argument) {
616 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
617 }
618 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200619
Radek Krejci0935f412019-08-20 16:15:18 +0200620 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200621 if (ext->argument) {
622 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
623 }
Radek Krejci7c960162019-09-18 14:16:12 +0200624 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200625 if (ext->argument) {
626 lysc_update_path(ctx, NULL, NULL);
627 }
Radek Krejci0935f412019-08-20 16:15:18 +0200628 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200629 ext_p->compiled = ext;
630
Radek Krejci7c960162019-09-18 14:16:12 +0200631 ret = LY_SUCCESS;
632cleanup:
633 if (prefixed_name && prefixed_name != ext_p->name) {
634 lydict_remove(ctx->ctx, prefixed_name);
635 }
636
Radek Krejcif56e2a42019-09-09 14:15:25 +0200637 lysc_update_path(ctx, NULL, NULL);
638 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200639
Radek Krejci7c960162019-09-18 14:16:12 +0200640 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200641}
642
643/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100644 * @brief Compile information from the if-feature statement
645 * @param[in] ctx Compile context.
646 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
Radek Krejcib56c7502019-02-13 14:19:54 +0100647 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
648 * @return LY_ERR value.
649 */
Radek Krejci19a96102018-11-15 13:38:09 +0100650static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200651lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100652{
653 const char *c = *value;
654 int r, rc = EXIT_FAILURE;
655 int i, j, last_not, checkversion = 0;
656 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
657 uint8_t op;
658 struct iff_stack stack = {0, 0, NULL};
659 struct lysc_feature *f;
660
661 assert(c);
662
663 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
664 for (i = j = last_not = 0; c[i]; i++) {
665 if (c[i] == '(') {
666 j++;
667 checkversion = 1;
668 continue;
669 } else if (c[i] == ')') {
670 j--;
671 continue;
672 } else if (isspace(c[i])) {
673 checkversion = 1;
674 continue;
675 }
676
677 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200678 int sp;
679 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
680 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
682 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
683 return LY_EVALID;
684 } else if (!isspace(c[i + r])) {
685 /* feature name starting with the not/and/or */
686 last_not = 0;
687 f_size++;
688 } else if (c[i] == 'n') { /* not operation */
689 if (last_not) {
690 /* double not */
691 expr_size = expr_size - 2;
692 last_not = 0;
693 } else {
694 last_not = 1;
695 }
696 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200697 if (f_exp != f_size) {
698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
699 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
700 return LY_EVALID;
701 }
Radek Krejci19a96102018-11-15 13:38:09 +0100702 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200703
Radek Krejci19a96102018-11-15 13:38:09 +0100704 /* not a not operation */
705 last_not = 0;
706 }
707 i += r;
708 } else {
709 f_size++;
710 last_not = 0;
711 }
712 expr_size++;
713
714 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200715 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100716 i--;
717 break;
718 }
719 i++;
720 }
721 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200722 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100723 /* not matching count of ( and ) */
724 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
725 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
726 return LY_EVALID;
727 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200728 if (f_exp != f_size) {
729 /* features do not match the needed arguments for the logical operations */
730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
731 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
732 "the required number of operands for the operations.", *value);
733 return LY_EVALID;
734 }
Radek Krejci19a96102018-11-15 13:38:09 +0100735
736 if (checkversion || expr_size > 1) {
737 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100738 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100739 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
740 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
741 return LY_EVALID;
742 }
743 }
744
745 /* allocate the memory */
746 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
747 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
748 stack.stack = malloc(expr_size * sizeof *stack.stack);
749 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
750
751 stack.size = expr_size;
752 f_size--; expr_size--; /* used as indexes from now */
753
754 for (i--; i >= 0; i--) {
755 if (c[i] == ')') {
756 /* push it on stack */
757 iff_stack_push(&stack, LYS_IFF_RP);
758 continue;
759 } else if (c[i] == '(') {
760 /* pop from the stack into result all operators until ) */
761 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
762 iff_setop(iff->expr, op, expr_size--);
763 }
764 continue;
765 } else if (isspace(c[i])) {
766 continue;
767 }
768
769 /* end of operator or operand -> find beginning and get what is it */
770 j = i + 1;
771 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
772 i--;
773 }
774 i++; /* go back by one step */
775
776 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
777 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
778 /* double not */
779 iff_stack_pop(&stack);
780 } else {
781 /* not has the highest priority, so do not pop from the stack
782 * as in case of AND and OR */
783 iff_stack_push(&stack, LYS_IFF_NOT);
784 }
785 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
786 /* as for OR - pop from the stack all operators with the same or higher
787 * priority and store them to the result, then push the AND to the stack */
788 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
789 op = iff_stack_pop(&stack);
790 iff_setop(iff->expr, op, expr_size--);
791 }
792 iff_stack_push(&stack, LYS_IFF_AND);
793 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
794 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
795 op = iff_stack_pop(&stack);
796 iff_setop(iff->expr, op, expr_size--);
797 }
798 iff_stack_push(&stack, LYS_IFF_OR);
799 } else {
800 /* feature name, length is j - i */
801
802 /* add it to the expression */
803 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
804
805 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100806 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
807 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
808 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
809 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100810 iff->features[f_size] = f;
811 LY_ARRAY_INCREMENT(iff->features);
812 f_size--;
813 }
814 }
815 while (stack.index) {
816 op = iff_stack_pop(&stack);
817 iff_setop(iff->expr, op, expr_size--);
818 }
819
820 if (++expr_size || ++f_size) {
821 /* not all expected operators and operands found */
822 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
823 "Invalid value \"%s\" of if-feature - processing error.", *value);
824 rc = LY_EINT;
825 } else {
826 rc = LY_SUCCESS;
827 }
828
829error:
830 /* cleanup */
831 iff_stack_clean(&stack);
832
833 return rc;
834}
835
Radek Krejcib56c7502019-02-13 14:19:54 +0100836/**
Michal Vasko175012e2019-11-06 15:49:14 +0100837 * @brief Get the XPath context node for the given schema node.
838 * @param[in] start The schema node where the XPath expression appears.
839 * @return The context node to evaluate XPath expression in given schema node.
840 * @return NULL in case the context node is the root node.
841 */
842static struct lysc_node *
843lysc_xpath_context(struct lysc_node *start)
844{
Michal Vasko1bf09392020-03-27 12:38:10 +0100845 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF));
Michal Vasko175012e2019-11-06 15:49:14 +0100846 start = start->parent);
847 return start;
848}
849
850/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100851 * @brief Compile information from the when statement
852 * @param[in] ctx Compile context.
853 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100854 * @param[in] flags Flags of the node with the "when" defiition.
855 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100856 * @param[out] when Pointer where to store pointer to the created compiled when structure.
857 * @return LY_ERR value.
858 */
Radek Krejci19a96102018-11-15 13:38:09 +0100859static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100860lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100861{
Radek Krejci19a96102018-11-15 13:38:09 +0100862 LY_ERR ret = LY_SUCCESS;
863
Radek Krejci00b874b2019-02-12 10:54:50 +0100864 *when = calloc(1, sizeof **when);
865 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200866 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200867 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100868 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100869 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
870 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
871 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200872 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100873 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100874
875done:
876 return ret;
877}
878
Radek Krejcib56c7502019-02-13 14:19:54 +0100879/**
880 * @brief Compile information from the must statement
881 * @param[in] ctx Compile context.
882 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100883 * @param[in,out] must Prepared (empty) compiled must structure to fill.
884 * @return LY_ERR value.
885 */
Radek Krejci19a96102018-11-15 13:38:09 +0100886static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200887lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100888{
Radek Krejci19a96102018-11-15 13:38:09 +0100889 LY_ERR ret = LY_SUCCESS;
890
Michal Vasko004d3152020-06-11 19:59:22 +0200891 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100892 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200893 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100894 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
895 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100896 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
897 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200898 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100899
900done:
901 return ret;
902}
903
Radek Krejcib56c7502019-02-13 14:19:54 +0100904/**
905 * @brief Compile information from the import statement
906 * @param[in] ctx Compile context.
907 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100908 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
909 * @return LY_ERR value.
910 */
Radek Krejci19a96102018-11-15 13:38:09 +0100911static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200912lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100913{
Radek Krejci19a96102018-11-15 13:38:09 +0100914 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100915 LY_ERR ret = LY_SUCCESS;
916
917 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200918 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100919 imp->module = imp_p->module;
920
Radek Krejci7f2a5362018-11-28 13:05:37 +0100921 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
922 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100923 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100924 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100925 /* try to use filepath if present */
926 if (imp->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200927 struct ly_in *in;
928 if (ly_in_new_filepath(imp->module->filepath, 0, &in)) {
929 LOGINT(ctx->ctx);
930 } else {
931 mod = lys_parse(ctx->ctx, in, !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
932 if (mod != imp->module) {
933 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", imp->module->filepath, imp->module->name);
934 mod = NULL;
935 }
Radek Krejci19a96102018-11-15 13:38:09 +0100936 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200937 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100938 }
939 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100940 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100941 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 +0100942 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100943 return LY_ENOTFOUND;
944 }
945 }
Radek Krejci19a96102018-11-15 13:38:09 +0100946 }
947
948done:
949 return ret;
950}
951
Michal Vasko33ff9422020-07-03 09:50:39 +0200952LY_ERR
953lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
954 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +0100955{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200956 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +0200957 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +0100958 LY_ERR ret = LY_SUCCESS;
959
Michal Vasko33ff9422020-07-03 09:50:39 +0200960 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +0200961
Michal Vasko33ff9422020-07-03 09:50:39 +0200962 if (!ctx_sc) {
963 context.ctx = ctx;
964 context.mod = module;
965 context.path_len = 1;
966 context.path[0] = '/';
967 ctx_sc = &context;
968 }
Radek Krejci19a96102018-11-15 13:38:09 +0100969
Michal Vasko33ff9422020-07-03 09:50:39 +0200970 if (!identities_p) {
971 return LY_SUCCESS;
972 }
973 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200974 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +0200975 }
976
977 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200978 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +0200979 LY_ARRAY_FOR(identities_p, u) {
980 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
981
982 LY_ARRAY_INCREMENT(*identities);
983 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
984 DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name);
985 DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc);
986 DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref);
987 (*identities)[offset + u].module = ctx_sc->mod;
988 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
989 lys_compile_iffeature, ret, done);
990 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
991 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
992 LYEXT_PAR_IDENT, ret, done);
993 (*identities)[offset + u].flags = identities_p[u].flags;
994
995 lysc_update_path(ctx_sc, NULL, NULL);
996 }
997 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100998done:
999 return ret;
1000}
1001
Radek Krejcib56c7502019-02-13 14:19:54 +01001002/**
1003 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1004 *
1005 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1006 *
1007 * @param[in] ctx Compile context for logging.
1008 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1009 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1010 * @return LY_SUCCESS if everything is ok.
1011 * @return LY_EVALID if the identity is derived from itself.
1012 */
Radek Krejci38222632019-02-12 16:55:05 +01001013static LY_ERR
1014lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1015{
1016 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001017 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001018 struct ly_set recursion = {0};
1019 struct lysc_ident *drv;
1020
1021 if (!derived) {
1022 return LY_SUCCESS;
1023 }
1024
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001025 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001026 if (ident == derived[u]) {
1027 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1028 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1029 goto cleanup;
1030 }
1031 ly_set_add(&recursion, derived[u], 0);
1032 }
1033
1034 for (v = 0; v < recursion.count; ++v) {
1035 drv = recursion.objs[v];
1036 if (!drv->derived) {
1037 continue;
1038 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001039 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001040 if (ident == drv->derived[u]) {
1041 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1042 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1043 goto cleanup;
1044 }
1045 ly_set_add(&recursion, drv->derived[u], 0);
1046 }
1047 }
1048 ret = LY_SUCCESS;
1049
1050cleanup:
1051 ly_set_erase(&recursion, NULL);
1052 return ret;
1053}
1054
Radek Krejcia3045382018-11-22 14:30:31 +01001055/**
1056 * @brief Find and process the referenced base identities from another identity or identityref
1057 *
Radek Krejciaca74032019-06-04 08:53:06 +02001058 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001059 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1060 * to distinguish these two use cases.
1061 *
1062 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1063 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001064 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001065 * @param[in] bases Array of bases of identityref to fill in.
1066 * @return LY_ERR value.
1067 */
Radek Krejci19a96102018-11-15 13:38:09 +01001068static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001069lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
1070 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001071{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001072 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001073 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001074 struct lys_module *mod;
Michal Vasko33ff9422020-07-03 09:50:39 +02001075 struct lysc_ident **idref, *identities;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001076
1077 assert(ident || bases);
1078
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001079 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001080 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1081 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1082 return LY_EVALID;
1083 }
1084
Michal Vasko33ff9422020-07-03 09:50:39 +02001085 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001086 s = strchr(bases_p[u], ':');
1087 if (s) {
1088 /* prefixed identity */
1089 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001090 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001091 } else {
1092 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001093 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001094 }
1095 if (!mod) {
1096 if (ident) {
1097 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1098 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1099 } else {
1100 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1101 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1102 }
1103 return LY_EVALID;
1104 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001105
Radek Krejci555cb5b2018-11-16 14:54:33 +01001106 idref = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02001107 if (mod->compiled) {
1108 identities = mod->compiled->identities;
1109 } else {
1110 identities = mod->dis_identities;
1111 }
1112 LY_ARRAY_FOR(identities, v) {
1113 if (!strcmp(name, identities[v].name)) {
1114 if (ident) {
1115 if (ident == &identities[v]) {
1116 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1117 "Identity \"%s\" is derived from itself.", ident->name);
1118 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001119 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001120 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived));
1121 /* we have match! store the backlink */
1122 LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM);
1123 *idref = ident;
1124 } else {
1125 /* we have match! store the found identity */
1126 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
1127 *idref = &identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001128 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001129 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001130 }
1131 }
1132 if (!idref || !(*idref)) {
1133 if (ident) {
1134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1135 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1136 } else {
1137 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1138 "Unable to find base (%s) of identityref.", bases_p[u]);
1139 }
1140 return LY_EVALID;
1141 }
1142 }
1143 return LY_SUCCESS;
1144}
1145
Radek Krejcia3045382018-11-22 14:30:31 +01001146/**
1147 * @brief For the given array of identities, set the backlinks from all their base identities.
1148 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1149 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1150 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1151 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1152 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001153static LY_ERR
1154lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1155{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001156 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001157
Michal Vasko33ff9422020-07-03 09:50:39 +02001158 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001159 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001160 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001161 continue;
1162 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001163 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001164 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 +02001165 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001166 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001167 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001168 return LY_SUCCESS;
1169}
1170
Radek Krejci0af46292019-01-11 16:02:31 +01001171LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001172lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
1173 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001174{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001175 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001176 struct lysc_ctx context = {0};
1177
Radek Krejci327de162019-06-14 12:52:07 +02001178 assert(ctx_sc || ctx);
1179
1180 if (!ctx_sc) {
1181 context.ctx = ctx;
1182 context.mod = module;
1183 context.path_len = 1;
1184 context.path[0] = '/';
1185 ctx_sc = &context;
1186 }
Radek Krejci0af46292019-01-11 16:02:31 +01001187
1188 if (!features_p) {
1189 return LY_SUCCESS;
1190 }
1191 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001192 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001193 }
1194
Radek Krejci327de162019-06-14 12:52:07 +02001195 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001196 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001197 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001198 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1199
Radek Krejci0af46292019-01-11 16:02:31 +01001200 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001201 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci327de162019-06-14 12:52:07 +02001202 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1203 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1204 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001205 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001206 (*features)[offset + u].module = ctx_sc->mod;
1207
1208 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001209 }
Radek Krejci327de162019-06-14 12:52:07 +02001210 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001211
1212 return LY_SUCCESS;
1213}
1214
Radek Krejcia3045382018-11-22 14:30:31 +01001215/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001216 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001217 *
1218 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1219 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001220 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001221 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1222 * being currently processed).
1223 * @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 +01001224 * @return LY_SUCCESS if everything is ok.
1225 * @return LY_EVALID if the feature references indirectly itself.
1226 */
1227static LY_ERR
1228lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1229{
1230 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001231 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001232 struct ly_set recursion = {0};
1233 struct lysc_feature *drv;
1234
1235 if (!depfeatures) {
1236 return LY_SUCCESS;
1237 }
1238
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001239 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001240 if (feature == depfeatures[u]) {
1241 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1242 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1243 goto cleanup;
1244 }
1245 ly_set_add(&recursion, depfeatures[u], 0);
1246 }
1247
1248 for (v = 0; v < recursion.count; ++v) {
1249 drv = recursion.objs[v];
1250 if (!drv->depfeatures) {
1251 continue;
1252 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001253 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001254 if (feature == drv->depfeatures[u]) {
1255 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1256 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1257 goto cleanup;
1258 }
1259 ly_set_add(&recursion, drv->depfeatures[u], 0);
1260 }
1261 }
1262 ret = LY_SUCCESS;
1263
1264cleanup:
1265 ly_set_erase(&recursion, NULL);
1266 return ret;
1267}
1268
1269/**
Radek Krejci0af46292019-01-11 16:02:31 +01001270 * @brief Create pre-compiled features array.
1271 *
1272 * See lys_feature_precompile() for more details.
1273 *
Radek Krejcia3045382018-11-22 14:30:31 +01001274 * @param[in] ctx Compile context.
1275 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001276 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001277 * @return LY_ERR value.
1278 */
Radek Krejci19a96102018-11-15 13:38:09 +01001279static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001280lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001281{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001282 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001283 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001284 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001285
Radek Krejci327de162019-06-14 12:52:07 +02001286
Radek Krejci0af46292019-01-11 16:02:31 +01001287 /* find the preprecompiled feature */
1288 LY_ARRAY_FOR(features, x) {
1289 if (strcmp(features[x].name, feature_p->name)) {
1290 continue;
1291 }
1292 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001293 lysc_update_path(ctx, NULL, "{feature}");
1294 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001295
Radek Krejci0af46292019-01-11 16:02:31 +01001296 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001297 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001298 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001299 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001300 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001301 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001302 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001303 /* check for circular dependency - direct reference first,... */
1304 if (feature == feature->iffeatures[u].features[v]) {
1305 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1306 "Feature \"%s\" is referenced from itself.", feature->name);
1307 return LY_EVALID;
1308 }
1309 /* ... and indirect circular reference */
1310 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1311
Radek Krejci0af46292019-01-11 16:02:31 +01001312 /* add itself into the dependants list */
1313 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1314 *df = feature;
1315 }
Radek Krejci19a96102018-11-15 13:38:09 +01001316 }
Radek Krejci19a96102018-11-15 13:38:09 +01001317 }
1318 }
Radek Krejci327de162019-06-14 12:52:07 +02001319 lysc_update_path(ctx, NULL, NULL);
1320 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001321 done:
1322 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001323 }
Radek Krejci0af46292019-01-11 16:02:31 +01001324
1325 LOGINT(ctx->ctx);
1326 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001327}
1328
Radek Krejcib56c7502019-02-13 14:19:54 +01001329/**
1330 * @brief Revert compiled list of features back to the precompiled state.
1331 *
1332 * 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 +02001333 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001334 *
1335 * @param[in] ctx Compilation context.
1336 * @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 +02001337 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001338 */
Radek Krejci95710c92019-02-11 15:49:55 +01001339static void
1340lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1341{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001342 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001343
Michal Vasko33ff9422020-07-03 09:50:39 +02001344 /* keep the dis_features list until the complete lys_module is freed */
1345 mod->dis_features = mod->compiled->features;
Radek Krejci95710c92019-02-11 15:49:55 +01001346 mod->compiled->features = NULL;
1347
Michal Vasko33ff9422020-07-03 09:50:39 +02001348 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001349 * which may points into the data being freed here */
Michal Vasko33ff9422020-07-03 09:50:39 +02001350 LY_ARRAY_FOR(mod->dis_features, u) {
1351 LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) {
1352 lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001353 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001354 LY_ARRAY_FREE(mod->dis_features[u].iffeatures);
1355 mod->dis_features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001356
Michal Vasko33ff9422020-07-03 09:50:39 +02001357 LY_ARRAY_FOR(mod->dis_features[u].exts, v) {
1358 lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001359 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001360 LY_ARRAY_FREE(mod->dis_features[u].exts);
1361 mod->dis_features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001362 }
1363}
1364
Radek Krejcia3045382018-11-22 14:30:31 +01001365/**
1366 * @brief Validate and normalize numeric value from a range definition.
1367 * @param[in] ctx Compile context.
1368 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1369 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1370 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1371 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1372 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1373 * @param[in] value String value of the range boundary.
1374 * @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.
1375 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1376 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1377 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001378LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001379range_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 +01001380{
Radek Krejci6cba4292018-11-15 17:33:29 +01001381 size_t fraction = 0, size;
1382
Radek Krejci19a96102018-11-15 13:38:09 +01001383 *len = 0;
1384
1385 assert(value);
1386 /* parse value */
1387 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1388 return LY_EVALID;
1389 }
1390
1391 if ((value[*len] == '-') || (value[*len] == '+')) {
1392 ++(*len);
1393 }
1394
1395 while (isdigit(value[*len])) {
1396 ++(*len);
1397 }
1398
1399 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001400 if (basetype == LY_TYPE_DEC64) {
1401 goto decimal;
1402 } else {
1403 *valcopy = strndup(value, *len);
1404 return LY_SUCCESS;
1405 }
Radek Krejci19a96102018-11-15 13:38:09 +01001406 }
1407 fraction = *len;
1408
1409 ++(*len);
1410 while (isdigit(value[*len])) {
1411 ++(*len);
1412 }
1413
Radek Krejci6cba4292018-11-15 17:33:29 +01001414 if (basetype == LY_TYPE_DEC64) {
1415decimal:
1416 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001417 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001418 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1419 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1420 *len, value, frdigits);
1421 return LY_EINVAL;
1422 }
1423 if (fraction) {
1424 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1425 } else {
1426 size = (*len) + frdigits + 1;
1427 }
1428 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001429 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1430
Radek Krejci6cba4292018-11-15 17:33:29 +01001431 (*valcopy)[size - 1] = '\0';
1432 if (fraction) {
1433 memcpy(&(*valcopy)[0], &value[0], fraction);
1434 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1435 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1436 } else {
1437 memcpy(&(*valcopy)[0], &value[0], *len);
1438 memset(&(*valcopy)[*len], '0', frdigits);
1439 }
Radek Krejci19a96102018-11-15 13:38:09 +01001440 }
1441 return LY_SUCCESS;
1442}
1443
Radek Krejcia3045382018-11-22 14:30:31 +01001444/**
1445 * @brief Check that values in range are in ascendant order.
1446 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001447 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1448 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001449 * @param[in] value Current value to check.
1450 * @param[in] prev_value The last seen value.
1451 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1452 */
Radek Krejci19a96102018-11-15 13:38:09 +01001453static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001454range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001455{
1456 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001457 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001458 return LY_EEXIST;
1459 }
1460 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001461 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001462 return LY_EEXIST;
1463 }
1464 }
1465 return LY_SUCCESS;
1466}
1467
Radek Krejcia3045382018-11-22 14:30:31 +01001468/**
1469 * @brief Set min/max value of the range part.
1470 * @param[in] ctx Compile context.
1471 * @param[in] part Range part structure to fill.
1472 * @param[in] max Flag to distinguish if storing min or max value.
1473 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1474 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1475 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1476 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1477 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001478 * @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 +01001479 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1480 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1481 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1482 * frdigits value), LY_EMEM.
1483 */
Radek Krejci19a96102018-11-15 13:38:09 +01001484static LY_ERR
1485range_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 +01001486 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001487{
1488 LY_ERR ret = LY_SUCCESS;
1489 char *valcopy = NULL;
1490 size_t len;
1491
1492 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001493 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001494 LY_CHECK_GOTO(ret, finalize);
1495 }
1496 if (!valcopy && base_range) {
1497 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001498 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001499 } else {
1500 part->min_64 = base_range->parts[0].min_64;
1501 }
1502 if (!first) {
1503 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1504 }
1505 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001506 }
1507
1508 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001509 case LY_TYPE_INT8: /* range */
1510 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001511 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 +01001512 } else if (max) {
1513 part->max_64 = INT64_C(127);
1514 } else {
1515 part->min_64 = INT64_C(-128);
1516 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001517 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001518 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001519 }
1520 break;
1521 case LY_TYPE_INT16: /* range */
1522 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001523 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 +01001524 } else if (max) {
1525 part->max_64 = INT64_C(32767);
1526 } else {
1527 part->min_64 = INT64_C(-32768);
1528 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001529 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001530 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001531 }
1532 break;
1533 case LY_TYPE_INT32: /* range */
1534 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001535 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 +01001536 } else if (max) {
1537 part->max_64 = INT64_C(2147483647);
1538 } else {
1539 part->min_64 = INT64_C(-2147483648);
1540 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001541 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001542 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001543 }
1544 break;
1545 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001546 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001547 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001548 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001549 max ? &part->max_64 : &part->min_64);
1550 } else if (max) {
1551 part->max_64 = INT64_C(9223372036854775807);
1552 } else {
1553 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1554 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001555 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001556 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001557 }
1558 break;
1559 case LY_TYPE_UINT8: /* range */
1560 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001561 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 +01001562 } else if (max) {
1563 part->max_u64 = UINT64_C(255);
1564 } else {
1565 part->min_u64 = UINT64_C(0);
1566 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001567 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001568 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001569 }
1570 break;
1571 case LY_TYPE_UINT16: /* range */
1572 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001573 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 +01001574 } else if (max) {
1575 part->max_u64 = UINT64_C(65535);
1576 } else {
1577 part->min_u64 = UINT64_C(0);
1578 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001579 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001580 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001581 }
1582 break;
1583 case LY_TYPE_UINT32: /* range */
1584 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001585 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 +01001586 } else if (max) {
1587 part->max_u64 = UINT64_C(4294967295);
1588 } else {
1589 part->min_u64 = UINT64_C(0);
1590 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001591 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001592 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001593 }
1594 break;
1595 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001596 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001597 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001598 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001599 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 +01001600 } else if (max) {
1601 part->max_u64 = UINT64_C(18446744073709551615);
1602 } else {
1603 part->min_u64 = UINT64_C(0);
1604 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001605 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001606 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001607 }
1608 break;
1609 default:
1610 LOGINT(ctx->ctx);
1611 ret = LY_EINT;
1612 }
1613
Radek Krejci5969f272018-11-23 10:03:58 +01001614finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001615 if (ret == LY_EDENIED) {
1616 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1617 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1618 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1619 } else if (ret == LY_EVALID) {
1620 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1621 "Invalid %s restriction - invalid value \"%s\".",
1622 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1623 } else if (ret == LY_EEXIST) {
1624 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1625 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001626 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001627 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001628 } else if (!ret && value) {
1629 *value = *value + len;
1630 }
1631 free(valcopy);
1632 return ret;
1633}
1634
Radek Krejcia3045382018-11-22 14:30:31 +01001635/**
1636 * @brief Compile the parsed range restriction.
1637 * @param[in] ctx Compile context.
1638 * @param[in] range_p Parsed range structure to compile.
1639 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1640 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1641 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1642 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1643 * range restriction must be more restrictive than the base_range.
1644 * @param[in,out] range Pointer to the created current range structure.
1645 * @return LY_ERR value.
1646 */
Radek Krejci19a96102018-11-15 13:38:09 +01001647static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001648lys_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 +01001649 struct lysc_range *base_range, struct lysc_range **range)
1650{
1651 LY_ERR ret = LY_EVALID;
1652 const char *expr;
1653 struct lysc_range_part *parts = NULL, *part;
1654 int range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001655 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001656
1657 assert(range);
1658 assert(range_p);
1659
1660 expr = range_p->arg;
1661 while(1) {
1662 if (isspace(*expr)) {
1663 ++expr;
1664 } else if (*expr == '\0') {
1665 if (range_expected) {
1666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1667 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1668 length_restr ? "length" : "range", range_p->arg);
1669 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001670 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1672 "Invalid %s restriction - unexpected end of the expression (%s).",
1673 length_restr ? "length" : "range", range_p->arg);
1674 goto cleanup;
1675 }
1676 parts_done++;
1677 break;
1678 } else if (!strncmp(expr, "min", 3)) {
1679 if (parts) {
1680 /* min cannot be used elsewhere than in the first part */
1681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1682 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1683 expr - range_p->arg, range_p->arg);
1684 goto cleanup;
1685 }
1686 expr += 3;
1687
1688 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001689 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 +01001690 part->max_64 = part->min_64;
1691 } else if (*expr == '|') {
1692 if (!parts || range_expected) {
1693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1694 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1695 goto cleanup;
1696 }
1697 expr++;
1698 parts_done++;
1699 /* process next part of the expression */
1700 } else if (!strncmp(expr, "..", 2)) {
1701 expr += 2;
1702 while (isspace(*expr)) {
1703 expr++;
1704 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001705 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1707 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1708 goto cleanup;
1709 }
1710 /* continue expecting the upper boundary */
1711 range_expected = 1;
1712 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1713 /* number */
1714 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001715 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001716 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 +01001717 range_expected = 0;
1718 } else {
1719 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001720 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 +01001721 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001722 part->max_64 = part->min_64;
1723 }
1724
1725 /* continue with possible another expression part */
1726 } else if (!strncmp(expr, "max", 3)) {
1727 expr += 3;
1728 while (isspace(*expr)) {
1729 expr++;
1730 }
1731 if (*expr != '\0') {
1732 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1733 length_restr ? "length" : "range", expr);
1734 goto cleanup;
1735 }
1736 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001737 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001738 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 +01001739 range_expected = 0;
1740 } else {
1741 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001742 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 +01001743 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001744 part->min_64 = part->max_64;
1745 }
1746 } else {
1747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1748 length_restr ? "length" : "range", expr);
1749 goto cleanup;
1750 }
1751 }
1752
1753 /* check with the previous range/length restriction */
1754 if (base_range) {
1755 switch (basetype) {
1756 case LY_TYPE_BINARY:
1757 case LY_TYPE_UINT8:
1758 case LY_TYPE_UINT16:
1759 case LY_TYPE_UINT32:
1760 case LY_TYPE_UINT64:
1761 case LY_TYPE_STRING:
1762 uns = 1;
1763 break;
1764 case LY_TYPE_DEC64:
1765 case LY_TYPE_INT8:
1766 case LY_TYPE_INT16:
1767 case LY_TYPE_INT32:
1768 case LY_TYPE_INT64:
1769 uns = 0;
1770 break;
1771 default:
1772 LOGINT(ctx->ctx);
1773 ret = LY_EINT;
1774 goto cleanup;
1775 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001776 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001777 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1778 goto baseerror;
1779 }
1780 /* current lower bound is not lower than the base */
1781 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1782 /* base has single value */
1783 if (base_range->parts[v].min_64 == parts[u].min_64) {
1784 /* both lower bounds are the same */
1785 if (parts[u].min_64 != parts[u].max_64) {
1786 /* current continues with a range */
1787 goto baseerror;
1788 } else {
1789 /* equal single values, move both forward */
1790 ++v;
1791 continue;
1792 }
1793 } else {
1794 /* base is single value lower than current range, so the
1795 * value from base range is removed in the current,
1796 * move only base and repeat checking */
1797 ++v;
1798 --u;
1799 continue;
1800 }
1801 } else {
1802 /* base is the range */
1803 if (parts[u].min_64 == parts[u].max_64) {
1804 /* current is a single value */
1805 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1806 /* current is behind the base range, so base range is omitted,
1807 * move the base and keep the current for further check */
1808 ++v;
1809 --u;
1810 } /* else it is within the base range, so move the current, but keep the base */
1811 continue;
1812 } else {
1813 /* both are ranges - check the higher bound, the lower was already checked */
1814 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1815 /* higher bound is higher than the current higher bound */
1816 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1817 /* but the current lower bound is also higher, so the base range is omitted,
1818 * continue with the same current, but move the base */
1819 --u;
1820 ++v;
1821 continue;
1822 }
1823 /* current range starts within the base range but end behind it */
1824 goto baseerror;
1825 } else {
1826 /* current range is smaller than the base,
1827 * move current, but stay with the base */
1828 continue;
1829 }
1830 }
1831 }
1832 }
1833 if (u != parts_done) {
1834baseerror:
1835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1836 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1837 length_restr ? "length" : "range", range_p->arg);
1838 goto cleanup;
1839 }
1840 }
1841
1842 if (!(*range)) {
1843 *range = calloc(1, sizeof **range);
1844 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1845 }
1846
Radek Krejcic8b31002019-01-08 10:24:45 +01001847 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001848 if (range_p->eapptag) {
1849 lydict_remove(ctx->ctx, (*range)->eapptag);
1850 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1851 }
1852 if (range_p->emsg) {
1853 lydict_remove(ctx->ctx, (*range)->emsg);
1854 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1855 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001856 if (range_p->dsc) {
1857 lydict_remove(ctx->ctx, (*range)->dsc);
1858 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1859 }
1860 if (range_p->ref) {
1861 lydict_remove(ctx->ctx, (*range)->ref);
1862 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1863 }
Radek Krejci19a96102018-11-15 13:38:09 +01001864 /* extensions are taken only from the last range by the caller */
1865
1866 (*range)->parts = parts;
1867 parts = NULL;
1868 ret = LY_SUCCESS;
1869cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001870 LY_ARRAY_FREE(parts);
1871
1872 return ret;
1873}
1874
1875/**
1876 * @brief Checks pattern syntax.
1877 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001878 * @param[in] ctx Context.
1879 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001880 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001881 * @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 +01001882 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001883 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001884LY_ERR
1885lys_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 +01001886{
Michal Vasko40a00082020-05-27 15:20:01 +02001887 int idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001888 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001889 int err_code;
1890 const char *orig_ptr;
1891 PCRE2_SIZE err_offset;
1892 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001893#define URANGE_LEN 19
1894 char *ublock2urange[][2] = {
1895 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1896 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1897 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1898 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1899 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1900 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1901 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1902 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1903 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1904 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1905 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1906 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1907 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1908 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1909 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1910 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1911 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1912 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1913 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1914 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1915 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1916 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1917 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1918 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1919 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1920 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1921 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1922 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1923 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1924 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1925 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1926 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1927 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1928 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1929 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1930 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1931 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1932 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1933 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1934 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1935 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1936 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1937 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1938 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1939 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1940 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1941 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1942 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1943 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1944 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1945 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1946 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1947 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1948 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1949 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1950 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1951 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1952 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1953 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1954 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1955 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1956 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1957 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1958 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1959 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1960 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1961 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1962 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1963 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1964 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1965 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1966 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1967 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1968 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1969 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1970 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1971 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1972 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1973 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1974 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1975 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1976 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1977 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1978 {NULL, NULL}
1979 };
1980
1981 /* adjust the expression to a Perl equivalent
1982 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1983
Michal Vasko40a00082020-05-27 15:20:01 +02001984 /* allocate space for the transformed pattern */
1985 size = strlen(pattern) + 1;
1986 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001988 perl_regex[0] = '\0';
1989
Michal Vasko40a00082020-05-27 15:20:01 +02001990 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1991 brack = 0;
1992 idx = 0;
1993 orig_ptr = pattern;
1994 while (orig_ptr[0]) {
1995 switch (orig_ptr[0]) {
1996 case '$':
1997 case '^':
1998 if (!brack) {
1999 /* make space for the extra character */
2000 ++size;
2001 perl_regex = ly_realloc(perl_regex, size);
2002 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002003
Michal Vasko40a00082020-05-27 15:20:01 +02002004 /* print escape slash */
2005 perl_regex[idx] = '\\';
2006 ++idx;
2007 }
2008 break;
2009 case '[':
2010 /* must not be escaped */
2011 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2012 ++brack;
2013 }
2014 break;
2015 case ']':
2016 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2017 /* pattern was checked and compiled already */
2018 assert(brack);
2019 --brack;
2020 }
2021 break;
2022 default:
2023 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002024 }
Michal Vasko40a00082020-05-27 15:20:01 +02002025
2026 /* copy char */
2027 perl_regex[idx] = orig_ptr[0];
2028
2029 ++idx;
2030 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002031 }
Michal Vasko40a00082020-05-27 15:20:01 +02002032 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002033
2034 /* substitute Unicode Character Blocks with exact Character Ranges */
2035 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2036 start = ptr - perl_regex;
2037
2038 ptr = strchr(ptr, '}');
2039 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002041 pattern, perl_regex + start + 2, "unterminated character property");
2042 free(perl_regex);
2043 return LY_EVALID;
2044 }
2045 end = (ptr - perl_regex) + 1;
2046
2047 /* need more space */
2048 if (end - start < URANGE_LEN) {
2049 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002050 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002051 }
2052
2053 /* find our range */
2054 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2055 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2056 break;
2057 }
2058 }
2059 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002061 pattern, perl_regex + start + 5, "unknown block name");
2062 free(perl_regex);
2063 return LY_EVALID;
2064 }
2065
2066 /* 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 +02002067 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002068 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002069 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002070 }
2071 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002072 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002073 }
2074 }
Michal Vasko40a00082020-05-27 15:20:01 +02002075 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002076 /* skip brackets */
2077 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2078 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2079 } else {
2080 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2081 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2082 }
2083 }
2084
2085 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002086 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2087 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002088 &err_code, &err_offset, NULL);
2089 if (!code_local) {
2090 PCRE2_UCHAR err_msg[256] = {0};
2091 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002093 free(perl_regex);
2094 return LY_EVALID;
2095 }
2096 free(perl_regex);
2097
Radek Krejci54579462019-04-30 12:47:06 +02002098 if (code) {
2099 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002100 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002101 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002102 }
2103
2104 return LY_SUCCESS;
2105
2106#undef URANGE_LEN
2107}
2108
Radek Krejcia3045382018-11-22 14:30:31 +01002109/**
2110 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2111 * @param[in] ctx Compile context.
2112 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002113 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2114 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2115 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2116 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2117 */
Radek Krejci19a96102018-11-15 13:38:09 +01002118static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002119lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002120 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2121{
2122 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002123 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002124 LY_ERR ret = LY_SUCCESS;
2125
2126 /* first, copy the patterns from the base type */
2127 if (base_patterns) {
2128 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2129 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2130 }
2131
2132 LY_ARRAY_FOR(patterns_p, u) {
2133 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2134 *pattern = calloc(1, sizeof **pattern);
2135 ++(*pattern)->refcount;
2136
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002138 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002139
2140 if (patterns_p[u].arg[0] == 0x15) {
2141 (*pattern)->inverted = 1;
2142 }
Radek Krejci54579462019-04-30 12:47:06 +02002143 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002144 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2145 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002146 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2147 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002148 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002149 }
2150done:
2151 return ret;
2152}
2153
Radek Krejcia3045382018-11-22 14:30:31 +01002154/**
2155 * @brief map of the possible restrictions combination for the specific built-in type.
2156 */
Radek Krejci19a96102018-11-15 13:38:09 +01002157static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2158 0 /* LY_TYPE_UNKNOWN */,
2159 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002160 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2161 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2162 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2163 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2164 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002165 LYS_SET_BIT /* LY_TYPE_BITS */,
2166 0 /* LY_TYPE_BOOL */,
2167 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2168 0 /* LY_TYPE_EMPTY */,
2169 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2170 LYS_SET_BASE /* LY_TYPE_IDENT */,
2171 LYS_SET_REQINST /* LY_TYPE_INST */,
2172 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002173 LYS_SET_TYPE /* LY_TYPE_UNION */,
2174 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002175 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002176 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002177 LYS_SET_RANGE /* LY_TYPE_INT64 */
2178};
2179
2180/**
2181 * @brief stringification of the YANG built-in data types
2182 */
2183const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2184 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2185 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002186};
2187
Radek Krejcia3045382018-11-22 14:30:31 +01002188/**
2189 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2190 * @param[in] ctx Compile context.
2191 * @param[in] enums_p Array of the parsed enum structures to compile.
2192 * @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 +01002193 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2194 * @param[out] enums Newly created array of the compiled enums information for the current type.
2195 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2196 */
Radek Krejci19a96102018-11-15 13:38:09 +01002197static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002198lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002199 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002200{
2201 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002202 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002203 int32_t value = 0;
2204 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002205 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002206
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002207 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002208 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2209 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2210 return LY_EVALID;
2211 }
2212
2213 LY_ARRAY_FOR(enums_p, u) {
2214 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2215 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002216 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2217 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002218 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002219 if (base_enums) {
2220 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2221 LY_ARRAY_FOR(base_enums, v) {
2222 if (!strcmp(e->name, base_enums[v].name)) {
2223 break;
2224 }
2225 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002226 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002227 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2228 "Invalid %s - derived type adds new item \"%s\".",
2229 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2230 return LY_EVALID;
2231 }
2232 match = v;
2233 }
2234
2235 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002236 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002237 if (enums_p[u].flags & LYS_SET_VALUE) {
2238 e->value = (int32_t)enums_p[u].value;
2239 if (!u || e->value >= value) {
2240 value = e->value + 1;
2241 }
2242 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002243 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002244 if (e->value == (*enums)[v].value) {
2245 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2246 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2247 e->value, e->name, (*enums)[v].name);
2248 return LY_EVALID;
2249 }
2250 }
2251 } else if (base_enums) {
2252 /* inherit the assigned value */
2253 e->value = base_enums[match].value;
2254 if (!u || e->value >= value) {
2255 value = e->value + 1;
2256 }
2257 } else {
2258 /* assign value automatically */
2259 if (u && value == INT32_MIN) {
2260 /* counter overflow */
2261 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2262 "Invalid enumeration - it is not possible to auto-assign enum value for "
2263 "\"%s\" since the highest value is already 2147483647.", e->name);
2264 return LY_EVALID;
2265 }
2266 e->value = value++;
2267 }
2268 } else { /* LY_TYPE_BITS */
2269 if (enums_p[u].flags & LYS_SET_VALUE) {
2270 e->value = (int32_t)enums_p[u].value;
2271 if (!u || (uint32_t)e->value >= position) {
2272 position = (uint32_t)e->value + 1;
2273 }
2274 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002275 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002276 if (e->value == (*enums)[v].value) {
2277 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2278 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2279 (uint32_t)e->value, e->name, (*enums)[v].name);
2280 return LY_EVALID;
2281 }
2282 }
2283 } else if (base_enums) {
2284 /* inherit the assigned value */
2285 e->value = base_enums[match].value;
2286 if (!u || (uint32_t)e->value >= position) {
2287 position = (uint32_t)e->value + 1;
2288 }
2289 } else {
2290 /* assign value automatically */
2291 if (u && position == 0) {
2292 /* counter overflow */
2293 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2294 "Invalid bits - it is not possible to auto-assign bit position for "
2295 "\"%s\" since the highest value is already 4294967295.", e->name);
2296 return LY_EVALID;
2297 }
2298 e->value = position++;
2299 }
2300 }
2301
2302 if (base_enums) {
2303 /* the assigned values must not change from the derived type */
2304 if (e->value != base_enums[match].value) {
2305 if (basetype == LY_TYPE_ENUM) {
2306 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2307 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2308 e->name, base_enums[match].value, e->value);
2309 } else {
2310 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2311 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2312 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2313 }
2314 return LY_EVALID;
2315 }
2316 }
2317
Radek Krejciec4da802019-05-02 13:02:41 +02002318 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002319 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 +01002320
2321 if (basetype == LY_TYPE_BITS) {
2322 /* keep bits ordered by position */
2323 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2324 if (v != u) {
2325 memcpy(&storage, e, sizeof *e);
2326 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2327 memcpy(&(*enums)[v], &storage, sizeof storage);
2328 }
2329 }
2330 }
2331
2332done:
2333 return ret;
2334}
2335
Radek Krejcia3045382018-11-22 14:30:31 +01002336/**
2337 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2338 *
2339 * path-arg = absolute-path / relative-path
2340 * absolute-path = 1*("/" (node-identifier *path-predicate))
2341 * relative-path = 1*(".." "/") descendant-path
2342 *
2343 * @param[in,out] path Path to parse.
2344 * @param[out] prefix Prefix of the token, NULL if there is not any.
2345 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2346 * @param[out] name Name of the token.
2347 * @param[out] nam_len Length of the name.
2348 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2349 * must not be changed between consecutive calls. -1 if the
2350 * path is absolute.
2351 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2352 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2353 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002354LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002355lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2356 int *parent_times, int *has_predicate)
2357{
2358 int par_times = 0;
2359
2360 assert(path && *path);
2361 assert(parent_times);
2362 assert(prefix);
2363 assert(prefix_len);
2364 assert(name);
2365 assert(name_len);
2366 assert(has_predicate);
2367
2368 *prefix = NULL;
2369 *prefix_len = 0;
2370 *name = NULL;
2371 *name_len = 0;
2372 *has_predicate = 0;
2373
2374 if (!*parent_times) {
2375 if (!strncmp(*path, "..", 2)) {
2376 *path += 2;
2377 ++par_times;
2378 while (!strncmp(*path, "/..", 3)) {
2379 *path += 3;
2380 ++par_times;
2381 }
2382 }
2383 if (par_times) {
2384 *parent_times = par_times;
2385 } else {
2386 *parent_times = -1;
2387 }
2388 }
2389
2390 if (**path != '/') {
2391 return LY_EINVAL;
2392 }
2393 /* skip '/' */
2394 ++(*path);
2395
2396 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002397 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002398
2399 if ((**path == '/' && (*path)[1]) || !**path) {
2400 /* path continues by another token or this is the last token */
2401 return LY_SUCCESS;
2402 } else if ((*path)[0] != '[') {
2403 /* unexpected character */
2404 return LY_EINVAL;
2405 } else {
2406 /* predicate starting with [ */
2407 *has_predicate = 1;
2408 return LY_SUCCESS;
2409 }
2410}
2411
2412/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002413 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2414 *
2415 * The set of features used for target must be a subset of features used for the leafref.
2416 * This is not a perfect, we should compare the truth tables but it could require too much resources
2417 * and RFC 7950 does not require it explicitely, so we simplify that.
2418 *
2419 * @param[in] refnode The leafref node.
2420 * @param[in] target Tha target node of the leafref.
2421 * @return LY_SUCCESS or LY_EVALID;
2422 */
2423static LY_ERR
2424lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2425{
2426 LY_ERR ret = LY_EVALID;
2427 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002428 LY_ARRAY_COUNT_TYPE u, v, count;
Radek Krejci58d171e2018-11-23 13:50:55 +01002429 struct ly_set features = {0};
2430
2431 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002432 if (iter->iffeatures) {
2433 LY_ARRAY_FOR(iter->iffeatures, u) {
2434 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2435 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002436 }
2437 }
2438 }
2439 }
2440
2441 /* we should have, in features set, a superset of features applicable to the target node.
2442 * So when adding features applicable to the target into the features set, we should not be
2443 * able to actually add any new feature, otherwise it is not a subset of features applicable
2444 * to the leafref itself. */
2445 count = features.count;
2446 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002447 if (iter->iffeatures) {
2448 LY_ARRAY_FOR(iter->iffeatures, u) {
2449 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2450 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002451 /* new feature was added (or LY_EMEM) */
2452 goto cleanup;
2453 }
2454 }
2455 }
2456 }
2457 }
2458 ret = LY_SUCCESS;
2459
2460cleanup:
2461 ly_set_erase(&features, NULL);
2462 return ret;
2463}
2464
Michal Vasko004d3152020-06-11 19:59:22 +02002465static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2466 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2467 struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002468
Radek Krejcia3045382018-11-22 14:30:31 +01002469/**
2470 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2471 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002472 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2473 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2474 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2475 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002476 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002477 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002478 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002479 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2480 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2481 * @param[out] type Newly created type structure with the filled information about the type.
2482 * @return LY_ERR value.
2483 */
Radek Krejci19a96102018-11-15 13:38:09 +01002484static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002485lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2486 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2487 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2488 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002489{
2490 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002491 struct lysc_type_bin *bin;
2492 struct lysc_type_num *num;
2493 struct lysc_type_str *str;
2494 struct lysc_type_bits *bits;
2495 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002496 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002497 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002498 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002499 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002500
2501 switch (basetype) {
2502 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002503 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002504
2505 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002506 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002507 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2508 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002509 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002510 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 +01002511 }
2512 }
2513
2514 if (tpdfname) {
2515 type_p->compiled = *type;
2516 *type = calloc(1, sizeof(struct lysc_type_bin));
2517 }
2518 break;
2519 case LY_TYPE_BITS:
2520 /* RFC 7950 9.7 - bits */
2521 bits = (struct lysc_type_bits*)(*type);
2522 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002523 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002524 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2525 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002526 }
2527
Radek Krejci555cb5b2018-11-16 14:54:33 +01002528 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002529 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002530 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002532 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002533 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002534 free(*type);
2535 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002536 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002537 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002538 }
2539
2540 if (tpdfname) {
2541 type_p->compiled = *type;
2542 *type = calloc(1, sizeof(struct lysc_type_bits));
2543 }
2544 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002545 case LY_TYPE_DEC64:
2546 dec = (struct lysc_type_dec*)(*type);
2547
2548 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002549 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002550 if (!type_p->fraction_digits) {
2551 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002552 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002553 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002555 free(*type);
2556 *type = NULL;
2557 }
2558 return LY_EVALID;
2559 }
2560 } else if (type_p->fraction_digits) {
2561 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002562 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002563 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2564 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002565 tpdfname);
2566 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002567 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2568 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002569 free(*type);
2570 *type = NULL;
2571 }
2572 return LY_EVALID;
2573 }
2574 dec->fraction_digits = type_p->fraction_digits;
2575
2576 /* RFC 7950 9.2.4 - range */
2577 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002578 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2579 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002580 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002581 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 +01002582 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002583 }
2584
2585 if (tpdfname) {
2586 type_p->compiled = *type;
2587 *type = calloc(1, sizeof(struct lysc_type_dec));
2588 }
2589 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002590 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002591 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002592
2593 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002594 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002595 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2596 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002597 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002598 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 +01002599 }
2600 } else if (base && ((struct lysc_type_str*)base)->length) {
2601 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2602 }
2603
2604 /* RFC 7950 9.4.5 - pattern */
2605 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002606 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002607 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002608 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2609 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2610 }
2611
2612 if (tpdfname) {
2613 type_p->compiled = *type;
2614 *type = calloc(1, sizeof(struct lysc_type_str));
2615 }
2616 break;
2617 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002618 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002619
2620 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002621 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002622 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002623 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002624 }
2625
Radek Krejci555cb5b2018-11-16 14:54:33 +01002626 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002627 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002628 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002630 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002631 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002632 free(*type);
2633 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002634 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002635 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002636 }
2637
2638 if (tpdfname) {
2639 type_p->compiled = *type;
2640 *type = calloc(1, sizeof(struct lysc_type_enum));
2641 }
2642 break;
2643 case LY_TYPE_INT8:
2644 case LY_TYPE_UINT8:
2645 case LY_TYPE_INT16:
2646 case LY_TYPE_UINT16:
2647 case LY_TYPE_INT32:
2648 case LY_TYPE_UINT32:
2649 case LY_TYPE_INT64:
2650 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002651 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002652
2653 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002654 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002655 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2656 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002657 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002658 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 +01002659 }
2660 }
2661
2662 if (tpdfname) {
2663 type_p->compiled = *type;
2664 *type = calloc(1, sizeof(struct lysc_type_num));
2665 }
2666 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002667 case LY_TYPE_IDENT:
2668 idref = (struct lysc_type_identityref*)(*type);
2669
2670 /* RFC 7950 9.10.2 - base */
2671 if (type_p->bases) {
2672 if (base) {
2673 /* only the directly derived identityrefs can contain base specification */
2674 if (tpdfname) {
2675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002676 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002677 tpdfname);
2678 } else {
2679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002680 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002681 free(*type);
2682 *type = NULL;
2683 }
2684 return LY_EVALID;
2685 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002686 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002687 }
2688
2689 if (!base && !type_p->flags) {
2690 /* type derived from identityref built-in type must contain at least one base */
2691 if (tpdfname) {
2692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2693 } else {
2694 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2695 free(*type);
2696 *type = NULL;
2697 }
2698 return LY_EVALID;
2699 }
2700
2701 if (tpdfname) {
2702 type_p->compiled = *type;
2703 *type = calloc(1, sizeof(struct lysc_type_identityref));
2704 }
2705 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002706 case LY_TYPE_LEAFREF:
Michal Vasko004d3152020-06-11 19:59:22 +02002707 lref = (struct lysc_type_leafref*)*type;
2708
Radek Krejcia3045382018-11-22 14:30:31 +01002709 /* RFC 7950 9.9.3 - require-instance */
2710 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002711 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002712 if (tpdfname) {
2713 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2714 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2715 } else {
2716 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2717 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2718 free(*type);
2719 *type = NULL;
2720 }
2721 return LY_EVALID;
2722 }
Michal Vasko004d3152020-06-11 19:59:22 +02002723 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002724 } else if (base) {
2725 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002726 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002727 } else {
2728 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002729 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002730 }
2731 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002732 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2733 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002734 } else if (base) {
Michal Vasko004d3152020-06-11 19:59:22 +02002735 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path);
2736 lref->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002737 } else if (tpdfname) {
2738 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2739 return LY_EVALID;
2740 } else {
2741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2742 free(*type);
2743 *type = NULL;
2744 return LY_EVALID;
2745 }
2746 if (tpdfname) {
2747 type_p->compiled = *type;
2748 *type = calloc(1, sizeof(struct lysc_type_leafref));
2749 }
2750 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002751 case LY_TYPE_INST:
2752 /* RFC 7950 9.9.3 - require-instance */
2753 if (type_p->flags & LYS_SET_REQINST) {
2754 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2755 } else {
2756 /* default is true */
2757 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2758 }
2759
2760 if (tpdfname) {
2761 type_p->compiled = *type;
2762 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2763 }
2764 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002765 case LY_TYPE_UNION:
2766 un = (struct lysc_type_union*)(*type);
2767
2768 /* RFC 7950 7.4 - type */
2769 if (type_p->types) {
2770 if (base) {
2771 /* only the directly derived union can contain types specification */
2772 if (tpdfname) {
2773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2774 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2775 tpdfname);
2776 } else {
2777 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2778 "Invalid type substatement for the type not directly derived from union built-in type.");
2779 free(*type);
2780 *type = NULL;
2781 }
2782 return LY_EVALID;
2783 }
2784 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002785 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2786 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002787 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
2788 &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002789 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2790 /* add space for additional types from the union subtype */
2791 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002792 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 +02002793 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002794
2795 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002796 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002797 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2798 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2799 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2800 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 +02002801 lref = (struct lysc_type_leafref *)un->types[u + additional];
2802
2803 lref->basetype = LY_TYPE_LEAFREF;
2804 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path);
2805 lref->refcount = 1;
2806 lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2807 lref->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002808 /* TODO extensions */
2809
2810 } else {
2811 un->types[u + additional] = un_aux->types[v];
2812 ++un_aux->types[v]->refcount;
2813 }
2814 ++additional;
2815 LY_ARRAY_INCREMENT(un->types);
2816 }
2817 /* compensate u increment in main loop */
2818 --additional;
2819
2820 /* free the replaced union subtype */
2821 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2822 } else {
2823 LY_ARRAY_INCREMENT(un->types);
2824 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002825 }
2826 }
2827
2828 if (!base && !type_p->flags) {
2829 /* type derived from union built-in type must contain at least one type */
2830 if (tpdfname) {
2831 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2832 } else {
2833 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2834 free(*type);
2835 *type = NULL;
2836 }
2837 return LY_EVALID;
2838 }
2839
2840 if (tpdfname) {
2841 type_p->compiled = *type;
2842 *type = calloc(1, sizeof(struct lysc_type_union));
2843 }
2844 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002845 case LY_TYPE_BOOL:
2846 case LY_TYPE_EMPTY:
2847 case LY_TYPE_UNKNOWN: /* just to complete switch */
2848 break;
2849 }
2850 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2851done:
2852 return ret;
2853}
2854
Radek Krejcia3045382018-11-22 14:30:31 +01002855/**
2856 * @brief Compile information about the leaf/leaf-list's type.
2857 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002858 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2859 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2860 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2861 * @param[in] context_name Name of the context node or referencing typedef for logging.
2862 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002863 * @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 +01002864 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002865 * @return LY_ERR value.
2866 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002867static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002868lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2869 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2870 struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002871{
2872 LY_ERR ret = LY_SUCCESS;
2873 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002874 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002875 struct type_context {
2876 const struct lysp_tpdf *tpdf;
2877 struct lysp_node *node;
2878 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002879 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002880 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002881 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002882 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002883 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002884 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002885
2886 (*type) = NULL;
2887
2888 tctx = calloc(1, sizeof *tctx);
2889 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002890 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002891 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2892 ret == LY_SUCCESS;
2893 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2894 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2895 if (basetype) {
2896 break;
2897 }
2898
2899 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002900 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2901 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002902 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2903
Radek Krejcicdfecd92018-11-26 11:27:32 +01002904 if (units && !*units) {
2905 /* inherit units */
2906 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2907 }
Radek Krejci01342af2019-01-03 15:18:08 +01002908 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002909 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002910 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02002911 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002912 }
Radek Krejci01342af2019-01-03 15:18:08 +01002913 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002914 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2915 break;
2916 }
2917
Radek Krejci19a96102018-11-15 13:38:09 +01002918 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002919 /* it is not necessary to continue, the rest of the chain was already compiled,
2920 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002921 basetype = tctx->tpdf->type.compiled->basetype;
2922 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002923 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002924 dummyloops = 1;
2925 goto preparenext;
2926 } else {
2927 tctx = NULL;
2928 break;
2929 }
Radek Krejci19a96102018-11-15 13:38:09 +01002930 }
2931
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002932 /* circular typedef reference detection */
2933 for (u = 0; u < tpdf_chain.count; u++) {
2934 /* local part */
2935 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2936 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2937 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2938 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2939 free(tctx);
2940 ret = LY_EVALID;
2941 goto cleanup;
2942 }
2943 }
2944 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2945 /* global part for unions corner case */
2946 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2947 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2949 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2950 free(tctx);
2951 ret = LY_EVALID;
2952 goto cleanup;
2953 }
2954 }
2955
Radek Krejci19a96102018-11-15 13:38:09 +01002956 /* store information for the following processing */
2957 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2958
Radek Krejcicdfecd92018-11-26 11:27:32 +01002959preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002960 /* prepare next loop */
2961 tctx_prev = tctx;
2962 tctx = calloc(1, sizeof *tctx);
2963 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2964 }
2965 free(tctx);
2966
2967 /* allocate type according to the basetype */
2968 switch (basetype) {
2969 case LY_TYPE_BINARY:
2970 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002971 break;
2972 case LY_TYPE_BITS:
2973 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002974 break;
2975 case LY_TYPE_BOOL:
2976 case LY_TYPE_EMPTY:
2977 *type = calloc(1, sizeof(struct lysc_type));
2978 break;
2979 case LY_TYPE_DEC64:
2980 *type = calloc(1, sizeof(struct lysc_type_dec));
2981 break;
2982 case LY_TYPE_ENUM:
2983 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002984 break;
2985 case LY_TYPE_IDENT:
2986 *type = calloc(1, sizeof(struct lysc_type_identityref));
2987 break;
2988 case LY_TYPE_INST:
2989 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2990 break;
2991 case LY_TYPE_LEAFREF:
2992 *type = calloc(1, sizeof(struct lysc_type_leafref));
2993 break;
2994 case LY_TYPE_STRING:
2995 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002996 break;
2997 case LY_TYPE_UNION:
2998 *type = calloc(1, sizeof(struct lysc_type_union));
2999 break;
3000 case LY_TYPE_INT8:
3001 case LY_TYPE_UINT8:
3002 case LY_TYPE_INT16:
3003 case LY_TYPE_UINT16:
3004 case LY_TYPE_INT32:
3005 case LY_TYPE_UINT32:
3006 case LY_TYPE_INT64:
3007 case LY_TYPE_UINT64:
3008 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003009 break;
3010 case LY_TYPE_UNKNOWN:
3011 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3012 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3013 ret = LY_EVALID;
3014 goto cleanup;
3015 }
3016 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003017 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003018 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3019 ly_data_type2str[basetype]);
3020 free(*type);
3021 (*type) = NULL;
3022 ret = LY_EVALID;
3023 goto cleanup;
3024 }
3025
3026 /* get restrictions from the referred typedefs */
3027 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3028 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003029
3030 /* remember the typedef context for circular check */
3031 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3032
Radek Krejci43699232018-11-23 14:59:46 +01003033 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003034 base = tctx->tpdf->type.compiled;
3035 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003036 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003037 /* no change, just use the type information from the base */
3038 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3039 ++base->refcount;
3040 continue;
3041 }
3042
3043 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003044 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3045 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3046 tctx->tpdf->name, ly_data_type2str[basetype]);
3047 ret = LY_EVALID;
3048 goto cleanup;
3049 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3050 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3051 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3052 tctx->tpdf->name, tctx->tpdf->dflt);
3053 ret = LY_EVALID;
3054 goto cleanup;
3055 }
3056
Radek Krejci19a96102018-11-15 13:38:09 +01003057 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003058 /* TODO user type plugins */
3059 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003060 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003061 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 +01003062 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003063 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003064 LY_CHECK_GOTO(ret, cleanup);
3065 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003066 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003067 /* remove the processed typedef contexts from the stack for circular check */
3068 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003069
Radek Krejcic5c27e52018-11-15 14:38:11 +01003070 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003071 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003072 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003073 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003074 /* TODO user type plugins */
3075 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003076 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003077 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 +01003078 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003079 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003080 /* no specific restriction in leaf's type definition, copy from the base */
3081 free(*type);
3082 (*type) = base;
3083 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003084 }
Radek Krejcia1911222019-07-22 17:24:50 +02003085 if (dflt && !(*type)->dflt) {
3086 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003087 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003088 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3089 (*type)->dflt->realtype = (*type);
3090 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3091 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003092 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003093 if (err) {
3094 ly_err_print(err);
3095 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3096 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3097 ly_err_free(err);
3098 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003099 if (ret == LY_EINCOMPLETE) {
3100 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003101 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003102
3103 /* but in general result is so far ok */
3104 ret = LY_SUCCESS;
3105 }
Radek Krejcia1911222019-07-22 17:24:50 +02003106 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003107 }
Radek Krejci19a96102018-11-15 13:38:09 +01003108
Radek Krejci0935f412019-08-20 16:15:18 +02003109 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003110
3111cleanup:
3112 ly_set_erase(&tpdf_chain, free);
3113 return ret;
3114}
3115
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003116/**
3117 * @brief Compile status information of the given node.
3118 *
3119 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3120 * has the status correctly set during the compilation.
3121 *
3122 * @param[in] ctx Compile context
3123 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3124 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3125 * the compatibility with the parent's status value.
3126 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3127 * @return LY_ERR value.
3128 */
3129static LY_ERR
3130lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3131{
3132 /* status - it is not inherited by specification, but it does not make sense to have
3133 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3134 if (!((*node_flags) & LYS_STATUS_MASK)) {
3135 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3136 if ((parent_flags & 0x3) != 0x3) {
3137 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3138 * combination of bits (0x3) which marks the uses_status value */
3139 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3140 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3141 }
3142 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3143 } else {
3144 (*node_flags) |= LYS_STATUS_CURR;
3145 }
3146 } else if (parent_flags & LYS_STATUS_MASK) {
3147 /* check status compatibility with the parent */
3148 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3149 if ((*node_flags) & LYS_STATUS_CURR) {
3150 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3151 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3152 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3153 } else { /* LYS_STATUS_DEPRC */
3154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3155 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3156 }
3157 return LY_EVALID;
3158 }
3159 }
3160 return LY_SUCCESS;
3161}
3162
Radek Krejci8cce8532019-03-05 11:27:45 +01003163/**
3164 * @brief Check uniqness of the node/action/notification name.
3165 *
3166 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3167 * structures, but they share the namespace so we need to check their name collisions.
3168 *
3169 * @param[in] ctx Compile context.
3170 * @param[in] children List (linked list) of data nodes to go through.
3171 * @param[in] actions List (sized array) of actions or RPCs to go through.
3172 * @param[in] notifs List (sized array) of Notifications to go through.
3173 * @param[in] name Name of the item to find in the given lists.
3174 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3175 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3176 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3177 */
3178static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02003179lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions,
3180 const struct lysc_notif *notifs, const char *name, void *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003181{
3182 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003183 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003184
3185 LY_LIST_FOR(children, iter) {
3186 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3187 goto error;
3188 }
3189 }
3190 LY_ARRAY_FOR(actions, u) {
3191 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3192 goto error;
3193 }
3194 }
3195 LY_ARRAY_FOR(notifs, u) {
3196 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3197 goto error;
3198 }
3199 }
3200 return LY_SUCCESS;
3201error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003202 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003203 return LY_EEXIST;
3204}
3205
Radek Krejciec4da802019-05-02 13:02:41 +02003206static 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 +01003207
Radek Krejcia3045382018-11-22 14:30:31 +01003208/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003209 * @brief Compile parsed RPC/action schema node information.
3210 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003211 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003212 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003213 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3214 * @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).
3215 * Zero means no uses, non-zero value with no status bit set mean the default status.
3216 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3217 */
3218static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003219lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003220 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3221{
3222 LY_ERR ret = LY_SUCCESS;
3223 struct lysp_node *child_p;
3224 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003225 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003226
Radek Krejci327de162019-06-14 12:52:07 +02003227 lysc_update_path(ctx, parent, action_p->name);
3228
Radek Krejci8cce8532019-03-05 11:27:45 +01003229 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3230 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3231 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3232 action_p->name, action)) {
3233 return LY_EVALID;
3234 }
3235
Radek Krejciec4da802019-05-02 13:02:41 +02003236 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003237 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003238 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003239 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003240 return LY_EVALID;
3241 }
3242
Michal Vasko1bf09392020-03-27 12:38:10 +01003243 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003244 action->module = ctx->mod;
3245 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003246 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003247 action->sp = action_p;
3248 }
3249 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3250
3251 /* status - it is not inherited by specification, but it does not make sense to have
3252 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003253 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003254
3255 DUP_STRING(ctx->ctx, action_p->name, action->name);
3256 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3257 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003258 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003259 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003260
3261 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003262 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003263 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003264 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 +02003265 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003266 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003267 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003268 }
Radek Krejci327de162019-06-14 12:52:07 +02003269 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003270 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003271
3272 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003273 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003274 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003275 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 +02003276 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003277 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003278 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003279 }
Radek Krejci327de162019-06-14 12:52:07 +02003280 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003281 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003282
3283 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3284 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003285 ly_set_add(&ctx->xpath, action, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003286 }
3287
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003288cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003289 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003290 return ret;
3291}
3292
3293/**
Radek Krejci43981a32019-04-12 09:44:11 +02003294 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003295 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003296 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003297 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3298 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3299 * @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 +02003300 * Zero means no uses, non-zero value with no status bit set mean the default status.
3301 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3302 */
3303static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003304lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003305 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3306{
3307 LY_ERR ret = LY_SUCCESS;
3308 struct lysp_node *child_p;
3309 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003310 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003311
Radek Krejci327de162019-06-14 12:52:07 +02003312 lysc_update_path(ctx, parent, notif_p->name);
3313
Radek Krejcifc11bd72019-04-11 16:00:05 +02003314 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3315 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3316 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3317 notif_p->name, notif)) {
3318 return LY_EVALID;
3319 }
3320
Radek Krejciec4da802019-05-02 13:02:41 +02003321 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003322 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3323 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003324 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003325 return LY_EVALID;
3326 }
3327
3328 notif->nodetype = LYS_NOTIF;
3329 notif->module = ctx->mod;
3330 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003331 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003332 notif->sp = notif_p;
3333 }
3334 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3335
3336 /* status - it is not inherited by specification, but it does not make sense to have
3337 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3338 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3339
3340 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3341 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3342 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003343 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003344 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003345 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3346 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003347 ly_set_add(&ctx->xpath, notif, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003348 }
Radek Krejci0935f412019-08-20 16:15:18 +02003349 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003350
Radek Krejciec4da802019-05-02 13:02:41 +02003351 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003352 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003353 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003354 }
3355
Radek Krejci327de162019-06-14 12:52:07 +02003356 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003357cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003358 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003359 return ret;
3360}
3361
3362/**
Radek Krejcia3045382018-11-22 14:30:31 +01003363 * @brief Compile parsed container node information.
3364 * @param[in] ctx Compile context
3365 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003366 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3367 * is enriched with the container-specific information.
3368 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3369 */
Radek Krejci19a96102018-11-15 13:38:09 +01003370static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003371lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003372{
3373 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3374 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3375 struct lysp_node *child_p;
3376 unsigned int u;
3377 LY_ERR ret = LY_SUCCESS;
3378
Radek Krejcife909632019-02-12 15:34:42 +01003379 if (cont_p->presence) {
3380 cont->flags |= LYS_PRESENCE;
3381 }
3382
Radek Krejci19a96102018-11-15 13:38:09 +01003383 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003384 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003385 }
3386
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003387 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003388 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3389 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003390 ly_set_add(&ctx->xpath, cont, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003391 }
Radek Krejciec4da802019-05-02 13:02:41 +02003392 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3393 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003394
3395done:
3396 return ret;
3397}
3398
Radek Krejci33f72892019-02-21 10:36:58 +01003399/*
3400 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3401 * @param[in] ctx Compile context.
3402 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3403 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003404 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3405 * @return LY_ERR value.
3406 */
3407static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003408lys_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 +01003409{
Radek Krejci33f72892019-02-21 10:36:58 +01003410 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3411
Radek Krejciec4da802019-05-02 13:02:41 +02003412 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 +01003413 leaf->units ? NULL : &leaf->units));
3414 if (leaf->nodetype == LYS_LEAFLIST) {
3415 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003416 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3417 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003418 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003419 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3420 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3421 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3422 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003423 LY_ARRAY_INCREMENT(llist->dflts);
3424 }
3425 } else {
3426 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003427 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003428 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3429 leaf->dflt->realtype = leaf->type->dflt->realtype;
3430 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3431 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003432 }
3433 }
3434 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3435 /* 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 +02003436 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003437 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003438 LY_ARRAY_COUNT_TYPE u;
Radek Krejci33f72892019-02-21 10:36:58 +01003439 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3440 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3441 /* 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 +02003442 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003443 }
3444 }
3445 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003446 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3447 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3448 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3449 return LY_EVALID;
3450 }
3451 }
3452
Radek Krejci33f72892019-02-21 10:36:58 +01003453 return LY_SUCCESS;
3454}
3455
Radek Krejcia3045382018-11-22 14:30:31 +01003456/**
3457 * @brief Compile parsed leaf node information.
3458 * @param[in] ctx Compile context
3459 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003460 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3461 * is enriched with the leaf-specific information.
3462 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3463 */
Radek Krejci19a96102018-11-15 13:38:09 +01003464static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003465lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003466{
3467 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3468 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3469 unsigned int u;
3470 LY_ERR ret = LY_SUCCESS;
3471
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003472 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003473 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3474 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003475 ly_set_add(&ctx->xpath, leaf, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003476 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003477 if (leaf_p->units) {
3478 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3479 leaf->flags |= LYS_SET_UNITS;
3480 }
Radek Krejcia1911222019-07-22 17:24:50 +02003481
3482 /* the dflt member is just filled to avoid getting the default value from the type */
3483 leaf->dflt = (void*)leaf_p->dflt;
3484 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003485 if (ret) {
3486 leaf->dflt = NULL;
3487 return ret;
3488 }
Radek Krejcia1911222019-07-22 17:24:50 +02003489
Radek Krejciccd20f12019-02-15 14:12:27 +01003490 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003491 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003492 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003493 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3494 leaf->dflt->realtype = leaf->type;
3495 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3496 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003497 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003498 leaf->dflt->realtype->refcount++;
3499 if (err) {
3500 ly_err_print(err);
3501 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3502 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3503 ly_err_free(err);
3504 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003505 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003506 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003507 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003508
3509 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003510 ret = LY_SUCCESS;
3511 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003512 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003513 leaf->flags |= LYS_SET_DFLT;
3514 }
Radek Krejci43699232018-11-23 14:59:46 +01003515
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003516
Radek Krejci19a96102018-11-15 13:38:09 +01003517done:
3518 return ret;
3519}
3520
Radek Krejcia3045382018-11-22 14:30:31 +01003521/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003522 * @brief Compile parsed leaf-list node information.
3523 * @param[in] ctx Compile context
3524 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003525 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3526 * is enriched with the leaf-list-specific information.
3527 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3528 */
3529static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003530lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003531{
3532 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3533 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003534 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003535 LY_ERR ret = LY_SUCCESS;
3536
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003537 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003538 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3539 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003540 ly_set_add(&ctx->xpath, llist, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003541 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003542 if (llist_p->units) {
3543 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3544 llist->flags |= LYS_SET_UNITS;
3545 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003546
Radek Krejcia1911222019-07-22 17:24:50 +02003547 /* the dflts member is just filled to avoid getting the default value from the type */
3548 llist->dflts = (void*)llist_p->dflts;
3549 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Michal Vasko6a044b22020-01-15 12:25:39 +01003550 if (ret != LY_SUCCESS) {
3551 /* make sure the defaults are freed correctly */
3552 if (llist_p->dflts) {
3553 llist->dflts = NULL;
3554 }
3555 return ret;
3556 }
3557
Radek Krejci0e5d8382018-11-28 16:37:53 +01003558 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003559 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003560 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
3561 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003562 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003563 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003564 LY_ARRAY_INCREMENT(llist->dflts_mods);
3565 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003566 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003567 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3568 llist->dflts[u]->realtype = llist->type;
3569 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3570 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003571 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003572 llist->dflts[u]->realtype->refcount++;
3573 if (err) {
3574 ly_err_print(err);
3575 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3576 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3577 ly_err_free(err);
3578 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003579 if (ret == LY_EINCOMPLETE) {
3580 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003581 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003582
3583 /* but in general result is so far ok */
3584 ret = LY_SUCCESS;
3585 }
Radek Krejcia1911222019-07-22 17:24:50 +02003586 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003587 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003588 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003589 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003590 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02003591 /* configuration data values must be unique - so check the default values */
3592 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003593 for (v = u + 1; v < LY_ARRAY_COUNT(llist->dflts); ++v) {
Radek Krejcia1911222019-07-22 17:24:50 +02003594 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3595 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003596 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 +02003597 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3598 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3599 if (dynamic) {
3600 free((char*)val);
3601 }
3602 return LY_EVALID;
3603 }
3604 }
3605 }
3606 }
3607
3608 /* 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 +01003609
3610 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003611 if (llist->min) {
3612 llist->flags |= LYS_MAND_TRUE;
3613 }
Radek Krejcib7408632018-11-28 17:12:11 +01003614 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003615
Radek Krejci0e5d8382018-11-28 16:37:53 +01003616done:
3617 return ret;
3618}
3619
3620/**
Radek Krejci7af64242019-02-18 13:07:53 +01003621 * @brief Compile information about list's uniques.
3622 * @param[in] ctx Compile context.
3623 * @param[in] context_module Module where the prefixes are going to be resolved.
3624 * @param[in] uniques Sized array list of unique statements.
3625 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3626 * @return LY_ERR value.
3627 */
3628static LY_ERR
3629lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3630{
3631 LY_ERR ret = LY_SUCCESS;
3632 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003633 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003634 const char *keystr, *delim;
3635 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003636 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7af64242019-02-18 13:07:53 +01003637 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003638 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003639
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003640 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003641 config = -1;
3642 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3643 keystr = uniques[v];
3644 while (keystr) {
3645 delim = strpbrk(keystr, " \t\n");
3646 if (delim) {
3647 len = delim - keystr;
3648 while (isspace(*delim)) {
3649 ++delim;
3650 }
3651 } else {
3652 len = strlen(keystr);
3653 }
3654
3655 /* unique node must be present */
3656 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003657 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3658 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003659 if (ret != LY_SUCCESS) {
3660 if (ret == LY_EDENIED) {
3661 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003662 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003663 len, keystr, lys_nodetype2str((*key)->nodetype));
3664 }
3665 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003666 } else if (flags) {
3667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3668 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003669 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003670 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003671 }
3672
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003673
Radek Krejci7af64242019-02-18 13:07:53 +01003674 /* all referenced leafs must be of the same config type */
3675 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3676 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003677 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003678 return LY_EVALID;
3679 } else if ((*key)->flags & LYS_CONFIG_W) {
3680 config = 1;
3681 } else { /* LYS_CONFIG_R */
3682 config = 0;
3683 }
3684
Michal Vasko14654712020-02-06 08:35:21 +01003685 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3686 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3687 if (parent->nodetype == LYS_LIST) {
3688 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3689 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3690 return LY_EVALID;
3691 }
3692 }
3693
Radek Krejci7af64242019-02-18 13:07:53 +01003694 /* check status */
3695 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3696 (*key)->flags, (*key)->module, (*key)->name));
3697
3698 /* mark leaf as unique */
3699 (*key)->flags |= LYS_UNIQUE;
3700
3701 /* next unique value in line */
3702 keystr = delim;
3703 }
3704 /* next unique definition */
3705 }
3706
3707 return LY_SUCCESS;
3708}
3709
3710/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003711 * @brief Compile parsed list node information.
3712 * @param[in] ctx Compile context
3713 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003714 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3715 * is enriched with the list-specific information.
3716 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3717 */
3718static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003719lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003720{
3721 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3722 struct lysc_node_list *list = (struct lysc_node_list*)node;
3723 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003724 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003725 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003726 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003727 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003728 LY_ERR ret = LY_SUCCESS;
3729
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003730 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003731 if (list->min) {
3732 list->flags |= LYS_MAND_TRUE;
3733 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003734 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3735
3736 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003737 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003738 }
3739
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003740 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003741 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3742 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003743 ly_set_add(&ctx->xpath, list, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003744 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003745
3746 /* keys */
3747 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3749 return LY_EVALID;
3750 }
3751
3752 /* find all the keys (must be direct children) */
3753 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003754 if (!keystr) {
3755 /* keyless list */
3756 list->flags |= LYS_KEYLESS;
3757 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003758 while (keystr) {
3759 delim = strpbrk(keystr, " \t\n");
3760 if (delim) {
3761 len = delim - keystr;
3762 while (isspace(*delim)) {
3763 ++delim;
3764 }
3765 } else {
3766 len = strlen(keystr);
3767 }
3768
3769 /* key node must be present */
Michal Vaskoe444f752020-02-10 12:20:06 +01003770 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 +02003771 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003772 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3773 "The list's key \"%.*s\" not found.", len, keystr);
3774 return LY_EVALID;
3775 }
3776 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003777 if (key->flags & LYS_KEY) {
3778 /* the node was already marked as a key */
3779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3780 "Duplicated key identifier \"%.*s\".", len, keystr);
3781 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003782 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003783
3784 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003785 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003786 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003787 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3788 return LY_EVALID;
3789 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003790 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003791 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003792 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003793 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003794 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003795 return LY_EVALID;
3796 }
3797 } else {
3798 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003799 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003800 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003801 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003802 return LY_EVALID;
3803 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003804 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003805 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003806 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003807 return LY_EVALID;
3808 }
3809 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003810
3811 /* check status */
3812 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003813 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003814
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003815 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003816 if (key->dflt) {
3817 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3818 lysc_type_free(ctx->ctx, key->dflt->realtype);
3819 free(key->dflt);
3820 key->dflt = NULL;
3821 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003822 }
3823 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003824 key->flags |= LYS_KEY;
3825
3826 /* move it to the correct position */
3827 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3828 /* fix links in closest previous siblings of the key */
3829 if (key->next) {
3830 key->next->prev = key->prev;
3831 } else {
3832 /* last child */
3833 list->child->prev = key->prev;
3834 }
3835 if (key->prev->next) {
3836 key->prev->next = key->next;
3837 }
3838 /* fix links in the key */
3839 if (prev_key) {
3840 key->prev = (struct lysc_node*)prev_key;
3841 key->next = prev_key->next;
3842 } else {
3843 key->prev = list->child->prev;
3844 key->next = list->child;
3845 }
3846 /* fix links in closes future siblings of the key */
3847 if (prev_key) {
3848 if (prev_key->next) {
3849 prev_key->next->prev = (struct lysc_node*)key;
3850 } else {
3851 list->child->prev = (struct lysc_node*)key;
3852 }
3853 prev_key->next = (struct lysc_node*)key;
3854 } else {
3855 list->child->prev = (struct lysc_node*)key;
3856 }
3857 /* fix links in parent */
3858 if (!key->prev->next) {
3859 list->child = (struct lysc_node*)key;
3860 }
3861 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003862
3863 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003864 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003865 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003866 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003867 }
3868
3869 /* uniques */
3870 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003871 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003872 }
3873
Radek Krejciec4da802019-05-02 13:02:41 +02003874 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3875 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003876
3877done:
3878 return ret;
3879}
3880
Radek Krejcib56c7502019-02-13 14:19:54 +01003881/**
3882 * @brief Do some checks and set the default choice's case.
3883 *
3884 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3885 *
3886 * @param[in] ctx Compile context.
3887 * @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,
3888 * not the reference to the imported module.
3889 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3890 * @return LY_ERR value.
3891 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003892static LY_ERR
3893lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3894{
3895 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3896 const char *prefix = NULL, *name;
3897 size_t prefix_len = 0;
3898
3899 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3900 name = strchr(dflt, ':');
3901 if (name) {
3902 prefix = dflt;
3903 prefix_len = name - prefix;
3904 ++name;
3905 } else {
3906 name = dflt;
3907 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003908 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003909 /* prefixed default case make sense only for the prefix of the schema itself */
3910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3911 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3912 prefix_len, prefix);
3913 return LY_EVALID;
3914 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003915 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 +01003916 if (!ch->dflt) {
3917 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3918 "Default case \"%s\" not found.", dflt);
3919 return LY_EVALID;
3920 }
3921 /* no mandatory nodes directly under the default case */
3922 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003923 if (iter->parent != (struct lysc_node*)ch->dflt) {
3924 break;
3925 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003926 if (iter->flags & LYS_MAND_TRUE) {
3927 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3928 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3929 return LY_EVALID;
3930 }
3931 }
Radek Krejci01342af2019-01-03 15:18:08 +01003932 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003933 return LY_SUCCESS;
3934}
3935
Radek Krejciccd20f12019-02-15 14:12:27 +01003936static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003937lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003938{
3939 struct lys_module *mod;
3940 const char *prefix = NULL, *name;
3941 size_t prefix_len = 0;
3942 struct lysc_node_case *cs;
3943 struct lysc_node *node;
3944
3945 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3946 name = strchr(dflt, ':');
3947 if (name) {
3948 prefix = dflt;
3949 prefix_len = name - prefix;
3950 ++name;
3951 } else {
3952 name = dflt;
3953 }
3954 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3955 if (prefix) {
3956 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3957 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003958 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3959 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003960 return LY_EVALID;
3961 }
3962 } else {
3963 mod = ctx->mod;
3964 }
3965 /* get the default case */
Michal Vaskoe444f752020-02-10 12:20:06 +01003966 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 +01003967 if (!cs) {
3968 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003969 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003970 return LY_EVALID;
3971 }
3972
3973 /* check that there is no mandatory node */
3974 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003975 if (node->parent != (struct lysc_node*)cs) {
3976 break;
3977 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003978 if (node->flags & LYS_MAND_TRUE) {
3979 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003980 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3981 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003982 return LY_EVALID;
3983 }
3984 }
3985
3986 /* set the default case in choice */
3987 ch->dflt = cs;
3988 cs->flags |= LYS_SET_DFLT;
3989
3990 return LY_SUCCESS;
3991}
3992
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003993/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003994 * @brief Compile parsed choice node information.
3995 * @param[in] ctx Compile context
3996 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003997 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003998 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003999 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4000 */
4001static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004002lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004003{
4004 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4005 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4006 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004007 LY_ERR ret = LY_SUCCESS;
4008
Radek Krejci056d0a82018-12-06 16:57:25 +01004009 LY_LIST_FOR(ch_p->child, child_p) {
4010 if (child_p->nodetype == LYS_CASE) {
4011 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004012 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004013 }
4014 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004015 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004016 }
4017 }
4018
4019 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004020 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004021 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004022 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004023
Radek Krejci9800fb82018-12-13 14:26:23 +01004024 return ret;
4025}
4026
4027/**
4028 * @brief Compile parsed anydata or anyxml node information.
4029 * @param[in] ctx Compile context
4030 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004031 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4032 * is enriched with the any-specific information.
4033 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4034 */
4035static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004036lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004037{
4038 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4039 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4040 unsigned int u;
4041 LY_ERR ret = LY_SUCCESS;
4042
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004043 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004044 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4045 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004046 ly_set_add(&ctx->xpath, any, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004047 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004048
4049 if (any->flags & LYS_CONFIG_W) {
4050 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004051 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004052 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004053done:
4054 return ret;
4055}
4056
Radek Krejcib56c7502019-02-13 14:19:54 +01004057/**
Michal Vasko795b3752020-07-13 15:24:27 +02004058 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
4059 * keep specific order of augments targetting the same node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004060 *
4061 * @param[in] ctx Compile context
4062 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4063 * the choice itself is expected instead of a specific case node.
4064 * @param[in] node Schema node to connect into the list.
4065 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004066 * 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 +01004067 */
4068static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004069lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004070{
Michal Vasko795b3752020-07-13 15:24:27 +02004071 struct lysc_node **children, *start, *end;
4072 const struct lys_module *mod;
Radek Krejci056d0a82018-12-06 16:57:25 +01004073
4074 if (node->nodetype == LYS_CASE) {
4075 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4076 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004077 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004078 }
4079 if (children) {
4080 if (!(*children)) {
4081 /* first child */
4082 *children = node;
4083 } else if (*children != node) {
4084 /* by the condition in previous branch we cover the choice/case children
4085 * - the children list is shared by the choice and the the first case, in addition
4086 * the first child of each case must be referenced from the case node. So the node is
4087 * actually always already inserted in case it is the first children - so here such
4088 * a situation actually corresponds to the first branch */
Michal Vasko795b3752020-07-13 15:24:27 +02004089 if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module)
4090 && (strcmp((*children)->prev->module->name, node->module->name) > 0)) {
4091 /* some augments are already connected and we are connecting new ones,
4092 * keep module name order and insert the node into the children list */
4093 end = (*children);
4094 do {
4095 end = end->prev;
4096 mod = end->module;
4097 while (end->prev->module == mod) {
4098 end = end->prev;
4099 }
4100 } while ((end->prev->module != (*children)->module) && (end->prev->module != node->module)
4101 && (strcmp(mod->name, node->module->name) > 0));
4102
4103 /* we have the last existing node after our node, easily get the first before and connect it */
4104 start = end->prev;
4105 start->next = node;
4106 node->next = end;
4107 end->prev = node;
4108 node->prev = start;
4109 } else {
4110 /* insert at the end of the parent's children list */
4111 (*children)->prev->next = node;
4112 node->prev = (*children)->prev;
4113 (*children)->prev = node;
4114 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004115
4116 /* check the name uniqueness */
4117 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4118 lysc_node_notifs(parent), node->name, node)) {
4119 return LY_EEXIST;
4120 }
4121 }
4122 }
4123 return LY_SUCCESS;
4124}
4125
Radek Krejci95710c92019-02-11 15:49:55 +01004126/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004127 * @brief Prepare the case structure in choice node for the new data node.
4128 *
4129 * 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
4130 * created in the choice when the first child was processed.
4131 *
4132 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004133 * @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,
4134 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004135 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4136 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4137 * @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,
4138 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004139 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004140static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004141lys_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 +01004142{
4143 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004144 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004145 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004146 unsigned int u;
4147 LY_ERR ret;
4148
Radek Krejci95710c92019-02-11 15:49:55 +01004149#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004150 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004151 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004152 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4153 return NULL; \
4154 } \
4155 }
4156
Radek Krejci95710c92019-02-11 15:49:55 +01004157 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4158 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004159
4160 /* we have to add an implicit case node into the parent choice */
4161 cs = calloc(1, sizeof(struct lysc_node_case));
4162 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004163 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004164 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004165 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004166 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4167 /* the case is already present since the child is not its first children */
4168 return (struct lysc_node_case*)ch->cases->prev;
4169 }
Radek Krejci95710c92019-02-11 15:49:55 +01004170 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004171
4172 /* explicit parent case is not present (this is its first child) */
4173 cs = calloc(1, sizeof(struct lysc_node_case));
4174 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004175 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004176 cs->flags = LYS_STATUS_MASK & node_p->flags;
4177 cs->sp = node_p;
4178
Radek Krejcib1b59152019-01-07 13:21:56 +01004179 /* 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 +02004180 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004181
4182 if (node_p->when) {
4183 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004184 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004185 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004186
4187 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4188 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004189 ly_set_add(&ctx->xpath, cs, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004190 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004191 }
Radek Krejciec4da802019-05-02 13:02:41 +02004192 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004193 } else {
4194 LOGINT(ctx->ctx);
4195 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004196 }
4197 cs->module = ctx->mod;
4198 cs->prev = (struct lysc_node*)cs;
4199 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004200 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004201 cs->child = child;
4202
4203 return cs;
4204error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004205 if (cs) {
4206 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4207 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004208 return NULL;
4209
4210#undef UNIQUE_CHECK
4211}
4212
Radek Krejcib56c7502019-02-13 14:19:54 +01004213/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004214 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004215 *
4216 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004217 * @param[in] node Target node where the config is supposed to be changed.
4218 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004219 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4220 * 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 +01004221 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4222 * @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 +01004223 * @return LY_ERR value.
4224 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004225static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004226lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004227 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004228{
4229 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004230 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004231
4232 if (config == (node->flags & LYS_CONFIG_MASK)) {
4233 /* nothing to do */
4234 return LY_SUCCESS;
4235 }
4236
4237 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004238 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004239 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4240 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004241 "Invalid %s of config - configuration node cannot be child of any state data node.",
4242 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004243 return LY_EVALID;
4244 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004245 node->flags |= LYS_SET_CONFIG;
4246 } else {
4247 if (node->flags & LYS_SET_CONFIG) {
4248 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4249 /* setting config flags, but have node with explicit config true */
4250 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004251 "Invalid %s of config - configuration node cannot be child of any state data node.",
4252 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004253 return LY_EVALID;
4254 }
4255 /* do not change config on nodes where the config is explicitely set, this does not apply to
4256 * nodes, which are being changed explicitly (targets of refine or deviation) */
4257 return LY_SUCCESS;
4258 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004259 }
4260 node->flags &= ~LYS_CONFIG_MASK;
4261 node->flags |= config;
4262
4263 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004264 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004265 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004266 }
4267
Radek Krejci76b3e962018-12-14 17:01:25 +01004268 return LY_SUCCESS;
4269}
4270
Radek Krejcib56c7502019-02-13 14:19:54 +01004271/**
4272 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4273 *
4274 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4275 * the flag to such parents from a mandatory children.
4276 *
4277 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4278 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4279 * (mandatory children was removed).
4280 */
Radek Krejcife909632019-02-12 15:34:42 +01004281void
4282lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4283{
4284 struct lysc_node *iter;
4285
4286 if (add) { /* set flag */
4287 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4288 parent = parent->parent) {
4289 parent->flags |= LYS_MAND_TRUE;
4290 }
4291 } else { /* unset flag */
4292 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004293 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004294 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004295 /* there is another mandatory node */
4296 return;
4297 }
4298 }
4299 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4300 parent->flags &= ~LYS_MAND_TRUE;
4301 }
4302 }
4303}
4304
Radek Krejci056d0a82018-12-06 16:57:25 +01004305/**
Radek Krejci3641f562019-02-13 15:38:40 +01004306 * @brief Internal sorting process for the lys_compile_augment_sort().
4307 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4308 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4309 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4310 */
4311static void
4312lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4313{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004314 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004315 size_t len;
4316
4317 len = strlen(aug_p->nodeid);
4318 LY_ARRAY_FOR(result, v) {
4319 if (strlen(result[v]->nodeid) <= len) {
4320 continue;
4321 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004322 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004323 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004324 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004325 break;
4326 }
4327 }
4328 result[v] = aug_p;
4329 LY_ARRAY_INCREMENT(result);
4330}
4331
4332/**
4333 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4334 *
4335 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4336 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4337 *
4338 * @param[in] ctx Compile context.
4339 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4340 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4341 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4342 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4343 * @return LY_ERR value.
4344 */
4345LY_ERR
4346lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4347{
4348 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004349 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004350
4351 assert(augments);
4352
4353 /* get count of the augments in module and all its submodules */
4354 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004355 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004356 }
4357 LY_ARRAY_FOR(inc_p, u) {
4358 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004359 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004360 }
4361 }
4362
4363 if (!count) {
4364 *augments = NULL;
4365 return LY_SUCCESS;
4366 }
4367 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4368
4369 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4370 * together, so there can be even /z/y betwwen them. */
4371 LY_ARRAY_FOR(aug_p, u) {
4372 lys_compile_augment_sort_(&aug_p[u], result);
4373 }
4374 LY_ARRAY_FOR(inc_p, u) {
4375 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4376 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4377 }
4378 }
4379
4380 *augments = result;
4381 return LY_SUCCESS;
4382}
4383
4384/**
4385 * @brief Compile the parsed augment connecting it into its target.
4386 *
4387 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4388 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4389 * are already implemented and compiled.
4390 *
4391 * @param[in] ctx Compile context.
4392 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004393 * @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
4394 * children in case of the augmenting uses data.
4395 * @return LY_SUCCESS on success.
4396 * @return LY_EVALID on failure.
4397 */
4398LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004399lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004400{
Michal Vaskoe6143202020-07-03 13:02:08 +02004401 LY_ERR ret = LY_SUCCESS, rc;
Radek Krejci3641f562019-02-13 15:38:40 +01004402 struct lysp_node *node_p, *case_node_p;
4403 struct lysc_node *target; /* target target of the augment */
4404 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004405 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004406 struct lys_module **aug_mod;
Radek Krejci3641f562019-02-13 15:38:40 +01004407 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004408 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004409 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejciec4da802019-05-02 13:02:41 +02004410 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004411
Radek Krejci327de162019-06-14 12:52:07 +02004412 lysc_update_path(ctx, NULL, "{augment}");
4413 lysc_update_path(ctx, NULL, aug_p->nodeid);
4414
Radek Krejci7af64242019-02-18 13:07:53 +01004415 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004416 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004417 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004418 if (ret != LY_SUCCESS) {
4419 if (ret == LY_EDENIED) {
4420 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4421 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4422 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4423 }
4424 return LY_EVALID;
4425 }
4426
4427 /* check for mandatory nodes
4428 * - new cases augmenting some choice can have mandatory nodes
4429 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4430 */
Radek Krejci733988a2019-02-15 15:12:44 +01004431 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004432 allow_mandatory = 1;
4433 }
4434
4435 when_shared = NULL;
4436 LY_LIST_FOR(aug_p->child, node_p) {
4437 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4438 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004439 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004440 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4441 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004442 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004443 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4444 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004445 return LY_EVALID;
4446 }
4447
4448 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004449 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004450 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004451 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004452 } else {
4453 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004454 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004455 }
4456 }
Radek Krejciec4da802019-05-02 13:02:41 +02004457 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004458
Radek Krejcife13da42019-02-15 14:51:01 +01004459 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4460 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004461 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004462 /* 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 +01004463 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 +01004464 } else if (target->nodetype == LYS_CHOICE) {
4465 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4466 node = ((struct lysc_node_choice*)target)->cases->prev;
4467 } else {
4468 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004469 node = (struct lysc_node*)lysc_node_children(target, flags);
4470 if (!node) {
4471 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4472 break;
4473 }
4474 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004475 }
4476
Radek Krejci733988a2019-02-15 15:12:44 +01004477 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004478 node->flags &= ~LYS_MAND_TRUE;
4479 lys_compile_mandatory_parents(target, 0);
4480 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004481 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004482 return LY_EVALID;
4483 }
4484
4485 /* pass augment's when to all the children */
4486 if (aug_p->when) {
4487 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4488 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004489 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004490 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004491
4492 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4493 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004494 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004495 }
4496
Radek Krejci3641f562019-02-13 15:38:40 +01004497 when_shared = *when;
4498 } else {
4499 ++when_shared->refcount;
4500 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004501
4502 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4503 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004504 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004505 }
Radek Krejci3641f562019-02-13 15:38:40 +01004506 }
4507 }
4508 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004509
Radek Krejciec4da802019-05-02 13:02:41 +02004510 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004511 switch (target->nodetype) {
4512 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004513 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004514 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004515 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004516 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004517 break;
4518 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004519 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004520 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004521 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004522 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004523 break;
4524 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004525 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004526 if (aug_p->actions) {
4527 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004528 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4529 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004530 return LY_EVALID;
4531 }
4532 if (aug_p->notifs) {
4533 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004534 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004535 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004536 return LY_EVALID;
4537 }
4538 }
Radek Krejci3641f562019-02-13 15:38:40 +01004539
Michal Vaskoe6143202020-07-03 13:02:08 +02004540 /* add this module into the target module augmented_by, if not there already */
4541 rc = LY_SUCCESS;
4542 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4543 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4544 rc = LY_EEXIST;
4545 break;
4546 }
4547 }
4548 if (!rc) {
4549 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4550 *aug_mod = ctx->mod;
4551 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004552
Radek Krejci327de162019-06-14 12:52:07 +02004553 lysc_update_path(ctx, NULL, NULL);
4554 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004555
Radek Krejci3641f562019-02-13 15:38:40 +01004556error:
Radek Krejciec4da802019-05-02 13:02:41 +02004557 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004558 return ret;
4559}
4560
4561/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004562 * @brief Apply refined or deviated mandatory flag to the target node.
4563 *
4564 * @param[in] ctx Compile context.
4565 * @param[in] node Target node where the mandatory property is supposed to be changed.
4566 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004567 * @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 +01004568 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4569 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4570 * 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 +01004571 * @return LY_ERR value.
4572 */
4573static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004574lys_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 +01004575{
4576 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4577 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004578 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4579 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004580 return LY_EVALID;
4581 }
4582
4583 if (mandatory_flag & LYS_MAND_TRUE) {
4584 /* check if node has default value */
4585 if (node->nodetype & LYS_LEAF) {
4586 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004587 if (refine_flag) {
4588 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004589 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004590 return LY_EVALID;
4591 }
Radek Krejcia1911222019-07-22 17:24:50 +02004592 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004593 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004594 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004595
4596 /* update the list of incomplete default values if needed */
4597 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4598
Radek Krejcia1911222019-07-22 17:24:50 +02004599 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4600 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4601 free(leaf->dflt);
4602 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004603 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004604 }
4605 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004606 if (refine_flag) {
4607 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004608 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004609 return LY_EVALID;
4610 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004611 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004612 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004613 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 +01004614 return LY_EVALID;
4615 }
4616
4617 node->flags &= ~LYS_MAND_FALSE;
4618 node->flags |= LYS_MAND_TRUE;
4619 lys_compile_mandatory_parents(node->parent, 1);
4620 } else {
4621 /* make mandatory false */
4622 node->flags &= ~LYS_MAND_TRUE;
4623 node->flags |= LYS_MAND_FALSE;
4624 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004625 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 +01004626 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004627 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004628 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004629 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4630 leaf->dflt->realtype = leaf->type->dflt->realtype;
4631 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4632 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004633 }
4634 }
4635 return LY_SUCCESS;
4636}
4637
4638/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004639 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4640 * If present, also apply uses's modificators.
4641 *
4642 * @param[in] ctx Compile context
4643 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004644 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4645 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4646 * the compile context.
4647 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4648 */
4649static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004650lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004651{
4652 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004653 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004654 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004655 struct lysc_node_container context_node_fake =
4656 {.nodetype = LYS_CONTAINER,
4657 .module = ctx->mod,
4658 .flags = parent ? parent->flags : 0,
4659 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004660 .prev = (struct lysc_node*)&context_node_fake,
4661 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004662 struct lysp_grp *grp = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004663 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004664 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004665 int found;
4666 const char *id, *name, *prefix;
4667 size_t prefix_len, name_len;
4668 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004669 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004670 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004671 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004672 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004673 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004674 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004675 struct lysp_augment **augments = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004676 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004677 struct lysc_notif **notifs = NULL;
4678 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004679
4680 /* search for the grouping definition */
4681 found = 0;
4682 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004683 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004684 if (prefix) {
4685 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4686 if (!mod) {
4687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004688 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004689 return LY_EVALID;
4690 }
4691 } else {
4692 mod = ctx->mod_def;
4693 }
4694 if (mod == ctx->mod_def) {
4695 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004696 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004697 LY_ARRAY_FOR(grp, u) {
4698 if (!strcmp(grp[u].name, name)) {
4699 grp = &grp[u];
4700 found = 1;
4701 break;
4702 }
4703 }
4704 }
4705 }
4706 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004707 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004708 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004709 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004710 for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004711 if (!strcmp(grp[u].name, name)) {
4712 grp = &grp[u];
4713 found = 1;
4714 }
4715 }
4716 }
4717 if (!found && mod->parsed->includes) {
4718 /* ... and all the submodules */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004719 for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004720 grp = mod->parsed->includes[u].submodule->groupings;
4721 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004722 for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004723 if (!strcmp(grp[v].name, name)) {
4724 grp = &grp[v];
4725 found = 1;
4726 }
4727 }
4728 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004729 }
4730 }
4731 }
4732 if (!found) {
4733 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4734 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4735 return LY_EVALID;
4736 }
4737
4738 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4739 grp_stack_count = ctx->groupings.count;
4740 ly_set_add(&ctx->groupings, (void*)grp, 0);
4741 if (grp_stack_count == ctx->groupings.count) {
4742 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4744 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4745 return LY_EVALID;
4746 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004747 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4748 /* remember that the grouping is instantiated to avoid its standalone validation */
4749 grp->flags |= LYS_USED_GRP;
4750 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004751
4752 /* switch context's mod_def */
4753 mod_old = ctx->mod_def;
4754 ctx->mod_def = mod;
4755
4756 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004757 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 +01004758
Radek Krejcifc11bd72019-04-11 16:00:05 +02004759 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004760 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004761 /* 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 +02004762 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 +01004763
4764 /* some preparation for applying refines */
4765 if (grp->data == node_p) {
4766 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004767 if (parent) {
4768 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4769 } else if (ctx->mod->compiled->data) {
4770 child = ctx->mod->compiled->data;
4771 } else {
4772 child = NULL;
4773 }
4774 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004775 }
4776 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004777 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004778 LY_LIST_FOR(context_node_fake.child, child) {
4779 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004780
Radek Krejcifc11bd72019-04-11 16:00:05 +02004781 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004782 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004783 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004784 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004785 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4786
4787 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4788 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004789 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004790 }
4791
Radek Krejci00b874b2019-02-12 10:54:50 +01004792 when_shared = *when;
4793 } else {
4794 ++when_shared->refcount;
4795 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004796
4797 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4798 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004799 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004800 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004801 }
4802 }
Radek Krejci01342af2019-01-03 15:18:08 +01004803 }
4804 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004805 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004806 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004807 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004808 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 +01004809 }
4810
Radek Krejcifc11bd72019-04-11 16:00:05 +02004811 /* compile actions */
4812 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4813 if (actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004814 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004815 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004816 if (*actions && (uses_p->augments || uses_p->refines)) {
4817 /* 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 +02004818 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
4819 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
4820 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004821 }
4822 }
4823
4824 /* compile notifications */
4825 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4826 if (notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004827 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004828 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004829 if (*notifs && (uses_p->augments || uses_p->refines)) {
4830 /* 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 +02004831 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
4832 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
4833 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004834 }
4835 }
4836
4837
Radek Krejci3641f562019-02-13 15:38:40 +01004838 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004839 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004840 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004841 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004842 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004843
Radek Krejcif0089082019-01-07 16:42:01 +01004844 /* reload previous context's mod_def */
4845 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004846 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004847
Radek Krejci76b3e962018-12-14 17:01:25 +01004848 /* apply refine */
4849 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004850 lysc_update_path(ctx, NULL, rfn->nodeid);
4851
Radek Krejci7af64242019-02-18 13:07:53 +01004852 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 +01004853 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004854 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004855 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004856
4857 /* default value */
4858 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004859 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004860 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004861 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.",
4862 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004863 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004864 }
4865 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4866 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004867 "Invalid refine of default - %s cannot hold default value(s).",
4868 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004869 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004870 }
4871 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004872 struct ly_err_item *err = NULL;
4873 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4874 if (leaf->dflt) {
4875 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004876 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004877 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4878 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4879 } else {
4880 /* prepare a new one */
4881 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4882 leaf->dflt->realtype = leaf->type;
4883 }
4884 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004885 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004886 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4887 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004888 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004889 leaf->dflt->realtype->refcount++;
4890 if (err) {
4891 ly_err_print(err);
4892 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4893 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4894 ly_err_free(err);
4895 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004896 if (rc == LY_EINCOMPLETE) {
4897 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004898 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004899
4900 /* but in general result is so far ok */
4901 rc = LY_SUCCESS;
4902 }
Radek Krejcia1911222019-07-22 17:24:50 +02004903 LY_CHECK_GOTO(rc, cleanup);
4904 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004905 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004906 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4907
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004908 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004909 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4910 "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 +02004911 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004912 }
Radek Krejcia1911222019-07-22 17:24:50 +02004913
4914 /* remove previous set of default values */
4915 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004916 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004917 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4918 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4919 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004920 }
Radek Krejcia1911222019-07-22 17:24:50 +02004921 LY_ARRAY_FREE(llist->dflts);
4922 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004923 LY_ARRAY_FREE(llist->dflts_mods);
4924 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004925
4926 /* create the new set of the default values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004927 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
4928 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004929 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004930 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004931 LY_ARRAY_INCREMENT(llist->dflts_mods);
4932 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004933 LY_ARRAY_INCREMENT(llist->dflts);
4934 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4935 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004936 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004937 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004938 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004939 llist->dflts[u]->realtype->refcount++;
4940 if (err) {
4941 ly_err_print(err);
4942 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4943 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4944 ly_err_free(err);
4945 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004946 if (rc == LY_EINCOMPLETE) {
4947 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004948 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004949
4950 /* but in general result is so far ok */
4951 rc = LY_SUCCESS;
4952 }
4953 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004954 }
Radek Krejcia1911222019-07-22 17:24:50 +02004955 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004956 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004957 if (((struct lysc_node_choice*)node)->dflt) {
4958 /* unset LYS_SET_DFLT from the current default case */
4959 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4960 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004961 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 +01004962 }
4963 }
4964
Radek Krejci12fb9142019-01-08 09:45:30 +01004965 /* description */
4966 if (rfn->dsc) {
4967 FREE_STRING(ctx->ctx, node->dsc);
4968 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4969 }
4970
4971 /* reference */
4972 if (rfn->ref) {
4973 FREE_STRING(ctx->ctx, node->ref);
4974 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4975 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004976
4977 /* config */
4978 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004979 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004980 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004981 } else {
4982 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004983 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004984 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004985 }
4986
4987 /* mandatory */
4988 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004989 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004990 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004991
4992 /* presence */
4993 if (rfn->presence) {
4994 if (node->nodetype != LYS_CONTAINER) {
4995 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004996 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4997 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004998 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004999 }
5000 node->flags |= LYS_PRESENCE;
5001 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005002
5003 /* must */
5004 if (rfn->musts) {
5005 switch (node->nodetype) {
5006 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005007 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 +01005008 break;
5009 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005010 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 +01005011 break;
5012 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005013 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 +01005014 break;
5015 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005016 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 +01005017 break;
5018 case LYS_ANYXML:
5019 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005020 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 +01005021 break;
5022 default:
5023 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005024 "Invalid refine of must statement - %s cannot hold any must statement.",
5025 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005026 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005027 }
Michal Vasko004d3152020-06-11 19:59:22 +02005028 ly_set_add(&ctx->xpath, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01005029 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005030
5031 /* min/max-elements */
5032 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5033 switch (node->nodetype) {
5034 case LYS_LEAFLIST:
5035 if (rfn->flags & LYS_SET_MAX) {
5036 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5037 }
5038 if (rfn->flags & LYS_SET_MIN) {
5039 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005040 if (rfn->min) {
5041 node->flags |= LYS_MAND_TRUE;
5042 lys_compile_mandatory_parents(node->parent, 1);
5043 } else {
5044 node->flags &= ~LYS_MAND_TRUE;
5045 lys_compile_mandatory_parents(node->parent, 0);
5046 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005047 }
5048 break;
5049 case LYS_LIST:
5050 if (rfn->flags & LYS_SET_MAX) {
5051 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5052 }
5053 if (rfn->flags & LYS_SET_MIN) {
5054 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005055 if (rfn->min) {
5056 node->flags |= LYS_MAND_TRUE;
5057 lys_compile_mandatory_parents(node->parent, 1);
5058 } else {
5059 node->flags &= ~LYS_MAND_TRUE;
5060 lys_compile_mandatory_parents(node->parent, 0);
5061 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005062 }
5063 break;
5064 default:
5065 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005066 "Invalid refine of %s statement - %s cannot hold this statement.",
5067 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005068 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005069 }
5070 }
Radek Krejcif0089082019-01-07 16:42:01 +01005071
5072 /* if-feature */
5073 if (rfn->iffeatures) {
5074 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005075 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005076 }
Radek Krejci327de162019-06-14 12:52:07 +02005077
5078 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005079 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005080
Radek Krejcif2271f12019-01-07 16:42:23 +01005081 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005082 for (uint32_t i = 0; i < refined.count; ++i) {
5083 node = (struct lysc_node*)refined.objs[i];
5084 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02005085 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005086
5087 /* check possible conflict with default value (default added, mandatory left true) */
5088 if ((node->flags & LYS_MAND_TRUE) &&
5089 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5090 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5091 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005092 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005093 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005094 }
5095
5096 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5097 if (node->nodetype == LYS_LIST) {
5098 min = ((struct lysc_node_list*)node)->min;
5099 max = ((struct lysc_node_list*)node)->max;
5100 } else {
5101 min = ((struct lysc_node_leaflist*)node)->min;
5102 max = ((struct lysc_node_leaflist*)node)->max;
5103 }
5104 if (min > max) {
5105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005106 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5107 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005108 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005109 }
5110 }
5111 }
5112
Radek Krejci327de162019-06-14 12:52:07 +02005113 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005114 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005115
5116cleanup:
5117 /* fix connection of the children nodes from fake context node back into the parent */
5118 if (context_node_fake.child) {
5119 context_node_fake.child->prev = child;
5120 }
5121 LY_LIST_FOR(context_node_fake.child, child) {
5122 child->parent = parent;
5123 }
5124
5125 if (uses_p->augments || uses_p->refines) {
5126 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005127 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005128 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005129 LY_ARRAY_FREE(context_node_fake.actions);
5130 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005131 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005132 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005133 LY_ARRAY_FREE(context_node_fake.notifs);
5134 }
5135 }
5136
Radek Krejcie86bf772018-12-14 11:39:53 +01005137 /* reload previous context's mod_def */
5138 ctx->mod_def = mod_old;
5139 /* remove the grouping from the stack for circular groupings dependency check */
5140 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5141 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005142 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005143 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005144
5145 return ret;
5146}
5147
Radek Krejci327de162019-06-14 12:52:07 +02005148static int
5149lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5150{
5151 struct lysp_node *iter;
5152 int len = 0;
5153
5154 *path = NULL;
5155 for (iter = node; iter && len >= 0; iter = iter->parent) {
5156 char *s = *path;
5157 char *id;
5158
5159 switch (iter->nodetype) {
5160 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005161 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005162 break;
5163 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005164 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005165 break;
5166 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005167 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005168 break;
5169 default:
5170 id = strdup(iter->name);
5171 break;
5172 }
5173
5174 if (!iter->parent) {
5175 /* print prefix */
5176 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5177 } else {
5178 /* prefix is the same as in parent */
5179 len = asprintf(path, "/%s%s", id, s ? s : "");
5180 }
5181 free(s);
5182 free(id);
5183 }
5184
5185 if (len < 0) {
5186 free(*path);
5187 *path = NULL;
5188 } else if (len == 0) {
5189 *path = strdup("/");
5190 len = 1;
5191 }
5192 return len;
5193}
5194
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005195/**
5196 * @brief Validate groupings that were defined but not directly used in the schema itself.
5197 *
5198 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5199 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5200 */
5201static LY_ERR
5202lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5203{
5204 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005205 char *path;
5206 int len;
5207
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005208 struct lysp_node_uses fake_uses = {
5209 .parent = node_p,
5210 .nodetype = LYS_USES,
5211 .flags = 0, .next = NULL,
5212 .name = grp->name,
5213 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5214 .refines = NULL, .augments = NULL
5215 };
5216 struct lysc_node_container fake_container = {
5217 .nodetype = LYS_CONTAINER,
5218 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5219 .module = ctx->mod,
5220 .sp = NULL, .parent = NULL, .next = NULL,
5221 .prev = (struct lysc_node*)&fake_container,
5222 .name = "fake",
5223 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5224 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5225 };
5226
5227 if (grp->parent) {
5228 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5229 }
Radek Krejci327de162019-06-14 12:52:07 +02005230
5231 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5232 if (len < 0) {
5233 LOGMEM(ctx->ctx);
5234 return LY_EMEM;
5235 }
5236 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5237 ctx->path_len = (uint16_t)len;
5238 free(path);
5239
5240 lysc_update_path(ctx, NULL, "{grouping}");
5241 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005242 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005243 lysc_update_path(ctx, NULL, NULL);
5244 lysc_update_path(ctx, NULL, NULL);
5245
5246 ctx->path_len = 1;
5247 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005248
5249 /* cleanup */
5250 lysc_node_container_free(ctx->ctx, &fake_container);
5251
5252 return ret;
5253}
Radek Krejcife909632019-02-12 15:34:42 +01005254
Radek Krejcie86bf772018-12-14 11:39:53 +01005255/**
Radek Krejcia3045382018-11-22 14:30:31 +01005256 * @brief Compile parsed schema node information.
5257 * @param[in] ctx Compile context
5258 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005259 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5260 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5261 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005262 * @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).
5263 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005264 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5265 */
Radek Krejci19a96102018-11-15 13:38:09 +01005266static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005267lys_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 +01005268{
Radek Krejci1c54f462020-05-12 17:25:34 +02005269 LY_ERR ret = LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01005270 struct lysc_node *node;
5271 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005272 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005273 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005274 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005275
Radek Krejci327de162019-06-14 12:52:07 +02005276 if (node_p->nodetype != LYS_USES) {
5277 lysc_update_path(ctx, parent, node_p->name);
5278 } else {
5279 lysc_update_path(ctx, NULL, "{uses}");
5280 lysc_update_path(ctx, NULL, node_p->name);
5281 }
5282
Radek Krejci19a96102018-11-15 13:38:09 +01005283 switch (node_p->nodetype) {
5284 case LYS_CONTAINER:
5285 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5286 node_compile_spec = lys_compile_node_container;
5287 break;
5288 case LYS_LEAF:
5289 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5290 node_compile_spec = lys_compile_node_leaf;
5291 break;
5292 case LYS_LIST:
5293 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005294 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005295 break;
5296 case LYS_LEAFLIST:
5297 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005298 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005299 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005300 case LYS_CHOICE:
5301 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005302 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005303 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005304 case LYS_ANYXML:
5305 case LYS_ANYDATA:
5306 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005307 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005308 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005309 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005310 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5311 lysc_update_path(ctx, NULL, NULL);
5312 lysc_update_path(ctx, NULL, NULL);
5313 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005314 default:
5315 LOGINT(ctx->ctx);
5316 return LY_EINT;
5317 }
5318 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5319 node->nodetype = node_p->nodetype;
5320 node->module = ctx->mod;
5321 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005322 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005323
5324 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005325 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005326 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005327 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005328 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5329 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005330 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005331 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005332 node->flags |= LYS_CONFIG_R;
5333 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005334 /* config not explicitely set, inherit it from parent */
5335 if (parent) {
5336 node->flags |= parent->flags & LYS_CONFIG_MASK;
5337 } else {
5338 /* default is config true */
5339 node->flags |= LYS_CONFIG_W;
5340 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005341 } else {
5342 /* config set explicitely */
5343 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005344 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005345 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5346 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5347 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005348 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005349 goto error;
5350 }
Radek Krejci19a96102018-11-15 13:38:09 +01005351
Radek Krejcia6d57732018-11-29 13:40:37 +01005352 /* *list ordering */
5353 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5354 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005355 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005356 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5357 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005358 node->flags &= ~LYS_ORDBY_MASK;
5359 node->flags |= LYS_ORDBY_SYSTEM;
5360 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5361 /* default ordering is system */
5362 node->flags |= LYS_ORDBY_SYSTEM;
5363 }
5364 }
5365
Radek Krejci19a96102018-11-15 13:38:09 +01005366 /* status - it is not inherited by specification, but it does not make sense to have
5367 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005368 if (!parent || parent->nodetype != LYS_CHOICE) {
5369 /* in case of choice/case's children, postpone the check to the moment we know if
5370 * 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 +02005371 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 +01005372 }
5373
Radek Krejciec4da802019-05-02 13:02:41 +02005374 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005375 node->sp = node_p;
5376 }
5377 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005378 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5379 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005380 if (node_p->when) {
5381 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005382 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005383
5384 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5385 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02005386 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01005387 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005388 }
Radek Krejciec4da802019-05-02 13:02:41 +02005389 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005390
5391 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005392 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005393
Radek Krejci0935f412019-08-20 16:15:18 +02005394 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5395
Radek Krejcife909632019-02-12 15:34:42 +01005396 /* inherit LYS_MAND_TRUE in parent containers */
5397 if (node->flags & LYS_MAND_TRUE) {
5398 lys_compile_mandatory_parents(parent, 1);
5399 }
5400
Radek Krejci327de162019-06-14 12:52:07 +02005401 lysc_update_path(ctx, NULL, NULL);
5402
Radek Krejci19a96102018-11-15 13:38:09 +01005403 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005404 if (parent) {
5405 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005406 if (node_p->parent->nodetype == LYS_CASE) {
5407 lysc_update_path(ctx, parent, node_p->parent->name);
5408 } else {
5409 lysc_update_path(ctx, parent, node->name);
5410 }
Radek Krejciec4da802019-05-02 13:02:41 +02005411 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005412 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005413 if (uses_status) {
5414
5415 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005416 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005417 * it directly gets the same status flags as the choice;
5418 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005419 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005420 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005421 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005422 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005423 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005424 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005425 }
Radek Krejciec4da802019-05-02 13:02:41 +02005426 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 +02005427
5428 if (parent->nodetype == LYS_CHOICE) {
5429 lysc_update_path(ctx, NULL, NULL);
5430 }
Radek Krejci19a96102018-11-15 13:38:09 +01005431 } else {
5432 /* top-level element */
5433 if (!ctx->mod->compiled->data) {
5434 ctx->mod->compiled->data = node;
5435 } else {
5436 /* insert at the end of the module's top-level nodes list */
5437 ctx->mod->compiled->data->prev->next = node;
5438 node->prev = ctx->mod->compiled->data->prev;
5439 ctx->mod->compiled->data->prev = node;
5440 }
Radek Krejci327de162019-06-14 12:52:07 +02005441 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005442 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5443 ctx->mod->compiled->notifs, node->name, node)) {
5444 return LY_EVALID;
5445 }
Radek Krejci19a96102018-11-15 13:38:09 +01005446 }
Radek Krejci327de162019-06-14 12:52:07 +02005447 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005448
5449 return LY_SUCCESS;
5450
5451error:
5452 lysc_node_free(ctx->ctx, node);
5453 return ret;
5454}
5455
Radek Krejciccd20f12019-02-15 14:12:27 +01005456static void
5457lysc_disconnect(struct lysc_node *node)
5458{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005459 struct lysc_node *parent, *child, *prev = NULL, *next;
5460 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005461 int remove_cs = 0;
5462
5463 parent = node->parent;
5464
5465 /* parent's first child */
5466 if (!parent) {
5467 return;
5468 }
5469 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005470 cs = (struct lysc_node_case*)node;
5471 } else if (parent->nodetype == LYS_CASE) {
5472 /* disconnecting some node in a case */
5473 cs = (struct lysc_node_case*)parent;
5474 parent = cs->parent;
5475 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5476 if (child == node) {
5477 if (cs->child == child) {
5478 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5479 /* case with a single child -> remove also the case */
5480 child->parent = NULL;
5481 remove_cs = 1;
5482 } else {
5483 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005484 }
5485 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005486 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005487 }
5488 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005489 if (!remove_cs) {
5490 cs = NULL;
5491 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005492 } else if (lysc_node_children(parent, node->flags) == node) {
5493 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005494 }
5495
5496 if (cs) {
5497 if (remove_cs) {
5498 /* cs has only one child which is being also removed */
5499 lysc_disconnect((struct lysc_node*)cs);
5500 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5501 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005502 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5503 /* default case removed */
5504 ((struct lysc_node_choice*)parent)->dflt = NULL;
5505 }
5506 if (((struct lysc_node_choice*)parent)->cases == cs) {
5507 /* first case removed */
5508 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5509 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005510 if (cs->child) {
5511 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5512 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5513 prev = cs->child->prev;
5514 } /* else all the children are under a single case */
5515 LY_LIST_FOR_SAFE(cs->child, next, child) {
5516 if (child->parent != (struct lysc_node*)cs) {
5517 break;
5518 }
5519 lysc_node_free(node->module->ctx, child);
5520 }
5521 if (prev) {
5522 if (prev->next) {
5523 prev->next = child;
5524 }
5525 if (child) {
5526 child->prev = prev;
5527 } else {
5528 /* link from the first child under the cases */
5529 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5530 }
5531 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005532 }
5533 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005534 }
5535
5536 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005537 if (node->prev->next) {
5538 node->prev->next = node->next;
5539 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005540 if (node->next) {
5541 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005542 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005543 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005544 if (child) {
5545 child->prev = node->prev;
5546 }
5547 } else if (((struct lysc_node_choice*)parent)->cases) {
5548 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005549 }
5550}
5551
5552LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005553lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005554{
Radek Krejcia1911222019-07-22 17:24:50 +02005555 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005556 struct ly_set devs_p = {0};
5557 struct ly_set targets = {0};
5558 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005559 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005560 struct lysc_action *rpcs;
5561 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005562 struct lysp_deviation *dev;
5563 struct lysp_deviate *d, **dp_new;
5564 struct lysp_deviate_add *d_add;
5565 struct lysp_deviate_del *d_del;
5566 struct lysp_deviate_rpl *d_rpl;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005567 LY_ARRAY_COUNT_TYPE u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005568 struct lysc_deviation {
5569 const char *nodeid;
5570 struct lysc_node *target; /* target node of the deviation */
5571 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005572 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005573 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5574 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005575 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005576 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005577 const char *prefix, *name, *nodeid, *dflt;
Michal Vasko57c10cd2020-05-27 15:57:11 +02005578 struct lys_module *mod, **dev_mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005579 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005580 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005581
5582 /* get all deviations from the module and all its submodules ... */
5583 LY_ARRAY_FOR(mod_p->deviations, u) {
5584 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5585 }
5586 LY_ARRAY_FOR(mod_p->includes, v) {
5587 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5588 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5589 }
5590 }
5591 if (!devs_p.count) {
5592 /* nothing to do */
5593 return LY_SUCCESS;
5594 }
5595
Radek Krejci327de162019-06-14 12:52:07 +02005596 lysc_update_path(ctx, NULL, "{deviation}");
5597
Radek Krejciccd20f12019-02-15 14:12:27 +01005598 /* ... and group them by the target node */
5599 devs = calloc(devs_p.count, sizeof *devs);
5600 for (u = 0; u < devs_p.count; ++u) {
5601 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005602 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005603
5604 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005605 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5606 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01005607 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005608 /* move the target pointer to input/output to make them different from the action and
5609 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5610 * back to the RPC/action node due to a better compatibility and decision code in this function.
5611 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5612 if (flags & LYSC_OPT_RPC_INPUT) {
5613 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5614 flags |= LYSC_OPT_INTERNAL;
5615 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5616 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5617 flags |= LYSC_OPT_INTERNAL;
5618 }
5619 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005620 /* insert into the set of targets with duplicity detection */
5621 i = ly_set_add(&targets, target, 0);
5622 if (!devs[i]) {
5623 /* new record */
5624 devs[i] = calloc(1, sizeof **devs);
5625 devs[i]->target = target;
5626 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005627 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005628 }
5629 /* add deviates into the deviation's list of deviates */
5630 for (d = dev->deviates; d; d = d->next) {
5631 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5632 *dp_new = d;
5633 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5634 devs[i]->not_supported = 1;
5635 }
5636 }
Radek Krejci327de162019-06-14 12:52:07 +02005637
5638 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005639 }
5640
5641 /* MACROS for deviates checking */
5642#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5643 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005644 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 +01005645 goto cleanup; \
5646 }
5647
5648#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005649 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5650 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5651 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005652 goto cleanup; \
5653 }
5654
Radek Krejcia1911222019-07-22 17:24:50 +02005655
Radek Krejciccd20f12019-02-15 14:12:27 +01005656#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005657 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5658 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5659 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5660 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5661 goto cleanup; \
5662 }
5663
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005664#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005665 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005666 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005667 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5668 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005670 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5671 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005672 goto cleanup; \
5673 }
5674
Radek Krejci551b12c2019-02-19 16:11:21 +01005675#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5676 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005678 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5679 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005680 goto cleanup; \
5681 }
5682
Radek Krejciccd20f12019-02-15 14:12:27 +01005683#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5684 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005685 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005686 goto cleanup; \
5687 }
5688
Radek Krejci551b12c2019-02-19 16:11:21 +01005689#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5690 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005692 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005693 goto cleanup; \
5694 }
5695
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005696#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5697 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5698 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5699 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5700 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 +01005701 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005702 if (y == LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005703 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005704 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5705 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005706 goto cleanup; \
5707 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005708 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5709 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5710 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5711 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005712 (LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005713 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005714 if (!LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005715 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5716 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005717 }
5718
5719 /* apply deviations */
5720 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005721 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5722 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5723 struct ly_err_item *err = NULL;
5724
5725 dflt = NULL;
5726 changed_type = 0;
5727
Radek Krejci327de162019-06-14 12:52:07 +02005728 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5729
Radek Krejcif538ce52019-03-05 10:46:14 +01005730 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5731 /* fix the target pointer in case of RPC's/action's input/output */
5732 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5733 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5734 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5735 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5736 }
5737 }
5738
Radek Krejciccd20f12019-02-15 14:12:27 +01005739 /* not-supported */
5740 if (devs[u]->not_supported) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005741 if (LY_ARRAY_COUNT(devs[u]->deviates) > 1) {
5742 LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.",
5743 LY_ARRAY_COUNT(devs[u]->deviates), devs[u]->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005744 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005745
5746#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5747 if (devs[u]->target->parent) { \
5748 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5749 } else { \
5750 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5751 } \
5752 LY_ARRAY_FOR(ARRAY, x) { \
5753 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5754 } \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005755 if (x < LY_ARRAY_COUNT(ARRAY)) { \
Radek Krejcifc11bd72019-04-11 16:00:05 +02005756 FREEFUNC(ctx->ctx, &ARRAY[x]); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005757 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
Radek Krejcifc11bd72019-04-11 16:00:05 +02005758 LY_ARRAY_DECREMENT(ARRAY); \
5759 }
5760
Michal Vasko1bf09392020-03-27 12:38:10 +01005761 if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005762 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5763 /* remove RPC's/action's input */
5764 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5765 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005766 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5767 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005768 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5769 /* remove RPC's/action's output */
5770 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5771 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005772 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5773 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005774 } else {
5775 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005776 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005777 }
5778 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005779 /* remove Notification */
5780 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005781 } else {
5782 /* remove the target node */
5783 lysc_disconnect(devs[u]->target);
5784 lysc_node_free(ctx->ctx, devs[u]->target);
5785 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005786
Radek Krejci474f9b82019-07-24 11:36:37 +02005787 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5788 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005789 continue;
5790 }
5791
5792 /* list of deviates (not-supported is not present in the list) */
5793 LY_ARRAY_FOR(devs[u]->deviates, v) {
5794 d = devs[u]->deviates[v];
5795
5796 switch (d->mod) {
5797 case LYS_DEV_ADD:
5798 d_add = (struct lysp_deviate_add*)d;
5799 /* [units-stmt] */
5800 if (d_add->units) {
5801 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5802 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5803
5804 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5805 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5806 }
5807
5808 /* *must-stmt */
5809 if (d_add->musts) {
5810 switch (devs[u]->target->nodetype) {
5811 case LYS_CONTAINER:
5812 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005813 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5814 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005815 break;
5816 case LYS_LEAF:
5817 case LYS_LEAFLIST:
5818 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005819 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5820 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005821 break;
5822 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005823 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5824 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005825 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005826 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005827 case LYS_ACTION:
5828 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005829 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5830 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005831 break;
Michal Vaskocc048b22020-03-27 15:52:38 +01005832 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005833 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5834 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005835 break;
5836 }
5837 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005838 default:
5839 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005840 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005841 goto cleanup;
5842 }
Michal Vasko004d3152020-06-11 19:59:22 +02005843 ly_set_add(&ctx->xpath, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01005844 }
5845
5846 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005847 if (d_add->uniques) {
5848 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5849 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5850 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005851
5852 /* *default-stmt */
5853 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005854 switch (devs[u]->target->nodetype) {
5855 case LYS_LEAF:
5856 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005857 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 +02005858 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005859 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005860 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005861 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5862 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5863 } else {
5864 /* prepare new default value storage */
5865 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005866 }
Radek Krejcia1911222019-07-22 17:24:50 +02005867 dflt = d_add->dflts[0];
5868 /* parsing is done at the end after possible replace of the leaf's type */
5869
Radek Krejci551b12c2019-02-19 16:11:21 +01005870 /* mark the new default values as leaf's own */
5871 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005872 break;
5873 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005874 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005875 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005876 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005877 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005878 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5879 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5880 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005881 }
Radek Krejcia1911222019-07-22 17:24:50 +02005882 LY_ARRAY_FREE(llist->dflts);
5883 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005884 LY_ARRAY_FREE(llist->dflts_mods);
5885 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005886 }
5887 /* add new default value(s) */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005888 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup);
5889 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup);
5890 for (x = y = LY_ARRAY_COUNT(llist->dflts);
5891 x < LY_ARRAY_COUNT(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005892 LY_ARRAY_INCREMENT(llist->dflts_mods);
5893 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005894 LY_ARRAY_INCREMENT(llist->dflts);
5895 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5896 llist->dflts[x]->realtype = llist->type;
5897 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 +02005898 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005899 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005900 llist->dflts[x]->realtype->refcount++;
5901 if (err) {
5902 ly_err_print(err);
5903 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5904 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5905 d_add->dflts[x - y], err->msg);
5906 ly_err_free(err);
5907 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005908 if (rc == LY_EINCOMPLETE) {
5909 /* postpone default compilation when the tree is complete */
5910 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5911
5912 /* but in general result is so far ok */
5913 rc = LY_SUCCESS;
5914 }
Radek Krejcia1911222019-07-22 17:24:50 +02005915 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005916 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005917 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005918 devs[u]->target->flags |= LYS_SET_DFLT;
5919 break;
5920 case LYS_CHOICE:
5921 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5922 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5923 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5924 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005925 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 +01005926 goto cleanup;
5927 }
5928 break;
5929 default:
5930 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005931 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005932 goto cleanup;
5933 }
5934 }
5935
5936 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005937 if (d_add->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01005938 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005939 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005940 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5941 goto cleanup;
5942 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005943 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005944 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
Michal Vaskoa3881362020-01-21 15:57:35 +01005945 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005946 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005947 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005949 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5950 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005951 goto cleanup;
5952 }
Radek Krejci327de162019-06-14 12:52:07 +02005953 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005954 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005955
5956 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005957 if (d_add->flags & LYS_MAND_MASK) {
5958 if (devs[u]->target->flags & LYS_MAND_MASK) {
5959 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005960 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5961 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005962 goto cleanup;
5963 }
Radek Krejci327de162019-06-14 12:52:07 +02005964 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005965 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005966
5967 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005968 if (d_add->flags & LYS_SET_MIN) {
5969 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5970 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5971 /* change value */
5972 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5973 } else if (devs[u]->target->nodetype == LYS_LIST) {
5974 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5975 /* change value */
5976 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5977 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005979 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5980 goto cleanup;
5981 }
5982 if (d_add->min) {
5983 devs[u]->target->flags |= LYS_MAND_TRUE;
5984 }
5985 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005986
5987 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005988 if (d_add->flags & LYS_SET_MAX) {
5989 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5990 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5991 /* change value */
5992 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5993 } else if (devs[u]->target->nodetype == LYS_LIST) {
5994 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5995 /* change value */
5996 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5997 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005998 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005999 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6000 goto cleanup;
6001 }
6002 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006003
6004 break;
6005 case LYS_DEV_DELETE:
6006 d_del = (struct lysp_deviate_del*)d;
6007
6008 /* [units-stmt] */
6009 if (d_del->units) {
6010 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006011 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6012 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6013 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6014 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6015 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6016 goto cleanup;
6017 }
6018 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6019 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006020 }
6021
6022 /* *must-stmt */
6023 if (d_del->musts) {
6024 switch (devs[u]->target->nodetype) {
6025 case LYS_CONTAINER:
6026 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006027 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006028 break;
6029 case LYS_LEAF:
6030 case LYS_LEAFLIST:
6031 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006032 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006033 break;
6034 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006035 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006036 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006037 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006038 case LYS_ACTION:
6039 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6040 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6041 break;
6042 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6043 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6044 break;
6045 }
6046 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006047 default:
6048 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006049 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006050 goto cleanup;
6051 }
6052 }
6053
6054 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006055 if (d_del->uniques) {
6056 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6057 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6058 LY_ARRAY_FOR(d_del->uniques, x) {
6059 LY_ARRAY_FOR(list->uniques, z) {
6060 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6061 nodeid = strpbrk(name, " \t\n");
6062 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02006063 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006064 break;
6065 }
6066 while (isspace(*nodeid)) {
6067 ++nodeid;
6068 }
6069 } else {
6070 if (strcmp(name, list->uniques[z][y]->name)) {
6071 break;
6072 }
6073 }
6074 }
6075 if (!name) {
6076 /* complete match - remove the unique */
6077 LY_ARRAY_DECREMENT(list->uniques);
6078 LY_ARRAY_FREE(list->uniques[z]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006079 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
Radek Krejci7af64242019-02-18 13:07:53 +01006080 --z;
6081 break;
6082 }
6083 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006084 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006085 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006086 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6087 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006088 goto cleanup;
6089 }
6090 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006091 if (!LY_ARRAY_COUNT(list->uniques)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006092 LY_ARRAY_FREE(list->uniques);
6093 list->uniques = NULL;
6094 }
6095 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006096
6097 /* *default-stmt */
6098 if (d_del->dflts) {
6099 switch (devs[u]->target->nodetype) {
6100 case LYS_LEAF:
6101 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6102 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6103 dflt, "deleting", "default", d_del->dflts[0]);
6104
Radek Krejcia1911222019-07-22 17:24:50 +02006105 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006106 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006107 if (strcmp(dflt, d_del->dflts[0])) {
6108 if (i) {
6109 free((char*)dflt);
6110 }
6111 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6112 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6113 d_del->dflts[0], dflt);
6114 goto cleanup;
6115 }
6116 if (i) {
6117 free((char*)dflt);
6118 }
6119 dflt = NULL;
6120
Radek Krejci474f9b82019-07-24 11:36:37 +02006121 /* update the list of incomplete default values if needed */
6122 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6123
Radek Krejcia1911222019-07-22 17:24:50 +02006124 /* remove the default specification */
6125 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6126 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6127 free(leaf->dflt);
6128 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006129 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006130 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006131 break;
6132 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006133 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6134 LY_ARRAY_FOR(d_del->dflts, x) {
6135 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006136 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 +02006137 if (!strcmp(dflt, d_del->dflts[x])) {
6138 if (i) {
6139 free((char*)dflt);
6140 }
6141 dflt = NULL;
6142 break;
6143 }
6144 if (i) {
6145 free((char*)dflt);
6146 }
6147 dflt = NULL;
6148 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006149 if (y == LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02006150 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6151 "which does not match any of the target's property values.", d_del->dflts[x]);
6152 goto cleanup;
6153 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006154
6155 /* update the list of incomplete default values if needed */
6156 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6157
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006158 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006159 LY_ARRAY_DECREMENT(llist->dflts);
6160 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6161 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6162 free(llist->dflts[y]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006163 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
6164 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 +02006165 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006166 if (!LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006167 LY_ARRAY_FREE(llist->dflts_mods);
6168 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006169 LY_ARRAY_FREE(llist->dflts);
6170 llist->dflts = NULL;
6171 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006172 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006173 break;
6174 case LYS_CHOICE:
6175 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6176 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6177 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006178 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006179 if (prefix) {
6180 /* use module prefixes from the deviation module to match the module of the default case */
6181 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6182 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006183 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6184 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006185 goto cleanup;
6186 }
6187 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6188 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006189 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6190 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006191 goto cleanup;
6192 }
6193 }
6194 /* else {
6195 * strictly, the default prefix would point to the deviation module, but the value should actually
6196 * match the default string in the original module (usually unprefixed), so in this case we do not check
6197 * the module of the default case, just matching its name */
6198 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6199 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006200 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6201 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006202 goto cleanup;
6203 }
6204 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6205 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6206 break;
6207 default:
6208 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006209 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006210 goto cleanup;
6211 }
6212 }
6213
6214 break;
6215 case LYS_DEV_REPLACE:
6216 d_rpl = (struct lysp_deviate_rpl*)d;
6217
6218 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006219 if (d_rpl->type) {
6220 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6221 /* type is mandatory, so checking for its presence is not necessary */
6222 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006223
6224 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6225 /* the target has default from the previous type - remove it */
6226 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006227 /* update the list of incomplete default values if needed */
6228 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6229
Radek Krejcia1911222019-07-22 17:24:50 +02006230 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6231 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6232 free(leaf->dflt);
6233 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006234 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006235 } else { /* LYS_LEAFLIST */
6236 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006237 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006238 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6239 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6240 free(llist->dflts[x]);
6241 }
6242 LY_ARRAY_FREE(llist->dflts);
6243 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006244 LY_ARRAY_FREE(llist->dflts_mods);
6245 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006246 }
6247 }
6248 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006249 /* there is no default value, do not set changed_type after type compilation
6250 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006251 changed_type = -1;
6252 }
Radek Krejciec4da802019-05-02 13:02:41 +02006253 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 +02006254 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006255 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006256
6257 /* [units-stmt] */
6258 if (d_rpl->units) {
6259 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6260 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6261 units, "replacing", "units", d_rpl->units);
6262
6263 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6264 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6265 }
6266
6267 /* [default-stmt] */
6268 if (d_rpl->dflt) {
6269 switch (devs[u]->target->nodetype) {
6270 case LYS_LEAF:
6271 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6272 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006273 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006274 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006275 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6276 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6277 dflt = d_rpl->dflt;
6278 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006279 break;
6280 case LYS_CHOICE:
6281 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006282 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 +01006283 goto cleanup;
6284 }
6285 break;
6286 default:
6287 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006288 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006289 goto cleanup;
6290 }
6291 }
6292
6293 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006294 if (d_rpl->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01006295 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006296 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006297 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6298 goto cleanup;
6299 }
6300 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006301 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006302 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6303 goto cleanup;
6304 }
Radek Krejci327de162019-06-14 12:52:07 +02006305 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006306 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006307
6308 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006309 if (d_rpl->flags & LYS_MAND_MASK) {
6310 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006311 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006312 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6313 goto cleanup;
6314 }
Radek Krejci327de162019-06-14 12:52:07 +02006315 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006316 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006317
6318 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006319 if (d_rpl->flags & LYS_SET_MIN) {
6320 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6321 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6322 /* change value */
6323 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6324 } else if (devs[u]->target->nodetype == LYS_LIST) {
6325 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6326 /* change value */
6327 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6328 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006329 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006330 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6331 goto cleanup;
6332 }
6333 if (d_rpl->min) {
6334 devs[u]->target->flags |= LYS_MAND_TRUE;
6335 }
6336 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006337
6338 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006339 if (d_rpl->flags & LYS_SET_MAX) {
6340 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6341 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6342 /* change value */
6343 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6344 } else if (devs[u]->target->nodetype == LYS_LIST) {
6345 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6346 /* change value */
6347 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6348 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006349 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006350 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6351 goto cleanup;
6352 }
6353 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006354
6355 break;
6356 default:
6357 LOGINT(ctx->ctx);
6358 goto cleanup;
6359 }
6360 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006361
Radek Krejci33f72892019-02-21 10:36:58 +01006362 /* final check when all deviations of a single target node are applied */
6363
Radek Krejci551b12c2019-02-19 16:11:21 +01006364 /* check min-max compatibility */
6365 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6366 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6367 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6368 } else if (devs[u]->target->nodetype == LYS_LIST) {
6369 min = ((struct lysc_node_list*)devs[u]->target)->min;
6370 max = ((struct lysc_node_list*)devs[u]->target)->max;
6371 } else {
6372 min = max = 0;
6373 }
6374 if (min > max) {
6375 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 +02006376 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006377 goto cleanup;
6378 }
6379
Radek Krejcia1911222019-07-22 17:24:50 +02006380 if (dflt) {
6381 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006382 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006383 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006384 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6385 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006386 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006387 leaf->dflt->realtype->refcount++;
6388 if (err) {
6389 ly_err_print(err);
6390 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6391 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6392 ly_err_free(err);
6393 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006394 if (rc == LY_EINCOMPLETE) {
6395 /* postpone default compilation when the tree is complete */
6396 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6397
6398 /* but in general result is so far ok */
6399 rc = LY_SUCCESS;
6400 }
Radek Krejcia1911222019-07-22 17:24:50 +02006401 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006402 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006403 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6404 int dynamic;
6405 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006406 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006407
6408 /* update the list of incomplete default values if needed */
6409 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6410
6411 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006412 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6413 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6414 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006415 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6416 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006417 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006418 leaf->dflt->realtype->refcount++;
6419 if (err) {
6420 ly_err_print(err);
6421 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6422 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6423 ly_err_free(err);
6424 }
6425 if (dynamic) {
6426 free((void*)dflt);
6427 }
Radek Krejcia1911222019-07-22 17:24:50 +02006428 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006429 if (rc == LY_EINCOMPLETE) {
6430 /* postpone default compilation when the tree is complete */
6431 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6432
6433 /* but in general result is so far ok */
6434 rc = LY_SUCCESS;
6435 }
6436 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006437 } else { /* LYS_LEAFLIST */
6438 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006439 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 +02006440 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6441 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6442 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006443 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6444 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006445 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6446 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006447 llist->dflts[x]->realtype->refcount++;
6448 if (err) {
6449 ly_err_print(err);
6450 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6451 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6452 dflt, err->msg);
6453 ly_err_free(err);
6454 }
6455 if (dynamic) {
6456 free((void*)dflt);
6457 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006458 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006459 if (rc == LY_EINCOMPLETE) {
6460 /* postpone default compilation when the tree is complete */
6461 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6462
6463 /* but in general result is so far ok */
6464 rc = LY_SUCCESS;
6465 }
Radek Krejcia1911222019-07-22 17:24:50 +02006466 LY_CHECK_GOTO(rc, cleanup);
6467 }
6468 }
6469 }
6470
Radek Krejci551b12c2019-02-19 16:11:21 +01006471 /* check mandatory - default compatibility */
6472 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6473 && (devs[u]->target->flags & LYS_SET_DFLT)
6474 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6475 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006476 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006477 goto cleanup;
6478 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6479 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6480 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006481 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 +01006482 goto cleanup;
6483 }
6484 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6485 /* mandatory node under a default case */
6486 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006487 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6488 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006489 goto cleanup;
6490 }
Radek Krejci33f72892019-02-21 10:36:58 +01006491
Michal Vaskoe6143202020-07-03 13:02:08 +02006492 /* add this module into the target module deviated_by, if not there already */
6493 rc = LY_SUCCESS;
6494 LY_ARRAY_FOR(devs[u]->target->module->compiled->deviated_by, v) {
6495 if (devs[u]->target->module->compiled->deviated_by[v] == mod_p->mod) {
6496 rc = LY_EEXIST;
6497 break;
6498 }
6499 }
6500 if (!rc) {
6501 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup);
6502 *dev_mod = mod_p->mod;
6503 }
Michal Vasko57c10cd2020-05-27 15:57:11 +02006504
Radek Krejci327de162019-06-14 12:52:07 +02006505 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006506 }
6507
Radek Krejci327de162019-06-14 12:52:07 +02006508 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006509 ret = LY_SUCCESS;
6510
6511cleanup:
6512 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6513 LY_ARRAY_FREE(devs[u]->deviates);
6514 free(devs[u]);
6515 }
6516 free(devs);
6517 ly_set_erase(&targets, NULL);
6518 ly_set_erase(&devs_p, NULL);
6519
6520 return ret;
6521}
6522
Radek Krejcib56c7502019-02-13 14:19:54 +01006523/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006524 * @brief Compile the given YANG submodule into the main module.
6525 * @param[in] ctx Compile context
6526 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006527 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6528 */
6529LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006530lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006531{
6532 unsigned int u;
6533 LY_ERR ret = LY_SUCCESS;
6534 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006535 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006536 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006537 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006538
Michal Vasko33ff9422020-07-03 09:50:39 +02006539 if (!mainmod->mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01006540 /* features are compiled directly into the compiled module structure,
6541 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6542 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006543 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6544 LY_CHECK_GOTO(ret, error);
6545 }
Radek Krejci0af46292019-01-11 16:02:31 +01006546
Michal Vasko33ff9422020-07-03 09:50:39 +02006547 if (!mainmod->mod->dis_identities) {
6548 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities);
6549 LY_CHECK_GOTO(ret, error);
6550 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01006551
Radek Krejci474f9b82019-07-24 11:36:37 +02006552 /* data nodes */
6553 LY_LIST_FOR(submod->data, node_p) {
6554 ret = lys_compile_node(ctx, node_p, NULL, 0);
6555 LY_CHECK_GOTO(ret, error);
6556 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006557
Radek Krejciec4da802019-05-02 13:02:41 +02006558 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6559 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006560
Radek Krejcid05cbd92018-12-05 14:26:40 +01006561error:
6562 return ret;
6563}
6564
Radek Krejci335332a2019-09-05 13:03:35 +02006565static void *
6566lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6567{
6568 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6569 if (substmts[u].stmt == stmt) {
6570 return substmts[u].storage;
6571 }
6572 }
6573 return NULL;
6574}
6575
6576LY_ERR
6577lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6578{
6579 LY_ERR ret = LY_EVALID, r;
6580 unsigned int u;
6581 struct lysp_stmt *stmt;
6582 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006583
6584 /* check for invalid substatements */
6585 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006586 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6587 continue;
6588 }
Radek Krejci335332a2019-09-05 13:03:35 +02006589 for (u = 0; substmts[u].stmt; ++u) {
6590 if (substmts[u].stmt == stmt->kw) {
6591 break;
6592 }
6593 }
6594 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006595 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6596 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006597 goto cleanup;
6598 }
Radek Krejci335332a2019-09-05 13:03:35 +02006599 }
6600
Radek Krejciad5963b2019-09-06 16:03:05 +02006601 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6602
Radek Krejci335332a2019-09-05 13:03:35 +02006603 /* keep order of the processing the same as the order in the defined substmts,
6604 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6605 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006606 int stmt_present = 0;
6607
Radek Krejci335332a2019-09-05 13:03:35 +02006608 for (stmt = ext->child; stmt; stmt = stmt->next) {
6609 if (substmts[u].stmt != stmt->kw) {
6610 continue;
6611 }
6612
Radek Krejciad5963b2019-09-06 16:03:05 +02006613 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006614 if (substmts[u].storage) {
6615 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006616 case LY_STMT_STATUS:
6617 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6618 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6619 break;
6620 case LY_STMT_UNITS: {
6621 const char **units;
6622
6623 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6624 /* single item */
6625 if (*((const char **)substmts[u].storage)) {
6626 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6627 goto cleanup;
6628 }
6629 units = (const char **)substmts[u].storage;
6630 } else {
6631 /* sized array */
6632 const char ***units_array = (const char ***)substmts[u].storage;
6633 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6634 }
6635 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6636 break;
6637 }
Radek Krejci335332a2019-09-05 13:03:35 +02006638 case LY_STMT_TYPE: {
6639 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6640 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6641
6642 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6643 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006644 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006645 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6646 goto cleanup;
6647 }
6648 compiled = substmts[u].storage;
6649 } else {
6650 /* sized array */
6651 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6652 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6653 compiled = (void*)type;
6654 }
6655
Radek Krejciad5963b2019-09-06 16:03:05 +02006656 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006657 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6658 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006659 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6660 lysp_type_free(ctx->ctx, parsed);
6661 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006662 break;
6663 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006664 case LY_STMT_IF_FEATURE: {
6665 struct lysc_iffeature *iff = NULL;
6666
6667 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6668 /* single item */
6669 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6671 goto cleanup;
6672 }
6673 iff = (struct lysc_iffeature*)substmts[u].storage;
6674 } else {
6675 /* sized array */
6676 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6677 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6678 }
6679 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6680 break;
6681 }
6682 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6683 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006684 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006685 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.",
6686 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006687 goto cleanup;
6688 }
6689 }
Radek Krejci335332a2019-09-05 13:03:35 +02006690 }
Radek Krejci335332a2019-09-05 13:03:35 +02006691
Radek Krejciad5963b2019-09-06 16:03:05 +02006692 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6694 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6695 goto cleanup;
6696 }
Radek Krejci335332a2019-09-05 13:03:35 +02006697 }
6698
6699 ret = LY_SUCCESS;
6700
6701cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006702 return ret;
6703}
6704
Michal Vasko175012e2019-11-06 15:49:14 +01006705/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006706 * @brief Check when for cyclic dependencies.
6707 * @param[in] set Set with all the referenced nodes.
6708 * @param[in] node Node whose "when" referenced nodes are in @p set.
6709 * @return LY_ERR value
6710 */
6711static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006712lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006713{
6714 struct lyxp_set tmp_set;
6715 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006716 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006717 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006718 int idx;
6719 struct lysc_when *when;
6720 LY_ERR ret = LY_SUCCESS;
6721
6722 memset(&tmp_set, 0, sizeof tmp_set);
6723
6724 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006725 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006726 xp_scnode = &set->val.scnodes[i];
6727
Michal Vasko5c4e5892019-11-14 12:31:38 +01006728 if (xp_scnode->in_ctx != -1) {
6729 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006730 xp_scnode->in_ctx = 1;
6731 }
6732 }
6733
6734 for (i = 0; i < set->used; ++i) {
6735 xp_scnode = &set->val.scnodes[i];
6736 if (xp_scnode->in_ctx != 1) {
6737 /* already checked */
6738 continue;
6739 }
6740
Michal Vasko1bf09392020-03-27 12:38:10 +01006741 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006742 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006743 /* no when to check */
6744 xp_scnode->in_ctx = 0;
6745 continue;
6746 }
6747
6748 node = xp_scnode->scnode;
6749 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006750 LY_ARRAY_FOR(node->when, u) {
6751 when = node->when[u];
Michal Vasko52927e22020-03-16 17:26:14 +01006752 ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006753 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6754 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006755 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006756 goto cleanup;
6757 }
6758
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006759 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006760 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006761 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006762 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006763 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 +01006764 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006765 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 +01006766 ret = LY_EVALID;
6767 goto cleanup;
6768 }
6769
6770 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006771 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006772 } else {
6773 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006774 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006775 }
6776 }
6777
6778 /* merge this set into the global when set */
6779 lyxp_set_scnode_merge(set, &tmp_set);
6780 }
6781
6782 /* check when of non-data parents as well */
6783 node = node->parent;
6784 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6785
Michal Vasko251f56e2019-11-14 16:06:47 +01006786 /* this node when was checked (xp_scnode could have been reallocd) */
6787 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006788 }
6789
6790cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006791 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006792 return ret;
6793}
6794
6795/**
Michal Vasko175012e2019-11-06 15:49:14 +01006796 * @brief Check when/must expressions of a node on a compiled schema tree.
6797 * @param[in] ctx Compile context.
6798 * @param[in] node Node to check.
6799 * @return LY_ERR value
6800 */
6801static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006802lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006803{
Michal Vasko175012e2019-11-06 15:49:14 +01006804 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006805 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006806 LY_ARRAY_COUNT_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01006807 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006808 struct lysc_when **when = NULL;
6809 struct lysc_must *musts = NULL;
6810 LY_ERR ret = LY_SUCCESS;
6811
6812 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006813 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko175012e2019-11-06 15:49:14 +01006814
6815 switch (node->nodetype) {
6816 case LYS_CONTAINER:
6817 when = ((struct lysc_node_container *)node)->when;
6818 musts = ((struct lysc_node_container *)node)->musts;
6819 break;
6820 case LYS_CHOICE:
6821 when = ((struct lysc_node_choice *)node)->when;
6822 break;
6823 case LYS_LEAF:
6824 when = ((struct lysc_node_leaf *)node)->when;
6825 musts = ((struct lysc_node_leaf *)node)->musts;
6826 break;
6827 case LYS_LEAFLIST:
6828 when = ((struct lysc_node_leaflist *)node)->when;
6829 musts = ((struct lysc_node_leaflist *)node)->musts;
6830 break;
6831 case LYS_LIST:
6832 when = ((struct lysc_node_list *)node)->when;
6833 musts = ((struct lysc_node_list *)node)->musts;
6834 break;
6835 case LYS_ANYXML:
6836 case LYS_ANYDATA:
6837 when = ((struct lysc_node_anydata *)node)->when;
6838 musts = ((struct lysc_node_anydata *)node)->musts;
6839 break;
6840 case LYS_CASE:
6841 when = ((struct lysc_node_case *)node)->when;
6842 break;
6843 case LYS_NOTIF:
6844 musts = ((struct lysc_notif *)node)->musts;
6845 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006846 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006847 case LYS_ACTION:
6848 /* first process input musts */
6849 musts = ((struct lysc_action *)node)->input.musts;
6850 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006851 default:
6852 /* nothing to check */
6853 break;
6854 }
6855
Michal Vasko175012e2019-11-06 15:49:14 +01006856 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006857 LY_ARRAY_FOR(when, u) {
Michal Vasko004d3152020-06-11 19:59:22 +02006858 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 +02006859 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006860 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006861 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006862 goto cleanup;
6863 }
6864
Michal Vaskodc052f32019-11-07 11:11:38 +01006865 ctx->path[0] = '\0';
6866 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006867 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006868 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006869 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6870 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006871
Michal Vaskoecd62de2019-11-13 12:35:11 +01006872 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006873 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006874 schema->name);
6875 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006876
6877 /* check dummy node accessing */
6878 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006879 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006880 ret = LY_EVALID;
6881 goto cleanup;
6882 }
Michal Vasko175012e2019-11-06 15:49:14 +01006883 }
6884 }
6885
Michal Vaskoecd62de2019-11-13 12:35:11 +01006886 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006887 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006888 LY_CHECK_GOTO(ret, cleanup);
6889
Michal Vaskod3678892020-05-21 10:06:58 +02006890 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006891 }
6892
Michal Vasko5d8756a2019-11-07 15:21:00 +01006893check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006894 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006895 LY_ARRAY_FOR(musts, u) {
6896 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 +01006897 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006898 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006899 goto cleanup;
6900 }
6901
Michal Vaskodc052f32019-11-07 11:11:38 +01006902 ctx->path[0] = '\0';
6903 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006904 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006905 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006906 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006907 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006908 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6909 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006910 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006911 }
6912 }
6913
Michal Vaskod3678892020-05-21 10:06:58 +02006914 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006915 }
6916
Michal Vasko1bf09392020-03-27 12:38:10 +01006917 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006918 /* now check output musts */
6919 input_done = 1;
6920 musts = ((struct lysc_action *)node)->output.musts;
6921 opts = LYXP_SCNODE_OUTPUT;
6922 goto check_musts;
6923 }
6924
Michal Vasko175012e2019-11-06 15:49:14 +01006925cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006926 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006927 return ret;
6928}
6929
Michal Vasko8d544252020-03-02 10:19:52 +01006930static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006931lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6932{
6933 const struct lysc_node *target = NULL;
6934 struct ly_path *p;
6935 struct lysc_type *type;
6936
6937 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6938
6939 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02006940 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
6941 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
6942 lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02006943
6944 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006945 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02006946 ly_path_free(node->module->ctx, p);
6947
6948 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
6949 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6950 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
6951 lref->path->expr, lys_nodetype2str(target->nodetype));
6952 return LY_EVALID;
6953 }
6954
6955 /* check status */
6956 ctx->path[0] = '\0';
6957 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6958 ctx->path_len = strlen(ctx->path);
6959 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
6960 return LY_EVALID;
6961 }
6962 ctx->path_len = 1;
6963 ctx->path[1] = '\0';
6964
6965 /* check config */
6966 if (lref->require_instance && (node->flags & LYS_CONFIG_W)) {
6967 if (target->flags & LYS_CONFIG_R) {
6968 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
6969 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
6970 return LY_EVALID;
6971 }
6972 }
6973
6974 /* store the target's type and check for circular chain of leafrefs */
6975 lref->realtype = ((struct lysc_node_leaf *)target)->type;
6976 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
6977 if (type == (struct lysc_type *)lref) {
6978 /* circular chain detected */
6979 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6980 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
6981 return LY_EVALID;
6982 }
6983 }
6984
6985 /* check if leafref and its target are under common if-features */
6986 if (lys_compile_leafref_features_validate(node, target)) {
6987 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6988 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
6989 " features applicable to the leafref itself.", lref->path->expr);
6990 return LY_EVALID;
6991 }
6992
6993 return LY_SUCCESS;
6994}
6995
6996static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01006997lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
6998{
6999 struct lysc_ext_instance *ext;
7000 struct lysp_ext_instance *ext_p = NULL;
7001 struct lysp_stmt *stmt;
7002 const struct lys_module *ext_mod;
7003 LY_ERR ret = LY_SUCCESS;
7004
7005 /* create the parsed extension instance manually */
7006 ext_p = calloc(1, sizeof *ext_p);
7007 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
7008 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
7009 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
7010 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7011 ext_p->insubstmt_index = 0;
7012
7013 stmt = calloc(1, sizeof *ext_p->child);
7014 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
7015 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
7016 stmt->kw = LY_STMT_TYPE;
7017 ext_p->child = stmt;
7018
7019 /* allocate new extension instance */
7020 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7021
7022 /* manually get extension definition module */
7023 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7024
7025 /* compile the extension instance */
7026 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7027
7028cleanup:
7029 lysp_ext_instance_free(ctx->ctx, ext_p);
7030 free(ext_p);
7031 return ret;
7032}
7033
Michal Vasko004d3152020-06-11 19:59:22 +02007034static LY_ERR
7035lys_compile_unres(struct lysc_ctx *ctx)
7036{
7037 struct lysc_node *node;
7038 struct lysc_type *type, *typeiter;
7039 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007040 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007041 uint32_t i;
7042
7043 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7044 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7045 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7046 for (i = 0; i < ctx->leafrefs.count; ++i) {
7047 node = ctx->leafrefs.objs[i];
7048 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7049 type = ((struct lysc_node_leaf *)node)->type;
7050 if (type->basetype == LY_TYPE_LEAFREF) {
7051 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7052 } else if (type->basetype == LY_TYPE_UNION) {
7053 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7054 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7055 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7056 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7057 }
7058 }
7059 }
7060 }
7061 for (i = 0; i < ctx->leafrefs.count; ++i) {
7062 /* store pointer to the real type */
7063 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7064 if (type->basetype == LY_TYPE_LEAFREF) {
7065 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7066 typeiter->basetype == LY_TYPE_LEAFREF;
7067 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7068 ((struct lysc_type_leafref*)type)->realtype = typeiter;
7069 } else if (type->basetype == LY_TYPE_UNION) {
7070 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7071 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7072 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7073 typeiter->basetype == LY_TYPE_LEAFREF;
7074 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7075 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7076 }
7077 }
7078 }
7079 }
7080
7081 /* check xpath */
7082 for (i = 0; i < ctx->xpath.count; ++i) {
7083 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7084 }
7085
7086 /* finish incomplete default values compilation */
7087 for (i = 0; i < ctx->dflts.count; ++i) {
7088 struct ly_err_item *err = NULL;
7089 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
7090 LY_ERR ret;
7091 ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
7092 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7093 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7094 if (err) {
7095 ly_err_print(err);
7096 ctx->path[0] = '\0';
7097 lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7098 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7099 "Invalid default - value does not fit the type (%s).", err->msg);
7100 ly_err_free(err);
7101 }
7102 LY_CHECK_RET(ret);
7103 }
7104
7105 return LY_SUCCESS;
7106}
7107
Radek Krejci19a96102018-11-15 13:38:09 +01007108LY_ERR
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007109lys_compile(struct lys_module **mod, int options)
Radek Krejci19a96102018-11-15 13:38:09 +01007110{
7111 struct lysc_ctx ctx = {0};
7112 struct lysc_module *mod_c;
7113 struct lysp_module *sp;
7114 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007115 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007116 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007117 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007118 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007119 uint32_t i;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007120 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007121
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007122 LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007123
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007124 if (!(*mod)->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007125 /* just imported modules are not compiled */
7126 return LY_SUCCESS;
7127 }
7128
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007129 sp = (*mod)->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007130
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007131 ctx.ctx = (*mod)->ctx;
7132 ctx.mod = *mod;
7133 ctx.mod_def = *mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007134 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007135 ctx.path_len = 1;
7136 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007137
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007138 (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
7139 LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
7140 mod_c->mod = *mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007141
Radek Krejciec4da802019-05-02 13:02:41 +02007142 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007143 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007144 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007145 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7146 }
Radek Krejci0935f412019-08-20 16:15:18 +02007147
7148 /* features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007149 if ((*mod)->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007150 /* there is already precompiled array of features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007151 mod_c->features = (*mod)->dis_features;
7152 (*mod)->dis_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007153 } else {
7154 /* features are compiled directly into the compiled module structure,
7155 * 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 +02007156 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007157 LY_CHECK_GOTO(ret, error);
7158 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007159
Radek Krejci0af46292019-01-11 16:02:31 +01007160 /* finish feature compilation, not only for the main module, but also for the submodules.
7161 * Due to possible forward references, it must be done when all the features (including submodules)
7162 * are present. */
7163 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007164 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007165 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7166 }
Radek Krejci327de162019-06-14 12:52:07 +02007167 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007168 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007169 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007170 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007171 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007172 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7173 }
Radek Krejci327de162019-06-14 12:52:07 +02007174 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007175 }
Radek Krejci327de162019-06-14 12:52:07 +02007176 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007177
Michal Vasko33ff9422020-07-03 09:50:39 +02007178 /* identities, work similarly to features with the precompilation */
7179 if ((*mod)->dis_identities) {
7180 mod_c->identities = (*mod)->dis_identities;
7181 (*mod)->dis_identities = NULL;
7182 } else {
7183 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
7184 LY_CHECK_GOTO(ret, error);
7185 }
Radek Krejci19a96102018-11-15 13:38:09 +01007186 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007187 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007188 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007189 lysc_update_path(&ctx, NULL, "{submodule}");
7190 LY_ARRAY_FOR(sp->includes, v) {
7191 if (sp->includes[v].submodule->identities) {
7192 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7193 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities);
7194 LY_CHECK_GOTO(ret, error);
7195 lysc_update_path(&ctx, NULL, NULL);
7196 }
7197 }
Radek Krejci327de162019-06-14 12:52:07 +02007198 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007199
Radek Krejci95710c92019-02-11 15:49:55 +01007200 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007201 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007202 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007203 }
Radek Krejci95710c92019-02-11 15:49:55 +01007204
Radek Krejciec4da802019-05-02 13:02:41 +02007205 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7206 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007207
Radek Krejci95710c92019-02-11 15:49:55 +01007208 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007209 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007210 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007211 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007212 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007213
Radek Krejci474f9b82019-07-24 11:36:37 +02007214 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007215 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007216
Radek Krejci0935f412019-08-20 16:15:18 +02007217 /* extension instances TODO cover extension instances from submodules */
7218 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007219
Michal Vasko004d3152020-06-11 19:59:22 +02007220 /* finish compilation for all unresolved items in the context */
7221 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007222
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007223 /* validate non-instantiated groupings from the parsed schema,
7224 * without it we would accept even the schemas with invalid grouping specification */
7225 ctx.options |= LYSC_OPT_GROUPING;
7226 LY_ARRAY_FOR(sp->groupings, u) {
7227 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007228 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007229 }
7230 }
7231 LY_LIST_FOR(sp->data, node_p) {
7232 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7233 LY_ARRAY_FOR(grps, u) {
7234 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007235 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007236 }
7237 }
7238 }
7239
Radek Krejci474f9b82019-07-24 11:36:37 +02007240 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7241 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7242 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7243 }
7244
Michal Vasko8d544252020-03-02 10:19:52 +01007245#if 0
7246 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7247 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7248 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7249 * the anotation definitions available in the internal schema structure. */
7250 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7251 if (lyp_add_ietf_netconf_annotations(mod)) {
7252 lys_free(mod, NULL, 1, 1);
7253 return NULL;
7254 }
7255 }
7256#endif
7257
7258 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007259 if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
7260 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007261 }
7262
Radek Krejci1c0c3442019-07-23 16:08:47 +02007263 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007264 ly_set_erase(&ctx.xpath, NULL);
7265 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007266 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007267 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007268 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007269
Radek Krejciec4da802019-05-02 13:02:41 +02007270 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007271 lysp_module_free((*mod)->parsed);
7272 (*mod)->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007273 }
7274
Radek Krejciec4da802019-05-02 13:02:41 +02007275 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007276 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007277 for (i = 0; i < ctx.ctx->list.count; ++i) {
7278 m = ctx.ctx->list.objs[i];
Radek Krejci95710c92019-02-11 15:49:55 +01007279 if (m->implemented == 2) {
7280 m->implemented = 1;
7281 }
7282 }
7283 }
7284
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007285 (*mod)->compiled = mod_c;
Radek Krejci19a96102018-11-15 13:38:09 +01007286 return LY_SUCCESS;
7287
7288error:
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007289 lys_feature_precompile_revert(&ctx, *mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007290 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007291 ly_set_erase(&ctx.xpath, NULL);
7292 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007293 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007294 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007295 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007296 lysc_module_free(mod_c, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007297 (*mod)->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007298
7299 /* revert compilation of modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007300 for (i = 0; i < ctx.ctx->list.count; ++i) {
7301 m = ctx.ctx->list.objs[i];
Michal Vasko2947ce12019-12-16 10:37:19 +01007302 if ((m->implemented == 2) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007303 /* revert features list to the precompiled state */
7304 lys_feature_precompile_revert(&ctx, m);
7305 /* mark module as imported-only / not-implemented */
7306 m->implemented = 0;
7307 /* free the compiled version of the module */
7308 lysc_module_free(m->compiled, NULL);
7309 m->compiled = NULL;
7310 }
7311 }
7312
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007313 /* remove the module itself from the context and free it */
7314 ly_set_rm(&ctx.ctx->list, *mod, NULL);
7315 lys_module_free(*mod, NULL);
7316 *mod = NULL;
7317
Radek Krejci19a96102018-11-15 13:38:09 +01007318 return ret;
7319}