blob: 058b19accf053c86488f0a2300b67917e37ecd02 [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"
26#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "dict.h"
28#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020029#include "path.h"
Radek Krejci0935f412019-08-20 16:15:18 +020030#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020032#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020036#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010037#include "tree_schema_internal.h"
38#include "xpath.h"
39
Michal Vasko5fe75f12020-03-02 13:52:37 +010040static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
41 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
42
Radek Krejci19a96102018-11-15 13:38:09 +010043/**
44 * @brief Duplicate string into dictionary
45 * @param[in] CTX libyang context of the dictionary.
46 * @param[in] ORIG String to duplicate.
47 * @param[out] DUP Where to store the result.
48 */
49#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
50
Radek Krejciec4da802019-05-02 13:02:41 +020051#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010052 if (ARRAY_P) { \
53 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020054 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010055 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
56 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020057 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010058 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
59 } \
60 }
61
Radek Krejciec4da802019-05-02 13:02:41 +020062#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010063 if (ARRAY_P) { \
64 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020065 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010066 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
67 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020068 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010069 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
70 } \
71 }
72
Radek Krejci0935f412019-08-20 16:15:18 +020073#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
74 if (EXTS_P) { \
75 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020076 for (LY_ARRAY_SIZE_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \
Radek Krejci0935f412019-08-20 16:15:18 +020077 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010078 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 +020079 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
80 } \
81 }
82
Radek Krejciec4da802019-05-02 13:02:41 +020083#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010084 if (ARRAY_P) { \
85 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020086 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010087 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
88 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020089 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010090 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
91 } \
92 }
93
Radek Krejciec4da802019-05-02 13:02:41 +020094#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010095 if (MEMBER_P) { \
96 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
97 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +020098 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010099 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
100 }
101
Radek Krejciec4da802019-05-02 13:02:41 +0200102#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100103 if (MEMBER_P) { \
104 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200105 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100106 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200107 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100108 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
109 }
110
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100111#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100112 if (ARRAY) { \
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200113 for (LY_ARRAY_SIZE_TYPE u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100114 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100115 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
116 return LY_EVALID; \
117 } \
118 } \
119 }
120
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100121#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
122 if (ARRAY) { \
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200123 for (LY_ARRAY_SIZE_TYPE u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100124 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
125 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
126 return LY_EVALID; \
127 } \
128 } \
129 }
130
131struct lysc_ext *
132lysc_ext_dup(struct lysc_ext *orig)
133{
134 ++orig->refcount;
135 return orig;
136}
137
Radek Krejci19a96102018-11-15 13:38:09 +0100138static struct lysc_ext_instance *
139lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
140{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100141 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100142 (void) ctx;
143 (void) orig;
144 return NULL;
145}
146
Radek Krejcib56c7502019-02-13 14:19:54 +0100147/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200148 * @brief Add record into the compile context's list of incomplete default values.
149 * @param[in] ctx Compile context with the incomplete default values list.
150 * @param[in] context_node Context schema node to store in the record.
151 * @param[in] dflt Incomplete default value to store in the record.
152 * @param[in] dflt_mod Module of the default value definition to store in the record.
153 * @return LY_EMEM in case of memory allocation failure.
154 * @return LY_SUCCESS
155 */
156static LY_ERR
157lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
158{
159 struct lysc_incomplete_dflt *r;
160 r = malloc(sizeof *r);
161 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
162 r->context_node = context_node;
163 r->dflt = dflt;
164 r->dflt_mod = dflt_mod;
165 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
166
167 return LY_SUCCESS;
168}
169
170/**
171 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
172 * @param[in] ctx Compile context with the incomplete default values list.
173 * @param[in] dflt Incomplete default values identifying the record to remove.
174 */
175static void
176lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
177{
178 unsigned int u;
179 struct lysc_incomplete_dflt *r;
180
181 for (u = 0; u < ctx->dflts.count; ++u) {
182 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
183 if (r->dflt == dflt) {
184 free(ctx->dflts.objs[u]);
185 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
186 --ctx->dflts.count;
187 return;
188 }
189 }
190}
191
Radek Krejci0e59c312019-08-15 15:34:15 +0200192void
Radek Krejci327de162019-06-14 12:52:07 +0200193lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
194{
195 int len;
196 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
197
198 if (!name) {
199 /* removing last path segment */
200 if (ctx->path[ctx->path_len - 1] == '}') {
201 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
202 if (ctx->path[ctx->path_len] == '=') {
203 ctx->path[ctx->path_len++] = '}';
204 } else {
205 /* not a top-level special tag, remove also preceiding '/' */
206 goto remove_nodelevel;
207 }
208 } else {
209remove_nodelevel:
210 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
211 if (ctx->path_len == 0) {
212 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200213 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200214 }
215 }
216 /* set new terminating NULL-byte */
217 ctx->path[ctx->path_len] = '\0';
218 } else {
219 if (ctx->path_len > 1) {
220 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
221 /* extension of the special tag */
222 nextlevel = 2;
223 --ctx->path_len;
224 } else {
225 /* there is already some path, so add next level */
226 nextlevel = 1;
227 }
228 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
229
230 if (nextlevel != 2) {
231 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
232 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200233 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200234 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200235 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 +0200236 }
237 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200238 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200239 }
Radek Krejciacc79042019-07-25 14:14:57 +0200240 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
241 /* output truncated */
242 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
243 } else {
244 ctx->path_len += len;
245 }
Radek Krejci327de162019-06-14 12:52:07 +0200246 }
247}
248
249/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100250 * @brief Duplicate the compiled pattern structure.
251 *
252 * Instead of duplicating memory, the reference counter in the @p orig is increased.
253 *
254 * @param[in] orig The pattern structure to duplicate.
255 * @return The duplicated structure to use.
256 */
Radek Krejci19a96102018-11-15 13:38:09 +0100257static struct lysc_pattern*
258lysc_pattern_dup(struct lysc_pattern *orig)
259{
260 ++orig->refcount;
261 return orig;
262}
263
Radek Krejcib56c7502019-02-13 14:19:54 +0100264/**
265 * @brief Duplicate the array of compiled patterns.
266 *
267 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
268 *
269 * @param[in] ctx Libyang context for logging.
270 * @param[in] orig The patterns sized array to duplicate.
271 * @return New sized array as a copy of @p orig.
272 * @return NULL in case of memory allocation error.
273 */
Radek Krejci19a96102018-11-15 13:38:09 +0100274static struct lysc_pattern**
275lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
276{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100277 struct lysc_pattern **dup = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200278 LY_ARRAY_SIZE_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100279
Radek Krejcib56c7502019-02-13 14:19:54 +0100280 assert(orig);
281
Radek Krejci19a96102018-11-15 13:38:09 +0100282 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
283 LY_ARRAY_FOR(orig, u) {
284 dup[u] = lysc_pattern_dup(orig[u]);
285 LY_ARRAY_INCREMENT(dup);
286 }
287 return dup;
288}
289
Radek Krejcib56c7502019-02-13 14:19:54 +0100290/**
291 * @brief Duplicate compiled range structure.
292 *
293 * @param[in] ctx Libyang context for logging.
294 * @param[in] orig The range structure to be duplicated.
295 * @return New compiled range structure as a copy of @p orig.
296 * @return NULL in case of memory allocation error.
297 */
Radek Krejci19a96102018-11-15 13:38:09 +0100298struct lysc_range*
299lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
300{
301 struct lysc_range *dup;
302 LY_ERR ret;
303
Radek Krejcib56c7502019-02-13 14:19:54 +0100304 assert(orig);
305
Radek Krejci19a96102018-11-15 13:38:09 +0100306 dup = calloc(1, sizeof *dup);
307 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
308 if (orig->parts) {
309 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
310 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
311 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
312 }
313 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
314 DUP_STRING(ctx, orig->emsg, dup->emsg);
315 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
316
317 return dup;
318cleanup:
319 free(dup);
320 (void) ret; /* set but not used due to the return type */
321 return NULL;
322}
323
Radek Krejcib56c7502019-02-13 14:19:54 +0100324/**
325 * @brief Stack for processing if-feature expressions.
326 */
Radek Krejci19a96102018-11-15 13:38:09 +0100327struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100328 int size; /**< number of items in the stack */
329 int index; /**< first empty item */
330 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100331};
332
Radek Krejcib56c7502019-02-13 14:19:54 +0100333/**
334 * @brief Add @ref ifftokens into the stack.
335 * @param[in] stack The if-feature stack to use.
336 * @param[in] value One of the @ref ifftokens to store in the stack.
337 * @return LY_EMEM in case of memory allocation error
338 * @return LY_ESUCCESS if the value successfully stored.
339 */
Radek Krejci19a96102018-11-15 13:38:09 +0100340static LY_ERR
341iff_stack_push(struct iff_stack *stack, uint8_t value)
342{
343 if (stack->index == stack->size) {
344 stack->size += 4;
345 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
346 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
347 }
348 stack->stack[stack->index++] = value;
349 return LY_SUCCESS;
350}
351
Radek Krejcib56c7502019-02-13 14:19:54 +0100352/**
353 * @brief Get (and remove) the last item form the stack.
354 * @param[in] stack The if-feature stack to use.
355 * @return The value from the top of the stack.
356 */
Radek Krejci19a96102018-11-15 13:38:09 +0100357static uint8_t
358iff_stack_pop(struct iff_stack *stack)
359{
Radek Krejcib56c7502019-02-13 14:19:54 +0100360 assert(stack && stack->index);
361
Radek Krejci19a96102018-11-15 13:38:09 +0100362 stack->index--;
363 return stack->stack[stack->index];
364}
365
Radek Krejcib56c7502019-02-13 14:19:54 +0100366/**
367 * @brief Clean up the stack.
368 * @param[in] stack The if-feature stack to use.
369 */
Radek Krejci19a96102018-11-15 13:38:09 +0100370static void
371iff_stack_clean(struct iff_stack *stack)
372{
373 stack->size = 0;
374 free(stack->stack);
375}
376
Radek Krejcib56c7502019-02-13 14:19:54 +0100377/**
378 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
379 * (libyang format of the if-feature expression).
380 * @param[in,out] list The 2bits array to modify.
381 * @param[in] op The operand (@ref ifftokens) to store.
382 * @param[in] pos Position (0-based) where to store the given @p op.
383 */
Radek Krejci19a96102018-11-15 13:38:09 +0100384static void
385iff_setop(uint8_t *list, uint8_t op, int pos)
386{
387 uint8_t *item;
388 uint8_t mask = 3;
389
390 assert(pos >= 0);
391 assert(op <= 3); /* max 2 bits */
392
393 item = &list[pos / 4];
394 mask = mask << 2 * (pos % 4);
395 *item = (*item) & ~mask;
396 *item = (*item) | (op << 2 * (pos % 4));
397}
398
Radek Krejcib56c7502019-02-13 14:19:54 +0100399#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
400#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100401
Radek Krejci0af46292019-01-11 16:02:31 +0100402/**
403 * @brief Find a feature of the given name and referenced in the given module.
404 *
405 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
406 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
407 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
408 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
409 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
410 * assigned till that time will be still valid.
411 *
412 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
413 * @param[in] name Name of the feature including possible prefix.
414 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
415 * @return Pointer to the feature structure if found, NULL otherwise.
416 */
Radek Krejci19a96102018-11-15 13:38:09 +0100417static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100418lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100419{
420 size_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200421 LY_ARRAY_SIZE_TYPE u;
Radek Krejci0af46292019-01-11 16:02:31 +0100422 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100423
424 for (i = 0; i < len; ++i) {
425 if (name[i] == ':') {
426 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100427 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100428 LY_CHECK_RET(!mod, NULL);
429
430 name = &name[i + 1];
431 len = len - i - 1;
432 }
433 }
434
435 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100436 if (mod->implemented) {
437 /* module is implemented so there is already the compiled schema */
438 flist = mod->compiled->features;
439 } else {
440 flist = mod->off_features;
441 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200442 LY_ARRAY_FOR(flist, u) {
443 f = &flist[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200444 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100445 return f;
446 }
447 }
448
449 return NULL;
450}
451
Michal Vasko8d544252020-03-02 10:19:52 +0100452/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100453 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
454 */
455static LY_ERR
456lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
457{
458 LY_ERR ret = LY_SUCCESS;
459
460 if (!ext_p->compiled) {
461 lysc_update_path(ctx, NULL, "{extension}");
462 lysc_update_path(ctx, NULL, ext_p->name);
463
464 /* compile the extension definition */
465 ext_p->compiled = calloc(1, sizeof **ext);
466 ext_p->compiled->refcount = 1;
467 DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name);
468 DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument);
469 ext_p->compiled->module = (struct lys_module *)ext_mod;
470 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
471
472 lysc_update_path(ctx, NULL, NULL);
473 lysc_update_path(ctx, NULL, NULL);
474
475 /* find extension definition plugin */
476 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
477 }
478
479 *ext = lysc_ext_dup(ext_p->compiled);
480
481done:
482 return ret;
483}
484
485/**
Michal Vasko8d544252020-03-02 10:19:52 +0100486 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
487 *
488 * @param[in] ctx Compilation context.
489 * @param[in] ext_p Parsed extension instance.
490 * @param[in,out] ext Prepared compiled extension instance.
491 * @param[in] parent Extension instance parent.
492 * @param[in] parent_type Extension instance parent type.
493 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
494 */
Radek Krejci19a96102018-11-15 13:38:09 +0100495static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100496lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
497 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100498{
Radek Krejci7c960162019-09-18 14:16:12 +0200499 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100500 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200501 size_t u;
502 LY_ARRAY_SIZE_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200503 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100504
505 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
506 ext->insubstmt = ext_p->insubstmt;
507 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200508 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200509 ext->parent = parent;
510 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100511
Radek Krejcif56e2a42019-09-09 14:15:25 +0200512 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200513
Radek Krejci19a96102018-11-15 13:38:09 +0100514 /* get module where the extension definition should be placed */
Radek Krejci7c960162019-09-18 14:16:12 +0200515 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u);
516 if (ext_p->yin) {
517 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
518 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
519 * YANG (import) prefix must be done here. */
520 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
521 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
522 u = 0;
523 } else if (ctx->mod_def->compiled) {
Radek Krejci7c960162019-09-18 14:16:12 +0200524 LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) {
525 if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) {
526 char *s;
527 asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]);
528 prefixed_name = lydict_insert_zc(ctx->ctx, s);
529 u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */
530 break;
531 }
532 }
533 }
534 if (!prefixed_name) {
535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
536 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
537 goto cleanup;
538 }
539 } else {
540 prefixed_name = ext_p->name;
541 }
542 lysc_update_path(ctx, NULL, prefixed_name);
543
Michal Vasko8d544252020-03-02 10:19:52 +0100544 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200545 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100546 if (!ext_mod) {
547 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
548 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
549 goto cleanup;
550 } else if (!ext_mod->parsed->extensions) {
551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
552 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
553 prefixed_name, ext_mod->name);
554 goto cleanup;
555 }
Radek Krejci7c960162019-09-18 14:16:12 +0200556 }
557 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200558
Michal Vasko5fe75f12020-03-02 13:52:37 +0100559 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200560 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
561 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100562 /* compile extension definition and assign it */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200563 LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def));
Radek Krejci19a96102018-11-15 13:38:09 +0100564 break;
565 }
566 }
Radek Krejci7c960162019-09-18 14:16:12 +0200567 if (!ext->def) {
568 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
569 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
570 goto cleanup;
571 }
Radek Krejci0935f412019-08-20 16:15:18 +0200572
Radek Krejcif56e2a42019-09-09 14:15:25 +0200573 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
574 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
575 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200576 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200577 /* Schema was parsed from YIN and an argument is expected, ... */
578 struct lysp_stmt *stmt = NULL;
579
580 if (ext->def->flags & LYS_YINELEM_TRUE) {
581 /* ... argument was the first XML child element */
582 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
583 /* TODO check namespace of the statement */
584 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
585 stmt = ext_p->child;
586 }
587 }
588 } else {
589 /* ... argument was one of the XML attributes which are represented as child stmt
590 * with LYS_YIN_ATTR flag */
591 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
592 if (!strcmp(stmt->stmt, ext->def->argument)) {
593 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200594 break;
595 }
596 }
597 }
598 if (!stmt) {
599 /* missing extension's argument */
600 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200601 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
602 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200603
604 }
605 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
606 stmt->flags |= LYS_YIN_ARGUMENT;
607 }
Radek Krejci7c960162019-09-18 14:16:12 +0200608 if (prefixed_name != ext_p->name) {
609 lydict_remove(ctx->ctx, ext_p->name);
610 ext_p->name = prefixed_name;
611 if (!ext_p->argument && ext->argument) {
612 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
613 }
614 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200615
Radek Krejci0935f412019-08-20 16:15:18 +0200616 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200617 if (ext->argument) {
618 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
619 }
Radek Krejci7c960162019-09-18 14:16:12 +0200620 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200621 if (ext->argument) {
622 lysc_update_path(ctx, NULL, NULL);
623 }
Radek Krejci0935f412019-08-20 16:15:18 +0200624 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200625 ext_p->compiled = ext;
626
Radek Krejci7c960162019-09-18 14:16:12 +0200627 ret = LY_SUCCESS;
628cleanup:
629 if (prefixed_name && prefixed_name != ext_p->name) {
630 lydict_remove(ctx->ctx, prefixed_name);
631 }
632
Radek Krejcif56e2a42019-09-09 14:15:25 +0200633 lysc_update_path(ctx, NULL, NULL);
634 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200635
Radek Krejci7c960162019-09-18 14:16:12 +0200636 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200637}
638
639/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100640 * @brief Compile information from the if-feature statement
641 * @param[in] ctx Compile context.
642 * @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 +0100643 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
644 * @return LY_ERR value.
645 */
Radek Krejci19a96102018-11-15 13:38:09 +0100646static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200647lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100648{
649 const char *c = *value;
650 int r, rc = EXIT_FAILURE;
651 int i, j, last_not, checkversion = 0;
652 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
653 uint8_t op;
654 struct iff_stack stack = {0, 0, NULL};
655 struct lysc_feature *f;
656
657 assert(c);
658
659 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
660 for (i = j = last_not = 0; c[i]; i++) {
661 if (c[i] == '(') {
662 j++;
663 checkversion = 1;
664 continue;
665 } else if (c[i] == ')') {
666 j--;
667 continue;
668 } else if (isspace(c[i])) {
669 checkversion = 1;
670 continue;
671 }
672
673 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 +0200674 int sp;
675 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
676 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
678 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
679 return LY_EVALID;
680 } else if (!isspace(c[i + r])) {
681 /* feature name starting with the not/and/or */
682 last_not = 0;
683 f_size++;
684 } else if (c[i] == 'n') { /* not operation */
685 if (last_not) {
686 /* double not */
687 expr_size = expr_size - 2;
688 last_not = 0;
689 } else {
690 last_not = 1;
691 }
692 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200693 if (f_exp != f_size) {
694 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
695 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
696 return LY_EVALID;
697 }
Radek Krejci19a96102018-11-15 13:38:09 +0100698 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200699
Radek Krejci19a96102018-11-15 13:38:09 +0100700 /* not a not operation */
701 last_not = 0;
702 }
703 i += r;
704 } else {
705 f_size++;
706 last_not = 0;
707 }
708 expr_size++;
709
710 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200711 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100712 i--;
713 break;
714 }
715 i++;
716 }
717 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200718 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100719 /* not matching count of ( and ) */
720 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
721 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
722 return LY_EVALID;
723 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200724 if (f_exp != f_size) {
725 /* features do not match the needed arguments for the logical operations */
726 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
727 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
728 "the required number of operands for the operations.", *value);
729 return LY_EVALID;
730 }
Radek Krejci19a96102018-11-15 13:38:09 +0100731
732 if (checkversion || expr_size > 1) {
733 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100734 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
736 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
737 return LY_EVALID;
738 }
739 }
740
741 /* allocate the memory */
742 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
743 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
744 stack.stack = malloc(expr_size * sizeof *stack.stack);
745 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
746
747 stack.size = expr_size;
748 f_size--; expr_size--; /* used as indexes from now */
749
750 for (i--; i >= 0; i--) {
751 if (c[i] == ')') {
752 /* push it on stack */
753 iff_stack_push(&stack, LYS_IFF_RP);
754 continue;
755 } else if (c[i] == '(') {
756 /* pop from the stack into result all operators until ) */
757 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
758 iff_setop(iff->expr, op, expr_size--);
759 }
760 continue;
761 } else if (isspace(c[i])) {
762 continue;
763 }
764
765 /* end of operator or operand -> find beginning and get what is it */
766 j = i + 1;
767 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
768 i--;
769 }
770 i++; /* go back by one step */
771
772 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
773 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
774 /* double not */
775 iff_stack_pop(&stack);
776 } else {
777 /* not has the highest priority, so do not pop from the stack
778 * as in case of AND and OR */
779 iff_stack_push(&stack, LYS_IFF_NOT);
780 }
781 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
782 /* as for OR - pop from the stack all operators with the same or higher
783 * priority and store them to the result, then push the AND to the stack */
784 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
785 op = iff_stack_pop(&stack);
786 iff_setop(iff->expr, op, expr_size--);
787 }
788 iff_stack_push(&stack, LYS_IFF_AND);
789 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
790 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
791 op = iff_stack_pop(&stack);
792 iff_setop(iff->expr, op, expr_size--);
793 }
794 iff_stack_push(&stack, LYS_IFF_OR);
795 } else {
796 /* feature name, length is j - i */
797
798 /* add it to the expression */
799 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
800
801 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100802 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
803 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
804 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
805 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100806 iff->features[f_size] = f;
807 LY_ARRAY_INCREMENT(iff->features);
808 f_size--;
809 }
810 }
811 while (stack.index) {
812 op = iff_stack_pop(&stack);
813 iff_setop(iff->expr, op, expr_size--);
814 }
815
816 if (++expr_size || ++f_size) {
817 /* not all expected operators and operands found */
818 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
819 "Invalid value \"%s\" of if-feature - processing error.", *value);
820 rc = LY_EINT;
821 } else {
822 rc = LY_SUCCESS;
823 }
824
825error:
826 /* cleanup */
827 iff_stack_clean(&stack);
828
829 return rc;
830}
831
Radek Krejcib56c7502019-02-13 14:19:54 +0100832/**
Michal Vasko175012e2019-11-06 15:49:14 +0100833 * @brief Get the XPath context node for the given schema node.
834 * @param[in] start The schema node where the XPath expression appears.
835 * @return The context node to evaluate XPath expression in given schema node.
836 * @return NULL in case the context node is the root node.
837 */
838static struct lysc_node *
839lysc_xpath_context(struct lysc_node *start)
840{
Michal Vasko1bf09392020-03-27 12:38:10 +0100841 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 +0100842 start = start->parent);
843 return start;
844}
845
846/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100847 * @brief Compile information from the when statement
848 * @param[in] ctx Compile context.
849 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100850 * @param[in] flags Flags of the node with the "when" defiition.
851 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100852 * @param[out] when Pointer where to store pointer to the created compiled when structure.
853 * @return LY_ERR value.
854 */
Radek Krejci19a96102018-11-15 13:38:09 +0100855static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100856lys_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 +0100857{
Radek Krejci19a96102018-11-15 13:38:09 +0100858 LY_ERR ret = LY_SUCCESS;
859
Radek Krejci00b874b2019-02-12 10:54:50 +0100860 *when = calloc(1, sizeof **when);
861 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200862 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200863 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100864 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100865 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
866 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
867 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200868 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100869 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100870
871done:
872 return ret;
873}
874
Radek Krejcib56c7502019-02-13 14:19:54 +0100875/**
876 * @brief Compile information from the must statement
877 * @param[in] ctx Compile context.
878 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100879 * @param[in,out] must Prepared (empty) compiled must structure to fill.
880 * @return LY_ERR value.
881 */
Radek Krejci19a96102018-11-15 13:38:09 +0100882static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200883lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100884{
Radek Krejci19a96102018-11-15 13:38:09 +0100885 LY_ERR ret = LY_SUCCESS;
886
Michal Vasko004d3152020-06-11 19:59:22 +0200887 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100888 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200889 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100890 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
891 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100892 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
893 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200894 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100895
896done:
897 return ret;
898}
899
Radek Krejcib56c7502019-02-13 14:19:54 +0100900/**
901 * @brief Compile information from the import statement
902 * @param[in] ctx Compile context.
903 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100904 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
905 * @return LY_ERR value.
906 */
Radek Krejci19a96102018-11-15 13:38:09 +0100907static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200908lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100909{
Radek Krejci19a96102018-11-15 13:38:09 +0100910 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100911 LY_ERR ret = LY_SUCCESS;
912
913 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200914 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100915 imp->module = imp_p->module;
916
Radek Krejci7f2a5362018-11-28 13:05:37 +0100917 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
918 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100919 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100920 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100921 /* try to use filepath if present */
922 if (imp->module->filepath) {
923 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
924 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100925 if (mod != imp->module) {
926 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100927 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100928 mod = NULL;
929 }
930 }
931 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100932 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100933 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100934 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100935 return LY_ENOTFOUND;
936 }
937 }
Radek Krejci19a96102018-11-15 13:38:09 +0100938 }
939
940done:
941 return ret;
942}
943
Radek Krejcib56c7502019-02-13 14:19:54 +0100944/**
945 * @brief Compile information from the identity statement
946 *
947 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
948 *
949 * @param[in] ctx Compile context.
950 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100951 * @param[in] idents List of so far compiled identities to check the name uniqueness.
952 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
953 * @return LY_ERR value.
954 */
Radek Krejci19a96102018-11-15 13:38:09 +0100955static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200956lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, struct lysc_ident *idents, struct lysc_ident *ident)
Radek Krejci19a96102018-11-15 13:38:09 +0100957{
958 unsigned int u;
959 LY_ERR ret = LY_SUCCESS;
960
Radek Krejci327de162019-06-14 12:52:07 +0200961 lysc_update_path(ctx, NULL, ident_p->name);
962
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100963 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100964 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200965 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
966 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200967 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200968 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100969 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
Radek Krejci0935f412019-08-20 16:15:18 +0200970 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100971 ident->flags = ident_p->flags;
972
Radek Krejci327de162019-06-14 12:52:07 +0200973 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100974done:
975 return ret;
976}
977
Radek Krejcib56c7502019-02-13 14:19:54 +0100978/**
979 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
980 *
981 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
982 *
983 * @param[in] ctx Compile context for logging.
984 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
985 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
986 * @return LY_SUCCESS if everything is ok.
987 * @return LY_EVALID if the identity is derived from itself.
988 */
Radek Krejci38222632019-02-12 16:55:05 +0100989static LY_ERR
990lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
991{
992 LY_ERR ret = LY_EVALID;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200993 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +0100994 struct ly_set recursion = {0};
995 struct lysc_ident *drv;
996
997 if (!derived) {
998 return LY_SUCCESS;
999 }
1000
1001 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
1002 if (ident == derived[u]) {
1003 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1004 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1005 goto cleanup;
1006 }
1007 ly_set_add(&recursion, derived[u], 0);
1008 }
1009
1010 for (v = 0; v < recursion.count; ++v) {
1011 drv = recursion.objs[v];
1012 if (!drv->derived) {
1013 continue;
1014 }
1015 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
1016 if (ident == drv->derived[u]) {
1017 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1018 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1019 goto cleanup;
1020 }
1021 ly_set_add(&recursion, drv->derived[u], 0);
1022 }
1023 }
1024 ret = LY_SUCCESS;
1025
1026cleanup:
1027 ly_set_erase(&recursion, NULL);
1028 return ret;
1029}
1030
Radek Krejcia3045382018-11-22 14:30:31 +01001031/**
1032 * @brief Find and process the referenced base identities from another identity or identityref
1033 *
Radek Krejciaca74032019-06-04 08:53:06 +02001034 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001035 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1036 * to distinguish these two use cases.
1037 *
1038 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1039 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
1040 * @param[in] ident Referencing identity to work with.
1041 * @param[in] bases Array of bases of identityref to fill in.
1042 * @return LY_ERR value.
1043 */
Radek Krejci19a96102018-11-15 13:38:09 +01001044static LY_ERR
Radek Krejci0a33b042020-05-27 10:05:06 +02001045lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001046{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001047 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001048 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001049 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001050 struct lysc_ident **idref;
1051
1052 assert(ident || bases);
1053
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001054 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001055 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1056 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1057 return LY_EVALID;
1058 }
1059
1060 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
1061 s = strchr(bases_p[u], ':');
1062 if (s) {
1063 /* prefixed identity */
1064 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001065 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001066 } else {
1067 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001068 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001069 }
1070 if (!mod) {
1071 if (ident) {
1072 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1073 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1074 } else {
1075 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1076 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1077 }
1078 return LY_EVALID;
1079 }
1080 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001081 if (mod->compiled && mod->compiled->identities) {
1082 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
1083 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001084 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +01001085 if (ident == &mod->compiled->identities[v]) {
1086 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1087 "Identity \"%s\" is derived from itself.", ident->name);
1088 return LY_EVALID;
1089 }
1090 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001091 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +01001092 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001093 *idref = ident;
1094 } else {
1095 /* we have match! store the found identity */
1096 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01001097 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001098 }
1099 break;
1100 }
1101 }
1102 }
1103 if (!idref || !(*idref)) {
1104 if (ident) {
1105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1106 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1107 } else {
1108 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1109 "Unable to find base (%s) of identityref.", bases_p[u]);
1110 }
1111 return LY_EVALID;
1112 }
1113 }
1114 return LY_SUCCESS;
1115}
1116
Radek Krejcia3045382018-11-22 14:30:31 +01001117/**
1118 * @brief For the given array of identities, set the backlinks from all their base identities.
1119 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1120 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1121 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1122 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1123 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001124static LY_ERR
1125lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1126{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001127 LY_ARRAY_SIZE_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001128
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001129 for (u = 0; u < LY_ARRAY_SIZE(idents_p); ++u) {
1130 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001131 continue;
1132 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001133 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001134 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 +02001135 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001136 }
1137 return LY_SUCCESS;
1138}
1139
Radek Krejci0af46292019-01-11 16:02:31 +01001140LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001141lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001142{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001143 LY_ARRAY_SIZE_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001144 struct lysc_ctx context = {0};
1145
Radek Krejci327de162019-06-14 12:52:07 +02001146 assert(ctx_sc || ctx);
1147
1148 if (!ctx_sc) {
1149 context.ctx = ctx;
1150 context.mod = module;
1151 context.path_len = 1;
1152 context.path[0] = '/';
1153 ctx_sc = &context;
1154 }
Radek Krejci0af46292019-01-11 16:02:31 +01001155
1156 if (!features_p) {
1157 return LY_SUCCESS;
1158 }
1159 if (*features) {
1160 offset = LY_ARRAY_SIZE(*features);
1161 }
1162
Radek Krejci327de162019-06-14 12:52:07 +02001163 lysc_update_path(ctx_sc, NULL, "{feature}");
1164 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001165 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001166 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1167
Radek Krejci0af46292019-01-11 16:02:31 +01001168 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001169 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci327de162019-06-14 12:52:07 +02001170 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1171 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1172 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001173 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001174 (*features)[offset + u].module = ctx_sc->mod;
1175
1176 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001177 }
Radek Krejci327de162019-06-14 12:52:07 +02001178 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001179
1180 return LY_SUCCESS;
1181}
1182
Radek Krejcia3045382018-11-22 14:30:31 +01001183/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001184 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001185 *
1186 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1187 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001188 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001189 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1190 * being currently processed).
1191 * @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 +01001192 * @return LY_SUCCESS if everything is ok.
1193 * @return LY_EVALID if the feature references indirectly itself.
1194 */
1195static LY_ERR
1196lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1197{
1198 LY_ERR ret = LY_EVALID;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001199 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001200 struct ly_set recursion = {0};
1201 struct lysc_feature *drv;
1202
1203 if (!depfeatures) {
1204 return LY_SUCCESS;
1205 }
1206
1207 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1208 if (feature == depfeatures[u]) {
1209 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1210 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1211 goto cleanup;
1212 }
1213 ly_set_add(&recursion, depfeatures[u], 0);
1214 }
1215
1216 for (v = 0; v < recursion.count; ++v) {
1217 drv = recursion.objs[v];
1218 if (!drv->depfeatures) {
1219 continue;
1220 }
1221 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1222 if (feature == drv->depfeatures[u]) {
1223 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1224 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1225 goto cleanup;
1226 }
1227 ly_set_add(&recursion, drv->depfeatures[u], 0);
1228 }
1229 }
1230 ret = LY_SUCCESS;
1231
1232cleanup:
1233 ly_set_erase(&recursion, NULL);
1234 return ret;
1235}
1236
1237/**
Radek Krejci0af46292019-01-11 16:02:31 +01001238 * @brief Create pre-compiled features array.
1239 *
1240 * See lys_feature_precompile() for more details.
1241 *
Radek Krejcia3045382018-11-22 14:30:31 +01001242 * @param[in] ctx Compile context.
1243 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001244 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001245 * @return LY_ERR value.
1246 */
Radek Krejci19a96102018-11-15 13:38:09 +01001247static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001248lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001249{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001250 LY_ARRAY_SIZE_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001251 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001252 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001253
Radek Krejci327de162019-06-14 12:52:07 +02001254
Radek Krejci0af46292019-01-11 16:02:31 +01001255 /* find the preprecompiled feature */
1256 LY_ARRAY_FOR(features, x) {
1257 if (strcmp(features[x].name, feature_p->name)) {
1258 continue;
1259 }
1260 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001261 lysc_update_path(ctx, NULL, "{feature}");
1262 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001263
Radek Krejci0af46292019-01-11 16:02:31 +01001264 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001265 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001266 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001267 if (feature->iffeatures) {
1268 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1269 if (feature->iffeatures[u].features) {
1270 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001271 /* check for circular dependency - direct reference first,... */
1272 if (feature == feature->iffeatures[u].features[v]) {
1273 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1274 "Feature \"%s\" is referenced from itself.", feature->name);
1275 return LY_EVALID;
1276 }
1277 /* ... and indirect circular reference */
1278 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1279
Radek Krejci0af46292019-01-11 16:02:31 +01001280 /* add itself into the dependants list */
1281 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1282 *df = feature;
1283 }
Radek Krejci19a96102018-11-15 13:38:09 +01001284 }
Radek Krejci19a96102018-11-15 13:38:09 +01001285 }
1286 }
Radek Krejci327de162019-06-14 12:52:07 +02001287 lysc_update_path(ctx, NULL, NULL);
1288 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001289 done:
1290 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001291 }
Radek Krejci0af46292019-01-11 16:02:31 +01001292
1293 LOGINT(ctx->ctx);
1294 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001295}
1296
Radek Krejcib56c7502019-02-13 14:19:54 +01001297/**
1298 * @brief Revert compiled list of features back to the precompiled state.
1299 *
1300 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1301 * The features are supposed to be stored again as off_features in ::lys_module structure.
1302 *
1303 * @param[in] ctx Compilation context.
1304 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1305 * and supposed to hold the off_features list.
1306 */
Radek Krejci95710c92019-02-11 15:49:55 +01001307static void
1308lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1309{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001310 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001311
1312 /* keep the off_features list until the complete lys_module is freed */
1313 mod->off_features = mod->compiled->features;
1314 mod->compiled->features = NULL;
1315
1316 /* in the off_features list, remove all the parts (from finished compiling process)
1317 * which may points into the data being freed here */
1318 LY_ARRAY_FOR(mod->off_features, u) {
1319 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1320 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1321 }
1322 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1323 mod->off_features[u].iffeatures = NULL;
1324
1325 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1326 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1327 }
1328 LY_ARRAY_FREE(mod->off_features[u].exts);
1329 mod->off_features[u].exts = NULL;
1330 }
1331}
1332
Radek Krejcia3045382018-11-22 14:30:31 +01001333/**
1334 * @brief Validate and normalize numeric value from a range definition.
1335 * @param[in] ctx Compile context.
1336 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1337 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1338 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1339 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1340 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1341 * @param[in] value String value of the range boundary.
1342 * @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.
1343 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1344 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1345 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001346LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001347range_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 +01001348{
Radek Krejci6cba4292018-11-15 17:33:29 +01001349 size_t fraction = 0, size;
1350
Radek Krejci19a96102018-11-15 13:38:09 +01001351 *len = 0;
1352
1353 assert(value);
1354 /* parse value */
1355 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1356 return LY_EVALID;
1357 }
1358
1359 if ((value[*len] == '-') || (value[*len] == '+')) {
1360 ++(*len);
1361 }
1362
1363 while (isdigit(value[*len])) {
1364 ++(*len);
1365 }
1366
1367 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001368 if (basetype == LY_TYPE_DEC64) {
1369 goto decimal;
1370 } else {
1371 *valcopy = strndup(value, *len);
1372 return LY_SUCCESS;
1373 }
Radek Krejci19a96102018-11-15 13:38:09 +01001374 }
1375 fraction = *len;
1376
1377 ++(*len);
1378 while (isdigit(value[*len])) {
1379 ++(*len);
1380 }
1381
Radek Krejci6cba4292018-11-15 17:33:29 +01001382 if (basetype == LY_TYPE_DEC64) {
1383decimal:
1384 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001385 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001386 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1387 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1388 *len, value, frdigits);
1389 return LY_EINVAL;
1390 }
1391 if (fraction) {
1392 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1393 } else {
1394 size = (*len) + frdigits + 1;
1395 }
1396 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001397 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1398
Radek Krejci6cba4292018-11-15 17:33:29 +01001399 (*valcopy)[size - 1] = '\0';
1400 if (fraction) {
1401 memcpy(&(*valcopy)[0], &value[0], fraction);
1402 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1403 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1404 } else {
1405 memcpy(&(*valcopy)[0], &value[0], *len);
1406 memset(&(*valcopy)[*len], '0', frdigits);
1407 }
Radek Krejci19a96102018-11-15 13:38:09 +01001408 }
1409 return LY_SUCCESS;
1410}
1411
Radek Krejcia3045382018-11-22 14:30:31 +01001412/**
1413 * @brief Check that values in range are in ascendant order.
1414 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001415 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1416 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001417 * @param[in] value Current value to check.
1418 * @param[in] prev_value The last seen value.
1419 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1420 */
Radek Krejci19a96102018-11-15 13:38:09 +01001421static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001422range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001423{
1424 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001425 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001426 return LY_EEXIST;
1427 }
1428 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001429 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001430 return LY_EEXIST;
1431 }
1432 }
1433 return LY_SUCCESS;
1434}
1435
Radek Krejcia3045382018-11-22 14:30:31 +01001436/**
1437 * @brief Set min/max value of the range part.
1438 * @param[in] ctx Compile context.
1439 * @param[in] part Range part structure to fill.
1440 * @param[in] max Flag to distinguish if storing min or max value.
1441 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1442 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1443 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1444 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1445 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001446 * @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 +01001447 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1448 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1449 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1450 * frdigits value), LY_EMEM.
1451 */
Radek Krejci19a96102018-11-15 13:38:09 +01001452static LY_ERR
1453range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr,
Radek Krejci5969f272018-11-23 10:03:58 +01001454 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001455{
1456 LY_ERR ret = LY_SUCCESS;
1457 char *valcopy = NULL;
1458 size_t len;
1459
1460 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001461 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001462 LY_CHECK_GOTO(ret, finalize);
1463 }
1464 if (!valcopy && base_range) {
1465 if (max) {
1466 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1467 } else {
1468 part->min_64 = base_range->parts[0].min_64;
1469 }
1470 if (!first) {
1471 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1472 }
1473 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001474 }
1475
1476 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001477 case LY_TYPE_INT8: /* range */
1478 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001479 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 +01001480 } else if (max) {
1481 part->max_64 = INT64_C(127);
1482 } else {
1483 part->min_64 = INT64_C(-128);
1484 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001485 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001486 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001487 }
1488 break;
1489 case LY_TYPE_INT16: /* range */
1490 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001491 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 +01001492 } else if (max) {
1493 part->max_64 = INT64_C(32767);
1494 } else {
1495 part->min_64 = INT64_C(-32768);
1496 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001497 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001498 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001499 }
1500 break;
1501 case LY_TYPE_INT32: /* range */
1502 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001503 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 +01001504 } else if (max) {
1505 part->max_64 = INT64_C(2147483647);
1506 } else {
1507 part->min_64 = INT64_C(-2147483648);
1508 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001509 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001510 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001511 }
1512 break;
1513 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001514 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001515 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001516 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001517 max ? &part->max_64 : &part->min_64);
1518 } else if (max) {
1519 part->max_64 = INT64_C(9223372036854775807);
1520 } else {
1521 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1522 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001523 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001524 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001525 }
1526 break;
1527 case LY_TYPE_UINT8: /* range */
1528 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001529 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 +01001530 } else if (max) {
1531 part->max_u64 = UINT64_C(255);
1532 } else {
1533 part->min_u64 = UINT64_C(0);
1534 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001535 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001536 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001537 }
1538 break;
1539 case LY_TYPE_UINT16: /* range */
1540 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001541 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 +01001542 } else if (max) {
1543 part->max_u64 = UINT64_C(65535);
1544 } else {
1545 part->min_u64 = UINT64_C(0);
1546 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001547 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001548 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001549 }
1550 break;
1551 case LY_TYPE_UINT32: /* range */
1552 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001553 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 +01001554 } else if (max) {
1555 part->max_u64 = UINT64_C(4294967295);
1556 } else {
1557 part->min_u64 = UINT64_C(0);
1558 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001559 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001560 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001561 }
1562 break;
1563 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001564 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001565 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001566 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001567 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 +01001568 } else if (max) {
1569 part->max_u64 = UINT64_C(18446744073709551615);
1570 } else {
1571 part->min_u64 = UINT64_C(0);
1572 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001573 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001574 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001575 }
1576 break;
1577 default:
1578 LOGINT(ctx->ctx);
1579 ret = LY_EINT;
1580 }
1581
Radek Krejci5969f272018-11-23 10:03:58 +01001582finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001583 if (ret == LY_EDENIED) {
1584 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1585 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1586 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1587 } else if (ret == LY_EVALID) {
1588 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1589 "Invalid %s restriction - invalid value \"%s\".",
1590 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1591 } else if (ret == LY_EEXIST) {
1592 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1593 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001594 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001595 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001596 } else if (!ret && value) {
1597 *value = *value + len;
1598 }
1599 free(valcopy);
1600 return ret;
1601}
1602
Radek Krejcia3045382018-11-22 14:30:31 +01001603/**
1604 * @brief Compile the parsed range restriction.
1605 * @param[in] ctx Compile context.
1606 * @param[in] range_p Parsed range structure to compile.
1607 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1608 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1609 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1610 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1611 * range restriction must be more restrictive than the base_range.
1612 * @param[in,out] range Pointer to the created current range structure.
1613 * @return LY_ERR value.
1614 */
Radek Krejci19a96102018-11-15 13:38:09 +01001615static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001616lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits,
Radek Krejci19a96102018-11-15 13:38:09 +01001617 struct lysc_range *base_range, struct lysc_range **range)
1618{
1619 LY_ERR ret = LY_EVALID;
1620 const char *expr;
1621 struct lysc_range_part *parts = NULL, *part;
1622 int range_expected = 0, uns;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001623 LY_ARRAY_SIZE_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001624
1625 assert(range);
1626 assert(range_p);
1627
1628 expr = range_p->arg;
1629 while(1) {
1630 if (isspace(*expr)) {
1631 ++expr;
1632 } else if (*expr == '\0') {
1633 if (range_expected) {
1634 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1635 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1636 length_restr ? "length" : "range", range_p->arg);
1637 goto cleanup;
1638 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1639 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1640 "Invalid %s restriction - unexpected end of the expression (%s).",
1641 length_restr ? "length" : "range", range_p->arg);
1642 goto cleanup;
1643 }
1644 parts_done++;
1645 break;
1646 } else if (!strncmp(expr, "min", 3)) {
1647 if (parts) {
1648 /* min cannot be used elsewhere than in the first part */
1649 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1650 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1651 expr - range_p->arg, range_p->arg);
1652 goto cleanup;
1653 }
1654 expr += 3;
1655
1656 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001657 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 +01001658 part->max_64 = part->min_64;
1659 } else if (*expr == '|') {
1660 if (!parts || range_expected) {
1661 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1662 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1663 goto cleanup;
1664 }
1665 expr++;
1666 parts_done++;
1667 /* process next part of the expression */
1668 } else if (!strncmp(expr, "..", 2)) {
1669 expr += 2;
1670 while (isspace(*expr)) {
1671 expr++;
1672 }
1673 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1674 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1675 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1676 goto cleanup;
1677 }
1678 /* continue expecting the upper boundary */
1679 range_expected = 1;
1680 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1681 /* number */
1682 if (range_expected) {
1683 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001684 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 +01001685 range_expected = 0;
1686 } else {
1687 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1688 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001689 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001690 part->max_64 = part->min_64;
1691 }
1692
1693 /* continue with possible another expression part */
1694 } else if (!strncmp(expr, "max", 3)) {
1695 expr += 3;
1696 while (isspace(*expr)) {
1697 expr++;
1698 }
1699 if (*expr != '\0') {
1700 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1701 length_restr ? "length" : "range", expr);
1702 goto cleanup;
1703 }
1704 if (range_expected) {
1705 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001706 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 +01001707 range_expected = 0;
1708 } else {
1709 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1710 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001711 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001712 part->min_64 = part->max_64;
1713 }
1714 } else {
1715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1716 length_restr ? "length" : "range", expr);
1717 goto cleanup;
1718 }
1719 }
1720
1721 /* check with the previous range/length restriction */
1722 if (base_range) {
1723 switch (basetype) {
1724 case LY_TYPE_BINARY:
1725 case LY_TYPE_UINT8:
1726 case LY_TYPE_UINT16:
1727 case LY_TYPE_UINT32:
1728 case LY_TYPE_UINT64:
1729 case LY_TYPE_STRING:
1730 uns = 1;
1731 break;
1732 case LY_TYPE_DEC64:
1733 case LY_TYPE_INT8:
1734 case LY_TYPE_INT16:
1735 case LY_TYPE_INT32:
1736 case LY_TYPE_INT64:
1737 uns = 0;
1738 break;
1739 default:
1740 LOGINT(ctx->ctx);
1741 ret = LY_EINT;
1742 goto cleanup;
1743 }
1744 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1745 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1746 goto baseerror;
1747 }
1748 /* current lower bound is not lower than the base */
1749 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1750 /* base has single value */
1751 if (base_range->parts[v].min_64 == parts[u].min_64) {
1752 /* both lower bounds are the same */
1753 if (parts[u].min_64 != parts[u].max_64) {
1754 /* current continues with a range */
1755 goto baseerror;
1756 } else {
1757 /* equal single values, move both forward */
1758 ++v;
1759 continue;
1760 }
1761 } else {
1762 /* base is single value lower than current range, so the
1763 * value from base range is removed in the current,
1764 * move only base and repeat checking */
1765 ++v;
1766 --u;
1767 continue;
1768 }
1769 } else {
1770 /* base is the range */
1771 if (parts[u].min_64 == parts[u].max_64) {
1772 /* current is a single value */
1773 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1774 /* current is behind the base range, so base range is omitted,
1775 * move the base and keep the current for further check */
1776 ++v;
1777 --u;
1778 } /* else it is within the base range, so move the current, but keep the base */
1779 continue;
1780 } else {
1781 /* both are ranges - check the higher bound, the lower was already checked */
1782 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1783 /* higher bound is higher than the current higher bound */
1784 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1785 /* but the current lower bound is also higher, so the base range is omitted,
1786 * continue with the same current, but move the base */
1787 --u;
1788 ++v;
1789 continue;
1790 }
1791 /* current range starts within the base range but end behind it */
1792 goto baseerror;
1793 } else {
1794 /* current range is smaller than the base,
1795 * move current, but stay with the base */
1796 continue;
1797 }
1798 }
1799 }
1800 }
1801 if (u != parts_done) {
1802baseerror:
1803 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1804 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1805 length_restr ? "length" : "range", range_p->arg);
1806 goto cleanup;
1807 }
1808 }
1809
1810 if (!(*range)) {
1811 *range = calloc(1, sizeof **range);
1812 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1813 }
1814
Radek Krejcic8b31002019-01-08 10:24:45 +01001815 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001816 if (range_p->eapptag) {
1817 lydict_remove(ctx->ctx, (*range)->eapptag);
1818 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1819 }
1820 if (range_p->emsg) {
1821 lydict_remove(ctx->ctx, (*range)->emsg);
1822 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1823 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001824 if (range_p->dsc) {
1825 lydict_remove(ctx->ctx, (*range)->dsc);
1826 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1827 }
1828 if (range_p->ref) {
1829 lydict_remove(ctx->ctx, (*range)->ref);
1830 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1831 }
Radek Krejci19a96102018-11-15 13:38:09 +01001832 /* extensions are taken only from the last range by the caller */
1833
1834 (*range)->parts = parts;
1835 parts = NULL;
1836 ret = LY_SUCCESS;
1837cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001838 LY_ARRAY_FREE(parts);
1839
1840 return ret;
1841}
1842
1843/**
1844 * @brief Checks pattern syntax.
1845 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001846 * @param[in] ctx Context.
1847 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001848 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001849 * @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 +01001850 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001851 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001852LY_ERR
1853lys_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 +01001854{
Michal Vasko40a00082020-05-27 15:20:01 +02001855 int idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001856 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001857 int err_code;
1858 const char *orig_ptr;
1859 PCRE2_SIZE err_offset;
1860 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001861#define URANGE_LEN 19
1862 char *ublock2urange[][2] = {
1863 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1864 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1865 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1866 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1867 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1868 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1869 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1870 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1871 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1872 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1873 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1874 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1875 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1876 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1877 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1878 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1879 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1880 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1881 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1882 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1883 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1884 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1885 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1886 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1887 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1888 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1889 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1890 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1891 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1892 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1893 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1894 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1895 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1896 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1897 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1898 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1899 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1900 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1901 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1902 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1903 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1904 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1905 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1906 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1907 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1908 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1909 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1910 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1911 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1912 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1913 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1914 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1915 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1916 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1917 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1918 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1919 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1920 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1921 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1922 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1923 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1924 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1925 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1926 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1927 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1928 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1929 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1930 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1931 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1932 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1933 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1934 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1935 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1936 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1937 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1938 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1939 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1940 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1941 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1942 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1943 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1944 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1945 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1946 {NULL, NULL}
1947 };
1948
1949 /* adjust the expression to a Perl equivalent
1950 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1951
Michal Vasko40a00082020-05-27 15:20:01 +02001952 /* allocate space for the transformed pattern */
1953 size = strlen(pattern) + 1;
1954 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001955 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001956 perl_regex[0] = '\0';
1957
Michal Vasko40a00082020-05-27 15:20:01 +02001958 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1959 brack = 0;
1960 idx = 0;
1961 orig_ptr = pattern;
1962 while (orig_ptr[0]) {
1963 switch (orig_ptr[0]) {
1964 case '$':
1965 case '^':
1966 if (!brack) {
1967 /* make space for the extra character */
1968 ++size;
1969 perl_regex = ly_realloc(perl_regex, size);
1970 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001971
Michal Vasko40a00082020-05-27 15:20:01 +02001972 /* print escape slash */
1973 perl_regex[idx] = '\\';
1974 ++idx;
1975 }
1976 break;
1977 case '[':
1978 /* must not be escaped */
1979 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
1980 ++brack;
1981 }
1982 break;
1983 case ']':
1984 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
1985 /* pattern was checked and compiled already */
1986 assert(brack);
1987 --brack;
1988 }
1989 break;
1990 default:
1991 break;
Radek Krejci19a96102018-11-15 13:38:09 +01001992 }
Michal Vasko40a00082020-05-27 15:20:01 +02001993
1994 /* copy char */
1995 perl_regex[idx] = orig_ptr[0];
1996
1997 ++idx;
1998 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001999 }
Michal Vasko40a00082020-05-27 15:20:01 +02002000 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002001
2002 /* substitute Unicode Character Blocks with exact Character Ranges */
2003 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2004 start = ptr - perl_regex;
2005
2006 ptr = strchr(ptr, '}');
2007 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002008 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002009 pattern, perl_regex + start + 2, "unterminated character property");
2010 free(perl_regex);
2011 return LY_EVALID;
2012 }
2013 end = (ptr - perl_regex) + 1;
2014
2015 /* need more space */
2016 if (end - start < URANGE_LEN) {
2017 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002018 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002019 }
2020
2021 /* find our range */
2022 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2023 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2024 break;
2025 }
2026 }
2027 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002029 pattern, perl_regex + start + 5, "unknown block name");
2030 free(perl_regex);
2031 return LY_EVALID;
2032 }
2033
2034 /* 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 +02002035 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002036 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002037 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002038 }
2039 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002040 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002041 }
2042 }
Michal Vasko40a00082020-05-27 15:20:01 +02002043 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002044 /* skip brackets */
2045 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2046 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2047 } else {
2048 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2049 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2050 }
2051 }
2052
2053 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002054 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2055 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002056 &err_code, &err_offset, NULL);
2057 if (!code_local) {
2058 PCRE2_UCHAR err_msg[256] = {0};
2059 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002061 free(perl_regex);
2062 return LY_EVALID;
2063 }
2064 free(perl_regex);
2065
Radek Krejci54579462019-04-30 12:47:06 +02002066 if (code) {
2067 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002068 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002069 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002070 }
2071
2072 return LY_SUCCESS;
2073
2074#undef URANGE_LEN
2075}
2076
Radek Krejcia3045382018-11-22 14:30:31 +01002077/**
2078 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2079 * @param[in] ctx Compile context.
2080 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002081 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2082 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2083 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2084 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2085 */
Radek Krejci19a96102018-11-15 13:38:09 +01002086static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002087lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002088 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2089{
2090 struct lysc_pattern **pattern;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002091 LY_ARRAY_SIZE_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002092 LY_ERR ret = LY_SUCCESS;
2093
2094 /* first, copy the patterns from the base type */
2095 if (base_patterns) {
2096 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2097 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2098 }
2099
2100 LY_ARRAY_FOR(patterns_p, u) {
2101 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2102 *pattern = calloc(1, sizeof **pattern);
2103 ++(*pattern)->refcount;
2104
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002106 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002107
2108 if (patterns_p[u].arg[0] == 0x15) {
2109 (*pattern)->inverted = 1;
2110 }
Radek Krejci54579462019-04-30 12:47:06 +02002111 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002112 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2113 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002114 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2115 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002116 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002117 }
2118done:
2119 return ret;
2120}
2121
Radek Krejcia3045382018-11-22 14:30:31 +01002122/**
2123 * @brief map of the possible restrictions combination for the specific built-in type.
2124 */
Radek Krejci19a96102018-11-15 13:38:09 +01002125static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2126 0 /* LY_TYPE_UNKNOWN */,
2127 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002128 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2129 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2130 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2131 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2132 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002133 LYS_SET_BIT /* LY_TYPE_BITS */,
2134 0 /* LY_TYPE_BOOL */,
2135 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2136 0 /* LY_TYPE_EMPTY */,
2137 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2138 LYS_SET_BASE /* LY_TYPE_IDENT */,
2139 LYS_SET_REQINST /* LY_TYPE_INST */,
2140 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002141 LYS_SET_TYPE /* LY_TYPE_UNION */,
2142 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002143 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002144 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002145 LYS_SET_RANGE /* LY_TYPE_INT64 */
2146};
2147
2148/**
2149 * @brief stringification of the YANG built-in data types
2150 */
2151const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2152 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2153 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002154};
2155
Radek Krejcia3045382018-11-22 14:30:31 +01002156/**
2157 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2158 * @param[in] ctx Compile context.
2159 * @param[in] enums_p Array of the parsed enum structures to compile.
2160 * @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 +01002161 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2162 * @param[out] enums Newly created array of the compiled enums information for the current type.
2163 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2164 */
Radek Krejci19a96102018-11-15 13:38:09 +01002165static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002166lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002167 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002168{
2169 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002170 LY_ARRAY_SIZE_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002171 int32_t value = 0;
2172 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002173 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002174
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002175 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002176 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2177 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2178 return LY_EVALID;
2179 }
2180
2181 LY_ARRAY_FOR(enums_p, u) {
2182 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2183 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002184 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2185 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002186 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002187 if (base_enums) {
2188 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2189 LY_ARRAY_FOR(base_enums, v) {
2190 if (!strcmp(e->name, base_enums[v].name)) {
2191 break;
2192 }
2193 }
2194 if (v == LY_ARRAY_SIZE(base_enums)) {
2195 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2196 "Invalid %s - derived type adds new item \"%s\".",
2197 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2198 return LY_EVALID;
2199 }
2200 match = v;
2201 }
2202
2203 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002204 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002205 if (enums_p[u].flags & LYS_SET_VALUE) {
2206 e->value = (int32_t)enums_p[u].value;
2207 if (!u || e->value >= value) {
2208 value = e->value + 1;
2209 }
2210 /* check collision with other values */
2211 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2212 if (e->value == (*enums)[v].value) {
2213 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2214 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2215 e->value, e->name, (*enums)[v].name);
2216 return LY_EVALID;
2217 }
2218 }
2219 } else if (base_enums) {
2220 /* inherit the assigned value */
2221 e->value = base_enums[match].value;
2222 if (!u || e->value >= value) {
2223 value = e->value + 1;
2224 }
2225 } else {
2226 /* assign value automatically */
2227 if (u && value == INT32_MIN) {
2228 /* counter overflow */
2229 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2230 "Invalid enumeration - it is not possible to auto-assign enum value for "
2231 "\"%s\" since the highest value is already 2147483647.", e->name);
2232 return LY_EVALID;
2233 }
2234 e->value = value++;
2235 }
2236 } else { /* LY_TYPE_BITS */
2237 if (enums_p[u].flags & LYS_SET_VALUE) {
2238 e->value = (int32_t)enums_p[u].value;
2239 if (!u || (uint32_t)e->value >= position) {
2240 position = (uint32_t)e->value + 1;
2241 }
2242 /* check collision with other values */
2243 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2244 if (e->value == (*enums)[v].value) {
2245 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2246 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2247 (uint32_t)e->value, e->name, (*enums)[v].name);
2248 return LY_EVALID;
2249 }
2250 }
2251 } else if (base_enums) {
2252 /* inherit the assigned value */
2253 e->value = base_enums[match].value;
2254 if (!u || (uint32_t)e->value >= position) {
2255 position = (uint32_t)e->value + 1;
2256 }
2257 } else {
2258 /* assign value automatically */
2259 if (u && position == 0) {
2260 /* counter overflow */
2261 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2262 "Invalid bits - it is not possible to auto-assign bit position for "
2263 "\"%s\" since the highest value is already 4294967295.", e->name);
2264 return LY_EVALID;
2265 }
2266 e->value = position++;
2267 }
2268 }
2269
2270 if (base_enums) {
2271 /* the assigned values must not change from the derived type */
2272 if (e->value != base_enums[match].value) {
2273 if (basetype == LY_TYPE_ENUM) {
2274 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2275 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2276 e->name, base_enums[match].value, e->value);
2277 } else {
2278 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2279 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2280 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2281 }
2282 return LY_EVALID;
2283 }
2284 }
2285
Radek Krejciec4da802019-05-02 13:02:41 +02002286 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002287 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 +01002288
2289 if (basetype == LY_TYPE_BITS) {
2290 /* keep bits ordered by position */
2291 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2292 if (v != u) {
2293 memcpy(&storage, e, sizeof *e);
2294 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2295 memcpy(&(*enums)[v], &storage, sizeof storage);
2296 }
2297 }
2298 }
2299
2300done:
2301 return ret;
2302}
2303
Radek Krejcia3045382018-11-22 14:30:31 +01002304/**
2305 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2306 *
2307 * path-arg = absolute-path / relative-path
2308 * absolute-path = 1*("/" (node-identifier *path-predicate))
2309 * relative-path = 1*(".." "/") descendant-path
2310 *
2311 * @param[in,out] path Path to parse.
2312 * @param[out] prefix Prefix of the token, NULL if there is not any.
2313 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2314 * @param[out] name Name of the token.
2315 * @param[out] nam_len Length of the name.
2316 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2317 * must not be changed between consecutive calls. -1 if the
2318 * path is absolute.
2319 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2320 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2321 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002322LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002323lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2324 int *parent_times, int *has_predicate)
2325{
2326 int par_times = 0;
2327
2328 assert(path && *path);
2329 assert(parent_times);
2330 assert(prefix);
2331 assert(prefix_len);
2332 assert(name);
2333 assert(name_len);
2334 assert(has_predicate);
2335
2336 *prefix = NULL;
2337 *prefix_len = 0;
2338 *name = NULL;
2339 *name_len = 0;
2340 *has_predicate = 0;
2341
2342 if (!*parent_times) {
2343 if (!strncmp(*path, "..", 2)) {
2344 *path += 2;
2345 ++par_times;
2346 while (!strncmp(*path, "/..", 3)) {
2347 *path += 3;
2348 ++par_times;
2349 }
2350 }
2351 if (par_times) {
2352 *parent_times = par_times;
2353 } else {
2354 *parent_times = -1;
2355 }
2356 }
2357
2358 if (**path != '/') {
2359 return LY_EINVAL;
2360 }
2361 /* skip '/' */
2362 ++(*path);
2363
2364 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002365 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002366
2367 if ((**path == '/' && (*path)[1]) || !**path) {
2368 /* path continues by another token or this is the last token */
2369 return LY_SUCCESS;
2370 } else if ((*path)[0] != '[') {
2371 /* unexpected character */
2372 return LY_EINVAL;
2373 } else {
2374 /* predicate starting with [ */
2375 *has_predicate = 1;
2376 return LY_SUCCESS;
2377 }
2378}
2379
2380/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002381 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2382 *
2383 * The set of features used for target must be a subset of features used for the leafref.
2384 * This is not a perfect, we should compare the truth tables but it could require too much resources
2385 * and RFC 7950 does not require it explicitely, so we simplify that.
2386 *
2387 * @param[in] refnode The leafref node.
2388 * @param[in] target Tha target node of the leafref.
2389 * @return LY_SUCCESS or LY_EVALID;
2390 */
2391static LY_ERR
2392lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2393{
2394 LY_ERR ret = LY_EVALID;
2395 const struct lysc_node *iter;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002396 LY_ARRAY_SIZE_TYPE u, v, count;
Radek Krejci58d171e2018-11-23 13:50:55 +01002397 struct ly_set features = {0};
2398
2399 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002400 if (iter->iffeatures) {
2401 LY_ARRAY_FOR(iter->iffeatures, u) {
2402 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2403 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002404 }
2405 }
2406 }
2407 }
2408
2409 /* we should have, in features set, a superset of features applicable to the target node.
2410 * So when adding features applicable to the target into the features set, we should not be
2411 * able to actually add any new feature, otherwise it is not a subset of features applicable
2412 * to the leafref itself. */
2413 count = features.count;
2414 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002415 if (iter->iffeatures) {
2416 LY_ARRAY_FOR(iter->iffeatures, u) {
2417 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2418 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002419 /* new feature was added (or LY_EMEM) */
2420 goto cleanup;
2421 }
2422 }
2423 }
2424 }
2425 }
2426 ret = LY_SUCCESS;
2427
2428cleanup:
2429 ly_set_erase(&features, NULL);
2430 return ret;
2431}
2432
Michal Vasko004d3152020-06-11 19:59:22 +02002433static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2434 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2435 struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002436
Radek Krejcia3045382018-11-22 14:30:31 +01002437/**
2438 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2439 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002440 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2441 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2442 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2443 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002444 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002445 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002446 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002447 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2448 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2449 * @param[out] type Newly created type structure with the filled information about the type.
2450 * @return LY_ERR value.
2451 */
Radek Krejci19a96102018-11-15 13:38:09 +01002452static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002453lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2454 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2455 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2456 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002457{
2458 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002459 struct lysc_type_bin *bin;
2460 struct lysc_type_num *num;
2461 struct lysc_type_str *str;
2462 struct lysc_type_bits *bits;
2463 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002464 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002465 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002466 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002467 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002468
2469 switch (basetype) {
2470 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002471 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002472
2473 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002474 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002475 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2476 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002477 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002478 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 +01002479 }
2480 }
2481
2482 if (tpdfname) {
2483 type_p->compiled = *type;
2484 *type = calloc(1, sizeof(struct lysc_type_bin));
2485 }
2486 break;
2487 case LY_TYPE_BITS:
2488 /* RFC 7950 9.7 - bits */
2489 bits = (struct lysc_type_bits*)(*type);
2490 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002491 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002492 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2493 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002494 }
2495
Radek Krejci555cb5b2018-11-16 14:54:33 +01002496 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002497 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002498 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002499 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002500 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002501 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002502 free(*type);
2503 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002504 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002505 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002506 }
2507
2508 if (tpdfname) {
2509 type_p->compiled = *type;
2510 *type = calloc(1, sizeof(struct lysc_type_bits));
2511 }
2512 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002513 case LY_TYPE_DEC64:
2514 dec = (struct lysc_type_dec*)(*type);
2515
2516 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002517 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002518 if (!type_p->fraction_digits) {
2519 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002520 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002521 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002522 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002523 free(*type);
2524 *type = NULL;
2525 }
2526 return LY_EVALID;
2527 }
2528 } else if (type_p->fraction_digits) {
2529 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002530 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2532 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002533 tpdfname);
2534 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2536 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002537 free(*type);
2538 *type = NULL;
2539 }
2540 return LY_EVALID;
2541 }
2542 dec->fraction_digits = type_p->fraction_digits;
2543
2544 /* RFC 7950 9.2.4 - range */
2545 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002546 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2547 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002548 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002549 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 +01002550 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002551 }
2552
2553 if (tpdfname) {
2554 type_p->compiled = *type;
2555 *type = calloc(1, sizeof(struct lysc_type_dec));
2556 }
2557 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002558 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002559 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002560
2561 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002562 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002563 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2564 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002565 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002566 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 +01002567 }
2568 } else if (base && ((struct lysc_type_str*)base)->length) {
2569 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2570 }
2571
2572 /* RFC 7950 9.4.5 - pattern */
2573 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002574 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002575 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002576 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2577 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2578 }
2579
2580 if (tpdfname) {
2581 type_p->compiled = *type;
2582 *type = calloc(1, sizeof(struct lysc_type_str));
2583 }
2584 break;
2585 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002586 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002587
2588 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002589 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002590 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002591 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002592 }
2593
Radek Krejci555cb5b2018-11-16 14:54:33 +01002594 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002595 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002596 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002597 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002598 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002599 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002600 free(*type);
2601 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002602 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002603 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002604 }
2605
2606 if (tpdfname) {
2607 type_p->compiled = *type;
2608 *type = calloc(1, sizeof(struct lysc_type_enum));
2609 }
2610 break;
2611 case LY_TYPE_INT8:
2612 case LY_TYPE_UINT8:
2613 case LY_TYPE_INT16:
2614 case LY_TYPE_UINT16:
2615 case LY_TYPE_INT32:
2616 case LY_TYPE_UINT32:
2617 case LY_TYPE_INT64:
2618 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002619 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002620
2621 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002622 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002623 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2624 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002625 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002626 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 +01002627 }
2628 }
2629
2630 if (tpdfname) {
2631 type_p->compiled = *type;
2632 *type = calloc(1, sizeof(struct lysc_type_num));
2633 }
2634 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002635 case LY_TYPE_IDENT:
2636 idref = (struct lysc_type_identityref*)(*type);
2637
2638 /* RFC 7950 9.10.2 - base */
2639 if (type_p->bases) {
2640 if (base) {
2641 /* only the directly derived identityrefs can contain base specification */
2642 if (tpdfname) {
2643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002644 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002645 tpdfname);
2646 } else {
2647 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002648 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002649 free(*type);
2650 *type = NULL;
2651 }
2652 return LY_EVALID;
2653 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002654 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002655 }
2656
2657 if (!base && !type_p->flags) {
2658 /* type derived from identityref built-in type must contain at least one base */
2659 if (tpdfname) {
2660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2661 } else {
2662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2663 free(*type);
2664 *type = NULL;
2665 }
2666 return LY_EVALID;
2667 }
2668
2669 if (tpdfname) {
2670 type_p->compiled = *type;
2671 *type = calloc(1, sizeof(struct lysc_type_identityref));
2672 }
2673 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002674 case LY_TYPE_LEAFREF:
Michal Vasko004d3152020-06-11 19:59:22 +02002675 lref = (struct lysc_type_leafref*)*type;
2676
Radek Krejcia3045382018-11-22 14:30:31 +01002677 /* RFC 7950 9.9.3 - require-instance */
2678 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002679 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002680 if (tpdfname) {
2681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2682 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2683 } else {
2684 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2685 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2686 free(*type);
2687 *type = NULL;
2688 }
2689 return LY_EVALID;
2690 }
Michal Vasko004d3152020-06-11 19:59:22 +02002691 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002692 } else if (base) {
2693 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002694 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002695 } else {
2696 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002697 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002698 }
2699 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002700 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2701 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002702 } else if (base) {
Michal Vasko004d3152020-06-11 19:59:22 +02002703 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path);
2704 lref->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002705 } else if (tpdfname) {
2706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2707 return LY_EVALID;
2708 } else {
2709 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2710 free(*type);
2711 *type = NULL;
2712 return LY_EVALID;
2713 }
2714 if (tpdfname) {
2715 type_p->compiled = *type;
2716 *type = calloc(1, sizeof(struct lysc_type_leafref));
2717 }
2718 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002719 case LY_TYPE_INST:
2720 /* RFC 7950 9.9.3 - require-instance */
2721 if (type_p->flags & LYS_SET_REQINST) {
2722 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2723 } else {
2724 /* default is true */
2725 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2726 }
2727
2728 if (tpdfname) {
2729 type_p->compiled = *type;
2730 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2731 }
2732 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002733 case LY_TYPE_UNION:
2734 un = (struct lysc_type_union*)(*type);
2735
2736 /* RFC 7950 7.4 - type */
2737 if (type_p->types) {
2738 if (base) {
2739 /* only the directly derived union can contain types specification */
2740 if (tpdfname) {
2741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2742 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2743 tpdfname);
2744 } else {
2745 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2746 "Invalid type substatement for the type not directly derived from union built-in type.");
2747 free(*type);
2748 *type = NULL;
2749 }
2750 return LY_EVALID;
2751 }
2752 /* compile the type */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002753 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002754 for (LY_ARRAY_SIZE_TYPE u = 0, additional = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002755 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
2756 &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002757 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2758 /* add space for additional types from the union subtype */
2759 un_aux = (struct lysc_type_union *)un->types[u + additional];
Radek Krejcic4fa0292020-05-14 10:54:49 +02002760 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t*)(type_p->types) - 1)) + additional + LY_ARRAY_SIZE(un_aux->types) - 1,
2761 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002762
2763 /* copy subtypes of the subtype union */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002764 for (LY_ARRAY_SIZE_TYPE v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002765 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2766 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2767 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2768 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 +02002769 lref = (struct lysc_type_leafref *)un->types[u + additional];
2770
2771 lref->basetype = LY_TYPE_LEAFREF;
2772 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path);
2773 lref->refcount = 1;
2774 lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2775 lref->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002776 /* TODO extensions */
2777
2778 } else {
2779 un->types[u + additional] = un_aux->types[v];
2780 ++un_aux->types[v]->refcount;
2781 }
2782 ++additional;
2783 LY_ARRAY_INCREMENT(un->types);
2784 }
2785 /* compensate u increment in main loop */
2786 --additional;
2787
2788 /* free the replaced union subtype */
2789 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2790 } else {
2791 LY_ARRAY_INCREMENT(un->types);
2792 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002793 }
2794 }
2795
2796 if (!base && !type_p->flags) {
2797 /* type derived from union built-in type must contain at least one type */
2798 if (tpdfname) {
2799 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2800 } else {
2801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2802 free(*type);
2803 *type = NULL;
2804 }
2805 return LY_EVALID;
2806 }
2807
2808 if (tpdfname) {
2809 type_p->compiled = *type;
2810 *type = calloc(1, sizeof(struct lysc_type_union));
2811 }
2812 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002813 case LY_TYPE_BOOL:
2814 case LY_TYPE_EMPTY:
2815 case LY_TYPE_UNKNOWN: /* just to complete switch */
2816 break;
2817 }
2818 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2819done:
2820 return ret;
2821}
2822
Radek Krejcia3045382018-11-22 14:30:31 +01002823/**
2824 * @brief Compile information about the leaf/leaf-list's type.
2825 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002826 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2827 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2828 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2829 * @param[in] context_name Name of the context node or referencing typedef for logging.
2830 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002831 * @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 +01002832 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002833 * @return LY_ERR value.
2834 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002835static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002836lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2837 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2838 struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002839{
2840 LY_ERR ret = LY_SUCCESS;
2841 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002842 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002843 struct type_context {
2844 const struct lysp_tpdf *tpdf;
2845 struct lysp_node *node;
2846 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002847 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002848 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002849 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002850 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002851 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002852 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002853
2854 (*type) = NULL;
2855
2856 tctx = calloc(1, sizeof *tctx);
2857 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002858 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002859 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2860 ret == LY_SUCCESS;
2861 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2862 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2863 if (basetype) {
2864 break;
2865 }
2866
2867 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002868 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2869 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002870 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2871
Radek Krejcicdfecd92018-11-26 11:27:32 +01002872 if (units && !*units) {
2873 /* inherit units */
2874 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2875 }
Radek Krejci01342af2019-01-03 15:18:08 +01002876 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002877 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002878 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02002879 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002880 }
Radek Krejci01342af2019-01-03 15:18:08 +01002881 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002882 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2883 break;
2884 }
2885
Radek Krejci19a96102018-11-15 13:38:09 +01002886 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002887 /* it is not necessary to continue, the rest of the chain was already compiled,
2888 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002889 basetype = tctx->tpdf->type.compiled->basetype;
2890 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002891 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002892 dummyloops = 1;
2893 goto preparenext;
2894 } else {
2895 tctx = NULL;
2896 break;
2897 }
Radek Krejci19a96102018-11-15 13:38:09 +01002898 }
2899
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002900 /* circular typedef reference detection */
2901 for (u = 0; u < tpdf_chain.count; u++) {
2902 /* local part */
2903 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2904 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2905 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2906 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2907 free(tctx);
2908 ret = LY_EVALID;
2909 goto cleanup;
2910 }
2911 }
2912 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2913 /* global part for unions corner case */
2914 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2915 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2916 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2917 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2918 free(tctx);
2919 ret = LY_EVALID;
2920 goto cleanup;
2921 }
2922 }
2923
Radek Krejci19a96102018-11-15 13:38:09 +01002924 /* store information for the following processing */
2925 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2926
Radek Krejcicdfecd92018-11-26 11:27:32 +01002927preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002928 /* prepare next loop */
2929 tctx_prev = tctx;
2930 tctx = calloc(1, sizeof *tctx);
2931 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2932 }
2933 free(tctx);
2934
2935 /* allocate type according to the basetype */
2936 switch (basetype) {
2937 case LY_TYPE_BINARY:
2938 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002939 break;
2940 case LY_TYPE_BITS:
2941 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002942 break;
2943 case LY_TYPE_BOOL:
2944 case LY_TYPE_EMPTY:
2945 *type = calloc(1, sizeof(struct lysc_type));
2946 break;
2947 case LY_TYPE_DEC64:
2948 *type = calloc(1, sizeof(struct lysc_type_dec));
2949 break;
2950 case LY_TYPE_ENUM:
2951 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002952 break;
2953 case LY_TYPE_IDENT:
2954 *type = calloc(1, sizeof(struct lysc_type_identityref));
2955 break;
2956 case LY_TYPE_INST:
2957 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2958 break;
2959 case LY_TYPE_LEAFREF:
2960 *type = calloc(1, sizeof(struct lysc_type_leafref));
2961 break;
2962 case LY_TYPE_STRING:
2963 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002964 break;
2965 case LY_TYPE_UNION:
2966 *type = calloc(1, sizeof(struct lysc_type_union));
2967 break;
2968 case LY_TYPE_INT8:
2969 case LY_TYPE_UINT8:
2970 case LY_TYPE_INT16:
2971 case LY_TYPE_UINT16:
2972 case LY_TYPE_INT32:
2973 case LY_TYPE_UINT32:
2974 case LY_TYPE_INT64:
2975 case LY_TYPE_UINT64:
2976 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002977 break;
2978 case LY_TYPE_UNKNOWN:
2979 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2980 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2981 ret = LY_EVALID;
2982 goto cleanup;
2983 }
2984 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002985 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2987 ly_data_type2str[basetype]);
2988 free(*type);
2989 (*type) = NULL;
2990 ret = LY_EVALID;
2991 goto cleanup;
2992 }
2993
2994 /* get restrictions from the referred typedefs */
2995 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2996 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002997
2998 /* remember the typedef context for circular check */
2999 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3000
Radek Krejci43699232018-11-23 14:59:46 +01003001 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003002 base = tctx->tpdf->type.compiled;
3003 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003004 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003005 /* no change, just use the type information from the base */
3006 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3007 ++base->refcount;
3008 continue;
3009 }
3010
3011 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003012 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3013 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3014 tctx->tpdf->name, ly_data_type2str[basetype]);
3015 ret = LY_EVALID;
3016 goto cleanup;
3017 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3018 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3019 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3020 tctx->tpdf->name, tctx->tpdf->dflt);
3021 ret = LY_EVALID;
3022 goto cleanup;
3023 }
3024
Radek Krejci19a96102018-11-15 13:38:09 +01003025 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003026 /* TODO user type plugins */
3027 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003028 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003029 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 +01003030 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003031 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003032 LY_CHECK_GOTO(ret, cleanup);
3033 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003034 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003035 /* remove the processed typedef contexts from the stack for circular check */
3036 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003037
Radek Krejcic5c27e52018-11-15 14:38:11 +01003038 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003039 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003040 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003041 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003042 /* TODO user type plugins */
3043 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003044 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003045 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 +01003046 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003047 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003048 /* no specific restriction in leaf's type definition, copy from the base */
3049 free(*type);
3050 (*type) = base;
3051 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003052 }
Radek Krejcia1911222019-07-22 17:24:50 +02003053 if (dflt && !(*type)->dflt) {
3054 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003055 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003056 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3057 (*type)->dflt->realtype = (*type);
3058 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3059 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003060 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003061 if (err) {
3062 ly_err_print(err);
3063 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3064 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3065 ly_err_free(err);
3066 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003067 if (ret == LY_EINCOMPLETE) {
3068 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003069 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003070
3071 /* but in general result is so far ok */
3072 ret = LY_SUCCESS;
3073 }
Radek Krejcia1911222019-07-22 17:24:50 +02003074 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003075 }
Radek Krejci19a96102018-11-15 13:38:09 +01003076
Radek Krejci0935f412019-08-20 16:15:18 +02003077 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003078
3079cleanup:
3080 ly_set_erase(&tpdf_chain, free);
3081 return ret;
3082}
3083
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003084/**
3085 * @brief Compile status information of the given node.
3086 *
3087 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3088 * has the status correctly set during the compilation.
3089 *
3090 * @param[in] ctx Compile context
3091 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3092 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3093 * the compatibility with the parent's status value.
3094 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3095 * @return LY_ERR value.
3096 */
3097static LY_ERR
3098lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3099{
3100 /* status - it is not inherited by specification, but it does not make sense to have
3101 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3102 if (!((*node_flags) & LYS_STATUS_MASK)) {
3103 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3104 if ((parent_flags & 0x3) != 0x3) {
3105 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3106 * combination of bits (0x3) which marks the uses_status value */
3107 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3108 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3109 }
3110 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3111 } else {
3112 (*node_flags) |= LYS_STATUS_CURR;
3113 }
3114 } else if (parent_flags & LYS_STATUS_MASK) {
3115 /* check status compatibility with the parent */
3116 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3117 if ((*node_flags) & LYS_STATUS_CURR) {
3118 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3119 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3120 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3121 } else { /* LYS_STATUS_DEPRC */
3122 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3123 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3124 }
3125 return LY_EVALID;
3126 }
3127 }
3128 return LY_SUCCESS;
3129}
3130
Radek Krejci8cce8532019-03-05 11:27:45 +01003131/**
3132 * @brief Check uniqness of the node/action/notification name.
3133 *
3134 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3135 * structures, but they share the namespace so we need to check their name collisions.
3136 *
3137 * @param[in] ctx Compile context.
3138 * @param[in] children List (linked list) of data nodes to go through.
3139 * @param[in] actions List (sized array) of actions or RPCs to go through.
3140 * @param[in] notifs List (sized array) of Notifications to go through.
3141 * @param[in] name Name of the item to find in the given lists.
3142 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3143 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3144 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3145 */
3146static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02003147lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions,
3148 const struct lysc_notif *notifs, const char *name, void *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003149{
3150 const struct lysc_node *iter;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003151 LY_ARRAY_SIZE_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003152
3153 LY_LIST_FOR(children, iter) {
3154 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3155 goto error;
3156 }
3157 }
3158 LY_ARRAY_FOR(actions, u) {
3159 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3160 goto error;
3161 }
3162 }
3163 LY_ARRAY_FOR(notifs, u) {
3164 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3165 goto error;
3166 }
3167 }
3168 return LY_SUCCESS;
3169error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003170 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003171 return LY_EEXIST;
3172}
3173
Radek Krejciec4da802019-05-02 13:02:41 +02003174static 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 +01003175
Radek Krejcia3045382018-11-22 14:30:31 +01003176/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003177 * @brief Compile parsed RPC/action schema node information.
3178 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003179 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003180 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003181 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3182 * @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).
3183 * Zero means no uses, non-zero value with no status bit set mean the default status.
3184 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3185 */
3186static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003187lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003188 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3189{
3190 LY_ERR ret = LY_SUCCESS;
3191 struct lysp_node *child_p;
3192 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003193 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003194
Radek Krejci327de162019-06-14 12:52:07 +02003195 lysc_update_path(ctx, parent, action_p->name);
3196
Radek Krejci8cce8532019-03-05 11:27:45 +01003197 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3198 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3199 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3200 action_p->name, action)) {
3201 return LY_EVALID;
3202 }
3203
Radek Krejciec4da802019-05-02 13:02:41 +02003204 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003205 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003206 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003207 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003208 return LY_EVALID;
3209 }
3210
Michal Vasko1bf09392020-03-27 12:38:10 +01003211 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003212 action->module = ctx->mod;
3213 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003214 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003215 action->sp = action_p;
3216 }
3217 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3218
3219 /* status - it is not inherited by specification, but it does not make sense to have
3220 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003221 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003222
3223 DUP_STRING(ctx->ctx, action_p->name, action->name);
3224 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3225 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003226 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003227 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003228
3229 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003230 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003231 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003232 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 +02003233 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003234 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003235 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003236 }
Radek Krejci327de162019-06-14 12:52:07 +02003237 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003238 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003239
3240 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003241 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003242 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003243 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 +02003244 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003245 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003246 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003247 }
Radek Krejci327de162019-06-14 12:52:07 +02003248 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003249 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003250
3251 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3252 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003253 ly_set_add(&ctx->xpath, action, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003254 }
3255
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003256cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003257 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003258 return ret;
3259}
3260
3261/**
Radek Krejci43981a32019-04-12 09:44:11 +02003262 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003263 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003264 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003265 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3266 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3267 * @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 +02003268 * Zero means no uses, non-zero value with no status bit set mean the default status.
3269 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3270 */
3271static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003272lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003273 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3274{
3275 LY_ERR ret = LY_SUCCESS;
3276 struct lysp_node *child_p;
3277 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003278 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003279
Radek Krejci327de162019-06-14 12:52:07 +02003280 lysc_update_path(ctx, parent, notif_p->name);
3281
Radek Krejcifc11bd72019-04-11 16:00:05 +02003282 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3283 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3284 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3285 notif_p->name, notif)) {
3286 return LY_EVALID;
3287 }
3288
Radek Krejciec4da802019-05-02 13:02:41 +02003289 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003290 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3291 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003292 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003293 return LY_EVALID;
3294 }
3295
3296 notif->nodetype = LYS_NOTIF;
3297 notif->module = ctx->mod;
3298 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003299 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003300 notif->sp = notif_p;
3301 }
3302 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3303
3304 /* status - it is not inherited by specification, but it does not make sense to have
3305 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3306 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3307
3308 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3309 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3310 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003311 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003312 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003313 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3314 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003315 ly_set_add(&ctx->xpath, notif, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003316 }
Radek Krejci0935f412019-08-20 16:15:18 +02003317 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003318
Radek Krejciec4da802019-05-02 13:02:41 +02003319 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003320 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003321 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003322 }
3323
Radek Krejci327de162019-06-14 12:52:07 +02003324 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003325cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003326 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003327 return ret;
3328}
3329
3330/**
Radek Krejcia3045382018-11-22 14:30:31 +01003331 * @brief Compile parsed container node information.
3332 * @param[in] ctx Compile context
3333 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003334 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3335 * is enriched with the container-specific information.
3336 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3337 */
Radek Krejci19a96102018-11-15 13:38:09 +01003338static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003339lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003340{
3341 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3342 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3343 struct lysp_node *child_p;
3344 unsigned int u;
3345 LY_ERR ret = LY_SUCCESS;
3346
Radek Krejcife909632019-02-12 15:34:42 +01003347 if (cont_p->presence) {
3348 cont->flags |= LYS_PRESENCE;
3349 }
3350
Radek Krejci19a96102018-11-15 13:38:09 +01003351 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003352 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003353 }
3354
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003355 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003356 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3357 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003358 ly_set_add(&ctx->xpath, cont, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003359 }
Radek Krejciec4da802019-05-02 13:02:41 +02003360 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3361 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003362
3363done:
3364 return ret;
3365}
3366
Radek Krejci33f72892019-02-21 10:36:58 +01003367/*
3368 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3369 * @param[in] ctx Compile context.
3370 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3371 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003372 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3373 * @return LY_ERR value.
3374 */
3375static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003376lys_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 +01003377{
Radek Krejci33f72892019-02-21 10:36:58 +01003378 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3379
Radek Krejciec4da802019-05-02 13:02:41 +02003380 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 +01003381 leaf->units ? NULL : &leaf->units));
3382 if (leaf->nodetype == LYS_LEAFLIST) {
3383 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003384 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3385 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003386 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003387 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3388 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3389 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3390 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003391 LY_ARRAY_INCREMENT(llist->dflts);
3392 }
3393 } else {
3394 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003395 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003396 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3397 leaf->dflt->realtype = leaf->type->dflt->realtype;
3398 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3399 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003400 }
3401 }
3402 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3403 /* 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 +02003404 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003405 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003406 LY_ARRAY_SIZE_TYPE u;
Radek Krejci33f72892019-02-21 10:36:58 +01003407 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3408 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3409 /* 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 +02003410 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003411 }
3412 }
3413 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003414 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3415 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3416 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3417 return LY_EVALID;
3418 }
3419 }
3420
Radek Krejci33f72892019-02-21 10:36:58 +01003421 return LY_SUCCESS;
3422}
3423
Radek Krejcia3045382018-11-22 14:30:31 +01003424/**
3425 * @brief Compile parsed leaf node information.
3426 * @param[in] ctx Compile context
3427 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003428 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3429 * is enriched with the leaf-specific information.
3430 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3431 */
Radek Krejci19a96102018-11-15 13:38:09 +01003432static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003433lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003434{
3435 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3436 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3437 unsigned int u;
3438 LY_ERR ret = LY_SUCCESS;
3439
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003440 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003441 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3442 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003443 ly_set_add(&ctx->xpath, leaf, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003444 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003445 if (leaf_p->units) {
3446 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3447 leaf->flags |= LYS_SET_UNITS;
3448 }
Radek Krejcia1911222019-07-22 17:24:50 +02003449
3450 /* the dflt member is just filled to avoid getting the default value from the type */
3451 leaf->dflt = (void*)leaf_p->dflt;
3452 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003453 if (ret) {
3454 leaf->dflt = NULL;
3455 return ret;
3456 }
Radek Krejcia1911222019-07-22 17:24:50 +02003457
Radek Krejciccd20f12019-02-15 14:12:27 +01003458 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003459 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003460 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003461 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3462 leaf->dflt->realtype = leaf->type;
3463 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3464 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003465 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003466 leaf->dflt->realtype->refcount++;
3467 if (err) {
3468 ly_err_print(err);
3469 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3470 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3471 ly_err_free(err);
3472 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003473 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003474 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003475 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003476
3477 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003478 ret = LY_SUCCESS;
3479 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003480 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003481 leaf->flags |= LYS_SET_DFLT;
3482 }
Radek Krejci43699232018-11-23 14:59:46 +01003483
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003484
Radek Krejci19a96102018-11-15 13:38:09 +01003485done:
3486 return ret;
3487}
3488
Radek Krejcia3045382018-11-22 14:30:31 +01003489/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003490 * @brief Compile parsed leaf-list node information.
3491 * @param[in] ctx Compile context
3492 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003493 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3494 * is enriched with the leaf-list-specific information.
3495 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3496 */
3497static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003498lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003499{
3500 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3501 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003502 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003503 LY_ERR ret = LY_SUCCESS;
3504
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003505 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003506 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3507 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003508 ly_set_add(&ctx->xpath, llist, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003509 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003510 if (llist_p->units) {
3511 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3512 llist->flags |= LYS_SET_UNITS;
3513 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003514
Radek Krejcia1911222019-07-22 17:24:50 +02003515 /* the dflts member is just filled to avoid getting the default value from the type */
3516 llist->dflts = (void*)llist_p->dflts;
3517 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Michal Vasko6a044b22020-01-15 12:25:39 +01003518 if (ret != LY_SUCCESS) {
3519 /* make sure the defaults are freed correctly */
3520 if (llist_p->dflts) {
3521 llist->dflts = NULL;
3522 }
3523 return ret;
3524 }
3525
Radek Krejci0e5d8382018-11-28 16:37:53 +01003526 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003527 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003528 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003529 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3530 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003531 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003532 LY_ARRAY_INCREMENT(llist->dflts_mods);
3533 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003534 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003535 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3536 llist->dflts[u]->realtype = llist->type;
3537 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3538 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003539 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003540 llist->dflts[u]->realtype->refcount++;
3541 if (err) {
3542 ly_err_print(err);
3543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3544 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3545 ly_err_free(err);
3546 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003547 if (ret == LY_EINCOMPLETE) {
3548 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003549 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003550
3551 /* but in general result is so far ok */
3552 ret = LY_SUCCESS;
3553 }
Radek Krejcia1911222019-07-22 17:24:50 +02003554 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003555 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003556 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003557 }
Radek Krejcia1911222019-07-22 17:24:50 +02003558 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3559 /* configuration data values must be unique - so check the default values */
3560 LY_ARRAY_FOR(llist->dflts, u) {
3561 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3562 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3563 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003564 const char *val = llist->type->plugin->print(llist->dflts[v], LYD_XML, lys_get_prefix, llist->dflts_mods[v], &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +02003565 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3566 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3567 if (dynamic) {
3568 free((char*)val);
3569 }
3570 return LY_EVALID;
3571 }
3572 }
3573 }
3574 }
3575
3576 /* 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 +01003577
3578 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003579 if (llist->min) {
3580 llist->flags |= LYS_MAND_TRUE;
3581 }
Radek Krejcib7408632018-11-28 17:12:11 +01003582 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003583
Radek Krejci0e5d8382018-11-28 16:37:53 +01003584done:
3585 return ret;
3586}
3587
3588/**
Radek Krejci7af64242019-02-18 13:07:53 +01003589 * @brief Compile information about list's uniques.
3590 * @param[in] ctx Compile context.
3591 * @param[in] context_module Module where the prefixes are going to be resolved.
3592 * @param[in] uniques Sized array list of unique statements.
3593 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3594 * @return LY_ERR value.
3595 */
3596static LY_ERR
3597lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3598{
3599 LY_ERR ret = LY_SUCCESS;
3600 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003601 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003602 const char *keystr, *delim;
3603 size_t len;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003604 LY_ARRAY_SIZE_TYPE v;
Radek Krejci7af64242019-02-18 13:07:53 +01003605 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003606 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003607
3608 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3609 config = -1;
3610 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3611 keystr = uniques[v];
3612 while (keystr) {
3613 delim = strpbrk(keystr, " \t\n");
3614 if (delim) {
3615 len = delim - keystr;
3616 while (isspace(*delim)) {
3617 ++delim;
3618 }
3619 } else {
3620 len = strlen(keystr);
3621 }
3622
3623 /* unique node must be present */
3624 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003625 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3626 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003627 if (ret != LY_SUCCESS) {
3628 if (ret == LY_EDENIED) {
3629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003630 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003631 len, keystr, lys_nodetype2str((*key)->nodetype));
3632 }
3633 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003634 } else if (flags) {
3635 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3636 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003637 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003638 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003639 }
3640
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003641
Radek Krejci7af64242019-02-18 13:07:53 +01003642 /* all referenced leafs must be of the same config type */
3643 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003645 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003646 return LY_EVALID;
3647 } else if ((*key)->flags & LYS_CONFIG_W) {
3648 config = 1;
3649 } else { /* LYS_CONFIG_R */
3650 config = 0;
3651 }
3652
Michal Vasko14654712020-02-06 08:35:21 +01003653 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3654 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3655 if (parent->nodetype == LYS_LIST) {
3656 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3657 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3658 return LY_EVALID;
3659 }
3660 }
3661
Radek Krejci7af64242019-02-18 13:07:53 +01003662 /* check status */
3663 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3664 (*key)->flags, (*key)->module, (*key)->name));
3665
3666 /* mark leaf as unique */
3667 (*key)->flags |= LYS_UNIQUE;
3668
3669 /* next unique value in line */
3670 keystr = delim;
3671 }
3672 /* next unique definition */
3673 }
3674
3675 return LY_SUCCESS;
3676}
3677
3678/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003679 * @brief Compile parsed list node information.
3680 * @param[in] ctx Compile context
3681 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003682 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3683 * is enriched with the list-specific information.
3684 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3685 */
3686static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003687lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003688{
3689 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3690 struct lysc_node_list *list = (struct lysc_node_list*)node;
3691 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003692 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003693 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003694 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003695 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003696 LY_ERR ret = LY_SUCCESS;
3697
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003698 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003699 if (list->min) {
3700 list->flags |= LYS_MAND_TRUE;
3701 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003702 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3703
3704 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003705 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003706 }
3707
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003708 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003709 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3710 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003711 ly_set_add(&ctx->xpath, list, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003712 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003713
3714 /* keys */
3715 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3716 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3717 return LY_EVALID;
3718 }
3719
3720 /* find all the keys (must be direct children) */
3721 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003722 if (!keystr) {
3723 /* keyless list */
3724 list->flags |= LYS_KEYLESS;
3725 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003726 while (keystr) {
3727 delim = strpbrk(keystr, " \t\n");
3728 if (delim) {
3729 len = delim - keystr;
3730 while (isspace(*delim)) {
3731 ++delim;
3732 }
3733 } else {
3734 len = strlen(keystr);
3735 }
3736
3737 /* key node must be present */
Michal Vaskoe444f752020-02-10 12:20:06 +01003738 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 +02003739 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3741 "The list's key \"%.*s\" not found.", len, keystr);
3742 return LY_EVALID;
3743 }
3744 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003745 if (key->flags & LYS_KEY) {
3746 /* the node was already marked as a key */
3747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3748 "Duplicated key identifier \"%.*s\".", len, keystr);
3749 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003750 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003751
3752 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003753 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003754 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003755 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3756 return LY_EVALID;
3757 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003758 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003759 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003760 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003761 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003762 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003763 return LY_EVALID;
3764 }
3765 } else {
3766 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003767 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003768 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003769 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003770 return LY_EVALID;
3771 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003772 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003774 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003775 return LY_EVALID;
3776 }
3777 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003778
3779 /* check status */
3780 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003781 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003782
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003783 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003784 if (key->dflt) {
3785 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3786 lysc_type_free(ctx->ctx, key->dflt->realtype);
3787 free(key->dflt);
3788 key->dflt = NULL;
3789 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003790 }
3791 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003792 key->flags |= LYS_KEY;
3793
3794 /* move it to the correct position */
3795 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3796 /* fix links in closest previous siblings of the key */
3797 if (key->next) {
3798 key->next->prev = key->prev;
3799 } else {
3800 /* last child */
3801 list->child->prev = key->prev;
3802 }
3803 if (key->prev->next) {
3804 key->prev->next = key->next;
3805 }
3806 /* fix links in the key */
3807 if (prev_key) {
3808 key->prev = (struct lysc_node*)prev_key;
3809 key->next = prev_key->next;
3810 } else {
3811 key->prev = list->child->prev;
3812 key->next = list->child;
3813 }
3814 /* fix links in closes future siblings of the key */
3815 if (prev_key) {
3816 if (prev_key->next) {
3817 prev_key->next->prev = (struct lysc_node*)key;
3818 } else {
3819 list->child->prev = (struct lysc_node*)key;
3820 }
3821 prev_key->next = (struct lysc_node*)key;
3822 } else {
3823 list->child->prev = (struct lysc_node*)key;
3824 }
3825 /* fix links in parent */
3826 if (!key->prev->next) {
3827 list->child = (struct lysc_node*)key;
3828 }
3829 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003830
3831 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003832 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003833 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003834 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003835 }
3836
3837 /* uniques */
3838 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003839 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003840 }
3841
Radek Krejciec4da802019-05-02 13:02:41 +02003842 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3843 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003844
3845done:
3846 return ret;
3847}
3848
Radek Krejcib56c7502019-02-13 14:19:54 +01003849/**
3850 * @brief Do some checks and set the default choice's case.
3851 *
3852 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3853 *
3854 * @param[in] ctx Compile context.
3855 * @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,
3856 * not the reference to the imported module.
3857 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3858 * @return LY_ERR value.
3859 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003860static LY_ERR
3861lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3862{
3863 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3864 const char *prefix = NULL, *name;
3865 size_t prefix_len = 0;
3866
3867 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3868 name = strchr(dflt, ':');
3869 if (name) {
3870 prefix = dflt;
3871 prefix_len = name - prefix;
3872 ++name;
3873 } else {
3874 name = dflt;
3875 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003876 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003877 /* prefixed default case make sense only for the prefix of the schema itself */
3878 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3879 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3880 prefix_len, prefix);
3881 return LY_EVALID;
3882 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003883 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 +01003884 if (!ch->dflt) {
3885 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3886 "Default case \"%s\" not found.", dflt);
3887 return LY_EVALID;
3888 }
3889 /* no mandatory nodes directly under the default case */
3890 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003891 if (iter->parent != (struct lysc_node*)ch->dflt) {
3892 break;
3893 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003894 if (iter->flags & LYS_MAND_TRUE) {
3895 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3896 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3897 return LY_EVALID;
3898 }
3899 }
Radek Krejci01342af2019-01-03 15:18:08 +01003900 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003901 return LY_SUCCESS;
3902}
3903
Radek Krejciccd20f12019-02-15 14:12:27 +01003904static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003905lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003906{
3907 struct lys_module *mod;
3908 const char *prefix = NULL, *name;
3909 size_t prefix_len = 0;
3910 struct lysc_node_case *cs;
3911 struct lysc_node *node;
3912
3913 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3914 name = strchr(dflt, ':');
3915 if (name) {
3916 prefix = dflt;
3917 prefix_len = name - prefix;
3918 ++name;
3919 } else {
3920 name = dflt;
3921 }
3922 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3923 if (prefix) {
3924 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3925 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003926 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3927 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003928 return LY_EVALID;
3929 }
3930 } else {
3931 mod = ctx->mod;
3932 }
3933 /* get the default case */
Michal Vaskoe444f752020-02-10 12:20:06 +01003934 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 +01003935 if (!cs) {
3936 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003937 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003938 return LY_EVALID;
3939 }
3940
3941 /* check that there is no mandatory node */
3942 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003943 if (node->parent != (struct lysc_node*)cs) {
3944 break;
3945 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003946 if (node->flags & LYS_MAND_TRUE) {
3947 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003948 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3949 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003950 return LY_EVALID;
3951 }
3952 }
3953
3954 /* set the default case in choice */
3955 ch->dflt = cs;
3956 cs->flags |= LYS_SET_DFLT;
3957
3958 return LY_SUCCESS;
3959}
3960
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003961/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003962 * @brief Compile parsed choice node information.
3963 * @param[in] ctx Compile context
3964 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003965 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003966 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003967 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3968 */
3969static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003970lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003971{
3972 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3973 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3974 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01003975 LY_ERR ret = LY_SUCCESS;
3976
Radek Krejci056d0a82018-12-06 16:57:25 +01003977 LY_LIST_FOR(ch_p->child, child_p) {
3978 if (child_p->nodetype == LYS_CASE) {
3979 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003980 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003981 }
3982 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003983 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003984 }
3985 }
3986
3987 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003988 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003989 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003990 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003991
Radek Krejci9800fb82018-12-13 14:26:23 +01003992 return ret;
3993}
3994
3995/**
3996 * @brief Compile parsed anydata or anyxml node information.
3997 * @param[in] ctx Compile context
3998 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01003999 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4000 * is enriched with the any-specific information.
4001 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4002 */
4003static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004004lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004005{
4006 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4007 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4008 unsigned int u;
4009 LY_ERR ret = LY_SUCCESS;
4010
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004011 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004012 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4013 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004014 ly_set_add(&ctx->xpath, any, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004015 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004016
4017 if (any->flags & LYS_CONFIG_W) {
4018 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004019 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004020 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004021done:
4022 return ret;
4023}
4024
Radek Krejcib56c7502019-02-13 14:19:54 +01004025/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004026 * @brief Connect the node into the siblings list and check its name uniqueness.
4027 *
4028 * @param[in] ctx Compile context
4029 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4030 * the choice itself is expected instead of a specific case node.
4031 * @param[in] node Schema node to connect into the list.
4032 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004033 * 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 +01004034 */
4035static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004036lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004037{
4038 struct lysc_node **children;
4039
4040 if (node->nodetype == LYS_CASE) {
4041 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4042 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004043 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004044 }
4045 if (children) {
4046 if (!(*children)) {
4047 /* first child */
4048 *children = node;
4049 } else if (*children != node) {
4050 /* by the condition in previous branch we cover the choice/case children
4051 * - the children list is shared by the choice and the the first case, in addition
4052 * the first child of each case must be referenced from the case node. So the node is
4053 * actually always already inserted in case it is the first children - so here such
4054 * a situation actually corresponds to the first branch */
4055 /* insert at the end of the parent's children list */
4056 (*children)->prev->next = node;
4057 node->prev = (*children)->prev;
4058 (*children)->prev = node;
4059
4060 /* check the name uniqueness */
4061 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4062 lysc_node_notifs(parent), node->name, node)) {
4063 return LY_EEXIST;
4064 }
4065 }
4066 }
4067 return LY_SUCCESS;
4068}
4069
Radek Krejci95710c92019-02-11 15:49:55 +01004070/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004071 * @brief Prepare the case structure in choice node for the new data node.
4072 *
4073 * 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
4074 * created in the choice when the first child was processed.
4075 *
4076 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004077 * @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,
4078 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004079 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4080 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4081 * @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,
4082 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004083 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004084static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004085lys_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 +01004086{
4087 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004088 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004089 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004090 unsigned int u;
4091 LY_ERR ret;
4092
Radek Krejci95710c92019-02-11 15:49:55 +01004093#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004094 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004095 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004096 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4097 return NULL; \
4098 } \
4099 }
4100
Radek Krejci95710c92019-02-11 15:49:55 +01004101 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4102 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004103
4104 /* we have to add an implicit case node into the parent choice */
4105 cs = calloc(1, sizeof(struct lysc_node_case));
4106 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004107 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004108 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004109 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004110 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4111 /* the case is already present since the child is not its first children */
4112 return (struct lysc_node_case*)ch->cases->prev;
4113 }
Radek Krejci95710c92019-02-11 15:49:55 +01004114 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004115
4116 /* explicit parent case is not present (this is its first child) */
4117 cs = calloc(1, sizeof(struct lysc_node_case));
4118 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004119 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004120 cs->flags = LYS_STATUS_MASK & node_p->flags;
4121 cs->sp = node_p;
4122
Radek Krejcib1b59152019-01-07 13:21:56 +01004123 /* 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 +02004124 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004125
4126 if (node_p->when) {
4127 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004128 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004129 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004130
4131 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4132 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004133 ly_set_add(&ctx->xpath, cs, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004134 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004135 }
Radek Krejciec4da802019-05-02 13:02:41 +02004136 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004137 } else {
4138 LOGINT(ctx->ctx);
4139 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004140 }
4141 cs->module = ctx->mod;
4142 cs->prev = (struct lysc_node*)cs;
4143 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004144 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004145 cs->child = child;
4146
4147 return cs;
4148error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004149 if (cs) {
4150 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4151 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004152 return NULL;
4153
4154#undef UNIQUE_CHECK
4155}
4156
Radek Krejcib56c7502019-02-13 14:19:54 +01004157/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004158 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004159 *
4160 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004161 * @param[in] node Target node where the config is supposed to be changed.
4162 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004163 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4164 * 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 +01004165 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4166 * @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 +01004167 * @return LY_ERR value.
4168 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004169static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004170lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004171 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004172{
4173 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004174 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004175
4176 if (config == (node->flags & LYS_CONFIG_MASK)) {
4177 /* nothing to do */
4178 return LY_SUCCESS;
4179 }
4180
4181 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004182 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004183 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4184 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004185 "Invalid %s of config - configuration node cannot be child of any state data node.",
4186 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004187 return LY_EVALID;
4188 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004189 node->flags |= LYS_SET_CONFIG;
4190 } else {
4191 if (node->flags & LYS_SET_CONFIG) {
4192 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4193 /* setting config flags, but have node with explicit config true */
4194 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004195 "Invalid %s of config - configuration node cannot be child of any state data node.",
4196 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004197 return LY_EVALID;
4198 }
4199 /* do not change config on nodes where the config is explicitely set, this does not apply to
4200 * nodes, which are being changed explicitly (targets of refine or deviation) */
4201 return LY_SUCCESS;
4202 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004203 }
4204 node->flags &= ~LYS_CONFIG_MASK;
4205 node->flags |= config;
4206
4207 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004208 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004209 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004210 }
4211
Radek Krejci76b3e962018-12-14 17:01:25 +01004212 return LY_SUCCESS;
4213}
4214
Radek Krejcib56c7502019-02-13 14:19:54 +01004215/**
4216 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4217 *
4218 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4219 * the flag to such parents from a mandatory children.
4220 *
4221 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4222 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4223 * (mandatory children was removed).
4224 */
Radek Krejcife909632019-02-12 15:34:42 +01004225void
4226lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4227{
4228 struct lysc_node *iter;
4229
4230 if (add) { /* set flag */
4231 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4232 parent = parent->parent) {
4233 parent->flags |= LYS_MAND_TRUE;
4234 }
4235 } else { /* unset flag */
4236 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004237 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004238 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004239 /* there is another mandatory node */
4240 return;
4241 }
4242 }
4243 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4244 parent->flags &= ~LYS_MAND_TRUE;
4245 }
4246 }
4247}
4248
Radek Krejci056d0a82018-12-06 16:57:25 +01004249/**
Radek Krejci3641f562019-02-13 15:38:40 +01004250 * @brief Internal sorting process for the lys_compile_augment_sort().
4251 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4252 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4253 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4254 */
4255static void
4256lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4257{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004258 LY_ARRAY_SIZE_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004259 size_t len;
4260
4261 len = strlen(aug_p->nodeid);
4262 LY_ARRAY_FOR(result, v) {
4263 if (strlen(result[v]->nodeid) <= len) {
4264 continue;
4265 }
4266 if (v < LY_ARRAY_SIZE(result)) {
4267 /* move the rest of array */
4268 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4269 break;
4270 }
4271 }
4272 result[v] = aug_p;
4273 LY_ARRAY_INCREMENT(result);
4274}
4275
4276/**
4277 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4278 *
4279 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4280 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4281 *
4282 * @param[in] ctx Compile context.
4283 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4284 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4285 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4286 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4287 * @return LY_ERR value.
4288 */
4289LY_ERR
4290lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4291{
4292 struct lysp_augment **result = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004293 LY_ARRAY_SIZE_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004294
4295 assert(augments);
4296
4297 /* get count of the augments in module and all its submodules */
4298 if (aug_p) {
4299 count += LY_ARRAY_SIZE(aug_p);
4300 }
4301 LY_ARRAY_FOR(inc_p, u) {
4302 if (inc_p[u].submodule->augments) {
4303 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4304 }
4305 }
4306
4307 if (!count) {
4308 *augments = NULL;
4309 return LY_SUCCESS;
4310 }
4311 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4312
4313 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4314 * together, so there can be even /z/y betwwen them. */
4315 LY_ARRAY_FOR(aug_p, u) {
4316 lys_compile_augment_sort_(&aug_p[u], result);
4317 }
4318 LY_ARRAY_FOR(inc_p, u) {
4319 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4320 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4321 }
4322 }
4323
4324 *augments = result;
4325 return LY_SUCCESS;
4326}
4327
4328/**
4329 * @brief Compile the parsed augment connecting it into its target.
4330 *
4331 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4332 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4333 * are already implemented and compiled.
4334 *
4335 * @param[in] ctx Compile context.
4336 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004337 * @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
4338 * children in case of the augmenting uses data.
4339 * @return LY_SUCCESS on success.
4340 * @return LY_EVALID on failure.
4341 */
4342LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004343lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004344{
4345 LY_ERR ret = LY_SUCCESS;
4346 struct lysp_node *node_p, *case_node_p;
4347 struct lysc_node *target; /* target target of the augment */
4348 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004349 struct lysc_when **when, *when_shared;
4350 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004351 uint16_t flags = 0;
4352 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004353 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004354
Radek Krejci327de162019-06-14 12:52:07 +02004355 lysc_update_path(ctx, NULL, "{augment}");
4356 lysc_update_path(ctx, NULL, aug_p->nodeid);
4357
Radek Krejci7af64242019-02-18 13:07:53 +01004358 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004359 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004360 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004361 if (ret != LY_SUCCESS) {
4362 if (ret == LY_EDENIED) {
4363 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4364 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4365 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4366 }
4367 return LY_EVALID;
4368 }
4369
4370 /* check for mandatory nodes
4371 * - new cases augmenting some choice can have mandatory nodes
4372 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4373 */
Radek Krejci733988a2019-02-15 15:12:44 +01004374 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004375 allow_mandatory = 1;
4376 }
4377
4378 when_shared = NULL;
4379 LY_LIST_FOR(aug_p->child, node_p) {
4380 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4381 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004382 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004383 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4384 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004385 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004386 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4387 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004388 return LY_EVALID;
4389 }
4390
4391 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004392 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004393 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004394 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004395 } else {
4396 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004397 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004398 }
4399 }
Radek Krejciec4da802019-05-02 13:02:41 +02004400 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004401
Radek Krejcife13da42019-02-15 14:51:01 +01004402 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4403 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004404 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004405 /* 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 +01004406 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 +01004407 } else if (target->nodetype == LYS_CHOICE) {
4408 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4409 node = ((struct lysc_node_choice*)target)->cases->prev;
4410 } else {
4411 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004412 node = (struct lysc_node*)lysc_node_children(target, flags);
4413 if (!node) {
4414 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4415 break;
4416 }
4417 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004418 }
4419
Radek Krejci733988a2019-02-15 15:12:44 +01004420 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004421 node->flags &= ~LYS_MAND_TRUE;
4422 lys_compile_mandatory_parents(target, 0);
4423 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004424 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004425 return LY_EVALID;
4426 }
4427
4428 /* pass augment's when to all the children */
4429 if (aug_p->when) {
4430 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4431 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004432 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004433 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004434
4435 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4436 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004437 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004438 }
4439
Radek Krejci3641f562019-02-13 15:38:40 +01004440 when_shared = *when;
4441 } else {
4442 ++when_shared->refcount;
4443 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004444
4445 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4446 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004447 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004448 }
Radek Krejci3641f562019-02-13 15:38:40 +01004449 }
4450 }
4451 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004452
Radek Krejciec4da802019-05-02 13:02:41 +02004453 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004454 switch (target->nodetype) {
4455 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004456 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004457 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004458 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004459 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004460 break;
4461 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004462 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004463 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004464 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004465 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004466 break;
4467 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004468 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004469 if (aug_p->actions) {
4470 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004471 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4472 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004473 return LY_EVALID;
4474 }
4475 if (aug_p->notifs) {
4476 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004477 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004478 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004479 return LY_EVALID;
4480 }
4481 }
Radek Krejci3641f562019-02-13 15:38:40 +01004482
Radek Krejci327de162019-06-14 12:52:07 +02004483 lysc_update_path(ctx, NULL, NULL);
4484 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004485error:
Radek Krejciec4da802019-05-02 13:02:41 +02004486 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004487 return ret;
4488}
4489
4490/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004491 * @brief Apply refined or deviated mandatory flag to the target node.
4492 *
4493 * @param[in] ctx Compile context.
4494 * @param[in] node Target node where the mandatory property is supposed to be changed.
4495 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004496 * @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 +01004497 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4498 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4499 * 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 +01004500 * @return LY_ERR value.
4501 */
4502static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004503lys_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 +01004504{
4505 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4506 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004507 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4508 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004509 return LY_EVALID;
4510 }
4511
4512 if (mandatory_flag & LYS_MAND_TRUE) {
4513 /* check if node has default value */
4514 if (node->nodetype & LYS_LEAF) {
4515 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004516 if (refine_flag) {
4517 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004518 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004519 return LY_EVALID;
4520 }
Radek Krejcia1911222019-07-22 17:24:50 +02004521 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004522 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004523 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004524
4525 /* update the list of incomplete default values if needed */
4526 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4527
Radek Krejcia1911222019-07-22 17:24:50 +02004528 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4529 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4530 free(leaf->dflt);
4531 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004532 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004533 }
4534 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004535 if (refine_flag) {
4536 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004537 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004538 return LY_EVALID;
4539 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004540 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004541 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004542 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 +01004543 return LY_EVALID;
4544 }
4545
4546 node->flags &= ~LYS_MAND_FALSE;
4547 node->flags |= LYS_MAND_TRUE;
4548 lys_compile_mandatory_parents(node->parent, 1);
4549 } else {
4550 /* make mandatory false */
4551 node->flags &= ~LYS_MAND_TRUE;
4552 node->flags |= LYS_MAND_FALSE;
4553 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004554 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 +01004555 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004556 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004557 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004558 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4559 leaf->dflt->realtype = leaf->type->dflt->realtype;
4560 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4561 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004562 }
4563 }
4564 return LY_SUCCESS;
4565}
4566
4567/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004568 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4569 * If present, also apply uses's modificators.
4570 *
4571 * @param[in] ctx Compile context
4572 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004573 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4574 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4575 * the compile context.
4576 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4577 */
4578static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004579lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004580{
4581 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004582 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004583 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004584 struct lysc_node_container context_node_fake =
4585 {.nodetype = LYS_CONTAINER,
4586 .module = ctx->mod,
4587 .flags = parent ? parent->flags : 0,
4588 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004589 .prev = (struct lysc_node*)&context_node_fake,
4590 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004591 struct lysp_grp *grp = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004592 LY_ARRAY_SIZE_TYPE u, v;
4593 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004594 int found;
4595 const char *id, *name, *prefix;
4596 size_t prefix_len, name_len;
4597 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004598 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004599 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004600 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004601 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004602 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004603 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004604 struct lysp_augment **augments = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004605 LY_ARRAY_SIZE_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004606 struct lysc_notif **notifs = NULL;
4607 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004608
4609 /* search for the grouping definition */
4610 found = 0;
4611 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004612 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004613 if (prefix) {
4614 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4615 if (!mod) {
4616 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004617 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004618 return LY_EVALID;
4619 }
4620 } else {
4621 mod = ctx->mod_def;
4622 }
4623 if (mod == ctx->mod_def) {
4624 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004625 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004626 LY_ARRAY_FOR(grp, u) {
4627 if (!strcmp(grp[u].name, name)) {
4628 grp = &grp[u];
4629 found = 1;
4630 break;
4631 }
4632 }
4633 }
4634 }
4635 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004636 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004637 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004638 if (grp) {
4639 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4640 if (!strcmp(grp[u].name, name)) {
4641 grp = &grp[u];
4642 found = 1;
4643 }
4644 }
4645 }
4646 if (!found && mod->parsed->includes) {
4647 /* ... and all the submodules */
4648 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4649 grp = mod->parsed->includes[u].submodule->groupings;
4650 if (grp) {
4651 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4652 if (!strcmp(grp[v].name, name)) {
4653 grp = &grp[v];
4654 found = 1;
4655 }
4656 }
4657 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004658 }
4659 }
4660 }
4661 if (!found) {
4662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4663 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4664 return LY_EVALID;
4665 }
4666
4667 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4668 grp_stack_count = ctx->groupings.count;
4669 ly_set_add(&ctx->groupings, (void*)grp, 0);
4670 if (grp_stack_count == ctx->groupings.count) {
4671 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4673 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4674 return LY_EVALID;
4675 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004676 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4677 /* remember that the grouping is instantiated to avoid its standalone validation */
4678 grp->flags |= LYS_USED_GRP;
4679 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004680
4681 /* switch context's mod_def */
4682 mod_old = ctx->mod_def;
4683 ctx->mod_def = mod;
4684
4685 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004686 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 +01004687
Radek Krejcifc11bd72019-04-11 16:00:05 +02004688 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004689 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004690 /* 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 +02004691 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004692
4693 /* some preparation for applying refines */
4694 if (grp->data == node_p) {
4695 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004696 if (parent) {
4697 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4698 } else if (ctx->mod->compiled->data) {
4699 child = ctx->mod->compiled->data;
4700 } else {
4701 child = NULL;
4702 }
4703 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004704 }
4705 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004706 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004707 LY_LIST_FOR(context_node_fake.child, child) {
4708 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004709
Radek Krejcifc11bd72019-04-11 16:00:05 +02004710 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004711 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004712 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004713 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004714 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4715
4716 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4717 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004718 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004719 }
4720
Radek Krejci00b874b2019-02-12 10:54:50 +01004721 when_shared = *when;
4722 } else {
4723 ++when_shared->refcount;
4724 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004725
4726 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4727 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004728 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004729 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004730 }
4731 }
Radek Krejci01342af2019-01-03 15:18:08 +01004732 }
4733 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004734 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004735 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004736 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004737 context_node_fake.child->prev = parent ? lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev;
Radek Krejci76b3e962018-12-14 17:01:25 +01004738 }
4739
Radek Krejcifc11bd72019-04-11 16:00:05 +02004740 /* compile actions */
4741 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4742 if (actions) {
4743 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004744 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004745 if (*actions && (uses_p->augments || uses_p->refines)) {
4746 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4747 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4748 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4749 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4750 }
4751 }
4752
4753 /* compile notifications */
4754 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4755 if (notifs) {
4756 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004757 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004758 if (*notifs && (uses_p->augments || uses_p->refines)) {
4759 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4760 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4761 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4762 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4763 }
4764 }
4765
4766
Radek Krejci3641f562019-02-13 15:38:40 +01004767 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004768 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004769 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004770 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004771 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004772
Radek Krejcif0089082019-01-07 16:42:01 +01004773 /* reload previous context's mod_def */
4774 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004775 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004776
Radek Krejci76b3e962018-12-14 17:01:25 +01004777 /* apply refine */
4778 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004779 lysc_update_path(ctx, NULL, rfn->nodeid);
4780
Radek Krejci7af64242019-02-18 13:07:53 +01004781 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 +01004782 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004783 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004784 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004785
4786 /* default value */
4787 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004788 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004790 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_SIZE_TYPE" default values.",
Radek Krejci327de162019-06-14 12:52:07 +02004791 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004792 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004793 }
4794 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4795 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004796 "Invalid refine of default - %s cannot hold default value(s).",
4797 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004798 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004799 }
4800 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004801 struct ly_err_item *err = NULL;
4802 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4803 if (leaf->dflt) {
4804 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004805 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004806 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4807 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4808 } else {
4809 /* prepare a new one */
4810 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4811 leaf->dflt->realtype = leaf->type;
4812 }
4813 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004814 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004815 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4816 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004817 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004818 leaf->dflt->realtype->refcount++;
4819 if (err) {
4820 ly_err_print(err);
4821 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4822 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4823 ly_err_free(err);
4824 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004825 if (rc == LY_EINCOMPLETE) {
4826 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004827 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004828
4829 /* but in general result is so far ok */
4830 rc = LY_SUCCESS;
4831 }
Radek Krejcia1911222019-07-22 17:24:50 +02004832 LY_CHECK_GOTO(rc, cleanup);
4833 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004834 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004835 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4836
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004837 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004838 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4839 "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 +02004840 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004841 }
Radek Krejcia1911222019-07-22 17:24:50 +02004842
4843 /* remove previous set of default values */
4844 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004845 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004846 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4847 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4848 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004849 }
Radek Krejcia1911222019-07-22 17:24:50 +02004850 LY_ARRAY_FREE(llist->dflts);
4851 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004852 LY_ARRAY_FREE(llist->dflts_mods);
4853 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004854
4855 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004856 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02004857 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004858 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004859 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004860 LY_ARRAY_INCREMENT(llist->dflts_mods);
4861 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004862 LY_ARRAY_INCREMENT(llist->dflts);
4863 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4864 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004865 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004866 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004867 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004868 llist->dflts[u]->realtype->refcount++;
4869 if (err) {
4870 ly_err_print(err);
4871 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4872 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4873 ly_err_free(err);
4874 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004875 if (rc == LY_EINCOMPLETE) {
4876 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004877 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004878
4879 /* but in general result is so far ok */
4880 rc = LY_SUCCESS;
4881 }
4882 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004883 }
Radek Krejcia1911222019-07-22 17:24:50 +02004884 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004885 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004886 if (((struct lysc_node_choice*)node)->dflt) {
4887 /* unset LYS_SET_DFLT from the current default case */
4888 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4889 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004890 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 +01004891 }
4892 }
4893
Radek Krejci12fb9142019-01-08 09:45:30 +01004894 /* description */
4895 if (rfn->dsc) {
4896 FREE_STRING(ctx->ctx, node->dsc);
4897 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4898 }
4899
4900 /* reference */
4901 if (rfn->ref) {
4902 FREE_STRING(ctx->ctx, node->ref);
4903 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4904 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004905
4906 /* config */
4907 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004908 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004909 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004910 } else {
4911 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004912 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004913 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004914 }
4915
4916 /* mandatory */
4917 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004918 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004919 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004920
4921 /* presence */
4922 if (rfn->presence) {
4923 if (node->nodetype != LYS_CONTAINER) {
4924 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004925 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4926 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004927 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004928 }
4929 node->flags |= LYS_PRESENCE;
4930 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004931
4932 /* must */
4933 if (rfn->musts) {
4934 switch (node->nodetype) {
4935 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004936 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 +01004937 break;
4938 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004939 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 +01004940 break;
4941 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004942 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 +01004943 break;
4944 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004945 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 +01004946 break;
4947 case LYS_ANYXML:
4948 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004949 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 +01004950 break;
4951 default:
4952 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004953 "Invalid refine of must statement - %s cannot hold any must statement.",
4954 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004955 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004956 }
Michal Vasko004d3152020-06-11 19:59:22 +02004957 ly_set_add(&ctx->xpath, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01004958 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004959
4960 /* min/max-elements */
4961 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4962 switch (node->nodetype) {
4963 case LYS_LEAFLIST:
4964 if (rfn->flags & LYS_SET_MAX) {
4965 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4966 }
4967 if (rfn->flags & LYS_SET_MIN) {
4968 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004969 if (rfn->min) {
4970 node->flags |= LYS_MAND_TRUE;
4971 lys_compile_mandatory_parents(node->parent, 1);
4972 } else {
4973 node->flags &= ~LYS_MAND_TRUE;
4974 lys_compile_mandatory_parents(node->parent, 0);
4975 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004976 }
4977 break;
4978 case LYS_LIST:
4979 if (rfn->flags & LYS_SET_MAX) {
4980 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4981 }
4982 if (rfn->flags & LYS_SET_MIN) {
4983 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004984 if (rfn->min) {
4985 node->flags |= LYS_MAND_TRUE;
4986 lys_compile_mandatory_parents(node->parent, 1);
4987 } else {
4988 node->flags &= ~LYS_MAND_TRUE;
4989 lys_compile_mandatory_parents(node->parent, 0);
4990 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004991 }
4992 break;
4993 default:
4994 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004995 "Invalid refine of %s statement - %s cannot hold this statement.",
4996 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004997 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004998 }
4999 }
Radek Krejcif0089082019-01-07 16:42:01 +01005000
5001 /* if-feature */
5002 if (rfn->iffeatures) {
5003 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005004 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005005 }
Radek Krejci327de162019-06-14 12:52:07 +02005006
5007 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005008 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005009
Radek Krejcif2271f12019-01-07 16:42:23 +01005010 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005011 for (uint32_t i = 0; i < refined.count; ++i) {
5012 node = (struct lysc_node*)refined.objs[i];
5013 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02005014 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005015
5016 /* check possible conflict with default value (default added, mandatory left true) */
5017 if ((node->flags & LYS_MAND_TRUE) &&
5018 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5019 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005021 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005022 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005023 }
5024
5025 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5026 if (node->nodetype == LYS_LIST) {
5027 min = ((struct lysc_node_list*)node)->min;
5028 max = ((struct lysc_node_list*)node)->max;
5029 } else {
5030 min = ((struct lysc_node_leaflist*)node)->min;
5031 max = ((struct lysc_node_leaflist*)node)->max;
5032 }
5033 if (min > max) {
5034 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005035 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5036 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005037 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005038 }
5039 }
5040 }
5041
Radek Krejci327de162019-06-14 12:52:07 +02005042 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005043 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005044
5045cleanup:
5046 /* fix connection of the children nodes from fake context node back into the parent */
5047 if (context_node_fake.child) {
5048 context_node_fake.child->prev = child;
5049 }
5050 LY_LIST_FOR(context_node_fake.child, child) {
5051 child->parent = parent;
5052 }
5053
5054 if (uses_p->augments || uses_p->refines) {
5055 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005056 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005057 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5058 LY_ARRAY_FREE(context_node_fake.actions);
5059 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005060 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005061 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5062 LY_ARRAY_FREE(context_node_fake.notifs);
5063 }
5064 }
5065
Radek Krejcie86bf772018-12-14 11:39:53 +01005066 /* reload previous context's mod_def */
5067 ctx->mod_def = mod_old;
5068 /* remove the grouping from the stack for circular groupings dependency check */
5069 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5070 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005071 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005072 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005073
5074 return ret;
5075}
5076
Radek Krejci327de162019-06-14 12:52:07 +02005077static int
5078lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5079{
5080 struct lysp_node *iter;
5081 int len = 0;
5082
5083 *path = NULL;
5084 for (iter = node; iter && len >= 0; iter = iter->parent) {
5085 char *s = *path;
5086 char *id;
5087
5088 switch (iter->nodetype) {
5089 case LYS_USES:
5090 asprintf(&id, "{uses='%s'}", iter->name);
5091 break;
5092 case LYS_GROUPING:
5093 asprintf(&id, "{grouping='%s'}", iter->name);
5094 break;
5095 case LYS_AUGMENT:
5096 asprintf(&id, "{augment='%s'}", iter->name);
5097 break;
5098 default:
5099 id = strdup(iter->name);
5100 break;
5101 }
5102
5103 if (!iter->parent) {
5104 /* print prefix */
5105 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5106 } else {
5107 /* prefix is the same as in parent */
5108 len = asprintf(path, "/%s%s", id, s ? s : "");
5109 }
5110 free(s);
5111 free(id);
5112 }
5113
5114 if (len < 0) {
5115 free(*path);
5116 *path = NULL;
5117 } else if (len == 0) {
5118 *path = strdup("/");
5119 len = 1;
5120 }
5121 return len;
5122}
5123
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005124/**
5125 * @brief Validate groupings that were defined but not directly used in the schema itself.
5126 *
5127 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5128 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5129 */
5130static LY_ERR
5131lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5132{
5133 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005134 char *path;
5135 int len;
5136
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005137 struct lysp_node_uses fake_uses = {
5138 .parent = node_p,
5139 .nodetype = LYS_USES,
5140 .flags = 0, .next = NULL,
5141 .name = grp->name,
5142 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5143 .refines = NULL, .augments = NULL
5144 };
5145 struct lysc_node_container fake_container = {
5146 .nodetype = LYS_CONTAINER,
5147 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5148 .module = ctx->mod,
5149 .sp = NULL, .parent = NULL, .next = NULL,
5150 .prev = (struct lysc_node*)&fake_container,
5151 .name = "fake",
5152 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5153 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5154 };
5155
5156 if (grp->parent) {
5157 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5158 }
Radek Krejci327de162019-06-14 12:52:07 +02005159
5160 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5161 if (len < 0) {
5162 LOGMEM(ctx->ctx);
5163 return LY_EMEM;
5164 }
5165 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5166 ctx->path_len = (uint16_t)len;
5167 free(path);
5168
5169 lysc_update_path(ctx, NULL, "{grouping}");
5170 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005171 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005172 lysc_update_path(ctx, NULL, NULL);
5173 lysc_update_path(ctx, NULL, NULL);
5174
5175 ctx->path_len = 1;
5176 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005177
5178 /* cleanup */
5179 lysc_node_container_free(ctx->ctx, &fake_container);
5180
5181 return ret;
5182}
Radek Krejcife909632019-02-12 15:34:42 +01005183
Radek Krejcie86bf772018-12-14 11:39:53 +01005184/**
Radek Krejcia3045382018-11-22 14:30:31 +01005185 * @brief Compile parsed schema node information.
5186 * @param[in] ctx Compile context
5187 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005188 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5189 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5190 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005191 * @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).
5192 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005193 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5194 */
Radek Krejci19a96102018-11-15 13:38:09 +01005195static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005196lys_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 +01005197{
Radek Krejci1c54f462020-05-12 17:25:34 +02005198 LY_ERR ret = LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01005199 struct lysc_node *node;
5200 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005201 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005202 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005203 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005204
Radek Krejci327de162019-06-14 12:52:07 +02005205 if (node_p->nodetype != LYS_USES) {
5206 lysc_update_path(ctx, parent, node_p->name);
5207 } else {
5208 lysc_update_path(ctx, NULL, "{uses}");
5209 lysc_update_path(ctx, NULL, node_p->name);
5210 }
5211
Radek Krejci19a96102018-11-15 13:38:09 +01005212 switch (node_p->nodetype) {
5213 case LYS_CONTAINER:
5214 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5215 node_compile_spec = lys_compile_node_container;
5216 break;
5217 case LYS_LEAF:
5218 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5219 node_compile_spec = lys_compile_node_leaf;
5220 break;
5221 case LYS_LIST:
5222 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005223 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005224 break;
5225 case LYS_LEAFLIST:
5226 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005227 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005228 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005229 case LYS_CHOICE:
5230 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005231 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005232 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005233 case LYS_ANYXML:
5234 case LYS_ANYDATA:
5235 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005236 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005237 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005238 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005239 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5240 lysc_update_path(ctx, NULL, NULL);
5241 lysc_update_path(ctx, NULL, NULL);
5242 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005243 default:
5244 LOGINT(ctx->ctx);
5245 return LY_EINT;
5246 }
5247 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5248 node->nodetype = node_p->nodetype;
5249 node->module = ctx->mod;
5250 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005251 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005252
5253 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005254 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005255 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005256 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005257 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5258 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005259 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005260 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005261 node->flags |= LYS_CONFIG_R;
5262 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005263 /* config not explicitely set, inherit it from parent */
5264 if (parent) {
5265 node->flags |= parent->flags & LYS_CONFIG_MASK;
5266 } else {
5267 /* default is config true */
5268 node->flags |= LYS_CONFIG_W;
5269 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005270 } else {
5271 /* config set explicitely */
5272 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005273 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005274 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5275 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5276 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005277 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005278 goto error;
5279 }
Radek Krejci19a96102018-11-15 13:38:09 +01005280
Radek Krejcia6d57732018-11-29 13:40:37 +01005281 /* *list ordering */
5282 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5283 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005284 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005285 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5286 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005287 node->flags &= ~LYS_ORDBY_MASK;
5288 node->flags |= LYS_ORDBY_SYSTEM;
5289 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5290 /* default ordering is system */
5291 node->flags |= LYS_ORDBY_SYSTEM;
5292 }
5293 }
5294
Radek Krejci19a96102018-11-15 13:38:09 +01005295 /* status - it is not inherited by specification, but it does not make sense to have
5296 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005297 if (!parent || parent->nodetype != LYS_CHOICE) {
5298 /* in case of choice/case's children, postpone the check to the moment we know if
5299 * 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 +02005300 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 +01005301 }
5302
Radek Krejciec4da802019-05-02 13:02:41 +02005303 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005304 node->sp = node_p;
5305 }
5306 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005307 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5308 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005309 if (node_p->when) {
5310 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005311 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005312
5313 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5314 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02005315 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01005316 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005317 }
Radek Krejciec4da802019-05-02 13:02:41 +02005318 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005319
5320 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005321 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005322
Radek Krejci0935f412019-08-20 16:15:18 +02005323 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5324
Radek Krejcife909632019-02-12 15:34:42 +01005325 /* inherit LYS_MAND_TRUE in parent containers */
5326 if (node->flags & LYS_MAND_TRUE) {
5327 lys_compile_mandatory_parents(parent, 1);
5328 }
5329
Radek Krejci327de162019-06-14 12:52:07 +02005330 lysc_update_path(ctx, NULL, NULL);
5331
Radek Krejci19a96102018-11-15 13:38:09 +01005332 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005333 if (parent) {
5334 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005335 if (node_p->parent->nodetype == LYS_CASE) {
5336 lysc_update_path(ctx, parent, node_p->parent->name);
5337 } else {
5338 lysc_update_path(ctx, parent, node->name);
5339 }
Radek Krejciec4da802019-05-02 13:02:41 +02005340 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005341 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005342 if (uses_status) {
5343
5344 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005345 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005346 * it directly gets the same status flags as the choice;
5347 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005348 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005349 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005350 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005351 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005352 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005353 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005354 }
Radek Krejciec4da802019-05-02 13:02:41 +02005355 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 +02005356
5357 if (parent->nodetype == LYS_CHOICE) {
5358 lysc_update_path(ctx, NULL, NULL);
5359 }
Radek Krejci19a96102018-11-15 13:38:09 +01005360 } else {
5361 /* top-level element */
5362 if (!ctx->mod->compiled->data) {
5363 ctx->mod->compiled->data = node;
5364 } else {
5365 /* insert at the end of the module's top-level nodes list */
5366 ctx->mod->compiled->data->prev->next = node;
5367 node->prev = ctx->mod->compiled->data->prev;
5368 ctx->mod->compiled->data->prev = node;
5369 }
Radek Krejci327de162019-06-14 12:52:07 +02005370 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005371 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5372 ctx->mod->compiled->notifs, node->name, node)) {
5373 return LY_EVALID;
5374 }
Radek Krejci19a96102018-11-15 13:38:09 +01005375 }
Radek Krejci327de162019-06-14 12:52:07 +02005376 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005377
5378 return LY_SUCCESS;
5379
5380error:
5381 lysc_node_free(ctx->ctx, node);
5382 return ret;
5383}
5384
Radek Krejciccd20f12019-02-15 14:12:27 +01005385static void
5386lysc_disconnect(struct lysc_node *node)
5387{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005388 struct lysc_node *parent, *child, *prev = NULL, *next;
5389 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005390 int remove_cs = 0;
5391
5392 parent = node->parent;
5393
5394 /* parent's first child */
5395 if (!parent) {
5396 return;
5397 }
5398 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005399 cs = (struct lysc_node_case*)node;
5400 } else if (parent->nodetype == LYS_CASE) {
5401 /* disconnecting some node in a case */
5402 cs = (struct lysc_node_case*)parent;
5403 parent = cs->parent;
5404 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5405 if (child == node) {
5406 if (cs->child == child) {
5407 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5408 /* case with a single child -> remove also the case */
5409 child->parent = NULL;
5410 remove_cs = 1;
5411 } else {
5412 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005413 }
5414 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005415 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005416 }
5417 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005418 if (!remove_cs) {
5419 cs = NULL;
5420 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005421 } else if (lysc_node_children(parent, node->flags) == node) {
5422 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005423 }
5424
5425 if (cs) {
5426 if (remove_cs) {
5427 /* cs has only one child which is being also removed */
5428 lysc_disconnect((struct lysc_node*)cs);
5429 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5430 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005431 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5432 /* default case removed */
5433 ((struct lysc_node_choice*)parent)->dflt = NULL;
5434 }
5435 if (((struct lysc_node_choice*)parent)->cases == cs) {
5436 /* first case removed */
5437 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5438 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005439 if (cs->child) {
5440 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5441 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5442 prev = cs->child->prev;
5443 } /* else all the children are under a single case */
5444 LY_LIST_FOR_SAFE(cs->child, next, child) {
5445 if (child->parent != (struct lysc_node*)cs) {
5446 break;
5447 }
5448 lysc_node_free(node->module->ctx, child);
5449 }
5450 if (prev) {
5451 if (prev->next) {
5452 prev->next = child;
5453 }
5454 if (child) {
5455 child->prev = prev;
5456 } else {
5457 /* link from the first child under the cases */
5458 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5459 }
5460 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005461 }
5462 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005463 }
5464
5465 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005466 if (node->prev->next) {
5467 node->prev->next = node->next;
5468 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005469 if (node->next) {
5470 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005471 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005472 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005473 if (child) {
5474 child->prev = node->prev;
5475 }
5476 } else if (((struct lysc_node_choice*)parent)->cases) {
5477 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005478 }
5479}
5480
5481LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005482lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005483{
Radek Krejcia1911222019-07-22 17:24:50 +02005484 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005485 struct ly_set devs_p = {0};
5486 struct ly_set targets = {0};
5487 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005488 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005489 struct lysc_action *rpcs;
5490 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005491 struct lysp_deviation *dev;
5492 struct lysp_deviate *d, **dp_new;
5493 struct lysp_deviate_add *d_add;
5494 struct lysp_deviate_del *d_del;
5495 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005496 LY_ARRAY_SIZE_TYPE u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005497 struct lysc_deviation {
5498 const char *nodeid;
5499 struct lysc_node *target; /* target node of the deviation */
5500 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005501 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005502 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5503 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005504 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005505 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005506 const char *prefix, *name, *nodeid, *dflt;
Michal Vasko57c10cd2020-05-27 15:57:11 +02005507 struct lys_module *mod, **dev_mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005508 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005509 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005510
5511 /* get all deviations from the module and all its submodules ... */
5512 LY_ARRAY_FOR(mod_p->deviations, u) {
5513 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5514 }
5515 LY_ARRAY_FOR(mod_p->includes, v) {
5516 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5517 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5518 }
5519 }
5520 if (!devs_p.count) {
5521 /* nothing to do */
5522 return LY_SUCCESS;
5523 }
5524
Radek Krejci327de162019-06-14 12:52:07 +02005525 lysc_update_path(ctx, NULL, "{deviation}");
5526
Radek Krejciccd20f12019-02-15 14:12:27 +01005527 /* ... and group them by the target node */
5528 devs = calloc(devs_p.count, sizeof *devs);
5529 for (u = 0; u < devs_p.count; ++u) {
5530 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005531 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005532
5533 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005534 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5535 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01005536 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005537 /* move the target pointer to input/output to make them different from the action and
5538 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5539 * back to the RPC/action node due to a better compatibility and decision code in this function.
5540 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5541 if (flags & LYSC_OPT_RPC_INPUT) {
5542 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5543 flags |= LYSC_OPT_INTERNAL;
5544 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5545 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5546 flags |= LYSC_OPT_INTERNAL;
5547 }
5548 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005549 /* insert into the set of targets with duplicity detection */
5550 i = ly_set_add(&targets, target, 0);
5551 if (!devs[i]) {
5552 /* new record */
5553 devs[i] = calloc(1, sizeof **devs);
5554 devs[i]->target = target;
5555 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005556 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005557 }
5558 /* add deviates into the deviation's list of deviates */
5559 for (d = dev->deviates; d; d = d->next) {
5560 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5561 *dp_new = d;
5562 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5563 devs[i]->not_supported = 1;
5564 }
5565 }
Radek Krejci327de162019-06-14 12:52:07 +02005566
5567 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005568 }
5569
5570 /* MACROS for deviates checking */
5571#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5572 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005573 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
Radek Krejciccd20f12019-02-15 14:12:27 +01005574 goto cleanup; \
5575 }
5576
5577#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5578 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005579 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_SIZE_TYPE") %s properties.", \
Radek Krejci327de162019-06-14 12:52:07 +02005580 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005581 goto cleanup; \
5582 }
5583
Radek Krejcia1911222019-07-22 17:24:50 +02005584
Radek Krejciccd20f12019-02-15 14:12:27 +01005585#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005586 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5587 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5588 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5589 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5590 goto cleanup; \
5591 }
5592
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005593#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005594 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005595 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005596 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5597 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005598 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005599 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5600 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005601 goto cleanup; \
5602 }
5603
Radek Krejci551b12c2019-02-19 16:11:21 +01005604#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5605 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5606 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005607 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5608 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005609 goto cleanup; \
5610 }
5611
Radek Krejciccd20f12019-02-15 14:12:27 +01005612#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5613 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005614 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005615 goto cleanup; \
5616 }
5617
Radek Krejci551b12c2019-02-19 16:11:21 +01005618#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5619 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5620 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005621 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005622 goto cleanup; \
5623 }
5624
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005625#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5626 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5627 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5628 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5629 if (!strcmp(((TYPE)devs[u]->target)->ARRAY_TRG[y]VALMEMBER_CMP, d_del->ARRAY_DEV[x]VALMEMBER)) { break; } \
Radek Krejciccd20f12019-02-15 14:12:27 +01005630 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005631 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005632 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005633 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5634 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005635 goto cleanup; \
5636 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005637 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5638 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5639 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5640 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5641 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005642 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005643 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5644 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5645 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005646 }
5647
5648 /* apply deviations */
5649 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005650 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5651 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5652 struct ly_err_item *err = NULL;
5653
5654 dflt = NULL;
5655 changed_type = 0;
5656
Radek Krejci327de162019-06-14 12:52:07 +02005657 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5658
Radek Krejcif538ce52019-03-05 10:46:14 +01005659 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5660 /* fix the target pointer in case of RPC's/action's input/output */
5661 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5662 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5663 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5664 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5665 }
5666 }
5667
Radek Krejciccd20f12019-02-15 14:12:27 +01005668 /* not-supported */
5669 if (devs[u]->not_supported) {
5670 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005671 LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_SIZE_TYPE") deviates on node \"%s\" since the node is not-supported.",
Radek Krejciccd20f12019-02-15 14:12:27 +01005672 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5673 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005674
5675#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5676 if (devs[u]->target->parent) { \
5677 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5678 } else { \
5679 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5680 } \
5681 LY_ARRAY_FOR(ARRAY, x) { \
5682 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5683 } \
5684 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5685 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5686 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5687 LY_ARRAY_DECREMENT(ARRAY); \
5688 }
5689
Michal Vasko1bf09392020-03-27 12:38:10 +01005690 if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005691 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5692 /* remove RPC's/action's input */
5693 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5694 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005695 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5696 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005697 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5698 /* remove RPC's/action's output */
5699 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5700 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005701 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5702 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005703 } else {
5704 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005705 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005706 }
5707 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005708 /* remove Notification */
5709 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005710 } else {
5711 /* remove the target node */
5712 lysc_disconnect(devs[u]->target);
5713 lysc_node_free(ctx->ctx, devs[u]->target);
5714 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005715
Radek Krejci474f9b82019-07-24 11:36:37 +02005716 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5717 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005718 continue;
5719 }
5720
5721 /* list of deviates (not-supported is not present in the list) */
5722 LY_ARRAY_FOR(devs[u]->deviates, v) {
5723 d = devs[u]->deviates[v];
5724
5725 switch (d->mod) {
5726 case LYS_DEV_ADD:
5727 d_add = (struct lysp_deviate_add*)d;
5728 /* [units-stmt] */
5729 if (d_add->units) {
5730 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5731 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5732
5733 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5734 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5735 }
5736
5737 /* *must-stmt */
5738 if (d_add->musts) {
5739 switch (devs[u]->target->nodetype) {
5740 case LYS_CONTAINER:
5741 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005742 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5743 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005744 break;
5745 case LYS_LEAF:
5746 case LYS_LEAFLIST:
5747 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005748 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5749 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005750 break;
5751 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005752 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5753 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005754 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005755 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005756 case LYS_ACTION:
5757 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005758 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5759 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005760 break;
Michal Vaskocc048b22020-03-27 15:52:38 +01005761 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005762 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5763 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005764 break;
5765 }
5766 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005767 default:
5768 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005769 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005770 goto cleanup;
5771 }
Michal Vasko004d3152020-06-11 19:59:22 +02005772 ly_set_add(&ctx->xpath, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01005773 }
5774
5775 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005776 if (d_add->uniques) {
5777 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5778 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5779 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005780
5781 /* *default-stmt */
5782 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005783 switch (devs[u]->target->nodetype) {
5784 case LYS_LEAF:
5785 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005786 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt, dflt_mod);
Radek Krejcia1911222019-07-22 17:24:50 +02005787 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005788 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005789 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005790 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5791 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5792 } else {
5793 /* prepare new default value storage */
5794 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005795 }
Radek Krejcia1911222019-07-22 17:24:50 +02005796 dflt = d_add->dflts[0];
5797 /* parsing is done at the end after possible replace of the leaf's type */
5798
Radek Krejci551b12c2019-02-19 16:11:21 +01005799 /* mark the new default values as leaf's own */
5800 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005801 break;
5802 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005803 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005804 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005805 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005806 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005807 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5808 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5809 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005810 }
Radek Krejcia1911222019-07-22 17:24:50 +02005811 LY_ARRAY_FREE(llist->dflts);
5812 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005813 LY_ARRAY_FREE(llist->dflts_mods);
5814 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005815 }
5816 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005817 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005818 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5819 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01005820 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005821 LY_ARRAY_INCREMENT(llist->dflts_mods);
5822 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005823 LY_ARRAY_INCREMENT(llist->dflts);
5824 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5825 llist->dflts[x]->realtype = llist->type;
5826 rc = llist->type->plugin->store(ctx->ctx, llist->type, d_add->dflts[x - y], strlen(d_add->dflts[x - y]),
Radek Krejci474f9b82019-07-24 11:36:37 +02005827 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005828 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005829 llist->dflts[x]->realtype->refcount++;
5830 if (err) {
5831 ly_err_print(err);
5832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5833 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5834 d_add->dflts[x - y], err->msg);
5835 ly_err_free(err);
5836 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005837 if (rc == LY_EINCOMPLETE) {
5838 /* postpone default compilation when the tree is complete */
5839 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5840
5841 /* but in general result is so far ok */
5842 rc = LY_SUCCESS;
5843 }
Radek Krejcia1911222019-07-22 17:24:50 +02005844 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005845 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005846 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005847 devs[u]->target->flags |= LYS_SET_DFLT;
5848 break;
5849 case LYS_CHOICE:
5850 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5851 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5852 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5853 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005854 if (lys_compile_deviation_set_choice_dflt(ctx, d_add->dflts[0], (struct lysc_node_choice*)devs[u]->target)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005855 goto cleanup;
5856 }
5857 break;
5858 default:
5859 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005860 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005861 goto cleanup;
5862 }
5863 }
5864
5865 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005866 if (d_add->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01005867 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005868 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005869 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5870 goto cleanup;
5871 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005872 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005873 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
Michal Vaskoa3881362020-01-21 15:57:35 +01005874 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005875 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005876 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5877 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005878 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5879 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005880 goto cleanup;
5881 }
Radek Krejci327de162019-06-14 12:52:07 +02005882 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005883 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005884
5885 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005886 if (d_add->flags & LYS_MAND_MASK) {
5887 if (devs[u]->target->flags & LYS_MAND_MASK) {
5888 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005889 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5890 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005891 goto cleanup;
5892 }
Radek Krejci327de162019-06-14 12:52:07 +02005893 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005894 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005895
5896 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005897 if (d_add->flags & LYS_SET_MIN) {
5898 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5899 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5900 /* change value */
5901 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5902 } else if (devs[u]->target->nodetype == LYS_LIST) {
5903 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5904 /* change value */
5905 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5906 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005907 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005908 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5909 goto cleanup;
5910 }
5911 if (d_add->min) {
5912 devs[u]->target->flags |= LYS_MAND_TRUE;
5913 }
5914 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005915
5916 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005917 if (d_add->flags & LYS_SET_MAX) {
5918 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5919 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5920 /* change value */
5921 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5922 } else if (devs[u]->target->nodetype == LYS_LIST) {
5923 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5924 /* change value */
5925 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5926 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005927 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005928 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5929 goto cleanup;
5930 }
5931 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005932
5933 break;
5934 case LYS_DEV_DELETE:
5935 d_del = (struct lysp_deviate_del*)d;
5936
5937 /* [units-stmt] */
5938 if (d_del->units) {
5939 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02005940 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
5941 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
5942 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5943 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
5944 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5945 goto cleanup;
5946 }
5947 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5948 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005949 }
5950
5951 /* *must-stmt */
5952 if (d_del->musts) {
5953 switch (devs[u]->target->nodetype) {
5954 case LYS_CONTAINER:
5955 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005956 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005957 break;
5958 case LYS_LEAF:
5959 case LYS_LEAFLIST:
5960 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005961 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005962 break;
5963 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005964 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005965 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005966 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005967 case LYS_ACTION:
5968 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5969 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5970 break;
5971 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5972 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5973 break;
5974 }
5975 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005976 default:
5977 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005978 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005979 goto cleanup;
5980 }
5981 }
5982
5983 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005984 if (d_del->uniques) {
5985 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5986 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5987 LY_ARRAY_FOR(d_del->uniques, x) {
5988 LY_ARRAY_FOR(list->uniques, z) {
5989 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5990 nodeid = strpbrk(name, " \t\n");
5991 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02005992 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01005993 break;
5994 }
5995 while (isspace(*nodeid)) {
5996 ++nodeid;
5997 }
5998 } else {
5999 if (strcmp(name, list->uniques[z][y]->name)) {
6000 break;
6001 }
6002 }
6003 }
6004 if (!name) {
6005 /* complete match - remove the unique */
6006 LY_ARRAY_DECREMENT(list->uniques);
6007 LY_ARRAY_FREE(list->uniques[z]);
6008 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6009 --z;
6010 break;
6011 }
6012 }
6013 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6014 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006015 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6016 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006017 goto cleanup;
6018 }
6019 }
6020 if (!LY_ARRAY_SIZE(list->uniques)) {
6021 LY_ARRAY_FREE(list->uniques);
6022 list->uniques = NULL;
6023 }
6024 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006025
6026 /* *default-stmt */
6027 if (d_del->dflts) {
6028 switch (devs[u]->target->nodetype) {
6029 case LYS_LEAF:
6030 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6031 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6032 dflt, "deleting", "default", d_del->dflts[0]);
6033
Radek Krejcia1911222019-07-22 17:24:50 +02006034 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006035 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006036 if (strcmp(dflt, d_del->dflts[0])) {
6037 if (i) {
6038 free((char*)dflt);
6039 }
6040 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6041 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6042 d_del->dflts[0], dflt);
6043 goto cleanup;
6044 }
6045 if (i) {
6046 free((char*)dflt);
6047 }
6048 dflt = NULL;
6049
Radek Krejci474f9b82019-07-24 11:36:37 +02006050 /* update the list of incomplete default values if needed */
6051 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6052
Radek Krejcia1911222019-07-22 17:24:50 +02006053 /* remove the default specification */
6054 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6055 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6056 free(leaf->dflt);
6057 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006058 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006059 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006060 break;
6061 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006062 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6063 LY_ARRAY_FOR(d_del->dflts, x) {
6064 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006065 dflt = llist->type->plugin->print(llist->dflts[y], LYD_XML, lys_get_prefix, llist->dflts_mods[y], &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006066 if (!strcmp(dflt, d_del->dflts[x])) {
6067 if (i) {
6068 free((char*)dflt);
6069 }
6070 dflt = NULL;
6071 break;
6072 }
6073 if (i) {
6074 free((char*)dflt);
6075 }
6076 dflt = NULL;
6077 }
6078 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6079 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6080 "which does not match any of the target's property values.", d_del->dflts[x]);
6081 goto cleanup;
6082 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006083
6084 /* update the list of incomplete default values if needed */
6085 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6086
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006087 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006088 LY_ARRAY_DECREMENT(llist->dflts);
6089 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6090 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6091 free(llist->dflts[y]);
6092 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006093 memmove(&llist->dflts_mods[y], &llist->dflts_mods[y + 1], (LY_ARRAY_SIZE(llist->dflts_mods) - y) * (sizeof *llist->dflts_mods));
Radek Krejcia1911222019-07-22 17:24:50 +02006094 }
6095 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006096 LY_ARRAY_FREE(llist->dflts_mods);
6097 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006098 LY_ARRAY_FREE(llist->dflts);
6099 llist->dflts = NULL;
6100 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006101 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006102 break;
6103 case LYS_CHOICE:
6104 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6105 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6106 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006107 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006108 if (prefix) {
6109 /* use module prefixes from the deviation module to match the module of the default case */
6110 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6111 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006112 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6113 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006114 goto cleanup;
6115 }
6116 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6117 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006118 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6119 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006120 goto cleanup;
6121 }
6122 }
6123 /* else {
6124 * strictly, the default prefix would point to the deviation module, but the value should actually
6125 * match the default string in the original module (usually unprefixed), so in this case we do not check
6126 * the module of the default case, just matching its name */
6127 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6128 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006129 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6130 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006131 goto cleanup;
6132 }
6133 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6134 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6135 break;
6136 default:
6137 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006138 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006139 goto cleanup;
6140 }
6141 }
6142
6143 break;
6144 case LYS_DEV_REPLACE:
6145 d_rpl = (struct lysp_deviate_rpl*)d;
6146
6147 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006148 if (d_rpl->type) {
6149 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6150 /* type is mandatory, so checking for its presence is not necessary */
6151 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006152
6153 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6154 /* the target has default from the previous type - remove it */
6155 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006156 /* update the list of incomplete default values if needed */
6157 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6158
Radek Krejcia1911222019-07-22 17:24:50 +02006159 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6160 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6161 free(leaf->dflt);
6162 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006163 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006164 } else { /* LYS_LEAFLIST */
6165 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006166 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006167 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6168 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6169 free(llist->dflts[x]);
6170 }
6171 LY_ARRAY_FREE(llist->dflts);
6172 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006173 LY_ARRAY_FREE(llist->dflts_mods);
6174 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006175 }
6176 }
6177 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006178 /* there is no default value, do not set changed_type after type compilation
6179 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006180 changed_type = -1;
6181 }
Radek Krejciec4da802019-05-02 13:02:41 +02006182 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, (struct lysc_node_leaf*)devs[u]->target), cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006183 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006184 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006185
6186 /* [units-stmt] */
6187 if (d_rpl->units) {
6188 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6189 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6190 units, "replacing", "units", d_rpl->units);
6191
6192 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6193 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6194 }
6195
6196 /* [default-stmt] */
6197 if (d_rpl->dflt) {
6198 switch (devs[u]->target->nodetype) {
6199 case LYS_LEAF:
6200 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6201 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006202 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006203 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006204 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6205 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6206 dflt = d_rpl->dflt;
6207 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006208 break;
6209 case LYS_CHOICE:
6210 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006211 if (lys_compile_deviation_set_choice_dflt(ctx, d_rpl->dflt, (struct lysc_node_choice*)devs[u]->target)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006212 goto cleanup;
6213 }
6214 break;
6215 default:
6216 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006217 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006218 goto cleanup;
6219 }
6220 }
6221
6222 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006223 if (d_rpl->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01006224 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006225 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006226 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6227 goto cleanup;
6228 }
6229 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006230 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006231 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6232 goto cleanup;
6233 }
Radek Krejci327de162019-06-14 12:52:07 +02006234 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006235 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006236
6237 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006238 if (d_rpl->flags & LYS_MAND_MASK) {
6239 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006240 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006241 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6242 goto cleanup;
6243 }
Radek Krejci327de162019-06-14 12:52:07 +02006244 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006245 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006246
6247 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006248 if (d_rpl->flags & LYS_SET_MIN) {
6249 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6250 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6251 /* change value */
6252 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6253 } else if (devs[u]->target->nodetype == LYS_LIST) {
6254 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6255 /* change value */
6256 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6257 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006258 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006259 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6260 goto cleanup;
6261 }
6262 if (d_rpl->min) {
6263 devs[u]->target->flags |= LYS_MAND_TRUE;
6264 }
6265 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006266
6267 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006268 if (d_rpl->flags & LYS_SET_MAX) {
6269 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6270 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6271 /* change value */
6272 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6273 } else if (devs[u]->target->nodetype == LYS_LIST) {
6274 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6275 /* change value */
6276 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6277 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006278 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006279 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6280 goto cleanup;
6281 }
6282 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006283
6284 break;
6285 default:
6286 LOGINT(ctx->ctx);
6287 goto cleanup;
6288 }
6289 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006290
Radek Krejci33f72892019-02-21 10:36:58 +01006291 /* final check when all deviations of a single target node are applied */
6292
Radek Krejci551b12c2019-02-19 16:11:21 +01006293 /* check min-max compatibility */
6294 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6295 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6296 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6297 } else if (devs[u]->target->nodetype == LYS_LIST) {
6298 min = ((struct lysc_node_list*)devs[u]->target)->min;
6299 max = ((struct lysc_node_list*)devs[u]->target)->max;
6300 } else {
6301 min = max = 0;
6302 }
6303 if (min > max) {
6304 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
Radek Krejci327de162019-06-14 12:52:07 +02006305 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006306 goto cleanup;
6307 }
6308
Radek Krejcia1911222019-07-22 17:24:50 +02006309 if (dflt) {
6310 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006311 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006312 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006313 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6314 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006315 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006316 leaf->dflt->realtype->refcount++;
6317 if (err) {
6318 ly_err_print(err);
6319 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6320 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6321 ly_err_free(err);
6322 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006323 if (rc == LY_EINCOMPLETE) {
6324 /* postpone default compilation when the tree is complete */
6325 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6326
6327 /* but in general result is so far ok */
6328 rc = LY_SUCCESS;
6329 }
Radek Krejcia1911222019-07-22 17:24:50 +02006330 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006331 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006332 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6333 int dynamic;
6334 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006335 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006336
6337 /* update the list of incomplete default values if needed */
6338 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6339
6340 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006341 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6342 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6343 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006344 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6345 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006346 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006347 leaf->dflt->realtype->refcount++;
6348 if (err) {
6349 ly_err_print(err);
6350 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6351 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6352 ly_err_free(err);
6353 }
6354 if (dynamic) {
6355 free((void*)dflt);
6356 }
Radek Krejcia1911222019-07-22 17:24:50 +02006357 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006358 if (rc == LY_EINCOMPLETE) {
6359 /* postpone default compilation when the tree is complete */
6360 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6361
6362 /* but in general result is so far ok */
6363 rc = LY_SUCCESS;
6364 }
6365 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006366 } else { /* LYS_LEAFLIST */
6367 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006368 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LYD_XML, lys_get_prefix, llist->dflts_mods[x], &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +02006369 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6370 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6371 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006372 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6373 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006374 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6375 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006376 llist->dflts[x]->realtype->refcount++;
6377 if (err) {
6378 ly_err_print(err);
6379 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6380 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6381 dflt, err->msg);
6382 ly_err_free(err);
6383 }
6384 if (dynamic) {
6385 free((void*)dflt);
6386 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006387 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006388 if (rc == LY_EINCOMPLETE) {
6389 /* postpone default compilation when the tree is complete */
6390 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6391
6392 /* but in general result is so far ok */
6393 rc = LY_SUCCESS;
6394 }
Radek Krejcia1911222019-07-22 17:24:50 +02006395 LY_CHECK_GOTO(rc, cleanup);
6396 }
6397 }
6398 }
6399
Radek Krejci551b12c2019-02-19 16:11:21 +01006400 /* check mandatory - default compatibility */
6401 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6402 && (devs[u]->target->flags & LYS_SET_DFLT)
6403 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6404 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006405 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006406 goto cleanup;
6407 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6408 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6409 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006410 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
Radek Krejci551b12c2019-02-19 16:11:21 +01006411 goto cleanup;
6412 }
6413 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6414 /* mandatory node under a default case */
6415 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006416 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6417 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006418 goto cleanup;
6419 }
Radek Krejci33f72892019-02-21 10:36:58 +01006420
Michal Vasko57c10cd2020-05-27 15:57:11 +02006421 /* add this module into the target module deviated_by */
6422 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup);
6423 *dev_mod = mod_p->mod;
6424
Radek Krejci327de162019-06-14 12:52:07 +02006425 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006426 }
6427
Radek Krejci327de162019-06-14 12:52:07 +02006428 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006429 ret = LY_SUCCESS;
6430
6431cleanup:
6432 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6433 LY_ARRAY_FREE(devs[u]->deviates);
6434 free(devs[u]);
6435 }
6436 free(devs);
6437 ly_set_erase(&targets, NULL);
6438 ly_set_erase(&devs_p, NULL);
6439
6440 return ret;
6441}
6442
Radek Krejcib56c7502019-02-13 14:19:54 +01006443/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006444 * @brief Compile the given YANG submodule into the main module.
6445 * @param[in] ctx Compile context
6446 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006447 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6448 */
6449LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006450lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006451{
6452 unsigned int u;
6453 LY_ERR ret = LY_SUCCESS;
6454 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006455 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006456 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006457 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006458
Radek Krejci0af46292019-01-11 16:02:31 +01006459 if (!mainmod->mod->off_features) {
6460 /* features are compiled directly into the compiled module structure,
6461 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6462 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006463 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6464 LY_CHECK_GOTO(ret, error);
6465 }
Radek Krejci0af46292019-01-11 16:02:31 +01006466
Radek Krejci327de162019-06-14 12:52:07 +02006467 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006468 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006469 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006470
Radek Krejci474f9b82019-07-24 11:36:37 +02006471 /* data nodes */
6472 LY_LIST_FOR(submod->data, node_p) {
6473 ret = lys_compile_node(ctx, node_p, NULL, 0);
6474 LY_CHECK_GOTO(ret, error);
6475 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006476
Radek Krejciec4da802019-05-02 13:02:41 +02006477 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6478 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006479
Radek Krejcid05cbd92018-12-05 14:26:40 +01006480error:
6481 return ret;
6482}
6483
Radek Krejci335332a2019-09-05 13:03:35 +02006484static void *
6485lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6486{
6487 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6488 if (substmts[u].stmt == stmt) {
6489 return substmts[u].storage;
6490 }
6491 }
6492 return NULL;
6493}
6494
6495LY_ERR
6496lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6497{
6498 LY_ERR ret = LY_EVALID, r;
6499 unsigned int u;
6500 struct lysp_stmt *stmt;
6501 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006502
6503 /* check for invalid substatements */
6504 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006505 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6506 continue;
6507 }
Radek Krejci335332a2019-09-05 13:03:35 +02006508 for (u = 0; substmts[u].stmt; ++u) {
6509 if (substmts[u].stmt == stmt->kw) {
6510 break;
6511 }
6512 }
6513 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006514 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6515 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006516 goto cleanup;
6517 }
Radek Krejci335332a2019-09-05 13:03:35 +02006518 }
6519
Radek Krejciad5963b2019-09-06 16:03:05 +02006520 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6521
Radek Krejci335332a2019-09-05 13:03:35 +02006522 /* keep order of the processing the same as the order in the defined substmts,
6523 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6524 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006525 int stmt_present = 0;
6526
Radek Krejci335332a2019-09-05 13:03:35 +02006527 for (stmt = ext->child; stmt; stmt = stmt->next) {
6528 if (substmts[u].stmt != stmt->kw) {
6529 continue;
6530 }
6531
Radek Krejciad5963b2019-09-06 16:03:05 +02006532 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006533 if (substmts[u].storage) {
6534 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006535 case LY_STMT_STATUS:
6536 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6537 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6538 break;
6539 case LY_STMT_UNITS: {
6540 const char **units;
6541
6542 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6543 /* single item */
6544 if (*((const char **)substmts[u].storage)) {
6545 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6546 goto cleanup;
6547 }
6548 units = (const char **)substmts[u].storage;
6549 } else {
6550 /* sized array */
6551 const char ***units_array = (const char ***)substmts[u].storage;
6552 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6553 }
6554 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6555 break;
6556 }
Radek Krejci335332a2019-09-05 13:03:35 +02006557 case LY_STMT_TYPE: {
6558 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6559 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6560
6561 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6562 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006563 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006564 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6565 goto cleanup;
6566 }
6567 compiled = substmts[u].storage;
6568 } else {
6569 /* sized array */
6570 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6571 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6572 compiled = (void*)type;
6573 }
6574
Radek Krejciad5963b2019-09-06 16:03:05 +02006575 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006576 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6577 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006578 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6579 lysp_type_free(ctx->ctx, parsed);
6580 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006581 break;
6582 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006583 case LY_STMT_IF_FEATURE: {
6584 struct lysc_iffeature *iff = NULL;
6585
6586 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6587 /* single item */
6588 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6589 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6590 goto cleanup;
6591 }
6592 iff = (struct lysc_iffeature*)substmts[u].storage;
6593 } else {
6594 /* sized array */
6595 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6596 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6597 }
6598 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6599 break;
6600 }
6601 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6602 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006603 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006604 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.",
6605 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006606 goto cleanup;
6607 }
6608 }
Radek Krejci335332a2019-09-05 13:03:35 +02006609 }
Radek Krejci335332a2019-09-05 13:03:35 +02006610
Radek Krejciad5963b2019-09-06 16:03:05 +02006611 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6612 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6613 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6614 goto cleanup;
6615 }
Radek Krejci335332a2019-09-05 13:03:35 +02006616 }
6617
6618 ret = LY_SUCCESS;
6619
6620cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006621 return ret;
6622}
6623
Michal Vasko175012e2019-11-06 15:49:14 +01006624/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006625 * @brief Check when for cyclic dependencies.
6626 * @param[in] set Set with all the referenced nodes.
6627 * @param[in] node Node whose "when" referenced nodes are in @p set.
6628 * @return LY_ERR value
6629 */
6630static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006631lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006632{
6633 struct lyxp_set tmp_set;
6634 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006635 uint32_t i, j;
6636 LY_ARRAY_SIZE_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006637 int idx;
6638 struct lysc_when *when;
6639 LY_ERR ret = LY_SUCCESS;
6640
6641 memset(&tmp_set, 0, sizeof tmp_set);
6642
6643 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006644 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006645 xp_scnode = &set->val.scnodes[i];
6646
Michal Vasko5c4e5892019-11-14 12:31:38 +01006647 if (xp_scnode->in_ctx != -1) {
6648 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006649 xp_scnode->in_ctx = 1;
6650 }
6651 }
6652
6653 for (i = 0; i < set->used; ++i) {
6654 xp_scnode = &set->val.scnodes[i];
6655 if (xp_scnode->in_ctx != 1) {
6656 /* already checked */
6657 continue;
6658 }
6659
Michal Vasko1bf09392020-03-27 12:38:10 +01006660 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006661 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006662 /* no when to check */
6663 xp_scnode->in_ctx = 0;
6664 continue;
6665 }
6666
6667 node = xp_scnode->scnode;
6668 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006669 LY_ARRAY_FOR(node->when, u) {
6670 when = node->when[u];
Michal Vasko52927e22020-03-16 17:26:14 +01006671 ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006672 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6673 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006674 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006675 goto cleanup;
6676 }
6677
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006678 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006679 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006680 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006681 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006682 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 +01006683 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006684 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 +01006685 ret = LY_EVALID;
6686 goto cleanup;
6687 }
6688
6689 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006690 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006691 } else {
6692 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006693 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006694 }
6695 }
6696
6697 /* merge this set into the global when set */
6698 lyxp_set_scnode_merge(set, &tmp_set);
6699 }
6700
6701 /* check when of non-data parents as well */
6702 node = node->parent;
6703 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6704
Michal Vasko251f56e2019-11-14 16:06:47 +01006705 /* this node when was checked (xp_scnode could have been reallocd) */
6706 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006707 }
6708
6709cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006710 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006711 return ret;
6712}
6713
6714/**
Michal Vasko175012e2019-11-06 15:49:14 +01006715 * @brief Check when/must expressions of a node on a compiled schema tree.
6716 * @param[in] ctx Compile context.
6717 * @param[in] node Node to check.
6718 * @return LY_ERR value
6719 */
6720static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006721lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006722{
Michal Vasko175012e2019-11-06 15:49:14 +01006723 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006724 uint32_t i;
6725 LY_ARRAY_SIZE_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01006726 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006727 struct lysc_when **when = NULL;
6728 struct lysc_must *musts = NULL;
6729 LY_ERR ret = LY_SUCCESS;
6730
6731 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006732 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko175012e2019-11-06 15:49:14 +01006733
6734 switch (node->nodetype) {
6735 case LYS_CONTAINER:
6736 when = ((struct lysc_node_container *)node)->when;
6737 musts = ((struct lysc_node_container *)node)->musts;
6738 break;
6739 case LYS_CHOICE:
6740 when = ((struct lysc_node_choice *)node)->when;
6741 break;
6742 case LYS_LEAF:
6743 when = ((struct lysc_node_leaf *)node)->when;
6744 musts = ((struct lysc_node_leaf *)node)->musts;
6745 break;
6746 case LYS_LEAFLIST:
6747 when = ((struct lysc_node_leaflist *)node)->when;
6748 musts = ((struct lysc_node_leaflist *)node)->musts;
6749 break;
6750 case LYS_LIST:
6751 when = ((struct lysc_node_list *)node)->when;
6752 musts = ((struct lysc_node_list *)node)->musts;
6753 break;
6754 case LYS_ANYXML:
6755 case LYS_ANYDATA:
6756 when = ((struct lysc_node_anydata *)node)->when;
6757 musts = ((struct lysc_node_anydata *)node)->musts;
6758 break;
6759 case LYS_CASE:
6760 when = ((struct lysc_node_case *)node)->when;
6761 break;
6762 case LYS_NOTIF:
6763 musts = ((struct lysc_notif *)node)->musts;
6764 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006765 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006766 case LYS_ACTION:
6767 /* first process input musts */
6768 musts = ((struct lysc_action *)node)->input.musts;
6769 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006770 default:
6771 /* nothing to check */
6772 break;
6773 }
6774
Michal Vasko175012e2019-11-06 15:49:14 +01006775 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006776 LY_ARRAY_FOR(when, u) {
Michal Vasko004d3152020-06-11 19:59:22 +02006777 ret = lyxp_atomize(when[u]->cond, LYD_SCHEMA, when[u]->module, when[u]->context ? when[u]->context : node,
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006778 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006779 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006780 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006781 goto cleanup;
6782 }
6783
Michal Vaskodc052f32019-11-07 11:11:38 +01006784 ctx->path[0] = '\0';
6785 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006786 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006787 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006788 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6789 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006790
Michal Vaskoecd62de2019-11-13 12:35:11 +01006791 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006792 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006793 schema->name);
6794 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006795
6796 /* check dummy node accessing */
6797 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006798 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006799 ret = LY_EVALID;
6800 goto cleanup;
6801 }
Michal Vasko175012e2019-11-06 15:49:14 +01006802 }
6803 }
6804
Michal Vaskoecd62de2019-11-13 12:35:11 +01006805 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006806 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006807 LY_CHECK_GOTO(ret, cleanup);
6808
Michal Vaskod3678892020-05-21 10:06:58 +02006809 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006810 }
6811
Michal Vasko5d8756a2019-11-07 15:21:00 +01006812check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006813 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006814 LY_ARRAY_FOR(musts, u) {
6815 ret = lyxp_atomize(musts[u].cond, LYD_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006816 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006817 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006818 goto cleanup;
6819 }
6820
Michal Vaskodc052f32019-11-07 11:11:38 +01006821 ctx->path[0] = '\0';
6822 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006823 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006824 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006825 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006826 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006827 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6828 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006829 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006830 }
6831 }
6832
Michal Vaskod3678892020-05-21 10:06:58 +02006833 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006834 }
6835
Michal Vasko1bf09392020-03-27 12:38:10 +01006836 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006837 /* now check output musts */
6838 input_done = 1;
6839 musts = ((struct lysc_action *)node)->output.musts;
6840 opts = LYXP_SCNODE_OUTPUT;
6841 goto check_musts;
6842 }
6843
Michal Vasko175012e2019-11-06 15:49:14 +01006844cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006845 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006846 return ret;
6847}
6848
Michal Vasko8d544252020-03-02 10:19:52 +01006849static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006850lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6851{
6852 const struct lysc_node *target = NULL;
6853 struct ly_path *p;
6854 struct lysc_type *type;
6855
6856 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6857
6858 /* try to find the target */
6859 LY_CHECK_RET(ly_path_compile(node->module, node, lref->path, LY_PATH_LREF_TRUE, lys_resolve_prefix, lref->path_context,
6860 LYD_SCHEMA, &p));
6861
6862 /* get the target node */
6863 target = p[LY_ARRAY_SIZE(p) - 1].node;
6864 ly_path_free(node->module->ctx, p);
6865
6866 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
6867 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6868 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
6869 lref->path->expr, lys_nodetype2str(target->nodetype));
6870 return LY_EVALID;
6871 }
6872
6873 /* check status */
6874 ctx->path[0] = '\0';
6875 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6876 ctx->path_len = strlen(ctx->path);
6877 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
6878 return LY_EVALID;
6879 }
6880 ctx->path_len = 1;
6881 ctx->path[1] = '\0';
6882
6883 /* check config */
6884 if (lref->require_instance && (node->flags & LYS_CONFIG_W)) {
6885 if (target->flags & LYS_CONFIG_R) {
6886 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
6887 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
6888 return LY_EVALID;
6889 }
6890 }
6891
6892 /* store the target's type and check for circular chain of leafrefs */
6893 lref->realtype = ((struct lysc_node_leaf *)target)->type;
6894 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
6895 if (type == (struct lysc_type *)lref) {
6896 /* circular chain detected */
6897 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6898 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
6899 return LY_EVALID;
6900 }
6901 }
6902
6903 /* check if leafref and its target are under common if-features */
6904 if (lys_compile_leafref_features_validate(node, target)) {
6905 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6906 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
6907 " features applicable to the leafref itself.", lref->path->expr);
6908 return LY_EVALID;
6909 }
6910
6911 return LY_SUCCESS;
6912}
6913
6914static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01006915lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
6916{
6917 struct lysc_ext_instance *ext;
6918 struct lysp_ext_instance *ext_p = NULL;
6919 struct lysp_stmt *stmt;
6920 const struct lys_module *ext_mod;
6921 LY_ERR ret = LY_SUCCESS;
6922
6923 /* create the parsed extension instance manually */
6924 ext_p = calloc(1, sizeof *ext_p);
6925 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
6926 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
6927 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
6928 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
6929 ext_p->insubstmt_index = 0;
6930
6931 stmt = calloc(1, sizeof *ext_p->child);
6932 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
6933 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
6934 stmt->kw = LY_STMT_TYPE;
6935 ext_p->child = stmt;
6936
6937 /* allocate new extension instance */
6938 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
6939
6940 /* manually get extension definition module */
6941 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
6942
6943 /* compile the extension instance */
6944 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
6945
6946cleanup:
6947 lysp_ext_instance_free(ctx->ctx, ext_p);
6948 free(ext_p);
6949 return ret;
6950}
6951
Michal Vasko004d3152020-06-11 19:59:22 +02006952static LY_ERR
6953lys_compile_unres(struct lysc_ctx *ctx)
6954{
6955 struct lysc_node *node;
6956 struct lysc_type *type, *typeiter;
6957 struct lysc_type_leafref *lref;
6958 LY_ARRAY_SIZE_TYPE v;
6959 uint32_t i;
6960
6961 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6962 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6963 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
6964 for (i = 0; i < ctx->leafrefs.count; ++i) {
6965 node = ctx->leafrefs.objs[i];
6966 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6967 type = ((struct lysc_node_leaf *)node)->type;
6968 if (type->basetype == LY_TYPE_LEAFREF) {
6969 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
6970 } else if (type->basetype == LY_TYPE_UNION) {
6971 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
6972 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6973 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
6974 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
6975 }
6976 }
6977 }
6978 }
6979 for (i = 0; i < ctx->leafrefs.count; ++i) {
6980 /* store pointer to the real type */
6981 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
6982 if (type->basetype == LY_TYPE_LEAFREF) {
6983 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6984 typeiter->basetype == LY_TYPE_LEAFREF;
6985 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6986 ((struct lysc_type_leafref*)type)->realtype = typeiter;
6987 } else if (type->basetype == LY_TYPE_UNION) {
6988 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6989 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6990 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6991 typeiter->basetype == LY_TYPE_LEAFREF;
6992 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6993 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
6994 }
6995 }
6996 }
6997 }
6998
6999 /* check xpath */
7000 for (i = 0; i < ctx->xpath.count; ++i) {
7001 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7002 }
7003
7004 /* finish incomplete default values compilation */
7005 for (i = 0; i < ctx->dflts.count; ++i) {
7006 struct ly_err_item *err = NULL;
7007 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
7008 LY_ERR ret;
7009 ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
7010 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7011 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7012 if (err) {
7013 ly_err_print(err);
7014 ctx->path[0] = '\0';
7015 lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7016 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7017 "Invalid default - value does not fit the type (%s).", err->msg);
7018 ly_err_free(err);
7019 }
7020 LY_CHECK_RET(ret);
7021 }
7022
7023 return LY_SUCCESS;
7024}
7025
Radek Krejci19a96102018-11-15 13:38:09 +01007026LY_ERR
7027lys_compile(struct lys_module *mod, int options)
7028{
7029 struct lysc_ctx ctx = {0};
7030 struct lysc_module *mod_c;
7031 struct lysp_module *sp;
7032 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007033 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007034 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007035 struct lys_module *m;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007036 LY_ARRAY_SIZE_TYPE u, v;
7037 uint32_t i;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007038 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007039
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007040 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007041
7042 if (!mod->implemented) {
7043 /* just imported modules are not compiled */
7044 return LY_SUCCESS;
7045 }
7046
Radek Krejci19a96102018-11-15 13:38:09 +01007047 sp = mod->parsed;
7048
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007049 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01007050 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01007051 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007052 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007053 ctx.path_len = 1;
7054 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007055
7056 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007057 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
7058 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007059
Radek Krejciec4da802019-05-02 13:02:41 +02007060 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007061 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007062 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007063 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7064 }
Radek Krejci0935f412019-08-20 16:15:18 +02007065
7066 /* features */
Radek Krejci0af46292019-01-11 16:02:31 +01007067 if (mod->off_features) {
7068 /* there is already precompiled array of features */
7069 mod_c->features = mod->off_features;
7070 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007071 } else {
7072 /* features are compiled directly into the compiled module structure,
7073 * 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 +02007074 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007075 LY_CHECK_GOTO(ret, error);
7076 }
7077 /* finish feature compilation, not only for the main module, but also for the submodules.
7078 * Due to possible forward references, it must be done when all the features (including submodules)
7079 * are present. */
7080 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007081 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007082 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7083 }
Radek Krejci327de162019-06-14 12:52:07 +02007084 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007085 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007086 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007087 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007088 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007089 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7090 }
Radek Krejci327de162019-06-14 12:52:07 +02007091 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007092 }
Radek Krejci327de162019-06-14 12:52:07 +02007093 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007094
Radek Krejci0935f412019-08-20 16:15:18 +02007095 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02007096 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02007097 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007098 if (sp->identities) {
7099 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
7100 }
Radek Krejci327de162019-06-14 12:52:07 +02007101 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007102
Radek Krejci95710c92019-02-11 15:49:55 +01007103 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007104 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007105 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007106 }
Radek Krejci95710c92019-02-11 15:49:55 +01007107
Radek Krejciec4da802019-05-02 13:02:41 +02007108 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7109 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007110
Radek Krejci95710c92019-02-11 15:49:55 +01007111 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007112 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007113 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007114 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007115 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007116
Radek Krejci474f9b82019-07-24 11:36:37 +02007117 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007118 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007119
Radek Krejci0935f412019-08-20 16:15:18 +02007120 /* extension instances TODO cover extension instances from submodules */
7121 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007122
Michal Vasko004d3152020-06-11 19:59:22 +02007123 /* finish compilation for all unresolved items in the context */
7124 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007125
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007126 /* validate non-instantiated groupings from the parsed schema,
7127 * without it we would accept even the schemas with invalid grouping specification */
7128 ctx.options |= LYSC_OPT_GROUPING;
7129 LY_ARRAY_FOR(sp->groupings, u) {
7130 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007131 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007132 }
7133 }
7134 LY_LIST_FOR(sp->data, node_p) {
7135 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7136 LY_ARRAY_FOR(grps, u) {
7137 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007138 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007139 }
7140 }
7141 }
7142
Radek Krejci474f9b82019-07-24 11:36:37 +02007143 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7144 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7145 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7146 }
7147
Michal Vasko8d544252020-03-02 10:19:52 +01007148#if 0
7149 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7150 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7151 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7152 * the anotation definitions available in the internal schema structure. */
7153 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7154 if (lyp_add_ietf_netconf_annotations(mod)) {
7155 lys_free(mod, NULL, 1, 1);
7156 return NULL;
7157 }
7158 }
7159#endif
7160
7161 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
7162 if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
7163 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error);
7164 }
7165
Radek Krejci1c0c3442019-07-23 16:08:47 +02007166 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007167 ly_set_erase(&ctx.xpath, NULL);
7168 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007169 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007170 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007171 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007172
Radek Krejciec4da802019-05-02 13:02:41 +02007173 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01007174 lysp_module_free(mod->parsed);
7175 ((struct lys_module*)mod)->parsed = NULL;
7176 }
7177
Radek Krejciec4da802019-05-02 13:02:41 +02007178 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007179 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007180 for (i = 0; i < ctx.ctx->list.count; ++i) {
7181 m = ctx.ctx->list.objs[i];
Radek Krejci95710c92019-02-11 15:49:55 +01007182 if (m->implemented == 2) {
7183 m->implemented = 1;
7184 }
7185 }
7186 }
7187
Radek Krejci19a96102018-11-15 13:38:09 +01007188 ((struct lys_module*)mod)->compiled = mod_c;
7189 return LY_SUCCESS;
7190
7191error:
Radek Krejci95710c92019-02-11 15:49:55 +01007192 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007193 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007194 ly_set_erase(&ctx.xpath, NULL);
7195 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007196 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007197 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007198 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007199 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007200 mod->compiled = NULL;
7201
7202 /* revert compilation of modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007203 for (i = 0; i < ctx.ctx->list.count; ++i) {
7204 m = ctx.ctx->list.objs[i];
Michal Vasko2947ce12019-12-16 10:37:19 +01007205 if ((m->implemented == 2) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007206 /* revert features list to the precompiled state */
7207 lys_feature_precompile_revert(&ctx, m);
7208 /* mark module as imported-only / not-implemented */
7209 m->implemented = 0;
7210 /* free the compiled version of the module */
7211 lysc_module_free(m->compiled, NULL);
7212 m->compiled = NULL;
7213 }
7214 }
7215
Radek Krejci19a96102018-11-15 13:38:09 +01007216 return ret;
7217}