blob: ecdf200f6ffb4d8eb3727e637f31d4474d12933b [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"
Michal Vasko7c8439f2020-08-05 13:25:19 +020039#include "tree_data_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020040#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010041#include "tree_schema_internal.h"
42#include "xpath.h"
43
Michal Vasko5fe75f12020-03-02 13:52:37 +010044static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
45 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
46
Radek Krejci19a96102018-11-15 13:38:09 +010047/**
48 * @brief Duplicate string into dictionary
49 * @param[in] CTX libyang context of the dictionary.
50 * @param[in] ORIG String to duplicate.
51 * @param[out] DUP Where to store the result.
52 */
53#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
54
Radek Krejciec4da802019-05-02 13:02:41 +020055#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010056 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020057 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
58 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci19a96102018-11-15 13:38:09 +010060 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020061 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
Radek Krejciec4da802019-05-02 13:02:41 +020066#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010067 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020068 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
69 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
70 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010071 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020072 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010073 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
74 } \
75 }
76
Radek Krejci0935f412019-08-20 16:15:18 +020077#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
78 if (EXTS_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020079 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
80 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 +020081 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010082 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 +020083 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
84 } \
85 }
86
Radek Krejciec4da802019-05-02 13:02:41 +020087#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010088 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020089 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
90 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
91 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010092 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020093 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010094 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
95 } \
96 }
97
Radek Krejciec4da802019-05-02 13:02:41 +020098#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010099 if (MEMBER_P) { \
100 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
101 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200102 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100103 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
104 }
105
Radek Krejciec4da802019-05-02 13:02:41 +0200106#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100107 if (MEMBER_P) { \
108 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200109 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100110 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200111 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100112 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
113 }
114
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100115#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100116 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200117 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100118 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100119 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
120 return LY_EVALID; \
121 } \
122 } \
123 }
124
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100125#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
126 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200127 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100128 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
129 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
130 return LY_EVALID; \
131 } \
132 } \
133 }
134
135struct lysc_ext *
136lysc_ext_dup(struct lysc_ext *orig)
137{
138 ++orig->refcount;
139 return orig;
140}
141
Radek Krejci19a96102018-11-15 13:38:09 +0100142static struct lysc_ext_instance *
143lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
144{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100145 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100146 (void) ctx;
147 (void) orig;
148 return NULL;
149}
150
Radek Krejcib56c7502019-02-13 14:19:54 +0100151/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200152 * @brief Add record into the compile context's list of incomplete default values.
153 * @param[in] ctx Compile context with the incomplete default values list.
154 * @param[in] context_node Context schema node to store in the record.
155 * @param[in] dflt Incomplete default value to store in the record.
156 * @param[in] dflt_mod Module of the default value definition to store in the record.
157 * @return LY_EMEM in case of memory allocation failure.
158 * @return LY_SUCCESS
159 */
160static LY_ERR
161lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
162{
163 struct lysc_incomplete_dflt *r;
164 r = malloc(sizeof *r);
165 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
166 r->context_node = context_node;
167 r->dflt = dflt;
168 r->dflt_mod = dflt_mod;
169 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
170
171 return LY_SUCCESS;
172}
173
174/**
175 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
176 * @param[in] ctx Compile context with the incomplete default values list.
177 * @param[in] dflt Incomplete default values identifying the record to remove.
178 */
179static void
180lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
181{
182 unsigned int u;
183 struct lysc_incomplete_dflt *r;
184
185 for (u = 0; u < ctx->dflts.count; ++u) {
186 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
187 if (r->dflt == dflt) {
188 free(ctx->dflts.objs[u]);
189 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
190 --ctx->dflts.count;
191 return;
192 }
193 }
194}
195
Radek Krejci0e59c312019-08-15 15:34:15 +0200196void
Radek Krejci327de162019-06-14 12:52:07 +0200197lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
198{
199 int len;
200 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
201
202 if (!name) {
203 /* removing last path segment */
204 if (ctx->path[ctx->path_len - 1] == '}') {
205 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
206 if (ctx->path[ctx->path_len] == '=') {
207 ctx->path[ctx->path_len++] = '}';
208 } else {
209 /* not a top-level special tag, remove also preceiding '/' */
210 goto remove_nodelevel;
211 }
212 } else {
213remove_nodelevel:
214 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
215 if (ctx->path_len == 0) {
216 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200217 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200218 }
219 }
220 /* set new terminating NULL-byte */
221 ctx->path[ctx->path_len] = '\0';
222 } else {
223 if (ctx->path_len > 1) {
224 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
225 /* extension of the special tag */
226 nextlevel = 2;
227 --ctx->path_len;
228 } else {
229 /* there is already some path, so add next level */
230 nextlevel = 1;
231 }
232 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
233
234 if (nextlevel != 2) {
235 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
236 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200237 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200238 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200239 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 +0200240 }
241 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200242 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200243 }
Radek Krejciacc79042019-07-25 14:14:57 +0200244 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
245 /* output truncated */
246 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
247 } else {
248 ctx->path_len += len;
249 }
Radek Krejci327de162019-06-14 12:52:07 +0200250 }
251}
252
253/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100254 * @brief Duplicate the compiled pattern structure.
255 *
256 * Instead of duplicating memory, the reference counter in the @p orig is increased.
257 *
258 * @param[in] orig The pattern structure to duplicate.
259 * @return The duplicated structure to use.
260 */
Radek Krejci19a96102018-11-15 13:38:09 +0100261static struct lysc_pattern*
262lysc_pattern_dup(struct lysc_pattern *orig)
263{
264 ++orig->refcount;
265 return orig;
266}
267
Radek Krejcib56c7502019-02-13 14:19:54 +0100268/**
269 * @brief Duplicate the array of compiled patterns.
270 *
271 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
272 *
273 * @param[in] ctx Libyang context for logging.
274 * @param[in] orig The patterns sized array to duplicate.
275 * @return New sized array as a copy of @p orig.
276 * @return NULL in case of memory allocation error.
277 */
Radek Krejci19a96102018-11-15 13:38:09 +0100278static struct lysc_pattern**
279lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
280{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100281 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200282 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100283
Radek Krejcib56c7502019-02-13 14:19:54 +0100284 assert(orig);
285
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200286 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100287 LY_ARRAY_FOR(orig, u) {
288 dup[u] = lysc_pattern_dup(orig[u]);
289 LY_ARRAY_INCREMENT(dup);
290 }
291 return dup;
292}
293
Radek Krejcib56c7502019-02-13 14:19:54 +0100294/**
295 * @brief Duplicate compiled range structure.
296 *
297 * @param[in] ctx Libyang context for logging.
298 * @param[in] orig The range structure to be duplicated.
299 * @return New compiled range structure as a copy of @p orig.
300 * @return NULL in case of memory allocation error.
301 */
Radek Krejci19a96102018-11-15 13:38:09 +0100302struct lysc_range*
303lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
304{
305 struct lysc_range *dup;
306 LY_ERR ret;
307
Radek Krejcib56c7502019-02-13 14:19:54 +0100308 assert(orig);
309
Radek Krejci19a96102018-11-15 13:38:09 +0100310 dup = calloc(1, sizeof *dup);
311 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
312 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200313 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
314 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
315 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100316 }
317 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
318 DUP_STRING(ctx, orig->emsg, dup->emsg);
319 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
320
321 return dup;
322cleanup:
323 free(dup);
324 (void) ret; /* set but not used due to the return type */
325 return NULL;
326}
327
Radek Krejcib56c7502019-02-13 14:19:54 +0100328/**
329 * @brief Stack for processing if-feature expressions.
330 */
Radek Krejci19a96102018-11-15 13:38:09 +0100331struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100332 int size; /**< number of items in the stack */
333 int index; /**< first empty item */
334 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100335};
336
Radek Krejcib56c7502019-02-13 14:19:54 +0100337/**
338 * @brief Add @ref ifftokens into the stack.
339 * @param[in] stack The if-feature stack to use.
340 * @param[in] value One of the @ref ifftokens to store in the stack.
341 * @return LY_EMEM in case of memory allocation error
342 * @return LY_ESUCCESS if the value successfully stored.
343 */
Radek Krejci19a96102018-11-15 13:38:09 +0100344static LY_ERR
345iff_stack_push(struct iff_stack *stack, uint8_t value)
346{
347 if (stack->index == stack->size) {
348 stack->size += 4;
349 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
350 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
351 }
352 stack->stack[stack->index++] = value;
353 return LY_SUCCESS;
354}
355
Radek Krejcib56c7502019-02-13 14:19:54 +0100356/**
357 * @brief Get (and remove) the last item form the stack.
358 * @param[in] stack The if-feature stack to use.
359 * @return The value from the top of the stack.
360 */
Radek Krejci19a96102018-11-15 13:38:09 +0100361static uint8_t
362iff_stack_pop(struct iff_stack *stack)
363{
Radek Krejcib56c7502019-02-13 14:19:54 +0100364 assert(stack && stack->index);
365
Radek Krejci19a96102018-11-15 13:38:09 +0100366 stack->index--;
367 return stack->stack[stack->index];
368}
369
Radek Krejcib56c7502019-02-13 14:19:54 +0100370/**
371 * @brief Clean up the stack.
372 * @param[in] stack The if-feature stack to use.
373 */
Radek Krejci19a96102018-11-15 13:38:09 +0100374static void
375iff_stack_clean(struct iff_stack *stack)
376{
377 stack->size = 0;
378 free(stack->stack);
379}
380
Radek Krejcib56c7502019-02-13 14:19:54 +0100381/**
382 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
383 * (libyang format of the if-feature expression).
384 * @param[in,out] list The 2bits array to modify.
385 * @param[in] op The operand (@ref ifftokens) to store.
386 * @param[in] pos Position (0-based) where to store the given @p op.
387 */
Radek Krejci19a96102018-11-15 13:38:09 +0100388static void
389iff_setop(uint8_t *list, uint8_t op, int pos)
390{
391 uint8_t *item;
392 uint8_t mask = 3;
393
394 assert(pos >= 0);
395 assert(op <= 3); /* max 2 bits */
396
397 item = &list[pos / 4];
398 mask = mask << 2 * (pos % 4);
399 *item = (*item) & ~mask;
400 *item = (*item) | (op << 2 * (pos % 4));
401}
402
Radek Krejcib56c7502019-02-13 14:19:54 +0100403#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
404#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100405
Radek Krejci0af46292019-01-11 16:02:31 +0100406/**
407 * @brief Find a feature of the given name and referenced in the given module.
408 *
409 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
410 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
411 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
412 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
413 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
414 * assigned till that time will be still valid.
415 *
416 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
417 * @param[in] name Name of the feature including possible prefix.
418 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
419 * @return Pointer to the feature structure if found, NULL otherwise.
420 */
Radek Krejci19a96102018-11-15 13:38:09 +0100421static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100422lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100423{
424 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200425 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0af46292019-01-11 16:02:31 +0100426 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100427
Radek Krejci120d8542020-08-12 09:29:16 +0200428 assert(mod);
429
Radek Krejci19a96102018-11-15 13:38:09 +0100430 for (i = 0; i < len; ++i) {
431 if (name[i] == ':') {
432 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100433 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100434 LY_CHECK_RET(!mod, NULL);
435
436 name = &name[i + 1];
437 len = len - i - 1;
438 }
439 }
440
441 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100442 if (mod->implemented) {
443 /* module is implemented so there is already the compiled schema */
444 flist = mod->compiled->features;
445 } else {
Michal Vasko33ff9422020-07-03 09:50:39 +0200446 flist = mod->dis_features;
Radek Krejci0af46292019-01-11 16:02:31 +0100447 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200448 LY_ARRAY_FOR(flist, u) {
449 f = &flist[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200450 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100451 return f;
452 }
453 }
454
455 return NULL;
456}
457
Michal Vasko8d544252020-03-02 10:19:52 +0100458/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100459 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
460 */
461static LY_ERR
462lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
463{
464 LY_ERR ret = LY_SUCCESS;
465
466 if (!ext_p->compiled) {
467 lysc_update_path(ctx, NULL, "{extension}");
468 lysc_update_path(ctx, NULL, ext_p->name);
469
470 /* compile the extension definition */
471 ext_p->compiled = calloc(1, sizeof **ext);
472 ext_p->compiled->refcount = 1;
473 DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name);
474 DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument);
475 ext_p->compiled->module = (struct lys_module *)ext_mod;
476 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
477
478 lysc_update_path(ctx, NULL, NULL);
479 lysc_update_path(ctx, NULL, NULL);
480
481 /* find extension definition plugin */
482 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
483 }
484
485 *ext = lysc_ext_dup(ext_p->compiled);
486
487done:
488 return ret;
489}
490
491/**
Michal Vasko8d544252020-03-02 10:19:52 +0100492 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
493 *
494 * @param[in] ctx Compilation context.
495 * @param[in] ext_p Parsed extension instance.
496 * @param[in,out] ext Prepared compiled extension instance.
497 * @param[in] parent Extension instance parent.
498 * @param[in] parent_type Extension instance parent type.
499 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
500 */
Radek Krejci19a96102018-11-15 13:38:09 +0100501static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100502lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
503 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100504{
Radek Krejci7c960162019-09-18 14:16:12 +0200505 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100506 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200507 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200508 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200509 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100510
511 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
512 ext->insubstmt = ext_p->insubstmt;
513 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200514 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200515 ext->parent = parent;
516 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100517
Radek Krejcif56e2a42019-09-09 14:15:25 +0200518 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200519
Radek Krejci19a96102018-11-15 13:38:09 +0100520 /* get module where the extension definition should be placed */
Radek Krejci7c960162019-09-18 14:16:12 +0200521 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u);
522 if (ext_p->yin) {
523 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
524 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
525 * YANG (import) prefix must be done here. */
526 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
527 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
528 u = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200529 } else {
530 assert(ctx->mod_def->parsed);
531 LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) {
532 if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) {
Radek Krejci7c960162019-09-18 14:16:12 +0200533 char *s;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200534 LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->parsed->imports[v].prefix, &ext_p->name[u]) == -1,
Radek Krejci200f1062020-07-11 22:51:03 +0200535 ret = LY_EMEM, cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200536 prefixed_name = lydict_insert_zc(ctx->ctx, s);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200537 u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */
Radek Krejci7c960162019-09-18 14:16:12 +0200538 break;
539 }
540 }
541 }
542 if (!prefixed_name) {
543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
544 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
545 goto cleanup;
546 }
547 } else {
548 prefixed_name = ext_p->name;
549 }
550 lysc_update_path(ctx, NULL, prefixed_name);
551
Michal Vasko8d544252020-03-02 10:19:52 +0100552 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200553 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100554 if (!ext_mod) {
555 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
556 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
557 goto cleanup;
558 } else if (!ext_mod->parsed->extensions) {
559 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
560 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
561 prefixed_name, ext_mod->name);
562 goto cleanup;
563 }
Radek Krejci7c960162019-09-18 14:16:12 +0200564 }
565 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200566
Michal Vasko5fe75f12020-03-02 13:52:37 +0100567 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200568 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
569 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100570 /* compile extension definition and assign it */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200571 LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def));
Radek Krejci19a96102018-11-15 13:38:09 +0100572 break;
573 }
574 }
Radek Krejci7c960162019-09-18 14:16:12 +0200575 if (!ext->def) {
576 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
577 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
578 goto cleanup;
579 }
Radek Krejci0935f412019-08-20 16:15:18 +0200580
Radek Krejcif56e2a42019-09-09 14:15:25 +0200581 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
582 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
583 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200584 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200585 /* Schema was parsed from YIN and an argument is expected, ... */
586 struct lysp_stmt *stmt = NULL;
587
588 if (ext->def->flags & LYS_YINELEM_TRUE) {
589 /* ... argument was the first XML child element */
590 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
591 /* TODO check namespace of the statement */
592 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
593 stmt = ext_p->child;
594 }
595 }
596 } else {
597 /* ... argument was one of the XML attributes which are represented as child stmt
598 * with LYS_YIN_ATTR flag */
599 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
600 if (!strcmp(stmt->stmt, ext->def->argument)) {
601 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200602 break;
603 }
604 }
605 }
606 if (!stmt) {
607 /* missing extension's argument */
608 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200609 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
610 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200611
612 }
613 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
614 stmt->flags |= LYS_YIN_ARGUMENT;
615 }
Radek Krejci7c960162019-09-18 14:16:12 +0200616 if (prefixed_name != ext_p->name) {
617 lydict_remove(ctx->ctx, ext_p->name);
618 ext_p->name = prefixed_name;
619 if (!ext_p->argument && ext->argument) {
620 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
621 }
622 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200623
Radek Krejci0935f412019-08-20 16:15:18 +0200624 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200625 if (ext->argument) {
626 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
627 }
Radek Krejci7c960162019-09-18 14:16:12 +0200628 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200629 if (ext->argument) {
630 lysc_update_path(ctx, NULL, NULL);
631 }
Radek Krejci0935f412019-08-20 16:15:18 +0200632 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200633 ext_p->compiled = ext;
634
Radek Krejci7c960162019-09-18 14:16:12 +0200635 ret = LY_SUCCESS;
636cleanup:
637 if (prefixed_name && prefixed_name != ext_p->name) {
638 lydict_remove(ctx->ctx, prefixed_name);
639 }
640
Radek Krejcif56e2a42019-09-09 14:15:25 +0200641 lysc_update_path(ctx, NULL, NULL);
642 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200643
Radek Krejci7c960162019-09-18 14:16:12 +0200644 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200645}
646
647/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100648 * @brief Compile information from the if-feature statement
649 * @param[in] ctx Compile context.
650 * @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 +0100651 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
652 * @return LY_ERR value.
653 */
Radek Krejci19a96102018-11-15 13:38:09 +0100654static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200655lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100656{
657 const char *c = *value;
658 int r, rc = EXIT_FAILURE;
659 int i, j, last_not, checkversion = 0;
660 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
661 uint8_t op;
662 struct iff_stack stack = {0, 0, NULL};
663 struct lysc_feature *f;
664
665 assert(c);
666
667 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
668 for (i = j = last_not = 0; c[i]; i++) {
669 if (c[i] == '(') {
670 j++;
671 checkversion = 1;
672 continue;
673 } else if (c[i] == ')') {
674 j--;
675 continue;
676 } else if (isspace(c[i])) {
677 checkversion = 1;
678 continue;
679 }
680
681 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 +0200682 int sp;
683 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
684 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100685 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
686 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
687 return LY_EVALID;
688 } else if (!isspace(c[i + r])) {
689 /* feature name starting with the not/and/or */
690 last_not = 0;
691 f_size++;
692 } else if (c[i] == 'n') { /* not operation */
693 if (last_not) {
694 /* double not */
695 expr_size = expr_size - 2;
696 last_not = 0;
697 } else {
698 last_not = 1;
699 }
700 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200701 if (f_exp != f_size) {
702 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
703 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
704 return LY_EVALID;
705 }
Radek Krejci19a96102018-11-15 13:38:09 +0100706 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200707
Radek Krejci19a96102018-11-15 13:38:09 +0100708 /* not a not operation */
709 last_not = 0;
710 }
711 i += r;
712 } else {
713 f_size++;
714 last_not = 0;
715 }
716 expr_size++;
717
718 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200719 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100720 i--;
721 break;
722 }
723 i++;
724 }
725 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200726 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100727 /* not matching count of ( and ) */
728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
729 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
730 return LY_EVALID;
731 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200732 if (f_exp != f_size) {
733 /* features do not match the needed arguments for the logical operations */
734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
735 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
736 "the required number of operands for the operations.", *value);
737 return LY_EVALID;
738 }
Radek Krejci19a96102018-11-15 13:38:09 +0100739
740 if (checkversion || expr_size > 1) {
741 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100742 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
744 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
745 return LY_EVALID;
746 }
747 }
748
749 /* allocate the memory */
750 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
751 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
752 stack.stack = malloc(expr_size * sizeof *stack.stack);
753 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
754
755 stack.size = expr_size;
756 f_size--; expr_size--; /* used as indexes from now */
757
758 for (i--; i >= 0; i--) {
759 if (c[i] == ')') {
760 /* push it on stack */
761 iff_stack_push(&stack, LYS_IFF_RP);
762 continue;
763 } else if (c[i] == '(') {
764 /* pop from the stack into result all operators until ) */
765 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
766 iff_setop(iff->expr, op, expr_size--);
767 }
768 continue;
769 } else if (isspace(c[i])) {
770 continue;
771 }
772
773 /* end of operator or operand -> find beginning and get what is it */
774 j = i + 1;
775 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
776 i--;
777 }
778 i++; /* go back by one step */
779
780 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
781 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
782 /* double not */
783 iff_stack_pop(&stack);
784 } else {
785 /* not has the highest priority, so do not pop from the stack
786 * as in case of AND and OR */
787 iff_stack_push(&stack, LYS_IFF_NOT);
788 }
789 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
790 /* as for OR - pop from the stack all operators with the same or higher
791 * priority and store them to the result, then push the AND to the stack */
792 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
793 op = iff_stack_pop(&stack);
794 iff_setop(iff->expr, op, expr_size--);
795 }
796 iff_stack_push(&stack, LYS_IFF_AND);
797 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
798 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
799 op = iff_stack_pop(&stack);
800 iff_setop(iff->expr, op, expr_size--);
801 }
802 iff_stack_push(&stack, LYS_IFF_OR);
803 } else {
804 /* feature name, length is j - i */
805
806 /* add it to the expression */
807 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
808
809 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100810 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
811 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
812 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
813 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100814 iff->features[f_size] = f;
815 LY_ARRAY_INCREMENT(iff->features);
816 f_size--;
817 }
818 }
819 while (stack.index) {
820 op = iff_stack_pop(&stack);
821 iff_setop(iff->expr, op, expr_size--);
822 }
823
824 if (++expr_size || ++f_size) {
825 /* not all expected operators and operands found */
826 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
827 "Invalid value \"%s\" of if-feature - processing error.", *value);
828 rc = LY_EINT;
829 } else {
830 rc = LY_SUCCESS;
831 }
832
833error:
834 /* cleanup */
835 iff_stack_clean(&stack);
836
837 return rc;
838}
839
Radek Krejcib56c7502019-02-13 14:19:54 +0100840/**
Michal Vasko175012e2019-11-06 15:49:14 +0100841 * @brief Get the XPath context node for the given schema node.
842 * @param[in] start The schema node where the XPath expression appears.
843 * @return The context node to evaluate XPath expression in given schema node.
844 * @return NULL in case the context node is the root node.
845 */
846static struct lysc_node *
847lysc_xpath_context(struct lysc_node *start)
848{
Michal Vasko1bf09392020-03-27 12:38:10 +0100849 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 +0100850 start = start->parent);
851 return start;
852}
853
854/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100855 * @brief Compile information from the when statement
856 * @param[in] ctx Compile context.
857 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100858 * @param[in] flags Flags of the node with the "when" defiition.
859 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100860 * @param[out] when Pointer where to store pointer to the created compiled when structure.
861 * @return LY_ERR value.
862 */
Radek Krejci19a96102018-11-15 13:38:09 +0100863static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100864lys_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 +0100865{
Radek Krejci19a96102018-11-15 13:38:09 +0100866 LY_ERR ret = LY_SUCCESS;
867
Radek Krejci00b874b2019-02-12 10:54:50 +0100868 *when = calloc(1, sizeof **when);
869 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200870 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200871 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100872 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100873 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
874 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
875 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200876 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100877 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100878
879done:
880 return ret;
881}
882
Radek Krejcib56c7502019-02-13 14:19:54 +0100883/**
884 * @brief Compile information from the must statement
885 * @param[in] ctx Compile context.
886 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100887 * @param[in,out] must Prepared (empty) compiled must structure to fill.
888 * @return LY_ERR value.
889 */
Radek Krejci19a96102018-11-15 13:38:09 +0100890static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200891lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100892{
Radek Krejci19a96102018-11-15 13:38:09 +0100893 LY_ERR ret = LY_SUCCESS;
894
Michal Vasko004d3152020-06-11 19:59:22 +0200895 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100896 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200897 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100898 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
899 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100900 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
901 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200902 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100903
904done:
905 return ret;
906}
907
Radek Krejcib56c7502019-02-13 14:19:54 +0100908/**
Michal Vasko7c8439f2020-08-05 13:25:19 +0200909 * @brief Compile information in the import statement - make sure there is the target module
Radek Krejcib56c7502019-02-13 14:19:54 +0100910 * @param[in] ctx Compile context.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200911 * @param[in] imp_p The parsed import statement structure to fill the module to.
Radek Krejcib56c7502019-02-13 14:19:54 +0100912 * @return LY_ERR value.
913 */
Radek Krejci19a96102018-11-15 13:38:09 +0100914static LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200915lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
Radek Krejci19a96102018-11-15 13:38:09 +0100916{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200917 const struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100918 LY_ERR ret = LY_SUCCESS;
919
Radek Krejci7f2a5362018-11-28 13:05:37 +0100920 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
921 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100922 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200923 if (!imp_p->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100924 /* try to use filepath if present */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200925 if (imp_p->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200926 struct ly_in *in;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200927 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200928 LOGINT(ctx->ctx);
929 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200930 LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)],
Michal Vasko3a41dff2020-07-15 14:30:28 +0200931 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200932 if (mod != imp_p->module) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200933 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200934 imp_p->module->filepath, imp_p->module->name);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200935 mod = NULL;
936 }
Radek Krejci19a96102018-11-15 13:38:09 +0100937 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200938 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100939 }
940 if (!mod) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200941 if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, (struct lys_module **)&mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100942 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Michal Vasko7c8439f2020-08-05 13:25:19 +0200943 imp_p->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100944 return LY_ENOTFOUND;
945 }
946 }
Radek Krejci19a96102018-11-15 13:38:09 +0100947 }
948
Radek Krejci19a96102018-11-15 13:38:09 +0100949 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;
Radek Krejci120d8542020-08-12 09:29:16 +0200965 context.mod_def = module;
Michal Vasko33ff9422020-07-03 09:50:39 +0200966 context.path_len = 1;
967 context.path[0] = '/';
968 ctx_sc = &context;
969 }
Radek Krejci19a96102018-11-15 13:38:09 +0100970
Michal Vasko33ff9422020-07-03 09:50:39 +0200971 if (!identities_p) {
972 return LY_SUCCESS;
973 }
974 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200975 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +0200976 }
977
978 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200979 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +0200980 LY_ARRAY_FOR(identities_p, u) {
981 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
982
983 LY_ARRAY_INCREMENT(*identities);
984 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
985 DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name);
986 DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc);
987 DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref);
988 (*identities)[offset + u].module = ctx_sc->mod;
989 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
990 lys_compile_iffeature, ret, done);
991 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
992 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
993 LYEXT_PAR_IDENT, ret, done);
994 (*identities)[offset + u].flags = identities_p[u].flags;
995
996 lysc_update_path(ctx_sc, NULL, NULL);
997 }
998 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100999done:
1000 return ret;
1001}
1002
Radek Krejcib56c7502019-02-13 14:19:54 +01001003/**
1004 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1005 *
1006 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1007 *
1008 * @param[in] ctx Compile context for logging.
1009 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1010 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1011 * @return LY_SUCCESS if everything is ok.
1012 * @return LY_EVALID if the identity is derived from itself.
1013 */
Radek Krejci38222632019-02-12 16:55:05 +01001014static LY_ERR
1015lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1016{
1017 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001018 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001019 struct ly_set recursion = {0};
1020 struct lysc_ident *drv;
1021
1022 if (!derived) {
1023 return LY_SUCCESS;
1024 }
1025
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001026 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001027 if (ident == derived[u]) {
1028 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1029 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1030 goto cleanup;
1031 }
1032 ly_set_add(&recursion, derived[u], 0);
1033 }
1034
1035 for (v = 0; v < recursion.count; ++v) {
1036 drv = recursion.objs[v];
1037 if (!drv->derived) {
1038 continue;
1039 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001040 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001041 if (ident == drv->derived[u]) {
1042 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1043 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1044 goto cleanup;
1045 }
1046 ly_set_add(&recursion, drv->derived[u], 0);
1047 }
1048 }
1049 ret = LY_SUCCESS;
1050
1051cleanup:
1052 ly_set_erase(&recursion, NULL);
1053 return ret;
1054}
1055
Radek Krejcia3045382018-11-22 14:30:31 +01001056/**
1057 * @brief Find and process the referenced base identities from another identity or identityref
1058 *
Radek Krejciaca74032019-06-04 08:53:06 +02001059 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001060 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1061 * to distinguish these two use cases.
1062 *
1063 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1064 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001065 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001066 * @param[in] bases Array of bases of identityref to fill in.
1067 * @return LY_ERR value.
1068 */
Radek Krejci19a96102018-11-15 13:38:09 +01001069static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001070lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
1071 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001072{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001073 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001074 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001075 struct lys_module *mod;
Michal Vasko33ff9422020-07-03 09:50:39 +02001076 struct lysc_ident **idref, *identities;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001077
1078 assert(ident || bases);
1079
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001080 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001081 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1082 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1083 return LY_EVALID;
1084 }
1085
Michal Vasko33ff9422020-07-03 09:50:39 +02001086 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001087 s = strchr(bases_p[u], ':');
1088 if (s) {
1089 /* prefixed identity */
1090 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001091 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001092 } else {
1093 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001094 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001095 }
1096 if (!mod) {
1097 if (ident) {
1098 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1099 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1100 } else {
1101 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1102 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1103 }
1104 return LY_EVALID;
1105 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001106
Radek Krejci555cb5b2018-11-16 14:54:33 +01001107 idref = NULL;
Michal Vasko33ff9422020-07-03 09:50:39 +02001108 if (mod->compiled) {
1109 identities = mod->compiled->identities;
1110 } else {
1111 identities = mod->dis_identities;
1112 }
1113 LY_ARRAY_FOR(identities, v) {
1114 if (!strcmp(name, identities[v].name)) {
1115 if (ident) {
1116 if (ident == &identities[v]) {
1117 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1118 "Identity \"%s\" is derived from itself.", ident->name);
1119 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001120 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001121 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived));
1122 /* we have match! store the backlink */
1123 LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM);
1124 *idref = ident;
1125 } else {
1126 /* we have match! store the found identity */
1127 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
1128 *idref = &identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001129 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001130 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001131 }
1132 }
1133 if (!idref || !(*idref)) {
1134 if (ident) {
1135 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1136 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1137 } else {
1138 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1139 "Unable to find base (%s) of identityref.", bases_p[u]);
1140 }
1141 return LY_EVALID;
1142 }
1143 }
1144 return LY_SUCCESS;
1145}
1146
Radek Krejcia3045382018-11-22 14:30:31 +01001147/**
1148 * @brief For the given array of identities, set the backlinks from all their base identities.
1149 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1150 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1151 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1152 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1153 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001154static LY_ERR
1155lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1156{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001157 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001158
Michal Vasko33ff9422020-07-03 09:50:39 +02001159 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001160 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001161 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001162 continue;
1163 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001164 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001165 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 +02001166 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001167 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001168 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001169 return LY_SUCCESS;
1170}
1171
Radek Krejci0af46292019-01-11 16:02:31 +01001172LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001173lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
1174 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001175{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001176 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001177 struct lysc_ctx context = {0};
1178
Radek Krejci327de162019-06-14 12:52:07 +02001179 assert(ctx_sc || ctx);
1180
1181 if (!ctx_sc) {
1182 context.ctx = ctx;
1183 context.mod = module;
1184 context.path_len = 1;
1185 context.path[0] = '/';
1186 ctx_sc = &context;
1187 }
Radek Krejci0af46292019-01-11 16:02:31 +01001188
1189 if (!features_p) {
1190 return LY_SUCCESS;
1191 }
1192 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001193 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001194 }
1195
Radek Krejci327de162019-06-14 12:52:07 +02001196 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001197 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001198 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001199 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1200
Radek Krejci0af46292019-01-11 16:02:31 +01001201 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001202 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci327de162019-06-14 12:52:07 +02001203 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1204 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1205 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001206 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001207 (*features)[offset + u].module = ctx_sc->mod;
1208
1209 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001210 }
Radek Krejci327de162019-06-14 12:52:07 +02001211 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001212
1213 return LY_SUCCESS;
1214}
1215
Radek Krejcia3045382018-11-22 14:30:31 +01001216/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001217 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001218 *
1219 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1220 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001221 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001222 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1223 * being currently processed).
1224 * @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 +01001225 * @return LY_SUCCESS if everything is ok.
1226 * @return LY_EVALID if the feature references indirectly itself.
1227 */
1228static LY_ERR
1229lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1230{
1231 LY_ERR ret = LY_EVALID;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001232 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001233 struct ly_set recursion = {0};
1234 struct lysc_feature *drv;
1235
1236 if (!depfeatures) {
1237 return LY_SUCCESS;
1238 }
1239
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001240 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001241 if (feature == depfeatures[u]) {
1242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1243 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1244 goto cleanup;
1245 }
1246 ly_set_add(&recursion, depfeatures[u], 0);
1247 }
1248
1249 for (v = 0; v < recursion.count; ++v) {
1250 drv = recursion.objs[v];
1251 if (!drv->depfeatures) {
1252 continue;
1253 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001254 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001255 if (feature == drv->depfeatures[u]) {
1256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1257 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1258 goto cleanup;
1259 }
1260 ly_set_add(&recursion, drv->depfeatures[u], 0);
1261 }
1262 }
1263 ret = LY_SUCCESS;
1264
1265cleanup:
1266 ly_set_erase(&recursion, NULL);
1267 return ret;
1268}
1269
1270/**
Radek Krejci0af46292019-01-11 16:02:31 +01001271 * @brief Create pre-compiled features array.
1272 *
1273 * See lys_feature_precompile() for more details.
1274 *
Radek Krejcia3045382018-11-22 14:30:31 +01001275 * @param[in] ctx Compile context.
1276 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001277 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001278 * @return LY_ERR value.
1279 */
Radek Krejci19a96102018-11-15 13:38:09 +01001280static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001281lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001282{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001283 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001284 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001285 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001286
Radek Krejci327de162019-06-14 12:52:07 +02001287
Radek Krejci0af46292019-01-11 16:02:31 +01001288 /* find the preprecompiled feature */
1289 LY_ARRAY_FOR(features, x) {
1290 if (strcmp(features[x].name, feature_p->name)) {
1291 continue;
1292 }
1293 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001294 lysc_update_path(ctx, NULL, "{feature}");
1295 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001296
Radek Krejci0af46292019-01-11 16:02:31 +01001297 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001298 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001299 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001300 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001301 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001302 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001303 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001304 /* check for circular dependency - direct reference first,... */
1305 if (feature == feature->iffeatures[u].features[v]) {
1306 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1307 "Feature \"%s\" is referenced from itself.", feature->name);
1308 return LY_EVALID;
1309 }
1310 /* ... and indirect circular reference */
1311 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1312
Radek Krejci0af46292019-01-11 16:02:31 +01001313 /* add itself into the dependants list */
1314 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1315 *df = feature;
1316 }
Radek Krejci19a96102018-11-15 13:38:09 +01001317 }
Radek Krejci19a96102018-11-15 13:38:09 +01001318 }
1319 }
Radek Krejci327de162019-06-14 12:52:07 +02001320 lysc_update_path(ctx, NULL, NULL);
1321 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001322 done:
1323 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001324 }
Radek Krejci0af46292019-01-11 16:02:31 +01001325
1326 LOGINT(ctx->ctx);
1327 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001328}
1329
Radek Krejcib56c7502019-02-13 14:19:54 +01001330/**
1331 * @brief Revert compiled list of features back to the precompiled state.
1332 *
1333 * 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 +02001334 * The features are supposed to be stored again as dis_features in ::lys_module structure.
Radek Krejcib56c7502019-02-13 14:19:54 +01001335 *
1336 * @param[in] ctx Compilation context.
1337 * @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 +02001338 * and supposed to hold the dis_features list.
Radek Krejcib56c7502019-02-13 14:19:54 +01001339 */
Radek Krejci95710c92019-02-11 15:49:55 +01001340static void
1341lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1342{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001343 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001344
Radek Krejcia46012b2020-08-12 15:41:04 +02001345 if (mod->compiled) {
1346 /* keep the dis_features list until the complete lys_module is freed */
1347 mod->dis_features = mod->compiled->features;
1348 mod->compiled->features = NULL;
1349 }
Radek Krejci95710c92019-02-11 15:49:55 +01001350
Michal Vasko33ff9422020-07-03 09:50:39 +02001351 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001352 * which may points into the data being freed here */
Michal Vasko33ff9422020-07-03 09:50:39 +02001353 LY_ARRAY_FOR(mod->dis_features, u) {
1354 LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) {
1355 lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001356 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001357 LY_ARRAY_FREE(mod->dis_features[u].iffeatures);
1358 mod->dis_features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001359
Michal Vasko33ff9422020-07-03 09:50:39 +02001360 LY_ARRAY_FOR(mod->dis_features[u].exts, v) {
1361 lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001362 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001363 LY_ARRAY_FREE(mod->dis_features[u].exts);
1364 mod->dis_features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001365 }
1366}
1367
Radek Krejcia3045382018-11-22 14:30:31 +01001368/**
1369 * @brief Validate and normalize numeric value from a range definition.
1370 * @param[in] ctx Compile context.
1371 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1372 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1373 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1374 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1375 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1376 * @param[in] value String value of the range boundary.
1377 * @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.
1378 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1379 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1380 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001381LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001382range_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 +01001383{
Radek Krejci6cba4292018-11-15 17:33:29 +01001384 size_t fraction = 0, size;
1385
Radek Krejci19a96102018-11-15 13:38:09 +01001386 *len = 0;
1387
1388 assert(value);
1389 /* parse value */
1390 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1391 return LY_EVALID;
1392 }
1393
1394 if ((value[*len] == '-') || (value[*len] == '+')) {
1395 ++(*len);
1396 }
1397
1398 while (isdigit(value[*len])) {
1399 ++(*len);
1400 }
1401
1402 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001403 if (basetype == LY_TYPE_DEC64) {
1404 goto decimal;
1405 } else {
1406 *valcopy = strndup(value, *len);
1407 return LY_SUCCESS;
1408 }
Radek Krejci19a96102018-11-15 13:38:09 +01001409 }
1410 fraction = *len;
1411
1412 ++(*len);
1413 while (isdigit(value[*len])) {
1414 ++(*len);
1415 }
1416
Radek Krejci6cba4292018-11-15 17:33:29 +01001417 if (basetype == LY_TYPE_DEC64) {
1418decimal:
1419 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001420 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001421 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1422 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1423 *len, value, frdigits);
1424 return LY_EINVAL;
1425 }
1426 if (fraction) {
1427 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1428 } else {
1429 size = (*len) + frdigits + 1;
1430 }
1431 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001432 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1433
Radek Krejci6cba4292018-11-15 17:33:29 +01001434 (*valcopy)[size - 1] = '\0';
1435 if (fraction) {
1436 memcpy(&(*valcopy)[0], &value[0], fraction);
1437 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1438 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1439 } else {
1440 memcpy(&(*valcopy)[0], &value[0], *len);
1441 memset(&(*valcopy)[*len], '0', frdigits);
1442 }
Radek Krejci19a96102018-11-15 13:38:09 +01001443 }
1444 return LY_SUCCESS;
1445}
1446
Radek Krejcia3045382018-11-22 14:30:31 +01001447/**
1448 * @brief Check that values in range are in ascendant order.
1449 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001450 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1451 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001452 * @param[in] value Current value to check.
1453 * @param[in] prev_value The last seen value.
1454 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1455 */
Radek Krejci19a96102018-11-15 13:38:09 +01001456static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001457range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001458{
1459 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001460 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001461 return LY_EEXIST;
1462 }
1463 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001464 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001465 return LY_EEXIST;
1466 }
1467 }
1468 return LY_SUCCESS;
1469}
1470
Radek Krejcia3045382018-11-22 14:30:31 +01001471/**
1472 * @brief Set min/max value of the range part.
1473 * @param[in] ctx Compile context.
1474 * @param[in] part Range part structure to fill.
1475 * @param[in] max Flag to distinguish if storing min or max value.
1476 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1477 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1478 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1479 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1480 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001481 * @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 +01001482 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1483 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1484 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1485 * frdigits value), LY_EMEM.
1486 */
Radek Krejci19a96102018-11-15 13:38:09 +01001487static LY_ERR
Michal Vaskoba417ac2020-08-06 14:48:20 +02001488range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype,
1489 int first, int length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001490{
1491 LY_ERR ret = LY_SUCCESS;
1492 char *valcopy = NULL;
1493 size_t len;
1494
1495 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001496 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001497 LY_CHECK_GOTO(ret, finalize);
1498 }
1499 if (!valcopy && base_range) {
1500 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001501 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001502 } else {
1503 part->min_64 = base_range->parts[0].min_64;
1504 }
1505 if (!first) {
1506 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1507 }
1508 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001509 }
1510
1511 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001512 case LY_TYPE_INT8: /* range */
1513 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001514 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 +01001515 } else if (max) {
1516 part->max_64 = INT64_C(127);
1517 } else {
1518 part->min_64 = INT64_C(-128);
1519 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001520 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001521 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001522 }
1523 break;
1524 case LY_TYPE_INT16: /* range */
1525 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001526 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 +01001527 } else if (max) {
1528 part->max_64 = INT64_C(32767);
1529 } else {
1530 part->min_64 = INT64_C(-32768);
1531 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001532 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001533 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001534 }
1535 break;
1536 case LY_TYPE_INT32: /* range */
1537 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001538 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 +01001539 } else if (max) {
1540 part->max_64 = INT64_C(2147483647);
1541 } else {
1542 part->min_64 = INT64_C(-2147483648);
1543 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001544 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001545 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001546 }
1547 break;
1548 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001549 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001550 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001551 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001552 max ? &part->max_64 : &part->min_64);
1553 } else if (max) {
1554 part->max_64 = INT64_C(9223372036854775807);
1555 } else {
1556 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1557 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001558 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001559 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001560 }
1561 break;
1562 case LY_TYPE_UINT8: /* range */
1563 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001564 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 +01001565 } else if (max) {
1566 part->max_u64 = UINT64_C(255);
1567 } else {
1568 part->min_u64 = UINT64_C(0);
1569 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001570 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001571 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001572 }
1573 break;
1574 case LY_TYPE_UINT16: /* range */
1575 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001576 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 +01001577 } else if (max) {
1578 part->max_u64 = UINT64_C(65535);
1579 } else {
1580 part->min_u64 = UINT64_C(0);
1581 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001582 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001583 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001584 }
1585 break;
1586 case LY_TYPE_UINT32: /* range */
1587 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001588 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 +01001589 } else if (max) {
1590 part->max_u64 = UINT64_C(4294967295);
1591 } else {
1592 part->min_u64 = UINT64_C(0);
1593 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001594 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001595 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001596 }
1597 break;
1598 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001599 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001600 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001601 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001602 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 +01001603 } else if (max) {
1604 part->max_u64 = UINT64_C(18446744073709551615);
1605 } else {
1606 part->min_u64 = UINT64_C(0);
1607 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001608 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001609 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001610 }
1611 break;
1612 default:
1613 LOGINT(ctx->ctx);
1614 ret = LY_EINT;
1615 }
1616
Radek Krejci5969f272018-11-23 10:03:58 +01001617finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001618 if (ret == LY_EDENIED) {
1619 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1620 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1621 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1622 } else if (ret == LY_EVALID) {
1623 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1624 "Invalid %s restriction - invalid value \"%s\".",
1625 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1626 } else if (ret == LY_EEXIST) {
1627 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1628 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001629 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001630 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001631 } else if (!ret && value) {
1632 *value = *value + len;
1633 }
1634 free(valcopy);
1635 return ret;
1636}
1637
Radek Krejcia3045382018-11-22 14:30:31 +01001638/**
1639 * @brief Compile the parsed range restriction.
1640 * @param[in] ctx Compile context.
1641 * @param[in] range_p Parsed range structure to compile.
1642 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1643 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1644 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1645 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1646 * range restriction must be more restrictive than the base_range.
1647 * @param[in,out] range Pointer to the created current range structure.
1648 * @return LY_ERR value.
1649 */
Radek Krejci19a96102018-11-15 13:38:09 +01001650static LY_ERR
Michal Vaskoba417ac2020-08-06 14:48:20 +02001651lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr,
1652 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
Radek Krejci19a96102018-11-15 13:38:09 +01001653{
1654 LY_ERR ret = LY_EVALID;
1655 const char *expr;
1656 struct lysc_range_part *parts = NULL, *part;
1657 int range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001658 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001659
1660 assert(range);
1661 assert(range_p);
1662
1663 expr = range_p->arg;
1664 while(1) {
1665 if (isspace(*expr)) {
1666 ++expr;
1667 } else if (*expr == '\0') {
1668 if (range_expected) {
1669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1670 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1671 length_restr ? "length" : "range", range_p->arg);
1672 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001673 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001674 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1675 "Invalid %s restriction - unexpected end of the expression (%s).",
1676 length_restr ? "length" : "range", range_p->arg);
1677 goto cleanup;
1678 }
1679 parts_done++;
1680 break;
1681 } else if (!strncmp(expr, "min", 3)) {
1682 if (parts) {
1683 /* min cannot be used elsewhere than in the first part */
1684 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1685 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1686 expr - range_p->arg, range_p->arg);
1687 goto cleanup;
1688 }
1689 expr += 3;
1690
1691 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001692 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 +01001693 part->max_64 = part->min_64;
1694 } else if (*expr == '|') {
1695 if (!parts || range_expected) {
1696 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1697 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1698 goto cleanup;
1699 }
1700 expr++;
1701 parts_done++;
1702 /* process next part of the expression */
1703 } else if (!strncmp(expr, "..", 2)) {
1704 expr += 2;
1705 while (isspace(*expr)) {
1706 expr++;
1707 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001708 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001709 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1710 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1711 goto cleanup;
1712 }
1713 /* continue expecting the upper boundary */
1714 range_expected = 1;
1715 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1716 /* number */
1717 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001718 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001719 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 +01001720 range_expected = 0;
1721 } else {
1722 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001723 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 +01001724 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001725 part->max_64 = part->min_64;
1726 }
1727
1728 /* continue with possible another expression part */
1729 } else if (!strncmp(expr, "max", 3)) {
1730 expr += 3;
1731 while (isspace(*expr)) {
1732 expr++;
1733 }
1734 if (*expr != '\0') {
1735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1736 length_restr ? "length" : "range", expr);
1737 goto cleanup;
1738 }
1739 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001740 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001741 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 +01001742 range_expected = 0;
1743 } else {
1744 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001745 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 +01001746 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001747 part->min_64 = part->max_64;
1748 }
1749 } else {
1750 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1751 length_restr ? "length" : "range", expr);
1752 goto cleanup;
1753 }
1754 }
1755
1756 /* check with the previous range/length restriction */
1757 if (base_range) {
1758 switch (basetype) {
1759 case LY_TYPE_BINARY:
1760 case LY_TYPE_UINT8:
1761 case LY_TYPE_UINT16:
1762 case LY_TYPE_UINT32:
1763 case LY_TYPE_UINT64:
1764 case LY_TYPE_STRING:
1765 uns = 1;
1766 break;
1767 case LY_TYPE_DEC64:
1768 case LY_TYPE_INT8:
1769 case LY_TYPE_INT16:
1770 case LY_TYPE_INT32:
1771 case LY_TYPE_INT64:
1772 uns = 0;
1773 break;
1774 default:
1775 LOGINT(ctx->ctx);
1776 ret = LY_EINT;
1777 goto cleanup;
1778 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001779 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001780 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1781 goto baseerror;
1782 }
1783 /* current lower bound is not lower than the base */
1784 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1785 /* base has single value */
1786 if (base_range->parts[v].min_64 == parts[u].min_64) {
1787 /* both lower bounds are the same */
1788 if (parts[u].min_64 != parts[u].max_64) {
1789 /* current continues with a range */
1790 goto baseerror;
1791 } else {
1792 /* equal single values, move both forward */
1793 ++v;
1794 continue;
1795 }
1796 } else {
1797 /* base is single value lower than current range, so the
1798 * value from base range is removed in the current,
1799 * move only base and repeat checking */
1800 ++v;
1801 --u;
1802 continue;
1803 }
1804 } else {
1805 /* base is the range */
1806 if (parts[u].min_64 == parts[u].max_64) {
1807 /* current is a single value */
1808 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1809 /* current is behind the base range, so base range is omitted,
1810 * move the base and keep the current for further check */
1811 ++v;
1812 --u;
1813 } /* else it is within the base range, so move the current, but keep the base */
1814 continue;
1815 } else {
1816 /* both are ranges - check the higher bound, the lower was already checked */
1817 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1818 /* higher bound is higher than the current higher bound */
1819 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1820 /* but the current lower bound is also higher, so the base range is omitted,
1821 * continue with the same current, but move the base */
1822 --u;
1823 ++v;
1824 continue;
1825 }
1826 /* current range starts within the base range but end behind it */
1827 goto baseerror;
1828 } else {
1829 /* current range is smaller than the base,
1830 * move current, but stay with the base */
1831 continue;
1832 }
1833 }
1834 }
1835 }
1836 if (u != parts_done) {
1837baseerror:
1838 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1839 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1840 length_restr ? "length" : "range", range_p->arg);
1841 goto cleanup;
1842 }
1843 }
1844
1845 if (!(*range)) {
1846 *range = calloc(1, sizeof **range);
1847 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1848 }
1849
Radek Krejcic8b31002019-01-08 10:24:45 +01001850 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001851 if (range_p->eapptag) {
1852 lydict_remove(ctx->ctx, (*range)->eapptag);
1853 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1854 }
1855 if (range_p->emsg) {
1856 lydict_remove(ctx->ctx, (*range)->emsg);
1857 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1858 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001859 if (range_p->dsc) {
1860 lydict_remove(ctx->ctx, (*range)->dsc);
1861 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1862 }
1863 if (range_p->ref) {
1864 lydict_remove(ctx->ctx, (*range)->ref);
1865 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1866 }
Radek Krejci19a96102018-11-15 13:38:09 +01001867 /* extensions are taken only from the last range by the caller */
1868
1869 (*range)->parts = parts;
1870 parts = NULL;
1871 ret = LY_SUCCESS;
1872cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001873 LY_ARRAY_FREE(parts);
1874
1875 return ret;
1876}
1877
1878/**
1879 * @brief Checks pattern syntax.
1880 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001881 * @param[in] ctx Context.
1882 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001883 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001884 * @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 +01001885 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001886 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001887LY_ERR
1888lys_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 +01001889{
Michal Vasko40a00082020-05-27 15:20:01 +02001890 int idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001891 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001892 int err_code;
1893 const char *orig_ptr;
1894 PCRE2_SIZE err_offset;
1895 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001896#define URANGE_LEN 19
1897 char *ublock2urange[][2] = {
1898 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1899 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1900 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1901 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1902 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1903 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1904 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1905 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1906 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1907 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1908 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1909 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1910 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1911 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1912 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1913 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1914 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1915 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1916 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1917 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1918 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1919 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1920 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1921 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1922 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1923 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1924 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1925 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1926 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1927 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1928 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1929 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1930 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1931 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1932 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1933 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1934 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1935 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1936 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1937 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1938 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1939 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1940 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1941 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1942 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1943 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1944 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1945 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1946 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1947 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1948 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1949 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1950 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1951 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1952 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1953 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1954 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1955 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1956 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1957 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1958 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1959 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1960 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1961 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1962 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1963 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1964 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1965 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1966 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1967 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1968 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1969 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1970 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1971 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1972 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1973 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1974 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1975 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1976 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1977 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1978 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1979 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1980 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1981 {NULL, NULL}
1982 };
1983
1984 /* adjust the expression to a Perl equivalent
1985 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1986
Michal Vasko40a00082020-05-27 15:20:01 +02001987 /* allocate space for the transformed pattern */
1988 size = strlen(pattern) + 1;
1989 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001990 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001991 perl_regex[0] = '\0';
1992
Michal Vasko40a00082020-05-27 15:20:01 +02001993 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1994 brack = 0;
1995 idx = 0;
1996 orig_ptr = pattern;
1997 while (orig_ptr[0]) {
1998 switch (orig_ptr[0]) {
1999 case '$':
2000 case '^':
2001 if (!brack) {
2002 /* make space for the extra character */
2003 ++size;
2004 perl_regex = ly_realloc(perl_regex, size);
2005 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002006
Michal Vasko40a00082020-05-27 15:20:01 +02002007 /* print escape slash */
2008 perl_regex[idx] = '\\';
2009 ++idx;
2010 }
2011 break;
2012 case '[':
2013 /* must not be escaped */
2014 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2015 ++brack;
2016 }
2017 break;
2018 case ']':
2019 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2020 /* pattern was checked and compiled already */
2021 assert(brack);
2022 --brack;
2023 }
2024 break;
2025 default:
2026 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002027 }
Michal Vasko40a00082020-05-27 15:20:01 +02002028
2029 /* copy char */
2030 perl_regex[idx] = orig_ptr[0];
2031
2032 ++idx;
2033 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002034 }
Michal Vasko40a00082020-05-27 15:20:01 +02002035 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002036
2037 /* substitute Unicode Character Blocks with exact Character Ranges */
2038 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2039 start = ptr - perl_regex;
2040
2041 ptr = strchr(ptr, '}');
2042 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002044 pattern, perl_regex + start + 2, "unterminated character property");
2045 free(perl_regex);
2046 return LY_EVALID;
2047 }
2048 end = (ptr - perl_regex) + 1;
2049
2050 /* need more space */
2051 if (end - start < URANGE_LEN) {
2052 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002053 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002054 }
2055
2056 /* find our range */
2057 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2058 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2059 break;
2060 }
2061 }
2062 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002064 pattern, perl_regex + start + 5, "unknown block name");
2065 free(perl_regex);
2066 return LY_EVALID;
2067 }
2068
2069 /* 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 +02002070 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002071 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 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002075 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002076 }
2077 }
Michal Vasko40a00082020-05-27 15:20:01 +02002078 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002079 /* skip brackets */
2080 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2081 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2082 } else {
2083 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2084 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2085 }
2086 }
2087
2088 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002089 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2090 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002091 &err_code, &err_offset, NULL);
2092 if (!code_local) {
2093 PCRE2_UCHAR err_msg[256] = {0};
2094 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002096 free(perl_regex);
2097 return LY_EVALID;
2098 }
2099 free(perl_regex);
2100
Radek Krejci54579462019-04-30 12:47:06 +02002101 if (code) {
2102 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002103 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002104 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002105 }
2106
2107 return LY_SUCCESS;
2108
2109#undef URANGE_LEN
2110}
2111
Radek Krejcia3045382018-11-22 14:30:31 +01002112/**
2113 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2114 * @param[in] ctx Compile context.
2115 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002116 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2117 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2118 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2119 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2120 */
Radek Krejci19a96102018-11-15 13:38:09 +01002121static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002122lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002123 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2124{
2125 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002126 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002127 LY_ERR ret = LY_SUCCESS;
2128
2129 /* first, copy the patterns from the base type */
2130 if (base_patterns) {
2131 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2132 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2133 }
2134
2135 LY_ARRAY_FOR(patterns_p, u) {
2136 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2137 *pattern = calloc(1, sizeof **pattern);
2138 ++(*pattern)->refcount;
2139
Michal Vasko03ff5a72019-09-11 13:49:33 +02002140 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002141 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002142
2143 if (patterns_p[u].arg[0] == 0x15) {
2144 (*pattern)->inverted = 1;
2145 }
Radek Krejci54579462019-04-30 12:47:06 +02002146 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002147 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2148 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002149 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2150 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002151 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002152 }
2153done:
2154 return ret;
2155}
2156
Radek Krejcia3045382018-11-22 14:30:31 +01002157/**
2158 * @brief map of the possible restrictions combination for the specific built-in type.
2159 */
Radek Krejci19a96102018-11-15 13:38:09 +01002160static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2161 0 /* LY_TYPE_UNKNOWN */,
2162 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002163 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2164 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2165 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2166 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2167 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002168 LYS_SET_BIT /* LY_TYPE_BITS */,
2169 0 /* LY_TYPE_BOOL */,
2170 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2171 0 /* LY_TYPE_EMPTY */,
2172 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2173 LYS_SET_BASE /* LY_TYPE_IDENT */,
2174 LYS_SET_REQINST /* LY_TYPE_INST */,
2175 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002176 LYS_SET_TYPE /* LY_TYPE_UNION */,
2177 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002178 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002179 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002180 LYS_SET_RANGE /* LY_TYPE_INT64 */
2181};
2182
2183/**
2184 * @brief stringification of the YANG built-in data types
2185 */
2186const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2187 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2188 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002189};
2190
Radek Krejcia3045382018-11-22 14:30:31 +01002191/**
2192 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2193 * @param[in] ctx Compile context.
2194 * @param[in] enums_p Array of the parsed enum structures to compile.
2195 * @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 +01002196 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2197 * @param[out] enums Newly created array of the compiled enums information for the current type.
2198 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2199 */
Radek Krejci19a96102018-11-15 13:38:09 +01002200static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002201lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002202 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002203{
2204 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002205 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002206 int32_t value = 0;
2207 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002208 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002209
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002210 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002211 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2212 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2213 return LY_EVALID;
2214 }
2215
2216 LY_ARRAY_FOR(enums_p, u) {
2217 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2218 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002219 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2220 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002221 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002222 if (base_enums) {
2223 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2224 LY_ARRAY_FOR(base_enums, v) {
2225 if (!strcmp(e->name, base_enums[v].name)) {
2226 break;
2227 }
2228 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002229 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002230 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2231 "Invalid %s - derived type adds new item \"%s\".",
2232 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2233 return LY_EVALID;
2234 }
2235 match = v;
2236 }
2237
2238 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002239 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002240 if (enums_p[u].flags & LYS_SET_VALUE) {
2241 e->value = (int32_t)enums_p[u].value;
2242 if (!u || e->value >= value) {
2243 value = e->value + 1;
2244 }
2245 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002246 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002247 if (e->value == (*enums)[v].value) {
2248 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2249 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2250 e->value, e->name, (*enums)[v].name);
2251 return LY_EVALID;
2252 }
2253 }
2254 } else if (base_enums) {
2255 /* inherit the assigned value */
2256 e->value = base_enums[match].value;
2257 if (!u || e->value >= value) {
2258 value = e->value + 1;
2259 }
2260 } else {
2261 /* assign value automatically */
2262 if (u && value == INT32_MIN) {
2263 /* counter overflow */
2264 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2265 "Invalid enumeration - it is not possible to auto-assign enum value for "
2266 "\"%s\" since the highest value is already 2147483647.", e->name);
2267 return LY_EVALID;
2268 }
2269 e->value = value++;
2270 }
2271 } else { /* LY_TYPE_BITS */
2272 if (enums_p[u].flags & LYS_SET_VALUE) {
2273 e->value = (int32_t)enums_p[u].value;
2274 if (!u || (uint32_t)e->value >= position) {
2275 position = (uint32_t)e->value + 1;
2276 }
2277 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002278 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002279 if (e->value == (*enums)[v].value) {
2280 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2281 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2282 (uint32_t)e->value, e->name, (*enums)[v].name);
2283 return LY_EVALID;
2284 }
2285 }
2286 } else if (base_enums) {
2287 /* inherit the assigned value */
2288 e->value = base_enums[match].value;
2289 if (!u || (uint32_t)e->value >= position) {
2290 position = (uint32_t)e->value + 1;
2291 }
2292 } else {
2293 /* assign value automatically */
2294 if (u && position == 0) {
2295 /* counter overflow */
2296 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2297 "Invalid bits - it is not possible to auto-assign bit position for "
2298 "\"%s\" since the highest value is already 4294967295.", e->name);
2299 return LY_EVALID;
2300 }
2301 e->value = position++;
2302 }
2303 }
2304
2305 if (base_enums) {
2306 /* the assigned values must not change from the derived type */
2307 if (e->value != base_enums[match].value) {
2308 if (basetype == LY_TYPE_ENUM) {
2309 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2310 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2311 e->name, base_enums[match].value, e->value);
2312 } else {
2313 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2314 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2315 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2316 }
2317 return LY_EVALID;
2318 }
2319 }
2320
Radek Krejciec4da802019-05-02 13:02:41 +02002321 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002322 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 +01002323
2324 if (basetype == LY_TYPE_BITS) {
2325 /* keep bits ordered by position */
2326 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2327 if (v != u) {
2328 memcpy(&storage, e, sizeof *e);
2329 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2330 memcpy(&(*enums)[v], &storage, sizeof storage);
2331 }
2332 }
2333 }
2334
2335done:
2336 return ret;
2337}
2338
Radek Krejcia3045382018-11-22 14:30:31 +01002339/**
2340 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2341 *
2342 * path-arg = absolute-path / relative-path
2343 * absolute-path = 1*("/" (node-identifier *path-predicate))
2344 * relative-path = 1*(".." "/") descendant-path
2345 *
2346 * @param[in,out] path Path to parse.
2347 * @param[out] prefix Prefix of the token, NULL if there is not any.
2348 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2349 * @param[out] name Name of the token.
2350 * @param[out] nam_len Length of the name.
2351 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2352 * must not be changed between consecutive calls. -1 if the
2353 * path is absolute.
2354 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2355 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2356 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002357LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002358lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2359 int *parent_times, int *has_predicate)
2360{
2361 int par_times = 0;
2362
2363 assert(path && *path);
2364 assert(parent_times);
2365 assert(prefix);
2366 assert(prefix_len);
2367 assert(name);
2368 assert(name_len);
2369 assert(has_predicate);
2370
2371 *prefix = NULL;
2372 *prefix_len = 0;
2373 *name = NULL;
2374 *name_len = 0;
2375 *has_predicate = 0;
2376
2377 if (!*parent_times) {
2378 if (!strncmp(*path, "..", 2)) {
2379 *path += 2;
2380 ++par_times;
2381 while (!strncmp(*path, "/..", 3)) {
2382 *path += 3;
2383 ++par_times;
2384 }
2385 }
2386 if (par_times) {
2387 *parent_times = par_times;
2388 } else {
2389 *parent_times = -1;
2390 }
2391 }
2392
2393 if (**path != '/') {
2394 return LY_EINVAL;
2395 }
2396 /* skip '/' */
2397 ++(*path);
2398
2399 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002400 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002401
2402 if ((**path == '/' && (*path)[1]) || !**path) {
2403 /* path continues by another token or this is the last token */
2404 return LY_SUCCESS;
2405 } else if ((*path)[0] != '[') {
2406 /* unexpected character */
2407 return LY_EINVAL;
2408 } else {
2409 /* predicate starting with [ */
2410 *has_predicate = 1;
2411 return LY_SUCCESS;
2412 }
2413}
2414
2415/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002416 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2417 *
2418 * The set of features used for target must be a subset of features used for the leafref.
2419 * This is not a perfect, we should compare the truth tables but it could require too much resources
2420 * and RFC 7950 does not require it explicitely, so we simplify that.
2421 *
2422 * @param[in] refnode The leafref node.
2423 * @param[in] target Tha target node of the leafref.
2424 * @return LY_SUCCESS or LY_EVALID;
2425 */
2426static LY_ERR
2427lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2428{
2429 LY_ERR ret = LY_EVALID;
2430 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002431 LY_ARRAY_COUNT_TYPE u, v, count;
Radek Krejci58d171e2018-11-23 13:50:55 +01002432 struct ly_set features = {0};
2433
2434 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002435 if (iter->iffeatures) {
2436 LY_ARRAY_FOR(iter->iffeatures, u) {
2437 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2438 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002439 }
2440 }
2441 }
2442 }
2443
2444 /* we should have, in features set, a superset of features applicable to the target node.
2445 * So when adding features applicable to the target into the features set, we should not be
2446 * able to actually add any new feature, otherwise it is not a subset of features applicable
2447 * to the leafref itself. */
2448 count = features.count;
2449 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002450 if (iter->iffeatures) {
2451 LY_ARRAY_FOR(iter->iffeatures, u) {
2452 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2453 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002454 /* new feature was added (or LY_EMEM) */
2455 goto cleanup;
2456 }
2457 }
2458 }
2459 }
2460 }
2461 ret = LY_SUCCESS;
2462
2463cleanup:
2464 ly_set_erase(&features, NULL);
2465 return ret;
2466}
2467
Michal Vasko004d3152020-06-11 19:59:22 +02002468static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2469 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2470 struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002471
Radek Krejcia3045382018-11-22 14:30:31 +01002472/**
2473 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2474 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002475 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2476 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2477 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2478 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002479 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002480 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002481 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002482 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2483 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2484 * @param[out] type Newly created type structure with the filled information about the type.
2485 * @return LY_ERR value.
2486 */
Radek Krejci19a96102018-11-15 13:38:09 +01002487static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002488lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2489 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2490 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2491 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002492{
2493 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002494 struct lysc_type_bin *bin;
2495 struct lysc_type_num *num;
2496 struct lysc_type_str *str;
2497 struct lysc_type_bits *bits;
2498 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002499 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002500 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002501 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002502 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002503
2504 switch (basetype) {
2505 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002506 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002507
2508 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002509 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002510 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2511 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002512 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002513 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 +01002514 }
2515 }
2516
2517 if (tpdfname) {
2518 type_p->compiled = *type;
2519 *type = calloc(1, sizeof(struct lysc_type_bin));
2520 }
2521 break;
2522 case LY_TYPE_BITS:
2523 /* RFC 7950 9.7 - bits */
2524 bits = (struct lysc_type_bits*)(*type);
2525 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002526 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002527 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2528 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002529 }
2530
Radek Krejci555cb5b2018-11-16 14:54:33 +01002531 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002532 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002533 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002534 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002535 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002536 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002537 free(*type);
2538 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002539 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002540 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002541 }
2542
2543 if (tpdfname) {
2544 type_p->compiled = *type;
2545 *type = calloc(1, sizeof(struct lysc_type_bits));
2546 }
2547 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002548 case LY_TYPE_DEC64:
2549 dec = (struct lysc_type_dec*)(*type);
2550
2551 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002552 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002553 if (!type_p->fraction_digits) {
2554 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002555 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002556 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002557 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002558 free(*type);
2559 *type = NULL;
2560 }
2561 return LY_EVALID;
2562 }
2563 } else if (type_p->fraction_digits) {
2564 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002565 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002566 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2567 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002568 tpdfname);
2569 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002570 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2571 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002572 free(*type);
2573 *type = NULL;
2574 }
2575 return LY_EVALID;
2576 }
2577 dec->fraction_digits = type_p->fraction_digits;
2578
2579 /* RFC 7950 9.2.4 - range */
2580 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002581 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2582 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002583 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002584 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 +01002585 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002586 }
2587
2588 if (tpdfname) {
2589 type_p->compiled = *type;
2590 *type = calloc(1, sizeof(struct lysc_type_dec));
2591 }
2592 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002593 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002594 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002595
2596 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002597 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002598 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2599 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002600 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002601 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 +01002602 }
2603 } else if (base && ((struct lysc_type_str*)base)->length) {
2604 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2605 }
2606
2607 /* RFC 7950 9.4.5 - pattern */
2608 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002609 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002610 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002611 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2612 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2613 }
2614
2615 if (tpdfname) {
2616 type_p->compiled = *type;
2617 *type = calloc(1, sizeof(struct lysc_type_str));
2618 }
2619 break;
2620 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002621 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002622
2623 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002624 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002625 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002626 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002627 }
2628
Radek Krejci555cb5b2018-11-16 14:54:33 +01002629 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002630 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002631 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002632 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002633 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002634 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002635 free(*type);
2636 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002637 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002638 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002639 }
2640
2641 if (tpdfname) {
2642 type_p->compiled = *type;
2643 *type = calloc(1, sizeof(struct lysc_type_enum));
2644 }
2645 break;
2646 case LY_TYPE_INT8:
2647 case LY_TYPE_UINT8:
2648 case LY_TYPE_INT16:
2649 case LY_TYPE_UINT16:
2650 case LY_TYPE_INT32:
2651 case LY_TYPE_UINT32:
2652 case LY_TYPE_INT64:
2653 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002654 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002655
2656 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002657 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002658 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2659 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002660 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002661 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 +01002662 }
2663 }
2664
2665 if (tpdfname) {
2666 type_p->compiled = *type;
2667 *type = calloc(1, sizeof(struct lysc_type_num));
2668 }
2669 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002670 case LY_TYPE_IDENT:
2671 idref = (struct lysc_type_identityref*)(*type);
2672
2673 /* RFC 7950 9.10.2 - base */
2674 if (type_p->bases) {
2675 if (base) {
2676 /* only the directly derived identityrefs can contain base specification */
2677 if (tpdfname) {
2678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002679 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002680 tpdfname);
2681 } else {
2682 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002683 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002684 free(*type);
2685 *type = NULL;
2686 }
2687 return LY_EVALID;
2688 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002689 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002690 }
2691
2692 if (!base && !type_p->flags) {
2693 /* type derived from identityref built-in type must contain at least one base */
2694 if (tpdfname) {
2695 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2696 } else {
2697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2698 free(*type);
2699 *type = NULL;
2700 }
2701 return LY_EVALID;
2702 }
2703
2704 if (tpdfname) {
2705 type_p->compiled = *type;
2706 *type = calloc(1, sizeof(struct lysc_type_identityref));
2707 }
2708 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002709 case LY_TYPE_LEAFREF:
Michal Vasko004d3152020-06-11 19:59:22 +02002710 lref = (struct lysc_type_leafref*)*type;
2711
Radek Krejcia3045382018-11-22 14:30:31 +01002712 /* RFC 7950 9.9.3 - require-instance */
2713 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002714 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002715 if (tpdfname) {
2716 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2717 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2718 } else {
2719 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2720 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2721 free(*type);
2722 *type = NULL;
2723 }
2724 return LY_EVALID;
2725 }
Michal Vasko004d3152020-06-11 19:59:22 +02002726 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002727 } else if (base) {
2728 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002729 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002730 } else {
2731 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002732 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002733 }
2734 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002735 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2736 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002737 } else if (base) {
Michal Vasko004d3152020-06-11 19:59:22 +02002738 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path);
2739 lref->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002740 } else if (tpdfname) {
2741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2742 return LY_EVALID;
2743 } else {
2744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2745 free(*type);
2746 *type = NULL;
2747 return LY_EVALID;
2748 }
2749 if (tpdfname) {
2750 type_p->compiled = *type;
2751 *type = calloc(1, sizeof(struct lysc_type_leafref));
2752 }
2753 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002754 case LY_TYPE_INST:
2755 /* RFC 7950 9.9.3 - require-instance */
2756 if (type_p->flags & LYS_SET_REQINST) {
2757 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2758 } else {
2759 /* default is true */
2760 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2761 }
2762
2763 if (tpdfname) {
2764 type_p->compiled = *type;
2765 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2766 }
2767 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002768 case LY_TYPE_UNION:
2769 un = (struct lysc_type_union*)(*type);
2770
2771 /* RFC 7950 7.4 - type */
2772 if (type_p->types) {
2773 if (base) {
2774 /* only the directly derived union can contain types specification */
2775 if (tpdfname) {
2776 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2777 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2778 tpdfname);
2779 } else {
2780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2781 "Invalid type substatement for the type not directly derived from union built-in type.");
2782 free(*type);
2783 *type = NULL;
2784 }
2785 return LY_EVALID;
2786 }
2787 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002788 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2789 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002790 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
2791 &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002792 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2793 /* add space for additional types from the union subtype */
2794 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002795 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 +02002796 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002797
2798 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002799 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002800 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2801 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2802 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2803 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 +02002804 lref = (struct lysc_type_leafref *)un->types[u + additional];
2805
2806 lref->basetype = LY_TYPE_LEAFREF;
2807 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path);
2808 lref->refcount = 1;
2809 lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2810 lref->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002811 /* TODO extensions */
2812
2813 } else {
2814 un->types[u + additional] = un_aux->types[v];
2815 ++un_aux->types[v]->refcount;
2816 }
2817 ++additional;
2818 LY_ARRAY_INCREMENT(un->types);
2819 }
2820 /* compensate u increment in main loop */
2821 --additional;
2822
2823 /* free the replaced union subtype */
2824 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2825 } else {
2826 LY_ARRAY_INCREMENT(un->types);
2827 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002828 }
2829 }
2830
2831 if (!base && !type_p->flags) {
2832 /* type derived from union built-in type must contain at least one type */
2833 if (tpdfname) {
2834 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2835 } else {
2836 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2837 free(*type);
2838 *type = NULL;
2839 }
2840 return LY_EVALID;
2841 }
2842
2843 if (tpdfname) {
2844 type_p->compiled = *type;
2845 *type = calloc(1, sizeof(struct lysc_type_union));
2846 }
2847 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002848 case LY_TYPE_BOOL:
2849 case LY_TYPE_EMPTY:
2850 case LY_TYPE_UNKNOWN: /* just to complete switch */
2851 break;
2852 }
2853 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2854done:
2855 return ret;
2856}
2857
Radek Krejcia3045382018-11-22 14:30:31 +01002858/**
2859 * @brief Compile information about the leaf/leaf-list's type.
2860 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002861 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2862 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2863 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2864 * @param[in] context_name Name of the context node or referencing typedef for logging.
2865 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002866 * @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 +01002867 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002868 * @return LY_ERR value.
2869 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002870static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002871lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2872 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2873 struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002874{
2875 LY_ERR ret = LY_SUCCESS;
2876 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002877 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002878 struct type_context {
2879 const struct lysp_tpdf *tpdf;
2880 struct lysp_node *node;
2881 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002882 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002883 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002884 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002885 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002886 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002887 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002888
2889 (*type) = NULL;
2890
2891 tctx = calloc(1, sizeof *tctx);
2892 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002893 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002894 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2895 ret == LY_SUCCESS;
2896 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2897 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2898 if (basetype) {
2899 break;
2900 }
2901
2902 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002903 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2904 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002905 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2906
Radek Krejcicdfecd92018-11-26 11:27:32 +01002907 if (units && !*units) {
2908 /* inherit units */
2909 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2910 }
Radek Krejci01342af2019-01-03 15:18:08 +01002911 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002912 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002913 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02002914 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002915 }
Radek Krejci01342af2019-01-03 15:18:08 +01002916 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002917 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2918 break;
2919 }
2920
Radek Krejci19a96102018-11-15 13:38:09 +01002921 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002922 /* it is not necessary to continue, the rest of the chain was already compiled,
2923 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002924 basetype = tctx->tpdf->type.compiled->basetype;
2925 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002926 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002927 dummyloops = 1;
2928 goto preparenext;
2929 } else {
2930 tctx = NULL;
2931 break;
2932 }
Radek Krejci19a96102018-11-15 13:38:09 +01002933 }
2934
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002935 /* circular typedef reference detection */
2936 for (u = 0; u < tpdf_chain.count; u++) {
2937 /* local part */
2938 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2939 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2940 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2941 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2942 free(tctx);
2943 ret = LY_EVALID;
2944 goto cleanup;
2945 }
2946 }
2947 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2948 /* global part for unions corner case */
2949 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2950 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2951 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2952 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2953 free(tctx);
2954 ret = LY_EVALID;
2955 goto cleanup;
2956 }
2957 }
2958
Radek Krejci19a96102018-11-15 13:38:09 +01002959 /* store information for the following processing */
2960 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2961
Radek Krejcicdfecd92018-11-26 11:27:32 +01002962preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002963 /* prepare next loop */
2964 tctx_prev = tctx;
2965 tctx = calloc(1, sizeof *tctx);
2966 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2967 }
2968 free(tctx);
2969
2970 /* allocate type according to the basetype */
2971 switch (basetype) {
2972 case LY_TYPE_BINARY:
2973 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002974 break;
2975 case LY_TYPE_BITS:
2976 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002977 break;
2978 case LY_TYPE_BOOL:
2979 case LY_TYPE_EMPTY:
2980 *type = calloc(1, sizeof(struct lysc_type));
2981 break;
2982 case LY_TYPE_DEC64:
2983 *type = calloc(1, sizeof(struct lysc_type_dec));
2984 break;
2985 case LY_TYPE_ENUM:
2986 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002987 break;
2988 case LY_TYPE_IDENT:
2989 *type = calloc(1, sizeof(struct lysc_type_identityref));
2990 break;
2991 case LY_TYPE_INST:
2992 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2993 break;
2994 case LY_TYPE_LEAFREF:
2995 *type = calloc(1, sizeof(struct lysc_type_leafref));
2996 break;
2997 case LY_TYPE_STRING:
2998 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002999 break;
3000 case LY_TYPE_UNION:
3001 *type = calloc(1, sizeof(struct lysc_type_union));
3002 break;
3003 case LY_TYPE_INT8:
3004 case LY_TYPE_UINT8:
3005 case LY_TYPE_INT16:
3006 case LY_TYPE_UINT16:
3007 case LY_TYPE_INT32:
3008 case LY_TYPE_UINT32:
3009 case LY_TYPE_INT64:
3010 case LY_TYPE_UINT64:
3011 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003012 break;
3013 case LY_TYPE_UNKNOWN:
3014 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3015 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3016 ret = LY_EVALID;
3017 goto cleanup;
3018 }
3019 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003020 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003021 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3022 ly_data_type2str[basetype]);
3023 free(*type);
3024 (*type) = NULL;
3025 ret = LY_EVALID;
3026 goto cleanup;
3027 }
3028
3029 /* get restrictions from the referred typedefs */
3030 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3031 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003032
3033 /* remember the typedef context for circular check */
3034 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3035
Radek Krejci43699232018-11-23 14:59:46 +01003036 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003037 base = tctx->tpdf->type.compiled;
3038 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003039 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003040 /* no change, just use the type information from the base */
3041 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3042 ++base->refcount;
3043 continue;
3044 }
3045
3046 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003047 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3048 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3049 tctx->tpdf->name, ly_data_type2str[basetype]);
3050 ret = LY_EVALID;
3051 goto cleanup;
3052 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3053 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3054 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3055 tctx->tpdf->name, tctx->tpdf->dflt);
3056 ret = LY_EVALID;
3057 goto cleanup;
3058 }
3059
Radek Krejci19a96102018-11-15 13:38:09 +01003060 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003061 /* TODO user type plugins */
3062 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003063 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003064 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 +01003065 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003066 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003067 LY_CHECK_GOTO(ret, cleanup);
3068 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003069 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003070 /* remove the processed typedef contexts from the stack for circular check */
3071 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003072
Radek Krejcic5c27e52018-11-15 14:38:11 +01003073 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003074 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003075 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003076 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003077 /* TODO user type plugins */
3078 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003079 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003080 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 +01003081 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003082 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003083 /* no specific restriction in leaf's type definition, copy from the base */
3084 free(*type);
3085 (*type) = base;
3086 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003087 }
Radek Krejcia1911222019-07-22 17:24:50 +02003088 if (dflt && !(*type)->dflt) {
3089 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003090 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003091 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3092 (*type)->dflt->realtype = (*type);
3093 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3094 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003095 LY_PREF_SCHEMA, (*type)->dflt_mod, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003096 if (err) {
3097 ly_err_print(err);
3098 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3099 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3100 ly_err_free(err);
3101 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003102 if (ret == LY_EINCOMPLETE) {
3103 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003104 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003105
3106 /* but in general result is so far ok */
3107 ret = LY_SUCCESS;
3108 }
Radek Krejcia1911222019-07-22 17:24:50 +02003109 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003110 }
Radek Krejci19a96102018-11-15 13:38:09 +01003111
Radek Krejci0935f412019-08-20 16:15:18 +02003112 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003113
3114cleanup:
3115 ly_set_erase(&tpdf_chain, free);
3116 return ret;
3117}
3118
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003119/**
3120 * @brief Compile status information of the given node.
3121 *
3122 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3123 * has the status correctly set during the compilation.
3124 *
3125 * @param[in] ctx Compile context
3126 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3127 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3128 * the compatibility with the parent's status value.
3129 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3130 * @return LY_ERR value.
3131 */
3132static LY_ERR
3133lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3134{
3135 /* status - it is not inherited by specification, but it does not make sense to have
3136 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3137 if (!((*node_flags) & LYS_STATUS_MASK)) {
3138 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3139 if ((parent_flags & 0x3) != 0x3) {
3140 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3141 * combination of bits (0x3) which marks the uses_status value */
3142 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3143 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3144 }
3145 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3146 } else {
3147 (*node_flags) |= LYS_STATUS_CURR;
3148 }
3149 } else if (parent_flags & LYS_STATUS_MASK) {
3150 /* check status compatibility with the parent */
3151 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3152 if ((*node_flags) & LYS_STATUS_CURR) {
3153 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3154 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3155 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3156 } else { /* LYS_STATUS_DEPRC */
3157 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3158 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3159 }
3160 return LY_EVALID;
3161 }
3162 }
3163 return LY_SUCCESS;
3164}
3165
Radek Krejci8cce8532019-03-05 11:27:45 +01003166/**
3167 * @brief Check uniqness of the node/action/notification name.
3168 *
3169 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3170 * structures, but they share the namespace so we need to check their name collisions.
3171 *
3172 * @param[in] ctx Compile context.
3173 * @param[in] children List (linked list) of data nodes to go through.
3174 * @param[in] actions List (sized array) of actions or RPCs to go through.
3175 * @param[in] notifs List (sized array) of Notifications to go through.
3176 * @param[in] name Name of the item to find in the given lists.
3177 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3178 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3179 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3180 */
3181static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02003182lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions,
3183 const struct lysc_notif *notifs, const char *name, void *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003184{
3185 const struct lysc_node *iter;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003186 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003187
3188 LY_LIST_FOR(children, iter) {
3189 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3190 goto error;
3191 }
3192 }
3193 LY_ARRAY_FOR(actions, u) {
3194 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3195 goto error;
3196 }
3197 }
3198 LY_ARRAY_FOR(notifs, u) {
3199 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3200 goto error;
3201 }
3202 }
3203 return LY_SUCCESS;
3204error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003205 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003206 return LY_EEXIST;
3207}
3208
Radek Krejciec4da802019-05-02 13:02:41 +02003209static 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 +01003210
Radek Krejcia3045382018-11-22 14:30:31 +01003211/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003212 * @brief Compile parsed RPC/action schema node information.
3213 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003214 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003215 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003216 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3217 * @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).
3218 * Zero means no uses, non-zero value with no status bit set mean the default status.
3219 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3220 */
3221static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003222lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003223 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3224{
3225 LY_ERR ret = LY_SUCCESS;
3226 struct lysp_node *child_p;
3227 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003228 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003229
Radek Krejci327de162019-06-14 12:52:07 +02003230 lysc_update_path(ctx, parent, action_p->name);
3231
Radek Krejci8cce8532019-03-05 11:27:45 +01003232 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3233 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3234 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3235 action_p->name, action)) {
3236 return LY_EVALID;
3237 }
3238
Radek Krejciec4da802019-05-02 13:02:41 +02003239 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003240 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003241 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003242 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003243 return LY_EVALID;
3244 }
3245
Michal Vasko1bf09392020-03-27 12:38:10 +01003246 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003247 action->module = ctx->mod;
3248 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003249 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003250 action->sp = action_p;
3251 }
3252 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3253
3254 /* status - it is not inherited by specification, but it does not make sense to have
3255 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003256 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003257
3258 DUP_STRING(ctx->ctx, action_p->name, action->name);
3259 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3260 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003261 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003262 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003263
3264 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003265 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003266 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003267 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 +02003268 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003269 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003270 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003271 }
Radek Krejci327de162019-06-14 12:52:07 +02003272 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003273 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003274
3275 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003276 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003277 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003278 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 +02003279 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003280 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003281 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003282 }
Radek Krejci327de162019-06-14 12:52:07 +02003283 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003284 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003285
3286 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3287 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003288 ly_set_add(&ctx->xpath, action, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003289 }
3290
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003291cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003292 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003293 return ret;
3294}
3295
3296/**
Radek Krejci43981a32019-04-12 09:44:11 +02003297 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003298 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003299 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003300 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3301 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3302 * @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 +02003303 * Zero means no uses, non-zero value with no status bit set mean the default status.
3304 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3305 */
3306static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003307lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003308 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3309{
3310 LY_ERR ret = LY_SUCCESS;
3311 struct lysp_node *child_p;
3312 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003313 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003314
Radek Krejci327de162019-06-14 12:52:07 +02003315 lysc_update_path(ctx, parent, notif_p->name);
3316
Radek Krejcifc11bd72019-04-11 16:00:05 +02003317 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3318 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3319 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3320 notif_p->name, notif)) {
3321 return LY_EVALID;
3322 }
3323
Radek Krejciec4da802019-05-02 13:02:41 +02003324 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003325 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3326 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003327 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003328 return LY_EVALID;
3329 }
3330
3331 notif->nodetype = LYS_NOTIF;
3332 notif->module = ctx->mod;
3333 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003334 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003335 notif->sp = notif_p;
3336 }
3337 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3338
3339 /* status - it is not inherited by specification, but it does not make sense to have
3340 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3341 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3342
3343 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3344 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3345 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003346 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003347 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003348 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3349 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003350 ly_set_add(&ctx->xpath, notif, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003351 }
Radek Krejci0935f412019-08-20 16:15:18 +02003352 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003353
Radek Krejciec4da802019-05-02 13:02:41 +02003354 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003355 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003356 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003357 }
3358
Radek Krejci327de162019-06-14 12:52:07 +02003359 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003360cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003361 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003362 return ret;
3363}
3364
3365/**
Radek Krejcia3045382018-11-22 14:30:31 +01003366 * @brief Compile parsed container node information.
3367 * @param[in] ctx Compile context
3368 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003369 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3370 * is enriched with the container-specific information.
3371 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3372 */
Radek Krejci19a96102018-11-15 13:38:09 +01003373static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003374lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003375{
3376 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3377 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3378 struct lysp_node *child_p;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003379 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003380 LY_ERR ret = LY_SUCCESS;
3381
Radek Krejcife909632019-02-12 15:34:42 +01003382 if (cont_p->presence) {
Michal Vaskoba417ac2020-08-06 14:48:20 +02003383 /* explicit presence */
Radek Krejcife909632019-02-12 15:34:42 +01003384 cont->flags |= LYS_PRESENCE;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003385 } else if (cont_p->musts) {
3386 /* container with a must condition */
Radek Krejci175f25b2020-08-13 12:02:36 +02003387 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name);
3388 cont->flags |= LYS_PRESENCE;
3389 } else if (cont_p->when) {
3390 /* container with a when condition */
3391 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"when\" condition.", cont_p->name);
Michal Vaskoba417ac2020-08-06 14:48:20 +02003392 cont->flags |= LYS_PRESENCE;
3393 } else if (cont_p->parent) {
3394 if (cont_p->parent->nodetype == LYS_CHOICE) {
3395 /* container is an implicit case, so its existence decides the existence of the whole case */
Radek Krejci175f25b2020-08-13 12:02:36 +02003396 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
Michal Vaskoba417ac2020-08-06 14:48:20 +02003397 cont_p->name, cont_p->parent->name);
3398 cont->flags |= LYS_PRESENCE;
3399 } else if ((cont_p->parent->nodetype == LYS_CASE)
3400 && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) {
3401 /* container is the only node in a case, so its existence decides the existence of the whole case */
Radek Krejci175f25b2020-08-13 12:02:36 +02003402 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
Michal Vaskoba417ac2020-08-06 14:48:20 +02003403 cont_p->name, cont_p->parent->name);
3404 cont->flags |= LYS_PRESENCE;
3405 }
Radek Krejcife909632019-02-12 15:34:42 +01003406 }
3407
Michal Vaskoba417ac2020-08-06 14:48:20 +02003408 /* more cases when the container has meaning but is kept NP for convenience:
3409 * - when condition
3410 * - direct child action/notification
3411 */
3412
Radek Krejci19a96102018-11-15 13:38:09 +01003413 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003414 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003415 }
3416
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003417 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003418 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3419 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003420 ly_set_add(&ctx->xpath, cont, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003421 }
Radek Krejciec4da802019-05-02 13:02:41 +02003422 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3423 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003424
3425done:
3426 return ret;
3427}
3428
Radek Krejci33f72892019-02-21 10:36:58 +01003429/*
3430 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3431 * @param[in] ctx Compile context.
3432 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3433 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003434 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3435 * @return LY_ERR value.
3436 */
3437static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003438lys_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 +01003439{
Radek Krejci33f72892019-02-21 10:36:58 +01003440 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3441
Radek Krejciec4da802019-05-02 13:02:41 +02003442 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 +01003443 leaf->units ? NULL : &leaf->units));
3444 if (leaf->nodetype == LYS_LEAFLIST) {
3445 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003446 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3447 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003448 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003449 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3450 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3451 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3452 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003453 LY_ARRAY_INCREMENT(llist->dflts);
3454 }
3455 } else {
3456 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003457 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003458 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3459 leaf->dflt->realtype = leaf->type->dflt->realtype;
3460 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3461 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003462 }
3463 }
3464 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3465 /* 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 +02003466 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003467 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003468 LY_ARRAY_COUNT_TYPE u;
Radek Krejci33f72892019-02-21 10:36:58 +01003469 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3470 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3471 /* 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 +02003472 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003473 }
3474 }
3475 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003476 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3477 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3478 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3479 return LY_EVALID;
3480 }
3481 }
3482
Radek Krejci33f72892019-02-21 10:36:58 +01003483 return LY_SUCCESS;
3484}
3485
Radek Krejcia3045382018-11-22 14:30:31 +01003486/**
3487 * @brief Compile parsed leaf node information.
3488 * @param[in] ctx Compile context
3489 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003490 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3491 * is enriched with the leaf-specific information.
3492 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3493 */
Radek Krejci19a96102018-11-15 13:38:09 +01003494static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003495lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003496{
3497 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3498 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Michal Vasko7c8439f2020-08-05 13:25:19 +02003499 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003500 LY_ERR ret = LY_SUCCESS;
3501
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003502 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003503 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3504 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003505 ly_set_add(&ctx->xpath, leaf, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003506 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003507 if (leaf_p->units) {
3508 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3509 leaf->flags |= LYS_SET_UNITS;
3510 }
Radek Krejcia1911222019-07-22 17:24:50 +02003511
3512 /* the dflt member is just filled to avoid getting the default value from the type */
3513 leaf->dflt = (void*)leaf_p->dflt;
3514 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003515 if (ret) {
3516 leaf->dflt = NULL;
3517 return ret;
3518 }
Radek Krejcia1911222019-07-22 17:24:50 +02003519
Radek Krejciccd20f12019-02-15 14:12:27 +01003520 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003521 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003522 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003523 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3524 leaf->dflt->realtype = leaf->type;
3525 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3526 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003527 LY_PREF_SCHEMA, leaf->dflt_mod, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003528 leaf->dflt->realtype->refcount++;
3529 if (err) {
3530 ly_err_print(err);
3531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3532 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3533 ly_err_free(err);
3534 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003535 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003536 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003537 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003538
3539 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003540 ret = LY_SUCCESS;
3541 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003542 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003543 leaf->flags |= LYS_SET_DFLT;
3544 }
Radek Krejci43699232018-11-23 14:59:46 +01003545
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003546
Radek Krejci19a96102018-11-15 13:38:09 +01003547done:
3548 return ret;
3549}
3550
Radek Krejcia3045382018-11-22 14:30:31 +01003551/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003552 * @brief Compile parsed leaf-list node information.
3553 * @param[in] ctx Compile context
3554 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003555 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3556 * is enriched with the leaf-list-specific information.
3557 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3558 */
3559static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003560lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003561{
3562 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3563 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003564 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003565 LY_ERR ret = LY_SUCCESS;
3566
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003567 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003568 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3569 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003570 ly_set_add(&ctx->xpath, llist, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003571 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003572 if (llist_p->units) {
3573 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3574 llist->flags |= LYS_SET_UNITS;
3575 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003576
Radek Krejcia1911222019-07-22 17:24:50 +02003577 /* the dflts member is just filled to avoid getting the default value from the type */
3578 llist->dflts = (void*)llist_p->dflts;
3579 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Michal Vasko6a044b22020-01-15 12:25:39 +01003580 if (ret != LY_SUCCESS) {
3581 /* make sure the defaults are freed correctly */
3582 if (llist_p->dflts) {
3583 llist->dflts = NULL;
3584 }
3585 return ret;
3586 }
3587
Radek Krejci0e5d8382018-11-28 16:37:53 +01003588 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003589 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003590 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
3591 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003592 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003593 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003594 LY_ARRAY_INCREMENT(llist->dflts_mods);
3595 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003596 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003597 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3598 llist->dflts[u]->realtype = llist->type;
3599 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3600 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003601 LY_PREF_SCHEMA, llist->dflts_mods[u], node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003602 llist->dflts[u]->realtype->refcount++;
3603 if (err) {
3604 ly_err_print(err);
3605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3606 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3607 ly_err_free(err);
3608 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003609 if (ret == LY_EINCOMPLETE) {
3610 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003611 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003612
3613 /* but in general result is so far ok */
3614 ret = LY_SUCCESS;
3615 }
Radek Krejcia1911222019-07-22 17:24:50 +02003616 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003617 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003618 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003619 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003620 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_COUNT(llist->dflts)) {
Radek Krejcia1911222019-07-22 17:24:50 +02003621 /* configuration data values must be unique - so check the default values */
3622 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003623 for (v = u + 1; v < LY_ARRAY_COUNT(llist->dflts); ++v) {
Radek Krejcia1911222019-07-22 17:24:50 +02003624 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3625 int dynamic = 0;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003626 const char *val = llist->type->plugin->print(llist->dflts[v], LY_PREF_SCHEMA, llist->dflts_mods[v],
3627 &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +02003628 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3629 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3630 if (dynamic) {
3631 free((char*)val);
3632 }
3633 return LY_EVALID;
3634 }
3635 }
3636 }
3637 }
3638
3639 /* 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 +01003640
3641 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003642 if (llist->min) {
3643 llist->flags |= LYS_MAND_TRUE;
3644 }
Radek Krejcib7408632018-11-28 17:12:11 +01003645 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003646
Radek Krejci0e5d8382018-11-28 16:37:53 +01003647done:
3648 return ret;
3649}
3650
3651/**
Radek Krejci7af64242019-02-18 13:07:53 +01003652 * @brief Compile information about list's uniques.
3653 * @param[in] ctx Compile context.
3654 * @param[in] context_module Module where the prefixes are going to be resolved.
3655 * @param[in] uniques Sized array list of unique statements.
3656 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3657 * @return LY_ERR value.
3658 */
3659static LY_ERR
3660lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3661{
3662 LY_ERR ret = LY_SUCCESS;
3663 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003664 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003665 const char *keystr, *delim;
3666 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003667 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7af64242019-02-18 13:07:53 +01003668 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003669 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003670
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003671 for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003672 config = -1;
3673 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3674 keystr = uniques[v];
3675 while (keystr) {
3676 delim = strpbrk(keystr, " \t\n");
3677 if (delim) {
3678 len = delim - keystr;
3679 while (isspace(*delim)) {
3680 ++delim;
3681 }
3682 } else {
3683 len = strlen(keystr);
3684 }
3685
3686 /* unique node must be present */
3687 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003688 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3689 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003690 if (ret != LY_SUCCESS) {
3691 if (ret == LY_EDENIED) {
3692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003693 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003694 len, keystr, lys_nodetype2str((*key)->nodetype));
3695 }
3696 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003697 } else if (flags) {
3698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3699 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003700 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003701 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003702 }
3703
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003704
Radek Krejci7af64242019-02-18 13:07:53 +01003705 /* all referenced leafs must be of the same config type */
3706 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3707 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003708 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003709 return LY_EVALID;
3710 } else if ((*key)->flags & LYS_CONFIG_W) {
3711 config = 1;
3712 } else { /* LYS_CONFIG_R */
3713 config = 0;
3714 }
3715
Michal Vasko14654712020-02-06 08:35:21 +01003716 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3717 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3718 if (parent->nodetype == LYS_LIST) {
3719 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3720 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3721 return LY_EVALID;
3722 }
3723 }
3724
Radek Krejci7af64242019-02-18 13:07:53 +01003725 /* check status */
3726 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3727 (*key)->flags, (*key)->module, (*key)->name));
3728
3729 /* mark leaf as unique */
3730 (*key)->flags |= LYS_UNIQUE;
3731
3732 /* next unique value in line */
3733 keystr = delim;
3734 }
3735 /* next unique definition */
3736 }
3737
3738 return LY_SUCCESS;
3739}
3740
3741/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003742 * @brief Compile parsed list node information.
3743 * @param[in] ctx Compile context
3744 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003745 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3746 * is enriched with the list-specific information.
3747 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3748 */
3749static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003750lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003751{
3752 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3753 struct lysc_node_list *list = (struct lysc_node_list*)node;
3754 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003755 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003756 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003757 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003758 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003759 LY_ERR ret = LY_SUCCESS;
3760
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003761 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003762 if (list->min) {
3763 list->flags |= LYS_MAND_TRUE;
3764 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003765 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3766
3767 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003768 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003769 }
3770
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003771 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003772 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3773 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003774 ly_set_add(&ctx->xpath, list, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003775 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003776
3777 /* keys */
3778 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3780 return LY_EVALID;
3781 }
3782
3783 /* find all the keys (must be direct children) */
3784 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003785 if (!keystr) {
3786 /* keyless list */
3787 list->flags |= LYS_KEYLESS;
3788 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003789 while (keystr) {
3790 delim = strpbrk(keystr, " \t\n");
3791 if (delim) {
3792 len = delim - keystr;
3793 while (isspace(*delim)) {
3794 ++delim;
3795 }
3796 } else {
3797 len = strlen(keystr);
3798 }
3799
3800 /* key node must be present */
Michal Vaskoe444f752020-02-10 12:20:06 +01003801 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 +02003802 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003803 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3804 "The list's key \"%.*s\" not found.", len, keystr);
3805 return LY_EVALID;
3806 }
3807 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003808 if (key->flags & LYS_KEY) {
3809 /* the node was already marked as a key */
3810 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3811 "Duplicated key identifier \"%.*s\".", len, keystr);
3812 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003813 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003814
3815 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003816 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003817 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003818 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3819 return LY_EVALID;
3820 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003821 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003822 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003823 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003824 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003825 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003826 return LY_EVALID;
3827 }
3828 } else {
3829 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003830 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003831 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003832 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003833 return LY_EVALID;
3834 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003835 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003836 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003837 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003838 return LY_EVALID;
3839 }
3840 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003841
3842 /* check status */
3843 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003844 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003845
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003846 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003847 if (key->dflt) {
3848 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3849 lysc_type_free(ctx->ctx, key->dflt->realtype);
3850 free(key->dflt);
3851 key->dflt = NULL;
3852 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003853 }
3854 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003855 key->flags |= LYS_KEY;
3856
3857 /* move it to the correct position */
3858 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3859 /* fix links in closest previous siblings of the key */
3860 if (key->next) {
3861 key->next->prev = key->prev;
3862 } else {
3863 /* last child */
3864 list->child->prev = key->prev;
3865 }
3866 if (key->prev->next) {
3867 key->prev->next = key->next;
3868 }
3869 /* fix links in the key */
3870 if (prev_key) {
3871 key->prev = (struct lysc_node*)prev_key;
3872 key->next = prev_key->next;
3873 } else {
3874 key->prev = list->child->prev;
3875 key->next = list->child;
3876 }
3877 /* fix links in closes future siblings of the key */
3878 if (prev_key) {
3879 if (prev_key->next) {
3880 prev_key->next->prev = (struct lysc_node*)key;
3881 } else {
3882 list->child->prev = (struct lysc_node*)key;
3883 }
3884 prev_key->next = (struct lysc_node*)key;
3885 } else {
3886 list->child->prev = (struct lysc_node*)key;
3887 }
3888 /* fix links in parent */
3889 if (!key->prev->next) {
3890 list->child = (struct lysc_node*)key;
3891 }
3892 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003893
3894 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003895 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003896 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003897 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003898 }
3899
3900 /* uniques */
3901 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003902 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003903 }
3904
Radek Krejciec4da802019-05-02 13:02:41 +02003905 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3906 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003907
3908done:
3909 return ret;
3910}
3911
Radek Krejcib56c7502019-02-13 14:19:54 +01003912/**
3913 * @brief Do some checks and set the default choice's case.
3914 *
3915 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3916 *
3917 * @param[in] ctx Compile context.
3918 * @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,
3919 * not the reference to the imported module.
3920 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3921 * @return LY_ERR value.
3922 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003923static LY_ERR
3924lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3925{
3926 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3927 const char *prefix = NULL, *name;
3928 size_t prefix_len = 0;
3929
3930 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3931 name = strchr(dflt, ':');
3932 if (name) {
3933 prefix = dflt;
3934 prefix_len = name - prefix;
3935 ++name;
3936 } else {
3937 name = dflt;
3938 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003939 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003940 /* prefixed default case make sense only for the prefix of the schema itself */
3941 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3942 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3943 prefix_len, prefix);
3944 return LY_EVALID;
3945 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003946 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 +01003947 if (!ch->dflt) {
3948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3949 "Default case \"%s\" not found.", dflt);
3950 return LY_EVALID;
3951 }
3952 /* no mandatory nodes directly under the default case */
3953 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003954 if (iter->parent != (struct lysc_node*)ch->dflt) {
3955 break;
3956 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003957 if (iter->flags & LYS_MAND_TRUE) {
3958 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3959 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3960 return LY_EVALID;
3961 }
3962 }
Radek Krejci01342af2019-01-03 15:18:08 +01003963 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003964 return LY_SUCCESS;
3965}
3966
Radek Krejciccd20f12019-02-15 14:12:27 +01003967static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003968lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003969{
3970 struct lys_module *mod;
3971 const char *prefix = NULL, *name;
3972 size_t prefix_len = 0;
3973 struct lysc_node_case *cs;
3974 struct lysc_node *node;
3975
3976 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3977 name = strchr(dflt, ':');
3978 if (name) {
3979 prefix = dflt;
3980 prefix_len = name - prefix;
3981 ++name;
3982 } else {
3983 name = dflt;
3984 }
3985 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3986 if (prefix) {
3987 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3988 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003989 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3990 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003991 return LY_EVALID;
3992 }
3993 } else {
3994 mod = ctx->mod;
3995 }
3996 /* get the default case */
Michal Vaskoe444f752020-02-10 12:20:06 +01003997 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 +01003998 if (!cs) {
3999 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004000 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004001 return LY_EVALID;
4002 }
4003
4004 /* check that there is no mandatory node */
4005 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01004006 if (node->parent != (struct lysc_node*)cs) {
4007 break;
4008 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004009 if (node->flags & LYS_MAND_TRUE) {
4010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004011 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4012 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004013 return LY_EVALID;
4014 }
4015 }
4016
4017 /* set the default case in choice */
4018 ch->dflt = cs;
4019 cs->flags |= LYS_SET_DFLT;
4020
4021 return LY_SUCCESS;
4022}
4023
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004024/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004025 * @brief Compile parsed choice node information.
4026 * @param[in] ctx Compile context
4027 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004028 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004029 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004030 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4031 */
4032static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004033lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004034{
4035 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4036 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4037 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004038 LY_ERR ret = LY_SUCCESS;
4039
Radek Krejci056d0a82018-12-06 16:57:25 +01004040 LY_LIST_FOR(ch_p->child, child_p) {
4041 if (child_p->nodetype == LYS_CASE) {
4042 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004043 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004044 }
4045 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004046 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004047 }
4048 }
4049
4050 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004051 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004052 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004053 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004054
Radek Krejci9800fb82018-12-13 14:26:23 +01004055 return ret;
4056}
4057
4058/**
4059 * @brief Compile parsed anydata or anyxml node information.
4060 * @param[in] ctx Compile context
4061 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004062 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4063 * is enriched with the any-specific information.
4064 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4065 */
4066static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004067lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004068{
4069 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4070 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4071 unsigned int u;
4072 LY_ERR ret = LY_SUCCESS;
4073
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004074 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004075 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4076 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004077 ly_set_add(&ctx->xpath, any, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004078 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004079
4080 if (any->flags & LYS_CONFIG_W) {
Radek Krejci5c4ed7b2020-08-12 11:29:44 +02004081 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
4082 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Radek Krejci9800fb82018-12-13 14:26:23 +01004083 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004084done:
4085 return ret;
4086}
4087
Radek Krejcib56c7502019-02-13 14:19:54 +01004088/**
Michal Vasko795b3752020-07-13 15:24:27 +02004089 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
4090 * keep specific order of augments targetting the same node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004091 *
4092 * @param[in] ctx Compile context
4093 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4094 * the choice itself is expected instead of a specific case node.
4095 * @param[in] node Schema node to connect into the list.
4096 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004097 * 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 +01004098 */
4099static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004100lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004101{
Michal Vasko795b3752020-07-13 15:24:27 +02004102 struct lysc_node **children, *start, *end;
4103 const struct lys_module *mod;
Radek Krejci056d0a82018-12-06 16:57:25 +01004104
4105 if (node->nodetype == LYS_CASE) {
4106 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4107 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004108 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004109 }
4110 if (children) {
4111 if (!(*children)) {
4112 /* first child */
4113 *children = node;
4114 } else if (*children != node) {
4115 /* by the condition in previous branch we cover the choice/case children
4116 * - the children list is shared by the choice and the the first case, in addition
4117 * the first child of each case must be referenced from the case node. So the node is
4118 * actually always already inserted in case it is the first children - so here such
4119 * a situation actually corresponds to the first branch */
Michal Vasko795b3752020-07-13 15:24:27 +02004120 if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module)
4121 && (strcmp((*children)->prev->module->name, node->module->name) > 0)) {
4122 /* some augments are already connected and we are connecting new ones,
4123 * keep module name order and insert the node into the children list */
4124 end = (*children);
4125 do {
4126 end = end->prev;
4127 mod = end->module;
4128 while (end->prev->module == mod) {
4129 end = end->prev;
4130 }
4131 } while ((end->prev->module != (*children)->module) && (end->prev->module != node->module)
4132 && (strcmp(mod->name, node->module->name) > 0));
4133
4134 /* we have the last existing node after our node, easily get the first before and connect it */
4135 start = end->prev;
4136 start->next = node;
4137 node->next = end;
4138 end->prev = node;
4139 node->prev = start;
4140 } else {
4141 /* insert at the end of the parent's children list */
4142 (*children)->prev->next = node;
4143 node->prev = (*children)->prev;
4144 (*children)->prev = node;
4145 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004146
4147 /* check the name uniqueness */
4148 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4149 lysc_node_notifs(parent), node->name, node)) {
4150 return LY_EEXIST;
4151 }
4152 }
4153 }
4154 return LY_SUCCESS;
4155}
4156
Radek Krejci95710c92019-02-11 15:49:55 +01004157/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004158 * @brief Prepare the case structure in choice node for the new data node.
4159 *
4160 * 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
4161 * created in the choice when the first child was processed.
4162 *
4163 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004164 * @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,
Radek Krejci39424802020-08-12 09:31:00 +02004165 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004166 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4167 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4168 * @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,
4169 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004170 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004171static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004172lys_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 +01004173{
4174 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004175 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004176 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004177 unsigned int u;
4178 LY_ERR ret;
4179
Radek Krejci95710c92019-02-11 15:49:55 +01004180#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004181 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004182 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004183 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4184 return NULL; \
4185 } \
4186 }
4187
Radek Krejci39424802020-08-12 09:31:00 +02004188 if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
Radek Krejci95710c92019-02-11 15:49:55 +01004189 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004190
4191 /* we have to add an implicit case node into the parent choice */
4192 cs = calloc(1, sizeof(struct lysc_node_case));
4193 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004194 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004195 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004196 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004197 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4198 /* the case is already present since the child is not its first children */
4199 return (struct lysc_node_case*)ch->cases->prev;
4200 }
Radek Krejci95710c92019-02-11 15:49:55 +01004201 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004202
4203 /* explicit parent case is not present (this is its first child) */
4204 cs = calloc(1, sizeof(struct lysc_node_case));
4205 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004206 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004207 cs->flags = LYS_STATUS_MASK & node_p->flags;
4208 cs->sp = node_p;
4209
Radek Krejcib1b59152019-01-07 13:21:56 +01004210 /* 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 +02004211 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004212
4213 if (node_p->when) {
4214 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004215 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004216 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004217
4218 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4219 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004220 ly_set_add(&ctx->xpath, cs, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004221 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004222 }
Radek Krejciec4da802019-05-02 13:02:41 +02004223 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004224 } else {
4225 LOGINT(ctx->ctx);
4226 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004227 }
4228 cs->module = ctx->mod;
4229 cs->prev = (struct lysc_node*)cs;
4230 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004231 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004232 cs->child = child;
4233
4234 return cs;
4235error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004236 if (cs) {
4237 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4238 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004239 return NULL;
4240
4241#undef UNIQUE_CHECK
4242}
4243
Radek Krejcib56c7502019-02-13 14:19:54 +01004244/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004245 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004246 *
4247 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004248 * @param[in] node Target node where the config is supposed to be changed.
4249 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004250 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4251 * 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 +01004252 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4253 * @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 +01004254 * @return LY_ERR value.
4255 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004256static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004257lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004258 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004259{
4260 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004261 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004262
4263 if (config == (node->flags & LYS_CONFIG_MASK)) {
4264 /* nothing to do */
4265 return LY_SUCCESS;
4266 }
4267
4268 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004269 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004270 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4271 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004272 "Invalid %s of config - configuration node cannot be child of any state data node.",
4273 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004274 return LY_EVALID;
4275 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004276 node->flags |= LYS_SET_CONFIG;
4277 } else {
4278 if (node->flags & LYS_SET_CONFIG) {
4279 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4280 /* setting config flags, but have node with explicit config true */
4281 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004282 "Invalid %s of config - configuration node cannot be child of any state data node.",
4283 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004284 return LY_EVALID;
4285 }
4286 /* do not change config on nodes where the config is explicitely set, this does not apply to
4287 * nodes, which are being changed explicitly (targets of refine or deviation) */
4288 return LY_SUCCESS;
4289 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004290 }
4291 node->flags &= ~LYS_CONFIG_MASK;
4292 node->flags |= config;
4293
4294 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004295 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004296 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004297 }
4298
Radek Krejci76b3e962018-12-14 17:01:25 +01004299 return LY_SUCCESS;
4300}
4301
Radek Krejcib56c7502019-02-13 14:19:54 +01004302/**
4303 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4304 *
4305 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4306 * the flag to such parents from a mandatory children.
4307 *
4308 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4309 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4310 * (mandatory children was removed).
4311 */
Radek Krejcife909632019-02-12 15:34:42 +01004312void
4313lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4314{
4315 struct lysc_node *iter;
4316
4317 if (add) { /* set flag */
4318 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4319 parent = parent->parent) {
4320 parent->flags |= LYS_MAND_TRUE;
4321 }
4322 } else { /* unset flag */
4323 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004324 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004325 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004326 /* there is another mandatory node */
4327 return;
4328 }
4329 }
4330 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4331 parent->flags &= ~LYS_MAND_TRUE;
4332 }
4333 }
4334}
4335
Radek Krejci056d0a82018-12-06 16:57:25 +01004336/**
Radek Krejci3641f562019-02-13 15:38:40 +01004337 * @brief Internal sorting process for the lys_compile_augment_sort().
4338 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4339 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4340 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4341 */
4342static void
4343lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4344{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004345 LY_ARRAY_COUNT_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004346 size_t len;
4347
4348 len = strlen(aug_p->nodeid);
4349 LY_ARRAY_FOR(result, v) {
4350 if (strlen(result[v]->nodeid) <= len) {
4351 continue;
4352 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004353 if (v < LY_ARRAY_COUNT(result)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004354 /* move the rest of array */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004355 memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result);
Radek Krejci3641f562019-02-13 15:38:40 +01004356 break;
4357 }
4358 }
4359 result[v] = aug_p;
4360 LY_ARRAY_INCREMENT(result);
4361}
4362
4363/**
4364 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4365 *
4366 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4367 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4368 *
4369 * @param[in] ctx Compile context.
4370 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4371 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4372 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4373 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4374 * @return LY_ERR value.
4375 */
4376LY_ERR
4377lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4378{
4379 struct lysp_augment **result = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004380 LY_ARRAY_COUNT_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004381
4382 assert(augments);
4383
4384 /* get count of the augments in module and all its submodules */
4385 if (aug_p) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004386 count += LY_ARRAY_COUNT(aug_p);
Radek Krejci3641f562019-02-13 15:38:40 +01004387 }
4388 LY_ARRAY_FOR(inc_p, u) {
4389 if (inc_p[u].submodule->augments) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004390 count += LY_ARRAY_COUNT(inc_p[u].submodule->augments);
Radek Krejci3641f562019-02-13 15:38:40 +01004391 }
4392 }
4393
4394 if (!count) {
4395 *augments = NULL;
4396 return LY_SUCCESS;
4397 }
4398 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4399
4400 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4401 * together, so there can be even /z/y betwwen them. */
4402 LY_ARRAY_FOR(aug_p, u) {
4403 lys_compile_augment_sort_(&aug_p[u], result);
4404 }
4405 LY_ARRAY_FOR(inc_p, u) {
4406 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4407 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4408 }
4409 }
4410
4411 *augments = result;
4412 return LY_SUCCESS;
4413}
4414
4415/**
4416 * @brief Compile the parsed augment connecting it into its target.
4417 *
4418 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4419 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4420 * are already implemented and compiled.
4421 *
4422 * @param[in] ctx Compile context.
4423 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004424 * @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
4425 * children in case of the augmenting uses data.
4426 * @return LY_SUCCESS on success.
4427 * @return LY_EVALID on failure.
4428 */
4429LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004430lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004431{
Michal Vaskoe6143202020-07-03 13:02:08 +02004432 LY_ERR ret = LY_SUCCESS, rc;
Radek Krejci3641f562019-02-13 15:38:40 +01004433 struct lysp_node *node_p, *case_node_p;
4434 struct lysc_node *target; /* target target of the augment */
4435 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004436 struct lysc_when **when, *when_shared;
Michal Vaskoa3af5982020-06-29 11:51:37 +02004437 struct lys_module **aug_mod;
Radek Krejci3641f562019-02-13 15:38:40 +01004438 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004439 uint16_t flags = 0;
Michal Vaskoe6143202020-07-03 13:02:08 +02004440 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejciec4da802019-05-02 13:02:41 +02004441 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004442
Radek Krejci327de162019-06-14 12:52:07 +02004443 lysc_update_path(ctx, NULL, "{augment}");
4444 lysc_update_path(ctx, NULL, aug_p->nodeid);
4445
Radek Krejci7af64242019-02-18 13:07:53 +01004446 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004447 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004448 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004449 if (ret != LY_SUCCESS) {
4450 if (ret == LY_EDENIED) {
4451 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4452 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4453 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4454 }
4455 return LY_EVALID;
4456 }
4457
4458 /* check for mandatory nodes
4459 * - new cases augmenting some choice can have mandatory nodes
4460 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4461 */
Radek Krejci733988a2019-02-15 15:12:44 +01004462 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004463 allow_mandatory = 1;
4464 }
4465
4466 when_shared = NULL;
4467 LY_LIST_FOR(aug_p->child, node_p) {
4468 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4469 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004470 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004471 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4472 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004473 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004474 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4475 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004476 return LY_EVALID;
4477 }
4478
4479 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004480 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004481 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004482 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004483 } else {
4484 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004485 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004486 }
4487 }
Radek Krejciec4da802019-05-02 13:02:41 +02004488 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004489
Radek Krejcife13da42019-02-15 14:51:01 +01004490 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4491 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004492 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004493 /* 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 +01004494 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 +01004495 } else if (target->nodetype == LYS_CHOICE) {
4496 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4497 node = ((struct lysc_node_choice*)target)->cases->prev;
4498 } else {
4499 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004500 node = (struct lysc_node*)lysc_node_children(target, flags);
4501 if (!node) {
4502 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4503 break;
4504 }
4505 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004506 }
4507
Radek Krejci733988a2019-02-15 15:12:44 +01004508 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004509 node->flags &= ~LYS_MAND_TRUE;
4510 lys_compile_mandatory_parents(target, 0);
4511 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004512 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004513 return LY_EVALID;
4514 }
4515
4516 /* pass augment's when to all the children */
4517 if (aug_p->when) {
4518 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4519 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004520 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004521 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004522
4523 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4524 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004525 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004526 }
4527
Radek Krejci3641f562019-02-13 15:38:40 +01004528 when_shared = *when;
4529 } else {
4530 ++when_shared->refcount;
4531 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004532
4533 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4534 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004535 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004536 }
Radek Krejci3641f562019-02-13 15:38:40 +01004537 }
4538 }
4539 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004540
Radek Krejciec4da802019-05-02 13:02:41 +02004541 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004542 switch (target->nodetype) {
4543 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004544 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004545 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004546 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004547 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004548 break;
4549 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004550 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004551 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004552 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004553 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004554 break;
4555 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004556 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004557 if (aug_p->actions) {
4558 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004559 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4560 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004561 return LY_EVALID;
4562 }
4563 if (aug_p->notifs) {
4564 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004565 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004566 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004567 return LY_EVALID;
4568 }
4569 }
Radek Krejci3641f562019-02-13 15:38:40 +01004570
Michal Vaskoe6143202020-07-03 13:02:08 +02004571 /* add this module into the target module augmented_by, if not there already */
4572 rc = LY_SUCCESS;
4573 LY_ARRAY_FOR(target->module->compiled->augmented_by, v) {
4574 if (target->module->compiled->augmented_by[v] == ctx->mod) {
4575 rc = LY_EEXIST;
4576 break;
4577 }
4578 }
4579 if (!rc) {
4580 LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error);
4581 *aug_mod = ctx->mod;
4582 }
Michal Vaskoa3af5982020-06-29 11:51:37 +02004583
Radek Krejci327de162019-06-14 12:52:07 +02004584 lysc_update_path(ctx, NULL, NULL);
4585 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa3af5982020-06-29 11:51:37 +02004586
Radek Krejci3641f562019-02-13 15:38:40 +01004587error:
Radek Krejciec4da802019-05-02 13:02:41 +02004588 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004589 return ret;
4590}
4591
4592/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004593 * @brief Apply refined or deviated mandatory flag to the target node.
4594 *
4595 * @param[in] ctx Compile context.
4596 * @param[in] node Target node where the mandatory property is supposed to be changed.
4597 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004598 * @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 +01004599 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4600 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4601 * 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 +01004602 * @return LY_ERR value.
4603 */
4604static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004605lys_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 +01004606{
4607 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4608 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004609 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4610 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004611 return LY_EVALID;
4612 }
4613
4614 if (mandatory_flag & LYS_MAND_TRUE) {
4615 /* check if node has default value */
4616 if (node->nodetype & LYS_LEAF) {
4617 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004618 if (refine_flag) {
4619 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004620 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004621 return LY_EVALID;
4622 }
Radek Krejcia1911222019-07-22 17:24:50 +02004623 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004624 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004625 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004626
4627 /* update the list of incomplete default values if needed */
4628 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4629
Radek Krejcia1911222019-07-22 17:24:50 +02004630 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4631 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4632 free(leaf->dflt);
4633 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004634 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004635 }
4636 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004637 if (refine_flag) {
4638 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004639 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004640 return LY_EVALID;
4641 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004642 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004643 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004644 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 +01004645 return LY_EVALID;
4646 }
4647
4648 node->flags &= ~LYS_MAND_FALSE;
4649 node->flags |= LYS_MAND_TRUE;
4650 lys_compile_mandatory_parents(node->parent, 1);
4651 } else {
4652 /* make mandatory false */
4653 node->flags &= ~LYS_MAND_TRUE;
4654 node->flags |= LYS_MAND_FALSE;
4655 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004656 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 +01004657 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004658 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004659 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004660 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4661 leaf->dflt->realtype = leaf->type->dflt->realtype;
4662 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4663 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004664 }
4665 }
4666 return LY_SUCCESS;
4667}
4668
4669/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004670 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4671 * If present, also apply uses's modificators.
4672 *
4673 * @param[in] ctx Compile context
4674 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004675 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4676 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4677 * the compile context.
4678 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4679 */
4680static LY_ERR
Radek Krejcic6b4f442020-08-12 14:45:18 +02004681lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p)
Radek Krejcie86bf772018-12-14 11:39:53 +01004682{
4683 struct lysp_node *node_p;
Radek Krejcic6b4f442020-08-12 14:45:18 +02004684 struct lysc_node *node, *child, *iter;
Radek Krejci12fb9142019-01-08 09:45:30 +01004685 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004686 struct lysc_node_container context_node_fake =
4687 {.nodetype = LYS_CONTAINER,
4688 .module = ctx->mod,
4689 .flags = parent ? parent->flags : 0,
4690 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004691 .prev = (struct lysc_node*)&context_node_fake,
4692 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004693 struct lysp_grp *grp = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004694 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004695 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004696 int found;
4697 const char *id, *name, *prefix;
4698 size_t prefix_len, name_len;
4699 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004700 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004701 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004702 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004703 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004704 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004705 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004706 struct lysp_augment **augments = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004707 LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004708 struct lysc_notif **notifs = NULL;
4709 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004710
4711 /* search for the grouping definition */
4712 found = 0;
4713 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004714 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004715 if (prefix) {
4716 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4717 if (!mod) {
4718 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004719 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004720 return LY_EVALID;
4721 }
4722 } else {
4723 mod = ctx->mod_def;
4724 }
4725 if (mod == ctx->mod_def) {
4726 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004727 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004728 LY_ARRAY_FOR(grp, u) {
4729 if (!strcmp(grp[u].name, name)) {
4730 grp = &grp[u];
4731 found = 1;
4732 break;
4733 }
4734 }
4735 }
4736 }
4737 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004738 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004739 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004740 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004741 for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004742 if (!strcmp(grp[u].name, name)) {
4743 grp = &grp[u];
4744 found = 1;
4745 }
4746 }
4747 }
4748 if (!found && mod->parsed->includes) {
4749 /* ... and all the submodules */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004750 for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004751 grp = mod->parsed->includes[u].submodule->groupings;
4752 if (grp) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004753 for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004754 if (!strcmp(grp[v].name, name)) {
4755 grp = &grp[v];
4756 found = 1;
4757 }
4758 }
4759 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004760 }
4761 }
4762 }
4763 if (!found) {
4764 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4765 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4766 return LY_EVALID;
4767 }
4768
4769 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4770 grp_stack_count = ctx->groupings.count;
4771 ly_set_add(&ctx->groupings, (void*)grp, 0);
4772 if (grp_stack_count == ctx->groupings.count) {
4773 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4774 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4775 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4776 return LY_EVALID;
4777 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004778 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4779 /* remember that the grouping is instantiated to avoid its standalone validation */
4780 grp->flags |= LYS_USED_GRP;
4781 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004782
4783 /* switch context's mod_def */
4784 mod_old = ctx->mod_def;
4785 ctx->mod_def = mod;
4786
4787 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004788 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 +01004789
Radek Krejcic6b4f442020-08-12 14:45:18 +02004790 /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly
4791 * applu refine and augment only to the nodes from the uses */
4792 if (parent) {
4793 if (parent->nodetype == LYS_CASE) {
4794 child = (struct lysc_node*)lysc_node_children(parent->parent, ctx->options & LYSC_OPT_RPC_MASK);
4795 } else {
4796 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4797 }
4798 } else if (ctx->mod->compiled->data) {
4799 child = ctx->mod->compiled->data;
4800 } else {
4801 child = NULL;
4802 }
4803 /* remember the last child */
4804 if (child) {
4805 child = child->prev;
4806 }
4807
Radek Krejcifc11bd72019-04-11 16:00:05 +02004808 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004809 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004810 /* 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 +02004811 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01004812 }
Radek Krejcic6b4f442020-08-12 14:45:18 +02004813
4814 /* split the children and add the uses's data into the fake context node */
4815 if (child) {
4816 context_node_fake.child = child->next;
4817 } else if (parent) {
4818 context_node_fake.child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4819 } else if (ctx->mod->compiled->data) {
4820 context_node_fake.child = ctx->mod->compiled->data;
4821 }
4822 if (context_node_fake.child) {
4823 /* remember child as the last data node added by grouping to fix the list later */
4824 child = context_node_fake.child->prev;
4825 context_node_fake.child->prev = NULL;
4826 }
4827
Radek Krejci00b874b2019-02-12 10:54:50 +01004828 when_shared = NULL;
Radek Krejcic6b4f442020-08-12 14:45:18 +02004829 LY_LIST_FOR(context_node_fake.child, iter) {
4830 iter->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004831
Radek Krejcifc11bd72019-04-11 16:00:05 +02004832 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004833 if (uses_p->when) {
Radek Krejcic6b4f442020-08-12 14:45:18 +02004834 LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004835 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004836 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4837
4838 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4839 /* do not check "when" semantics in a grouping */
Radek Krejcic6b4f442020-08-12 14:45:18 +02004840 ly_set_add(&ctx->xpath, iter, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004841 }
4842
Radek Krejci00b874b2019-02-12 10:54:50 +01004843 when_shared = *when;
4844 } else {
4845 ++when_shared->refcount;
4846 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004847
4848 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4849 /* in this case check "when" again for all children because of dummy node check */
Radek Krejcic6b4f442020-08-12 14:45:18 +02004850 ly_set_add(&ctx->xpath, iter, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004851 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004852 }
4853 }
Radek Krejci01342af2019-01-03 15:18:08 +01004854 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004855
Radek Krejcifc11bd72019-04-11 16:00:05 +02004856 /* compile actions */
4857 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4858 if (actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004859 actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004860 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004861 if (*actions && (uses_p->augments || uses_p->refines)) {
4862 /* 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 +02004863 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup);
4864 LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index;
4865 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004866 }
4867 }
4868
4869 /* compile notifications */
4870 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4871 if (notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004872 notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004873 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004874 if (*notifs && (uses_p->augments || uses_p->refines)) {
4875 /* 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 +02004876 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup);
4877 LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index;
4878 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004879 }
4880 }
4881
4882
Radek Krejci3641f562019-02-13 15:38:40 +01004883 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004884 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004885 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004886 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004887 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004888
Radek Krejcif0089082019-01-07 16:42:01 +01004889 /* reload previous context's mod_def */
4890 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004891 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004892
Radek Krejci76b3e962018-12-14 17:01:25 +01004893 /* apply refine */
4894 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004895 lysc_update_path(ctx, NULL, rfn->nodeid);
4896
Radek Krejci7af64242019-02-18 13:07:53 +01004897 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 +01004898 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004899 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004900 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004901
4902 /* default value */
4903 if (rfn->dflts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004904 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004905 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004906 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.",
4907 lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004908 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004909 }
4910 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004912 "Invalid refine of default - %s cannot hold default value(s).",
4913 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004914 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004915 }
4916 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004917 struct ly_err_item *err = NULL;
4918 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4919 if (leaf->dflt) {
4920 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004921 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004922 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4923 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4924 } else {
4925 /* prepare a new one */
4926 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4927 leaf->dflt->realtype = leaf->type;
4928 }
4929 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004930 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004931 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4932 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004933 LY_PREF_SCHEMA, leaf->dflt_mod, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004934 leaf->dflt->realtype->refcount++;
4935 if (err) {
4936 ly_err_print(err);
4937 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4938 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4939 ly_err_free(err);
4940 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004941 if (rc == LY_EINCOMPLETE) {
4942 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004943 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004944
4945 /* but in general result is so far ok */
4946 rc = LY_SUCCESS;
4947 }
Radek Krejcia1911222019-07-22 17:24:50 +02004948 LY_CHECK_GOTO(rc, cleanup);
4949 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004950 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004951 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4952
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004953 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004954 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4955 "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 +02004956 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004957 }
Radek Krejcia1911222019-07-22 17:24:50 +02004958
4959 /* remove previous set of default values */
4960 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004961 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004962 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4963 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4964 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004965 }
Radek Krejcia1911222019-07-22 17:24:50 +02004966 LY_ARRAY_FREE(llist->dflts);
4967 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004968 LY_ARRAY_FREE(llist->dflts_mods);
4969 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004970
4971 /* create the new set of the default values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004972 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
4973 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004974 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004975 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004976 LY_ARRAY_INCREMENT(llist->dflts_mods);
4977 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004978 LY_ARRAY_INCREMENT(llist->dflts);
4979 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4980 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004981 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004982 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
4983 LY_PREF_SCHEMA, llist->dflts_mods[u], node, NULL, llist->dflts[u],
4984 NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004985 llist->dflts[u]->realtype->refcount++;
4986 if (err) {
4987 ly_err_print(err);
4988 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4989 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4990 ly_err_free(err);
4991 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004992 if (rc == LY_EINCOMPLETE) {
4993 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004994 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004995
4996 /* but in general result is so far ok */
4997 rc = LY_SUCCESS;
4998 }
4999 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005000 }
Radek Krejcia1911222019-07-22 17:24:50 +02005001 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005002 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01005003 if (((struct lysc_node_choice*)node)->dflt) {
5004 /* unset LYS_SET_DFLT from the current default case */
5005 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
5006 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005007 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 +01005008 }
5009 }
5010
Radek Krejci12fb9142019-01-08 09:45:30 +01005011 /* description */
5012 if (rfn->dsc) {
5013 FREE_STRING(ctx->ctx, node->dsc);
5014 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
5015 }
5016
5017 /* reference */
5018 if (rfn->ref) {
5019 FREE_STRING(ctx->ctx, node->ref);
5020 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
5021 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005022
5023 /* config */
5024 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005025 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005026 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005027 } else {
5028 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01005029 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005030 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005031 }
5032
5033 /* mandatory */
5034 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02005035 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005036 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005037
5038 /* presence */
5039 if (rfn->presence) {
5040 if (node->nodetype != LYS_CONTAINER) {
5041 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005042 "Invalid refine of presence statement - %s cannot hold the presence statement.",
5043 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005044 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005045 }
5046 node->flags |= LYS_PRESENCE;
5047 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005048
5049 /* must */
5050 if (rfn->musts) {
5051 switch (node->nodetype) {
5052 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005053 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 +01005054 break;
5055 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005056 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 +01005057 break;
5058 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005059 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 +01005060 break;
5061 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005062 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 +01005063 break;
5064 case LYS_ANYXML:
5065 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005066 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 +01005067 break;
5068 default:
5069 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005070 "Invalid refine of must statement - %s cannot hold any must statement.",
5071 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005072 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005073 }
Michal Vasko004d3152020-06-11 19:59:22 +02005074 ly_set_add(&ctx->xpath, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01005075 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005076
5077 /* min/max-elements */
5078 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5079 switch (node->nodetype) {
5080 case LYS_LEAFLIST:
5081 if (rfn->flags & LYS_SET_MAX) {
5082 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5083 }
5084 if (rfn->flags & LYS_SET_MIN) {
5085 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005086 if (rfn->min) {
5087 node->flags |= LYS_MAND_TRUE;
5088 lys_compile_mandatory_parents(node->parent, 1);
5089 } else {
5090 node->flags &= ~LYS_MAND_TRUE;
5091 lys_compile_mandatory_parents(node->parent, 0);
5092 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005093 }
5094 break;
5095 case LYS_LIST:
5096 if (rfn->flags & LYS_SET_MAX) {
5097 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5098 }
5099 if (rfn->flags & LYS_SET_MIN) {
5100 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005101 if (rfn->min) {
5102 node->flags |= LYS_MAND_TRUE;
5103 lys_compile_mandatory_parents(node->parent, 1);
5104 } else {
5105 node->flags &= ~LYS_MAND_TRUE;
5106 lys_compile_mandatory_parents(node->parent, 0);
5107 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005108 }
5109 break;
5110 default:
5111 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005112 "Invalid refine of %s statement - %s cannot hold this statement.",
5113 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005114 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005115 }
5116 }
Radek Krejcif0089082019-01-07 16:42:01 +01005117
5118 /* if-feature */
5119 if (rfn->iffeatures) {
5120 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005121 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005122 }
Radek Krejci327de162019-06-14 12:52:07 +02005123
5124 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005125 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005126
Radek Krejcif2271f12019-01-07 16:42:23 +01005127 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005128 for (uint32_t i = 0; i < refined.count; ++i) {
5129 node = (struct lysc_node*)refined.objs[i];
5130 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02005131 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005132
5133 /* check possible conflict with default value (default added, mandatory left true) */
5134 if ((node->flags & LYS_MAND_TRUE) &&
5135 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5136 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5137 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005138 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005139 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005140 }
5141
5142 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5143 if (node->nodetype == LYS_LIST) {
5144 min = ((struct lysc_node_list*)node)->min;
5145 max = ((struct lysc_node_list*)node)->max;
5146 } else {
5147 min = ((struct lysc_node_leaflist*)node)->min;
5148 max = ((struct lysc_node_leaflist*)node)->max;
5149 }
5150 if (min > max) {
5151 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005152 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5153 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005154 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005155 }
5156 }
5157 }
5158
Radek Krejcic6b4f442020-08-12 14:45:18 +02005159 if (first_p) {
5160 *first_p = context_node_fake.child;
5161 }
Radek Krejci327de162019-06-14 12:52:07 +02005162 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005163 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005164
5165cleanup:
5166 /* fix connection of the children nodes from fake context node back into the parent */
5167 if (context_node_fake.child) {
5168 context_node_fake.child->prev = child;
5169 }
5170 LY_LIST_FOR(context_node_fake.child, child) {
5171 child->parent = parent;
5172 }
5173
5174 if (uses_p->augments || uses_p->refines) {
5175 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005176 if (context_node_fake.actions) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005177 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005178 LY_ARRAY_FREE(context_node_fake.actions);
5179 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005180 if (context_node_fake.notifs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02005181 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005182 LY_ARRAY_FREE(context_node_fake.notifs);
5183 }
5184 }
5185
Radek Krejcie86bf772018-12-14 11:39:53 +01005186 /* reload previous context's mod_def */
5187 ctx->mod_def = mod_old;
5188 /* remove the grouping from the stack for circular groupings dependency check */
5189 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5190 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005191 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005192 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005193
5194 return ret;
5195}
5196
Radek Krejci327de162019-06-14 12:52:07 +02005197static int
5198lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5199{
5200 struct lysp_node *iter;
5201 int len = 0;
5202
5203 *path = NULL;
5204 for (iter = node; iter && len >= 0; iter = iter->parent) {
5205 char *s = *path;
5206 char *id;
5207
5208 switch (iter->nodetype) {
5209 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02005210 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005211 break;
5212 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02005213 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005214 break;
5215 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02005216 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02005217 break;
5218 default:
5219 id = strdup(iter->name);
5220 break;
5221 }
5222
5223 if (!iter->parent) {
5224 /* print prefix */
5225 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5226 } else {
5227 /* prefix is the same as in parent */
5228 len = asprintf(path, "/%s%s", id, s ? s : "");
5229 }
5230 free(s);
5231 free(id);
5232 }
5233
5234 if (len < 0) {
5235 free(*path);
5236 *path = NULL;
5237 } else if (len == 0) {
5238 *path = strdup("/");
5239 len = 1;
5240 }
5241 return len;
5242}
5243
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005244/**
5245 * @brief Validate groupings that were defined but not directly used in the schema itself.
5246 *
5247 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5248 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5249 */
5250static LY_ERR
5251lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5252{
5253 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005254 char *path;
5255 int len;
5256
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005257 struct lysp_node_uses fake_uses = {
5258 .parent = node_p,
5259 .nodetype = LYS_USES,
5260 .flags = 0, .next = NULL,
5261 .name = grp->name,
5262 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5263 .refines = NULL, .augments = NULL
5264 };
5265 struct lysc_node_container fake_container = {
5266 .nodetype = LYS_CONTAINER,
5267 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5268 .module = ctx->mod,
5269 .sp = NULL, .parent = NULL, .next = NULL,
5270 .prev = (struct lysc_node*)&fake_container,
5271 .name = "fake",
5272 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5273 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5274 };
5275
5276 if (grp->parent) {
5277 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5278 }
Radek Krejci327de162019-06-14 12:52:07 +02005279
5280 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5281 if (len < 0) {
5282 LOGMEM(ctx->ctx);
5283 return LY_EMEM;
5284 }
5285 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5286 ctx->path_len = (uint16_t)len;
5287 free(path);
5288
5289 lysc_update_path(ctx, NULL, "{grouping}");
5290 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcic6b4f442020-08-12 14:45:18 +02005291 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02005292 lysc_update_path(ctx, NULL, NULL);
5293 lysc_update_path(ctx, NULL, NULL);
5294
5295 ctx->path_len = 1;
5296 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005297
5298 /* cleanup */
5299 lysc_node_container_free(ctx->ctx, &fake_container);
5300
5301 return ret;
5302}
Radek Krejcife909632019-02-12 15:34:42 +01005303
Radek Krejcie86bf772018-12-14 11:39:53 +01005304/**
Radek Krejcia3045382018-11-22 14:30:31 +01005305 * @brief Compile parsed schema node information.
5306 * @param[in] ctx Compile context
5307 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005308 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5309 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5310 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005311 * @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).
5312 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005313 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5314 */
Radek Krejci19a96102018-11-15 13:38:09 +01005315static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005316lys_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 +01005317{
Radek Krejci1c54f462020-05-12 17:25:34 +02005318 LY_ERR ret = LY_SUCCESS;
Radek Krejcic6b4f442020-08-12 14:45:18 +02005319 struct lysc_node *node = NULL;
5320 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01005321 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005322 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005323 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005324
Radek Krejci327de162019-06-14 12:52:07 +02005325 if (node_p->nodetype != LYS_USES) {
5326 lysc_update_path(ctx, parent, node_p->name);
5327 } else {
5328 lysc_update_path(ctx, NULL, "{uses}");
5329 lysc_update_path(ctx, NULL, node_p->name);
5330 }
5331
Radek Krejci19a96102018-11-15 13:38:09 +01005332 switch (node_p->nodetype) {
5333 case LYS_CONTAINER:
5334 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5335 node_compile_spec = lys_compile_node_container;
5336 break;
5337 case LYS_LEAF:
5338 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5339 node_compile_spec = lys_compile_node_leaf;
5340 break;
5341 case LYS_LIST:
5342 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005343 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005344 break;
5345 case LYS_LEAFLIST:
5346 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005347 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005348 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005349 case LYS_CHOICE:
5350 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005351 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005352 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005353 case LYS_ANYXML:
5354 case LYS_ANYDATA:
5355 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005356 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005357 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005358 case LYS_USES:
Radek Krejcic6b4f442020-08-12 14:45:18 +02005359 if (parent && parent->nodetype == LYS_CHOICE) {
5360 assert(node_p->parent->nodetype == LYS_CASE);
5361 lysc_update_path(ctx, parent, node_p->parent->name);
5362 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, NULL);
5363 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
5364 }
5365
5366 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, cs ? (struct lysc_node*)cs : parent, &node);
5367 if (cs) {
5368 cs->child = node;
5369 lysc_update_path(ctx, NULL, NULL);
5370 }
Radek Krejci327de162019-06-14 12:52:07 +02005371 lysc_update_path(ctx, NULL, NULL);
5372 lysc_update_path(ctx, NULL, NULL);
5373 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005374 default:
5375 LOGINT(ctx->ctx);
5376 return LY_EINT;
5377 }
5378 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5379 node->nodetype = node_p->nodetype;
5380 node->module = ctx->mod;
5381 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005382 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005383
5384 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005385 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005386 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005387 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005388 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5389 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005390 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005391 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005392 node->flags |= LYS_CONFIG_R;
5393 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005394 /* config not explicitely set, inherit it from parent */
5395 if (parent) {
5396 node->flags |= parent->flags & LYS_CONFIG_MASK;
5397 } else {
5398 /* default is config true */
5399 node->flags |= LYS_CONFIG_W;
5400 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005401 } else {
5402 /* config set explicitely */
5403 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005404 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005405 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5406 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5407 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005408 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005409 goto error;
5410 }
Radek Krejci19a96102018-11-15 13:38:09 +01005411
Radek Krejcia6d57732018-11-29 13:40:37 +01005412 /* *list ordering */
5413 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5414 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005415 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005416 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5417 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005418 node->flags &= ~LYS_ORDBY_MASK;
5419 node->flags |= LYS_ORDBY_SYSTEM;
5420 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5421 /* default ordering is system */
5422 node->flags |= LYS_ORDBY_SYSTEM;
5423 }
5424 }
5425
Radek Krejci19a96102018-11-15 13:38:09 +01005426 /* status - it is not inherited by specification, but it does not make sense to have
5427 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005428 if (!parent || parent->nodetype != LYS_CHOICE) {
5429 /* in case of choice/case's children, postpone the check to the moment we know if
5430 * 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 +02005431 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 +01005432 }
5433
Radek Krejciec4da802019-05-02 13:02:41 +02005434 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005435 node->sp = node_p;
5436 }
5437 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005438 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5439 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005440 if (node_p->when) {
5441 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005442 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005443
5444 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5445 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02005446 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01005447 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005448 }
Radek Krejciec4da802019-05-02 13:02:41 +02005449 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005450
5451 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005452 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005453
Radek Krejci0935f412019-08-20 16:15:18 +02005454 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5455
Radek Krejcife909632019-02-12 15:34:42 +01005456 /* inherit LYS_MAND_TRUE in parent containers */
5457 if (node->flags & LYS_MAND_TRUE) {
5458 lys_compile_mandatory_parents(parent, 1);
5459 }
5460
Radek Krejci327de162019-06-14 12:52:07 +02005461 lysc_update_path(ctx, NULL, NULL);
5462
Radek Krejci19a96102018-11-15 13:38:09 +01005463 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005464 if (parent) {
5465 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005466 if (node_p->parent->nodetype == LYS_CASE) {
5467 lysc_update_path(ctx, parent, node_p->parent->name);
5468 } else {
5469 lysc_update_path(ctx, parent, node->name);
5470 }
Radek Krejciec4da802019-05-02 13:02:41 +02005471 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005472 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005473 if (uses_status) {
5474
5475 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005476 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005477 * it directly gets the same status flags as the choice;
5478 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005479 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005480 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005481 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005482 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005483 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005484 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005485 }
Radek Krejciec4da802019-05-02 13:02:41 +02005486 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 +02005487
5488 if (parent->nodetype == LYS_CHOICE) {
5489 lysc_update_path(ctx, NULL, NULL);
5490 }
Radek Krejci19a96102018-11-15 13:38:09 +01005491 } else {
5492 /* top-level element */
5493 if (!ctx->mod->compiled->data) {
5494 ctx->mod->compiled->data = node;
5495 } else {
5496 /* insert at the end of the module's top-level nodes list */
5497 ctx->mod->compiled->data->prev->next = node;
5498 node->prev = ctx->mod->compiled->data->prev;
5499 ctx->mod->compiled->data->prev = node;
5500 }
Radek Krejci327de162019-06-14 12:52:07 +02005501 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005502 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5503 ctx->mod->compiled->notifs, node->name, node)) {
5504 return LY_EVALID;
5505 }
Radek Krejci19a96102018-11-15 13:38:09 +01005506 }
Radek Krejci327de162019-06-14 12:52:07 +02005507 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005508
5509 return LY_SUCCESS;
5510
5511error:
5512 lysc_node_free(ctx->ctx, node);
5513 return ret;
5514}
5515
Radek Krejciccd20f12019-02-15 14:12:27 +01005516static void
5517lysc_disconnect(struct lysc_node *node)
5518{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005519 struct lysc_node *parent, *child, *prev = NULL, *next;
5520 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005521 int remove_cs = 0;
5522
5523 parent = node->parent;
5524
5525 /* parent's first child */
5526 if (!parent) {
5527 return;
5528 }
5529 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005530 cs = (struct lysc_node_case*)node;
5531 } else if (parent->nodetype == LYS_CASE) {
5532 /* disconnecting some node in a case */
5533 cs = (struct lysc_node_case*)parent;
5534 parent = cs->parent;
5535 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5536 if (child == node) {
5537 if (cs->child == child) {
5538 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5539 /* case with a single child -> remove also the case */
5540 child->parent = NULL;
5541 remove_cs = 1;
5542 } else {
5543 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005544 }
5545 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005546 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005547 }
5548 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005549 if (!remove_cs) {
5550 cs = NULL;
5551 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005552 } else if (lysc_node_children(parent, node->flags) == node) {
5553 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005554 }
5555
5556 if (cs) {
5557 if (remove_cs) {
5558 /* cs has only one child which is being also removed */
5559 lysc_disconnect((struct lysc_node*)cs);
5560 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5561 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005562 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5563 /* default case removed */
5564 ((struct lysc_node_choice*)parent)->dflt = NULL;
5565 }
5566 if (((struct lysc_node_choice*)parent)->cases == cs) {
5567 /* first case removed */
5568 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5569 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005570 if (cs->child) {
5571 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5572 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5573 prev = cs->child->prev;
5574 } /* else all the children are under a single case */
5575 LY_LIST_FOR_SAFE(cs->child, next, child) {
5576 if (child->parent != (struct lysc_node*)cs) {
5577 break;
5578 }
5579 lysc_node_free(node->module->ctx, child);
5580 }
5581 if (prev) {
5582 if (prev->next) {
5583 prev->next = child;
5584 }
5585 if (child) {
5586 child->prev = prev;
5587 } else {
5588 /* link from the first child under the cases */
5589 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5590 }
5591 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005592 }
5593 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005594 }
5595
5596 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005597 if (node->prev->next) {
5598 node->prev->next = node->next;
5599 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005600 if (node->next) {
5601 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005602 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005603 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005604 if (child) {
5605 child->prev = node->prev;
5606 }
5607 } else if (((struct lysc_node_choice*)parent)->cases) {
5608 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005609 }
5610}
5611
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02005612struct lysc_deviation {
5613 const char *nodeid;
5614 struct lysc_node *target; /* target node of the deviation */
5615 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
5616 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
5617 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5618};
5619
Michal Vaskoccc062a2020-08-13 08:34:50 +02005620/* MACROS for deviates checking */
5621#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5622 if (!(target->nodetype & (NODETYPES))) { \
5623 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\
5624 goto cleanup; \
5625 }
5626
5627#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5628 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5630 lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
5631 goto cleanup; \
5632 }
5633
5634#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5635 if (!((TYPE)target)->MEMBER || COND) { \
5636 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
5637 goto cleanup; \
5638 }
5639
5640/**
5641 * @brief Apply deviate add.
5642 *
5643 * @param[in] ctx Compile context.
5644 * @param[in] target Deviation target.
5645 * @param[in] dev_flags Internal deviation flags.
5646 * @param[in] d Deviate add to apply.
5647 * @param[out] dflt Added default value.
5648 * @return LY_ERR value.
5649 */
5650static LY_ERR
5651lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysc_node *target, int dev_flags, struct lysp_deviate_add *d,
5652 const char **dflt)
5653{
5654 LY_ERR ret = LY_EVALID, rc;
5655 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
5656 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
5657 struct ly_err_item *err = NULL;
5658 LY_ARRAY_COUNT_TYPE x, y;
5659
5660#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5661 if (((TYPE)target)->MEMBER COND) { \
5662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5663 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5664 PROPERTY, ((TYPE)target)->MEMBER); \
5665 goto cleanup; \
5666 }
5667
5668#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
5669 if (((TYPE)target)->MEMBER && (COND)) { \
5670 int dynamic_ = 0; const char *val_; \
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005671 val_ = ((TYPE)target)->VALUEMEMBER->realtype->plugin->print(((TYPE)target)->VALUEMEMBER, LY_PREF_SCHEMA, \
5672 ((TYPE)target)->VALUEMODMEMBER, &dynamic_); \
Michal Vaskoccc062a2020-08-13 08:34:50 +02005673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5674 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5675 if (dynamic_) {free((void*)val_);} \
5676 goto cleanup; \
5677 }
5678
5679#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
5680 if (((TYPE)target)->MEMBER && (COND)) { \
5681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5682 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5683 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
5684 goto cleanup; \
5685 }
5686
5687 /* [units-stmt] */
5688 if (d->units) {
5689 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5690 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units);
5691
5692 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units);
5693 DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units);
5694 }
5695
5696 /* *must-stmt */
5697 if (d->musts) {
5698 switch (target->nodetype) {
5699 case LYS_CONTAINER:
5700 case LYS_LIST:
5701 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts,
5702 x, lys_compile_must, ret, cleanup);
5703 break;
5704 case LYS_LEAF:
5705 case LYS_LEAFLIST:
5706 case LYS_ANYDATA:
5707 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts,
5708 x, lys_compile_must, ret, cleanup);
5709 break;
5710 case LYS_NOTIF:
5711 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts,
5712 x, lys_compile_must, ret, cleanup);
5713 break;
5714 case LYS_RPC:
5715 case LYS_ACTION:
5716 if (dev_flags & LYSC_OPT_RPC_INPUT) {
5717 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts,
5718 x, lys_compile_must, ret, cleanup);
5719 break;
5720 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
5721 COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts,
5722 x, lys_compile_must, ret, cleanup);
5723 break;
5724 }
5725 /* fall through */
5726 default:
5727 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5728 lys_nodetype2str(target->nodetype), "add", "must");
5729 goto cleanup;
5730 }
5731 ly_set_add(&ctx->xpath, target, 0);
5732 }
5733
5734 /* *unique-stmt */
5735 if (d->uniques) {
5736 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5737 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup);
5738 }
5739
5740 /* *default-stmt */
5741 if (d->dflts) {
5742 switch (target->nodetype) {
5743 case LYS_LEAF:
5744 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
5745 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf *, (target->flags & LYS_SET_DFLT), dflt, "default", dflt, dflt_mod);
5746 if (leaf->dflt) {
5747 /* first, remove the default value taken from the type */
5748 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
5749 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5750 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5751 } else {
5752 /* prepare new default value storage */
5753 leaf->dflt = calloc(1, sizeof *leaf->dflt);
5754 }
5755 *dflt = d->dflts[0];
5756 /* parsing is done at the end after possible replace of the leaf's type */
5757
5758 /* mark the new default values as leaf's own */
5759 target->flags |= LYS_SET_DFLT;
5760 break;
5761 case LYS_LEAFLIST:
5762 if (llist->dflts && !(target->flags & LYS_SET_DFLT)) {
5763 /* first, remove the default value taken from the type */
5764 LY_ARRAY_FOR(llist->dflts, x) {
5765 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
5766 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5767 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5768 free(llist->dflts[x]);
5769 }
5770 LY_ARRAY_FREE(llist->dflts);
5771 llist->dflts = NULL;
5772 LY_ARRAY_FREE(llist->dflts_mods);
5773 llist->dflts_mods = NULL;
5774 }
5775 /* add new default value(s) */
5776 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(d->dflts), ret, cleanup);
5777 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(d->dflts), ret, cleanup);
5778 for (x = y = LY_ARRAY_COUNT(llist->dflts);
5779 x < LY_ARRAY_COUNT(d->dflts) + y; ++x) {
5780 LY_ARRAY_INCREMENT(llist->dflts_mods);
5781 llist->dflts_mods[x] = ctx->mod_def;
5782 LY_ARRAY_INCREMENT(llist->dflts);
5783 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5784 llist->dflts[x]->realtype = llist->type;
5785 rc = llist->type->plugin->store(ctx->ctx, llist->type, d->dflts[x - y], strlen(d->dflts[x - y]),
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005786 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
5787 LY_PREF_SCHEMA, llist->dflts_mods[x], target, NULL, llist->dflts[x], NULL, &err);
Michal Vaskoccc062a2020-08-13 08:34:50 +02005788 llist->dflts[x]->realtype->refcount++;
5789 if (err) {
5790 ly_err_print(err);
5791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5792 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5793 d->dflts[x - y], err->msg);
5794 ly_err_free(err);
5795 }
5796 if (rc == LY_EINCOMPLETE) {
5797 /* postpone default compilation when the tree is complete */
5798 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5799
5800 /* but in general result is so far ok */
5801 rc = LY_SUCCESS;
5802 }
5803 LY_CHECK_GOTO(rc, cleanup);
5804 }
5805 /* mark the new default values as leaf-list's own */
5806 target->flags |= LYS_SET_DFLT;
5807 break;
5808 case LYS_CHOICE:
5809 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
5810 DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name);
5811 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5812 * to allow making the default case even the augmented case from the deviating module */
5813 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) {
5814 goto cleanup;
5815 }
5816 break;
5817 default:
5818 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5819 lys_nodetype2str(target->nodetype), "add", "default");
5820 goto cleanup;
5821 }
5822 }
5823
5824 /* [config-stmt] */
5825 if (d->flags & LYS_CONFIG_MASK) {
5826 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
5827 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5828 lys_nodetype2str(target->nodetype), "add", "config");
5829 goto cleanup;
5830 }
5831 if (dev_flags) {
5832 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5833 dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
5834 }
5835 if (target->flags & LYS_SET_CONFIG) {
5836 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5837 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5838 target->flags & LYS_CONFIG_W ? "true" : "false");
5839 goto cleanup;
5840 }
5841 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
5842 }
5843
5844 /* [mandatory-stmt] */
5845 if (d->flags & LYS_MAND_MASK) {
5846 if (target->flags & LYS_MAND_MASK) {
5847 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5848 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5849 target->flags & LYS_MAND_TRUE ? "true" : "false");
5850 goto cleanup;
5851 }
5852 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
5853 }
5854
5855 /* [min-elements-stmt] */
5856 if (d->flags & LYS_SET_MIN) {
5857 if (target->nodetype == LYS_LEAFLIST) {
5858 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
5859 /* change value */
5860 ((struct lysc_node_leaflist*)target)->min = d->min;
5861 } else if (target->nodetype == LYS_LIST) {
5862 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
5863 /* change value */
5864 ((struct lysc_node_list*)target)->min = d->min;
5865 } else {
5866 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5867 lys_nodetype2str(target->nodetype), "add", "min-elements");
5868 goto cleanup;
5869 }
5870 if (d->min) {
5871 target->flags |= LYS_MAND_TRUE;
5872 }
5873 }
5874
5875 /* [max-elements-stmt] */
5876 if (d->flags & LYS_SET_MAX) {
5877 if (target->nodetype == LYS_LEAFLIST) {
5878 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
5879 /* change value */
5880 ((struct lysc_node_leaflist*)target)->max = d->max ? d->max : (uint32_t)-1;
5881 } else if (target->nodetype == LYS_LIST) {
5882 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
5883 /* change value */
5884 ((struct lysc_node_list*)target)->max = d->max ? d->max : (uint32_t)-1;
5885 } else {
5886 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5887 lys_nodetype2str(target->nodetype), "add", "max-elements");
5888 goto cleanup;
5889 }
5890 }
5891
5892 ret = LY_SUCCESS;
5893
5894cleanup:
5895 return ret;
5896}
5897
5898/**
5899 * @brief Apply deviate delete.
5900 *
5901 * @param[in] ctx Compile context.
5902 * @param[in] target Deviation target.
5903 * @param[in] dev_flags Internal deviation flags.
5904 * @param[in] d Deviate delete to apply.
5905 * @return LY_ERR value.
5906 */
5907static LY_ERR
5908lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysc_node *target, int dev_flags, struct lysp_deviate_del *d)
5909{
5910 LY_ERR ret = LY_EVALID;
5911 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
5912 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
5913 struct lysc_node_list *list = (struct lysc_node_list *)target;
5914 LY_ARRAY_COUNT_TYPE x, y, z;
5915 int i;
5916 size_t prefix_len, name_len;
5917 const char *prefix, *name, *nodeid, *dflt;
5918 struct lys_module *mod;
5919
5920#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5921 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \
5922 LY_ARRAY_FOR(d->ARRAY_DEV, x) { \
5923 LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \
5924 if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \
5925 } \
5926 if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5927 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5928 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5929 PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \
5930 goto cleanup; \
5931 } \
5932 LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \
5933 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \
5934 memmove(&((TYPE)target)->ARRAY_TRG[y], \
5935 &((TYPE)target)->ARRAY_TRG[y + 1], \
5936 (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \
5937 } \
5938 if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \
5939 LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \
5940 ((TYPE)target)->ARRAY_TRG = NULL; \
5941 }
5942
5943 /* [units-stmt] */
5944 if (d->units) {
5945 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5946 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d->units);
5947 if (strcmp(((struct lysc_node_leaf*)target)->units, d->units)) {
5948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5949 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
5950 d->units, ((struct lysc_node_leaf*)target)->units);
5951 goto cleanup;
5952 }
5953 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)target)->units);
5954 ((struct lysc_node_leaf*)target)->units = NULL;
5955 }
5956
5957 /* *must-stmt */
5958 if (d->musts) {
5959 switch (target->nodetype) {
5960 case LYS_CONTAINER:
5961 case LYS_LIST:
5962 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5963 break;
5964 case LYS_LEAF:
5965 case LYS_LEAFLIST:
5966 case LYS_ANYDATA:
5967 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5968 break;
5969 case LYS_NOTIF:
5970 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5971 break;
5972 case LYS_RPC:
5973 case LYS_ACTION:
5974 if (dev_flags & LYSC_OPT_RPC_INPUT) {
5975 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5976 break;
5977 } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) {
5978 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5979 break;
5980 }
5981 /* fall through */
5982 default:
5983 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
5984 lys_nodetype2str(target->nodetype), "delete", "must");
5985 goto cleanup;
5986 }
5987 }
5988
5989 /* *unique-stmt */
5990 if (d->uniques) {
5991 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5992 LY_ARRAY_FOR(d->uniques, x) {
5993 LY_ARRAY_FOR(list->uniques, z) {
5994 for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) {
5995 nodeid = strpbrk(name, " \t\n");
5996 if (nodeid) {
5997 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
5998 break;
5999 }
6000 while (isspace(*nodeid)) {
6001 ++nodeid;
6002 }
6003 } else {
6004 if (strcmp(name, list->uniques[z][y]->name)) {
6005 break;
6006 }
6007 }
6008 }
6009 if (!name) {
6010 /* complete match - remove the unique */
6011 LY_ARRAY_DECREMENT(list->uniques);
6012 LY_ARRAY_FREE(list->uniques[z]);
6013 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques));
6014 --z;
6015 break;
6016 }
6017 }
6018 if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) {
6019 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6020 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6021 d->uniques[x]);
6022 goto cleanup;
6023 }
6024 }
6025 if (!LY_ARRAY_COUNT(list->uniques)) {
6026 LY_ARRAY_FREE(list->uniques);
6027 list->uniques = NULL;
6028 }
6029 }
6030
6031 /* *default-stmt */
6032 if (d->dflts) {
6033 switch (target->nodetype) {
6034 case LYS_LEAF:
6035 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
6036 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(target->flags & LYS_SET_DFLT),
6037 dflt, "deleting", "default", d->dflts[0]);
6038
6039 /* check that the values matches */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006040 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, leaf->dflt_mod, &i);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006041 if (strcmp(dflt, d->dflts[0])) {
Michal Vaskoccc062a2020-08-13 08:34:50 +02006042 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6043 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6044 d->dflts[0], dflt);
Michal Vasko9acaf492020-08-13 09:05:58 +02006045 if (i) {
6046 free((char*)dflt);
6047 }
Michal Vaskoccc062a2020-08-13 08:34:50 +02006048 goto cleanup;
6049 }
6050 if (i) {
6051 free((char*)dflt);
6052 }
6053
6054 /* update the list of incomplete default values if needed */
6055 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6056
6057 /* remove the default specification */
6058 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6059 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6060 free(leaf->dflt);
6061 leaf->dflt = NULL;
6062 leaf->dflt_mod = NULL;
6063 target->flags &= ~LYS_SET_DFLT;
6064 break;
6065 case LYS_LEAFLIST:
6066 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d->dflts[0]);
6067 LY_ARRAY_FOR(d->dflts, x) {
6068 LY_ARRAY_FOR(llist->dflts, y) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006069 dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, llist->dflts_mods[y], &i);
Michal Vaskoccc062a2020-08-13 08:34:50 +02006070 if (!strcmp(dflt, d->dflts[x])) {
6071 if (i) {
6072 free((char*)dflt);
6073 }
6074 break;
6075 }
6076 if (i) {
6077 free((char*)dflt);
6078 }
6079 }
6080 if (y == LY_ARRAY_COUNT(llist->dflts)) {
6081 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6082 "which does not match any of the target's property values.", d->dflts[x]);
6083 goto cleanup;
6084 }
6085
6086 /* update the list of incomplete default values if needed */
6087 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6088
6089 LY_ARRAY_DECREMENT(llist->dflts_mods);
6090 LY_ARRAY_DECREMENT(llist->dflts);
6091 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6092 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6093 free(llist->dflts[y]);
6094 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts));
6095 memmove(&llist->dflts_mods[y], &llist->dflts_mods[y + 1], (LY_ARRAY_COUNT(llist->dflts_mods) - y) * (sizeof *llist->dflts_mods));
6096 }
6097 if (!LY_ARRAY_COUNT(llist->dflts)) {
6098 LY_ARRAY_FREE(llist->dflts_mods);
6099 llist->dflts_mods = NULL;
6100 LY_ARRAY_FREE(llist->dflts);
6101 llist->dflts = NULL;
6102 llist->flags &= ~LYS_SET_DFLT;
6103 }
6104 break;
6105 case LYS_CHOICE:
6106 DEV_CHECK_CARDINALITY(d->dflts, 1, "default");
6107 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d->dflts[0]);
6108 nodeid = d->dflts[0];
6109 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
6110 if (prefix) {
6111 /* use module prefixes from the deviation module to match the module of the default case */
6112 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6113 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
6114 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6115 "The prefix does not match any imported module of the deviation module.", d->dflts[0]);
6116 goto cleanup;
6117 }
6118 if (mod != ((struct lysc_node_choice*)target)->dflt->module) {
6119 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
6120 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6121 "The prefix does not match the default case's module.", d->dflts[0]);
6122 goto cleanup;
6123 }
6124 }
6125 /* else {
6126 * strictly, the default prefix would point to the deviation module, but the value should actually
6127 * match the default string in the original module (usually unprefixed), so in this case we do not check
6128 * the module of the default case, just matching its name */
6129 if (strcmp(name, ((struct lysc_node_choice*)target)->dflt->name)) {
6130 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6131 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6132 d->dflts[0], ((struct lysc_node_choice*)target)->dflt->name);
6133 goto cleanup;
6134 }
6135 ((struct lysc_node_choice*)target)->dflt->flags &= ~LYS_SET_DFLT;
6136 ((struct lysc_node_choice*)target)->dflt = NULL;
6137 break;
6138 default:
6139 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6140 lys_nodetype2str(target->nodetype), "delete", "default");
6141 goto cleanup;
6142 }
6143 }
6144
6145 ret = LY_SUCCESS;
6146
6147cleanup:
6148 return ret;
6149}
6150
6151/**
6152 * @brief Apply deviate replace.
6153 *
6154 * @param[in] ctx Compile context.
6155 * @param[in] target Deviation target.
6156 * @param[in] d Deviate replace to apply.
6157 * @param[out] dflt Added default value.
6158 * @param[out] changed_type Whether the target type was changed.
6159 * @return LY_ERR value.
6160 */
6161static LY_ERR
6162lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysc_node *target, struct lysp_deviate_rpl *d, const char **dflt,
6163 int *changed_type)
6164{
6165 LY_ERR ret = LY_EVALID;
6166 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6167 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6168 LY_ARRAY_COUNT_TYPE x;
6169
6170#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
6171 if (!(((TYPE)target)->MEMBER COND)) { \
6172 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
6173 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \
6174 goto cleanup; \
6175 }
6176
6177 /* [type-stmt] */
6178 if (d->type) {
6179 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6180 /* type is mandatory, so checking for its presence is not necessary */
6181 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type);
6182
6183 if (leaf->dflt && !(target->flags & LYS_SET_DFLT)) {
6184 /* the target has default from the previous type - remove it */
6185 if (target->nodetype == LYS_LEAF) {
6186 /* update the list of incomplete default values if needed */
6187 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6188
6189 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6190 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6191 free(leaf->dflt);
6192 leaf->dflt = NULL;
6193 leaf->dflt_mod = NULL;
6194 } else { /* LYS_LEAFLIST */
6195 LY_ARRAY_FOR(llist->dflts, x) {
6196 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
6197 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6198 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6199 free(llist->dflts[x]);
6200 }
6201 LY_ARRAY_FREE(llist->dflts);
6202 llist->dflts = NULL;
6203 LY_ARRAY_FREE(llist->dflts_mods);
6204 llist->dflts_mods = NULL;
6205 }
6206 }
6207 if (!leaf->dflt) {
6208 /* there is no default value, do not set changed_type after type compilation
6209 * which is used to recompile the default value */
6210 *changed_type = -1;
6211 }
6212 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d->type, leaf), cleanup);
6213 (*changed_type)++;
6214 }
6215
6216 /* [units-stmt] */
6217 if (d->units) {
6218 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6219 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS),
6220 units, "replacing", "units", d->units);
6221
6222 lydict_remove(ctx->ctx, leaf->units);
6223 DUP_STRING(ctx->ctx, d->units, leaf->units);
6224 }
6225
6226 /* [default-stmt] */
6227 if (d->dflt) {
6228 switch (target->nodetype) {
6229 case LYS_LEAF:
6230 DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT),
6231 dflt, "replacing", "default", d->dflt);
6232 /* first, remove the default value taken from the type */
6233 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6234 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6235 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6236 *dflt = d->dflt;
6237 /* parsing is done at the end after possible replace of the leaf's type */
6238 break;
6239 case LYS_CHOICE:
6240 DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt);
6241 if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) {
6242 goto cleanup;
6243 }
6244 break;
6245 default:
6246 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6247 lys_nodetype2str(target->nodetype), "replace", "default");
6248 goto cleanup;
6249 }
6250 }
6251
6252 /* [config-stmt] */
6253 if (d->flags & LYS_CONFIG_MASK) {
6254 if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
6255 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6256 lys_nodetype2str(target->nodetype), "replace", "config");
6257 goto cleanup;
6258 }
6259 if (!(target->flags & LYS_SET_CONFIG)) {
6260 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6261 "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false");
6262 goto cleanup;
6263 }
6264 LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup);
6265 }
6266
6267 /* [mandatory-stmt] */
6268 if (d->flags & LYS_MAND_MASK) {
6269 if (!(target->flags & LYS_MAND_MASK)) {
6270 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6271 "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6272 goto cleanup;
6273 }
6274 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup);
6275 }
6276
6277 /* [min-elements-stmt] */
6278 if (d->flags & LYS_SET_MIN) {
6279 if (target->nodetype == LYS_LEAFLIST) {
6280 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements");
6281 /* change value */
6282 ((struct lysc_node_leaflist *)target)->min = d->min;
6283 } else if (target->nodetype == LYS_LIST) {
6284 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements");
6285 /* change value */
6286 ((struct lysc_node_list *)target)->min = d->min;
6287 } else {
6288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6289 lys_nodetype2str(target->nodetype), "replace", "min-elements");
6290 goto cleanup;
6291 }
6292 if (d->min) {
6293 target->flags |= LYS_MAND_TRUE;
6294 }
6295 }
6296
6297 /* [max-elements-stmt] */
6298 if (d->flags & LYS_SET_MAX) {
6299 if (target->nodetype == LYS_LEAFLIST) {
6300 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements");
6301 /* change value */
6302 ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1;
6303 } else if (target->nodetype == LYS_LIST) {
6304 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements");
6305 /* change value */
6306 ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1;
6307 } else {
6308 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
6309 lys_nodetype2str(target->nodetype), "replace", "max-elements");
6310 goto cleanup;
6311 }
6312 }
6313
6314 ret = LY_SUCCESS;
6315
6316cleanup:
6317 return ret;
6318}
6319
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006320/**
6321 * @brief Apply all deviations of one target node.
6322 *
6323 * @param[in] ctx Compile context.
6324 * @param[in] dev Deviation structure to apply.
6325 * @return LY_ERR value.
6326 */
6327static LY_ERR
6328lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev)
Radek Krejciccd20f12019-02-15 14:12:27 +01006329{
Radek Krejcia1911222019-07-22 17:24:50 +02006330 LY_ERR ret = LY_EVALID, rc;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006331 struct lysc_node *target = dev->target;
6332 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target;
6333 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target;
6334 struct ly_err_item *err = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006335 struct lysc_action *rpcs;
6336 struct lysc_notif *notifs;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006337 struct lysp_deviate *d;
Michal Vaskoccc062a2020-08-13 08:34:50 +02006338 LY_ARRAY_COUNT_TYPE v, x;
6339 int changed_type = 0;
6340 const char *dflt = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006341 uint32_t min, max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006342
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006343 lysc_update_path(ctx, NULL, dev->nodeid);
6344
6345 /* not-supported */
6346 if (dev->not_supported) {
6347 if (LY_ARRAY_COUNT(dev->deviates) > 1) {
6348 LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.",
6349 LY_ARRAY_COUNT(dev->deviates), dev->nodeid);
6350 }
6351
6352#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
6353 if (target->parent) { \
6354 ARRAY = (TYPE*)GETFUNC(target->parent); \
6355 } else { \
6356 ARRAY = target->module->compiled->ARRAY; \
6357 } \
6358 LY_ARRAY_FOR(ARRAY, x) { \
6359 if (&ARRAY[x] == (TYPE*)target) { break; } \
6360 } \
6361 if (x < LY_ARRAY_COUNT(ARRAY)) { \
6362 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6363 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6364 LY_ARRAY_DECREMENT(ARRAY); \
6365 }
6366
6367 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
6368 if (dev->flags & LYSC_OPT_RPC_INPUT) {
6369 /* remove RPC's/action's input */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006370 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input);
6371 memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input);
6372 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free);
6373 ((struct lysc_action *)target)->input_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006374 } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) {
6375 /* remove RPC's/action's output */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006376 lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output);
6377 memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output);
6378 FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free);
6379 ((struct lysc_action *)target)->output_exts = NULL;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006380 } else {
6381 /* remove RPC/action */
6382 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
6383 }
6384 } else if (target->nodetype == LYS_NOTIF) {
6385 /* remove Notification */
6386 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
6387 } else {
6388 /* remove the target node */
6389 lysc_disconnect(target);
6390 lysc_node_free(ctx->ctx, target);
6391 }
6392
6393 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6394 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
6395 return LY_SUCCESS;
6396 }
6397
6398 /* list of deviates (not-supported is not present in the list) */
6399 LY_ARRAY_FOR(dev->deviates, v) {
6400 d = dev->deviates[v];
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006401 switch (d->mod) {
6402 case LYS_DEV_ADD:
Michal Vaskoccc062a2020-08-13 08:34:50 +02006403 LY_CHECK_GOTO(lys_apply_deviate_add(ctx, target, dev->flags, (struct lysp_deviate_add *)d, &dflt), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006404 break;
6405 case LYS_DEV_DELETE:
Michal Vaskoccc062a2020-08-13 08:34:50 +02006406 LY_CHECK_GOTO(lys_apply_deviate_delete(ctx, target, dev->flags, (struct lysp_deviate_del *)d), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006407 break;
6408 case LYS_DEV_REPLACE:
Michal Vaskoccc062a2020-08-13 08:34:50 +02006409 LY_CHECK_GOTO(lys_apply_deviate_replace(ctx, target, (struct lysp_deviate_rpl *)d, &dflt, &changed_type), cleanup);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006410 break;
6411 default:
6412 LOGINT(ctx->ctx);
6413 goto cleanup;
6414 }
6415 }
6416
6417 /* final check when all deviations of a single target node are applied */
6418
6419 /* check min-max compatibility */
6420 if (target->nodetype == LYS_LEAFLIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006421 min = ((struct lysc_node_leaflist *)target)->min;
6422 max = ((struct lysc_node_leaflist *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006423 } else if (target->nodetype == LYS_LIST) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006424 min = ((struct lysc_node_list *)target)->min;
6425 max = ((struct lysc_node_list *)target)->max;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006426 } else {
6427 min = max = 0;
6428 }
6429 if (min > max) {
6430 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
6431 "after deviation: min value %u is bigger than max value %u.", min, max);
6432 goto cleanup;
6433 }
6434
6435 if (dflt) {
6436 /* parse added/changed default value after possible change of the type */
6437 leaf->dflt_mod = ctx->mod_def;
6438 leaf->dflt->realtype = leaf->type;
6439 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6440 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006441 LY_PREF_SCHEMA, leaf->dflt_mod, target, NULL, leaf->dflt, NULL, &err);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006442 leaf->dflt->realtype->refcount++;
6443 if (err) {
6444 ly_err_print(err);
6445 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6446 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6447 ly_err_free(err);
6448 }
6449 if (rc == LY_EINCOMPLETE) {
6450 /* postpone default compilation when the tree is complete */
6451 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, target, leaf->dflt, leaf->dflt_mod), cleanup);
6452
6453 /* but in general result is so far ok */
6454 rc = LY_SUCCESS;
6455 }
6456 LY_CHECK_GOTO(rc, cleanup);
6457 } else if (changed_type) {
6458 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6459 int dynamic;
6460 if (target->nodetype == LYS_LEAF) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006461 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, leaf->dflt_mod, &dynamic);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006462
6463 /* update the list of incomplete default values if needed */
6464 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6465
6466 /* remove the previous default */
6467 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6468 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6469 leaf->dflt->realtype = leaf->type;
6470 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6471 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006472 LY_PREF_SCHEMA, leaf->dflt_mod, target, NULL, leaf->dflt, NULL, &err);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006473 leaf->dflt->realtype->refcount++;
6474 if (err) {
6475 ly_err_print(err);
6476 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6477 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6478 ly_err_free(err);
6479 }
6480 if (dynamic) {
6481 free((void*)dflt);
6482 }
6483 dflt = NULL;
6484 if (rc == LY_EINCOMPLETE) {
6485 /* postpone default compilation when the tree is complete */
6486 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, target, leaf->dflt, leaf->dflt_mod), cleanup);
6487
6488 /* but in general result is so far ok */
6489 rc = LY_SUCCESS;
6490 }
6491 LY_CHECK_GOTO(rc, cleanup);
6492 } else { /* LYS_LEAFLIST */
6493 LY_ARRAY_FOR(llist->dflts, x) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006494 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_SCHEMA, llist->dflts_mods[x], &dynamic);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006495 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6496 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6497 llist->dflts[x]->realtype = llist->type;
6498 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6499 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006500 LY_PREF_SCHEMA, llist->dflts_mods[x], target, NULL, llist->dflts[x], NULL, &err);
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006501 llist->dflts[x]->realtype->refcount++;
6502 if (err) {
6503 ly_err_print(err);
6504 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6505 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6506 dflt, err->msg);
6507 ly_err_free(err);
6508 }
6509 if (dynamic) {
6510 free((void*)dflt);
6511 }
6512 dflt = NULL;
6513 if (rc == LY_EINCOMPLETE) {
6514 /* postpone default compilation when the tree is complete */
6515 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6516
6517 /* but in general result is so far ok */
6518 rc = LY_SUCCESS;
6519 }
6520 LY_CHECK_GOTO(rc, cleanup);
6521 }
6522 }
6523 }
6524
6525 /* check mandatory - default compatibility */
6526 if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT)
6527 && (target->flags & LYS_MAND_TRUE)) {
6528 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6529 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype));
6530 goto cleanup;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006531 } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice* )target)->dflt
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006532 && (target->flags & LYS_MAND_TRUE)) {
6533 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
6534 goto cleanup;
6535 }
6536 if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) {
6537 /* mandatory node under a default case */
6538 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6539 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6540 lys_nodetype2str(target->nodetype), target->name, target->parent->name);
6541 goto cleanup;
6542 }
6543
6544 /* success */
6545 ret = LY_SUCCESS;
6546
6547cleanup:
6548 lysc_update_path(ctx, NULL, NULL);
6549 return ret;
6550}
6551
6552LY_ERR
6553lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
6554{
6555 LY_ERR ret = LY_EVALID;
6556 struct ly_set devs_p = {0};
6557 struct ly_set targets = {0};
6558 struct lysc_node *target; /* target target of the deviation */
6559 struct lysp_deviation *dev;
6560 struct lysp_deviate *d, **dp_new;
6561 LY_ARRAY_COUNT_TYPE u, v;
6562 struct lysc_deviation **devs = NULL;
6563 struct lys_module *target_mod, **dev_mod;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006564 uint16_t flags;
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006565 int i;
Radek Krejciccd20f12019-02-15 14:12:27 +01006566
6567 /* get all deviations from the module and all its submodules ... */
6568 LY_ARRAY_FOR(mod_p->deviations, u) {
6569 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
6570 }
6571 LY_ARRAY_FOR(mod_p->includes, v) {
6572 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
6573 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
6574 }
6575 }
6576 if (!devs_p.count) {
6577 /* nothing to do */
6578 return LY_SUCCESS;
6579 }
6580
Radek Krejci327de162019-06-14 12:52:07 +02006581 lysc_update_path(ctx, NULL, "{deviation}");
6582
Radek Krejciccd20f12019-02-15 14:12:27 +01006583 /* ... and group them by the target node */
6584 devs = calloc(devs_p.count, sizeof *devs);
6585 for (u = 0; u < devs_p.count; ++u) {
6586 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02006587 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01006588
6589 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006590 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
6591 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01006592 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01006593 /* move the target pointer to input/output to make them different from the action and
6594 * between them. Before the devs[] item is being processed, the target pointer must be fixed
6595 * back to the RPC/action node due to a better compatibility and decision code in this function.
6596 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
6597 if (flags & LYSC_OPT_RPC_INPUT) {
6598 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
6599 flags |= LYSC_OPT_INTERNAL;
6600 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
6601 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
6602 flags |= LYSC_OPT_INTERNAL;
6603 }
6604 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006605 /* insert into the set of targets with duplicity detection */
6606 i = ly_set_add(&targets, target, 0);
6607 if (!devs[i]) {
6608 /* new record */
6609 devs[i] = calloc(1, sizeof **devs);
6610 devs[i]->target = target;
6611 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006612 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01006613 }
6614 /* add deviates into the deviation's list of deviates */
6615 for (d = dev->deviates; d; d = d->next) {
6616 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
6617 *dp_new = d;
6618 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
6619 devs[i]->not_supported = 1;
6620 }
6621 }
Radek Krejci327de162019-06-14 12:52:07 +02006622
6623 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006624 }
6625
Radek Krejciccd20f12019-02-15 14:12:27 +01006626 /* apply deviations */
6627 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcif538ce52019-03-05 10:46:14 +01006628 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
6629 /* fix the target pointer in case of RPC's/action's input/output */
6630 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006631 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, input));
Radek Krejcif538ce52019-03-05 10:46:14 +01006632 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006633 devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, output));
Radek Krejcif538ce52019-03-05 10:46:14 +01006634 }
6635 }
6636
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006637 /* remember target module (the target node may be removed) */
6638 target_mod = devs[u]->target->module;
Radek Krejcifc11bd72019-04-11 16:00:05 +02006639
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006640 /* apply the deviation */
6641 LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup);
Radek Krejci33f72892019-02-21 10:36:58 +01006642
Michal Vaskoe6143202020-07-03 13:02:08 +02006643 /* add this module into the target module deviated_by, if not there already */
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006644 i = 0;
6645 LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) {
6646 if (target_mod->compiled->deviated_by[v] == mod_p->mod) {
6647 i = 1;
Michal Vaskoe6143202020-07-03 13:02:08 +02006648 break;
6649 }
6650 }
Michal Vasko1a3dc3b2020-08-12 15:47:42 +02006651 if (!i) {
6652 LY_ARRAY_NEW_GOTO(ctx->ctx, target_mod->compiled->deviated_by, dev_mod, ret, cleanup);
Michal Vaskoe6143202020-07-03 13:02:08 +02006653 *dev_mod = mod_p->mod;
6654 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006655 }
6656
Radek Krejci327de162019-06-14 12:52:07 +02006657 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006658 ret = LY_SUCCESS;
6659
6660cleanup:
6661 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6662 LY_ARRAY_FREE(devs[u]->deviates);
6663 free(devs[u]);
6664 }
6665 free(devs);
6666 ly_set_erase(&targets, NULL);
6667 ly_set_erase(&devs_p, NULL);
6668
6669 return ret;
6670}
6671
Radek Krejcib56c7502019-02-13 14:19:54 +01006672/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006673 * @brief Compile the given YANG submodule into the main module.
6674 * @param[in] ctx Compile context
6675 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006676 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6677 */
6678LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006679lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006680{
6681 unsigned int u;
6682 LY_ERR ret = LY_SUCCESS;
6683 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006684 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006685 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006686 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006687
Michal Vasko33ff9422020-07-03 09:50:39 +02006688 if (!mainmod->mod->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01006689 /* features are compiled directly into the compiled module structure,
6690 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6691 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006692 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6693 LY_CHECK_GOTO(ret, error);
6694 }
Radek Krejci0af46292019-01-11 16:02:31 +01006695
Michal Vasko33ff9422020-07-03 09:50:39 +02006696 if (!mainmod->mod->dis_identities) {
6697 ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities);
6698 LY_CHECK_GOTO(ret, error);
6699 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01006700
Radek Krejci474f9b82019-07-24 11:36:37 +02006701 /* data nodes */
6702 LY_LIST_FOR(submod->data, node_p) {
6703 ret = lys_compile_node(ctx, node_p, NULL, 0);
6704 LY_CHECK_GOTO(ret, error);
6705 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006706
Radek Krejciec4da802019-05-02 13:02:41 +02006707 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6708 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006709
Radek Krejcid05cbd92018-12-05 14:26:40 +01006710error:
6711 return ret;
6712}
6713
Radek Krejci335332a2019-09-05 13:03:35 +02006714static void *
6715lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6716{
6717 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6718 if (substmts[u].stmt == stmt) {
6719 return substmts[u].storage;
6720 }
6721 }
6722 return NULL;
6723}
6724
6725LY_ERR
6726lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6727{
6728 LY_ERR ret = LY_EVALID, r;
6729 unsigned int u;
6730 struct lysp_stmt *stmt;
6731 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006732
6733 /* check for invalid substatements */
6734 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006735 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6736 continue;
6737 }
Radek Krejci335332a2019-09-05 13:03:35 +02006738 for (u = 0; substmts[u].stmt; ++u) {
6739 if (substmts[u].stmt == stmt->kw) {
6740 break;
6741 }
6742 }
6743 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6745 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006746 goto cleanup;
6747 }
Radek Krejci335332a2019-09-05 13:03:35 +02006748 }
6749
Radek Krejciad5963b2019-09-06 16:03:05 +02006750 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6751
Radek Krejci335332a2019-09-05 13:03:35 +02006752 /* keep order of the processing the same as the order in the defined substmts,
6753 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6754 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006755 int stmt_present = 0;
6756
Radek Krejci335332a2019-09-05 13:03:35 +02006757 for (stmt = ext->child; stmt; stmt = stmt->next) {
6758 if (substmts[u].stmt != stmt->kw) {
6759 continue;
6760 }
6761
Radek Krejciad5963b2019-09-06 16:03:05 +02006762 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006763 if (substmts[u].storage) {
6764 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006765 case LY_STMT_STATUS:
6766 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6767 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6768 break;
6769 case LY_STMT_UNITS: {
6770 const char **units;
6771
6772 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6773 /* single item */
6774 if (*((const char **)substmts[u].storage)) {
6775 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6776 goto cleanup;
6777 }
6778 units = (const char **)substmts[u].storage;
6779 } else {
6780 /* sized array */
6781 const char ***units_array = (const char ***)substmts[u].storage;
6782 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6783 }
6784 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6785 break;
6786 }
Radek Krejci335332a2019-09-05 13:03:35 +02006787 case LY_STMT_TYPE: {
6788 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6789 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6790
6791 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6792 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006793 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006794 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6795 goto cleanup;
6796 }
6797 compiled = substmts[u].storage;
6798 } else {
6799 /* sized array */
6800 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6801 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6802 compiled = (void*)type;
6803 }
6804
Radek Krejciad5963b2019-09-06 16:03:05 +02006805 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006806 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6807 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006808 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6809 lysp_type_free(ctx->ctx, parsed);
6810 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006811 break;
6812 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006813 case LY_STMT_IF_FEATURE: {
6814 struct lysc_iffeature *iff = NULL;
6815
6816 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6817 /* single item */
6818 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6819 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6820 goto cleanup;
6821 }
6822 iff = (struct lysc_iffeature*)substmts[u].storage;
6823 } else {
6824 /* sized array */
6825 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6826 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6827 }
6828 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6829 break;
6830 }
6831 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6832 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006833 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006834 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.",
6835 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006836 goto cleanup;
6837 }
6838 }
Radek Krejci335332a2019-09-05 13:03:35 +02006839 }
Radek Krejci335332a2019-09-05 13:03:35 +02006840
Radek Krejciad5963b2019-09-06 16:03:05 +02006841 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6842 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6843 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6844 goto cleanup;
6845 }
Radek Krejci335332a2019-09-05 13:03:35 +02006846 }
6847
6848 ret = LY_SUCCESS;
6849
6850cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006851 return ret;
6852}
6853
Michal Vasko175012e2019-11-06 15:49:14 +01006854/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006855 * @brief Check when for cyclic dependencies.
6856 * @param[in] set Set with all the referenced nodes.
6857 * @param[in] node Node whose "when" referenced nodes are in @p set.
6858 * @return LY_ERR value
6859 */
6860static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006861lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006862{
6863 struct lyxp_set tmp_set;
6864 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006865 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006866 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006867 int idx;
6868 struct lysc_when *when;
6869 LY_ERR ret = LY_SUCCESS;
6870
6871 memset(&tmp_set, 0, sizeof tmp_set);
6872
6873 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006874 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006875 xp_scnode = &set->val.scnodes[i];
6876
Michal Vasko5c4e5892019-11-14 12:31:38 +01006877 if (xp_scnode->in_ctx != -1) {
6878 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006879 xp_scnode->in_ctx = 1;
6880 }
6881 }
6882
6883 for (i = 0; i < set->used; ++i) {
6884 xp_scnode = &set->val.scnodes[i];
6885 if (xp_scnode->in_ctx != 1) {
6886 /* already checked */
6887 continue;
6888 }
6889
Michal Vasko1bf09392020-03-27 12:38:10 +01006890 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006891 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006892 /* no when to check */
6893 xp_scnode->in_ctx = 0;
6894 continue;
6895 }
6896
6897 node = xp_scnode->scnode;
6898 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006899 LY_ARRAY_FOR(node->when, u) {
6900 when = node->when[u];
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006901 ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006902 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6903 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006904 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006905 goto cleanup;
6906 }
6907
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006908 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006909 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006910 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006911 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006912 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 +01006913 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006914 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 +01006915 ret = LY_EVALID;
6916 goto cleanup;
6917 }
6918
6919 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006920 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006921 } else {
6922 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006923 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006924 }
6925 }
6926
6927 /* merge this set into the global when set */
6928 lyxp_set_scnode_merge(set, &tmp_set);
6929 }
6930
6931 /* check when of non-data parents as well */
6932 node = node->parent;
6933 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6934
Michal Vasko251f56e2019-11-14 16:06:47 +01006935 /* this node when was checked (xp_scnode could have been reallocd) */
6936 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006937 }
6938
6939cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006940 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006941 return ret;
6942}
6943
6944/**
Michal Vasko175012e2019-11-06 15:49:14 +01006945 * @brief Check when/must expressions of a node on a compiled schema tree.
6946 * @param[in] ctx Compile context.
6947 * @param[in] node Node to check.
6948 * @return LY_ERR value
6949 */
6950static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006951lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006952{
Michal Vasko175012e2019-11-06 15:49:14 +01006953 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006954 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02006955 LY_ARRAY_COUNT_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01006956 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006957 struct lysc_when **when = NULL;
6958 struct lysc_must *musts = NULL;
6959 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b26e742020-07-17 15:02:10 +02006960 const struct lysc_node *op;
Michal Vasko175012e2019-11-06 15:49:14 +01006961
6962 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006963 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko6b26e742020-07-17 15:02:10 +02006964 if (node->flags & LYS_CONFIG_R) {
6965 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent);
6966 if (op) {
6967 /* we are actually in output */
6968 opts = LYXP_SCNODE_OUTPUT;
6969 }
6970 }
Michal Vasko175012e2019-11-06 15:49:14 +01006971
6972 switch (node->nodetype) {
6973 case LYS_CONTAINER:
6974 when = ((struct lysc_node_container *)node)->when;
6975 musts = ((struct lysc_node_container *)node)->musts;
6976 break;
6977 case LYS_CHOICE:
6978 when = ((struct lysc_node_choice *)node)->when;
6979 break;
6980 case LYS_LEAF:
6981 when = ((struct lysc_node_leaf *)node)->when;
6982 musts = ((struct lysc_node_leaf *)node)->musts;
6983 break;
6984 case LYS_LEAFLIST:
6985 when = ((struct lysc_node_leaflist *)node)->when;
6986 musts = ((struct lysc_node_leaflist *)node)->musts;
6987 break;
6988 case LYS_LIST:
6989 when = ((struct lysc_node_list *)node)->when;
6990 musts = ((struct lysc_node_list *)node)->musts;
6991 break;
6992 case LYS_ANYXML:
6993 case LYS_ANYDATA:
6994 when = ((struct lysc_node_anydata *)node)->when;
6995 musts = ((struct lysc_node_anydata *)node)->musts;
6996 break;
6997 case LYS_CASE:
6998 when = ((struct lysc_node_case *)node)->when;
6999 break;
7000 case LYS_NOTIF:
7001 musts = ((struct lysc_notif *)node)->musts;
7002 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01007003 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01007004 case LYS_ACTION:
7005 /* first process input musts */
7006 musts = ((struct lysc_action *)node)->input.musts;
7007 break;
Michal Vasko175012e2019-11-06 15:49:14 +01007008 default:
7009 /* nothing to check */
7010 break;
7011 }
7012
Michal Vasko175012e2019-11-06 15:49:14 +01007013 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007014 LY_ARRAY_FOR(when, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007015 ret = lyxp_atomize(when[u]->cond, LY_PREF_SCHEMA, when[u]->module, when[u]->context ? when[u]->context : node,
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007016 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01007017 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007018 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01007019 goto cleanup;
7020 }
7021
Michal Vaskodc052f32019-11-07 11:11:38 +01007022 ctx->path[0] = '\0';
7023 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007024 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01007025 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007026 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
7027 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01007028
Michal Vaskoecd62de2019-11-13 12:35:11 +01007029 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007030 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01007031 schema->name);
7032 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01007033
7034 /* check dummy node accessing */
7035 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007036 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01007037 ret = LY_EVALID;
7038 goto cleanup;
7039 }
Michal Vasko175012e2019-11-06 15:49:14 +01007040 }
7041 }
7042
Michal Vaskoecd62de2019-11-13 12:35:11 +01007043 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02007044 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007045 LY_CHECK_GOTO(ret, cleanup);
7046
Michal Vaskod3678892020-05-21 10:06:58 +02007047 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007048 }
7049
Michal Vasko5d8756a2019-11-07 15:21:00 +01007050check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01007051 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007052 LY_ARRAY_FOR(musts, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007053 ret = lyxp_atomize(musts[u].cond, LY_PREF_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01007054 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007055 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01007056 goto cleanup;
7057 }
7058
Michal Vaskodc052f32019-11-07 11:11:38 +01007059 ctx->path[0] = '\0';
7060 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007061 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01007062 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007063 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01007064 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007065 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
7066 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01007067 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01007068 }
7069 }
7070
Michal Vaskod3678892020-05-21 10:06:58 +02007071 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007072 }
7073
Michal Vasko1bf09392020-03-27 12:38:10 +01007074 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01007075 /* now check output musts */
7076 input_done = 1;
7077 musts = ((struct lysc_action *)node)->output.musts;
7078 opts = LYXP_SCNODE_OUTPUT;
7079 goto check_musts;
7080 }
7081
Michal Vasko175012e2019-11-06 15:49:14 +01007082cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007083 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007084 return ret;
7085}
7086
Michal Vasko8d544252020-03-02 10:19:52 +01007087static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007088lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
7089{
Michal Vasko6b26e742020-07-17 15:02:10 +02007090 const struct lysc_node *target = NULL, *siter;
Michal Vasko004d3152020-06-11 19:59:22 +02007091 struct ly_path *p;
7092 struct lysc_type *type;
7093
7094 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7095
7096 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02007097 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
7098 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007099 LY_PREF_SCHEMA, lref->path_context, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02007100
7101 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007102 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02007103 ly_path_free(node->module->ctx, p);
7104
7105 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7106 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7107 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
7108 lref->path->expr, lys_nodetype2str(target->nodetype));
7109 return LY_EVALID;
7110 }
7111
7112 /* check status */
7113 ctx->path[0] = '\0';
7114 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7115 ctx->path_len = strlen(ctx->path);
7116 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
7117 return LY_EVALID;
7118 }
7119 ctx->path_len = 1;
7120 ctx->path[1] = '\0';
7121
7122 /* check config */
Michal Vasko6b26e742020-07-17 15:02:10 +02007123 if (lref->require_instance) {
7124 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent);
7125 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007126 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
7127 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
7128 return LY_EVALID;
7129 }
7130 }
7131
7132 /* store the target's type and check for circular chain of leafrefs */
7133 lref->realtype = ((struct lysc_node_leaf *)target)->type;
7134 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
7135 if (type == (struct lysc_type *)lref) {
7136 /* circular chain detected */
7137 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7138 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
7139 return LY_EVALID;
7140 }
7141 }
7142
7143 /* check if leafref and its target are under common if-features */
7144 if (lys_compile_leafref_features_validate(node, target)) {
7145 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7146 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
7147 " features applicable to the leafref itself.", lref->path->expr);
7148 return LY_EVALID;
7149 }
7150
7151 return LY_SUCCESS;
7152}
7153
7154static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01007155lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7156{
7157 struct lysc_ext_instance *ext;
7158 struct lysp_ext_instance *ext_p = NULL;
7159 struct lysp_stmt *stmt;
7160 const struct lys_module *ext_mod;
7161 LY_ERR ret = LY_SUCCESS;
7162
7163 /* create the parsed extension instance manually */
7164 ext_p = calloc(1, sizeof *ext_p);
7165 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
7166 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
7167 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
7168 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7169 ext_p->insubstmt_index = 0;
7170
7171 stmt = calloc(1, sizeof *ext_p->child);
7172 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
7173 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
7174 stmt->kw = LY_STMT_TYPE;
7175 ext_p->child = stmt;
7176
7177 /* allocate new extension instance */
7178 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7179
7180 /* manually get extension definition module */
7181 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7182
7183 /* compile the extension instance */
7184 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7185
7186cleanup:
7187 lysp_ext_instance_free(ctx->ctx, ext_p);
7188 free(ext_p);
7189 return ret;
7190}
7191
Michal Vasko004d3152020-06-11 19:59:22 +02007192static LY_ERR
7193lys_compile_unres(struct lysc_ctx *ctx)
7194{
7195 struct lysc_node *node;
7196 struct lysc_type *type, *typeiter;
7197 struct lysc_type_leafref *lref;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007198 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007199 uint32_t i;
7200
7201 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7202 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7203 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7204 for (i = 0; i < ctx->leafrefs.count; ++i) {
7205 node = ctx->leafrefs.objs[i];
7206 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7207 type = ((struct lysc_node_leaf *)node)->type;
7208 if (type->basetype == LY_TYPE_LEAFREF) {
7209 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7210 } else if (type->basetype == LY_TYPE_UNION) {
7211 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7212 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7213 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7214 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7215 }
7216 }
7217 }
7218 }
7219 for (i = 0; i < ctx->leafrefs.count; ++i) {
7220 /* store pointer to the real type */
7221 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7222 if (type->basetype == LY_TYPE_LEAFREF) {
7223 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7224 typeiter->basetype == LY_TYPE_LEAFREF;
7225 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7226 ((struct lysc_type_leafref*)type)->realtype = typeiter;
7227 } else if (type->basetype == LY_TYPE_UNION) {
7228 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7229 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7230 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7231 typeiter->basetype == LY_TYPE_LEAFREF;
7232 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7233 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7234 }
7235 }
7236 }
7237 }
7238
7239 /* check xpath */
7240 for (i = 0; i < ctx->xpath.count; ++i) {
7241 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7242 }
7243
7244 /* finish incomplete default values compilation */
7245 for (i = 0; i < ctx->dflts.count; ++i) {
7246 struct ly_err_item *err = NULL;
7247 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
7248 LY_ERR ret;
7249 ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007250 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL,
7251 LY_PREF_SCHEMA, r->dflt_mod, r->context_node, NULL, r->dflt, NULL, &err);
Michal Vasko004d3152020-06-11 19:59:22 +02007252 if (err) {
7253 ly_err_print(err);
7254 ctx->path[0] = '\0';
7255 lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7257 "Invalid default - value does not fit the type (%s).", err->msg);
7258 ly_err_free(err);
7259 }
7260 LY_CHECK_RET(ret);
7261 }
7262
7263 return LY_SUCCESS;
7264}
7265
Radek Krejci19a96102018-11-15 13:38:09 +01007266LY_ERR
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007267lys_compile(struct lys_module **mod, int options)
Radek Krejci19a96102018-11-15 13:38:09 +01007268{
7269 struct lysc_ctx ctx = {0};
7270 struct lysc_module *mod_c;
7271 struct lysp_module *sp;
7272 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007273 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007274 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007275 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007276 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007277 uint32_t i;
Radek Krejcia46012b2020-08-12 15:41:04 +02007278 uint16_t compile_id;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007279 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007280
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007281 LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007282
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007283 if (!(*mod)->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007284 /* just imported modules are not compiled */
7285 return LY_SUCCESS;
7286 }
7287
Radek Krejcia46012b2020-08-12 15:41:04 +02007288 compile_id = ++(*mod)->ctx->module_set_id;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007289 sp = (*mod)->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007290
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007291 ctx.ctx = (*mod)->ctx;
7292 ctx.mod = *mod;
7293 ctx.mod_def = *mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007294 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007295 ctx.path_len = 1;
7296 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007297
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007298 (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
7299 LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
7300 mod_c->mod = *mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007301
Michal Vasko7c8439f2020-08-05 13:25:19 +02007302 LY_ARRAY_FOR(sp->imports, u) {
7303 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
7304 }
Radek Krejcid05cbd92018-12-05 14:26:40 +01007305 LY_ARRAY_FOR(sp->includes, u) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02007306 LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007307 }
Radek Krejci0935f412019-08-20 16:15:18 +02007308
7309 /* features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007310 if ((*mod)->dis_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007311 /* there is already precompiled array of features */
Michal Vasko33ff9422020-07-03 09:50:39 +02007312 mod_c->features = (*mod)->dis_features;
7313 (*mod)->dis_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007314 } else {
7315 /* features are compiled directly into the compiled module structure,
7316 * 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 +02007317 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007318 LY_CHECK_GOTO(ret, error);
7319 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007320
Radek Krejci0af46292019-01-11 16:02:31 +01007321 /* finish feature compilation, not only for the main module, but also for the submodules.
7322 * Due to possible forward references, it must be done when all the features (including submodules)
7323 * are present. */
7324 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007325 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007326 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7327 }
Radek Krejci327de162019-06-14 12:52:07 +02007328 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007329 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007330 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007331 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007332 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007333 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7334 }
Radek Krejci327de162019-06-14 12:52:07 +02007335 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007336 }
Radek Krejci327de162019-06-14 12:52:07 +02007337 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007338
Michal Vasko33ff9422020-07-03 09:50:39 +02007339 /* identities, work similarly to features with the precompilation */
7340 if ((*mod)->dis_identities) {
7341 mod_c->identities = (*mod)->dis_identities;
7342 (*mod)->dis_identities = NULL;
7343 } else {
7344 ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
7345 LY_CHECK_GOTO(ret, error);
7346 }
Radek Krejci19a96102018-11-15 13:38:09 +01007347 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007348 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007349 }
Michal Vasko33ff9422020-07-03 09:50:39 +02007350 lysc_update_path(&ctx, NULL, "{submodule}");
7351 LY_ARRAY_FOR(sp->includes, v) {
7352 if (sp->includes[v].submodule->identities) {
7353 lysc_update_path(&ctx, NULL, sp->includes[v].name);
7354 ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities);
7355 LY_CHECK_GOTO(ret, error);
7356 lysc_update_path(&ctx, NULL, NULL);
7357 }
7358 }
Radek Krejci327de162019-06-14 12:52:07 +02007359 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007360
Radek Krejci95710c92019-02-11 15:49:55 +01007361 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007362 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007363 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007364 }
Radek Krejci95710c92019-02-11 15:49:55 +01007365
Radek Krejciec4da802019-05-02 13:02:41 +02007366 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7367 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007368
Radek Krejci95710c92019-02-11 15:49:55 +01007369 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007370 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007371 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007372 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007373 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007374
Radek Krejci474f9b82019-07-24 11:36:37 +02007375 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007376 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007377
Radek Krejci0935f412019-08-20 16:15:18 +02007378 /* extension instances TODO cover extension instances from submodules */
7379 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007380
Michal Vasko004d3152020-06-11 19:59:22 +02007381 /* finish compilation for all unresolved items in the context */
7382 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007383
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007384 /* validate non-instantiated groupings from the parsed schema,
7385 * without it we would accept even the schemas with invalid grouping specification */
7386 ctx.options |= LYSC_OPT_GROUPING;
7387 LY_ARRAY_FOR(sp->groupings, u) {
7388 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007389 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007390 }
7391 }
7392 LY_LIST_FOR(sp->data, node_p) {
7393 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7394 LY_ARRAY_FOR(grps, u) {
7395 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007396 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007397 }
7398 }
7399 }
7400
Radek Krejci474f9b82019-07-24 11:36:37 +02007401 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7402 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7403 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7404 }
7405
Michal Vasko8d544252020-03-02 10:19:52 +01007406#if 0
7407 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7408 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7409 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7410 * the anotation definitions available in the internal schema structure. */
7411 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7412 if (lyp_add_ietf_netconf_annotations(mod)) {
7413 lys_free(mod, NULL, 1, 1);
7414 return NULL;
7415 }
7416 }
7417#endif
7418
7419 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007420 if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
7421 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007422 }
7423
Radek Krejci1c0c3442019-07-23 16:08:47 +02007424 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007425 ly_set_erase(&ctx.xpath, NULL);
7426 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007427 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007428 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007429 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007430
Radek Krejciec4da802019-05-02 13:02:41 +02007431 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007432 lysp_module_free((*mod)->parsed);
7433 (*mod)->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007434 }
7435
Radek Krejciec4da802019-05-02 13:02:41 +02007436 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007437 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007438 for (i = 0; i < ctx.ctx->list.count; ++i) {
7439 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007440 if (m->implemented > 1) {
Radek Krejci95710c92019-02-11 15:49:55 +01007441 m->implemented = 1;
7442 }
7443 }
7444 }
7445
Radek Krejci19a96102018-11-15 13:38:09 +01007446 return LY_SUCCESS;
7447
7448error:
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007449 lys_feature_precompile_revert(&ctx, *mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007450 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007451 ly_set_erase(&ctx.xpath, NULL);
7452 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007453 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007454 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007455 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007456 lysc_module_free(mod_c, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007457 (*mod)->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007458
Radek Krejcia46012b2020-08-12 15:41:04 +02007459 /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency
7460 * of this module, since this module can be also compiled from dependency, there can be some other modules being
7461 * processed and we are going to get back to them via stack, so freeing them is not a good idea. */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007462 for (i = 0; i < ctx.ctx->list.count; ++i) {
7463 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02007464 if ((m->implemented >= compile_id) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007465 /* revert features list to the precompiled state */
7466 lys_feature_precompile_revert(&ctx, m);
7467 /* mark module as imported-only / not-implemented */
7468 m->implemented = 0;
7469 /* free the compiled version of the module */
7470 lysc_module_free(m->compiled, NULL);
7471 m->compiled = NULL;
7472 }
7473 }
7474
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007475 /* remove the module itself from the context and free it */
7476 ly_set_rm(&ctx.ctx->list, *mod, NULL);
7477 lys_module_free(*mod, NULL);
7478 *mod = NULL;
7479
Radek Krejci19a96102018-11-15 13:38:09 +01007480 return ret;
7481}