blob: bb70f31af18fd1b6f491ea542869bef030f2ac3e [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 Krejcif0e1ba52020-05-22 15:14:35 +020030#include "parser.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020031#include "parser_schema.h"
Radek Krejci0935f412019-08-20 16:15:18 +020032#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020034#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020036#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020038#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010039#include "tree_schema_internal.h"
40#include "xpath.h"
41
Michal Vasko5fe75f12020-03-02 13:52:37 +010042static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
43 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
44
Radek Krejci19a96102018-11-15 13:38:09 +010045/**
46 * @brief Duplicate string into dictionary
47 * @param[in] CTX libyang context of the dictionary.
48 * @param[in] ORIG String to duplicate.
49 * @param[out] DUP Where to store the result.
50 */
51#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
52
Radek Krejciec4da802019-05-02 13:02:41 +020053#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010054 if (ARRAY_P) { \
55 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020056 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010057 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
58 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020059 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010060 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
61 } \
62 }
63
Radek Krejciec4da802019-05-02 13:02:41 +020064#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010065 if (ARRAY_P) { \
66 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020067 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010068 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
69 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020070 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010071 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
72 } \
73 }
74
Radek Krejci0935f412019-08-20 16:15:18 +020075#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
76 if (EXTS_P) { \
77 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020078 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 +020079 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010080 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 +020081 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
82 } \
83 }
84
Radek Krejciec4da802019-05-02 13:02:41 +020085#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010086 if (ARRAY_P) { \
87 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020088 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010089 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
90 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020091 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010092 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
93 } \
94 }
95
Radek Krejciec4da802019-05-02 13:02:41 +020096#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010097 if (MEMBER_P) { \
98 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
99 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200100 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100101 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
102 }
103
Radek Krejciec4da802019-05-02 13:02:41 +0200104#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100105 if (MEMBER_P) { \
106 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200107 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100108 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200109 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100110 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
111 }
112
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100113#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100114 if (ARRAY) { \
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200115 for (LY_ARRAY_SIZE_TYPE u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100116 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100117 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
118 return LY_EVALID; \
119 } \
120 } \
121 }
122
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100123#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
124 if (ARRAY) { \
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200125 for (LY_ARRAY_SIZE_TYPE u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100126 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
127 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
128 return LY_EVALID; \
129 } \
130 } \
131 }
132
133struct lysc_ext *
134lysc_ext_dup(struct lysc_ext *orig)
135{
136 ++orig->refcount;
137 return orig;
138}
139
Radek Krejci19a96102018-11-15 13:38:09 +0100140static struct lysc_ext_instance *
141lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
142{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100143 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100144 (void) ctx;
145 (void) orig;
146 return NULL;
147}
148
Radek Krejcib56c7502019-02-13 14:19:54 +0100149/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200150 * @brief Add record into the compile context's list of incomplete default values.
151 * @param[in] ctx Compile context with the incomplete default values list.
152 * @param[in] context_node Context schema node to store in the record.
153 * @param[in] dflt Incomplete default value to store in the record.
154 * @param[in] dflt_mod Module of the default value definition to store in the record.
155 * @return LY_EMEM in case of memory allocation failure.
156 * @return LY_SUCCESS
157 */
158static LY_ERR
159lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
160{
161 struct lysc_incomplete_dflt *r;
162 r = malloc(sizeof *r);
163 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
164 r->context_node = context_node;
165 r->dflt = dflt;
166 r->dflt_mod = dflt_mod;
167 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
168
169 return LY_SUCCESS;
170}
171
172/**
173 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
174 * @param[in] ctx Compile context with the incomplete default values list.
175 * @param[in] dflt Incomplete default values identifying the record to remove.
176 */
177static void
178lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
179{
180 unsigned int u;
181 struct lysc_incomplete_dflt *r;
182
183 for (u = 0; u < ctx->dflts.count; ++u) {
184 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
185 if (r->dflt == dflt) {
186 free(ctx->dflts.objs[u]);
187 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
188 --ctx->dflts.count;
189 return;
190 }
191 }
192}
193
Radek Krejci0e59c312019-08-15 15:34:15 +0200194void
Radek Krejci327de162019-06-14 12:52:07 +0200195lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
196{
197 int len;
198 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
199
200 if (!name) {
201 /* removing last path segment */
202 if (ctx->path[ctx->path_len - 1] == '}') {
203 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
204 if (ctx->path[ctx->path_len] == '=') {
205 ctx->path[ctx->path_len++] = '}';
206 } else {
207 /* not a top-level special tag, remove also preceiding '/' */
208 goto remove_nodelevel;
209 }
210 } else {
211remove_nodelevel:
212 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
213 if (ctx->path_len == 0) {
214 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200215 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200216 }
217 }
218 /* set new terminating NULL-byte */
219 ctx->path[ctx->path_len] = '\0';
220 } else {
221 if (ctx->path_len > 1) {
222 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
223 /* extension of the special tag */
224 nextlevel = 2;
225 --ctx->path_len;
226 } else {
227 /* there is already some path, so add next level */
228 nextlevel = 1;
229 }
230 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
231
232 if (nextlevel != 2) {
233 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
234 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200235 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200236 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200237 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 +0200238 }
239 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200240 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200241 }
Radek Krejciacc79042019-07-25 14:14:57 +0200242 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
243 /* output truncated */
244 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
245 } else {
246 ctx->path_len += len;
247 }
Radek Krejci327de162019-06-14 12:52:07 +0200248 }
249}
250
251/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100252 * @brief Duplicate the compiled pattern structure.
253 *
254 * Instead of duplicating memory, the reference counter in the @p orig is increased.
255 *
256 * @param[in] orig The pattern structure to duplicate.
257 * @return The duplicated structure to use.
258 */
Radek Krejci19a96102018-11-15 13:38:09 +0100259static struct lysc_pattern*
260lysc_pattern_dup(struct lysc_pattern *orig)
261{
262 ++orig->refcount;
263 return orig;
264}
265
Radek Krejcib56c7502019-02-13 14:19:54 +0100266/**
267 * @brief Duplicate the array of compiled patterns.
268 *
269 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
270 *
271 * @param[in] ctx Libyang context for logging.
272 * @param[in] orig The patterns sized array to duplicate.
273 * @return New sized array as a copy of @p orig.
274 * @return NULL in case of memory allocation error.
275 */
Radek Krejci19a96102018-11-15 13:38:09 +0100276static struct lysc_pattern**
277lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
278{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100279 struct lysc_pattern **dup = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200280 LY_ARRAY_SIZE_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100281
Radek Krejcib56c7502019-02-13 14:19:54 +0100282 assert(orig);
283
Radek Krejci19a96102018-11-15 13:38:09 +0100284 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
285 LY_ARRAY_FOR(orig, u) {
286 dup[u] = lysc_pattern_dup(orig[u]);
287 LY_ARRAY_INCREMENT(dup);
288 }
289 return dup;
290}
291
Radek Krejcib56c7502019-02-13 14:19:54 +0100292/**
293 * @brief Duplicate compiled range structure.
294 *
295 * @param[in] ctx Libyang context for logging.
296 * @param[in] orig The range structure to be duplicated.
297 * @return New compiled range structure as a copy of @p orig.
298 * @return NULL in case of memory allocation error.
299 */
Radek Krejci19a96102018-11-15 13:38:09 +0100300struct lysc_range*
301lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
302{
303 struct lysc_range *dup;
304 LY_ERR ret;
305
Radek Krejcib56c7502019-02-13 14:19:54 +0100306 assert(orig);
307
Radek Krejci19a96102018-11-15 13:38:09 +0100308 dup = calloc(1, sizeof *dup);
309 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
310 if (orig->parts) {
311 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
312 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
313 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
314 }
315 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
316 DUP_STRING(ctx, orig->emsg, dup->emsg);
317 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
318
319 return dup;
320cleanup:
321 free(dup);
322 (void) ret; /* set but not used due to the return type */
323 return NULL;
324}
325
Radek Krejcib56c7502019-02-13 14:19:54 +0100326/**
327 * @brief Stack for processing if-feature expressions.
328 */
Radek Krejci19a96102018-11-15 13:38:09 +0100329struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100330 int size; /**< number of items in the stack */
331 int index; /**< first empty item */
332 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100333};
334
Radek Krejcib56c7502019-02-13 14:19:54 +0100335/**
336 * @brief Add @ref ifftokens into the stack.
337 * @param[in] stack The if-feature stack to use.
338 * @param[in] value One of the @ref ifftokens to store in the stack.
339 * @return LY_EMEM in case of memory allocation error
340 * @return LY_ESUCCESS if the value successfully stored.
341 */
Radek Krejci19a96102018-11-15 13:38:09 +0100342static LY_ERR
343iff_stack_push(struct iff_stack *stack, uint8_t value)
344{
345 if (stack->index == stack->size) {
346 stack->size += 4;
347 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
348 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
349 }
350 stack->stack[stack->index++] = value;
351 return LY_SUCCESS;
352}
353
Radek Krejcib56c7502019-02-13 14:19:54 +0100354/**
355 * @brief Get (and remove) the last item form the stack.
356 * @param[in] stack The if-feature stack to use.
357 * @return The value from the top of the stack.
358 */
Radek Krejci19a96102018-11-15 13:38:09 +0100359static uint8_t
360iff_stack_pop(struct iff_stack *stack)
361{
Radek Krejcib56c7502019-02-13 14:19:54 +0100362 assert(stack && stack->index);
363
Radek Krejci19a96102018-11-15 13:38:09 +0100364 stack->index--;
365 return stack->stack[stack->index];
366}
367
Radek Krejcib56c7502019-02-13 14:19:54 +0100368/**
369 * @brief Clean up the stack.
370 * @param[in] stack The if-feature stack to use.
371 */
Radek Krejci19a96102018-11-15 13:38:09 +0100372static void
373iff_stack_clean(struct iff_stack *stack)
374{
375 stack->size = 0;
376 free(stack->stack);
377}
378
Radek Krejcib56c7502019-02-13 14:19:54 +0100379/**
380 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
381 * (libyang format of the if-feature expression).
382 * @param[in,out] list The 2bits array to modify.
383 * @param[in] op The operand (@ref ifftokens) to store.
384 * @param[in] pos Position (0-based) where to store the given @p op.
385 */
Radek Krejci19a96102018-11-15 13:38:09 +0100386static void
387iff_setop(uint8_t *list, uint8_t op, int pos)
388{
389 uint8_t *item;
390 uint8_t mask = 3;
391
392 assert(pos >= 0);
393 assert(op <= 3); /* max 2 bits */
394
395 item = &list[pos / 4];
396 mask = mask << 2 * (pos % 4);
397 *item = (*item) & ~mask;
398 *item = (*item) | (op << 2 * (pos % 4));
399}
400
Radek Krejcib56c7502019-02-13 14:19:54 +0100401#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
402#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100403
Radek Krejci0af46292019-01-11 16:02:31 +0100404/**
405 * @brief Find a feature of the given name and referenced in the given module.
406 *
407 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
408 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
409 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
410 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
411 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
412 * assigned till that time will be still valid.
413 *
414 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
415 * @param[in] name Name of the feature including possible prefix.
416 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
417 * @return Pointer to the feature structure if found, NULL otherwise.
418 */
Radek Krejci19a96102018-11-15 13:38:09 +0100419static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100420lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100421{
422 size_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200423 LY_ARRAY_SIZE_TYPE u;
Radek Krejci0af46292019-01-11 16:02:31 +0100424 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100425
426 for (i = 0; i < len; ++i) {
427 if (name[i] == ':') {
428 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100429 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100430 LY_CHECK_RET(!mod, NULL);
431
432 name = &name[i + 1];
433 len = len - i - 1;
434 }
435 }
436
437 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100438 if (mod->implemented) {
439 /* module is implemented so there is already the compiled schema */
440 flist = mod->compiled->features;
441 } else {
442 flist = mod->off_features;
443 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200444 LY_ARRAY_FOR(flist, u) {
445 f = &flist[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200446 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100447 return f;
448 }
449 }
450
451 return NULL;
452}
453
Michal Vasko8d544252020-03-02 10:19:52 +0100454/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100455 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
456 */
457static LY_ERR
458lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
459{
460 LY_ERR ret = LY_SUCCESS;
461
462 if (!ext_p->compiled) {
463 lysc_update_path(ctx, NULL, "{extension}");
464 lysc_update_path(ctx, NULL, ext_p->name);
465
466 /* compile the extension definition */
467 ext_p->compiled = calloc(1, sizeof **ext);
468 ext_p->compiled->refcount = 1;
469 DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name);
470 DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument);
471 ext_p->compiled->module = (struct lys_module *)ext_mod;
472 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
473
474 lysc_update_path(ctx, NULL, NULL);
475 lysc_update_path(ctx, NULL, NULL);
476
477 /* find extension definition plugin */
478 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
479 }
480
481 *ext = lysc_ext_dup(ext_p->compiled);
482
483done:
484 return ret;
485}
486
487/**
Michal Vasko8d544252020-03-02 10:19:52 +0100488 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
489 *
490 * @param[in] ctx Compilation context.
491 * @param[in] ext_p Parsed extension instance.
492 * @param[in,out] ext Prepared compiled extension instance.
493 * @param[in] parent Extension instance parent.
494 * @param[in] parent_type Extension instance parent type.
495 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
496 */
Radek Krejci19a96102018-11-15 13:38:09 +0100497static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100498lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
499 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100500{
Radek Krejci7c960162019-09-18 14:16:12 +0200501 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100502 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200503 size_t u;
504 LY_ARRAY_SIZE_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200505 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100506
507 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
508 ext->insubstmt = ext_p->insubstmt;
509 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200510 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200511 ext->parent = parent;
512 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100513
Radek Krejcif56e2a42019-09-09 14:15:25 +0200514 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200515
Radek Krejci19a96102018-11-15 13:38:09 +0100516 /* get module where the extension definition should be placed */
Radek Krejci7c960162019-09-18 14:16:12 +0200517 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u);
518 if (ext_p->yin) {
519 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
520 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
521 * YANG (import) prefix must be done here. */
522 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
523 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
524 u = 0;
525 } else if (ctx->mod_def->compiled) {
Radek Krejci7c960162019-09-18 14:16:12 +0200526 LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) {
527 if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) {
528 char *s;
529 asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]);
530 prefixed_name = lydict_insert_zc(ctx->ctx, s);
531 u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */
532 break;
533 }
534 }
535 }
536 if (!prefixed_name) {
537 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
538 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
539 goto cleanup;
540 }
541 } else {
542 prefixed_name = ext_p->name;
543 }
544 lysc_update_path(ctx, NULL, prefixed_name);
545
Michal Vasko8d544252020-03-02 10:19:52 +0100546 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200547 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100548 if (!ext_mod) {
549 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
550 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
551 goto cleanup;
552 } else if (!ext_mod->parsed->extensions) {
553 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
554 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
555 prefixed_name, ext_mod->name);
556 goto cleanup;
557 }
Radek Krejci7c960162019-09-18 14:16:12 +0200558 }
559 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200560
Michal Vasko5fe75f12020-03-02 13:52:37 +0100561 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200562 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
563 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100564 /* compile extension definition and assign it */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200565 LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def));
Radek Krejci19a96102018-11-15 13:38:09 +0100566 break;
567 }
568 }
Radek Krejci7c960162019-09-18 14:16:12 +0200569 if (!ext->def) {
570 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
571 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
572 goto cleanup;
573 }
Radek Krejci0935f412019-08-20 16:15:18 +0200574
Radek Krejcif56e2a42019-09-09 14:15:25 +0200575 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
576 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
577 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200578 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200579 /* Schema was parsed from YIN and an argument is expected, ... */
580 struct lysp_stmt *stmt = NULL;
581
582 if (ext->def->flags & LYS_YINELEM_TRUE) {
583 /* ... argument was the first XML child element */
584 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
585 /* TODO check namespace of the statement */
586 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
587 stmt = ext_p->child;
588 }
589 }
590 } else {
591 /* ... argument was one of the XML attributes which are represented as child stmt
592 * with LYS_YIN_ATTR flag */
593 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
594 if (!strcmp(stmt->stmt, ext->def->argument)) {
595 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200596 break;
597 }
598 }
599 }
600 if (!stmt) {
601 /* missing extension's argument */
602 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200603 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
604 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200605
606 }
607 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
608 stmt->flags |= LYS_YIN_ARGUMENT;
609 }
Radek Krejci7c960162019-09-18 14:16:12 +0200610 if (prefixed_name != ext_p->name) {
611 lydict_remove(ctx->ctx, ext_p->name);
612 ext_p->name = prefixed_name;
613 if (!ext_p->argument && ext->argument) {
614 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
615 }
616 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200617
Radek Krejci0935f412019-08-20 16:15:18 +0200618 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200619 if (ext->argument) {
620 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
621 }
Radek Krejci7c960162019-09-18 14:16:12 +0200622 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200623 if (ext->argument) {
624 lysc_update_path(ctx, NULL, NULL);
625 }
Radek Krejci0935f412019-08-20 16:15:18 +0200626 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200627 ext_p->compiled = ext;
628
Radek Krejci7c960162019-09-18 14:16:12 +0200629 ret = LY_SUCCESS;
630cleanup:
631 if (prefixed_name && prefixed_name != ext_p->name) {
632 lydict_remove(ctx->ctx, prefixed_name);
633 }
634
Radek Krejcif56e2a42019-09-09 14:15:25 +0200635 lysc_update_path(ctx, NULL, NULL);
636 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200637
Radek Krejci7c960162019-09-18 14:16:12 +0200638 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200639}
640
641/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100642 * @brief Compile information from the if-feature statement
643 * @param[in] ctx Compile context.
644 * @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 +0100645 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
646 * @return LY_ERR value.
647 */
Radek Krejci19a96102018-11-15 13:38:09 +0100648static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200649lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100650{
651 const char *c = *value;
652 int r, rc = EXIT_FAILURE;
653 int i, j, last_not, checkversion = 0;
654 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
655 uint8_t op;
656 struct iff_stack stack = {0, 0, NULL};
657 struct lysc_feature *f;
658
659 assert(c);
660
661 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
662 for (i = j = last_not = 0; c[i]; i++) {
663 if (c[i] == '(') {
664 j++;
665 checkversion = 1;
666 continue;
667 } else if (c[i] == ')') {
668 j--;
669 continue;
670 } else if (isspace(c[i])) {
671 checkversion = 1;
672 continue;
673 }
674
675 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 +0200676 int sp;
677 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
678 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
680 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
681 return LY_EVALID;
682 } else if (!isspace(c[i + r])) {
683 /* feature name starting with the not/and/or */
684 last_not = 0;
685 f_size++;
686 } else if (c[i] == 'n') { /* not operation */
687 if (last_not) {
688 /* double not */
689 expr_size = expr_size - 2;
690 last_not = 0;
691 } else {
692 last_not = 1;
693 }
694 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200695 if (f_exp != f_size) {
696 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
697 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
698 return LY_EVALID;
699 }
Radek Krejci19a96102018-11-15 13:38:09 +0100700 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200701
Radek Krejci19a96102018-11-15 13:38:09 +0100702 /* not a not operation */
703 last_not = 0;
704 }
705 i += r;
706 } else {
707 f_size++;
708 last_not = 0;
709 }
710 expr_size++;
711
712 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200713 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100714 i--;
715 break;
716 }
717 i++;
718 }
719 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200720 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100721 /* not matching count of ( and ) */
722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
723 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
724 return LY_EVALID;
725 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200726 if (f_exp != f_size) {
727 /* features do not match the needed arguments for the logical operations */
728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
729 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
730 "the required number of operands for the operations.", *value);
731 return LY_EVALID;
732 }
Radek Krejci19a96102018-11-15 13:38:09 +0100733
734 if (checkversion || expr_size > 1) {
735 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100736 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100737 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
738 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
739 return LY_EVALID;
740 }
741 }
742
743 /* allocate the memory */
744 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
745 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
746 stack.stack = malloc(expr_size * sizeof *stack.stack);
747 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
748
749 stack.size = expr_size;
750 f_size--; expr_size--; /* used as indexes from now */
751
752 for (i--; i >= 0; i--) {
753 if (c[i] == ')') {
754 /* push it on stack */
755 iff_stack_push(&stack, LYS_IFF_RP);
756 continue;
757 } else if (c[i] == '(') {
758 /* pop from the stack into result all operators until ) */
759 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
760 iff_setop(iff->expr, op, expr_size--);
761 }
762 continue;
763 } else if (isspace(c[i])) {
764 continue;
765 }
766
767 /* end of operator or operand -> find beginning and get what is it */
768 j = i + 1;
769 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
770 i--;
771 }
772 i++; /* go back by one step */
773
774 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
775 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
776 /* double not */
777 iff_stack_pop(&stack);
778 } else {
779 /* not has the highest priority, so do not pop from the stack
780 * as in case of AND and OR */
781 iff_stack_push(&stack, LYS_IFF_NOT);
782 }
783 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
784 /* as for OR - pop from the stack all operators with the same or higher
785 * priority and store them to the result, then push the AND to the stack */
786 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
787 op = iff_stack_pop(&stack);
788 iff_setop(iff->expr, op, expr_size--);
789 }
790 iff_stack_push(&stack, LYS_IFF_AND);
791 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
792 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
793 op = iff_stack_pop(&stack);
794 iff_setop(iff->expr, op, expr_size--);
795 }
796 iff_stack_push(&stack, LYS_IFF_OR);
797 } else {
798 /* feature name, length is j - i */
799
800 /* add it to the expression */
801 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
802
803 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100804 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
805 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
806 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
807 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100808 iff->features[f_size] = f;
809 LY_ARRAY_INCREMENT(iff->features);
810 f_size--;
811 }
812 }
813 while (stack.index) {
814 op = iff_stack_pop(&stack);
815 iff_setop(iff->expr, op, expr_size--);
816 }
817
818 if (++expr_size || ++f_size) {
819 /* not all expected operators and operands found */
820 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
821 "Invalid value \"%s\" of if-feature - processing error.", *value);
822 rc = LY_EINT;
823 } else {
824 rc = LY_SUCCESS;
825 }
826
827error:
828 /* cleanup */
829 iff_stack_clean(&stack);
830
831 return rc;
832}
833
Radek Krejcib56c7502019-02-13 14:19:54 +0100834/**
Michal Vasko175012e2019-11-06 15:49:14 +0100835 * @brief Get the XPath context node for the given schema node.
836 * @param[in] start The schema node where the XPath expression appears.
837 * @return The context node to evaluate XPath expression in given schema node.
838 * @return NULL in case the context node is the root node.
839 */
840static struct lysc_node *
841lysc_xpath_context(struct lysc_node *start)
842{
Michal Vasko1bf09392020-03-27 12:38:10 +0100843 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 +0100844 start = start->parent);
845 return start;
846}
847
848/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100849 * @brief Compile information from the when statement
850 * @param[in] ctx Compile context.
851 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100852 * @param[in] flags Flags of the node with the "when" defiition.
853 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100854 * @param[out] when Pointer where to store pointer to the created compiled when structure.
855 * @return LY_ERR value.
856 */
Radek Krejci19a96102018-11-15 13:38:09 +0100857static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100858lys_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 +0100859{
Radek Krejci19a96102018-11-15 13:38:09 +0100860 LY_ERR ret = LY_SUCCESS;
861
Radek Krejci00b874b2019-02-12 10:54:50 +0100862 *when = calloc(1, sizeof **when);
863 (*when)->refcount = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200864 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200865 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100866 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100867 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
868 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
869 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200870 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100871 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100872
873done:
874 return ret;
875}
876
Radek Krejcib56c7502019-02-13 14:19:54 +0100877/**
878 * @brief Compile information from the must statement
879 * @param[in] ctx Compile context.
880 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100881 * @param[in,out] must Prepared (empty) compiled must structure to fill.
882 * @return LY_ERR value.
883 */
Radek Krejci19a96102018-11-15 13:38:09 +0100884static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200885lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100886{
Radek Krejci19a96102018-11-15 13:38:09 +0100887 LY_ERR ret = LY_SUCCESS;
888
Michal Vasko004d3152020-06-11 19:59:22 +0200889 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100890 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200891 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100892 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
893 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100894 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
895 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200896 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100897
898done:
899 return ret;
900}
901
Radek Krejcib56c7502019-02-13 14:19:54 +0100902/**
903 * @brief Compile information from the import statement
904 * @param[in] ctx Compile context.
905 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100906 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
907 * @return LY_ERR value.
908 */
Radek Krejci19a96102018-11-15 13:38:09 +0100909static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200910lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100911{
Radek Krejci19a96102018-11-15 13:38:09 +0100912 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100913 LY_ERR ret = LY_SUCCESS;
914
915 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200916 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100917 imp->module = imp_p->module;
918
Radek Krejci7f2a5362018-11-28 13:05:37 +0100919 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
920 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100921 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100922 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100923 /* try to use filepath if present */
924 if (imp->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200925 struct ly_in *in;
926 if (ly_in_new_filepath(imp->module->filepath, 0, &in)) {
927 LOGINT(ctx->ctx);
928 } else {
929 mod = lys_parse(ctx->ctx, in, !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
930 if (mod != imp->module) {
931 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", imp->module->filepath, imp->module->name);
932 mod = NULL;
933 }
Radek Krejci19a96102018-11-15 13:38:09 +0100934 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200935 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +0100936 }
937 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100938 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100939 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 +0100940 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100941 return LY_ENOTFOUND;
942 }
943 }
Radek Krejci19a96102018-11-15 13:38:09 +0100944 }
945
946done:
947 return ret;
948}
949
Radek Krejcib56c7502019-02-13 14:19:54 +0100950/**
951 * @brief Compile information from the identity statement
952 *
953 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
954 *
955 * @param[in] ctx Compile context.
956 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100957 * @param[in] idents List of so far compiled identities to check the name uniqueness.
958 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
959 * @return LY_ERR value.
960 */
Radek Krejci19a96102018-11-15 13:38:09 +0100961static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200962lys_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 +0100963{
964 unsigned int u;
965 LY_ERR ret = LY_SUCCESS;
966
Radek Krejci327de162019-06-14 12:52:07 +0200967 lysc_update_path(ctx, NULL, ident_p->name);
968
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100969 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100970 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200971 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
972 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200973 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200974 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100975 /* 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 +0200976 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100977 ident->flags = ident_p->flags;
978
Radek Krejci327de162019-06-14 12:52:07 +0200979 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100980done:
981 return ret;
982}
983
Radek Krejcib56c7502019-02-13 14:19:54 +0100984/**
985 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
986 *
987 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
988 *
989 * @param[in] ctx Compile context for logging.
990 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
991 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
992 * @return LY_SUCCESS if everything is ok.
993 * @return LY_EVALID if the identity is derived from itself.
994 */
Radek Krejci38222632019-02-12 16:55:05 +0100995static LY_ERR
996lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
997{
998 LY_ERR ret = LY_EVALID;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200999 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001000 struct ly_set recursion = {0};
1001 struct lysc_ident *drv;
1002
1003 if (!derived) {
1004 return LY_SUCCESS;
1005 }
1006
1007 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
1008 if (ident == derived[u]) {
1009 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1010 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1011 goto cleanup;
1012 }
1013 ly_set_add(&recursion, derived[u], 0);
1014 }
1015
1016 for (v = 0; v < recursion.count; ++v) {
1017 drv = recursion.objs[v];
1018 if (!drv->derived) {
1019 continue;
1020 }
1021 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
1022 if (ident == drv->derived[u]) {
1023 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1024 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1025 goto cleanup;
1026 }
1027 ly_set_add(&recursion, drv->derived[u], 0);
1028 }
1029 }
1030 ret = LY_SUCCESS;
1031
1032cleanup:
1033 ly_set_erase(&recursion, NULL);
1034 return ret;
1035}
1036
Radek Krejcia3045382018-11-22 14:30:31 +01001037/**
1038 * @brief Find and process the referenced base identities from another identity or identityref
1039 *
Radek Krejciaca74032019-06-04 08:53:06 +02001040 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001041 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1042 * to distinguish these two use cases.
1043 *
1044 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1045 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
1046 * @param[in] ident Referencing identity to work with.
1047 * @param[in] bases Array of bases of identityref to fill in.
1048 * @return LY_ERR value.
1049 */
Radek Krejci19a96102018-11-15 13:38:09 +01001050static LY_ERR
Radek Krejci0a33b042020-05-27 10:05:06 +02001051lys_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 +01001052{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001053 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001054 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001055 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001056 struct lysc_ident **idref;
1057
1058 assert(ident || bases);
1059
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001060 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001061 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1062 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1063 return LY_EVALID;
1064 }
1065
1066 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
1067 s = strchr(bases_p[u], ':');
1068 if (s) {
1069 /* prefixed identity */
1070 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001071 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001072 } else {
1073 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001074 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001075 }
1076 if (!mod) {
1077 if (ident) {
1078 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1079 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1080 } else {
1081 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1082 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1083 }
1084 return LY_EVALID;
1085 }
1086 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001087 if (mod->compiled && mod->compiled->identities) {
1088 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
1089 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001090 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +01001091 if (ident == &mod->compiled->identities[v]) {
1092 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1093 "Identity \"%s\" is derived from itself.", ident->name);
1094 return LY_EVALID;
1095 }
1096 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001097 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +01001098 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001099 *idref = ident;
1100 } else {
1101 /* we have match! store the found identity */
1102 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01001103 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001104 }
1105 break;
1106 }
1107 }
1108 }
1109 if (!idref || !(*idref)) {
1110 if (ident) {
1111 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1112 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1113 } else {
1114 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1115 "Unable to find base (%s) of identityref.", bases_p[u]);
1116 }
1117 return LY_EVALID;
1118 }
1119 }
1120 return LY_SUCCESS;
1121}
1122
Radek Krejcia3045382018-11-22 14:30:31 +01001123/**
1124 * @brief For the given array of identities, set the backlinks from all their base identities.
1125 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1126 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1127 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1128 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1129 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001130static LY_ERR
1131lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1132{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001133 LY_ARRAY_SIZE_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001134
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001135 for (u = 0; u < LY_ARRAY_SIZE(idents_p); ++u) {
1136 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001137 continue;
1138 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001139 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001140 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 +02001141 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001142 }
1143 return LY_SUCCESS;
1144}
1145
Radek Krejci0af46292019-01-11 16:02:31 +01001146LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001147lys_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 +01001148{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001149 LY_ARRAY_SIZE_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001150 struct lysc_ctx context = {0};
1151
Radek Krejci327de162019-06-14 12:52:07 +02001152 assert(ctx_sc || ctx);
1153
1154 if (!ctx_sc) {
1155 context.ctx = ctx;
1156 context.mod = module;
1157 context.path_len = 1;
1158 context.path[0] = '/';
1159 ctx_sc = &context;
1160 }
Radek Krejci0af46292019-01-11 16:02:31 +01001161
1162 if (!features_p) {
1163 return LY_SUCCESS;
1164 }
1165 if (*features) {
1166 offset = LY_ARRAY_SIZE(*features);
1167 }
1168
Radek Krejci327de162019-06-14 12:52:07 +02001169 lysc_update_path(ctx_sc, NULL, "{feature}");
1170 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001171 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001172 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1173
Radek Krejci0af46292019-01-11 16:02:31 +01001174 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001175 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci327de162019-06-14 12:52:07 +02001176 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1177 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1178 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001179 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001180 (*features)[offset + u].module = ctx_sc->mod;
1181
1182 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001183 }
Radek Krejci327de162019-06-14 12:52:07 +02001184 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001185
1186 return LY_SUCCESS;
1187}
1188
Radek Krejcia3045382018-11-22 14:30:31 +01001189/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001190 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001191 *
1192 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1193 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001194 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001195 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1196 * being currently processed).
1197 * @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 +01001198 * @return LY_SUCCESS if everything is ok.
1199 * @return LY_EVALID if the feature references indirectly itself.
1200 */
1201static LY_ERR
1202lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1203{
1204 LY_ERR ret = LY_EVALID;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001205 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001206 struct ly_set recursion = {0};
1207 struct lysc_feature *drv;
1208
1209 if (!depfeatures) {
1210 return LY_SUCCESS;
1211 }
1212
1213 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1214 if (feature == depfeatures[u]) {
1215 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1216 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1217 goto cleanup;
1218 }
1219 ly_set_add(&recursion, depfeatures[u], 0);
1220 }
1221
1222 for (v = 0; v < recursion.count; ++v) {
1223 drv = recursion.objs[v];
1224 if (!drv->depfeatures) {
1225 continue;
1226 }
1227 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1228 if (feature == drv->depfeatures[u]) {
1229 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1230 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1231 goto cleanup;
1232 }
1233 ly_set_add(&recursion, drv->depfeatures[u], 0);
1234 }
1235 }
1236 ret = LY_SUCCESS;
1237
1238cleanup:
1239 ly_set_erase(&recursion, NULL);
1240 return ret;
1241}
1242
1243/**
Radek Krejci0af46292019-01-11 16:02:31 +01001244 * @brief Create pre-compiled features array.
1245 *
1246 * See lys_feature_precompile() for more details.
1247 *
Radek Krejcia3045382018-11-22 14:30:31 +01001248 * @param[in] ctx Compile context.
1249 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001250 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001251 * @return LY_ERR value.
1252 */
Radek Krejci19a96102018-11-15 13:38:09 +01001253static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001254lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001255{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001256 LY_ARRAY_SIZE_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001257 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001258 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001259
Radek Krejci327de162019-06-14 12:52:07 +02001260
Radek Krejci0af46292019-01-11 16:02:31 +01001261 /* find the preprecompiled feature */
1262 LY_ARRAY_FOR(features, x) {
1263 if (strcmp(features[x].name, feature_p->name)) {
1264 continue;
1265 }
1266 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001267 lysc_update_path(ctx, NULL, "{feature}");
1268 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001269
Radek Krejci0af46292019-01-11 16:02:31 +01001270 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001271 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001272 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001273 if (feature->iffeatures) {
1274 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1275 if (feature->iffeatures[u].features) {
1276 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001277 /* check for circular dependency - direct reference first,... */
1278 if (feature == feature->iffeatures[u].features[v]) {
1279 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1280 "Feature \"%s\" is referenced from itself.", feature->name);
1281 return LY_EVALID;
1282 }
1283 /* ... and indirect circular reference */
1284 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1285
Radek Krejci0af46292019-01-11 16:02:31 +01001286 /* add itself into the dependants list */
1287 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1288 *df = feature;
1289 }
Radek Krejci19a96102018-11-15 13:38:09 +01001290 }
Radek Krejci19a96102018-11-15 13:38:09 +01001291 }
1292 }
Radek Krejci327de162019-06-14 12:52:07 +02001293 lysc_update_path(ctx, NULL, NULL);
1294 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001295 done:
1296 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001297 }
Radek Krejci0af46292019-01-11 16:02:31 +01001298
1299 LOGINT(ctx->ctx);
1300 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001301}
1302
Radek Krejcib56c7502019-02-13 14:19:54 +01001303/**
1304 * @brief Revert compiled list of features back to the precompiled state.
1305 *
1306 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1307 * The features are supposed to be stored again as off_features in ::lys_module structure.
1308 *
1309 * @param[in] ctx Compilation context.
1310 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1311 * and supposed to hold the off_features list.
1312 */
Radek Krejci95710c92019-02-11 15:49:55 +01001313static void
1314lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1315{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001316 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001317
1318 /* keep the off_features list until the complete lys_module is freed */
1319 mod->off_features = mod->compiled->features;
1320 mod->compiled->features = NULL;
1321
1322 /* in the off_features list, remove all the parts (from finished compiling process)
1323 * which may points into the data being freed here */
1324 LY_ARRAY_FOR(mod->off_features, u) {
1325 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1326 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1327 }
1328 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1329 mod->off_features[u].iffeatures = NULL;
1330
1331 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1332 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1333 }
1334 LY_ARRAY_FREE(mod->off_features[u].exts);
1335 mod->off_features[u].exts = NULL;
1336 }
1337}
1338
Radek Krejcia3045382018-11-22 14:30:31 +01001339/**
1340 * @brief Validate and normalize numeric value from a range definition.
1341 * @param[in] ctx Compile context.
1342 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1343 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1344 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1345 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1346 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1347 * @param[in] value String value of the range boundary.
1348 * @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.
1349 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1350 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1351 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001352LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001353range_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 +01001354{
Radek Krejci6cba4292018-11-15 17:33:29 +01001355 size_t fraction = 0, size;
1356
Radek Krejci19a96102018-11-15 13:38:09 +01001357 *len = 0;
1358
1359 assert(value);
1360 /* parse value */
1361 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1362 return LY_EVALID;
1363 }
1364
1365 if ((value[*len] == '-') || (value[*len] == '+')) {
1366 ++(*len);
1367 }
1368
1369 while (isdigit(value[*len])) {
1370 ++(*len);
1371 }
1372
1373 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001374 if (basetype == LY_TYPE_DEC64) {
1375 goto decimal;
1376 } else {
1377 *valcopy = strndup(value, *len);
1378 return LY_SUCCESS;
1379 }
Radek Krejci19a96102018-11-15 13:38:09 +01001380 }
1381 fraction = *len;
1382
1383 ++(*len);
1384 while (isdigit(value[*len])) {
1385 ++(*len);
1386 }
1387
Radek Krejci6cba4292018-11-15 17:33:29 +01001388 if (basetype == LY_TYPE_DEC64) {
1389decimal:
1390 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001391 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001392 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1393 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1394 *len, value, frdigits);
1395 return LY_EINVAL;
1396 }
1397 if (fraction) {
1398 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1399 } else {
1400 size = (*len) + frdigits + 1;
1401 }
1402 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001403 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1404
Radek Krejci6cba4292018-11-15 17:33:29 +01001405 (*valcopy)[size - 1] = '\0';
1406 if (fraction) {
1407 memcpy(&(*valcopy)[0], &value[0], fraction);
1408 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1409 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1410 } else {
1411 memcpy(&(*valcopy)[0], &value[0], *len);
1412 memset(&(*valcopy)[*len], '0', frdigits);
1413 }
Radek Krejci19a96102018-11-15 13:38:09 +01001414 }
1415 return LY_SUCCESS;
1416}
1417
Radek Krejcia3045382018-11-22 14:30:31 +01001418/**
1419 * @brief Check that values in range are in ascendant order.
1420 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001421 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1422 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001423 * @param[in] value Current value to check.
1424 * @param[in] prev_value The last seen value.
1425 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1426 */
Radek Krejci19a96102018-11-15 13:38:09 +01001427static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001428range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001429{
1430 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001431 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001432 return LY_EEXIST;
1433 }
1434 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001435 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001436 return LY_EEXIST;
1437 }
1438 }
1439 return LY_SUCCESS;
1440}
1441
Radek Krejcia3045382018-11-22 14:30:31 +01001442/**
1443 * @brief Set min/max value of the range part.
1444 * @param[in] ctx Compile context.
1445 * @param[in] part Range part structure to fill.
1446 * @param[in] max Flag to distinguish if storing min or max value.
1447 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1448 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1449 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1450 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1451 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001452 * @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 +01001453 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1454 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1455 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1456 * frdigits value), LY_EMEM.
1457 */
Radek Krejci19a96102018-11-15 13:38:09 +01001458static LY_ERR
1459range_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 +01001460 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001461{
1462 LY_ERR ret = LY_SUCCESS;
1463 char *valcopy = NULL;
1464 size_t len;
1465
1466 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001467 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001468 LY_CHECK_GOTO(ret, finalize);
1469 }
1470 if (!valcopy && base_range) {
1471 if (max) {
1472 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1473 } else {
1474 part->min_64 = base_range->parts[0].min_64;
1475 }
1476 if (!first) {
1477 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1478 }
1479 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001480 }
1481
1482 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001483 case LY_TYPE_INT8: /* range */
1484 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001485 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 +01001486 } else if (max) {
1487 part->max_64 = INT64_C(127);
1488 } else {
1489 part->min_64 = INT64_C(-128);
1490 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001491 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001492 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001493 }
1494 break;
1495 case LY_TYPE_INT16: /* range */
1496 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001497 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 +01001498 } else if (max) {
1499 part->max_64 = INT64_C(32767);
1500 } else {
1501 part->min_64 = INT64_C(-32768);
1502 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001503 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001504 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001505 }
1506 break;
1507 case LY_TYPE_INT32: /* range */
1508 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001509 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 +01001510 } else if (max) {
1511 part->max_64 = INT64_C(2147483647);
1512 } else {
1513 part->min_64 = INT64_C(-2147483648);
1514 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001515 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001516 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001517 }
1518 break;
1519 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001520 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001521 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001522 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001523 max ? &part->max_64 : &part->min_64);
1524 } else if (max) {
1525 part->max_64 = INT64_C(9223372036854775807);
1526 } else {
1527 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1528 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001529 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001530 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001531 }
1532 break;
1533 case LY_TYPE_UINT8: /* range */
1534 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001535 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 +01001536 } else if (max) {
1537 part->max_u64 = UINT64_C(255);
1538 } else {
1539 part->min_u64 = UINT64_C(0);
1540 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001541 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001542 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001543 }
1544 break;
1545 case LY_TYPE_UINT16: /* range */
1546 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001547 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 +01001548 } else if (max) {
1549 part->max_u64 = UINT64_C(65535);
1550 } else {
1551 part->min_u64 = UINT64_C(0);
1552 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001553 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001554 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001555 }
1556 break;
1557 case LY_TYPE_UINT32: /* range */
1558 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001559 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 +01001560 } else if (max) {
1561 part->max_u64 = UINT64_C(4294967295);
1562 } else {
1563 part->min_u64 = UINT64_C(0);
1564 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001565 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001566 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001567 }
1568 break;
1569 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001570 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001571 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001572 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001573 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 +01001574 } else if (max) {
1575 part->max_u64 = UINT64_C(18446744073709551615);
1576 } else {
1577 part->min_u64 = UINT64_C(0);
1578 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001579 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001580 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001581 }
1582 break;
1583 default:
1584 LOGINT(ctx->ctx);
1585 ret = LY_EINT;
1586 }
1587
Radek Krejci5969f272018-11-23 10:03:58 +01001588finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001589 if (ret == LY_EDENIED) {
1590 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1591 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1592 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1593 } else if (ret == LY_EVALID) {
1594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1595 "Invalid %s restriction - invalid value \"%s\".",
1596 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1597 } else if (ret == LY_EEXIST) {
1598 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1599 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001600 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001601 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001602 } else if (!ret && value) {
1603 *value = *value + len;
1604 }
1605 free(valcopy);
1606 return ret;
1607}
1608
Radek Krejcia3045382018-11-22 14:30:31 +01001609/**
1610 * @brief Compile the parsed range restriction.
1611 * @param[in] ctx Compile context.
1612 * @param[in] range_p Parsed range structure to compile.
1613 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1614 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1615 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1616 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1617 * range restriction must be more restrictive than the base_range.
1618 * @param[in,out] range Pointer to the created current range structure.
1619 * @return LY_ERR value.
1620 */
Radek Krejci19a96102018-11-15 13:38:09 +01001621static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001622lys_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 +01001623 struct lysc_range *base_range, struct lysc_range **range)
1624{
1625 LY_ERR ret = LY_EVALID;
1626 const char *expr;
1627 struct lysc_range_part *parts = NULL, *part;
1628 int range_expected = 0, uns;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001629 LY_ARRAY_SIZE_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001630
1631 assert(range);
1632 assert(range_p);
1633
1634 expr = range_p->arg;
1635 while(1) {
1636 if (isspace(*expr)) {
1637 ++expr;
1638 } else if (*expr == '\0') {
1639 if (range_expected) {
1640 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1641 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1642 length_restr ? "length" : "range", range_p->arg);
1643 goto cleanup;
1644 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1645 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1646 "Invalid %s restriction - unexpected end of the expression (%s).",
1647 length_restr ? "length" : "range", range_p->arg);
1648 goto cleanup;
1649 }
1650 parts_done++;
1651 break;
1652 } else if (!strncmp(expr, "min", 3)) {
1653 if (parts) {
1654 /* min cannot be used elsewhere than in the first part */
1655 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1656 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1657 expr - range_p->arg, range_p->arg);
1658 goto cleanup;
1659 }
1660 expr += 3;
1661
1662 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001663 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 +01001664 part->max_64 = part->min_64;
1665 } else if (*expr == '|') {
1666 if (!parts || range_expected) {
1667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1668 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1669 goto cleanup;
1670 }
1671 expr++;
1672 parts_done++;
1673 /* process next part of the expression */
1674 } else if (!strncmp(expr, "..", 2)) {
1675 expr += 2;
1676 while (isspace(*expr)) {
1677 expr++;
1678 }
1679 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1681 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1682 goto cleanup;
1683 }
1684 /* continue expecting the upper boundary */
1685 range_expected = 1;
1686 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1687 /* number */
1688 if (range_expected) {
1689 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001690 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 +01001691 range_expected = 0;
1692 } else {
1693 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1694 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 +01001695 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001696 part->max_64 = part->min_64;
1697 }
1698
1699 /* continue with possible another expression part */
1700 } else if (!strncmp(expr, "max", 3)) {
1701 expr += 3;
1702 while (isspace(*expr)) {
1703 expr++;
1704 }
1705 if (*expr != '\0') {
1706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1707 length_restr ? "length" : "range", expr);
1708 goto cleanup;
1709 }
1710 if (range_expected) {
1711 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001712 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 +01001713 range_expected = 0;
1714 } else {
1715 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1716 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 +01001717 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001718 part->min_64 = part->max_64;
1719 }
1720 } else {
1721 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1722 length_restr ? "length" : "range", expr);
1723 goto cleanup;
1724 }
1725 }
1726
1727 /* check with the previous range/length restriction */
1728 if (base_range) {
1729 switch (basetype) {
1730 case LY_TYPE_BINARY:
1731 case LY_TYPE_UINT8:
1732 case LY_TYPE_UINT16:
1733 case LY_TYPE_UINT32:
1734 case LY_TYPE_UINT64:
1735 case LY_TYPE_STRING:
1736 uns = 1;
1737 break;
1738 case LY_TYPE_DEC64:
1739 case LY_TYPE_INT8:
1740 case LY_TYPE_INT16:
1741 case LY_TYPE_INT32:
1742 case LY_TYPE_INT64:
1743 uns = 0;
1744 break;
1745 default:
1746 LOGINT(ctx->ctx);
1747 ret = LY_EINT;
1748 goto cleanup;
1749 }
1750 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1751 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1752 goto baseerror;
1753 }
1754 /* current lower bound is not lower than the base */
1755 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1756 /* base has single value */
1757 if (base_range->parts[v].min_64 == parts[u].min_64) {
1758 /* both lower bounds are the same */
1759 if (parts[u].min_64 != parts[u].max_64) {
1760 /* current continues with a range */
1761 goto baseerror;
1762 } else {
1763 /* equal single values, move both forward */
1764 ++v;
1765 continue;
1766 }
1767 } else {
1768 /* base is single value lower than current range, so the
1769 * value from base range is removed in the current,
1770 * move only base and repeat checking */
1771 ++v;
1772 --u;
1773 continue;
1774 }
1775 } else {
1776 /* base is the range */
1777 if (parts[u].min_64 == parts[u].max_64) {
1778 /* current is a single value */
1779 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1780 /* current is behind the base range, so base range is omitted,
1781 * move the base and keep the current for further check */
1782 ++v;
1783 --u;
1784 } /* else it is within the base range, so move the current, but keep the base */
1785 continue;
1786 } else {
1787 /* both are ranges - check the higher bound, the lower was already checked */
1788 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1789 /* higher bound is higher than the current higher bound */
1790 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1791 /* but the current lower bound is also higher, so the base range is omitted,
1792 * continue with the same current, but move the base */
1793 --u;
1794 ++v;
1795 continue;
1796 }
1797 /* current range starts within the base range but end behind it */
1798 goto baseerror;
1799 } else {
1800 /* current range is smaller than the base,
1801 * move current, but stay with the base */
1802 continue;
1803 }
1804 }
1805 }
1806 }
1807 if (u != parts_done) {
1808baseerror:
1809 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1810 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1811 length_restr ? "length" : "range", range_p->arg);
1812 goto cleanup;
1813 }
1814 }
1815
1816 if (!(*range)) {
1817 *range = calloc(1, sizeof **range);
1818 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1819 }
1820
Radek Krejcic8b31002019-01-08 10:24:45 +01001821 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001822 if (range_p->eapptag) {
1823 lydict_remove(ctx->ctx, (*range)->eapptag);
1824 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1825 }
1826 if (range_p->emsg) {
1827 lydict_remove(ctx->ctx, (*range)->emsg);
1828 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1829 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001830 if (range_p->dsc) {
1831 lydict_remove(ctx->ctx, (*range)->dsc);
1832 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1833 }
1834 if (range_p->ref) {
1835 lydict_remove(ctx->ctx, (*range)->ref);
1836 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1837 }
Radek Krejci19a96102018-11-15 13:38:09 +01001838 /* extensions are taken only from the last range by the caller */
1839
1840 (*range)->parts = parts;
1841 parts = NULL;
1842 ret = LY_SUCCESS;
1843cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001844 LY_ARRAY_FREE(parts);
1845
1846 return ret;
1847}
1848
1849/**
1850 * @brief Checks pattern syntax.
1851 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001852 * @param[in] ctx Context.
1853 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001854 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001855 * @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 +01001856 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001857 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001858LY_ERR
1859lys_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 +01001860{
Michal Vasko40a00082020-05-27 15:20:01 +02001861 int idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001862 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001863 int err_code;
1864 const char *orig_ptr;
1865 PCRE2_SIZE err_offset;
1866 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001867#define URANGE_LEN 19
1868 char *ublock2urange[][2] = {
1869 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1870 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1871 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1872 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1873 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1874 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1875 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1876 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1877 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1878 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1879 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1880 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1881 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1882 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1883 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1884 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1885 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1886 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1887 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1888 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1889 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1890 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1891 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1892 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1893 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1894 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1895 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1896 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1897 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1898 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1899 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1900 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1901 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1902 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1903 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1904 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1905 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1906 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1907 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1908 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1909 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1910 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1911 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1912 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1913 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1914 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1915 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1916 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1917 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1918 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1919 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1920 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1921 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1922 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1923 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1924 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1925 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1926 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1927 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1928 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1929 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1930 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1931 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1932 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1933 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1934 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1935 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1936 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1937 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1938 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1939 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1940 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1941 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1942 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1943 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1944 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1945 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1946 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1947 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1948 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1949 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1950 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1951 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1952 {NULL, NULL}
1953 };
1954
1955 /* adjust the expression to a Perl equivalent
1956 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1957
Michal Vasko40a00082020-05-27 15:20:01 +02001958 /* allocate space for the transformed pattern */
1959 size = strlen(pattern) + 1;
1960 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001961 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001962 perl_regex[0] = '\0';
1963
Michal Vasko40a00082020-05-27 15:20:01 +02001964 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1965 brack = 0;
1966 idx = 0;
1967 orig_ptr = pattern;
1968 while (orig_ptr[0]) {
1969 switch (orig_ptr[0]) {
1970 case '$':
1971 case '^':
1972 if (!brack) {
1973 /* make space for the extra character */
1974 ++size;
1975 perl_regex = ly_realloc(perl_regex, size);
1976 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001977
Michal Vasko40a00082020-05-27 15:20:01 +02001978 /* print escape slash */
1979 perl_regex[idx] = '\\';
1980 ++idx;
1981 }
1982 break;
1983 case '[':
1984 /* must not be escaped */
1985 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
1986 ++brack;
1987 }
1988 break;
1989 case ']':
1990 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
1991 /* pattern was checked and compiled already */
1992 assert(brack);
1993 --brack;
1994 }
1995 break;
1996 default:
1997 break;
Radek Krejci19a96102018-11-15 13:38:09 +01001998 }
Michal Vasko40a00082020-05-27 15:20:01 +02001999
2000 /* copy char */
2001 perl_regex[idx] = orig_ptr[0];
2002
2003 ++idx;
2004 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002005 }
Michal Vasko40a00082020-05-27 15:20:01 +02002006 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002007
2008 /* substitute Unicode Character Blocks with exact Character Ranges */
2009 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2010 start = ptr - perl_regex;
2011
2012 ptr = strchr(ptr, '}');
2013 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002014 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002015 pattern, perl_regex + start + 2, "unterminated character property");
2016 free(perl_regex);
2017 return LY_EVALID;
2018 }
2019 end = (ptr - perl_regex) + 1;
2020
2021 /* need more space */
2022 if (end - start < URANGE_LEN) {
2023 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002025 }
2026
2027 /* find our range */
2028 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2029 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2030 break;
2031 }
2032 }
2033 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002035 pattern, perl_regex + start + 5, "unknown block name");
2036 free(perl_regex);
2037 return LY_EVALID;
2038 }
2039
2040 /* 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 +02002041 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002042 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002043 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002044 }
2045 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002046 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002047 }
2048 }
Michal Vasko40a00082020-05-27 15:20:01 +02002049 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002050 /* skip brackets */
2051 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2052 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2053 } else {
2054 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2055 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2056 }
2057 }
2058
2059 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002060 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2061 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002062 &err_code, &err_offset, NULL);
2063 if (!code_local) {
2064 PCRE2_UCHAR err_msg[256] = {0};
2065 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002066 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002067 free(perl_regex);
2068 return LY_EVALID;
2069 }
2070 free(perl_regex);
2071
Radek Krejci54579462019-04-30 12:47:06 +02002072 if (code) {
2073 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002074 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002075 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002076 }
2077
2078 return LY_SUCCESS;
2079
2080#undef URANGE_LEN
2081}
2082
Radek Krejcia3045382018-11-22 14:30:31 +01002083/**
2084 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2085 * @param[in] ctx Compile context.
2086 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002087 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2088 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2089 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2090 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2091 */
Radek Krejci19a96102018-11-15 13:38:09 +01002092static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002093lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002094 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2095{
2096 struct lysc_pattern **pattern;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002097 LY_ARRAY_SIZE_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002098 LY_ERR ret = LY_SUCCESS;
2099
2100 /* first, copy the patterns from the base type */
2101 if (base_patterns) {
2102 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2103 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2104 }
2105
2106 LY_ARRAY_FOR(patterns_p, u) {
2107 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2108 *pattern = calloc(1, sizeof **pattern);
2109 ++(*pattern)->refcount;
2110
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002112 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002113
2114 if (patterns_p[u].arg[0] == 0x15) {
2115 (*pattern)->inverted = 1;
2116 }
Radek Krejci54579462019-04-30 12:47:06 +02002117 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002118 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2119 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002120 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2121 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002122 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002123 }
2124done:
2125 return ret;
2126}
2127
Radek Krejcia3045382018-11-22 14:30:31 +01002128/**
2129 * @brief map of the possible restrictions combination for the specific built-in type.
2130 */
Radek Krejci19a96102018-11-15 13:38:09 +01002131static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2132 0 /* LY_TYPE_UNKNOWN */,
2133 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002134 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2135 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2136 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2137 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2138 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002139 LYS_SET_BIT /* LY_TYPE_BITS */,
2140 0 /* LY_TYPE_BOOL */,
2141 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2142 0 /* LY_TYPE_EMPTY */,
2143 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2144 LYS_SET_BASE /* LY_TYPE_IDENT */,
2145 LYS_SET_REQINST /* LY_TYPE_INST */,
2146 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002147 LYS_SET_TYPE /* LY_TYPE_UNION */,
2148 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002149 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002150 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002151 LYS_SET_RANGE /* LY_TYPE_INT64 */
2152};
2153
2154/**
2155 * @brief stringification of the YANG built-in data types
2156 */
2157const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2158 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2159 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002160};
2161
Radek Krejcia3045382018-11-22 14:30:31 +01002162/**
2163 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2164 * @param[in] ctx Compile context.
2165 * @param[in] enums_p Array of the parsed enum structures to compile.
2166 * @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 +01002167 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2168 * @param[out] enums Newly created array of the compiled enums information for the current type.
2169 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2170 */
Radek Krejci19a96102018-11-15 13:38:09 +01002171static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002172lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002173 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002174{
2175 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002176 LY_ARRAY_SIZE_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002177 int32_t value = 0;
2178 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002179 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002180
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002181 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002182 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2183 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2184 return LY_EVALID;
2185 }
2186
2187 LY_ARRAY_FOR(enums_p, u) {
2188 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2189 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002190 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2191 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002192 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002193 if (base_enums) {
2194 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2195 LY_ARRAY_FOR(base_enums, v) {
2196 if (!strcmp(e->name, base_enums[v].name)) {
2197 break;
2198 }
2199 }
2200 if (v == LY_ARRAY_SIZE(base_enums)) {
2201 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2202 "Invalid %s - derived type adds new item \"%s\".",
2203 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2204 return LY_EVALID;
2205 }
2206 match = v;
2207 }
2208
2209 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002210 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002211 if (enums_p[u].flags & LYS_SET_VALUE) {
2212 e->value = (int32_t)enums_p[u].value;
2213 if (!u || e->value >= value) {
2214 value = e->value + 1;
2215 }
2216 /* check collision with other values */
2217 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2218 if (e->value == (*enums)[v].value) {
2219 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2220 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2221 e->value, e->name, (*enums)[v].name);
2222 return LY_EVALID;
2223 }
2224 }
2225 } else if (base_enums) {
2226 /* inherit the assigned value */
2227 e->value = base_enums[match].value;
2228 if (!u || e->value >= value) {
2229 value = e->value + 1;
2230 }
2231 } else {
2232 /* assign value automatically */
2233 if (u && value == INT32_MIN) {
2234 /* counter overflow */
2235 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2236 "Invalid enumeration - it is not possible to auto-assign enum value for "
2237 "\"%s\" since the highest value is already 2147483647.", e->name);
2238 return LY_EVALID;
2239 }
2240 e->value = value++;
2241 }
2242 } else { /* LY_TYPE_BITS */
2243 if (enums_p[u].flags & LYS_SET_VALUE) {
2244 e->value = (int32_t)enums_p[u].value;
2245 if (!u || (uint32_t)e->value >= position) {
2246 position = (uint32_t)e->value + 1;
2247 }
2248 /* check collision with other values */
2249 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2250 if (e->value == (*enums)[v].value) {
2251 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2252 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2253 (uint32_t)e->value, e->name, (*enums)[v].name);
2254 return LY_EVALID;
2255 }
2256 }
2257 } else if (base_enums) {
2258 /* inherit the assigned value */
2259 e->value = base_enums[match].value;
2260 if (!u || (uint32_t)e->value >= position) {
2261 position = (uint32_t)e->value + 1;
2262 }
2263 } else {
2264 /* assign value automatically */
2265 if (u && position == 0) {
2266 /* counter overflow */
2267 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2268 "Invalid bits - it is not possible to auto-assign bit position for "
2269 "\"%s\" since the highest value is already 4294967295.", e->name);
2270 return LY_EVALID;
2271 }
2272 e->value = position++;
2273 }
2274 }
2275
2276 if (base_enums) {
2277 /* the assigned values must not change from the derived type */
2278 if (e->value != base_enums[match].value) {
2279 if (basetype == LY_TYPE_ENUM) {
2280 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2281 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2282 e->name, base_enums[match].value, e->value);
2283 } else {
2284 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2285 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2286 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2287 }
2288 return LY_EVALID;
2289 }
2290 }
2291
Radek Krejciec4da802019-05-02 13:02:41 +02002292 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002293 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 +01002294
2295 if (basetype == LY_TYPE_BITS) {
2296 /* keep bits ordered by position */
2297 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2298 if (v != u) {
2299 memcpy(&storage, e, sizeof *e);
2300 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2301 memcpy(&(*enums)[v], &storage, sizeof storage);
2302 }
2303 }
2304 }
2305
2306done:
2307 return ret;
2308}
2309
Radek Krejcia3045382018-11-22 14:30:31 +01002310/**
2311 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2312 *
2313 * path-arg = absolute-path / relative-path
2314 * absolute-path = 1*("/" (node-identifier *path-predicate))
2315 * relative-path = 1*(".." "/") descendant-path
2316 *
2317 * @param[in,out] path Path to parse.
2318 * @param[out] prefix Prefix of the token, NULL if there is not any.
2319 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2320 * @param[out] name Name of the token.
2321 * @param[out] nam_len Length of the name.
2322 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2323 * must not be changed between consecutive calls. -1 if the
2324 * path is absolute.
2325 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2326 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2327 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002328LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002329lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2330 int *parent_times, int *has_predicate)
2331{
2332 int par_times = 0;
2333
2334 assert(path && *path);
2335 assert(parent_times);
2336 assert(prefix);
2337 assert(prefix_len);
2338 assert(name);
2339 assert(name_len);
2340 assert(has_predicate);
2341
2342 *prefix = NULL;
2343 *prefix_len = 0;
2344 *name = NULL;
2345 *name_len = 0;
2346 *has_predicate = 0;
2347
2348 if (!*parent_times) {
2349 if (!strncmp(*path, "..", 2)) {
2350 *path += 2;
2351 ++par_times;
2352 while (!strncmp(*path, "/..", 3)) {
2353 *path += 3;
2354 ++par_times;
2355 }
2356 }
2357 if (par_times) {
2358 *parent_times = par_times;
2359 } else {
2360 *parent_times = -1;
2361 }
2362 }
2363
2364 if (**path != '/') {
2365 return LY_EINVAL;
2366 }
2367 /* skip '/' */
2368 ++(*path);
2369
2370 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002371 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002372
2373 if ((**path == '/' && (*path)[1]) || !**path) {
2374 /* path continues by another token or this is the last token */
2375 return LY_SUCCESS;
2376 } else if ((*path)[0] != '[') {
2377 /* unexpected character */
2378 return LY_EINVAL;
2379 } else {
2380 /* predicate starting with [ */
2381 *has_predicate = 1;
2382 return LY_SUCCESS;
2383 }
2384}
2385
2386/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002387 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2388 *
2389 * The set of features used for target must be a subset of features used for the leafref.
2390 * This is not a perfect, we should compare the truth tables but it could require too much resources
2391 * and RFC 7950 does not require it explicitely, so we simplify that.
2392 *
2393 * @param[in] refnode The leafref node.
2394 * @param[in] target Tha target node of the leafref.
2395 * @return LY_SUCCESS or LY_EVALID;
2396 */
2397static LY_ERR
2398lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2399{
2400 LY_ERR ret = LY_EVALID;
2401 const struct lysc_node *iter;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002402 LY_ARRAY_SIZE_TYPE u, v, count;
Radek Krejci58d171e2018-11-23 13:50:55 +01002403 struct ly_set features = {0};
2404
2405 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002406 if (iter->iffeatures) {
2407 LY_ARRAY_FOR(iter->iffeatures, u) {
2408 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2409 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002410 }
2411 }
2412 }
2413 }
2414
2415 /* we should have, in features set, a superset of features applicable to the target node.
2416 * So when adding features applicable to the target into the features set, we should not be
2417 * able to actually add any new feature, otherwise it is not a subset of features applicable
2418 * to the leafref itself. */
2419 count = features.count;
2420 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002421 if (iter->iffeatures) {
2422 LY_ARRAY_FOR(iter->iffeatures, u) {
2423 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2424 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002425 /* new feature was added (or LY_EMEM) */
2426 goto cleanup;
2427 }
2428 }
2429 }
2430 }
2431 }
2432 ret = LY_SUCCESS;
2433
2434cleanup:
2435 ly_set_erase(&features, NULL);
2436 return ret;
2437}
2438
Michal Vasko004d3152020-06-11 19:59:22 +02002439static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2440 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2441 struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002442
Radek Krejcia3045382018-11-22 14:30:31 +01002443/**
2444 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2445 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002446 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2447 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2448 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2449 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002450 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002451 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002452 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002453 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2454 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2455 * @param[out] type Newly created type structure with the filled information about the type.
2456 * @return LY_ERR value.
2457 */
Radek Krejci19a96102018-11-15 13:38:09 +01002458static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002459lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2460 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2461 struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base,
2462 struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002463{
2464 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002465 struct lysc_type_bin *bin;
2466 struct lysc_type_num *num;
2467 struct lysc_type_str *str;
2468 struct lysc_type_bits *bits;
2469 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002470 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002471 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002472 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002473 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002474
2475 switch (basetype) {
2476 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002477 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002478
2479 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002480 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002481 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2482 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002483 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002484 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 +01002485 }
2486 }
2487
2488 if (tpdfname) {
2489 type_p->compiled = *type;
2490 *type = calloc(1, sizeof(struct lysc_type_bin));
2491 }
2492 break;
2493 case LY_TYPE_BITS:
2494 /* RFC 7950 9.7 - bits */
2495 bits = (struct lysc_type_bits*)(*type);
2496 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002497 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002498 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2499 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002500 }
2501
Radek Krejci555cb5b2018-11-16 14:54:33 +01002502 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002503 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002504 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002505 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002506 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002507 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002508 free(*type);
2509 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002510 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002511 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002512 }
2513
2514 if (tpdfname) {
2515 type_p->compiled = *type;
2516 *type = calloc(1, sizeof(struct lysc_type_bits));
2517 }
2518 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002519 case LY_TYPE_DEC64:
2520 dec = (struct lysc_type_dec*)(*type);
2521
2522 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002523 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002524 if (!type_p->fraction_digits) {
2525 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002526 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002527 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002528 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002529 free(*type);
2530 *type = NULL;
2531 }
2532 return LY_EVALID;
2533 }
2534 } else if (type_p->fraction_digits) {
2535 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002536 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002537 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2538 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002539 tpdfname);
2540 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002541 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2542 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002543 free(*type);
2544 *type = NULL;
2545 }
2546 return LY_EVALID;
2547 }
2548 dec->fraction_digits = type_p->fraction_digits;
2549
2550 /* RFC 7950 9.2.4 - range */
2551 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002552 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2553 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002554 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002555 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 +01002556 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002557 }
2558
2559 if (tpdfname) {
2560 type_p->compiled = *type;
2561 *type = calloc(1, sizeof(struct lysc_type_dec));
2562 }
2563 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002564 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002565 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002566
2567 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002568 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002569 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2570 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002571 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002572 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 +01002573 }
2574 } else if (base && ((struct lysc_type_str*)base)->length) {
2575 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2576 }
2577
2578 /* RFC 7950 9.4.5 - pattern */
2579 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002580 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002581 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002582 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2583 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2584 }
2585
2586 if (tpdfname) {
2587 type_p->compiled = *type;
2588 *type = calloc(1, sizeof(struct lysc_type_str));
2589 }
2590 break;
2591 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002592 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002593
2594 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002595 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002596 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002597 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002598 }
2599
Radek Krejci555cb5b2018-11-16 14:54:33 +01002600 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002601 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002602 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002603 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002604 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002606 free(*type);
2607 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002608 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002609 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002610 }
2611
2612 if (tpdfname) {
2613 type_p->compiled = *type;
2614 *type = calloc(1, sizeof(struct lysc_type_enum));
2615 }
2616 break;
2617 case LY_TYPE_INT8:
2618 case LY_TYPE_UINT8:
2619 case LY_TYPE_INT16:
2620 case LY_TYPE_UINT16:
2621 case LY_TYPE_INT32:
2622 case LY_TYPE_UINT32:
2623 case LY_TYPE_INT64:
2624 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002625 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002626
2627 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002628 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002629 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2630 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002631 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002632 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 +01002633 }
2634 }
2635
2636 if (tpdfname) {
2637 type_p->compiled = *type;
2638 *type = calloc(1, sizeof(struct lysc_type_num));
2639 }
2640 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002641 case LY_TYPE_IDENT:
2642 idref = (struct lysc_type_identityref*)(*type);
2643
2644 /* RFC 7950 9.10.2 - base */
2645 if (type_p->bases) {
2646 if (base) {
2647 /* only the directly derived identityrefs can contain base specification */
2648 if (tpdfname) {
2649 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002650 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002651 tpdfname);
2652 } else {
2653 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002654 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002655 free(*type);
2656 *type = NULL;
2657 }
2658 return LY_EVALID;
2659 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002660 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002661 }
2662
2663 if (!base && !type_p->flags) {
2664 /* type derived from identityref built-in type must contain at least one base */
2665 if (tpdfname) {
2666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2667 } else {
2668 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2669 free(*type);
2670 *type = NULL;
2671 }
2672 return LY_EVALID;
2673 }
2674
2675 if (tpdfname) {
2676 type_p->compiled = *type;
2677 *type = calloc(1, sizeof(struct lysc_type_identityref));
2678 }
2679 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002680 case LY_TYPE_LEAFREF:
Michal Vasko004d3152020-06-11 19:59:22 +02002681 lref = (struct lysc_type_leafref*)*type;
2682
Radek Krejcia3045382018-11-22 14:30:31 +01002683 /* RFC 7950 9.9.3 - require-instance */
2684 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002685 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002686 if (tpdfname) {
2687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2688 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2689 } else {
2690 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2691 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2692 free(*type);
2693 *type = NULL;
2694 }
2695 return LY_EVALID;
2696 }
Michal Vasko004d3152020-06-11 19:59:22 +02002697 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002698 } else if (base) {
2699 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002700 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002701 } else {
2702 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002703 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002704 }
2705 if (type_p->path) {
Michal Vasko004d3152020-06-11 19:59:22 +02002706 lref->path = lyxp_expr_dup(ctx->ctx, type_p->path);
2707 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002708 } else if (base) {
Michal Vasko004d3152020-06-11 19:59:22 +02002709 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path);
2710 lref->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002711 } else if (tpdfname) {
2712 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2713 return LY_EVALID;
2714 } else {
2715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2716 free(*type);
2717 *type = NULL;
2718 return LY_EVALID;
2719 }
2720 if (tpdfname) {
2721 type_p->compiled = *type;
2722 *type = calloc(1, sizeof(struct lysc_type_leafref));
2723 }
2724 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002725 case LY_TYPE_INST:
2726 /* RFC 7950 9.9.3 - require-instance */
2727 if (type_p->flags & LYS_SET_REQINST) {
2728 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2729 } else {
2730 /* default is true */
2731 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2732 }
2733
2734 if (tpdfname) {
2735 type_p->compiled = *type;
2736 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2737 }
2738 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002739 case LY_TYPE_UNION:
2740 un = (struct lysc_type_union*)(*type);
2741
2742 /* RFC 7950 7.4 - type */
2743 if (type_p->types) {
2744 if (base) {
2745 /* only the directly derived union can contain types specification */
2746 if (tpdfname) {
2747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2748 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2749 tpdfname);
2750 } else {
2751 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2752 "Invalid type substatement for the type not directly derived from union built-in type.");
2753 free(*type);
2754 *type = NULL;
2755 }
2756 return LY_EVALID;
2757 }
2758 /* compile the type */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002759 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002760 for (LY_ARRAY_SIZE_TYPE u = 0, additional = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Michal Vasko004d3152020-06-11 19:59:22 +02002761 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name,
2762 &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002763 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2764 /* add space for additional types from the union subtype */
2765 un_aux = (struct lysc_type_union *)un->types[u + additional];
Radek Krejcic4fa0292020-05-14 10:54:49 +02002766 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t*)(type_p->types) - 1)) + additional + LY_ARRAY_SIZE(un_aux->types) - 1,
2767 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002768
2769 /* copy subtypes of the subtype union */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002770 for (LY_ARRAY_SIZE_TYPE v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002771 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2772 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2773 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2774 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 +02002775 lref = (struct lysc_type_leafref *)un->types[u + additional];
2776
2777 lref->basetype = LY_TYPE_LEAFREF;
2778 lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path);
2779 lref->refcount = 1;
2780 lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2781 lref->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002782 /* TODO extensions */
2783
2784 } else {
2785 un->types[u + additional] = un_aux->types[v];
2786 ++un_aux->types[v]->refcount;
2787 }
2788 ++additional;
2789 LY_ARRAY_INCREMENT(un->types);
2790 }
2791 /* compensate u increment in main loop */
2792 --additional;
2793
2794 /* free the replaced union subtype */
2795 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2796 } else {
2797 LY_ARRAY_INCREMENT(un->types);
2798 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002799 }
2800 }
2801
2802 if (!base && !type_p->flags) {
2803 /* type derived from union built-in type must contain at least one type */
2804 if (tpdfname) {
2805 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2806 } else {
2807 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2808 free(*type);
2809 *type = NULL;
2810 }
2811 return LY_EVALID;
2812 }
2813
2814 if (tpdfname) {
2815 type_p->compiled = *type;
2816 *type = calloc(1, sizeof(struct lysc_type_union));
2817 }
2818 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002819 case LY_TYPE_BOOL:
2820 case LY_TYPE_EMPTY:
2821 case LY_TYPE_UNKNOWN: /* just to complete switch */
2822 break;
2823 }
2824 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2825done:
2826 return ret;
2827}
2828
Radek Krejcia3045382018-11-22 14:30:31 +01002829/**
2830 * @brief Compile information about the leaf/leaf-list's type.
2831 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002832 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2833 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2834 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2835 * @param[in] context_name Name of the context node or referencing typedef for logging.
2836 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002837 * @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 +01002838 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002839 * @return LY_ERR value.
2840 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002841static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002842lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags,
2843 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
2844 struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002845{
2846 LY_ERR ret = LY_SUCCESS;
2847 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002848 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002849 struct type_context {
2850 const struct lysp_tpdf *tpdf;
2851 struct lysp_node *node;
2852 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002853 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002854 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002855 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002856 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002857 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002858 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002859
2860 (*type) = NULL;
2861
2862 tctx = calloc(1, sizeof *tctx);
2863 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002864 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002865 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2866 ret == LY_SUCCESS;
2867 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2868 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2869 if (basetype) {
2870 break;
2871 }
2872
2873 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002874 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2875 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002876 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2877
Radek Krejcicdfecd92018-11-26 11:27:32 +01002878 if (units && !*units) {
2879 /* inherit units */
2880 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2881 }
Radek Krejci01342af2019-01-03 15:18:08 +01002882 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002883 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002884 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02002885 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002886 }
Radek Krejci01342af2019-01-03 15:18:08 +01002887 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002888 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2889 break;
2890 }
2891
Radek Krejci19a96102018-11-15 13:38:09 +01002892 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002893 /* it is not necessary to continue, the rest of the chain was already compiled,
2894 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002895 basetype = tctx->tpdf->type.compiled->basetype;
2896 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002897 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002898 dummyloops = 1;
2899 goto preparenext;
2900 } else {
2901 tctx = NULL;
2902 break;
2903 }
Radek Krejci19a96102018-11-15 13:38:09 +01002904 }
2905
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002906 /* circular typedef reference detection */
2907 for (u = 0; u < tpdf_chain.count; u++) {
2908 /* local part */
2909 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2910 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2912 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2913 free(tctx);
2914 ret = LY_EVALID;
2915 goto cleanup;
2916 }
2917 }
2918 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2919 /* global part for unions corner case */
2920 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2921 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2922 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2923 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2924 free(tctx);
2925 ret = LY_EVALID;
2926 goto cleanup;
2927 }
2928 }
2929
Radek Krejci19a96102018-11-15 13:38:09 +01002930 /* store information for the following processing */
2931 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2932
Radek Krejcicdfecd92018-11-26 11:27:32 +01002933preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002934 /* prepare next loop */
2935 tctx_prev = tctx;
2936 tctx = calloc(1, sizeof *tctx);
2937 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2938 }
2939 free(tctx);
2940
2941 /* allocate type according to the basetype */
2942 switch (basetype) {
2943 case LY_TYPE_BINARY:
2944 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002945 break;
2946 case LY_TYPE_BITS:
2947 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002948 break;
2949 case LY_TYPE_BOOL:
2950 case LY_TYPE_EMPTY:
2951 *type = calloc(1, sizeof(struct lysc_type));
2952 break;
2953 case LY_TYPE_DEC64:
2954 *type = calloc(1, sizeof(struct lysc_type_dec));
2955 break;
2956 case LY_TYPE_ENUM:
2957 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002958 break;
2959 case LY_TYPE_IDENT:
2960 *type = calloc(1, sizeof(struct lysc_type_identityref));
2961 break;
2962 case LY_TYPE_INST:
2963 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2964 break;
2965 case LY_TYPE_LEAFREF:
2966 *type = calloc(1, sizeof(struct lysc_type_leafref));
2967 break;
2968 case LY_TYPE_STRING:
2969 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002970 break;
2971 case LY_TYPE_UNION:
2972 *type = calloc(1, sizeof(struct lysc_type_union));
2973 break;
2974 case LY_TYPE_INT8:
2975 case LY_TYPE_UINT8:
2976 case LY_TYPE_INT16:
2977 case LY_TYPE_UINT16:
2978 case LY_TYPE_INT32:
2979 case LY_TYPE_UINT32:
2980 case LY_TYPE_INT64:
2981 case LY_TYPE_UINT64:
2982 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002983 break;
2984 case LY_TYPE_UNKNOWN:
2985 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2986 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2987 ret = LY_EVALID;
2988 goto cleanup;
2989 }
2990 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002991 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002992 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2993 ly_data_type2str[basetype]);
2994 free(*type);
2995 (*type) = NULL;
2996 ret = LY_EVALID;
2997 goto cleanup;
2998 }
2999
3000 /* get restrictions from the referred typedefs */
3001 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3002 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003003
3004 /* remember the typedef context for circular check */
3005 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3006
Radek Krejci43699232018-11-23 14:59:46 +01003007 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003008 base = tctx->tpdf->type.compiled;
3009 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003010 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003011 /* no change, just use the type information from the base */
3012 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3013 ++base->refcount;
3014 continue;
3015 }
3016
3017 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003018 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3019 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3020 tctx->tpdf->name, ly_data_type2str[basetype]);
3021 ret = LY_EVALID;
3022 goto cleanup;
3023 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3024 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3025 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3026 tctx->tpdf->name, tctx->tpdf->dflt);
3027 ret = LY_EVALID;
3028 goto cleanup;
3029 }
3030
Radek Krejci19a96102018-11-15 13:38:09 +01003031 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003032 /* TODO user type plugins */
3033 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003034 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003035 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 +01003036 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003037 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003038 LY_CHECK_GOTO(ret, cleanup);
3039 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003040 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003041 /* remove the processed typedef contexts from the stack for circular check */
3042 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003043
Radek Krejcic5c27e52018-11-15 14:38:11 +01003044 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003045 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003046 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003047 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003048 /* TODO user type plugins */
3049 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003050 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003051 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 +01003052 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003053 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003054 /* no specific restriction in leaf's type definition, copy from the base */
3055 free(*type);
3056 (*type) = base;
3057 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003058 }
Radek Krejcia1911222019-07-22 17:24:50 +02003059 if (dflt && !(*type)->dflt) {
3060 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003061 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003062 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3063 (*type)->dflt->realtype = (*type);
3064 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3065 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003066 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003067 if (err) {
3068 ly_err_print(err);
3069 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3070 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3071 ly_err_free(err);
3072 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003073 if (ret == LY_EINCOMPLETE) {
3074 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003075 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003076
3077 /* but in general result is so far ok */
3078 ret = LY_SUCCESS;
3079 }
Radek Krejcia1911222019-07-22 17:24:50 +02003080 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003081 }
Radek Krejci19a96102018-11-15 13:38:09 +01003082
Radek Krejci0935f412019-08-20 16:15:18 +02003083 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003084
3085cleanup:
3086 ly_set_erase(&tpdf_chain, free);
3087 return ret;
3088}
3089
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003090/**
3091 * @brief Compile status information of the given node.
3092 *
3093 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3094 * has the status correctly set during the compilation.
3095 *
3096 * @param[in] ctx Compile context
3097 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3098 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3099 * the compatibility with the parent's status value.
3100 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3101 * @return LY_ERR value.
3102 */
3103static LY_ERR
3104lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3105{
3106 /* status - it is not inherited by specification, but it does not make sense to have
3107 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3108 if (!((*node_flags) & LYS_STATUS_MASK)) {
3109 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3110 if ((parent_flags & 0x3) != 0x3) {
3111 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3112 * combination of bits (0x3) which marks the uses_status value */
3113 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3114 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3115 }
3116 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3117 } else {
3118 (*node_flags) |= LYS_STATUS_CURR;
3119 }
3120 } else if (parent_flags & LYS_STATUS_MASK) {
3121 /* check status compatibility with the parent */
3122 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3123 if ((*node_flags) & LYS_STATUS_CURR) {
3124 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3125 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3126 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3127 } else { /* LYS_STATUS_DEPRC */
3128 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3129 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3130 }
3131 return LY_EVALID;
3132 }
3133 }
3134 return LY_SUCCESS;
3135}
3136
Radek Krejci8cce8532019-03-05 11:27:45 +01003137/**
3138 * @brief Check uniqness of the node/action/notification name.
3139 *
3140 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3141 * structures, but they share the namespace so we need to check their name collisions.
3142 *
3143 * @param[in] ctx Compile context.
3144 * @param[in] children List (linked list) of data nodes to go through.
3145 * @param[in] actions List (sized array) of actions or RPCs to go through.
3146 * @param[in] notifs List (sized array) of Notifications to go through.
3147 * @param[in] name Name of the item to find in the given lists.
3148 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3149 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3150 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3151 */
3152static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02003153lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions,
3154 const struct lysc_notif *notifs, const char *name, void *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003155{
3156 const struct lysc_node *iter;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003157 LY_ARRAY_SIZE_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003158
3159 LY_LIST_FOR(children, iter) {
3160 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3161 goto error;
3162 }
3163 }
3164 LY_ARRAY_FOR(actions, u) {
3165 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3166 goto error;
3167 }
3168 }
3169 LY_ARRAY_FOR(notifs, u) {
3170 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3171 goto error;
3172 }
3173 }
3174 return LY_SUCCESS;
3175error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003176 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003177 return LY_EEXIST;
3178}
3179
Radek Krejciec4da802019-05-02 13:02:41 +02003180static 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 +01003181
Radek Krejcia3045382018-11-22 14:30:31 +01003182/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003183 * @brief Compile parsed RPC/action schema node information.
3184 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003185 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003186 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003187 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3188 * @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).
3189 * Zero means no uses, non-zero value with no status bit set mean the default status.
3190 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3191 */
3192static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003193lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003194 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3195{
3196 LY_ERR ret = LY_SUCCESS;
3197 struct lysp_node *child_p;
3198 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003199 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003200
Radek Krejci327de162019-06-14 12:52:07 +02003201 lysc_update_path(ctx, parent, action_p->name);
3202
Radek Krejci8cce8532019-03-05 11:27:45 +01003203 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3204 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3205 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3206 action_p->name, action)) {
3207 return LY_EVALID;
3208 }
3209
Radek Krejciec4da802019-05-02 13:02:41 +02003210 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003211 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003212 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003213 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003214 return LY_EVALID;
3215 }
3216
Michal Vasko1bf09392020-03-27 12:38:10 +01003217 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003218 action->module = ctx->mod;
3219 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003220 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003221 action->sp = action_p;
3222 }
3223 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3224
3225 /* status - it is not inherited by specification, but it does not make sense to have
3226 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003227 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003228
3229 DUP_STRING(ctx->ctx, action_p->name, action->name);
3230 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3231 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003232 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003233 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003234
3235 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003236 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003237 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003238 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 +02003239 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003240 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003241 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003242 }
Radek Krejci327de162019-06-14 12:52:07 +02003243 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003244 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003245
3246 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003247 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003248 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003249 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 +02003250 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003251 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003252 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003253 }
Radek Krejci327de162019-06-14 12:52:07 +02003254 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003255 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003256
3257 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3258 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003259 ly_set_add(&ctx->xpath, action, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003260 }
3261
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003262cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003263 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003264 return ret;
3265}
3266
3267/**
Radek Krejci43981a32019-04-12 09:44:11 +02003268 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003269 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003270 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003271 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3272 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3273 * @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 +02003274 * Zero means no uses, non-zero value with no status bit set mean the default status.
3275 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3276 */
3277static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003278lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003279 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3280{
3281 LY_ERR ret = LY_SUCCESS;
3282 struct lysp_node *child_p;
3283 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003284 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003285
Radek Krejci327de162019-06-14 12:52:07 +02003286 lysc_update_path(ctx, parent, notif_p->name);
3287
Radek Krejcifc11bd72019-04-11 16:00:05 +02003288 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3289 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3290 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3291 notif_p->name, notif)) {
3292 return LY_EVALID;
3293 }
3294
Radek Krejciec4da802019-05-02 13:02:41 +02003295 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003296 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3297 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003298 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003299 return LY_EVALID;
3300 }
3301
3302 notif->nodetype = LYS_NOTIF;
3303 notif->module = ctx->mod;
3304 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003305 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003306 notif->sp = notif_p;
3307 }
3308 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3309
3310 /* status - it is not inherited by specification, but it does not make sense to have
3311 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3312 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3313
3314 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3315 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3316 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003317 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003318 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003319 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3320 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003321 ly_set_add(&ctx->xpath, notif, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003322 }
Radek Krejci0935f412019-08-20 16:15:18 +02003323 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003324
Radek Krejciec4da802019-05-02 13:02:41 +02003325 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003326 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003327 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003328 }
3329
Radek Krejci327de162019-06-14 12:52:07 +02003330 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003331cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003332 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003333 return ret;
3334}
3335
3336/**
Radek Krejcia3045382018-11-22 14:30:31 +01003337 * @brief Compile parsed container node information.
3338 * @param[in] ctx Compile context
3339 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003340 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3341 * is enriched with the container-specific information.
3342 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3343 */
Radek Krejci19a96102018-11-15 13:38:09 +01003344static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003345lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003346{
3347 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3348 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3349 struct lysp_node *child_p;
3350 unsigned int u;
3351 LY_ERR ret = LY_SUCCESS;
3352
Radek Krejcife909632019-02-12 15:34:42 +01003353 if (cont_p->presence) {
3354 cont->flags |= LYS_PRESENCE;
3355 }
3356
Radek Krejci19a96102018-11-15 13:38:09 +01003357 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003358 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003359 }
3360
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003361 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003362 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3363 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003364 ly_set_add(&ctx->xpath, cont, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003365 }
Radek Krejciec4da802019-05-02 13:02:41 +02003366 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3367 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003368
3369done:
3370 return ret;
3371}
3372
Radek Krejci33f72892019-02-21 10:36:58 +01003373/*
3374 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3375 * @param[in] ctx Compile context.
3376 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3377 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003378 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3379 * @return LY_ERR value.
3380 */
3381static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003382lys_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 +01003383{
Radek Krejci33f72892019-02-21 10:36:58 +01003384 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3385
Radek Krejciec4da802019-05-02 13:02:41 +02003386 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 +01003387 leaf->units ? NULL : &leaf->units));
3388 if (leaf->nodetype == LYS_LEAFLIST) {
3389 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003390 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3391 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003392 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003393 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3394 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3395 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3396 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003397 LY_ARRAY_INCREMENT(llist->dflts);
3398 }
3399 } else {
3400 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003401 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003402 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3403 leaf->dflt->realtype = leaf->type->dflt->realtype;
3404 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3405 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003406 }
3407 }
3408 if (leaf->type->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 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003412 LY_ARRAY_SIZE_TYPE u;
Radek Krejci33f72892019-02-21 10:36:58 +01003413 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3414 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3415 /* 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 +02003416 ly_set_add(&ctx->leafrefs, leaf, 0);
Radek Krejci33f72892019-02-21 10:36:58 +01003417 }
3418 }
3419 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003420 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3421 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3422 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3423 return LY_EVALID;
3424 }
3425 }
3426
Radek Krejci33f72892019-02-21 10:36:58 +01003427 return LY_SUCCESS;
3428}
3429
Radek Krejcia3045382018-11-22 14:30:31 +01003430/**
3431 * @brief Compile parsed leaf node information.
3432 * @param[in] ctx Compile context
3433 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003434 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3435 * is enriched with the leaf-specific information.
3436 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3437 */
Radek Krejci19a96102018-11-15 13:38:09 +01003438static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003439lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003440{
3441 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3442 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3443 unsigned int u;
3444 LY_ERR ret = LY_SUCCESS;
3445
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003446 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003447 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3448 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003449 ly_set_add(&ctx->xpath, leaf, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003450 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003451 if (leaf_p->units) {
3452 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3453 leaf->flags |= LYS_SET_UNITS;
3454 }
Radek Krejcia1911222019-07-22 17:24:50 +02003455
3456 /* the dflt member is just filled to avoid getting the default value from the type */
3457 leaf->dflt = (void*)leaf_p->dflt;
3458 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003459 if (ret) {
3460 leaf->dflt = NULL;
3461 return ret;
3462 }
Radek Krejcia1911222019-07-22 17:24:50 +02003463
Radek Krejciccd20f12019-02-15 14:12:27 +01003464 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003465 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003466 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003467 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3468 leaf->dflt->realtype = leaf->type;
3469 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3470 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003471 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003472 leaf->dflt->realtype->refcount++;
3473 if (err) {
3474 ly_err_print(err);
3475 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3476 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3477 ly_err_free(err);
3478 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003479 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003480 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003481 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003482
3483 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003484 ret = LY_SUCCESS;
3485 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003486 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003487 leaf->flags |= LYS_SET_DFLT;
3488 }
Radek Krejci43699232018-11-23 14:59:46 +01003489
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003490
Radek Krejci19a96102018-11-15 13:38:09 +01003491done:
3492 return ret;
3493}
3494
Radek Krejcia3045382018-11-22 14:30:31 +01003495/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003496 * @brief Compile parsed leaf-list node information.
3497 * @param[in] ctx Compile context
3498 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003499 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3500 * is enriched with the leaf-list-specific information.
3501 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3502 */
3503static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003504lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003505{
3506 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3507 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003508 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003509 LY_ERR ret = LY_SUCCESS;
3510
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003511 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003512 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3513 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003514 ly_set_add(&ctx->xpath, llist, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003515 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003516 if (llist_p->units) {
3517 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3518 llist->flags |= LYS_SET_UNITS;
3519 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003520
Radek Krejcia1911222019-07-22 17:24:50 +02003521 /* the dflts member is just filled to avoid getting the default value from the type */
3522 llist->dflts = (void*)llist_p->dflts;
3523 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Michal Vasko6a044b22020-01-15 12:25:39 +01003524 if (ret != LY_SUCCESS) {
3525 /* make sure the defaults are freed correctly */
3526 if (llist_p->dflts) {
3527 llist->dflts = NULL;
3528 }
3529 return ret;
3530 }
3531
Radek Krejci0e5d8382018-11-28 16:37:53 +01003532 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003533 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003534 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003535 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3536 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003537 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003538 LY_ARRAY_INCREMENT(llist->dflts_mods);
3539 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003540 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003541 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3542 llist->dflts[u]->realtype = llist->type;
3543 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3544 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003545 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003546 llist->dflts[u]->realtype->refcount++;
3547 if (err) {
3548 ly_err_print(err);
3549 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3550 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3551 ly_err_free(err);
3552 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003553 if (ret == LY_EINCOMPLETE) {
3554 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003555 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003556
3557 /* but in general result is so far ok */
3558 ret = LY_SUCCESS;
3559 }
Radek Krejcia1911222019-07-22 17:24:50 +02003560 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003561 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003562 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003563 }
Radek Krejcia1911222019-07-22 17:24:50 +02003564 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3565 /* configuration data values must be unique - so check the default values */
3566 LY_ARRAY_FOR(llist->dflts, u) {
3567 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3568 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3569 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003570 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 +02003571 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3572 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3573 if (dynamic) {
3574 free((char*)val);
3575 }
3576 return LY_EVALID;
3577 }
3578 }
3579 }
3580 }
3581
3582 /* 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 +01003583
3584 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003585 if (llist->min) {
3586 llist->flags |= LYS_MAND_TRUE;
3587 }
Radek Krejcib7408632018-11-28 17:12:11 +01003588 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003589
Radek Krejci0e5d8382018-11-28 16:37:53 +01003590done:
3591 return ret;
3592}
3593
3594/**
Radek Krejci7af64242019-02-18 13:07:53 +01003595 * @brief Compile information about list's uniques.
3596 * @param[in] ctx Compile context.
3597 * @param[in] context_module Module where the prefixes are going to be resolved.
3598 * @param[in] uniques Sized array list of unique statements.
3599 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3600 * @return LY_ERR value.
3601 */
3602static LY_ERR
3603lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3604{
3605 LY_ERR ret = LY_SUCCESS;
3606 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003607 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003608 const char *keystr, *delim;
3609 size_t len;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003610 LY_ARRAY_SIZE_TYPE v;
Radek Krejci7af64242019-02-18 13:07:53 +01003611 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003612 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003613
3614 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3615 config = -1;
3616 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3617 keystr = uniques[v];
3618 while (keystr) {
3619 delim = strpbrk(keystr, " \t\n");
3620 if (delim) {
3621 len = delim - keystr;
3622 while (isspace(*delim)) {
3623 ++delim;
3624 }
3625 } else {
3626 len = strlen(keystr);
3627 }
3628
3629 /* unique node must be present */
3630 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003631 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3632 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003633 if (ret != LY_SUCCESS) {
3634 if (ret == LY_EDENIED) {
3635 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003636 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003637 len, keystr, lys_nodetype2str((*key)->nodetype));
3638 }
3639 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003640 } else if (flags) {
3641 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3642 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003643 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003644 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003645 }
3646
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003647
Radek Krejci7af64242019-02-18 13:07:53 +01003648 /* all referenced leafs must be of the same config type */
3649 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3650 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01003651 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01003652 return LY_EVALID;
3653 } else if ((*key)->flags & LYS_CONFIG_W) {
3654 config = 1;
3655 } else { /* LYS_CONFIG_R */
3656 config = 0;
3657 }
3658
Michal Vasko14654712020-02-06 08:35:21 +01003659 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3660 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3661 if (parent->nodetype == LYS_LIST) {
3662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3663 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
3664 return LY_EVALID;
3665 }
3666 }
3667
Radek Krejci7af64242019-02-18 13:07:53 +01003668 /* check status */
3669 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3670 (*key)->flags, (*key)->module, (*key)->name));
3671
3672 /* mark leaf as unique */
3673 (*key)->flags |= LYS_UNIQUE;
3674
3675 /* next unique value in line */
3676 keystr = delim;
3677 }
3678 /* next unique definition */
3679 }
3680
3681 return LY_SUCCESS;
3682}
3683
3684/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003685 * @brief Compile parsed list node information.
3686 * @param[in] ctx Compile context
3687 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003688 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3689 * is enriched with the list-specific information.
3690 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3691 */
3692static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003693lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003694{
3695 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3696 struct lysc_node_list *list = (struct lysc_node_list*)node;
3697 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003698 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003699 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003700 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003701 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003702 LY_ERR ret = LY_SUCCESS;
3703
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003704 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003705 if (list->min) {
3706 list->flags |= LYS_MAND_TRUE;
3707 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003708 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3709
3710 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003711 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003712 }
3713
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003714 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003715 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3716 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02003717 ly_set_add(&ctx->xpath, list, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003718 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003719
3720 /* keys */
3721 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3723 return LY_EVALID;
3724 }
3725
3726 /* find all the keys (must be direct children) */
3727 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003728 if (!keystr) {
3729 /* keyless list */
3730 list->flags |= LYS_KEYLESS;
3731 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003732 while (keystr) {
3733 delim = strpbrk(keystr, " \t\n");
3734 if (delim) {
3735 len = delim - keystr;
3736 while (isspace(*delim)) {
3737 ++delim;
3738 }
3739 } else {
3740 len = strlen(keystr);
3741 }
3742
3743 /* key node must be present */
Michal Vaskoe444f752020-02-10 12:20:06 +01003744 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 +02003745 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003746 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3747 "The list's key \"%.*s\" not found.", len, keystr);
3748 return LY_EVALID;
3749 }
3750 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003751 if (key->flags & LYS_KEY) {
3752 /* the node was already marked as a key */
3753 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3754 "Duplicated key identifier \"%.*s\".", len, keystr);
3755 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003756 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003757
3758 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003759 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003760 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003761 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3762 return LY_EVALID;
3763 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003764 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003765 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003766 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003767 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003768 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003769 return LY_EVALID;
3770 }
3771 } else {
3772 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003773 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003774 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003775 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003776 return LY_EVALID;
3777 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003778 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003780 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003781 return LY_EVALID;
3782 }
3783 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003784
3785 /* check status */
3786 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003787 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003788
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003789 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003790 if (key->dflt) {
3791 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3792 lysc_type_free(ctx->ctx, key->dflt->realtype);
3793 free(key->dflt);
3794 key->dflt = NULL;
3795 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003796 }
3797 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003798 key->flags |= LYS_KEY;
3799
3800 /* move it to the correct position */
3801 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3802 /* fix links in closest previous siblings of the key */
3803 if (key->next) {
3804 key->next->prev = key->prev;
3805 } else {
3806 /* last child */
3807 list->child->prev = key->prev;
3808 }
3809 if (key->prev->next) {
3810 key->prev->next = key->next;
3811 }
3812 /* fix links in the key */
3813 if (prev_key) {
3814 key->prev = (struct lysc_node*)prev_key;
3815 key->next = prev_key->next;
3816 } else {
3817 key->prev = list->child->prev;
3818 key->next = list->child;
3819 }
3820 /* fix links in closes future siblings of the key */
3821 if (prev_key) {
3822 if (prev_key->next) {
3823 prev_key->next->prev = (struct lysc_node*)key;
3824 } else {
3825 list->child->prev = (struct lysc_node*)key;
3826 }
3827 prev_key->next = (struct lysc_node*)key;
3828 } else {
3829 list->child->prev = (struct lysc_node*)key;
3830 }
3831 /* fix links in parent */
3832 if (!key->prev->next) {
3833 list->child = (struct lysc_node*)key;
3834 }
3835 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003836
3837 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003838 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003839 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003840 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003841 }
3842
3843 /* uniques */
3844 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003845 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003846 }
3847
Radek Krejciec4da802019-05-02 13:02:41 +02003848 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3849 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003850
3851done:
3852 return ret;
3853}
3854
Radek Krejcib56c7502019-02-13 14:19:54 +01003855/**
3856 * @brief Do some checks and set the default choice's case.
3857 *
3858 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3859 *
3860 * @param[in] ctx Compile context.
3861 * @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,
3862 * not the reference to the imported module.
3863 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3864 * @return LY_ERR value.
3865 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003866static LY_ERR
3867lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3868{
3869 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3870 const char *prefix = NULL, *name;
3871 size_t prefix_len = 0;
3872
3873 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3874 name = strchr(dflt, ':');
3875 if (name) {
3876 prefix = dflt;
3877 prefix_len = name - prefix;
3878 ++name;
3879 } else {
3880 name = dflt;
3881 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02003882 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003883 /* prefixed default case make sense only for the prefix of the schema itself */
3884 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3885 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3886 prefix_len, prefix);
3887 return LY_EVALID;
3888 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003889 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 +01003890 if (!ch->dflt) {
3891 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3892 "Default case \"%s\" not found.", dflt);
3893 return LY_EVALID;
3894 }
3895 /* no mandatory nodes directly under the default case */
3896 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003897 if (iter->parent != (struct lysc_node*)ch->dflt) {
3898 break;
3899 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003900 if (iter->flags & LYS_MAND_TRUE) {
3901 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3902 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3903 return LY_EVALID;
3904 }
3905 }
Radek Krejci01342af2019-01-03 15:18:08 +01003906 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003907 return LY_SUCCESS;
3908}
3909
Radek Krejciccd20f12019-02-15 14:12:27 +01003910static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003911lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003912{
3913 struct lys_module *mod;
3914 const char *prefix = NULL, *name;
3915 size_t prefix_len = 0;
3916 struct lysc_node_case *cs;
3917 struct lysc_node *node;
3918
3919 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3920 name = strchr(dflt, ':');
3921 if (name) {
3922 prefix = dflt;
3923 prefix_len = name - prefix;
3924 ++name;
3925 } else {
3926 name = dflt;
3927 }
3928 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3929 if (prefix) {
3930 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3931 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003932 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3933 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003934 return LY_EVALID;
3935 }
3936 } else {
3937 mod = ctx->mod;
3938 }
3939 /* get the default case */
Michal Vaskoe444f752020-02-10 12:20:06 +01003940 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 +01003941 if (!cs) {
3942 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003943 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003944 return LY_EVALID;
3945 }
3946
3947 /* check that there is no mandatory node */
3948 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003949 if (node->parent != (struct lysc_node*)cs) {
3950 break;
3951 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003952 if (node->flags & LYS_MAND_TRUE) {
3953 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003954 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3955 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003956 return LY_EVALID;
3957 }
3958 }
3959
3960 /* set the default case in choice */
3961 ch->dflt = cs;
3962 cs->flags |= LYS_SET_DFLT;
3963
3964 return LY_SUCCESS;
3965}
3966
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003967/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003968 * @brief Compile parsed choice node information.
3969 * @param[in] ctx Compile context
3970 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003971 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003972 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003973 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3974 */
3975static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003976lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003977{
3978 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3979 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3980 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01003981 LY_ERR ret = LY_SUCCESS;
3982
Radek Krejci056d0a82018-12-06 16:57:25 +01003983 LY_LIST_FOR(ch_p->child, child_p) {
3984 if (child_p->nodetype == LYS_CASE) {
3985 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003986 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003987 }
3988 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003989 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003990 }
3991 }
3992
3993 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003994 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003995 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003996 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003997
Radek Krejci9800fb82018-12-13 14:26:23 +01003998 return ret;
3999}
4000
4001/**
4002 * @brief Compile parsed anydata or anyxml node information.
4003 * @param[in] ctx Compile context
4004 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004005 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4006 * is enriched with the any-specific information.
4007 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4008 */
4009static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004010lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004011{
4012 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4013 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4014 unsigned int u;
4015 LY_ERR ret = LY_SUCCESS;
4016
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004017 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004018 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4019 /* do not check "must" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004020 ly_set_add(&ctx->xpath, any, 0);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004021 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004022
4023 if (any->flags & LYS_CONFIG_W) {
4024 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004025 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004026 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004027done:
4028 return ret;
4029}
4030
Radek Krejcib56c7502019-02-13 14:19:54 +01004031/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004032 * @brief Connect the node into the siblings list and check its name uniqueness.
4033 *
4034 * @param[in] ctx Compile context
4035 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4036 * the choice itself is expected instead of a specific case node.
4037 * @param[in] node Schema node to connect into the list.
4038 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004039 * 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 +01004040 */
4041static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004042lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004043{
4044 struct lysc_node **children;
4045
4046 if (node->nodetype == LYS_CASE) {
4047 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4048 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004049 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004050 }
4051 if (children) {
4052 if (!(*children)) {
4053 /* first child */
4054 *children = node;
4055 } else if (*children != node) {
4056 /* by the condition in previous branch we cover the choice/case children
4057 * - the children list is shared by the choice and the the first case, in addition
4058 * the first child of each case must be referenced from the case node. So the node is
4059 * actually always already inserted in case it is the first children - so here such
4060 * a situation actually corresponds to the first branch */
4061 /* insert at the end of the parent's children list */
4062 (*children)->prev->next = node;
4063 node->prev = (*children)->prev;
4064 (*children)->prev = node;
4065
4066 /* check the name uniqueness */
4067 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4068 lysc_node_notifs(parent), node->name, node)) {
4069 return LY_EEXIST;
4070 }
4071 }
4072 }
4073 return LY_SUCCESS;
4074}
4075
Radek Krejci95710c92019-02-11 15:49:55 +01004076/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004077 * @brief Prepare the case structure in choice node for the new data node.
4078 *
4079 * 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
4080 * created in the choice when the first child was processed.
4081 *
4082 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004083 * @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,
4084 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004085 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4086 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4087 * @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,
4088 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004089 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004090static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004091lys_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 +01004092{
4093 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004094 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004095 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004096 unsigned int u;
4097 LY_ERR ret;
4098
Radek Krejci95710c92019-02-11 15:49:55 +01004099#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004100 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004101 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004102 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4103 return NULL; \
4104 } \
4105 }
4106
Radek Krejci95710c92019-02-11 15:49:55 +01004107 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4108 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004109
4110 /* we have to add an implicit case node into the parent choice */
4111 cs = calloc(1, sizeof(struct lysc_node_case));
4112 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004113 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004114 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004115 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004116 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4117 /* the case is already present since the child is not its first children */
4118 return (struct lysc_node_case*)ch->cases->prev;
4119 }
Radek Krejci95710c92019-02-11 15:49:55 +01004120 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004121
4122 /* explicit parent case is not present (this is its first child) */
4123 cs = calloc(1, sizeof(struct lysc_node_case));
4124 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004125 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004126 cs->flags = LYS_STATUS_MASK & node_p->flags;
4127 cs->sp = node_p;
4128
Radek Krejcib1b59152019-01-07 13:21:56 +01004129 /* 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 +02004130 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004131
4132 if (node_p->when) {
4133 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004134 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004135 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004136
4137 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4138 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004139 ly_set_add(&ctx->xpath, cs, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004140 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004141 }
Radek Krejciec4da802019-05-02 13:02:41 +02004142 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004143 } else {
4144 LOGINT(ctx->ctx);
4145 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004146 }
4147 cs->module = ctx->mod;
4148 cs->prev = (struct lysc_node*)cs;
4149 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004150 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004151 cs->child = child;
4152
4153 return cs;
4154error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004155 if (cs) {
4156 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4157 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004158 return NULL;
4159
4160#undef UNIQUE_CHECK
4161}
4162
Radek Krejcib56c7502019-02-13 14:19:54 +01004163/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004164 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004165 *
4166 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004167 * @param[in] node Target node where the config is supposed to be changed.
4168 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004169 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4170 * 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 +01004171 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4172 * @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 +01004173 * @return LY_ERR value.
4174 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004175static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004176lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004177 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004178{
4179 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004180 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004181
4182 if (config == (node->flags & LYS_CONFIG_MASK)) {
4183 /* nothing to do */
4184 return LY_SUCCESS;
4185 }
4186
4187 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004188 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004189 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4190 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004191 "Invalid %s of config - configuration node cannot be child of any state data node.",
4192 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004193 return LY_EVALID;
4194 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004195 node->flags |= LYS_SET_CONFIG;
4196 } else {
4197 if (node->flags & LYS_SET_CONFIG) {
4198 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4199 /* setting config flags, but have node with explicit config true */
4200 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004201 "Invalid %s of config - configuration node cannot be child of any state data node.",
4202 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004203 return LY_EVALID;
4204 }
4205 /* do not change config on nodes where the config is explicitely set, this does not apply to
4206 * nodes, which are being changed explicitly (targets of refine or deviation) */
4207 return LY_SUCCESS;
4208 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004209 }
4210 node->flags &= ~LYS_CONFIG_MASK;
4211 node->flags |= config;
4212
4213 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004214 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004215 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004216 }
4217
Radek Krejci76b3e962018-12-14 17:01:25 +01004218 return LY_SUCCESS;
4219}
4220
Radek Krejcib56c7502019-02-13 14:19:54 +01004221/**
4222 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4223 *
4224 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4225 * the flag to such parents from a mandatory children.
4226 *
4227 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4228 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4229 * (mandatory children was removed).
4230 */
Radek Krejcife909632019-02-12 15:34:42 +01004231void
4232lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4233{
4234 struct lysc_node *iter;
4235
4236 if (add) { /* set flag */
4237 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4238 parent = parent->parent) {
4239 parent->flags |= LYS_MAND_TRUE;
4240 }
4241 } else { /* unset flag */
4242 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004243 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004244 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004245 /* there is another mandatory node */
4246 return;
4247 }
4248 }
4249 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4250 parent->flags &= ~LYS_MAND_TRUE;
4251 }
4252 }
4253}
4254
Radek Krejci056d0a82018-12-06 16:57:25 +01004255/**
Radek Krejci3641f562019-02-13 15:38:40 +01004256 * @brief Internal sorting process for the lys_compile_augment_sort().
4257 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4258 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4259 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4260 */
4261static void
4262lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4263{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004264 LY_ARRAY_SIZE_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004265 size_t len;
4266
4267 len = strlen(aug_p->nodeid);
4268 LY_ARRAY_FOR(result, v) {
4269 if (strlen(result[v]->nodeid) <= len) {
4270 continue;
4271 }
4272 if (v < LY_ARRAY_SIZE(result)) {
4273 /* move the rest of array */
4274 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4275 break;
4276 }
4277 }
4278 result[v] = aug_p;
4279 LY_ARRAY_INCREMENT(result);
4280}
4281
4282/**
4283 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4284 *
4285 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4286 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4287 *
4288 * @param[in] ctx Compile context.
4289 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4290 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4291 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4292 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4293 * @return LY_ERR value.
4294 */
4295LY_ERR
4296lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4297{
4298 struct lysp_augment **result = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004299 LY_ARRAY_SIZE_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004300
4301 assert(augments);
4302
4303 /* get count of the augments in module and all its submodules */
4304 if (aug_p) {
4305 count += LY_ARRAY_SIZE(aug_p);
4306 }
4307 LY_ARRAY_FOR(inc_p, u) {
4308 if (inc_p[u].submodule->augments) {
4309 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4310 }
4311 }
4312
4313 if (!count) {
4314 *augments = NULL;
4315 return LY_SUCCESS;
4316 }
4317 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4318
4319 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4320 * together, so there can be even /z/y betwwen them. */
4321 LY_ARRAY_FOR(aug_p, u) {
4322 lys_compile_augment_sort_(&aug_p[u], result);
4323 }
4324 LY_ARRAY_FOR(inc_p, u) {
4325 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4326 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4327 }
4328 }
4329
4330 *augments = result;
4331 return LY_SUCCESS;
4332}
4333
4334/**
4335 * @brief Compile the parsed augment connecting it into its target.
4336 *
4337 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4338 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4339 * are already implemented and compiled.
4340 *
4341 * @param[in] ctx Compile context.
4342 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004343 * @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
4344 * children in case of the augmenting uses data.
4345 * @return LY_SUCCESS on success.
4346 * @return LY_EVALID on failure.
4347 */
4348LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004349lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004350{
4351 LY_ERR ret = LY_SUCCESS;
4352 struct lysp_node *node_p, *case_node_p;
4353 struct lysc_node *target; /* target target of the augment */
4354 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004355 struct lysc_when **when, *when_shared;
4356 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004357 uint16_t flags = 0;
4358 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004359 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004360
Radek Krejci327de162019-06-14 12:52:07 +02004361 lysc_update_path(ctx, NULL, "{augment}");
4362 lysc_update_path(ctx, NULL, aug_p->nodeid);
4363
Radek Krejci7af64242019-02-18 13:07:53 +01004364 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004365 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004366 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004367 if (ret != LY_SUCCESS) {
4368 if (ret == LY_EDENIED) {
4369 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4370 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4371 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4372 }
4373 return LY_EVALID;
4374 }
4375
4376 /* check for mandatory nodes
4377 * - new cases augmenting some choice can have mandatory nodes
4378 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4379 */
Radek Krejci733988a2019-02-15 15:12:44 +01004380 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004381 allow_mandatory = 1;
4382 }
4383
4384 when_shared = NULL;
4385 LY_LIST_FOR(aug_p->child, node_p) {
4386 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4387 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004388 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004389 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4390 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004391 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004392 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4393 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004394 return LY_EVALID;
4395 }
4396
4397 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004398 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004399 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004400 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004401 } else {
4402 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004403 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004404 }
4405 }
Radek Krejciec4da802019-05-02 13:02:41 +02004406 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004407
Radek Krejcife13da42019-02-15 14:51:01 +01004408 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4409 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004410 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004411 /* 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 +01004412 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 +01004413 } else if (target->nodetype == LYS_CHOICE) {
4414 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4415 node = ((struct lysc_node_choice*)target)->cases->prev;
4416 } else {
4417 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004418 node = (struct lysc_node*)lysc_node_children(target, flags);
4419 if (!node) {
4420 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4421 break;
4422 }
4423 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004424 }
4425
Radek Krejci733988a2019-02-15 15:12:44 +01004426 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004427 node->flags &= ~LYS_MAND_TRUE;
4428 lys_compile_mandatory_parents(target, 0);
4429 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004430 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004431 return LY_EVALID;
4432 }
4433
4434 /* pass augment's when to all the children */
4435 if (aug_p->when) {
4436 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4437 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004438 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004439 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004440
4441 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4442 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004443 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004444 }
4445
Radek Krejci3641f562019-02-13 15:38:40 +01004446 when_shared = *when;
4447 } else {
4448 ++when_shared->refcount;
4449 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004450
4451 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4452 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004453 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004454 }
Radek Krejci3641f562019-02-13 15:38:40 +01004455 }
4456 }
4457 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004458
Radek Krejciec4da802019-05-02 13:02:41 +02004459 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004460 switch (target->nodetype) {
4461 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004462 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)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_container*)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 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004468 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004469 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004470 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004471 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004472 break;
4473 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004474 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004475 if (aug_p->actions) {
4476 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004477 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4478 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004479 return LY_EVALID;
4480 }
4481 if (aug_p->notifs) {
4482 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004483 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004484 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004485 return LY_EVALID;
4486 }
4487 }
Radek Krejci3641f562019-02-13 15:38:40 +01004488
Radek Krejci327de162019-06-14 12:52:07 +02004489 lysc_update_path(ctx, NULL, NULL);
4490 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004491error:
Radek Krejciec4da802019-05-02 13:02:41 +02004492 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004493 return ret;
4494}
4495
4496/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004497 * @brief Apply refined or deviated mandatory flag to the target node.
4498 *
4499 * @param[in] ctx Compile context.
4500 * @param[in] node Target node where the mandatory property is supposed to be changed.
4501 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004502 * @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 +01004503 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4504 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4505 * 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 +01004506 * @return LY_ERR value.
4507 */
4508static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004509lys_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 +01004510{
4511 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4512 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004513 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4514 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004515 return LY_EVALID;
4516 }
4517
4518 if (mandatory_flag & LYS_MAND_TRUE) {
4519 /* check if node has default value */
4520 if (node->nodetype & LYS_LEAF) {
4521 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004522 if (refine_flag) {
4523 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004524 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004525 return LY_EVALID;
4526 }
Radek Krejcia1911222019-07-22 17:24:50 +02004527 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004528 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004529 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004530
4531 /* update the list of incomplete default values if needed */
4532 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4533
Radek Krejcia1911222019-07-22 17:24:50 +02004534 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4535 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4536 free(leaf->dflt);
4537 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004538 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004539 }
4540 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004541 if (refine_flag) {
4542 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004543 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004544 return LY_EVALID;
4545 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004546 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004547 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004548 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 +01004549 return LY_EVALID;
4550 }
4551
4552 node->flags &= ~LYS_MAND_FALSE;
4553 node->flags |= LYS_MAND_TRUE;
4554 lys_compile_mandatory_parents(node->parent, 1);
4555 } else {
4556 /* make mandatory false */
4557 node->flags &= ~LYS_MAND_TRUE;
4558 node->flags |= LYS_MAND_FALSE;
4559 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004560 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 +01004561 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004562 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004563 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004564 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4565 leaf->dflt->realtype = leaf->type->dflt->realtype;
4566 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4567 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004568 }
4569 }
4570 return LY_SUCCESS;
4571}
4572
4573/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004574 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4575 * If present, also apply uses's modificators.
4576 *
4577 * @param[in] ctx Compile context
4578 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004579 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4580 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4581 * the compile context.
4582 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4583 */
4584static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004585lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004586{
4587 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004588 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004589 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004590 struct lysc_node_container context_node_fake =
4591 {.nodetype = LYS_CONTAINER,
4592 .module = ctx->mod,
4593 .flags = parent ? parent->flags : 0,
4594 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004595 .prev = (struct lysc_node*)&context_node_fake,
4596 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004597 struct lysp_grp *grp = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004598 LY_ARRAY_SIZE_TYPE u, v;
4599 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004600 int found;
4601 const char *id, *name, *prefix;
4602 size_t prefix_len, name_len;
4603 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004604 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004605 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004606 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004607 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004608 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004609 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004610 struct lysp_augment **augments = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004611 LY_ARRAY_SIZE_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004612 struct lysc_notif **notifs = NULL;
4613 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004614
4615 /* search for the grouping definition */
4616 found = 0;
4617 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004618 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004619 if (prefix) {
4620 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4621 if (!mod) {
4622 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004623 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004624 return LY_EVALID;
4625 }
4626 } else {
4627 mod = ctx->mod_def;
4628 }
4629 if (mod == ctx->mod_def) {
4630 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004631 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004632 LY_ARRAY_FOR(grp, u) {
4633 if (!strcmp(grp[u].name, name)) {
4634 grp = &grp[u];
4635 found = 1;
4636 break;
4637 }
4638 }
4639 }
4640 }
4641 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004642 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004643 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004644 if (grp) {
4645 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4646 if (!strcmp(grp[u].name, name)) {
4647 grp = &grp[u];
4648 found = 1;
4649 }
4650 }
4651 }
4652 if (!found && mod->parsed->includes) {
4653 /* ... and all the submodules */
4654 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4655 grp = mod->parsed->includes[u].submodule->groupings;
4656 if (grp) {
4657 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4658 if (!strcmp(grp[v].name, name)) {
4659 grp = &grp[v];
4660 found = 1;
4661 }
4662 }
4663 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004664 }
4665 }
4666 }
4667 if (!found) {
4668 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4669 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4670 return LY_EVALID;
4671 }
4672
4673 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4674 grp_stack_count = ctx->groupings.count;
4675 ly_set_add(&ctx->groupings, (void*)grp, 0);
4676 if (grp_stack_count == ctx->groupings.count) {
4677 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4679 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4680 return LY_EVALID;
4681 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004682 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4683 /* remember that the grouping is instantiated to avoid its standalone validation */
4684 grp->flags |= LYS_USED_GRP;
4685 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004686
4687 /* switch context's mod_def */
4688 mod_old = ctx->mod_def;
4689 ctx->mod_def = mod;
4690
4691 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004692 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 +01004693
Radek Krejcifc11bd72019-04-11 16:00:05 +02004694 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004695 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004696 /* 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 +02004697 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 +01004698
4699 /* some preparation for applying refines */
4700 if (grp->data == node_p) {
4701 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004702 if (parent) {
4703 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4704 } else if (ctx->mod->compiled->data) {
4705 child = ctx->mod->compiled->data;
4706 } else {
4707 child = NULL;
4708 }
4709 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004710 }
4711 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004712 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004713 LY_LIST_FOR(context_node_fake.child, child) {
4714 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004715
Radek Krejcifc11bd72019-04-11 16:00:05 +02004716 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004717 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004718 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004719 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004720 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
4721
4722 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4723 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02004724 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004725 }
4726
Radek Krejci00b874b2019-02-12 10:54:50 +01004727 when_shared = *when;
4728 } else {
4729 ++when_shared->refcount;
4730 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004731
4732 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4733 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko004d3152020-06-11 19:59:22 +02004734 ly_set_add(&ctx->xpath, child, 0);
Michal Vasko5c4e5892019-11-14 12:31:38 +01004735 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004736 }
4737 }
Radek Krejci01342af2019-01-03 15:18:08 +01004738 }
4739 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004740 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004741 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004742 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004743 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 +01004744 }
4745
Radek Krejcifc11bd72019-04-11 16:00:05 +02004746 /* compile actions */
4747 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4748 if (actions) {
4749 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004750 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004751 if (*actions && (uses_p->augments || uses_p->refines)) {
4752 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4753 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4754 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4755 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4756 }
4757 }
4758
4759 /* compile notifications */
4760 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4761 if (notifs) {
4762 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004763 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004764 if (*notifs && (uses_p->augments || uses_p->refines)) {
4765 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4766 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4767 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4768 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4769 }
4770 }
4771
4772
Radek Krejci3641f562019-02-13 15:38:40 +01004773 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004774 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004775 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004776 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004777 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004778
Radek Krejcif0089082019-01-07 16:42:01 +01004779 /* reload previous context's mod_def */
4780 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004781 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004782
Radek Krejci76b3e962018-12-14 17:01:25 +01004783 /* apply refine */
4784 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004785 lysc_update_path(ctx, NULL, rfn->nodeid);
4786
Radek Krejci7af64242019-02-18 13:07:53 +01004787 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 +01004788 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004789 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004790 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004791
4792 /* default value */
4793 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004794 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004795 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004796 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_SIZE_TYPE" default values.",
Radek Krejci327de162019-06-14 12:52:07 +02004797 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004798 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004799 }
4800 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004802 "Invalid refine of default - %s cannot hold default value(s).",
4803 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004804 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004805 }
4806 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004807 struct ly_err_item *err = NULL;
4808 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4809 if (leaf->dflt) {
4810 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004811 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004812 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4813 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4814 } else {
4815 /* prepare a new one */
4816 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4817 leaf->dflt->realtype = leaf->type;
4818 }
4819 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004820 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004821 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4822 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004823 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004824 leaf->dflt->realtype->refcount++;
4825 if (err) {
4826 ly_err_print(err);
4827 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4828 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4829 ly_err_free(err);
4830 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004831 if (rc == LY_EINCOMPLETE) {
4832 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004833 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004834
4835 /* but in general result is so far ok */
4836 rc = LY_SUCCESS;
4837 }
Radek Krejcia1911222019-07-22 17:24:50 +02004838 LY_CHECK_GOTO(rc, cleanup);
4839 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004840 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004841 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4842
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004843 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004844 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4845 "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 +02004846 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004847 }
Radek Krejcia1911222019-07-22 17:24:50 +02004848
4849 /* remove previous set of default values */
4850 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004851 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004852 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4853 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4854 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004855 }
Radek Krejcia1911222019-07-22 17:24:50 +02004856 LY_ARRAY_FREE(llist->dflts);
4857 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004858 LY_ARRAY_FREE(llist->dflts_mods);
4859 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004860
4861 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004862 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02004863 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004864 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004865 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004866 LY_ARRAY_INCREMENT(llist->dflts_mods);
4867 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004868 LY_ARRAY_INCREMENT(llist->dflts);
4869 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4870 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004871 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004872 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004873 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004874 llist->dflts[u]->realtype->refcount++;
4875 if (err) {
4876 ly_err_print(err);
4877 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4878 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4879 ly_err_free(err);
4880 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004881 if (rc == LY_EINCOMPLETE) {
4882 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004883 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004884
4885 /* but in general result is so far ok */
4886 rc = LY_SUCCESS;
4887 }
4888 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004889 }
Radek Krejcia1911222019-07-22 17:24:50 +02004890 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004891 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004892 if (((struct lysc_node_choice*)node)->dflt) {
4893 /* unset LYS_SET_DFLT from the current default case */
4894 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4895 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004896 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 +01004897 }
4898 }
4899
Radek Krejci12fb9142019-01-08 09:45:30 +01004900 /* description */
4901 if (rfn->dsc) {
4902 FREE_STRING(ctx->ctx, node->dsc);
4903 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4904 }
4905
4906 /* reference */
4907 if (rfn->ref) {
4908 FREE_STRING(ctx->ctx, node->ref);
4909 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4910 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004911
4912 /* config */
4913 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004914 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004915 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004916 } else {
4917 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01004918 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004919 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004920 }
4921
4922 /* mandatory */
4923 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004924 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004925 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004926
4927 /* presence */
4928 if (rfn->presence) {
4929 if (node->nodetype != LYS_CONTAINER) {
4930 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004931 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4932 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004933 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004934 }
4935 node->flags |= LYS_PRESENCE;
4936 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004937
4938 /* must */
4939 if (rfn->musts) {
4940 switch (node->nodetype) {
4941 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004942 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 +01004943 break;
4944 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004945 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 +01004946 break;
4947 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004948 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 +01004949 break;
4950 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004951 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 +01004952 break;
4953 case LYS_ANYXML:
4954 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004955 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 +01004956 break;
4957 default:
4958 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004959 "Invalid refine of must statement - %s cannot hold any must statement.",
4960 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004961 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004962 }
Michal Vasko004d3152020-06-11 19:59:22 +02004963 ly_set_add(&ctx->xpath, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01004964 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004965
4966 /* min/max-elements */
4967 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4968 switch (node->nodetype) {
4969 case LYS_LEAFLIST:
4970 if (rfn->flags & LYS_SET_MAX) {
4971 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4972 }
4973 if (rfn->flags & LYS_SET_MIN) {
4974 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004975 if (rfn->min) {
4976 node->flags |= LYS_MAND_TRUE;
4977 lys_compile_mandatory_parents(node->parent, 1);
4978 } else {
4979 node->flags &= ~LYS_MAND_TRUE;
4980 lys_compile_mandatory_parents(node->parent, 0);
4981 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004982 }
4983 break;
4984 case LYS_LIST:
4985 if (rfn->flags & LYS_SET_MAX) {
4986 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4987 }
4988 if (rfn->flags & LYS_SET_MIN) {
4989 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004990 if (rfn->min) {
4991 node->flags |= LYS_MAND_TRUE;
4992 lys_compile_mandatory_parents(node->parent, 1);
4993 } else {
4994 node->flags &= ~LYS_MAND_TRUE;
4995 lys_compile_mandatory_parents(node->parent, 0);
4996 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004997 }
4998 break;
4999 default:
5000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005001 "Invalid refine of %s statement - %s cannot hold this statement.",
5002 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005003 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005004 }
5005 }
Radek Krejcif0089082019-01-07 16:42:01 +01005006
5007 /* if-feature */
5008 if (rfn->iffeatures) {
5009 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005010 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005011 }
Radek Krejci327de162019-06-14 12:52:07 +02005012
5013 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005014 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005015
Radek Krejcif2271f12019-01-07 16:42:23 +01005016 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005017 for (uint32_t i = 0; i < refined.count; ++i) {
5018 node = (struct lysc_node*)refined.objs[i];
5019 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02005020 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005021
5022 /* check possible conflict with default value (default added, mandatory left true) */
5023 if ((node->flags & LYS_MAND_TRUE) &&
5024 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5025 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5026 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005027 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005028 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005029 }
5030
5031 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5032 if (node->nodetype == LYS_LIST) {
5033 min = ((struct lysc_node_list*)node)->min;
5034 max = ((struct lysc_node_list*)node)->max;
5035 } else {
5036 min = ((struct lysc_node_leaflist*)node)->min;
5037 max = ((struct lysc_node_leaflist*)node)->max;
5038 }
5039 if (min > max) {
5040 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005041 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5042 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005043 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005044 }
5045 }
5046 }
5047
Radek Krejci327de162019-06-14 12:52:07 +02005048 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005049 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005050
5051cleanup:
5052 /* fix connection of the children nodes from fake context node back into the parent */
5053 if (context_node_fake.child) {
5054 context_node_fake.child->prev = child;
5055 }
5056 LY_LIST_FOR(context_node_fake.child, child) {
5057 child->parent = parent;
5058 }
5059
5060 if (uses_p->augments || uses_p->refines) {
5061 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005062 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005063 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5064 LY_ARRAY_FREE(context_node_fake.actions);
5065 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005066 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005067 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5068 LY_ARRAY_FREE(context_node_fake.notifs);
5069 }
5070 }
5071
Radek Krejcie86bf772018-12-14 11:39:53 +01005072 /* reload previous context's mod_def */
5073 ctx->mod_def = mod_old;
5074 /* remove the grouping from the stack for circular groupings dependency check */
5075 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5076 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005077 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005078 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005079
5080 return ret;
5081}
5082
Radek Krejci327de162019-06-14 12:52:07 +02005083static int
5084lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5085{
5086 struct lysp_node *iter;
5087 int len = 0;
5088
5089 *path = NULL;
5090 for (iter = node; iter && len >= 0; iter = iter->parent) {
5091 char *s = *path;
5092 char *id;
5093
5094 switch (iter->nodetype) {
5095 case LYS_USES:
5096 asprintf(&id, "{uses='%s'}", iter->name);
5097 break;
5098 case LYS_GROUPING:
5099 asprintf(&id, "{grouping='%s'}", iter->name);
5100 break;
5101 case LYS_AUGMENT:
5102 asprintf(&id, "{augment='%s'}", iter->name);
5103 break;
5104 default:
5105 id = strdup(iter->name);
5106 break;
5107 }
5108
5109 if (!iter->parent) {
5110 /* print prefix */
5111 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5112 } else {
5113 /* prefix is the same as in parent */
5114 len = asprintf(path, "/%s%s", id, s ? s : "");
5115 }
5116 free(s);
5117 free(id);
5118 }
5119
5120 if (len < 0) {
5121 free(*path);
5122 *path = NULL;
5123 } else if (len == 0) {
5124 *path = strdup("/");
5125 len = 1;
5126 }
5127 return len;
5128}
5129
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005130/**
5131 * @brief Validate groupings that were defined but not directly used in the schema itself.
5132 *
5133 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5134 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5135 */
5136static LY_ERR
5137lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5138{
5139 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005140 char *path;
5141 int len;
5142
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005143 struct lysp_node_uses fake_uses = {
5144 .parent = node_p,
5145 .nodetype = LYS_USES,
5146 .flags = 0, .next = NULL,
5147 .name = grp->name,
5148 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5149 .refines = NULL, .augments = NULL
5150 };
5151 struct lysc_node_container fake_container = {
5152 .nodetype = LYS_CONTAINER,
5153 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5154 .module = ctx->mod,
5155 .sp = NULL, .parent = NULL, .next = NULL,
5156 .prev = (struct lysc_node*)&fake_container,
5157 .name = "fake",
5158 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5159 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5160 };
5161
5162 if (grp->parent) {
5163 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5164 }
Radek Krejci327de162019-06-14 12:52:07 +02005165
5166 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5167 if (len < 0) {
5168 LOGMEM(ctx->ctx);
5169 return LY_EMEM;
5170 }
5171 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5172 ctx->path_len = (uint16_t)len;
5173 free(path);
5174
5175 lysc_update_path(ctx, NULL, "{grouping}");
5176 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005177 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005178 lysc_update_path(ctx, NULL, NULL);
5179 lysc_update_path(ctx, NULL, NULL);
5180
5181 ctx->path_len = 1;
5182 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005183
5184 /* cleanup */
5185 lysc_node_container_free(ctx->ctx, &fake_container);
5186
5187 return ret;
5188}
Radek Krejcife909632019-02-12 15:34:42 +01005189
Radek Krejcie86bf772018-12-14 11:39:53 +01005190/**
Radek Krejcia3045382018-11-22 14:30:31 +01005191 * @brief Compile parsed schema node information.
5192 * @param[in] ctx Compile context
5193 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005194 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5195 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5196 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005197 * @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).
5198 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005199 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5200 */
Radek Krejci19a96102018-11-15 13:38:09 +01005201static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005202lys_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 +01005203{
Radek Krejci1c54f462020-05-12 17:25:34 +02005204 LY_ERR ret = LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01005205 struct lysc_node *node;
5206 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005207 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005208 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005209 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005210
Radek Krejci327de162019-06-14 12:52:07 +02005211 if (node_p->nodetype != LYS_USES) {
5212 lysc_update_path(ctx, parent, node_p->name);
5213 } else {
5214 lysc_update_path(ctx, NULL, "{uses}");
5215 lysc_update_path(ctx, NULL, node_p->name);
5216 }
5217
Radek Krejci19a96102018-11-15 13:38:09 +01005218 switch (node_p->nodetype) {
5219 case LYS_CONTAINER:
5220 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5221 node_compile_spec = lys_compile_node_container;
5222 break;
5223 case LYS_LEAF:
5224 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5225 node_compile_spec = lys_compile_node_leaf;
5226 break;
5227 case LYS_LIST:
5228 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005229 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005230 break;
5231 case LYS_LEAFLIST:
5232 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005233 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005234 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005235 case LYS_CHOICE:
5236 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005237 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005238 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005239 case LYS_ANYXML:
5240 case LYS_ANYDATA:
5241 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005242 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005243 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005244 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005245 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5246 lysc_update_path(ctx, NULL, NULL);
5247 lysc_update_path(ctx, NULL, NULL);
5248 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005249 default:
5250 LOGINT(ctx->ctx);
5251 return LY_EINT;
5252 }
5253 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5254 node->nodetype = node_p->nodetype;
5255 node->module = ctx->mod;
5256 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005257 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005258
5259 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005260 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005261 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005262 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005263 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5264 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005265 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005266 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005267 node->flags |= LYS_CONFIG_R;
5268 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005269 /* config not explicitely set, inherit it from parent */
5270 if (parent) {
5271 node->flags |= parent->flags & LYS_CONFIG_MASK;
5272 } else {
5273 /* default is config true */
5274 node->flags |= LYS_CONFIG_W;
5275 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005276 } else {
5277 /* config set explicitely */
5278 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005279 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005280 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5281 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5282 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005283 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005284 goto error;
5285 }
Radek Krejci19a96102018-11-15 13:38:09 +01005286
Radek Krejcia6d57732018-11-29 13:40:37 +01005287 /* *list ordering */
5288 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5289 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005290 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005291 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5292 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005293 node->flags &= ~LYS_ORDBY_MASK;
5294 node->flags |= LYS_ORDBY_SYSTEM;
5295 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5296 /* default ordering is system */
5297 node->flags |= LYS_ORDBY_SYSTEM;
5298 }
5299 }
5300
Radek Krejci19a96102018-11-15 13:38:09 +01005301 /* status - it is not inherited by specification, but it does not make sense to have
5302 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005303 if (!parent || parent->nodetype != LYS_CHOICE) {
5304 /* in case of choice/case's children, postpone the check to the moment we know if
5305 * 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 +02005306 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 +01005307 }
5308
Radek Krejciec4da802019-05-02 13:02:41 +02005309 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005310 node->sp = node_p;
5311 }
5312 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005313 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5314 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005315 if (node_p->when) {
5316 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005317 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005318
5319 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5320 /* do not check "when" semantics in a grouping */
Michal Vasko004d3152020-06-11 19:59:22 +02005321 ly_set_add(&ctx->xpath, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01005322 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005323 }
Radek Krejciec4da802019-05-02 13:02:41 +02005324 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005325
5326 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005327 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005328
Radek Krejci0935f412019-08-20 16:15:18 +02005329 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5330
Radek Krejcife909632019-02-12 15:34:42 +01005331 /* inherit LYS_MAND_TRUE in parent containers */
5332 if (node->flags & LYS_MAND_TRUE) {
5333 lys_compile_mandatory_parents(parent, 1);
5334 }
5335
Radek Krejci327de162019-06-14 12:52:07 +02005336 lysc_update_path(ctx, NULL, NULL);
5337
Radek Krejci19a96102018-11-15 13:38:09 +01005338 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005339 if (parent) {
5340 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005341 if (node_p->parent->nodetype == LYS_CASE) {
5342 lysc_update_path(ctx, parent, node_p->parent->name);
5343 } else {
5344 lysc_update_path(ctx, parent, node->name);
5345 }
Radek Krejciec4da802019-05-02 13:02:41 +02005346 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005347 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005348 if (uses_status) {
5349
5350 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005351 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005352 * it directly gets the same status flags as the choice;
5353 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005354 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005355 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005356 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005357 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005358 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005359 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005360 }
Radek Krejciec4da802019-05-02 13:02:41 +02005361 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 +02005362
5363 if (parent->nodetype == LYS_CHOICE) {
5364 lysc_update_path(ctx, NULL, NULL);
5365 }
Radek Krejci19a96102018-11-15 13:38:09 +01005366 } else {
5367 /* top-level element */
5368 if (!ctx->mod->compiled->data) {
5369 ctx->mod->compiled->data = node;
5370 } else {
5371 /* insert at the end of the module's top-level nodes list */
5372 ctx->mod->compiled->data->prev->next = node;
5373 node->prev = ctx->mod->compiled->data->prev;
5374 ctx->mod->compiled->data->prev = node;
5375 }
Radek Krejci327de162019-06-14 12:52:07 +02005376 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005377 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5378 ctx->mod->compiled->notifs, node->name, node)) {
5379 return LY_EVALID;
5380 }
Radek Krejci19a96102018-11-15 13:38:09 +01005381 }
Radek Krejci327de162019-06-14 12:52:07 +02005382 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005383
5384 return LY_SUCCESS;
5385
5386error:
5387 lysc_node_free(ctx->ctx, node);
5388 return ret;
5389}
5390
Radek Krejciccd20f12019-02-15 14:12:27 +01005391static void
5392lysc_disconnect(struct lysc_node *node)
5393{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005394 struct lysc_node *parent, *child, *prev = NULL, *next;
5395 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005396 int remove_cs = 0;
5397
5398 parent = node->parent;
5399
5400 /* parent's first child */
5401 if (!parent) {
5402 return;
5403 }
5404 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005405 cs = (struct lysc_node_case*)node;
5406 } else if (parent->nodetype == LYS_CASE) {
5407 /* disconnecting some node in a case */
5408 cs = (struct lysc_node_case*)parent;
5409 parent = cs->parent;
5410 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5411 if (child == node) {
5412 if (cs->child == child) {
5413 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5414 /* case with a single child -> remove also the case */
5415 child->parent = NULL;
5416 remove_cs = 1;
5417 } else {
5418 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005419 }
5420 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005421 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005422 }
5423 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005424 if (!remove_cs) {
5425 cs = NULL;
5426 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005427 } else if (lysc_node_children(parent, node->flags) == node) {
5428 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005429 }
5430
5431 if (cs) {
5432 if (remove_cs) {
5433 /* cs has only one child which is being also removed */
5434 lysc_disconnect((struct lysc_node*)cs);
5435 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5436 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005437 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5438 /* default case removed */
5439 ((struct lysc_node_choice*)parent)->dflt = NULL;
5440 }
5441 if (((struct lysc_node_choice*)parent)->cases == cs) {
5442 /* first case removed */
5443 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5444 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005445 if (cs->child) {
5446 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5447 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5448 prev = cs->child->prev;
5449 } /* else all the children are under a single case */
5450 LY_LIST_FOR_SAFE(cs->child, next, child) {
5451 if (child->parent != (struct lysc_node*)cs) {
5452 break;
5453 }
5454 lysc_node_free(node->module->ctx, child);
5455 }
5456 if (prev) {
5457 if (prev->next) {
5458 prev->next = child;
5459 }
5460 if (child) {
5461 child->prev = prev;
5462 } else {
5463 /* link from the first child under the cases */
5464 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5465 }
5466 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005467 }
5468 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005469 }
5470
5471 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005472 if (node->prev->next) {
5473 node->prev->next = node->next;
5474 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005475 if (node->next) {
5476 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005477 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005478 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005479 if (child) {
5480 child->prev = node->prev;
5481 }
5482 } else if (((struct lysc_node_choice*)parent)->cases) {
5483 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005484 }
5485}
5486
5487LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005488lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005489{
Radek Krejcia1911222019-07-22 17:24:50 +02005490 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005491 struct ly_set devs_p = {0};
5492 struct ly_set targets = {0};
5493 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005494 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005495 struct lysc_action *rpcs;
5496 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005497 struct lysp_deviation *dev;
5498 struct lysp_deviate *d, **dp_new;
5499 struct lysp_deviate_add *d_add;
5500 struct lysp_deviate_del *d_del;
5501 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005502 LY_ARRAY_SIZE_TYPE u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005503 struct lysc_deviation {
5504 const char *nodeid;
5505 struct lysc_node *target; /* target node of the deviation */
5506 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005507 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005508 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5509 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005510 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005511 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005512 const char *prefix, *name, *nodeid, *dflt;
Michal Vasko57c10cd2020-05-27 15:57:11 +02005513 struct lys_module *mod, **dev_mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005514 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005515 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005516
5517 /* get all deviations from the module and all its submodules ... */
5518 LY_ARRAY_FOR(mod_p->deviations, u) {
5519 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5520 }
5521 LY_ARRAY_FOR(mod_p->includes, v) {
5522 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5523 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5524 }
5525 }
5526 if (!devs_p.count) {
5527 /* nothing to do */
5528 return LY_SUCCESS;
5529 }
5530
Radek Krejci327de162019-06-14 12:52:07 +02005531 lysc_update_path(ctx, NULL, "{deviation}");
5532
Radek Krejciccd20f12019-02-15 14:12:27 +01005533 /* ... and group them by the target node */
5534 devs = calloc(devs_p.count, sizeof *devs);
5535 for (u = 0; u < devs_p.count; ++u) {
5536 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005537 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005538
5539 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005540 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5541 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01005542 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005543 /* move the target pointer to input/output to make them different from the action and
5544 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5545 * back to the RPC/action node due to a better compatibility and decision code in this function.
5546 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5547 if (flags & LYSC_OPT_RPC_INPUT) {
5548 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5549 flags |= LYSC_OPT_INTERNAL;
5550 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5551 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5552 flags |= LYSC_OPT_INTERNAL;
5553 }
5554 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005555 /* insert into the set of targets with duplicity detection */
5556 i = ly_set_add(&targets, target, 0);
5557 if (!devs[i]) {
5558 /* new record */
5559 devs[i] = calloc(1, sizeof **devs);
5560 devs[i]->target = target;
5561 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005562 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005563 }
5564 /* add deviates into the deviation's list of deviates */
5565 for (d = dev->deviates; d; d = d->next) {
5566 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5567 *dp_new = d;
5568 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5569 devs[i]->not_supported = 1;
5570 }
5571 }
Radek Krejci327de162019-06-14 12:52:07 +02005572
5573 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005574 }
5575
5576 /* MACROS for deviates checking */
5577#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5578 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005579 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 +01005580 goto cleanup; \
5581 }
5582
5583#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5584 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005585 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 +02005586 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005587 goto cleanup; \
5588 }
5589
Radek Krejcia1911222019-07-22 17:24:50 +02005590
Radek Krejciccd20f12019-02-15 14:12:27 +01005591#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005592 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5593 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5594 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5595 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5596 goto cleanup; \
5597 }
5598
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005599#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005600 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005601 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005602 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5603 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005604 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005605 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5606 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005607 goto cleanup; \
5608 }
5609
Radek Krejci551b12c2019-02-19 16:11:21 +01005610#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5611 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5612 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005613 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5614 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005615 goto cleanup; \
5616 }
5617
Radek Krejciccd20f12019-02-15 14:12:27 +01005618#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5619 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005620 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005621 goto cleanup; \
5622 }
5623
Radek Krejci551b12c2019-02-19 16:11:21 +01005624#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5625 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5626 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005627 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005628 goto cleanup; \
5629 }
5630
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005631#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5632 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5633 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5634 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5635 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 +01005636 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005637 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005638 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005639 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5640 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005641 goto cleanup; \
5642 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005643 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5644 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5645 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5646 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5647 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005648 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005649 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5650 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5651 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005652 }
5653
5654 /* apply deviations */
5655 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005656 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5657 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5658 struct ly_err_item *err = NULL;
5659
5660 dflt = NULL;
5661 changed_type = 0;
5662
Radek Krejci327de162019-06-14 12:52:07 +02005663 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5664
Radek Krejcif538ce52019-03-05 10:46:14 +01005665 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5666 /* fix the target pointer in case of RPC's/action's input/output */
5667 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5668 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5669 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5670 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5671 }
5672 }
5673
Radek Krejciccd20f12019-02-15 14:12:27 +01005674 /* not-supported */
5675 if (devs[u]->not_supported) {
5676 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005677 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 +01005678 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5679 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005680
5681#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5682 if (devs[u]->target->parent) { \
5683 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5684 } else { \
5685 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5686 } \
5687 LY_ARRAY_FOR(ARRAY, x) { \
5688 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5689 } \
5690 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5691 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5692 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5693 LY_ARRAY_DECREMENT(ARRAY); \
5694 }
5695
Michal Vasko1bf09392020-03-27 12:38:10 +01005696 if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005697 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5698 /* remove RPC's/action's input */
5699 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5700 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005701 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5702 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005703 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5704 /* remove RPC's/action's output */
5705 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5706 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005707 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5708 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005709 } else {
5710 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005711 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005712 }
5713 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005714 /* remove Notification */
5715 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005716 } else {
5717 /* remove the target node */
5718 lysc_disconnect(devs[u]->target);
5719 lysc_node_free(ctx->ctx, devs[u]->target);
5720 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005721
Radek Krejci474f9b82019-07-24 11:36:37 +02005722 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5723 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005724 continue;
5725 }
5726
5727 /* list of deviates (not-supported is not present in the list) */
5728 LY_ARRAY_FOR(devs[u]->deviates, v) {
5729 d = devs[u]->deviates[v];
5730
5731 switch (d->mod) {
5732 case LYS_DEV_ADD:
5733 d_add = (struct lysp_deviate_add*)d;
5734 /* [units-stmt] */
5735 if (d_add->units) {
5736 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5737 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5738
5739 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5740 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5741 }
5742
5743 /* *must-stmt */
5744 if (d_add->musts) {
5745 switch (devs[u]->target->nodetype) {
5746 case LYS_CONTAINER:
5747 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005748 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5749 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005750 break;
5751 case LYS_LEAF:
5752 case LYS_LEAFLIST:
5753 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005754 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5755 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005756 break;
5757 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005758 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5759 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005760 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005761 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005762 case LYS_ACTION:
5763 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005764 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5765 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005766 break;
Michal Vaskocc048b22020-03-27 15:52:38 +01005767 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005768 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5769 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005770 break;
5771 }
5772 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005773 default:
5774 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005775 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005776 goto cleanup;
5777 }
Michal Vasko004d3152020-06-11 19:59:22 +02005778 ly_set_add(&ctx->xpath, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01005779 }
5780
5781 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005782 if (d_add->uniques) {
5783 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5784 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5785 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005786
5787 /* *default-stmt */
5788 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005789 switch (devs[u]->target->nodetype) {
5790 case LYS_LEAF:
5791 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005792 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 +02005793 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005794 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005795 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005796 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5797 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5798 } else {
5799 /* prepare new default value storage */
5800 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005801 }
Radek Krejcia1911222019-07-22 17:24:50 +02005802 dflt = d_add->dflts[0];
5803 /* parsing is done at the end after possible replace of the leaf's type */
5804
Radek Krejci551b12c2019-02-19 16:11:21 +01005805 /* mark the new default values as leaf's own */
5806 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005807 break;
5808 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005809 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005810 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005811 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005812 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005813 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5814 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5815 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005816 }
Radek Krejcia1911222019-07-22 17:24:50 +02005817 LY_ARRAY_FREE(llist->dflts);
5818 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005819 LY_ARRAY_FREE(llist->dflts_mods);
5820 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005821 }
5822 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005823 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005824 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5825 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01005826 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005827 LY_ARRAY_INCREMENT(llist->dflts_mods);
5828 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005829 LY_ARRAY_INCREMENT(llist->dflts);
5830 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5831 llist->dflts[x]->realtype = llist->type;
5832 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 +02005833 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005834 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005835 llist->dflts[x]->realtype->refcount++;
5836 if (err) {
5837 ly_err_print(err);
5838 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5839 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5840 d_add->dflts[x - y], err->msg);
5841 ly_err_free(err);
5842 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005843 if (rc == LY_EINCOMPLETE) {
5844 /* postpone default compilation when the tree is complete */
5845 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5846
5847 /* but in general result is so far ok */
5848 rc = LY_SUCCESS;
5849 }
Radek Krejcia1911222019-07-22 17:24:50 +02005850 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005851 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005852 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005853 devs[u]->target->flags |= LYS_SET_DFLT;
5854 break;
5855 case LYS_CHOICE:
5856 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5857 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5858 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5859 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005860 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 +01005861 goto cleanup;
5862 }
5863 break;
5864 default:
5865 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005866 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005867 goto cleanup;
5868 }
5869 }
5870
5871 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005872 if (d_add->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01005873 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005874 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005875 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5876 goto cleanup;
5877 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005878 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005879 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
Michal Vaskoa3881362020-01-21 15:57:35 +01005880 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005881 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005882 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5883 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005884 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5885 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005886 goto cleanup;
5887 }
Radek Krejci327de162019-06-14 12:52:07 +02005888 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005889 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005890
5891 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005892 if (d_add->flags & LYS_MAND_MASK) {
5893 if (devs[u]->target->flags & LYS_MAND_MASK) {
5894 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005895 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5896 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005897 goto cleanup;
5898 }
Radek Krejci327de162019-06-14 12:52:07 +02005899 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005900 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005901
5902 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005903 if (d_add->flags & LYS_SET_MIN) {
5904 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5905 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5906 /* change value */
5907 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5908 } else if (devs[u]->target->nodetype == LYS_LIST) {
5909 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5910 /* change value */
5911 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5912 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005913 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005914 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5915 goto cleanup;
5916 }
5917 if (d_add->min) {
5918 devs[u]->target->flags |= LYS_MAND_TRUE;
5919 }
5920 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005921
5922 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005923 if (d_add->flags & LYS_SET_MAX) {
5924 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5925 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5926 /* change value */
5927 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5928 } else if (devs[u]->target->nodetype == LYS_LIST) {
5929 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5930 /* change value */
5931 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5932 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005933 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005934 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5935 goto cleanup;
5936 }
5937 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005938
5939 break;
5940 case LYS_DEV_DELETE:
5941 d_del = (struct lysp_deviate_del*)d;
5942
5943 /* [units-stmt] */
5944 if (d_del->units) {
5945 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02005946 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
5947 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
5948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5949 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
5950 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5951 goto cleanup;
5952 }
5953 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5954 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005955 }
5956
5957 /* *must-stmt */
5958 if (d_del->musts) {
5959 switch (devs[u]->target->nodetype) {
5960 case LYS_CONTAINER:
5961 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005962 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005963 break;
5964 case LYS_LEAF:
5965 case LYS_LEAFLIST:
5966 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005967 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005968 break;
5969 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005970 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005971 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01005972 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005973 case LYS_ACTION:
5974 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5975 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5976 break;
5977 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5978 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5979 break;
5980 }
5981 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005982 default:
5983 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005984 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005985 goto cleanup;
5986 }
5987 }
5988
5989 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005990 if (d_del->uniques) {
5991 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5992 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5993 LY_ARRAY_FOR(d_del->uniques, x) {
5994 LY_ARRAY_FOR(list->uniques, z) {
5995 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5996 nodeid = strpbrk(name, " \t\n");
5997 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02005998 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01005999 break;
6000 }
6001 while (isspace(*nodeid)) {
6002 ++nodeid;
6003 }
6004 } else {
6005 if (strcmp(name, list->uniques[z][y]->name)) {
6006 break;
6007 }
6008 }
6009 }
6010 if (!name) {
6011 /* complete match - remove the unique */
6012 LY_ARRAY_DECREMENT(list->uniques);
6013 LY_ARRAY_FREE(list->uniques[z]);
6014 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6015 --z;
6016 break;
6017 }
6018 }
6019 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006021 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6022 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006023 goto cleanup;
6024 }
6025 }
6026 if (!LY_ARRAY_SIZE(list->uniques)) {
6027 LY_ARRAY_FREE(list->uniques);
6028 list->uniques = NULL;
6029 }
6030 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006031
6032 /* *default-stmt */
6033 if (d_del->dflts) {
6034 switch (devs[u]->target->nodetype) {
6035 case LYS_LEAF:
6036 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6037 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6038 dflt, "deleting", "default", d_del->dflts[0]);
6039
Radek Krejcia1911222019-07-22 17:24:50 +02006040 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006041 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006042 if (strcmp(dflt, d_del->dflts[0])) {
6043 if (i) {
6044 free((char*)dflt);
6045 }
6046 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6047 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6048 d_del->dflts[0], dflt);
6049 goto cleanup;
6050 }
6051 if (i) {
6052 free((char*)dflt);
6053 }
6054 dflt = NULL;
6055
Radek Krejci474f9b82019-07-24 11:36:37 +02006056 /* update the list of incomplete default values if needed */
6057 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6058
Radek Krejcia1911222019-07-22 17:24:50 +02006059 /* remove the default specification */
6060 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6061 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6062 free(leaf->dflt);
6063 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006064 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006065 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006066 break;
6067 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006068 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6069 LY_ARRAY_FOR(d_del->dflts, x) {
6070 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006071 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 +02006072 if (!strcmp(dflt, d_del->dflts[x])) {
6073 if (i) {
6074 free((char*)dflt);
6075 }
6076 dflt = NULL;
6077 break;
6078 }
6079 if (i) {
6080 free((char*)dflt);
6081 }
6082 dflt = NULL;
6083 }
6084 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6085 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6086 "which does not match any of the target's property values.", d_del->dflts[x]);
6087 goto cleanup;
6088 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006089
6090 /* update the list of incomplete default values if needed */
6091 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6092
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006093 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006094 LY_ARRAY_DECREMENT(llist->dflts);
6095 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6096 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6097 free(llist->dflts[y]);
6098 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006099 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 +02006100 }
6101 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006102 LY_ARRAY_FREE(llist->dflts_mods);
6103 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006104 LY_ARRAY_FREE(llist->dflts);
6105 llist->dflts = NULL;
6106 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006107 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006108 break;
6109 case LYS_CHOICE:
6110 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6111 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6112 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006113 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006114 if (prefix) {
6115 /* use module prefixes from the deviation module to match the module of the default case */
6116 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
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 any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006120 goto cleanup;
6121 }
6122 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6123 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006124 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6125 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006126 goto cleanup;
6127 }
6128 }
6129 /* else {
6130 * strictly, the default prefix would point to the deviation module, but the value should actually
6131 * match the default string in the original module (usually unprefixed), so in this case we do not check
6132 * the module of the default case, just matching its name */
6133 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006135 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6136 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006137 goto cleanup;
6138 }
6139 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6140 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6141 break;
6142 default:
6143 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006144 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006145 goto cleanup;
6146 }
6147 }
6148
6149 break;
6150 case LYS_DEV_REPLACE:
6151 d_rpl = (struct lysp_deviate_rpl*)d;
6152
6153 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006154 if (d_rpl->type) {
6155 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6156 /* type is mandatory, so checking for its presence is not necessary */
6157 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006158
6159 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6160 /* the target has default from the previous type - remove it */
6161 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006162 /* update the list of incomplete default values if needed */
6163 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6164
Radek Krejcia1911222019-07-22 17:24:50 +02006165 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6166 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6167 free(leaf->dflt);
6168 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006169 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006170 } else { /* LYS_LEAFLIST */
6171 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006172 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006173 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6174 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6175 free(llist->dflts[x]);
6176 }
6177 LY_ARRAY_FREE(llist->dflts);
6178 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006179 LY_ARRAY_FREE(llist->dflts_mods);
6180 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006181 }
6182 }
6183 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006184 /* there is no default value, do not set changed_type after type compilation
6185 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006186 changed_type = -1;
6187 }
Radek Krejciec4da802019-05-02 13:02:41 +02006188 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 +02006189 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006190 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006191
6192 /* [units-stmt] */
6193 if (d_rpl->units) {
6194 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6195 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6196 units, "replacing", "units", d_rpl->units);
6197
6198 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6199 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6200 }
6201
6202 /* [default-stmt] */
6203 if (d_rpl->dflt) {
6204 switch (devs[u]->target->nodetype) {
6205 case LYS_LEAF:
6206 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6207 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006208 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006209 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006210 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6211 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6212 dflt = d_rpl->dflt;
6213 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006214 break;
6215 case LYS_CHOICE:
6216 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006217 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 +01006218 goto cleanup;
6219 }
6220 break;
6221 default:
6222 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006223 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006224 goto cleanup;
6225 }
6226 }
6227
6228 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006229 if (d_rpl->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01006230 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006231 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006232 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6233 goto cleanup;
6234 }
6235 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006236 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006237 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6238 goto cleanup;
6239 }
Radek Krejci327de162019-06-14 12:52:07 +02006240 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006241 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006242
6243 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006244 if (d_rpl->flags & LYS_MAND_MASK) {
6245 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006246 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006247 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6248 goto cleanup;
6249 }
Radek Krejci327de162019-06-14 12:52:07 +02006250 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006251 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006252
6253 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006254 if (d_rpl->flags & LYS_SET_MIN) {
6255 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6256 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6257 /* change value */
6258 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6259 } else if (devs[u]->target->nodetype == LYS_LIST) {
6260 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6261 /* change value */
6262 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6263 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006264 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006265 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6266 goto cleanup;
6267 }
6268 if (d_rpl->min) {
6269 devs[u]->target->flags |= LYS_MAND_TRUE;
6270 }
6271 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006272
6273 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006274 if (d_rpl->flags & LYS_SET_MAX) {
6275 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6276 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6277 /* change value */
6278 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6279 } else if (devs[u]->target->nodetype == LYS_LIST) {
6280 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6281 /* change value */
6282 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6283 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006284 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006285 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6286 goto cleanup;
6287 }
6288 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006289
6290 break;
6291 default:
6292 LOGINT(ctx->ctx);
6293 goto cleanup;
6294 }
6295 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006296
Radek Krejci33f72892019-02-21 10:36:58 +01006297 /* final check when all deviations of a single target node are applied */
6298
Radek Krejci551b12c2019-02-19 16:11:21 +01006299 /* check min-max compatibility */
6300 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6301 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6302 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6303 } else if (devs[u]->target->nodetype == LYS_LIST) {
6304 min = ((struct lysc_node_list*)devs[u]->target)->min;
6305 max = ((struct lysc_node_list*)devs[u]->target)->max;
6306 } else {
6307 min = max = 0;
6308 }
6309 if (min > max) {
6310 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 +02006311 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006312 goto cleanup;
6313 }
6314
Radek Krejcia1911222019-07-22 17:24:50 +02006315 if (dflt) {
6316 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006317 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006318 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006319 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6320 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006321 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006322 leaf->dflt->realtype->refcount++;
6323 if (err) {
6324 ly_err_print(err);
6325 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6326 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6327 ly_err_free(err);
6328 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006329 if (rc == LY_EINCOMPLETE) {
6330 /* postpone default compilation when the tree is complete */
6331 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6332
6333 /* but in general result is so far ok */
6334 rc = LY_SUCCESS;
6335 }
Radek Krejcia1911222019-07-22 17:24:50 +02006336 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006337 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006338 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6339 int dynamic;
6340 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006341 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006342
6343 /* update the list of incomplete default values if needed */
6344 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6345
6346 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006347 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6348 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6349 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006350 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6351 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006352 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006353 leaf->dflt->realtype->refcount++;
6354 if (err) {
6355 ly_err_print(err);
6356 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6357 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6358 ly_err_free(err);
6359 }
6360 if (dynamic) {
6361 free((void*)dflt);
6362 }
Radek Krejcia1911222019-07-22 17:24:50 +02006363 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006364 if (rc == LY_EINCOMPLETE) {
6365 /* postpone default compilation when the tree is complete */
6366 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6367
6368 /* but in general result is so far ok */
6369 rc = LY_SUCCESS;
6370 }
6371 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006372 } else { /* LYS_LEAFLIST */
6373 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006374 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 +02006375 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6376 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6377 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006378 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6379 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006380 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6381 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006382 llist->dflts[x]->realtype->refcount++;
6383 if (err) {
6384 ly_err_print(err);
6385 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6386 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6387 dflt, err->msg);
6388 ly_err_free(err);
6389 }
6390 if (dynamic) {
6391 free((void*)dflt);
6392 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006393 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006394 if (rc == LY_EINCOMPLETE) {
6395 /* postpone default compilation when the tree is complete */
6396 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6397
6398 /* but in general result is so far ok */
6399 rc = LY_SUCCESS;
6400 }
Radek Krejcia1911222019-07-22 17:24:50 +02006401 LY_CHECK_GOTO(rc, cleanup);
6402 }
6403 }
6404 }
6405
Radek Krejci551b12c2019-02-19 16:11:21 +01006406 /* check mandatory - default compatibility */
6407 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6408 && (devs[u]->target->flags & LYS_SET_DFLT)
6409 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6410 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006411 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006412 goto cleanup;
6413 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6414 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6415 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006416 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 +01006417 goto cleanup;
6418 }
6419 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6420 /* mandatory node under a default case */
6421 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006422 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6423 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006424 goto cleanup;
6425 }
Radek Krejci33f72892019-02-21 10:36:58 +01006426
Michal Vasko57c10cd2020-05-27 15:57:11 +02006427 /* add this module into the target module deviated_by */
6428 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup);
6429 *dev_mod = mod_p->mod;
6430
Radek Krejci327de162019-06-14 12:52:07 +02006431 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006432 }
6433
Radek Krejci327de162019-06-14 12:52:07 +02006434 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006435 ret = LY_SUCCESS;
6436
6437cleanup:
6438 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6439 LY_ARRAY_FREE(devs[u]->deviates);
6440 free(devs[u]);
6441 }
6442 free(devs);
6443 ly_set_erase(&targets, NULL);
6444 ly_set_erase(&devs_p, NULL);
6445
6446 return ret;
6447}
6448
Radek Krejcib56c7502019-02-13 14:19:54 +01006449/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006450 * @brief Compile the given YANG submodule into the main module.
6451 * @param[in] ctx Compile context
6452 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006453 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6454 */
6455LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006456lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006457{
6458 unsigned int u;
6459 LY_ERR ret = LY_SUCCESS;
6460 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006461 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006462 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006463 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006464
Radek Krejci0af46292019-01-11 16:02:31 +01006465 if (!mainmod->mod->off_features) {
6466 /* features are compiled directly into the compiled module structure,
6467 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6468 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006469 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6470 LY_CHECK_GOTO(ret, error);
6471 }
Radek Krejci0af46292019-01-11 16:02:31 +01006472
Radek Krejci327de162019-06-14 12:52:07 +02006473 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006474 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006475 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006476
Radek Krejci474f9b82019-07-24 11:36:37 +02006477 /* data nodes */
6478 LY_LIST_FOR(submod->data, node_p) {
6479 ret = lys_compile_node(ctx, node_p, NULL, 0);
6480 LY_CHECK_GOTO(ret, error);
6481 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006482
Radek Krejciec4da802019-05-02 13:02:41 +02006483 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6484 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006485
Radek Krejcid05cbd92018-12-05 14:26:40 +01006486error:
6487 return ret;
6488}
6489
Radek Krejci335332a2019-09-05 13:03:35 +02006490static void *
6491lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6492{
6493 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6494 if (substmts[u].stmt == stmt) {
6495 return substmts[u].storage;
6496 }
6497 }
6498 return NULL;
6499}
6500
6501LY_ERR
6502lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6503{
6504 LY_ERR ret = LY_EVALID, r;
6505 unsigned int u;
6506 struct lysp_stmt *stmt;
6507 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006508
6509 /* check for invalid substatements */
6510 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006511 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6512 continue;
6513 }
Radek Krejci335332a2019-09-05 13:03:35 +02006514 for (u = 0; substmts[u].stmt; ++u) {
6515 if (substmts[u].stmt == stmt->kw) {
6516 break;
6517 }
6518 }
6519 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006520 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6521 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006522 goto cleanup;
6523 }
Radek Krejci335332a2019-09-05 13:03:35 +02006524 }
6525
Radek Krejciad5963b2019-09-06 16:03:05 +02006526 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6527
Radek Krejci335332a2019-09-05 13:03:35 +02006528 /* keep order of the processing the same as the order in the defined substmts,
6529 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6530 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006531 int stmt_present = 0;
6532
Radek Krejci335332a2019-09-05 13:03:35 +02006533 for (stmt = ext->child; stmt; stmt = stmt->next) {
6534 if (substmts[u].stmt != stmt->kw) {
6535 continue;
6536 }
6537
Radek Krejciad5963b2019-09-06 16:03:05 +02006538 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006539 if (substmts[u].storage) {
6540 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006541 case LY_STMT_STATUS:
6542 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6543 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6544 break;
6545 case LY_STMT_UNITS: {
6546 const char **units;
6547
6548 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6549 /* single item */
6550 if (*((const char **)substmts[u].storage)) {
6551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6552 goto cleanup;
6553 }
6554 units = (const char **)substmts[u].storage;
6555 } else {
6556 /* sized array */
6557 const char ***units_array = (const char ***)substmts[u].storage;
6558 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6559 }
6560 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6561 break;
6562 }
Radek Krejci335332a2019-09-05 13:03:35 +02006563 case LY_STMT_TYPE: {
6564 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6565 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6566
6567 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6568 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006569 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006570 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6571 goto cleanup;
6572 }
6573 compiled = substmts[u].storage;
6574 } else {
6575 /* sized array */
6576 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6577 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6578 compiled = (void*)type;
6579 }
6580
Radek Krejciad5963b2019-09-06 16:03:05 +02006581 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006582 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6583 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006584 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6585 lysp_type_free(ctx->ctx, parsed);
6586 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006587 break;
6588 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006589 case LY_STMT_IF_FEATURE: {
6590 struct lysc_iffeature *iff = NULL;
6591
6592 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6593 /* single item */
6594 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6595 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6596 goto cleanup;
6597 }
6598 iff = (struct lysc_iffeature*)substmts[u].storage;
6599 } else {
6600 /* sized array */
6601 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6602 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6603 }
6604 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6605 break;
6606 }
6607 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6608 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006609 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006610 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.",
6611 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006612 goto cleanup;
6613 }
6614 }
Radek Krejci335332a2019-09-05 13:03:35 +02006615 }
Radek Krejci335332a2019-09-05 13:03:35 +02006616
Radek Krejciad5963b2019-09-06 16:03:05 +02006617 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6618 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6619 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6620 goto cleanup;
6621 }
Radek Krejci335332a2019-09-05 13:03:35 +02006622 }
6623
6624 ret = LY_SUCCESS;
6625
6626cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006627 return ret;
6628}
6629
Michal Vasko175012e2019-11-06 15:49:14 +01006630/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006631 * @brief Check when for cyclic dependencies.
6632 * @param[in] set Set with all the referenced nodes.
6633 * @param[in] node Node whose "when" referenced nodes are in @p set.
6634 * @return LY_ERR value
6635 */
6636static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006637lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006638{
6639 struct lyxp_set tmp_set;
6640 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006641 uint32_t i, j;
6642 LY_ARRAY_SIZE_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006643 int idx;
6644 struct lysc_when *when;
6645 LY_ERR ret = LY_SUCCESS;
6646
6647 memset(&tmp_set, 0, sizeof tmp_set);
6648
6649 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006650 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006651 xp_scnode = &set->val.scnodes[i];
6652
Michal Vasko5c4e5892019-11-14 12:31:38 +01006653 if (xp_scnode->in_ctx != -1) {
6654 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006655 xp_scnode->in_ctx = 1;
6656 }
6657 }
6658
6659 for (i = 0; i < set->used; ++i) {
6660 xp_scnode = &set->val.scnodes[i];
6661 if (xp_scnode->in_ctx != 1) {
6662 /* already checked */
6663 continue;
6664 }
6665
Michal Vasko1bf09392020-03-27 12:38:10 +01006666 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006667 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006668 /* no when to check */
6669 xp_scnode->in_ctx = 0;
6670 continue;
6671 }
6672
6673 node = xp_scnode->scnode;
6674 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006675 LY_ARRAY_FOR(node->when, u) {
6676 when = node->when[u];
Michal Vasko52927e22020-03-16 17:26:14 +01006677 ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006678 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6679 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006680 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006681 goto cleanup;
6682 }
6683
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006684 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006685 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006686 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006687 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006688 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 +01006689 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006690 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 +01006691 ret = LY_EVALID;
6692 goto cleanup;
6693 }
6694
6695 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006696 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006697 } else {
6698 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006699 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006700 }
6701 }
6702
6703 /* merge this set into the global when set */
6704 lyxp_set_scnode_merge(set, &tmp_set);
6705 }
6706
6707 /* check when of non-data parents as well */
6708 node = node->parent;
6709 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
6710
Michal Vasko251f56e2019-11-14 16:06:47 +01006711 /* this node when was checked (xp_scnode could have been reallocd) */
6712 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006713 }
6714
6715cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006716 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006717 return ret;
6718}
6719
6720/**
Michal Vasko175012e2019-11-06 15:49:14 +01006721 * @brief Check when/must expressions of a node on a compiled schema tree.
6722 * @param[in] ctx Compile context.
6723 * @param[in] node Node to check.
6724 * @return LY_ERR value
6725 */
6726static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006727lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01006728{
Michal Vasko175012e2019-11-06 15:49:14 +01006729 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006730 uint32_t i;
6731 LY_ARRAY_SIZE_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01006732 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01006733 struct lysc_when **when = NULL;
6734 struct lysc_must *musts = NULL;
6735 LY_ERR ret = LY_SUCCESS;
6736
6737 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01006738 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko175012e2019-11-06 15:49:14 +01006739
6740 switch (node->nodetype) {
6741 case LYS_CONTAINER:
6742 when = ((struct lysc_node_container *)node)->when;
6743 musts = ((struct lysc_node_container *)node)->musts;
6744 break;
6745 case LYS_CHOICE:
6746 when = ((struct lysc_node_choice *)node)->when;
6747 break;
6748 case LYS_LEAF:
6749 when = ((struct lysc_node_leaf *)node)->when;
6750 musts = ((struct lysc_node_leaf *)node)->musts;
6751 break;
6752 case LYS_LEAFLIST:
6753 when = ((struct lysc_node_leaflist *)node)->when;
6754 musts = ((struct lysc_node_leaflist *)node)->musts;
6755 break;
6756 case LYS_LIST:
6757 when = ((struct lysc_node_list *)node)->when;
6758 musts = ((struct lysc_node_list *)node)->musts;
6759 break;
6760 case LYS_ANYXML:
6761 case LYS_ANYDATA:
6762 when = ((struct lysc_node_anydata *)node)->when;
6763 musts = ((struct lysc_node_anydata *)node)->musts;
6764 break;
6765 case LYS_CASE:
6766 when = ((struct lysc_node_case *)node)->when;
6767 break;
6768 case LYS_NOTIF:
6769 musts = ((struct lysc_notif *)node)->musts;
6770 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006771 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01006772 case LYS_ACTION:
6773 /* first process input musts */
6774 musts = ((struct lysc_action *)node)->input.musts;
6775 break;
Michal Vasko175012e2019-11-06 15:49:14 +01006776 default:
6777 /* nothing to check */
6778 break;
6779 }
6780
Michal Vasko175012e2019-11-06 15:49:14 +01006781 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006782 LY_ARRAY_FOR(when, u) {
Michal Vasko004d3152020-06-11 19:59:22 +02006783 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 +02006784 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01006785 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006786 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006787 goto cleanup;
6788 }
6789
Michal Vaskodc052f32019-11-07 11:11:38 +01006790 ctx->path[0] = '\0';
6791 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006792 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006793 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006794 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
6795 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01006796
Michal Vaskoecd62de2019-11-13 12:35:11 +01006797 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006798 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01006799 schema->name);
6800 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006801
6802 /* check dummy node accessing */
6803 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006804 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01006805 ret = LY_EVALID;
6806 goto cleanup;
6807 }
Michal Vasko175012e2019-11-06 15:49:14 +01006808 }
6809 }
6810
Michal Vaskoecd62de2019-11-13 12:35:11 +01006811 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02006812 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01006813 LY_CHECK_GOTO(ret, cleanup);
6814
Michal Vaskod3678892020-05-21 10:06:58 +02006815 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006816 }
6817
Michal Vasko5d8756a2019-11-07 15:21:00 +01006818check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01006819 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006820 LY_ARRAY_FOR(musts, u) {
6821 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 +01006822 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006823 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01006824 goto cleanup;
6825 }
6826
Michal Vaskodc052f32019-11-07 11:11:38 +01006827 ctx->path[0] = '\0';
6828 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006829 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01006830 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006831 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01006832 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006833 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
6834 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01006835 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01006836 }
6837 }
6838
Michal Vaskod3678892020-05-21 10:06:58 +02006839 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006840 }
6841
Michal Vasko1bf09392020-03-27 12:38:10 +01006842 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01006843 /* now check output musts */
6844 input_done = 1;
6845 musts = ((struct lysc_action *)node)->output.musts;
6846 opts = LYXP_SCNODE_OUTPUT;
6847 goto check_musts;
6848 }
6849
Michal Vasko175012e2019-11-06 15:49:14 +01006850cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02006851 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01006852 return ret;
6853}
6854
Michal Vasko8d544252020-03-02 10:19:52 +01006855static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006856lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
6857{
6858 const struct lysc_node *target = NULL;
6859 struct ly_path *p;
6860 struct lysc_type *type;
6861
6862 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6863
6864 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02006865 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
6866 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
6867 lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02006868
6869 /* get the target node */
6870 target = p[LY_ARRAY_SIZE(p) - 1].node;
6871 ly_path_free(node->module->ctx, p);
6872
6873 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
6874 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6875 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
6876 lref->path->expr, lys_nodetype2str(target->nodetype));
6877 return LY_EVALID;
6878 }
6879
6880 /* check status */
6881 ctx->path[0] = '\0';
6882 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
6883 ctx->path_len = strlen(ctx->path);
6884 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
6885 return LY_EVALID;
6886 }
6887 ctx->path_len = 1;
6888 ctx->path[1] = '\0';
6889
6890 /* check config */
6891 if (lref->require_instance && (node->flags & LYS_CONFIG_W)) {
6892 if (target->flags & LYS_CONFIG_R) {
6893 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
6894 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
6895 return LY_EVALID;
6896 }
6897 }
6898
6899 /* store the target's type and check for circular chain of leafrefs */
6900 lref->realtype = ((struct lysc_node_leaf *)target)->type;
6901 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
6902 if (type == (struct lysc_type *)lref) {
6903 /* circular chain detected */
6904 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6905 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
6906 return LY_EVALID;
6907 }
6908 }
6909
6910 /* check if leafref and its target are under common if-features */
6911 if (lys_compile_leafref_features_validate(node, target)) {
6912 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
6913 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
6914 " features applicable to the leafref itself.", lref->path->expr);
6915 return LY_EVALID;
6916 }
6917
6918 return LY_SUCCESS;
6919}
6920
6921static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01006922lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
6923{
6924 struct lysc_ext_instance *ext;
6925 struct lysp_ext_instance *ext_p = NULL;
6926 struct lysp_stmt *stmt;
6927 const struct lys_module *ext_mod;
6928 LY_ERR ret = LY_SUCCESS;
6929
6930 /* create the parsed extension instance manually */
6931 ext_p = calloc(1, sizeof *ext_p);
6932 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
6933 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
6934 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
6935 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
6936 ext_p->insubstmt_index = 0;
6937
6938 stmt = calloc(1, sizeof *ext_p->child);
6939 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
6940 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
6941 stmt->kw = LY_STMT_TYPE;
6942 ext_p->child = stmt;
6943
6944 /* allocate new extension instance */
6945 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
6946
6947 /* manually get extension definition module */
6948 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
6949
6950 /* compile the extension instance */
6951 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
6952
6953cleanup:
6954 lysp_ext_instance_free(ctx->ctx, ext_p);
6955 free(ext_p);
6956 return ret;
6957}
6958
Michal Vasko004d3152020-06-11 19:59:22 +02006959static LY_ERR
6960lys_compile_unres(struct lysc_ctx *ctx)
6961{
6962 struct lysc_node *node;
6963 struct lysc_type *type, *typeiter;
6964 struct lysc_type_leafref *lref;
6965 LY_ARRAY_SIZE_TYPE v;
6966 uint32_t i;
6967
6968 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6969 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6970 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
6971 for (i = 0; i < ctx->leafrefs.count; ++i) {
6972 node = ctx->leafrefs.objs[i];
6973 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
6974 type = ((struct lysc_node_leaf *)node)->type;
6975 if (type->basetype == LY_TYPE_LEAFREF) {
6976 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
6977 } else if (type->basetype == LY_TYPE_UNION) {
6978 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
6979 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6980 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
6981 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
6982 }
6983 }
6984 }
6985 }
6986 for (i = 0; i < ctx->leafrefs.count; ++i) {
6987 /* store pointer to the real type */
6988 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
6989 if (type->basetype == LY_TYPE_LEAFREF) {
6990 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6991 typeiter->basetype == LY_TYPE_LEAFREF;
6992 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6993 ((struct lysc_type_leafref*)type)->realtype = typeiter;
6994 } else if (type->basetype == LY_TYPE_UNION) {
6995 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6996 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6997 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6998 typeiter->basetype == LY_TYPE_LEAFREF;
6999 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7000 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7001 }
7002 }
7003 }
7004 }
7005
7006 /* check xpath */
7007 for (i = 0; i < ctx->xpath.count; ++i) {
7008 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7009 }
7010
7011 /* finish incomplete default values compilation */
7012 for (i = 0; i < ctx->dflts.count; ++i) {
7013 struct ly_err_item *err = NULL;
7014 struct lysc_incomplete_dflt *r = ctx->dflts.objs[i];
7015 LY_ERR ret;
7016 ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
7017 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7018 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7019 if (err) {
7020 ly_err_print(err);
7021 ctx->path[0] = '\0';
7022 lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7023 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7024 "Invalid default - value does not fit the type (%s).", err->msg);
7025 ly_err_free(err);
7026 }
7027 LY_CHECK_RET(ret);
7028 }
7029
7030 return LY_SUCCESS;
7031}
7032
Radek Krejci19a96102018-11-15 13:38:09 +01007033LY_ERR
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007034lys_compile(struct lys_module **mod, int options)
Radek Krejci19a96102018-11-15 13:38:09 +01007035{
7036 struct lysc_ctx ctx = {0};
7037 struct lysc_module *mod_c;
7038 struct lysp_module *sp;
7039 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007040 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007041 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007042 struct lys_module *m;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007043 LY_ARRAY_SIZE_TYPE u, v;
7044 uint32_t i;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007045 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007046
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007047 LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007048
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007049 if (!(*mod)->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01007050 /* just imported modules are not compiled */
7051 return LY_SUCCESS;
7052 }
7053
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007054 sp = (*mod)->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01007055
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007056 ctx.ctx = (*mod)->ctx;
7057 ctx.mod = *mod;
7058 ctx.mod_def = *mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007059 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007060 ctx.path_len = 1;
7061 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007062
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007063 (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
7064 LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
7065 mod_c->mod = *mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007066
Radek Krejciec4da802019-05-02 13:02:41 +02007067 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007068 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007069 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007070 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7071 }
Radek Krejci0935f412019-08-20 16:15:18 +02007072
7073 /* features */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007074 if ((*mod)->off_features) {
Radek Krejci0af46292019-01-11 16:02:31 +01007075 /* there is already precompiled array of features */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007076 mod_c->features = (*mod)->off_features;
7077 (*mod)->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007078 } else {
7079 /* features are compiled directly into the compiled module structure,
7080 * 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 +02007081 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007082 LY_CHECK_GOTO(ret, error);
7083 }
7084 /* finish feature compilation, not only for the main module, but also for the submodules.
7085 * Due to possible forward references, it must be done when all the features (including submodules)
7086 * are present. */
7087 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007088 ret = lys_feature_precompile_finish(&ctx, &sp->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, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007092 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007093 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007094 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007095 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007096 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7097 }
Radek Krejci327de162019-06-14 12:52:07 +02007098 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007099 }
Radek Krejci327de162019-06-14 12:52:07 +02007100 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007101
Radek Krejci0935f412019-08-20 16:15:18 +02007102 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02007103 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02007104 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007105 if (sp->identities) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007106 LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007107 }
Radek Krejci327de162019-06-14 12:52:07 +02007108 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007109
Radek Krejci95710c92019-02-11 15:49:55 +01007110 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007111 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007112 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007113 }
Radek Krejci95710c92019-02-11 15:49:55 +01007114
Radek Krejciec4da802019-05-02 13:02:41 +02007115 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7116 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007117
Radek Krejci95710c92019-02-11 15:49:55 +01007118 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007119 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007120 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007121 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007122 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007123
Radek Krejci474f9b82019-07-24 11:36:37 +02007124 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007125 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007126
Radek Krejci0935f412019-08-20 16:15:18 +02007127 /* extension instances TODO cover extension instances from submodules */
7128 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007129
Michal Vasko004d3152020-06-11 19:59:22 +02007130 /* finish compilation for all unresolved items in the context */
7131 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02007132
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007133 /* validate non-instantiated groupings from the parsed schema,
7134 * without it we would accept even the schemas with invalid grouping specification */
7135 ctx.options |= LYSC_OPT_GROUPING;
7136 LY_ARRAY_FOR(sp->groupings, u) {
7137 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007138 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007139 }
7140 }
7141 LY_LIST_FOR(sp->data, node_p) {
7142 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7143 LY_ARRAY_FOR(grps, u) {
7144 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007145 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007146 }
7147 }
7148 }
7149
Radek Krejci474f9b82019-07-24 11:36:37 +02007150 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7151 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7152 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7153 }
7154
Michal Vasko8d544252020-03-02 10:19:52 +01007155#if 0
7156 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7157 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7158 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7159 * the anotation definitions available in the internal schema structure. */
7160 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7161 if (lyp_add_ietf_netconf_annotations(mod)) {
7162 lys_free(mod, NULL, 1, 1);
7163 return NULL;
7164 }
7165 }
7166#endif
7167
7168 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007169 if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
7170 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01007171 }
7172
Radek Krejci1c0c3442019-07-23 16:08:47 +02007173 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007174 ly_set_erase(&ctx.xpath, NULL);
7175 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007176 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007177 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007178 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007179
Radek Krejciec4da802019-05-02 13:02:41 +02007180 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007181 lysp_module_free((*mod)->parsed);
7182 (*mod)->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01007183 }
7184
Radek Krejciec4da802019-05-02 13:02:41 +02007185 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007186 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007187 for (i = 0; i < ctx.ctx->list.count; ++i) {
7188 m = ctx.ctx->list.objs[i];
Radek Krejci95710c92019-02-11 15:49:55 +01007189 if (m->implemented == 2) {
7190 m->implemented = 1;
7191 }
7192 }
7193 }
7194
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007195 (*mod)->compiled = mod_c;
Radek Krejci19a96102018-11-15 13:38:09 +01007196 return LY_SUCCESS;
7197
7198error:
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007199 lys_feature_precompile_revert(&ctx, *mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007200 ly_set_erase(&ctx.dflts, free);
Michal Vasko004d3152020-06-11 19:59:22 +02007201 ly_set_erase(&ctx.xpath, NULL);
7202 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007203 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007204 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007205 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007206 lysc_module_free(mod_c, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007207 (*mod)->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01007208
7209 /* revert compilation of modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007210 for (i = 0; i < ctx.ctx->list.count; ++i) {
7211 m = ctx.ctx->list.objs[i];
Michal Vasko2947ce12019-12-16 10:37:19 +01007212 if ((m->implemented == 2) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007213 /* revert features list to the precompiled state */
7214 lys_feature_precompile_revert(&ctx, m);
7215 /* mark module as imported-only / not-implemented */
7216 m->implemented = 0;
7217 /* free the compiled version of the module */
7218 lysc_module_free(m->compiled, NULL);
7219 m->compiled = NULL;
7220 }
7221 }
7222
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007223 /* remove the module itself from the context and free it */
7224 ly_set_rm(&ctx.ctx->list, *mod, NULL);
7225 lys_module_free(*mod, NULL);
7226 *mod = NULL;
7227
Radek Krejci19a96102018-11-15 13:38:09 +01007228 return ret;
7229}