blob: a49a5de6a6f96b08dbea6308f4a624678e305d4a [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
15#include "common.h"
16
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 Krejcie7b95092019-05-15 11:03:07 +020025#include "dict.h"
26#include "log.h"
Radek Krejci0935f412019-08-20 16:15:18 +020027#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "set.h"
29#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020030#include "plugins_exts_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "tree.h"
32#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010033#include "tree_schema_internal.h"
34#include "xpath.h"
35
Michal Vasko5fe75f12020-03-02 13:52:37 +010036static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
37 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
38
Radek Krejci19a96102018-11-15 13:38:09 +010039/**
40 * @brief Duplicate string into dictionary
41 * @param[in] CTX libyang context of the dictionary.
42 * @param[in] ORIG String to duplicate.
43 * @param[out] DUP Where to store the result.
44 */
45#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
46
Radek Krejciec4da802019-05-02 13:02:41 +020047#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010048 if (ARRAY_P) { \
49 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020050 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010051 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
52 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020053 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010054 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
55 } \
56 }
57
Radek Krejciec4da802019-05-02 13:02:41 +020058#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010059 if (ARRAY_P) { \
60 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020061 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010062 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
63 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020064 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010065 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
66 } \
67 }
68
Radek Krejci0935f412019-08-20 16:15:18 +020069#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
70 if (EXTS_P) { \
71 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020072 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 +020073 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +010074 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 +020075 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
76 } \
77 }
78
Radek Krejciec4da802019-05-02 13:02:41 +020079#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010080 if (ARRAY_P) { \
81 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +020082 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010083 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
84 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020085 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010086 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
87 } \
88 }
89
Radek Krejciec4da802019-05-02 13:02:41 +020090#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010091 if (MEMBER_P) { \
92 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
93 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +020094 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010095 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
96 }
97
Radek Krejciec4da802019-05-02 13:02:41 +020098#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +010099 if (MEMBER_P) { \
100 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200101 LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100102 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200103 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100104 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
105 }
106
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100107#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100108 if (ARRAY) { \
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200109 for (LY_ARRAY_SIZE_TYPE u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100110 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100111 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
112 return LY_EVALID; \
113 } \
114 } \
115 }
116
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100117#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
118 if (ARRAY) { \
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200119 for (LY_ARRAY_SIZE_TYPE u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100120 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
121 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
122 return LY_EVALID; \
123 } \
124 } \
125 }
126
127struct lysc_ext *
128lysc_ext_dup(struct lysc_ext *orig)
129{
130 ++orig->refcount;
131 return orig;
132}
133
Radek Krejci19a96102018-11-15 13:38:09 +0100134static struct lysc_ext_instance *
135lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
136{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100137 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100138 (void) ctx;
139 (void) orig;
140 return NULL;
141}
142
Radek Krejcib56c7502019-02-13 14:19:54 +0100143/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200144 * @brief Add record into the compile context's list of incomplete default values.
145 * @param[in] ctx Compile context with the incomplete default values list.
146 * @param[in] context_node Context schema node to store in the record.
147 * @param[in] dflt Incomplete default value to store in the record.
148 * @param[in] dflt_mod Module of the default value definition to store in the record.
149 * @return LY_EMEM in case of memory allocation failure.
150 * @return LY_SUCCESS
151 */
152static LY_ERR
153lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
154{
155 struct lysc_incomplete_dflt *r;
156 r = malloc(sizeof *r);
157 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
158 r->context_node = context_node;
159 r->dflt = dflt;
160 r->dflt_mod = dflt_mod;
161 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
162
163 return LY_SUCCESS;
164}
165
166/**
167 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
168 * @param[in] ctx Compile context with the incomplete default values list.
169 * @param[in] dflt Incomplete default values identifying the record to remove.
170 */
171static void
172lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
173{
174 unsigned int u;
175 struct lysc_incomplete_dflt *r;
176
177 for (u = 0; u < ctx->dflts.count; ++u) {
178 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
179 if (r->dflt == dflt) {
180 free(ctx->dflts.objs[u]);
181 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
182 --ctx->dflts.count;
183 return;
184 }
185 }
186}
187
Radek Krejci0e59c312019-08-15 15:34:15 +0200188void
Radek Krejci327de162019-06-14 12:52:07 +0200189lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
190{
191 int len;
192 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
193
194 if (!name) {
195 /* removing last path segment */
196 if (ctx->path[ctx->path_len - 1] == '}') {
197 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
198 if (ctx->path[ctx->path_len] == '=') {
199 ctx->path[ctx->path_len++] = '}';
200 } else {
201 /* not a top-level special tag, remove also preceiding '/' */
202 goto remove_nodelevel;
203 }
204 } else {
205remove_nodelevel:
206 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
207 if (ctx->path_len == 0) {
208 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200209 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200210 }
211 }
212 /* set new terminating NULL-byte */
213 ctx->path[ctx->path_len] = '\0';
214 } else {
215 if (ctx->path_len > 1) {
216 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
217 /* extension of the special tag */
218 nextlevel = 2;
219 --ctx->path_len;
220 } else {
221 /* there is already some path, so add next level */
222 nextlevel = 1;
223 }
224 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
225
226 if (nextlevel != 2) {
227 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
228 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200229 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200230 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200231 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 +0200232 }
233 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200234 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200235 }
Radek Krejciacc79042019-07-25 14:14:57 +0200236 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
237 /* output truncated */
238 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
239 } else {
240 ctx->path_len += len;
241 }
Radek Krejci327de162019-06-14 12:52:07 +0200242 }
243}
244
245/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100246 * @brief Duplicate the compiled pattern structure.
247 *
248 * Instead of duplicating memory, the reference counter in the @p orig is increased.
249 *
250 * @param[in] orig The pattern structure to duplicate.
251 * @return The duplicated structure to use.
252 */
Radek Krejci19a96102018-11-15 13:38:09 +0100253static struct lysc_pattern*
254lysc_pattern_dup(struct lysc_pattern *orig)
255{
256 ++orig->refcount;
257 return orig;
258}
259
Radek Krejcib56c7502019-02-13 14:19:54 +0100260/**
261 * @brief Duplicate the array of compiled patterns.
262 *
263 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
264 *
265 * @param[in] ctx Libyang context for logging.
266 * @param[in] orig The patterns sized array to duplicate.
267 * @return New sized array as a copy of @p orig.
268 * @return NULL in case of memory allocation error.
269 */
Radek Krejci19a96102018-11-15 13:38:09 +0100270static struct lysc_pattern**
271lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
272{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100273 struct lysc_pattern **dup = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200274 LY_ARRAY_SIZE_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100275
Radek Krejcib56c7502019-02-13 14:19:54 +0100276 assert(orig);
277
Radek Krejci19a96102018-11-15 13:38:09 +0100278 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
279 LY_ARRAY_FOR(orig, u) {
280 dup[u] = lysc_pattern_dup(orig[u]);
281 LY_ARRAY_INCREMENT(dup);
282 }
283 return dup;
284}
285
Radek Krejcib56c7502019-02-13 14:19:54 +0100286/**
287 * @brief Duplicate compiled range structure.
288 *
289 * @param[in] ctx Libyang context for logging.
290 * @param[in] orig The range structure to be duplicated.
291 * @return New compiled range structure as a copy of @p orig.
292 * @return NULL in case of memory allocation error.
293 */
Radek Krejci19a96102018-11-15 13:38:09 +0100294struct lysc_range*
295lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
296{
297 struct lysc_range *dup;
298 LY_ERR ret;
299
Radek Krejcib56c7502019-02-13 14:19:54 +0100300 assert(orig);
301
Radek Krejci19a96102018-11-15 13:38:09 +0100302 dup = calloc(1, sizeof *dup);
303 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
304 if (orig->parts) {
305 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
306 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
307 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
308 }
309 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
310 DUP_STRING(ctx, orig->emsg, dup->emsg);
311 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
312
313 return dup;
314cleanup:
315 free(dup);
316 (void) ret; /* set but not used due to the return type */
317 return NULL;
318}
319
Radek Krejcib56c7502019-02-13 14:19:54 +0100320/**
321 * @brief Stack for processing if-feature expressions.
322 */
Radek Krejci19a96102018-11-15 13:38:09 +0100323struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100324 int size; /**< number of items in the stack */
325 int index; /**< first empty item */
326 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100327};
328
Radek Krejcib56c7502019-02-13 14:19:54 +0100329/**
330 * @brief Add @ref ifftokens into the stack.
331 * @param[in] stack The if-feature stack to use.
332 * @param[in] value One of the @ref ifftokens to store in the stack.
333 * @return LY_EMEM in case of memory allocation error
334 * @return LY_ESUCCESS if the value successfully stored.
335 */
Radek Krejci19a96102018-11-15 13:38:09 +0100336static LY_ERR
337iff_stack_push(struct iff_stack *stack, uint8_t value)
338{
339 if (stack->index == stack->size) {
340 stack->size += 4;
341 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
342 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
343 }
344 stack->stack[stack->index++] = value;
345 return LY_SUCCESS;
346}
347
Radek Krejcib56c7502019-02-13 14:19:54 +0100348/**
349 * @brief Get (and remove) the last item form the stack.
350 * @param[in] stack The if-feature stack to use.
351 * @return The value from the top of the stack.
352 */
Radek Krejci19a96102018-11-15 13:38:09 +0100353static uint8_t
354iff_stack_pop(struct iff_stack *stack)
355{
Radek Krejcib56c7502019-02-13 14:19:54 +0100356 assert(stack && stack->index);
357
Radek Krejci19a96102018-11-15 13:38:09 +0100358 stack->index--;
359 return stack->stack[stack->index];
360}
361
Radek Krejcib56c7502019-02-13 14:19:54 +0100362/**
363 * @brief Clean up the stack.
364 * @param[in] stack The if-feature stack to use.
365 */
Radek Krejci19a96102018-11-15 13:38:09 +0100366static void
367iff_stack_clean(struct iff_stack *stack)
368{
369 stack->size = 0;
370 free(stack->stack);
371}
372
Radek Krejcib56c7502019-02-13 14:19:54 +0100373/**
374 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
375 * (libyang format of the if-feature expression).
376 * @param[in,out] list The 2bits array to modify.
377 * @param[in] op The operand (@ref ifftokens) to store.
378 * @param[in] pos Position (0-based) where to store the given @p op.
379 */
Radek Krejci19a96102018-11-15 13:38:09 +0100380static void
381iff_setop(uint8_t *list, uint8_t op, int pos)
382{
383 uint8_t *item;
384 uint8_t mask = 3;
385
386 assert(pos >= 0);
387 assert(op <= 3); /* max 2 bits */
388
389 item = &list[pos / 4];
390 mask = mask << 2 * (pos % 4);
391 *item = (*item) & ~mask;
392 *item = (*item) | (op << 2 * (pos % 4));
393}
394
Radek Krejcib56c7502019-02-13 14:19:54 +0100395#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
396#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100397
Radek Krejci0af46292019-01-11 16:02:31 +0100398/**
399 * @brief Find a feature of the given name and referenced in the given module.
400 *
401 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
402 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
403 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
404 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
405 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
406 * assigned till that time will be still valid.
407 *
408 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
409 * @param[in] name Name of the feature including possible prefix.
410 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
411 * @return Pointer to the feature structure if found, NULL otherwise.
412 */
Radek Krejci19a96102018-11-15 13:38:09 +0100413static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100414lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100415{
416 size_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200417 LY_ARRAY_SIZE_TYPE u;
Radek Krejci0af46292019-01-11 16:02:31 +0100418 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100419
420 for (i = 0; i < len; ++i) {
421 if (name[i] == ':') {
422 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100423 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100424 LY_CHECK_RET(!mod, NULL);
425
426 name = &name[i + 1];
427 len = len - i - 1;
428 }
429 }
430
431 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100432 if (mod->implemented) {
433 /* module is implemented so there is already the compiled schema */
434 flist = mod->compiled->features;
435 } else {
436 flist = mod->off_features;
437 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200438 LY_ARRAY_FOR(flist, u) {
439 f = &flist[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200440 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100441 return f;
442 }
443 }
444
445 return NULL;
446}
447
Michal Vasko8d544252020-03-02 10:19:52 +0100448/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100449 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
450 */
451static LY_ERR
452lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
453{
454 LY_ERR ret = LY_SUCCESS;
455
456 if (!ext_p->compiled) {
457 lysc_update_path(ctx, NULL, "{extension}");
458 lysc_update_path(ctx, NULL, ext_p->name);
459
460 /* compile the extension definition */
461 ext_p->compiled = calloc(1, sizeof **ext);
462 ext_p->compiled->refcount = 1;
463 DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name);
464 DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument);
465 ext_p->compiled->module = (struct lys_module *)ext_mod;
466 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
467
468 lysc_update_path(ctx, NULL, NULL);
469 lysc_update_path(ctx, NULL, NULL);
470
471 /* find extension definition plugin */
472 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
473 }
474
475 *ext = lysc_ext_dup(ext_p->compiled);
476
477done:
478 return ret;
479}
480
481/**
Michal Vasko8d544252020-03-02 10:19:52 +0100482 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
483 *
484 * @param[in] ctx Compilation context.
485 * @param[in] ext_p Parsed extension instance.
486 * @param[in,out] ext Prepared compiled extension instance.
487 * @param[in] parent Extension instance parent.
488 * @param[in] parent_type Extension instance parent type.
489 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
490 */
Radek Krejci19a96102018-11-15 13:38:09 +0100491static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100492lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
493 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100494{
Radek Krejci7c960162019-09-18 14:16:12 +0200495 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100496 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200497 size_t u;
498 LY_ARRAY_SIZE_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200499 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100500
501 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
502 ext->insubstmt = ext_p->insubstmt;
503 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200504 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200505 ext->parent = parent;
506 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100507
Radek Krejcif56e2a42019-09-09 14:15:25 +0200508 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200509
Radek Krejci19a96102018-11-15 13:38:09 +0100510 /* get module where the extension definition should be placed */
Radek Krejci7c960162019-09-18 14:16:12 +0200511 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u);
512 if (ext_p->yin) {
513 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
514 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
515 * YANG (import) prefix must be done here. */
516 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
517 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
518 u = 0;
519 } else if (ctx->mod_def->compiled) {
Radek Krejci7c960162019-09-18 14:16:12 +0200520 LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) {
521 if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) {
522 char *s;
523 asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]);
524 prefixed_name = lydict_insert_zc(ctx->ctx, s);
525 u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */
526 break;
527 }
528 }
529 }
530 if (!prefixed_name) {
531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
532 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
533 goto cleanup;
534 }
535 } else {
536 prefixed_name = ext_p->name;
537 }
538 lysc_update_path(ctx, NULL, prefixed_name);
539
Michal Vasko8d544252020-03-02 10:19:52 +0100540 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200541 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100542 if (!ext_mod) {
543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
544 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
545 goto cleanup;
546 } else if (!ext_mod->parsed->extensions) {
547 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
548 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
549 prefixed_name, ext_mod->name);
550 goto cleanup;
551 }
Radek Krejci7c960162019-09-18 14:16:12 +0200552 }
553 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200554
Michal Vasko5fe75f12020-03-02 13:52:37 +0100555 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200556 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
557 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100558 /* compile extension definition and assign it */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200559 LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def));
Radek Krejci19a96102018-11-15 13:38:09 +0100560 break;
561 }
562 }
Radek Krejci7c960162019-09-18 14:16:12 +0200563 if (!ext->def) {
564 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
565 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
566 goto cleanup;
567 }
Radek Krejci0935f412019-08-20 16:15:18 +0200568
Radek Krejcif56e2a42019-09-09 14:15:25 +0200569 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
570 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
571 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200572 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200573 /* Schema was parsed from YIN and an argument is expected, ... */
574 struct lysp_stmt *stmt = NULL;
575
576 if (ext->def->flags & LYS_YINELEM_TRUE) {
577 /* ... argument was the first XML child element */
578 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
579 /* TODO check namespace of the statement */
580 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
581 stmt = ext_p->child;
582 }
583 }
584 } else {
585 /* ... argument was one of the XML attributes which are represented as child stmt
586 * with LYS_YIN_ATTR flag */
587 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
588 if (!strcmp(stmt->stmt, ext->def->argument)) {
589 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200590 break;
591 }
592 }
593 }
594 if (!stmt) {
595 /* missing extension's argument */
596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200597 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
598 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200599
600 }
601 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
602 stmt->flags |= LYS_YIN_ARGUMENT;
603 }
Radek Krejci7c960162019-09-18 14:16:12 +0200604 if (prefixed_name != ext_p->name) {
605 lydict_remove(ctx->ctx, ext_p->name);
606 ext_p->name = prefixed_name;
607 if (!ext_p->argument && ext->argument) {
608 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
609 }
610 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200611
Radek Krejci0935f412019-08-20 16:15:18 +0200612 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200613 if (ext->argument) {
614 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
615 }
Radek Krejci7c960162019-09-18 14:16:12 +0200616 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200617 if (ext->argument) {
618 lysc_update_path(ctx, NULL, NULL);
619 }
Radek Krejci0935f412019-08-20 16:15:18 +0200620 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200621 ext_p->compiled = ext;
622
Radek Krejci7c960162019-09-18 14:16:12 +0200623 ret = LY_SUCCESS;
624cleanup:
625 if (prefixed_name && prefixed_name != ext_p->name) {
626 lydict_remove(ctx->ctx, prefixed_name);
627 }
628
Radek Krejcif56e2a42019-09-09 14:15:25 +0200629 lysc_update_path(ctx, NULL, NULL);
630 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200631
Radek Krejci7c960162019-09-18 14:16:12 +0200632 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200633}
634
635/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100636 * @brief Compile information from the if-feature statement
637 * @param[in] ctx Compile context.
638 * @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 +0100639 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
640 * @return LY_ERR value.
641 */
Radek Krejci19a96102018-11-15 13:38:09 +0100642static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200643lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100644{
645 const char *c = *value;
646 int r, rc = EXIT_FAILURE;
647 int i, j, last_not, checkversion = 0;
648 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
649 uint8_t op;
650 struct iff_stack stack = {0, 0, NULL};
651 struct lysc_feature *f;
652
653 assert(c);
654
655 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
656 for (i = j = last_not = 0; c[i]; i++) {
657 if (c[i] == '(') {
658 j++;
659 checkversion = 1;
660 continue;
661 } else if (c[i] == ')') {
662 j--;
663 continue;
664 } else if (isspace(c[i])) {
665 checkversion = 1;
666 continue;
667 }
668
669 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 +0200670 int sp;
671 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
672 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
674 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
675 return LY_EVALID;
676 } else if (!isspace(c[i + r])) {
677 /* feature name starting with the not/and/or */
678 last_not = 0;
679 f_size++;
680 } else if (c[i] == 'n') { /* not operation */
681 if (last_not) {
682 /* double not */
683 expr_size = expr_size - 2;
684 last_not = 0;
685 } else {
686 last_not = 1;
687 }
688 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200689 if (f_exp != f_size) {
690 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
691 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
692 return LY_EVALID;
693 }
Radek Krejci19a96102018-11-15 13:38:09 +0100694 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200695
Radek Krejci19a96102018-11-15 13:38:09 +0100696 /* not a not operation */
697 last_not = 0;
698 }
699 i += r;
700 } else {
701 f_size++;
702 last_not = 0;
703 }
704 expr_size++;
705
706 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200707 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100708 i--;
709 break;
710 }
711 i++;
712 }
713 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200714 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100715 /* not matching count of ( and ) */
716 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
717 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
718 return LY_EVALID;
719 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200720 if (f_exp != f_size) {
721 /* features do not match the needed arguments for the logical operations */
722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
723 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
724 "the required number of operands for the operations.", *value);
725 return LY_EVALID;
726 }
Radek Krejci19a96102018-11-15 13:38:09 +0100727
728 if (checkversion || expr_size > 1) {
729 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100730 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
732 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
733 return LY_EVALID;
734 }
735 }
736
737 /* allocate the memory */
738 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
739 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
740 stack.stack = malloc(expr_size * sizeof *stack.stack);
741 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
742
743 stack.size = expr_size;
744 f_size--; expr_size--; /* used as indexes from now */
745
746 for (i--; i >= 0; i--) {
747 if (c[i] == ')') {
748 /* push it on stack */
749 iff_stack_push(&stack, LYS_IFF_RP);
750 continue;
751 } else if (c[i] == '(') {
752 /* pop from the stack into result all operators until ) */
753 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
754 iff_setop(iff->expr, op, expr_size--);
755 }
756 continue;
757 } else if (isspace(c[i])) {
758 continue;
759 }
760
761 /* end of operator or operand -> find beginning and get what is it */
762 j = i + 1;
763 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
764 i--;
765 }
766 i++; /* go back by one step */
767
768 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
769 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
770 /* double not */
771 iff_stack_pop(&stack);
772 } else {
773 /* not has the highest priority, so do not pop from the stack
774 * as in case of AND and OR */
775 iff_stack_push(&stack, LYS_IFF_NOT);
776 }
777 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
778 /* as for OR - pop from the stack all operators with the same or higher
779 * priority and store them to the result, then push the AND to the stack */
780 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
781 op = iff_stack_pop(&stack);
782 iff_setop(iff->expr, op, expr_size--);
783 }
784 iff_stack_push(&stack, LYS_IFF_AND);
785 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
786 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
787 op = iff_stack_pop(&stack);
788 iff_setop(iff->expr, op, expr_size--);
789 }
790 iff_stack_push(&stack, LYS_IFF_OR);
791 } else {
792 /* feature name, length is j - i */
793
794 /* add it to the expression */
795 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
796
797 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100798 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
799 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
800 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
801 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100802 iff->features[f_size] = f;
803 LY_ARRAY_INCREMENT(iff->features);
804 f_size--;
805 }
806 }
807 while (stack.index) {
808 op = iff_stack_pop(&stack);
809 iff_setop(iff->expr, op, expr_size--);
810 }
811
812 if (++expr_size || ++f_size) {
813 /* not all expected operators and operands found */
814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
815 "Invalid value \"%s\" of if-feature - processing error.", *value);
816 rc = LY_EINT;
817 } else {
818 rc = LY_SUCCESS;
819 }
820
821error:
822 /* cleanup */
823 iff_stack_clean(&stack);
824
825 return rc;
826}
827
Radek Krejcib56c7502019-02-13 14:19:54 +0100828/**
Michal Vasko175012e2019-11-06 15:49:14 +0100829 * @brief Get the XPath context node for the given schema node.
830 * @param[in] start The schema node where the XPath expression appears.
831 * @return The context node to evaluate XPath expression in given schema node.
832 * @return NULL in case the context node is the root node.
833 */
834static struct lysc_node *
835lysc_xpath_context(struct lysc_node *start)
836{
Michal Vasko1bf09392020-03-27 12:38:10 +0100837 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 +0100838 start = start->parent);
839 return start;
840}
841
842/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100843 * @brief Compile information from the when statement
844 * @param[in] ctx Compile context.
845 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100846 * @param[in] flags Flags of the node with the "when" defiition.
847 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100848 * @param[out] when Pointer where to store pointer to the created compiled when structure.
849 * @return LY_ERR value.
850 */
Radek Krejci19a96102018-11-15 13:38:09 +0100851static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100852lys_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 +0100853{
Radek Krejci19a96102018-11-15 13:38:09 +0100854 LY_ERR ret = LY_SUCCESS;
855
Radek Krejci00b874b2019-02-12 10:54:50 +0100856 *when = calloc(1, sizeof **when);
857 (*when)->refcount = 1;
858 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200859 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100860 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100861 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
862 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
863 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200864 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100865 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100866
867done:
868 return ret;
869}
870
Radek Krejcib56c7502019-02-13 14:19:54 +0100871/**
872 * @brief Compile information from the must statement
873 * @param[in] ctx Compile context.
874 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100875 * @param[in,out] must Prepared (empty) compiled must structure to fill.
876 * @return LY_ERR value.
877 */
Radek Krejci19a96102018-11-15 13:38:09 +0100878static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200879lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100880{
Radek Krejci19a96102018-11-15 13:38:09 +0100881 LY_ERR ret = LY_SUCCESS;
882
883 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
884 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200885 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100886 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
887 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100888 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
889 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200890 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100891
892done:
893 return ret;
894}
895
Radek Krejcib56c7502019-02-13 14:19:54 +0100896/**
897 * @brief Compile information from the import statement
898 * @param[in] ctx Compile context.
899 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100900 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
901 * @return LY_ERR value.
902 */
Radek Krejci19a96102018-11-15 13:38:09 +0100903static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200904lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100905{
Radek Krejci19a96102018-11-15 13:38:09 +0100906 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100907 LY_ERR ret = LY_SUCCESS;
908
909 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200910 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100911 imp->module = imp_p->module;
912
Radek Krejci7f2a5362018-11-28 13:05:37 +0100913 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
914 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100915 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100916 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100917 /* try to use filepath if present */
918 if (imp->module->filepath) {
919 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
920 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100921 if (mod != imp->module) {
922 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100923 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100924 mod = NULL;
925 }
926 }
927 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100928 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100929 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 +0100930 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100931 return LY_ENOTFOUND;
932 }
933 }
Radek Krejci19a96102018-11-15 13:38:09 +0100934 }
935
936done:
937 return ret;
938}
939
Radek Krejcib56c7502019-02-13 14:19:54 +0100940/**
941 * @brief Compile information from the identity statement
942 *
943 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
944 *
945 * @param[in] ctx Compile context.
946 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100947 * @param[in] idents List of so far compiled identities to check the name uniqueness.
948 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
949 * @return LY_ERR value.
950 */
Radek Krejci19a96102018-11-15 13:38:09 +0100951static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200952lys_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 +0100953{
954 unsigned int u;
955 LY_ERR ret = LY_SUCCESS;
956
Radek Krejci327de162019-06-14 12:52:07 +0200957 lysc_update_path(ctx, NULL, ident_p->name);
958
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100959 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100960 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200961 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
962 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200963 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200964 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100965 /* 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 +0200966 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100967 ident->flags = ident_p->flags;
968
Radek Krejci327de162019-06-14 12:52:07 +0200969 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100970done:
971 return ret;
972}
973
Radek Krejcib56c7502019-02-13 14:19:54 +0100974/**
975 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
976 *
977 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
978 *
979 * @param[in] ctx Compile context for logging.
980 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
981 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
982 * @return LY_SUCCESS if everything is ok.
983 * @return LY_EVALID if the identity is derived from itself.
984 */
Radek Krejci38222632019-02-12 16:55:05 +0100985static LY_ERR
986lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
987{
988 LY_ERR ret = LY_EVALID;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200989 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +0100990 struct ly_set recursion = {0};
991 struct lysc_ident *drv;
992
993 if (!derived) {
994 return LY_SUCCESS;
995 }
996
997 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
998 if (ident == derived[u]) {
999 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1000 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1001 goto cleanup;
1002 }
1003 ly_set_add(&recursion, derived[u], 0);
1004 }
1005
1006 for (v = 0; v < recursion.count; ++v) {
1007 drv = recursion.objs[v];
1008 if (!drv->derived) {
1009 continue;
1010 }
1011 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
1012 if (ident == drv->derived[u]) {
1013 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1014 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1015 goto cleanup;
1016 }
1017 ly_set_add(&recursion, drv->derived[u], 0);
1018 }
1019 }
1020 ret = LY_SUCCESS;
1021
1022cleanup:
1023 ly_set_erase(&recursion, NULL);
1024 return ret;
1025}
1026
Radek Krejcia3045382018-11-22 14:30:31 +01001027/**
1028 * @brief Find and process the referenced base identities from another identity or identityref
1029 *
Radek Krejciaca74032019-06-04 08:53:06 +02001030 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001031 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1032 * to distinguish these two use cases.
1033 *
1034 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1035 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
1036 * @param[in] ident Referencing identity to work with.
1037 * @param[in] bases Array of bases of identityref to fill in.
1038 * @return LY_ERR value.
1039 */
Radek Krejci19a96102018-11-15 13:38:09 +01001040static LY_ERR
Radek Krejci0a33b042020-05-27 10:05:06 +02001041lys_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 +01001042{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001043 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001044 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001045 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001046 struct lysc_ident **idref;
1047
1048 assert(ident || bases);
1049
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001050 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001051 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1052 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1053 return LY_EVALID;
1054 }
1055
1056 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
1057 s = strchr(bases_p[u], ':');
1058 if (s) {
1059 /* prefixed identity */
1060 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001061 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001062 } else {
1063 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001064 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001065 }
1066 if (!mod) {
1067 if (ident) {
1068 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1069 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1070 } else {
1071 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1072 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1073 }
1074 return LY_EVALID;
1075 }
1076 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001077 if (mod->compiled && mod->compiled->identities) {
1078 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
1079 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001080 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +01001081 if (ident == &mod->compiled->identities[v]) {
1082 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1083 "Identity \"%s\" is derived from itself.", ident->name);
1084 return LY_EVALID;
1085 }
1086 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001087 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +01001088 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001089 *idref = ident;
1090 } else {
1091 /* we have match! store the found identity */
1092 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01001093 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001094 }
1095 break;
1096 }
1097 }
1098 }
1099 if (!idref || !(*idref)) {
1100 if (ident) {
1101 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1102 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1103 } else {
1104 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1105 "Unable to find base (%s) of identityref.", bases_p[u]);
1106 }
1107 return LY_EVALID;
1108 }
1109 }
1110 return LY_SUCCESS;
1111}
1112
Radek Krejcia3045382018-11-22 14:30:31 +01001113/**
1114 * @brief For the given array of identities, set the backlinks from all their base identities.
1115 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1116 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1117 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1118 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1119 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001120static LY_ERR
1121lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1122{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001123 LY_ARRAY_SIZE_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001124
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001125 for (u = 0; u < LY_ARRAY_SIZE(idents_p); ++u) {
1126 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001127 continue;
1128 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001129 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001130 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 +02001131 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001132 }
1133 return LY_SUCCESS;
1134}
1135
Radek Krejci0af46292019-01-11 16:02:31 +01001136LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001137lys_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 +01001138{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001139 LY_ARRAY_SIZE_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001140 struct lysc_ctx context = {0};
1141
Radek Krejci327de162019-06-14 12:52:07 +02001142 assert(ctx_sc || ctx);
1143
1144 if (!ctx_sc) {
1145 context.ctx = ctx;
1146 context.mod = module;
1147 context.path_len = 1;
1148 context.path[0] = '/';
1149 ctx_sc = &context;
1150 }
Radek Krejci0af46292019-01-11 16:02:31 +01001151
1152 if (!features_p) {
1153 return LY_SUCCESS;
1154 }
1155 if (*features) {
1156 offset = LY_ARRAY_SIZE(*features);
1157 }
1158
Radek Krejci327de162019-06-14 12:52:07 +02001159 lysc_update_path(ctx_sc, NULL, "{feature}");
1160 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001161 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001162 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1163
Radek Krejci0af46292019-01-11 16:02:31 +01001164 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001165 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci327de162019-06-14 12:52:07 +02001166 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1167 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1168 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001169 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001170 (*features)[offset + u].module = ctx_sc->mod;
1171
1172 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001173 }
Radek Krejci327de162019-06-14 12:52:07 +02001174 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001175
1176 return LY_SUCCESS;
1177}
1178
Radek Krejcia3045382018-11-22 14:30:31 +01001179/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001180 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001181 *
1182 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1183 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001184 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001185 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1186 * being currently processed).
1187 * @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 +01001188 * @return LY_SUCCESS if everything is ok.
1189 * @return LY_EVALID if the feature references indirectly itself.
1190 */
1191static LY_ERR
1192lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1193{
1194 LY_ERR ret = LY_EVALID;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001195 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001196 struct ly_set recursion = {0};
1197 struct lysc_feature *drv;
1198
1199 if (!depfeatures) {
1200 return LY_SUCCESS;
1201 }
1202
1203 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1204 if (feature == depfeatures[u]) {
1205 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1206 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1207 goto cleanup;
1208 }
1209 ly_set_add(&recursion, depfeatures[u], 0);
1210 }
1211
1212 for (v = 0; v < recursion.count; ++v) {
1213 drv = recursion.objs[v];
1214 if (!drv->depfeatures) {
1215 continue;
1216 }
1217 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1218 if (feature == drv->depfeatures[u]) {
1219 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1220 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1221 goto cleanup;
1222 }
1223 ly_set_add(&recursion, drv->depfeatures[u], 0);
1224 }
1225 }
1226 ret = LY_SUCCESS;
1227
1228cleanup:
1229 ly_set_erase(&recursion, NULL);
1230 return ret;
1231}
1232
1233/**
Radek Krejci0af46292019-01-11 16:02:31 +01001234 * @brief Create pre-compiled features array.
1235 *
1236 * See lys_feature_precompile() for more details.
1237 *
Radek Krejcia3045382018-11-22 14:30:31 +01001238 * @param[in] ctx Compile context.
1239 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001240 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001241 * @return LY_ERR value.
1242 */
Radek Krejci19a96102018-11-15 13:38:09 +01001243static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001244lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001245{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001246 LY_ARRAY_SIZE_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001247 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001248 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001249
Radek Krejci327de162019-06-14 12:52:07 +02001250
Radek Krejci0af46292019-01-11 16:02:31 +01001251 /* find the preprecompiled feature */
1252 LY_ARRAY_FOR(features, x) {
1253 if (strcmp(features[x].name, feature_p->name)) {
1254 continue;
1255 }
1256 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001257 lysc_update_path(ctx, NULL, "{feature}");
1258 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001259
Radek Krejci0af46292019-01-11 16:02:31 +01001260 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001261 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001262 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001263 if (feature->iffeatures) {
1264 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1265 if (feature->iffeatures[u].features) {
1266 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001267 /* check for circular dependency - direct reference first,... */
1268 if (feature == feature->iffeatures[u].features[v]) {
1269 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1270 "Feature \"%s\" is referenced from itself.", feature->name);
1271 return LY_EVALID;
1272 }
1273 /* ... and indirect circular reference */
1274 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1275
Radek Krejci0af46292019-01-11 16:02:31 +01001276 /* add itself into the dependants list */
1277 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1278 *df = feature;
1279 }
Radek Krejci19a96102018-11-15 13:38:09 +01001280 }
Radek Krejci19a96102018-11-15 13:38:09 +01001281 }
1282 }
Radek Krejci327de162019-06-14 12:52:07 +02001283 lysc_update_path(ctx, NULL, NULL);
1284 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001285 done:
1286 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001287 }
Radek Krejci0af46292019-01-11 16:02:31 +01001288
1289 LOGINT(ctx->ctx);
1290 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001291}
1292
Radek Krejcib56c7502019-02-13 14:19:54 +01001293/**
1294 * @brief Revert compiled list of features back to the precompiled state.
1295 *
1296 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1297 * The features are supposed to be stored again as off_features in ::lys_module structure.
1298 *
1299 * @param[in] ctx Compilation context.
1300 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1301 * and supposed to hold the off_features list.
1302 */
Radek Krejci95710c92019-02-11 15:49:55 +01001303static void
1304lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1305{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001306 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001307
1308 /* keep the off_features list until the complete lys_module is freed */
1309 mod->off_features = mod->compiled->features;
1310 mod->compiled->features = NULL;
1311
1312 /* in the off_features list, remove all the parts (from finished compiling process)
1313 * which may points into the data being freed here */
1314 LY_ARRAY_FOR(mod->off_features, u) {
1315 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1316 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1317 }
1318 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1319 mod->off_features[u].iffeatures = NULL;
1320
1321 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1322 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1323 }
1324 LY_ARRAY_FREE(mod->off_features[u].exts);
1325 mod->off_features[u].exts = NULL;
1326 }
1327}
1328
Radek Krejcia3045382018-11-22 14:30:31 +01001329/**
1330 * @brief Validate and normalize numeric value from a range definition.
1331 * @param[in] ctx Compile context.
1332 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1333 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1334 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1335 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1336 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1337 * @param[in] value String value of the range boundary.
1338 * @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.
1339 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1340 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1341 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001342LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001343range_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 +01001344{
Radek Krejci6cba4292018-11-15 17:33:29 +01001345 size_t fraction = 0, size;
1346
Radek Krejci19a96102018-11-15 13:38:09 +01001347 *len = 0;
1348
1349 assert(value);
1350 /* parse value */
1351 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1352 return LY_EVALID;
1353 }
1354
1355 if ((value[*len] == '-') || (value[*len] == '+')) {
1356 ++(*len);
1357 }
1358
1359 while (isdigit(value[*len])) {
1360 ++(*len);
1361 }
1362
1363 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001364 if (basetype == LY_TYPE_DEC64) {
1365 goto decimal;
1366 } else {
1367 *valcopy = strndup(value, *len);
1368 return LY_SUCCESS;
1369 }
Radek Krejci19a96102018-11-15 13:38:09 +01001370 }
1371 fraction = *len;
1372
1373 ++(*len);
1374 while (isdigit(value[*len])) {
1375 ++(*len);
1376 }
1377
Radek Krejci6cba4292018-11-15 17:33:29 +01001378 if (basetype == LY_TYPE_DEC64) {
1379decimal:
1380 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001381 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001382 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1383 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1384 *len, value, frdigits);
1385 return LY_EINVAL;
1386 }
1387 if (fraction) {
1388 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1389 } else {
1390 size = (*len) + frdigits + 1;
1391 }
1392 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001393 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1394
Radek Krejci6cba4292018-11-15 17:33:29 +01001395 (*valcopy)[size - 1] = '\0';
1396 if (fraction) {
1397 memcpy(&(*valcopy)[0], &value[0], fraction);
1398 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1399 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1400 } else {
1401 memcpy(&(*valcopy)[0], &value[0], *len);
1402 memset(&(*valcopy)[*len], '0', frdigits);
1403 }
Radek Krejci19a96102018-11-15 13:38:09 +01001404 }
1405 return LY_SUCCESS;
1406}
1407
Radek Krejcia3045382018-11-22 14:30:31 +01001408/**
1409 * @brief Check that values in range are in ascendant order.
1410 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001411 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1412 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001413 * @param[in] value Current value to check.
1414 * @param[in] prev_value The last seen value.
1415 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1416 */
Radek Krejci19a96102018-11-15 13:38:09 +01001417static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001418range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001419{
1420 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001421 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001422 return LY_EEXIST;
1423 }
1424 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001425 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001426 return LY_EEXIST;
1427 }
1428 }
1429 return LY_SUCCESS;
1430}
1431
Radek Krejcia3045382018-11-22 14:30:31 +01001432/**
1433 * @brief Set min/max value of the range part.
1434 * @param[in] ctx Compile context.
1435 * @param[in] part Range part structure to fill.
1436 * @param[in] max Flag to distinguish if storing min or max value.
1437 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1438 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1439 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1440 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1441 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001442 * @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 +01001443 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1444 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1445 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1446 * frdigits value), LY_EMEM.
1447 */
Radek Krejci19a96102018-11-15 13:38:09 +01001448static LY_ERR
1449range_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 +01001450 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001451{
1452 LY_ERR ret = LY_SUCCESS;
1453 char *valcopy = NULL;
1454 size_t len;
1455
1456 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001457 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001458 LY_CHECK_GOTO(ret, finalize);
1459 }
1460 if (!valcopy && base_range) {
1461 if (max) {
1462 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1463 } else {
1464 part->min_64 = base_range->parts[0].min_64;
1465 }
1466 if (!first) {
1467 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1468 }
1469 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001470 }
1471
1472 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001473 case LY_TYPE_INT8: /* range */
1474 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001475 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 +01001476 } else if (max) {
1477 part->max_64 = INT64_C(127);
1478 } else {
1479 part->min_64 = INT64_C(-128);
1480 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001481 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001482 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001483 }
1484 break;
1485 case LY_TYPE_INT16: /* range */
1486 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001487 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 +01001488 } else if (max) {
1489 part->max_64 = INT64_C(32767);
1490 } else {
1491 part->min_64 = INT64_C(-32768);
1492 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001493 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001494 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001495 }
1496 break;
1497 case LY_TYPE_INT32: /* range */
1498 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001499 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 +01001500 } else if (max) {
1501 part->max_64 = INT64_C(2147483647);
1502 } else {
1503 part->min_64 = INT64_C(-2147483648);
1504 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001505 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001506 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001507 }
1508 break;
1509 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001510 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001511 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001512 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001513 max ? &part->max_64 : &part->min_64);
1514 } else if (max) {
1515 part->max_64 = INT64_C(9223372036854775807);
1516 } else {
1517 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1518 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001519 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001520 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001521 }
1522 break;
1523 case LY_TYPE_UINT8: /* range */
1524 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001525 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 +01001526 } else if (max) {
1527 part->max_u64 = UINT64_C(255);
1528 } else {
1529 part->min_u64 = UINT64_C(0);
1530 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001531 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001532 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001533 }
1534 break;
1535 case LY_TYPE_UINT16: /* range */
1536 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001537 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 +01001538 } else if (max) {
1539 part->max_u64 = UINT64_C(65535);
1540 } else {
1541 part->min_u64 = UINT64_C(0);
1542 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001543 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001544 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001545 }
1546 break;
1547 case LY_TYPE_UINT32: /* range */
1548 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001549 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 +01001550 } else if (max) {
1551 part->max_u64 = UINT64_C(4294967295);
1552 } else {
1553 part->min_u64 = UINT64_C(0);
1554 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001555 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001556 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001557 }
1558 break;
1559 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001560 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001561 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001562 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001563 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 +01001564 } else if (max) {
1565 part->max_u64 = UINT64_C(18446744073709551615);
1566 } else {
1567 part->min_u64 = UINT64_C(0);
1568 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001569 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001570 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001571 }
1572 break;
1573 default:
1574 LOGINT(ctx->ctx);
1575 ret = LY_EINT;
1576 }
1577
Radek Krejci5969f272018-11-23 10:03:58 +01001578finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001579 if (ret == LY_EDENIED) {
1580 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1581 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1582 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1583 } else if (ret == LY_EVALID) {
1584 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1585 "Invalid %s restriction - invalid value \"%s\".",
1586 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1587 } else if (ret == LY_EEXIST) {
1588 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1589 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001590 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001591 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001592 } else if (!ret && value) {
1593 *value = *value + len;
1594 }
1595 free(valcopy);
1596 return ret;
1597}
1598
Radek Krejcia3045382018-11-22 14:30:31 +01001599/**
1600 * @brief Compile the parsed range restriction.
1601 * @param[in] ctx Compile context.
1602 * @param[in] range_p Parsed range structure to compile.
1603 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1604 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1605 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1606 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1607 * range restriction must be more restrictive than the base_range.
1608 * @param[in,out] range Pointer to the created current range structure.
1609 * @return LY_ERR value.
1610 */
Radek Krejci19a96102018-11-15 13:38:09 +01001611static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001612lys_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 +01001613 struct lysc_range *base_range, struct lysc_range **range)
1614{
1615 LY_ERR ret = LY_EVALID;
1616 const char *expr;
1617 struct lysc_range_part *parts = NULL, *part;
1618 int range_expected = 0, uns;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001619 LY_ARRAY_SIZE_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001620
1621 assert(range);
1622 assert(range_p);
1623
1624 expr = range_p->arg;
1625 while(1) {
1626 if (isspace(*expr)) {
1627 ++expr;
1628 } else if (*expr == '\0') {
1629 if (range_expected) {
1630 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1631 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1632 length_restr ? "length" : "range", range_p->arg);
1633 goto cleanup;
1634 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1635 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1636 "Invalid %s restriction - unexpected end of the expression (%s).",
1637 length_restr ? "length" : "range", range_p->arg);
1638 goto cleanup;
1639 }
1640 parts_done++;
1641 break;
1642 } else if (!strncmp(expr, "min", 3)) {
1643 if (parts) {
1644 /* min cannot be used elsewhere than in the first part */
1645 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1646 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1647 expr - range_p->arg, range_p->arg);
1648 goto cleanup;
1649 }
1650 expr += 3;
1651
1652 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001653 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 +01001654 part->max_64 = part->min_64;
1655 } else if (*expr == '|') {
1656 if (!parts || range_expected) {
1657 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1658 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1659 goto cleanup;
1660 }
1661 expr++;
1662 parts_done++;
1663 /* process next part of the expression */
1664 } else if (!strncmp(expr, "..", 2)) {
1665 expr += 2;
1666 while (isspace(*expr)) {
1667 expr++;
1668 }
1669 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1671 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1672 goto cleanup;
1673 }
1674 /* continue expecting the upper boundary */
1675 range_expected = 1;
1676 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1677 /* number */
1678 if (range_expected) {
1679 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001680 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 +01001681 range_expected = 0;
1682 } else {
1683 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1684 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 +01001685 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001686 part->max_64 = part->min_64;
1687 }
1688
1689 /* continue with possible another expression part */
1690 } else if (!strncmp(expr, "max", 3)) {
1691 expr += 3;
1692 while (isspace(*expr)) {
1693 expr++;
1694 }
1695 if (*expr != '\0') {
1696 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1697 length_restr ? "length" : "range", expr);
1698 goto cleanup;
1699 }
1700 if (range_expected) {
1701 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001702 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 +01001703 range_expected = 0;
1704 } else {
1705 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1706 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 +01001707 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001708 part->min_64 = part->max_64;
1709 }
1710 } else {
1711 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1712 length_restr ? "length" : "range", expr);
1713 goto cleanup;
1714 }
1715 }
1716
1717 /* check with the previous range/length restriction */
1718 if (base_range) {
1719 switch (basetype) {
1720 case LY_TYPE_BINARY:
1721 case LY_TYPE_UINT8:
1722 case LY_TYPE_UINT16:
1723 case LY_TYPE_UINT32:
1724 case LY_TYPE_UINT64:
1725 case LY_TYPE_STRING:
1726 uns = 1;
1727 break;
1728 case LY_TYPE_DEC64:
1729 case LY_TYPE_INT8:
1730 case LY_TYPE_INT16:
1731 case LY_TYPE_INT32:
1732 case LY_TYPE_INT64:
1733 uns = 0;
1734 break;
1735 default:
1736 LOGINT(ctx->ctx);
1737 ret = LY_EINT;
1738 goto cleanup;
1739 }
1740 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1741 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1742 goto baseerror;
1743 }
1744 /* current lower bound is not lower than the base */
1745 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1746 /* base has single value */
1747 if (base_range->parts[v].min_64 == parts[u].min_64) {
1748 /* both lower bounds are the same */
1749 if (parts[u].min_64 != parts[u].max_64) {
1750 /* current continues with a range */
1751 goto baseerror;
1752 } else {
1753 /* equal single values, move both forward */
1754 ++v;
1755 continue;
1756 }
1757 } else {
1758 /* base is single value lower than current range, so the
1759 * value from base range is removed in the current,
1760 * move only base and repeat checking */
1761 ++v;
1762 --u;
1763 continue;
1764 }
1765 } else {
1766 /* base is the range */
1767 if (parts[u].min_64 == parts[u].max_64) {
1768 /* current is a single value */
1769 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1770 /* current is behind the base range, so base range is omitted,
1771 * move the base and keep the current for further check */
1772 ++v;
1773 --u;
1774 } /* else it is within the base range, so move the current, but keep the base */
1775 continue;
1776 } else {
1777 /* both are ranges - check the higher bound, the lower was already checked */
1778 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1779 /* higher bound is higher than the current higher bound */
1780 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1781 /* but the current lower bound is also higher, so the base range is omitted,
1782 * continue with the same current, but move the base */
1783 --u;
1784 ++v;
1785 continue;
1786 }
1787 /* current range starts within the base range but end behind it */
1788 goto baseerror;
1789 } else {
1790 /* current range is smaller than the base,
1791 * move current, but stay with the base */
1792 continue;
1793 }
1794 }
1795 }
1796 }
1797 if (u != parts_done) {
1798baseerror:
1799 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1800 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1801 length_restr ? "length" : "range", range_p->arg);
1802 goto cleanup;
1803 }
1804 }
1805
1806 if (!(*range)) {
1807 *range = calloc(1, sizeof **range);
1808 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1809 }
1810
Radek Krejcic8b31002019-01-08 10:24:45 +01001811 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001812 if (range_p->eapptag) {
1813 lydict_remove(ctx->ctx, (*range)->eapptag);
1814 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1815 }
1816 if (range_p->emsg) {
1817 lydict_remove(ctx->ctx, (*range)->emsg);
1818 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1819 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001820 if (range_p->dsc) {
1821 lydict_remove(ctx->ctx, (*range)->dsc);
1822 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1823 }
1824 if (range_p->ref) {
1825 lydict_remove(ctx->ctx, (*range)->ref);
1826 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1827 }
Radek Krejci19a96102018-11-15 13:38:09 +01001828 /* extensions are taken only from the last range by the caller */
1829
1830 (*range)->parts = parts;
1831 parts = NULL;
1832 ret = LY_SUCCESS;
1833cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001834 LY_ARRAY_FREE(parts);
1835
1836 return ret;
1837}
1838
1839/**
1840 * @brief Checks pattern syntax.
1841 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001842 * @param[in] ctx Context.
1843 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001844 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001845 * @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 +01001846 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001847 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001848LY_ERR
1849lys_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 +01001850{
Michal Vasko40a00082020-05-27 15:20:01 +02001851 int idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001852 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001853 int err_code;
1854 const char *orig_ptr;
1855 PCRE2_SIZE err_offset;
1856 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001857#define URANGE_LEN 19
1858 char *ublock2urange[][2] = {
1859 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1860 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1861 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1862 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1863 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1864 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1865 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1866 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1867 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1868 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1869 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1870 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1871 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1872 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1873 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1874 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1875 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1876 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1877 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1878 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1879 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1880 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1881 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1882 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1883 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1884 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1885 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1886 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1887 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1888 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1889 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1890 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1891 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1892 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1893 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1894 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1895 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1896 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1897 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1898 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1899 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1900 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1901 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1902 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1903 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1904 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1905 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1906 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1907 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1908 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1909 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1910 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1911 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1912 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1913 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1914 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1915 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1916 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1917 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1918 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1919 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1920 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1921 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1922 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1923 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1924 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1925 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1926 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1927 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1928 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1929 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1930 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1931 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1932 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1933 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1934 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1935 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1936 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1937 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1938 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1939 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1940 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1941 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1942 {NULL, NULL}
1943 };
1944
1945 /* adjust the expression to a Perl equivalent
1946 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1947
Michal Vasko40a00082020-05-27 15:20:01 +02001948 /* allocate space for the transformed pattern */
1949 size = strlen(pattern) + 1;
1950 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001951 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001952 perl_regex[0] = '\0';
1953
Michal Vasko40a00082020-05-27 15:20:01 +02001954 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1955 brack = 0;
1956 idx = 0;
1957 orig_ptr = pattern;
1958 while (orig_ptr[0]) {
1959 switch (orig_ptr[0]) {
1960 case '$':
1961 case '^':
1962 if (!brack) {
1963 /* make space for the extra character */
1964 ++size;
1965 perl_regex = ly_realloc(perl_regex, size);
1966 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001967
Michal Vasko40a00082020-05-27 15:20:01 +02001968 /* print escape slash */
1969 perl_regex[idx] = '\\';
1970 ++idx;
1971 }
1972 break;
1973 case '[':
1974 /* must not be escaped */
1975 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
1976 ++brack;
1977 }
1978 break;
1979 case ']':
1980 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
1981 /* pattern was checked and compiled already */
1982 assert(brack);
1983 --brack;
1984 }
1985 break;
1986 default:
1987 break;
Radek Krejci19a96102018-11-15 13:38:09 +01001988 }
Michal Vasko40a00082020-05-27 15:20:01 +02001989
1990 /* copy char */
1991 perl_regex[idx] = orig_ptr[0];
1992
1993 ++idx;
1994 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001995 }
Michal Vasko40a00082020-05-27 15:20:01 +02001996 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01001997
1998 /* substitute Unicode Character Blocks with exact Character Ranges */
1999 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2000 start = ptr - perl_regex;
2001
2002 ptr = strchr(ptr, '}');
2003 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002004 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002005 pattern, perl_regex + start + 2, "unterminated character property");
2006 free(perl_regex);
2007 return LY_EVALID;
2008 }
2009 end = (ptr - perl_regex) + 1;
2010
2011 /* need more space */
2012 if (end - start < URANGE_LEN) {
2013 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002014 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002015 }
2016
2017 /* find our range */
2018 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2019 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2020 break;
2021 }
2022 }
2023 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002025 pattern, perl_regex + start + 5, "unknown block name");
2026 free(perl_regex);
2027 return LY_EVALID;
2028 }
2029
2030 /* 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 +02002031 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002032 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002033 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002034 }
2035 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002036 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002037 }
2038 }
Michal Vasko40a00082020-05-27 15:20:01 +02002039 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002040 /* skip brackets */
2041 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2042 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2043 } else {
2044 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2045 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2046 }
2047 }
2048
2049 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002050 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2051 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002052 &err_code, &err_offset, NULL);
2053 if (!code_local) {
2054 PCRE2_UCHAR err_msg[256] = {0};
2055 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002056 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002057 free(perl_regex);
2058 return LY_EVALID;
2059 }
2060 free(perl_regex);
2061
Radek Krejci54579462019-04-30 12:47:06 +02002062 if (code) {
2063 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002064 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002065 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002066 }
2067
2068 return LY_SUCCESS;
2069
2070#undef URANGE_LEN
2071}
2072
Radek Krejcia3045382018-11-22 14:30:31 +01002073/**
2074 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2075 * @param[in] ctx Compile context.
2076 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002077 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2078 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2079 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2080 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2081 */
Radek Krejci19a96102018-11-15 13:38:09 +01002082static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002083lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002084 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2085{
2086 struct lysc_pattern **pattern;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002087 LY_ARRAY_SIZE_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002088 LY_ERR ret = LY_SUCCESS;
2089
2090 /* first, copy the patterns from the base type */
2091 if (base_patterns) {
2092 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2093 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2094 }
2095
2096 LY_ARRAY_FOR(patterns_p, u) {
2097 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2098 *pattern = calloc(1, sizeof **pattern);
2099 ++(*pattern)->refcount;
2100
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002102 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002103
2104 if (patterns_p[u].arg[0] == 0x15) {
2105 (*pattern)->inverted = 1;
2106 }
Radek Krejci54579462019-04-30 12:47:06 +02002107 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002108 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2109 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002110 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2111 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002112 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002113 }
2114done:
2115 return ret;
2116}
2117
Radek Krejcia3045382018-11-22 14:30:31 +01002118/**
2119 * @brief map of the possible restrictions combination for the specific built-in type.
2120 */
Radek Krejci19a96102018-11-15 13:38:09 +01002121static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2122 0 /* LY_TYPE_UNKNOWN */,
2123 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002124 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2125 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2126 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2127 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2128 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002129 LYS_SET_BIT /* LY_TYPE_BITS */,
2130 0 /* LY_TYPE_BOOL */,
2131 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2132 0 /* LY_TYPE_EMPTY */,
2133 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2134 LYS_SET_BASE /* LY_TYPE_IDENT */,
2135 LYS_SET_REQINST /* LY_TYPE_INST */,
2136 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002137 LYS_SET_TYPE /* LY_TYPE_UNION */,
2138 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002139 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002140 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002141 LYS_SET_RANGE /* LY_TYPE_INT64 */
2142};
2143
2144/**
2145 * @brief stringification of the YANG built-in data types
2146 */
2147const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2148 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2149 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002150};
2151
Radek Krejcia3045382018-11-22 14:30:31 +01002152/**
2153 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2154 * @param[in] ctx Compile context.
2155 * @param[in] enums_p Array of the parsed enum structures to compile.
2156 * @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 +01002157 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2158 * @param[out] enums Newly created array of the compiled enums information for the current type.
2159 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2160 */
Radek Krejci19a96102018-11-15 13:38:09 +01002161static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002162lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002163 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002164{
2165 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002166 LY_ARRAY_SIZE_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002167 int32_t value = 0;
2168 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002169 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002170
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002171 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002172 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2173 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2174 return LY_EVALID;
2175 }
2176
2177 LY_ARRAY_FOR(enums_p, u) {
2178 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2179 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002180 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2181 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002182 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002183 if (base_enums) {
2184 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2185 LY_ARRAY_FOR(base_enums, v) {
2186 if (!strcmp(e->name, base_enums[v].name)) {
2187 break;
2188 }
2189 }
2190 if (v == LY_ARRAY_SIZE(base_enums)) {
2191 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2192 "Invalid %s - derived type adds new item \"%s\".",
2193 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2194 return LY_EVALID;
2195 }
2196 match = v;
2197 }
2198
2199 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002200 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002201 if (enums_p[u].flags & LYS_SET_VALUE) {
2202 e->value = (int32_t)enums_p[u].value;
2203 if (!u || e->value >= value) {
2204 value = e->value + 1;
2205 }
2206 /* check collision with other values */
2207 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2208 if (e->value == (*enums)[v].value) {
2209 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2210 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2211 e->value, e->name, (*enums)[v].name);
2212 return LY_EVALID;
2213 }
2214 }
2215 } else if (base_enums) {
2216 /* inherit the assigned value */
2217 e->value = base_enums[match].value;
2218 if (!u || e->value >= value) {
2219 value = e->value + 1;
2220 }
2221 } else {
2222 /* assign value automatically */
2223 if (u && value == INT32_MIN) {
2224 /* counter overflow */
2225 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2226 "Invalid enumeration - it is not possible to auto-assign enum value for "
2227 "\"%s\" since the highest value is already 2147483647.", e->name);
2228 return LY_EVALID;
2229 }
2230 e->value = value++;
2231 }
2232 } else { /* LY_TYPE_BITS */
2233 if (enums_p[u].flags & LYS_SET_VALUE) {
2234 e->value = (int32_t)enums_p[u].value;
2235 if (!u || (uint32_t)e->value >= position) {
2236 position = (uint32_t)e->value + 1;
2237 }
2238 /* check collision with other values */
2239 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2240 if (e->value == (*enums)[v].value) {
2241 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2242 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2243 (uint32_t)e->value, e->name, (*enums)[v].name);
2244 return LY_EVALID;
2245 }
2246 }
2247 } else if (base_enums) {
2248 /* inherit the assigned value */
2249 e->value = base_enums[match].value;
2250 if (!u || (uint32_t)e->value >= position) {
2251 position = (uint32_t)e->value + 1;
2252 }
2253 } else {
2254 /* assign value automatically */
2255 if (u && position == 0) {
2256 /* counter overflow */
2257 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2258 "Invalid bits - it is not possible to auto-assign bit position for "
2259 "\"%s\" since the highest value is already 4294967295.", e->name);
2260 return LY_EVALID;
2261 }
2262 e->value = position++;
2263 }
2264 }
2265
2266 if (base_enums) {
2267 /* the assigned values must not change from the derived type */
2268 if (e->value != base_enums[match].value) {
2269 if (basetype == LY_TYPE_ENUM) {
2270 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2271 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2272 e->name, base_enums[match].value, e->value);
2273 } else {
2274 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2275 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2276 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2277 }
2278 return LY_EVALID;
2279 }
2280 }
2281
Radek Krejciec4da802019-05-02 13:02:41 +02002282 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002283 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 +01002284
2285 if (basetype == LY_TYPE_BITS) {
2286 /* keep bits ordered by position */
2287 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2288 if (v != u) {
2289 memcpy(&storage, e, sizeof *e);
2290 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2291 memcpy(&(*enums)[v], &storage, sizeof storage);
2292 }
2293 }
2294 }
2295
2296done:
2297 return ret;
2298}
2299
Radek Krejcia3045382018-11-22 14:30:31 +01002300#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2301 for ((NODE) = (NODE)->parent; \
Michal Vasko1bf09392020-03-27 12:38:10 +01002302 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_RPC | LYS_ACTION)); \
Radek Krejcia3045382018-11-22 14:30:31 +01002303 (NODE) = (NODE)->parent); \
2304 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2305 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2306 TERM; \
2307 }
2308
2309/**
2310 * @brief Validate the predicate(s) from the leafref path.
2311 * @param[in] ctx Compile context
2312 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2313 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002314 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002315 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002316 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002317 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2318 */
2319static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002320lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002321 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002322{
2323 LY_ERR ret = LY_EVALID;
2324 const struct lys_module *mod;
2325 const struct lysc_node *src_node, *dst_node;
2326 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2327 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002328 unsigned int dest_parent_times, c;
Radek Krejcia3045382018-11-22 14:30:31 +01002329 const char *start, *end, *pke_end;
2330 struct ly_set keys = {0};
2331 int i;
2332
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002333 assert(path_context);
2334
Radek Krejcia3045382018-11-22 14:30:31 +01002335 while (**predicate == '[') {
2336 start = (*predicate)++;
2337
2338 while (isspace(**predicate)) {
2339 ++(*predicate);
2340 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002341 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002342 while (isspace(**predicate)) {
2343 ++(*predicate);
2344 }
2345 if (**predicate != '=') {
2346 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002347 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2348 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002349 goto cleanup;
2350 }
2351 ++(*predicate);
2352 while (isspace(**predicate)) {
2353 ++(*predicate);
2354 }
2355
2356 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2357 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2358 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2359 goto cleanup;
2360 }
2361 --pke_end;
2362 while (isspace(*pke_end)) {
2363 --pke_end;
2364 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002365 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002366 /* localize path-key-expr */
2367 pke_start = path_key_expr = *predicate;
2368 /* move after the current predicate */
2369 *predicate = end + 1;
2370
2371 /* source (must be leaf or leaf-list) */
2372 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002373 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002374 if (!mod) {
2375 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2376 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002377 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002378 goto cleanup;
2379 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002380 if (!mod->implemented) {
2381 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002382 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci92cc8512019-04-25 09:57:06 +02002383 }
Radek Krejcia3045382018-11-22 14:30:31 +01002384 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002385 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002386 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002387 src_node = NULL;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002388 if (!(context_node->flags & LYS_KEYLESS)) {
2389 struct lysc_node *key;
2390 for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02002391 if (!ly_strncmp(key->name, src, src_len)) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002392 src_node = key;
Radek Krejcibade82a2018-12-05 10:13:48 +01002393 break;
2394 }
2395 }
2396 }
Radek Krejcia3045382018-11-22 14:30:31 +01002397 if (!src_node) {
2398 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002399 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002400 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002401 goto cleanup;
2402 }
Radek Krejcia3045382018-11-22 14:30:31 +01002403
2404 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002405 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002406 i = ly_set_add(&keys, (void*)src_node, 0);
2407 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002408 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002409 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002410 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002411 *predicate - start, start, src_node->name);
2412 goto cleanup;
2413 }
2414
2415 /* destination */
2416 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002417 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002418
2419 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002420 if (strncmp(path_key_expr, "current", 7)) {
2421error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002422 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2423 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2424 *predicate - start, start);
2425 goto cleanup;
2426 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002427 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2428 if (*path_key_expr != '(') {
2429 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002430 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002431 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2432 if (*path_key_expr != ')') {
2433 goto error_current_function_invocation;
2434 }
2435 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002436
2437 if (*path_key_expr != '/') {
2438 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2439 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2440 *predicate - start, start);
2441 goto cleanup;
2442 }
2443 ++path_key_expr;
2444 while (isspace(*path_key_expr)) {
2445 ++path_key_expr;
2446 }
2447
2448 /* rel-path-keyexpr:
2449 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2450 while (!strncmp(path_key_expr, "..", 2)) {
2451 ++dest_parent_times;
2452 path_key_expr += 2;
2453 while (isspace(*path_key_expr)) {
2454 ++path_key_expr;
2455 }
2456 if (*path_key_expr != '/') {
2457 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2458 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2459 *predicate - start, start);
2460 goto cleanup;
2461 }
2462 ++path_key_expr;
2463 while (isspace(*path_key_expr)) {
2464 ++path_key_expr;
2465 }
2466
2467 /* path is supposed to be evaluated in data tree, so we have to skip
2468 * all schema nodes that cannot be instantiated in data tree */
2469 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2470 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2471 *predicate - start, start);
2472 }
2473 if (!dest_parent_times) {
2474 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2475 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2476 *predicate - start, start);
2477 goto cleanup;
2478 }
2479 if (path_key_expr == pke_end) {
2480 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2481 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2482 *predicate - start, start);
2483 goto cleanup;
2484 }
2485
2486 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002487 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002488 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002489 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002490 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2491 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002492 goto cleanup;
2493 }
2494
2495 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002496 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002497 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002498 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002499 }
2500 if (!mod) {
2501 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002502 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002503 *predicate - start, start, dst_len, dst);
2504 goto cleanup;
2505 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002506 if (!mod->implemented) {
2507 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002508 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci92cc8512019-04-25 09:57:06 +02002509 }
Radek Krejcia3045382018-11-22 14:30:31 +01002510
Michal Vaskoe444f752020-02-10 12:20:06 +01002511 dst_node = lys_find_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
Radek Krejcia3045382018-11-22 14:30:31 +01002512 if (!dst_node) {
2513 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002514 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002515 *predicate - start, start, path_key_expr - pke_start, pke_start);
2516 goto cleanup;
2517 }
2518 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002519 if (!(dst_node->nodetype & (dst_node->module->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcia3045382018-11-22 14:30:31 +01002520 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002521 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002522 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2523 goto cleanup;
2524 }
2525 }
2526
2527 ret = LY_SUCCESS;
2528cleanup:
2529 ly_set_erase(&keys, NULL);
2530 return ret;
2531}
2532
2533/**
2534 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2535 *
2536 * path-arg = absolute-path / relative-path
2537 * absolute-path = 1*("/" (node-identifier *path-predicate))
2538 * relative-path = 1*(".." "/") descendant-path
2539 *
2540 * @param[in,out] path Path to parse.
2541 * @param[out] prefix Prefix of the token, NULL if there is not any.
2542 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2543 * @param[out] name Name of the token.
2544 * @param[out] nam_len Length of the name.
2545 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2546 * must not be changed between consecutive calls. -1 if the
2547 * path is absolute.
2548 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2549 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2550 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002551LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002552lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2553 int *parent_times, int *has_predicate)
2554{
2555 int par_times = 0;
2556
2557 assert(path && *path);
2558 assert(parent_times);
2559 assert(prefix);
2560 assert(prefix_len);
2561 assert(name);
2562 assert(name_len);
2563 assert(has_predicate);
2564
2565 *prefix = NULL;
2566 *prefix_len = 0;
2567 *name = NULL;
2568 *name_len = 0;
2569 *has_predicate = 0;
2570
2571 if (!*parent_times) {
2572 if (!strncmp(*path, "..", 2)) {
2573 *path += 2;
2574 ++par_times;
2575 while (!strncmp(*path, "/..", 3)) {
2576 *path += 3;
2577 ++par_times;
2578 }
2579 }
2580 if (par_times) {
2581 *parent_times = par_times;
2582 } else {
2583 *parent_times = -1;
2584 }
2585 }
2586
2587 if (**path != '/') {
2588 return LY_EINVAL;
2589 }
2590 /* skip '/' */
2591 ++(*path);
2592
2593 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002594 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002595
2596 if ((**path == '/' && (*path)[1]) || !**path) {
2597 /* path continues by another token or this is the last token */
2598 return LY_SUCCESS;
2599 } else if ((*path)[0] != '[') {
2600 /* unexpected character */
2601 return LY_EINVAL;
2602 } else {
2603 /* predicate starting with [ */
2604 *has_predicate = 1;
2605 return LY_SUCCESS;
2606 }
2607}
2608
2609/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002610 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2611 *
2612 * The set of features used for target must be a subset of features used for the leafref.
2613 * This is not a perfect, we should compare the truth tables but it could require too much resources
2614 * and RFC 7950 does not require it explicitely, so we simplify that.
2615 *
2616 * @param[in] refnode The leafref node.
2617 * @param[in] target Tha target node of the leafref.
2618 * @return LY_SUCCESS or LY_EVALID;
2619 */
2620static LY_ERR
2621lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2622{
2623 LY_ERR ret = LY_EVALID;
2624 const struct lysc_node *iter;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002625 LY_ARRAY_SIZE_TYPE u, v, count;
Radek Krejci58d171e2018-11-23 13:50:55 +01002626 struct ly_set features = {0};
2627
2628 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002629 if (iter->iffeatures) {
2630 LY_ARRAY_FOR(iter->iffeatures, u) {
2631 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2632 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002633 }
2634 }
2635 }
2636 }
2637
2638 /* we should have, in features set, a superset of features applicable to the target node.
2639 * So when adding features applicable to the target into the features set, we should not be
2640 * able to actually add any new feature, otherwise it is not a subset of features applicable
2641 * to the leafref itself. */
2642 count = features.count;
2643 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002644 if (iter->iffeatures) {
2645 LY_ARRAY_FOR(iter->iffeatures, u) {
2646 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2647 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002648 /* new feature was added (or LY_EMEM) */
2649 goto cleanup;
2650 }
2651 }
2652 }
2653 }
2654 }
2655 ret = LY_SUCCESS;
2656
2657cleanup:
2658 ly_set_erase(&features, NULL);
2659 return ret;
2660}
2661
2662/**
Radek Krejcia3045382018-11-22 14:30:31 +01002663 * @brief Validate the leafref path.
2664 * @param[in] ctx Compile context
2665 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002666 * @param[in] leafref Leafref to validate.
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002667 * @param[out] target Optional resolved leafref target.
Radek Krejcia3045382018-11-22 14:30:31 +01002668 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2669 */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002670LY_ERR
2671lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref,
2672 const struct lysc_node **target)
Radek Krejcia3045382018-11-22 14:30:31 +01002673{
Michal Vaskoecd62de2019-11-13 12:35:11 +01002674 const struct lysc_node *node = NULL, *parent = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +01002675 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002676 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002677 const char *id, *prefix, *name;
2678 size_t prefix_len, name_len;
2679 int parent_times = 0, has_predicate;
2680 unsigned int iter, u;
2681 LY_ERR ret = LY_SUCCESS;
2682
2683 assert(ctx);
2684 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002685 assert(leafref);
2686
Radek Krejci1c0c3442019-07-23 16:08:47 +02002687 ctx->path[0] = '\0';
Michal Vasko03ff5a72019-09-11 13:49:33 +02002688 lysc_path(startnode, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci1c0c3442019-07-23 16:08:47 +02002689 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002690
Radek Krejcia3045382018-11-22 14:30:31 +01002691 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002692 id = leafref->path;
Juraj Vijtiukbd0f2a62019-12-11 15:58:18 +01002693
2694 if (!*id) {
2695 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Empty leafref path.");
2696 return LY_EVALID;
2697 }
2698
Radek Krejcia3045382018-11-22 14:30:31 +01002699 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2700 if (!iter) { /* first iteration */
2701 /* precess ".." in relative paths */
2702 if (parent_times > 0) {
2703 /* move from the context node */
2704 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2705 /* path is supposed to be evaluated in data tree, so we have to skip
2706 * all schema nodes that cannot be instantiated in data tree */
2707 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002708 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002709 }
2710 }
2711 }
2712
2713 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002714 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002715 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002716 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002717 }
2718 if (!mod) {
2719 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002720 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2721 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002722 return LY_EVALID;
2723 }
Radek Krejci3d929562019-02-21 11:25:55 +01002724 if (!mod->implemented) {
2725 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002726 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci3d929562019-02-21 11:25:55 +01002727 }
Radek Krejcia3045382018-11-22 14:30:31 +01002728
Michal Vaskoe444f752020-02-10 12:20:06 +01002729 node = lys_find_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
Radek Krejcia3045382018-11-22 14:30:31 +01002730 if (!node) {
2731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002732 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002733 return LY_EVALID;
2734 }
2735 parent = node;
2736
2737 if (has_predicate) {
2738 /* we have predicate, so the current result must be list */
2739 if (node->nodetype != LYS_LIST) {
2740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2741 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002742 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002743 return LY_EVALID;
2744 }
2745
Radek Krejcibade82a2018-12-05 10:13:48 +01002746 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2747 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002748 }
2749
2750 ++iter;
2751 }
2752 if (ret) {
2753 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002754 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002755 return LY_EVALID;
2756 }
2757
2758 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2759 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2760 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002761 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002762 return LY_EVALID;
2763 }
2764
2765 /* check status */
2766 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2767 return LY_EVALID;
2768 }
2769
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002770 /* check config */
2771 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2772 if (node->flags & LYS_CONFIG_R) {
2773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2774 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2775 leafref->path);
2776 return LY_EVALID;
2777 }
2778 }
2779
Radek Krejci412ddfa2018-11-23 11:44:11 +01002780 /* store the target's type and check for circular chain of leafrefs */
2781 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2782 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2783 if (type == (struct lysc_type*)leafref) {
2784 /* circular chain detected */
2785 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2786 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2787 return LY_EVALID;
2788 }
2789 }
2790
Radek Krejci58d171e2018-11-23 13:50:55 +01002791 /* check if leafref and its target are under common if-features */
2792 if (lys_compile_leafref_features_validate(startnode, node)) {
2793 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2794 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2795 leafref->path);
2796 return LY_EVALID;
2797 }
2798
Radek Krejci327de162019-06-14 12:52:07 +02002799 ctx->path_len = 1;
2800 ctx->path[1] = '\0';
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002801 if (target) {
2802 *target = node;
2803 }
Radek Krejcia3045382018-11-22 14:30:31 +01002804 return LY_SUCCESS;
2805}
2806
Radek Krejcicdfecd92018-11-26 11:27:32 +01002807static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejciec4da802019-05-02 13:02:41 +02002808 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002809/**
2810 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2811 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002812 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2813 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2814 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2815 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002816 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002817 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002818 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002819 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2820 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2821 * @param[out] type Newly created type structure with the filled information about the type.
2822 * @return LY_ERR value.
2823 */
Radek Krejci19a96102018-11-15 13:38:09 +01002824static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002825lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejciec4da802019-05-02 13:02:41 +02002826 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002827 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002828{
2829 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002830 struct lysc_type_bin *bin;
2831 struct lysc_type_num *num;
2832 struct lysc_type_str *str;
2833 struct lysc_type_bits *bits;
2834 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002835 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002836 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002837 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002838
2839 switch (basetype) {
2840 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002841 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002842
2843 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002844 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002845 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2846 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002847 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002848 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 +01002849 }
2850 }
2851
2852 if (tpdfname) {
2853 type_p->compiled = *type;
2854 *type = calloc(1, sizeof(struct lysc_type_bin));
2855 }
2856 break;
2857 case LY_TYPE_BITS:
2858 /* RFC 7950 9.7 - bits */
2859 bits = (struct lysc_type_bits*)(*type);
2860 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002861 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002862 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2863 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002864 }
2865
Radek Krejci555cb5b2018-11-16 14:54:33 +01002866 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002867 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002868 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002869 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002870 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002871 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002872 free(*type);
2873 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002874 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002875 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002876 }
2877
2878 if (tpdfname) {
2879 type_p->compiled = *type;
2880 *type = calloc(1, sizeof(struct lysc_type_bits));
2881 }
2882 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002883 case LY_TYPE_DEC64:
2884 dec = (struct lysc_type_dec*)(*type);
2885
2886 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002887 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002888 if (!type_p->fraction_digits) {
2889 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002891 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002892 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002893 free(*type);
2894 *type = NULL;
2895 }
2896 return LY_EVALID;
2897 }
2898 } else if (type_p->fraction_digits) {
2899 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002900 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002901 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2902 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002903 tpdfname);
2904 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002905 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2906 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002907 free(*type);
2908 *type = NULL;
2909 }
2910 return LY_EVALID;
2911 }
2912 dec->fraction_digits = type_p->fraction_digits;
2913
2914 /* RFC 7950 9.2.4 - range */
2915 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002916 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2917 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002918 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002919 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 +01002920 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002921 }
2922
2923 if (tpdfname) {
2924 type_p->compiled = *type;
2925 *type = calloc(1, sizeof(struct lysc_type_dec));
2926 }
2927 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002928 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002929 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002930
2931 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002932 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002933 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2934 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002935 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002936 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 +01002937 }
2938 } else if (base && ((struct lysc_type_str*)base)->length) {
2939 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2940 }
2941
2942 /* RFC 7950 9.4.5 - pattern */
2943 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002944 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002945 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002946 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2947 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2948 }
2949
2950 if (tpdfname) {
2951 type_p->compiled = *type;
2952 *type = calloc(1, sizeof(struct lysc_type_str));
2953 }
2954 break;
2955 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002956 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002957
2958 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002959 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002960 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002961 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002962 }
2963
Radek Krejci555cb5b2018-11-16 14:54:33 +01002964 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002965 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002966 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002967 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002968 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002969 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002970 free(*type);
2971 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002972 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002973 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002974 }
2975
2976 if (tpdfname) {
2977 type_p->compiled = *type;
2978 *type = calloc(1, sizeof(struct lysc_type_enum));
2979 }
2980 break;
2981 case LY_TYPE_INT8:
2982 case LY_TYPE_UINT8:
2983 case LY_TYPE_INT16:
2984 case LY_TYPE_UINT16:
2985 case LY_TYPE_INT32:
2986 case LY_TYPE_UINT32:
2987 case LY_TYPE_INT64:
2988 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002989 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002990
2991 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002992 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002993 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2994 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002995 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002996 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 +01002997 }
2998 }
2999
3000 if (tpdfname) {
3001 type_p->compiled = *type;
3002 *type = calloc(1, sizeof(struct lysc_type_num));
3003 }
3004 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01003005 case LY_TYPE_IDENT:
3006 idref = (struct lysc_type_identityref*)(*type);
3007
3008 /* RFC 7950 9.10.2 - base */
3009 if (type_p->bases) {
3010 if (base) {
3011 /* only the directly derived identityrefs can contain base specification */
3012 if (tpdfname) {
3013 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01003014 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01003015 tpdfname);
3016 } else {
3017 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01003018 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01003019 free(*type);
3020 *type = NULL;
3021 }
3022 return LY_EVALID;
3023 }
Radek Krejci0a33b042020-05-27 10:05:06 +02003024 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01003025 }
3026
3027 if (!base && !type_p->flags) {
3028 /* type derived from identityref built-in type must contain at least one base */
3029 if (tpdfname) {
3030 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
3031 } else {
3032 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
3033 free(*type);
3034 *type = NULL;
3035 }
3036 return LY_EVALID;
3037 }
3038
3039 if (tpdfname) {
3040 type_p->compiled = *type;
3041 *type = calloc(1, sizeof(struct lysc_type_identityref));
3042 }
3043 break;
Radek Krejcia3045382018-11-22 14:30:31 +01003044 case LY_TYPE_LEAFREF:
3045 /* RFC 7950 9.9.3 - require-instance */
3046 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003047 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003048 if (tpdfname) {
3049 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3050 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
3051 } else {
3052 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3053 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
3054 free(*type);
3055 *type = NULL;
3056 }
3057 return LY_EVALID;
3058 }
Radek Krejcia3045382018-11-22 14:30:31 +01003059 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01003060 } else if (base) {
3061 /* inherit */
3062 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01003063 } else {
3064 /* default is true */
3065 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
3066 }
3067 if (type_p->path) {
3068 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003069 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01003070 } else if (base) {
3071 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003072 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01003073 } else if (tpdfname) {
3074 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
3075 return LY_EVALID;
3076 } else {
3077 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
3078 free(*type);
3079 *type = NULL;
3080 return LY_EVALID;
3081 }
3082 if (tpdfname) {
3083 type_p->compiled = *type;
3084 *type = calloc(1, sizeof(struct lysc_type_leafref));
3085 }
3086 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01003087 case LY_TYPE_INST:
3088 /* RFC 7950 9.9.3 - require-instance */
3089 if (type_p->flags & LYS_SET_REQINST) {
3090 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
3091 } else {
3092 /* default is true */
3093 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
3094 }
3095
3096 if (tpdfname) {
3097 type_p->compiled = *type;
3098 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3099 }
3100 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003101 case LY_TYPE_UNION:
3102 un = (struct lysc_type_union*)(*type);
3103
3104 /* RFC 7950 7.4 - type */
3105 if (type_p->types) {
3106 if (base) {
3107 /* only the directly derived union can contain types specification */
3108 if (tpdfname) {
3109 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3110 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
3111 tpdfname);
3112 } else {
3113 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3114 "Invalid type substatement for the type not directly derived from union built-in type.");
3115 free(*type);
3116 *type = NULL;
3117 }
3118 return LY_EVALID;
3119 }
3120 /* compile the type */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003121 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003122 for (LY_ARRAY_SIZE_TYPE u = 0, additional = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02003123 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01003124 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
3125 /* add space for additional types from the union subtype */
3126 un_aux = (struct lysc_type_union *)un->types[u + additional];
Radek Krejcic4fa0292020-05-14 10:54:49 +02003127 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t*)(type_p->types) - 1)) + additional + LY_ARRAY_SIZE(un_aux->types) - 1,
3128 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003129
3130 /* copy subtypes of the subtype union */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003131 for (LY_ARRAY_SIZE_TYPE v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003132 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
3133 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
3134 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
3135 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3136 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
3137 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
3138 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
3139 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
3140 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
3141 /* TODO extensions */
3142
3143 } else {
3144 un->types[u + additional] = un_aux->types[v];
3145 ++un_aux->types[v]->refcount;
3146 }
3147 ++additional;
3148 LY_ARRAY_INCREMENT(un->types);
3149 }
3150 /* compensate u increment in main loop */
3151 --additional;
3152
3153 /* free the replaced union subtype */
3154 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
3155 } else {
3156 LY_ARRAY_INCREMENT(un->types);
3157 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01003158 }
3159 }
3160
3161 if (!base && !type_p->flags) {
3162 /* type derived from union built-in type must contain at least one type */
3163 if (tpdfname) {
3164 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
3165 } else {
3166 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
3167 free(*type);
3168 *type = NULL;
3169 }
3170 return LY_EVALID;
3171 }
3172
3173 if (tpdfname) {
3174 type_p->compiled = *type;
3175 *type = calloc(1, sizeof(struct lysc_type_union));
3176 }
3177 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003178 case LY_TYPE_BOOL:
3179 case LY_TYPE_EMPTY:
3180 case LY_TYPE_UNKNOWN: /* just to complete switch */
3181 break;
3182 }
3183 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
3184done:
3185 return ret;
3186}
3187
Radek Krejcia3045382018-11-22 14:30:31 +01003188/**
3189 * @brief Compile information about the leaf/leaf-list's type.
3190 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01003191 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
3192 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3193 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3194 * @param[in] context_name Name of the context node or referencing typedef for logging.
3195 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01003196 * @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 +01003197 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01003198 * @return LY_ERR value.
3199 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01003200static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01003201lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejciec4da802019-05-02 13:02:41 +02003202 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01003203{
3204 LY_ERR ret = LY_SUCCESS;
3205 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003206 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01003207 struct type_context {
3208 const struct lysp_tpdf *tpdf;
3209 struct lysp_node *node;
3210 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003211 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01003212 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003213 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003214 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01003215 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02003216 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01003217
3218 (*type) = NULL;
3219
3220 tctx = calloc(1, sizeof *tctx);
3221 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01003222 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01003223 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
3224 ret == LY_SUCCESS;
3225 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
3226 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
3227 if (basetype) {
3228 break;
3229 }
3230
3231 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003232 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3233 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003234 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3235
Radek Krejcicdfecd92018-11-26 11:27:32 +01003236 if (units && !*units) {
3237 /* inherit units */
3238 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3239 }
Radek Krejci01342af2019-01-03 15:18:08 +01003240 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003241 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003242 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003243 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003244 }
Radek Krejci01342af2019-01-03 15:18:08 +01003245 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003246 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3247 break;
3248 }
3249
Radek Krejci19a96102018-11-15 13:38:09 +01003250 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003251 /* it is not necessary to continue, the rest of the chain was already compiled,
3252 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003253 basetype = tctx->tpdf->type.compiled->basetype;
3254 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003255 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003256 dummyloops = 1;
3257 goto preparenext;
3258 } else {
3259 tctx = NULL;
3260 break;
3261 }
Radek Krejci19a96102018-11-15 13:38:09 +01003262 }
3263
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003264 /* circular typedef reference detection */
3265 for (u = 0; u < tpdf_chain.count; u++) {
3266 /* local part */
3267 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3268 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3269 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3270 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3271 free(tctx);
3272 ret = LY_EVALID;
3273 goto cleanup;
3274 }
3275 }
3276 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3277 /* global part for unions corner case */
3278 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3279 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3280 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3281 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3282 free(tctx);
3283 ret = LY_EVALID;
3284 goto cleanup;
3285 }
3286 }
3287
Radek Krejci19a96102018-11-15 13:38:09 +01003288 /* store information for the following processing */
3289 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3290
Radek Krejcicdfecd92018-11-26 11:27:32 +01003291preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003292 /* prepare next loop */
3293 tctx_prev = tctx;
3294 tctx = calloc(1, sizeof *tctx);
3295 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3296 }
3297 free(tctx);
3298
3299 /* allocate type according to the basetype */
3300 switch (basetype) {
3301 case LY_TYPE_BINARY:
3302 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003303 break;
3304 case LY_TYPE_BITS:
3305 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003306 break;
3307 case LY_TYPE_BOOL:
3308 case LY_TYPE_EMPTY:
3309 *type = calloc(1, sizeof(struct lysc_type));
3310 break;
3311 case LY_TYPE_DEC64:
3312 *type = calloc(1, sizeof(struct lysc_type_dec));
3313 break;
3314 case LY_TYPE_ENUM:
3315 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003316 break;
3317 case LY_TYPE_IDENT:
3318 *type = calloc(1, sizeof(struct lysc_type_identityref));
3319 break;
3320 case LY_TYPE_INST:
3321 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3322 break;
3323 case LY_TYPE_LEAFREF:
3324 *type = calloc(1, sizeof(struct lysc_type_leafref));
3325 break;
3326 case LY_TYPE_STRING:
3327 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003328 break;
3329 case LY_TYPE_UNION:
3330 *type = calloc(1, sizeof(struct lysc_type_union));
3331 break;
3332 case LY_TYPE_INT8:
3333 case LY_TYPE_UINT8:
3334 case LY_TYPE_INT16:
3335 case LY_TYPE_UINT16:
3336 case LY_TYPE_INT32:
3337 case LY_TYPE_UINT32:
3338 case LY_TYPE_INT64:
3339 case LY_TYPE_UINT64:
3340 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003341 break;
3342 case LY_TYPE_UNKNOWN:
3343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3344 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3345 ret = LY_EVALID;
3346 goto cleanup;
3347 }
3348 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003349 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003350 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3351 ly_data_type2str[basetype]);
3352 free(*type);
3353 (*type) = NULL;
3354 ret = LY_EVALID;
3355 goto cleanup;
3356 }
3357
3358 /* get restrictions from the referred typedefs */
3359 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3360 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003361
3362 /* remember the typedef context for circular check */
3363 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3364
Radek Krejci43699232018-11-23 14:59:46 +01003365 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003366 base = tctx->tpdf->type.compiled;
3367 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003368 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003369 /* no change, just use the type information from the base */
3370 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3371 ++base->refcount;
3372 continue;
3373 }
3374
3375 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003376 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3377 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3378 tctx->tpdf->name, ly_data_type2str[basetype]);
3379 ret = LY_EVALID;
3380 goto cleanup;
3381 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3382 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3383 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3384 tctx->tpdf->name, tctx->tpdf->dflt);
3385 ret = LY_EVALID;
3386 goto cleanup;
3387 }
3388
Radek Krejci19a96102018-11-15 13:38:09 +01003389 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003390 /* TODO user type plugins */
3391 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003392 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003393 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 +01003394 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003395 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003396 LY_CHECK_GOTO(ret, cleanup);
3397 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003398 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003399 /* remove the processed typedef contexts from the stack for circular check */
3400 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003401
Radek Krejcic5c27e52018-11-15 14:38:11 +01003402 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003403 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003404 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003405 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003406 /* TODO user type plugins */
3407 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003408 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003409 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 +01003410 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003411 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003412 /* no specific restriction in leaf's type definition, copy from the base */
3413 free(*type);
3414 (*type) = base;
3415 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003416 }
Radek Krejcia1911222019-07-22 17:24:50 +02003417 if (dflt && !(*type)->dflt) {
3418 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003419 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003420 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3421 (*type)->dflt->realtype = (*type);
3422 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3423 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003424 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003425 if (err) {
3426 ly_err_print(err);
3427 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3428 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3429 ly_err_free(err);
3430 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003431 if (ret == LY_EINCOMPLETE) {
3432 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003433 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003434
3435 /* but in general result is so far ok */
3436 ret = LY_SUCCESS;
3437 }
Radek Krejcia1911222019-07-22 17:24:50 +02003438 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003439 }
Radek Krejci19a96102018-11-15 13:38:09 +01003440
Radek Krejci0935f412019-08-20 16:15:18 +02003441 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003442
3443cleanup:
3444 ly_set_erase(&tpdf_chain, free);
3445 return ret;
3446}
3447
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003448/**
3449 * @brief Compile status information of the given node.
3450 *
3451 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3452 * has the status correctly set during the compilation.
3453 *
3454 * @param[in] ctx Compile context
3455 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3456 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3457 * the compatibility with the parent's status value.
3458 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3459 * @return LY_ERR value.
3460 */
3461static LY_ERR
3462lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3463{
3464 /* status - it is not inherited by specification, but it does not make sense to have
3465 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3466 if (!((*node_flags) & LYS_STATUS_MASK)) {
3467 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3468 if ((parent_flags & 0x3) != 0x3) {
3469 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3470 * combination of bits (0x3) which marks the uses_status value */
3471 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3472 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3473 }
3474 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3475 } else {
3476 (*node_flags) |= LYS_STATUS_CURR;
3477 }
3478 } else if (parent_flags & LYS_STATUS_MASK) {
3479 /* check status compatibility with the parent */
3480 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3481 if ((*node_flags) & LYS_STATUS_CURR) {
3482 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3483 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3484 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3485 } else { /* LYS_STATUS_DEPRC */
3486 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3487 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3488 }
3489 return LY_EVALID;
3490 }
3491 }
3492 return LY_SUCCESS;
3493}
3494
Radek Krejci8cce8532019-03-05 11:27:45 +01003495/**
3496 * @brief Check uniqness of the node/action/notification name.
3497 *
3498 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3499 * structures, but they share the namespace so we need to check their name collisions.
3500 *
3501 * @param[in] ctx Compile context.
3502 * @param[in] children List (linked list) of data nodes to go through.
3503 * @param[in] actions List (sized array) of actions or RPCs to go through.
3504 * @param[in] notifs List (sized array) of Notifications to go through.
3505 * @param[in] name Name of the item to find in the given lists.
3506 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3507 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3508 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3509 */
3510static LY_ERR
3511lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3512 const struct lysc_action *actions, const struct lysc_notif *notifs,
3513 const char *name, void *exclude)
3514{
3515 const struct lysc_node *iter;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003516 LY_ARRAY_SIZE_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003517
3518 LY_LIST_FOR(children, iter) {
3519 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3520 goto error;
3521 }
3522 }
3523 LY_ARRAY_FOR(actions, u) {
3524 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3525 goto error;
3526 }
3527 }
3528 LY_ARRAY_FOR(notifs, u) {
3529 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3530 goto error;
3531 }
3532 }
3533 return LY_SUCCESS;
3534error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003536 return LY_EEXIST;
3537}
3538
Radek Krejciec4da802019-05-02 13:02:41 +02003539static 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 +01003540
Radek Krejcia3045382018-11-22 14:30:31 +01003541/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003542 * @brief Compile parsed RPC/action schema node information.
3543 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003544 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003545 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003546 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3547 * @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).
3548 * Zero means no uses, non-zero value with no status bit set mean the default status.
3549 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3550 */
3551static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003552lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003553 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3554{
3555 LY_ERR ret = LY_SUCCESS;
3556 struct lysp_node *child_p;
3557 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003558 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003559
Radek Krejci327de162019-06-14 12:52:07 +02003560 lysc_update_path(ctx, parent, action_p->name);
3561
Radek Krejci8cce8532019-03-05 11:27:45 +01003562 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3563 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3564 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3565 action_p->name, action)) {
3566 return LY_EVALID;
3567 }
3568
Radek Krejciec4da802019-05-02 13:02:41 +02003569 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003570 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003571 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003572 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003573 return LY_EVALID;
3574 }
3575
Michal Vasko1bf09392020-03-27 12:38:10 +01003576 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003577 action->module = ctx->mod;
3578 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003579 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003580 action->sp = action_p;
3581 }
3582 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3583
3584 /* status - it is not inherited by specification, but it does not make sense to have
3585 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003586 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003587
3588 DUP_STRING(ctx->ctx, action_p->name, action->name);
3589 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3590 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003591 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003592 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003593
3594 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003595 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003596 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003597 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 +02003598 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003599 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003600 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003601 }
Radek Krejci327de162019-06-14 12:52:07 +02003602 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003603 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003604
3605 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003606 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003607 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003608 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 +02003609 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003610 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003611 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003612 }
Radek Krejci327de162019-06-14 12:52:07 +02003613 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003614 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003615
3616 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3617 /* do not check "must" semantics in a grouping */
3618 ly_set_add(&ctx->unres, action, 0);
3619 }
3620
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003621cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003622 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003623 return ret;
3624}
3625
3626/**
Radek Krejci43981a32019-04-12 09:44:11 +02003627 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003628 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003629 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003630 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3631 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3632 * @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 +02003633 * Zero means no uses, non-zero value with no status bit set mean the default status.
3634 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3635 */
3636static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003637lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003638 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3639{
3640 LY_ERR ret = LY_SUCCESS;
3641 struct lysp_node *child_p;
3642 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003643 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003644
Radek Krejci327de162019-06-14 12:52:07 +02003645 lysc_update_path(ctx, parent, notif_p->name);
3646
Radek Krejcifc11bd72019-04-11 16:00:05 +02003647 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3648 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3649 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3650 notif_p->name, notif)) {
3651 return LY_EVALID;
3652 }
3653
Radek Krejciec4da802019-05-02 13:02:41 +02003654 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003655 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3656 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003657 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003658 return LY_EVALID;
3659 }
3660
3661 notif->nodetype = LYS_NOTIF;
3662 notif->module = ctx->mod;
3663 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003664 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003665 notif->sp = notif_p;
3666 }
3667 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3668
3669 /* status - it is not inherited by specification, but it does not make sense to have
3670 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3671 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3672
3673 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3674 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3675 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003676 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003677 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003678 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3679 /* do not check "must" semantics in a grouping */
3680 ly_set_add(&ctx->unres, notif, 0);
3681 }
Radek Krejci0935f412019-08-20 16:15:18 +02003682 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003683
Radek Krejciec4da802019-05-02 13:02:41 +02003684 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003685 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003686 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003687 }
3688
Radek Krejci327de162019-06-14 12:52:07 +02003689 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003690cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003691 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003692 return ret;
3693}
3694
3695/**
Radek Krejcia3045382018-11-22 14:30:31 +01003696 * @brief Compile parsed container node information.
3697 * @param[in] ctx Compile context
3698 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003699 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3700 * is enriched with the container-specific information.
3701 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3702 */
Radek Krejci19a96102018-11-15 13:38:09 +01003703static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003704lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003705{
3706 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3707 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3708 struct lysp_node *child_p;
3709 unsigned int u;
3710 LY_ERR ret = LY_SUCCESS;
3711
Radek Krejcife909632019-02-12 15:34:42 +01003712 if (cont_p->presence) {
3713 cont->flags |= LYS_PRESENCE;
3714 }
3715
Radek Krejci19a96102018-11-15 13:38:09 +01003716 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003717 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003718 }
3719
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003720 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003721 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3722 /* do not check "must" semantics in a grouping */
3723 ly_set_add(&ctx->unres, cont, 0);
3724 }
Radek Krejciec4da802019-05-02 13:02:41 +02003725 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3726 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003727
3728done:
3729 return ret;
3730}
3731
Radek Krejci33f72892019-02-21 10:36:58 +01003732/*
3733 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3734 * @param[in] ctx Compile context.
3735 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3736 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003737 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3738 * @return LY_ERR value.
3739 */
3740static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003741lys_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 +01003742{
Radek Krejci33f72892019-02-21 10:36:58 +01003743 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3744
Radek Krejciec4da802019-05-02 13:02:41 +02003745 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 +01003746 leaf->units ? NULL : &leaf->units));
3747 if (leaf->nodetype == LYS_LEAFLIST) {
3748 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003749 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3750 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003751 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003752 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3753 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3754 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3755 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003756 LY_ARRAY_INCREMENT(llist->dflts);
3757 }
3758 } else {
3759 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003760 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003761 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3762 leaf->dflt->realtype = leaf->type->dflt->realtype;
3763 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3764 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003765 }
3766 }
3767 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3768 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3769 ly_set_add(&ctx->unres, leaf, 0);
3770 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003771 LY_ARRAY_SIZE_TYPE u;
Radek Krejci33f72892019-02-21 10:36:58 +01003772 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3773 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3774 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3775 ly_set_add(&ctx->unres, leaf, 0);
3776 }
3777 }
3778 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003779 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3781 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3782 return LY_EVALID;
3783 }
3784 }
3785
Radek Krejci33f72892019-02-21 10:36:58 +01003786 return LY_SUCCESS;
3787}
3788
Radek Krejcia3045382018-11-22 14:30:31 +01003789/**
3790 * @brief Compile parsed leaf node information.
3791 * @param[in] ctx Compile context
3792 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003793 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3794 * is enriched with the leaf-specific information.
3795 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3796 */
Radek Krejci19a96102018-11-15 13:38:09 +01003797static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003798lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003799{
3800 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3801 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3802 unsigned int u;
3803 LY_ERR ret = LY_SUCCESS;
3804
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003805 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003806 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3807 /* do not check "must" semantics in a grouping */
3808 ly_set_add(&ctx->unres, leaf, 0);
3809 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003810 if (leaf_p->units) {
3811 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3812 leaf->flags |= LYS_SET_UNITS;
3813 }
Radek Krejcia1911222019-07-22 17:24:50 +02003814
3815 /* the dflt member is just filled to avoid getting the default value from the type */
3816 leaf->dflt = (void*)leaf_p->dflt;
3817 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003818 if (ret) {
3819 leaf->dflt = NULL;
3820 return ret;
3821 }
Radek Krejcia1911222019-07-22 17:24:50 +02003822
Radek Krejciccd20f12019-02-15 14:12:27 +01003823 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003824 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003825 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003826 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3827 leaf->dflt->realtype = leaf->type;
3828 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3829 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003830 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003831 leaf->dflt->realtype->refcount++;
3832 if (err) {
3833 ly_err_print(err);
3834 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3835 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3836 ly_err_free(err);
3837 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003838 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003839 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003840 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003841
3842 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003843 ret = LY_SUCCESS;
3844 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003845 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003846 leaf->flags |= LYS_SET_DFLT;
3847 }
Radek Krejci43699232018-11-23 14:59:46 +01003848
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003849
Radek Krejci19a96102018-11-15 13:38:09 +01003850done:
3851 return ret;
3852}
3853
Radek Krejcia3045382018-11-22 14:30:31 +01003854/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003855 * @brief Compile parsed leaf-list node information.
3856 * @param[in] ctx Compile context
3857 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003858 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3859 * is enriched with the leaf-list-specific information.
3860 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3861 */
3862static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003863lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003864{
3865 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3866 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003867 LY_ARRAY_SIZE_TYPE u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003868 LY_ERR ret = LY_SUCCESS;
3869
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003870 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003871 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3872 /* do not check "must" semantics in a grouping */
3873 ly_set_add(&ctx->unres, llist, 0);
3874 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003875 if (llist_p->units) {
3876 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3877 llist->flags |= LYS_SET_UNITS;
3878 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003879
Radek Krejcia1911222019-07-22 17:24:50 +02003880 /* the dflts member is just filled to avoid getting the default value from the type */
3881 llist->dflts = (void*)llist_p->dflts;
3882 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Michal Vasko6a044b22020-01-15 12:25:39 +01003883 if (ret != LY_SUCCESS) {
3884 /* make sure the defaults are freed correctly */
3885 if (llist_p->dflts) {
3886 llist->dflts = NULL;
3887 }
3888 return ret;
3889 }
3890
Radek Krejci0e5d8382018-11-28 16:37:53 +01003891 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003892 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003893 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003894 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3895 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003896 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003897 LY_ARRAY_INCREMENT(llist->dflts_mods);
3898 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003899 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003900 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3901 llist->dflts[u]->realtype = llist->type;
3902 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3903 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003904 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003905 llist->dflts[u]->realtype->refcount++;
3906 if (err) {
3907 ly_err_print(err);
3908 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3909 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3910 ly_err_free(err);
3911 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003912 if (ret == LY_EINCOMPLETE) {
3913 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003914 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003915
3916 /* but in general result is so far ok */
3917 ret = LY_SUCCESS;
3918 }
Radek Krejcia1911222019-07-22 17:24:50 +02003919 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003920 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003921 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003922 }
Radek Krejcia1911222019-07-22 17:24:50 +02003923 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3924 /* configuration data values must be unique - so check the default values */
3925 LY_ARRAY_FOR(llist->dflts, u) {
3926 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3927 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3928 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003929 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 +02003930 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3931 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3932 if (dynamic) {
3933 free((char*)val);
3934 }
3935 return LY_EVALID;
3936 }
3937 }
3938 }
3939 }
3940
3941 /* 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 +01003942
3943 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003944 if (llist->min) {
3945 llist->flags |= LYS_MAND_TRUE;
3946 }
Radek Krejcib7408632018-11-28 17:12:11 +01003947 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003948
Radek Krejci0e5d8382018-11-28 16:37:53 +01003949done:
3950 return ret;
3951}
3952
3953/**
Radek Krejci7af64242019-02-18 13:07:53 +01003954 * @brief Compile information about list's uniques.
3955 * @param[in] ctx Compile context.
3956 * @param[in] context_module Module where the prefixes are going to be resolved.
3957 * @param[in] uniques Sized array list of unique statements.
3958 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3959 * @return LY_ERR value.
3960 */
3961static LY_ERR
3962lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3963{
3964 LY_ERR ret = LY_SUCCESS;
3965 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003966 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003967 const char *keystr, *delim;
3968 size_t len;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003969 LY_ARRAY_SIZE_TYPE v;
Radek Krejci7af64242019-02-18 13:07:53 +01003970 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003971 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003972
3973 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3974 config = -1;
3975 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3976 keystr = uniques[v];
3977 while (keystr) {
3978 delim = strpbrk(keystr, " \t\n");
3979 if (delim) {
3980 len = delim - keystr;
3981 while (isspace(*delim)) {
3982 ++delim;
3983 }
3984 } else {
3985 len = strlen(keystr);
3986 }
3987
3988 /* unique node must be present */
3989 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003990 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3991 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003992 if (ret != LY_SUCCESS) {
3993 if (ret == LY_EDENIED) {
3994 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003995 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003996 len, keystr, lys_nodetype2str((*key)->nodetype));
3997 }
3998 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003999 } else if (flags) {
4000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4001 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01004002 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004003 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01004004 }
4005
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004006
Radek Krejci7af64242019-02-18 13:07:53 +01004007 /* all referenced leafs must be of the same config type */
4008 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
4009 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01004010 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01004011 return LY_EVALID;
4012 } else if ((*key)->flags & LYS_CONFIG_W) {
4013 config = 1;
4014 } else { /* LYS_CONFIG_R */
4015 config = 0;
4016 }
4017
Michal Vasko14654712020-02-06 08:35:21 +01004018 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
4019 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
4020 if (parent->nodetype == LYS_LIST) {
4021 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4022 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
4023 return LY_EVALID;
4024 }
4025 }
4026
Radek Krejci7af64242019-02-18 13:07:53 +01004027 /* check status */
4028 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
4029 (*key)->flags, (*key)->module, (*key)->name));
4030
4031 /* mark leaf as unique */
4032 (*key)->flags |= LYS_UNIQUE;
4033
4034 /* next unique value in line */
4035 keystr = delim;
4036 }
4037 /* next unique definition */
4038 }
4039
4040 return LY_SUCCESS;
4041}
4042
4043/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004044 * @brief Compile parsed list node information.
4045 * @param[in] ctx Compile context
4046 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004047 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4048 * is enriched with the list-specific information.
4049 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4050 */
4051static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004052lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004053{
4054 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
4055 struct lysc_node_list *list = (struct lysc_node_list*)node;
4056 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02004057 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004058 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01004059 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004060 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004061 LY_ERR ret = LY_SUCCESS;
4062
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004063 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01004064 if (list->min) {
4065 list->flags |= LYS_MAND_TRUE;
4066 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004067 list->max = list_p->max ? list_p->max : (uint32_t)-1;
4068
4069 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004070 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004071 }
4072
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004073 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004074 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4075 /* do not check "must" semantics in a grouping */
4076 ly_set_add(&ctx->unres, list, 0);
4077 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004078
4079 /* keys */
4080 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
4081 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
4082 return LY_EVALID;
4083 }
4084
4085 /* find all the keys (must be direct children) */
4086 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02004087 if (!keystr) {
4088 /* keyless list */
4089 list->flags |= LYS_KEYLESS;
4090 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004091 while (keystr) {
4092 delim = strpbrk(keystr, " \t\n");
4093 if (delim) {
4094 len = delim - keystr;
4095 while (isspace(*delim)) {
4096 ++delim;
4097 }
4098 } else {
4099 len = strlen(keystr);
4100 }
4101
4102 /* key node must be present */
Michal Vaskoe444f752020-02-10 12:20:06 +01004103 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 +02004104 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4106 "The list's key \"%.*s\" not found.", len, keystr);
4107 return LY_EVALID;
4108 }
4109 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004110 if (key->flags & LYS_KEY) {
4111 /* the node was already marked as a key */
4112 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4113 "Duplicated key identifier \"%.*s\".", len, keystr);
4114 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004115 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004116
4117 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004118 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004119 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004120 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
4121 return LY_EVALID;
4122 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004123 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004124 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004125 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004127 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004128 return LY_EVALID;
4129 }
4130 } else {
4131 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004132 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004133 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004134 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004135 return LY_EVALID;
4136 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004137 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004138 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004139 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004140 return LY_EVALID;
4141 }
4142 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004143
4144 /* check status */
4145 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02004146 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01004147
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004148 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004149 if (key->dflt) {
4150 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
4151 lysc_type_free(ctx->ctx, key->dflt->realtype);
4152 free(key->dflt);
4153 key->dflt = NULL;
4154 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004155 }
4156 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004157 key->flags |= LYS_KEY;
4158
4159 /* move it to the correct position */
4160 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
4161 /* fix links in closest previous siblings of the key */
4162 if (key->next) {
4163 key->next->prev = key->prev;
4164 } else {
4165 /* last child */
4166 list->child->prev = key->prev;
4167 }
4168 if (key->prev->next) {
4169 key->prev->next = key->next;
4170 }
4171 /* fix links in the key */
4172 if (prev_key) {
4173 key->prev = (struct lysc_node*)prev_key;
4174 key->next = prev_key->next;
4175 } else {
4176 key->prev = list->child->prev;
4177 key->next = list->child;
4178 }
4179 /* fix links in closes future siblings of the key */
4180 if (prev_key) {
4181 if (prev_key->next) {
4182 prev_key->next->prev = (struct lysc_node*)key;
4183 } else {
4184 list->child->prev = (struct lysc_node*)key;
4185 }
4186 prev_key->next = (struct lysc_node*)key;
4187 } else {
4188 list->child->prev = (struct lysc_node*)key;
4189 }
4190 /* fix links in parent */
4191 if (!key->prev->next) {
4192 list->child = (struct lysc_node*)key;
4193 }
4194 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004195
4196 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004197 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004198 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02004199 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004200 }
4201
4202 /* uniques */
4203 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01004204 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004205 }
4206
Radek Krejciec4da802019-05-02 13:02:41 +02004207 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
4208 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004209
4210done:
4211 return ret;
4212}
4213
Radek Krejcib56c7502019-02-13 14:19:54 +01004214/**
4215 * @brief Do some checks and set the default choice's case.
4216 *
4217 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
4218 *
4219 * @param[in] ctx Compile context.
4220 * @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,
4221 * not the reference to the imported module.
4222 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
4223 * @return LY_ERR value.
4224 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004225static LY_ERR
4226lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
4227{
4228 struct lysc_node *iter, *node = (struct lysc_node*)ch;
4229 const char *prefix = NULL, *name;
4230 size_t prefix_len = 0;
4231
4232 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4233 name = strchr(dflt, ':');
4234 if (name) {
4235 prefix = dflt;
4236 prefix_len = name - prefix;
4237 ++name;
4238 } else {
4239 name = dflt;
4240 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02004241 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004242 /* prefixed default case make sense only for the prefix of the schema itself */
4243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4244 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
4245 prefix_len, prefix);
4246 return LY_EVALID;
4247 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004248 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 +01004249 if (!ch->dflt) {
4250 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4251 "Default case \"%s\" not found.", dflt);
4252 return LY_EVALID;
4253 }
4254 /* no mandatory nodes directly under the default case */
4255 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01004256 if (iter->parent != (struct lysc_node*)ch->dflt) {
4257 break;
4258 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004259 if (iter->flags & LYS_MAND_TRUE) {
4260 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4261 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
4262 return LY_EVALID;
4263 }
4264 }
Radek Krejci01342af2019-01-03 15:18:08 +01004265 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004266 return LY_SUCCESS;
4267}
4268
Radek Krejciccd20f12019-02-15 14:12:27 +01004269static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004270lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01004271{
4272 struct lys_module *mod;
4273 const char *prefix = NULL, *name;
4274 size_t prefix_len = 0;
4275 struct lysc_node_case *cs;
4276 struct lysc_node *node;
4277
4278 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4279 name = strchr(dflt, ':');
4280 if (name) {
4281 prefix = dflt;
4282 prefix_len = name - prefix;
4283 ++name;
4284 } else {
4285 name = dflt;
4286 }
4287 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
4288 if (prefix) {
4289 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
4290 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004291 "Invalid deviation adding \"default\" property \"%s\" of choice. "
4292 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004293 return LY_EVALID;
4294 }
4295 } else {
4296 mod = ctx->mod;
4297 }
4298 /* get the default case */
Michal Vaskoe444f752020-02-10 12:20:06 +01004299 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 +01004300 if (!cs) {
4301 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004302 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004303 return LY_EVALID;
4304 }
4305
4306 /* check that there is no mandatory node */
4307 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01004308 if (node->parent != (struct lysc_node*)cs) {
4309 break;
4310 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004311 if (node->flags & LYS_MAND_TRUE) {
4312 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004313 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4314 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004315 return LY_EVALID;
4316 }
4317 }
4318
4319 /* set the default case in choice */
4320 ch->dflt = cs;
4321 cs->flags |= LYS_SET_DFLT;
4322
4323 return LY_SUCCESS;
4324}
4325
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004326/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004327 * @brief Compile parsed choice node information.
4328 * @param[in] ctx Compile context
4329 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004330 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004331 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004332 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4333 */
4334static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004335lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004336{
4337 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4338 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4339 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004340 LY_ERR ret = LY_SUCCESS;
4341
Radek Krejci056d0a82018-12-06 16:57:25 +01004342 LY_LIST_FOR(ch_p->child, child_p) {
4343 if (child_p->nodetype == LYS_CASE) {
4344 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004345 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004346 }
4347 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004348 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004349 }
4350 }
4351
4352 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004353 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004354 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004355 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004356
Radek Krejci9800fb82018-12-13 14:26:23 +01004357 return ret;
4358}
4359
4360/**
4361 * @brief Compile parsed anydata or anyxml node information.
4362 * @param[in] ctx Compile context
4363 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004364 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4365 * is enriched with the any-specific information.
4366 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4367 */
4368static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004369lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004370{
4371 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4372 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4373 unsigned int u;
4374 LY_ERR ret = LY_SUCCESS;
4375
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004376 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004377 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4378 /* do not check "must" semantics in a grouping */
4379 ly_set_add(&ctx->unres, any, 0);
4380 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004381
4382 if (any->flags & LYS_CONFIG_W) {
4383 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004384 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004385 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004386done:
4387 return ret;
4388}
4389
Radek Krejcib56c7502019-02-13 14:19:54 +01004390/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004391 * @brief Connect the node into the siblings list and check its name uniqueness.
4392 *
4393 * @param[in] ctx Compile context
4394 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4395 * the choice itself is expected instead of a specific case node.
4396 * @param[in] node Schema node to connect into the list.
4397 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004398 * 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 +01004399 */
4400static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004401lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004402{
4403 struct lysc_node **children;
4404
4405 if (node->nodetype == LYS_CASE) {
4406 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4407 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004408 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004409 }
4410 if (children) {
4411 if (!(*children)) {
4412 /* first child */
4413 *children = node;
4414 } else if (*children != node) {
4415 /* by the condition in previous branch we cover the choice/case children
4416 * - the children list is shared by the choice and the the first case, in addition
4417 * the first child of each case must be referenced from the case node. So the node is
4418 * actually always already inserted in case it is the first children - so here such
4419 * a situation actually corresponds to the first branch */
4420 /* insert at the end of the parent's children list */
4421 (*children)->prev->next = node;
4422 node->prev = (*children)->prev;
4423 (*children)->prev = node;
4424
4425 /* check the name uniqueness */
4426 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4427 lysc_node_notifs(parent), node->name, node)) {
4428 return LY_EEXIST;
4429 }
4430 }
4431 }
4432 return LY_SUCCESS;
4433}
4434
Radek Krejci95710c92019-02-11 15:49:55 +01004435/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004436 * @brief Prepare the case structure in choice node for the new data node.
4437 *
4438 * 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
4439 * created in the choice when the first child was processed.
4440 *
4441 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004442 * @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,
4443 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004444 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4445 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4446 * @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,
4447 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004448 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004449static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004450lys_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 +01004451{
4452 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004453 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004454 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004455 unsigned int u;
4456 LY_ERR ret;
4457
Radek Krejci95710c92019-02-11 15:49:55 +01004458#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004459 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004460 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004461 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4462 return NULL; \
4463 } \
4464 }
4465
Radek Krejci95710c92019-02-11 15:49:55 +01004466 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4467 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004468
4469 /* we have to add an implicit case node into the parent choice */
4470 cs = calloc(1, sizeof(struct lysc_node_case));
4471 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004472 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004473 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004474 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004475 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4476 /* the case is already present since the child is not its first children */
4477 return (struct lysc_node_case*)ch->cases->prev;
4478 }
Radek Krejci95710c92019-02-11 15:49:55 +01004479 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004480
4481 /* explicit parent case is not present (this is its first child) */
4482 cs = calloc(1, sizeof(struct lysc_node_case));
4483 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004484 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004485 cs->flags = LYS_STATUS_MASK & node_p->flags;
4486 cs->sp = node_p;
4487
Radek Krejcib1b59152019-01-07 13:21:56 +01004488 /* 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 +02004489 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004490
4491 if (node_p->when) {
4492 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004493 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004494 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004495
4496 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4497 /* do not check "when" semantics in a grouping */
4498 ly_set_add(&ctx->unres, cs, 0);
4499 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004500 }
Radek Krejciec4da802019-05-02 13:02:41 +02004501 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004502 } else {
4503 LOGINT(ctx->ctx);
4504 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004505 }
4506 cs->module = ctx->mod;
4507 cs->prev = (struct lysc_node*)cs;
4508 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004509 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004510 cs->child = child;
4511
4512 return cs;
4513error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004514 if (cs) {
4515 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4516 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004517 return NULL;
4518
4519#undef UNIQUE_CHECK
4520}
4521
Radek Krejcib56c7502019-02-13 14:19:54 +01004522/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004523 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004524 *
4525 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004526 * @param[in] node Target node where the config is supposed to be changed.
4527 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004528 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4529 * 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 +01004530 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4531 * @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 +01004532 * @return LY_ERR value.
4533 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004534static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004535lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004536 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004537{
4538 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004539 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004540
4541 if (config == (node->flags & LYS_CONFIG_MASK)) {
4542 /* nothing to do */
4543 return LY_SUCCESS;
4544 }
4545
4546 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004547 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004548 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4549 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004550 "Invalid %s of config - configuration node cannot be child of any state data node.",
4551 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004552 return LY_EVALID;
4553 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004554 node->flags |= LYS_SET_CONFIG;
4555 } else {
4556 if (node->flags & LYS_SET_CONFIG) {
4557 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4558 /* setting config flags, but have node with explicit config true */
4559 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004560 "Invalid %s of config - configuration node cannot be child of any state data node.",
4561 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004562 return LY_EVALID;
4563 }
4564 /* do not change config on nodes where the config is explicitely set, this does not apply to
4565 * nodes, which are being changed explicitly (targets of refine or deviation) */
4566 return LY_SUCCESS;
4567 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004568 }
4569 node->flags &= ~LYS_CONFIG_MASK;
4570 node->flags |= config;
4571
4572 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004573 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004574 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004575 }
4576
Radek Krejci76b3e962018-12-14 17:01:25 +01004577 return LY_SUCCESS;
4578}
4579
Radek Krejcib56c7502019-02-13 14:19:54 +01004580/**
4581 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4582 *
4583 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4584 * the flag to such parents from a mandatory children.
4585 *
4586 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4587 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4588 * (mandatory children was removed).
4589 */
Radek Krejcife909632019-02-12 15:34:42 +01004590void
4591lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4592{
4593 struct lysc_node *iter;
4594
4595 if (add) { /* set flag */
4596 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4597 parent = parent->parent) {
4598 parent->flags |= LYS_MAND_TRUE;
4599 }
4600 } else { /* unset flag */
4601 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004602 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004603 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004604 /* there is another mandatory node */
4605 return;
4606 }
4607 }
4608 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4609 parent->flags &= ~LYS_MAND_TRUE;
4610 }
4611 }
4612}
4613
Radek Krejci056d0a82018-12-06 16:57:25 +01004614/**
Radek Krejci3641f562019-02-13 15:38:40 +01004615 * @brief Internal sorting process for the lys_compile_augment_sort().
4616 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4617 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4618 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4619 */
4620static void
4621lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4622{
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004623 LY_ARRAY_SIZE_TYPE v;
Radek Krejci3641f562019-02-13 15:38:40 +01004624 size_t len;
4625
4626 len = strlen(aug_p->nodeid);
4627 LY_ARRAY_FOR(result, v) {
4628 if (strlen(result[v]->nodeid) <= len) {
4629 continue;
4630 }
4631 if (v < LY_ARRAY_SIZE(result)) {
4632 /* move the rest of array */
4633 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4634 break;
4635 }
4636 }
4637 result[v] = aug_p;
4638 LY_ARRAY_INCREMENT(result);
4639}
4640
4641/**
4642 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4643 *
4644 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4645 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4646 *
4647 * @param[in] ctx Compile context.
4648 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4649 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4650 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4651 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4652 * @return LY_ERR value.
4653 */
4654LY_ERR
4655lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4656{
4657 struct lysp_augment **result = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004658 LY_ARRAY_SIZE_TYPE u, v, count = 0;
Radek Krejci3641f562019-02-13 15:38:40 +01004659
4660 assert(augments);
4661
4662 /* get count of the augments in module and all its submodules */
4663 if (aug_p) {
4664 count += LY_ARRAY_SIZE(aug_p);
4665 }
4666 LY_ARRAY_FOR(inc_p, u) {
4667 if (inc_p[u].submodule->augments) {
4668 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4669 }
4670 }
4671
4672 if (!count) {
4673 *augments = NULL;
4674 return LY_SUCCESS;
4675 }
4676 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4677
4678 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4679 * together, so there can be even /z/y betwwen them. */
4680 LY_ARRAY_FOR(aug_p, u) {
4681 lys_compile_augment_sort_(&aug_p[u], result);
4682 }
4683 LY_ARRAY_FOR(inc_p, u) {
4684 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4685 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4686 }
4687 }
4688
4689 *augments = result;
4690 return LY_SUCCESS;
4691}
4692
4693/**
4694 * @brief Compile the parsed augment connecting it into its target.
4695 *
4696 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4697 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4698 * are already implemented and compiled.
4699 *
4700 * @param[in] ctx Compile context.
4701 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004702 * @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
4703 * children in case of the augmenting uses data.
4704 * @return LY_SUCCESS on success.
4705 * @return LY_EVALID on failure.
4706 */
4707LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004708lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004709{
4710 LY_ERR ret = LY_SUCCESS;
4711 struct lysp_node *node_p, *case_node_p;
4712 struct lysc_node *target; /* target target of the augment */
4713 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004714 struct lysc_when **when, *when_shared;
4715 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004716 uint16_t flags = 0;
4717 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004718 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004719
Radek Krejci327de162019-06-14 12:52:07 +02004720 lysc_update_path(ctx, NULL, "{augment}");
4721 lysc_update_path(ctx, NULL, aug_p->nodeid);
4722
Radek Krejci7af64242019-02-18 13:07:53 +01004723 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004724 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004725 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004726 if (ret != LY_SUCCESS) {
4727 if (ret == LY_EDENIED) {
4728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4729 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4730 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4731 }
4732 return LY_EVALID;
4733 }
4734
4735 /* check for mandatory nodes
4736 * - new cases augmenting some choice can have mandatory nodes
4737 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4738 */
Radek Krejci733988a2019-02-15 15:12:44 +01004739 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004740 allow_mandatory = 1;
4741 }
4742
4743 when_shared = NULL;
4744 LY_LIST_FOR(aug_p->child, node_p) {
4745 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4746 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
Michal Vasko1bf09392020-03-27 12:38:10 +01004747 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004748 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4749 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004750 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004751 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4752 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004753 return LY_EVALID;
4754 }
4755
4756 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004757 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004758 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004759 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004760 } else {
4761 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004762 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004763 }
4764 }
Radek Krejciec4da802019-05-02 13:02:41 +02004765 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004766
Radek Krejcife13da42019-02-15 14:51:01 +01004767 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4768 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004769 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004770 /* 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 +01004771 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 +01004772 } else if (target->nodetype == LYS_CHOICE) {
4773 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4774 node = ((struct lysc_node_choice*)target)->cases->prev;
4775 } else {
4776 /* the compiled node is the last child of the target */
Radek Krejci7c7783d2020-04-08 15:34:39 +02004777 node = (struct lysc_node*)lysc_node_children(target, flags);
4778 if (!node) {
4779 /* there is no data children (compiled nodes is e.g. notification or action or nothing) */
4780 break;
4781 }
4782 node = node->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004783 }
4784
Radek Krejci733988a2019-02-15 15:12:44 +01004785 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004786 node->flags &= ~LYS_MAND_TRUE;
4787 lys_compile_mandatory_parents(target, 0);
4788 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004789 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004790 return LY_EVALID;
4791 }
4792
4793 /* pass augment's when to all the children */
4794 if (aug_p->when) {
4795 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4796 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004797 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004798 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004799
4800 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4801 /* do not check "when" semantics in a grouping */
Michal Vasko5c4e5892019-11-14 12:31:38 +01004802 ly_set_add(&ctx->unres, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004803 }
4804
Radek Krejci3641f562019-02-13 15:38:40 +01004805 when_shared = *when;
4806 } else {
4807 ++when_shared->refcount;
4808 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004809
4810 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4811 /* in this case check "when" again for all children because of dummy node check */
4812 ly_set_add(&ctx->unres, node, 0);
4813 }
Radek Krejci3641f562019-02-13 15:38:40 +01004814 }
4815 }
4816 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004817
Radek Krejciec4da802019-05-02 13:02:41 +02004818 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004819 switch (target->nodetype) {
4820 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004821 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004822 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004823 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004824 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004825 break;
4826 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004827 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004828 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004829 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004830 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004831 break;
4832 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004833 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004834 if (aug_p->actions) {
4835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004836 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4837 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004838 return LY_EVALID;
4839 }
4840 if (aug_p->notifs) {
4841 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004842 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004843 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004844 return LY_EVALID;
4845 }
4846 }
Radek Krejci3641f562019-02-13 15:38:40 +01004847
Radek Krejci327de162019-06-14 12:52:07 +02004848 lysc_update_path(ctx, NULL, NULL);
4849 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004850error:
Radek Krejciec4da802019-05-02 13:02:41 +02004851 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004852 return ret;
4853}
4854
4855/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004856 * @brief Apply refined or deviated mandatory flag to the target node.
4857 *
4858 * @param[in] ctx Compile context.
4859 * @param[in] node Target node where the mandatory property is supposed to be changed.
4860 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004861 * @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 +01004862 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4863 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4864 * 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 +01004865 * @return LY_ERR value.
4866 */
4867static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004868lys_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 +01004869{
4870 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4871 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004872 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4873 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004874 return LY_EVALID;
4875 }
4876
4877 if (mandatory_flag & LYS_MAND_TRUE) {
4878 /* check if node has default value */
4879 if (node->nodetype & LYS_LEAF) {
4880 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004881 if (refine_flag) {
4882 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004883 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004884 return LY_EVALID;
4885 }
Radek Krejcia1911222019-07-22 17:24:50 +02004886 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004887 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004888 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004889
4890 /* update the list of incomplete default values if needed */
4891 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4892
Radek Krejcia1911222019-07-22 17:24:50 +02004893 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4894 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4895 free(leaf->dflt);
4896 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004897 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004898 }
4899 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004900 if (refine_flag) {
4901 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004902 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004903 return LY_EVALID;
4904 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004905 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004906 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004907 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 +01004908 return LY_EVALID;
4909 }
4910
4911 node->flags &= ~LYS_MAND_FALSE;
4912 node->flags |= LYS_MAND_TRUE;
4913 lys_compile_mandatory_parents(node->parent, 1);
4914 } else {
4915 /* make mandatory false */
4916 node->flags &= ~LYS_MAND_TRUE;
4917 node->flags |= LYS_MAND_FALSE;
4918 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004919 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 +01004920 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004921 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004922 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004923 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4924 leaf->dflt->realtype = leaf->type->dflt->realtype;
4925 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4926 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004927 }
4928 }
4929 return LY_SUCCESS;
4930}
4931
4932/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004933 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4934 * If present, also apply uses's modificators.
4935 *
4936 * @param[in] ctx Compile context
4937 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004938 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4939 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4940 * the compile context.
4941 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4942 */
4943static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004944lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004945{
4946 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004947 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004948 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004949 struct lysc_node_container context_node_fake =
4950 {.nodetype = LYS_CONTAINER,
4951 .module = ctx->mod,
4952 .flags = parent ? parent->flags : 0,
4953 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004954 .prev = (struct lysc_node*)&context_node_fake,
4955 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004956 struct lysp_grp *grp = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004957 LY_ARRAY_SIZE_TYPE u, v;
4958 uint32_t grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004959 int found;
4960 const char *id, *name, *prefix;
4961 size_t prefix_len, name_len;
4962 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004963 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004964 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004965 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004966 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004967 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004968 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004969 struct lysp_augment **augments = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02004970 LY_ARRAY_SIZE_TYPE actions_index = 0, notifs_index = 0;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004971 struct lysc_notif **notifs = NULL;
4972 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004973
4974 /* search for the grouping definition */
4975 found = 0;
4976 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004977 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004978 if (prefix) {
4979 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4980 if (!mod) {
4981 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004982 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004983 return LY_EVALID;
4984 }
4985 } else {
4986 mod = ctx->mod_def;
4987 }
4988 if (mod == ctx->mod_def) {
4989 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004990 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004991 LY_ARRAY_FOR(grp, u) {
4992 if (!strcmp(grp[u].name, name)) {
4993 grp = &grp[u];
4994 found = 1;
4995 break;
4996 }
4997 }
4998 }
4999 }
5000 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01005001 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01005002 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01005003 if (grp) {
5004 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
5005 if (!strcmp(grp[u].name, name)) {
5006 grp = &grp[u];
5007 found = 1;
5008 }
5009 }
5010 }
5011 if (!found && mod->parsed->includes) {
5012 /* ... and all the submodules */
5013 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
5014 grp = mod->parsed->includes[u].submodule->groupings;
5015 if (grp) {
5016 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
5017 if (!strcmp(grp[v].name, name)) {
5018 grp = &grp[v];
5019 found = 1;
5020 }
5021 }
5022 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005023 }
5024 }
5025 }
5026 if (!found) {
5027 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5028 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
5029 return LY_EVALID;
5030 }
5031
5032 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
5033 grp_stack_count = ctx->groupings.count;
5034 ly_set_add(&ctx->groupings, (void*)grp, 0);
5035 if (grp_stack_count == ctx->groupings.count) {
5036 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
5037 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5038 "Grouping \"%s\" references itself through a uses statement.", grp->name);
5039 return LY_EVALID;
5040 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005041 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5042 /* remember that the grouping is instantiated to avoid its standalone validation */
5043 grp->flags |= LYS_USED_GRP;
5044 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005045
5046 /* switch context's mod_def */
5047 mod_old = ctx->mod_def;
5048 ctx->mod_def = mod;
5049
5050 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005051 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 +01005052
Radek Krejcifc11bd72019-04-11 16:00:05 +02005053 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01005054 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01005055 /* 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 +02005056 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 +01005057
5058 /* some preparation for applying refines */
5059 if (grp->data == node_p) {
5060 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02005061 if (parent) {
5062 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5063 } else if (ctx->mod->compiled->data) {
5064 child = ctx->mod->compiled->data;
5065 } else {
5066 child = NULL;
5067 }
5068 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01005069 }
5070 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005071 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01005072 LY_LIST_FOR(context_node_fake.child, child) {
5073 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01005074
Radek Krejcifc11bd72019-04-11 16:00:05 +02005075 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01005076 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005077 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01005078 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01005079 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
5080
5081 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5082 /* do not check "when" semantics in a grouping */
5083 ly_set_add(&ctx->unres, child, 0);
5084 }
5085
Radek Krejci00b874b2019-02-12 10:54:50 +01005086 when_shared = *when;
5087 } else {
5088 ++when_shared->refcount;
5089 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01005090
5091 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5092 /* in this case check "when" again for all children because of dummy node check */
5093 ly_set_add(&ctx->unres, child, 0);
5094 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005095 }
5096 }
Radek Krejci01342af2019-01-03 15:18:08 +01005097 }
5098 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005099 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01005100 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005101 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02005102 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 +01005103 }
5104
Radek Krejcifc11bd72019-04-11 16:00:05 +02005105 /* compile actions */
5106 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
5107 if (actions) {
5108 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02005109 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005110 if (*actions && (uses_p->augments || uses_p->refines)) {
5111 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
5112 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
5113 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
5114 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5115 }
5116 }
5117
5118 /* compile notifications */
5119 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
5120 if (notifs) {
5121 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02005122 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005123 if (*notifs && (uses_p->augments || uses_p->refines)) {
5124 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
5125 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
5126 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
5127 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5128 }
5129 }
5130
5131
Radek Krejci3641f562019-02-13 15:38:40 +01005132 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005133 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01005134 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005135 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01005136 }
Radek Krejci12fb9142019-01-08 09:45:30 +01005137
Radek Krejcif0089082019-01-07 16:42:01 +01005138 /* reload previous context's mod_def */
5139 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02005140 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01005141
Radek Krejci76b3e962018-12-14 17:01:25 +01005142 /* apply refine */
5143 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02005144 lysc_update_path(ctx, NULL, rfn->nodeid);
5145
Radek Krejci7af64242019-02-18 13:07:53 +01005146 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 +01005147 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02005148 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01005149 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01005150
5151 /* default value */
5152 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01005153 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01005154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005155 "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_SIZE_TYPE" default values.",
Radek Krejci327de162019-06-14 12:52:07 +02005156 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005157 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005158 }
5159 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
5160 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005161 "Invalid refine of default - %s cannot hold default value(s).",
5162 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005163 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005164 }
5165 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02005166 struct ly_err_item *err = NULL;
5167 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
5168 if (leaf->dflt) {
5169 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02005170 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005171 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5172 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5173 } else {
5174 /* prepare a new one */
5175 leaf->dflt = calloc(1, sizeof *leaf->dflt);
5176 leaf->dflt->realtype = leaf->type;
5177 }
5178 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005179 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005180 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
5181 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005182 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005183 leaf->dflt->realtype->refcount++;
5184 if (err) {
5185 ly_err_print(err);
5186 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5187 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
5188 ly_err_free(err);
5189 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005190 if (rc == LY_EINCOMPLETE) {
5191 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005192 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005193
5194 /* but in general result is so far ok */
5195 rc = LY_SUCCESS;
5196 }
Radek Krejcia1911222019-07-22 17:24:50 +02005197 LY_CHECK_GOTO(rc, cleanup);
5198 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005199 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02005200 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
5201
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005202 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01005203 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5204 "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 +02005205 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01005206 }
Radek Krejcia1911222019-07-22 17:24:50 +02005207
5208 /* remove previous set of default values */
5209 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005210 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02005211 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
5212 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
5213 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01005214 }
Radek Krejcia1911222019-07-22 17:24:50 +02005215 LY_ARRAY_FREE(llist->dflts);
5216 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005217 LY_ARRAY_FREE(llist->dflts_mods);
5218 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005219
5220 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005221 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005222 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005223 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005224 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005225 LY_ARRAY_INCREMENT(llist->dflts_mods);
5226 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005227 LY_ARRAY_INCREMENT(llist->dflts);
5228 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
5229 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02005230 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02005231 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005232 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005233 llist->dflts[u]->realtype->refcount++;
5234 if (err) {
5235 ly_err_print(err);
5236 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5237 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
5238 ly_err_free(err);
5239 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005240 if (rc == LY_EINCOMPLETE) {
5241 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005242 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005243
5244 /* but in general result is so far ok */
5245 rc = LY_SUCCESS;
5246 }
5247 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005248 }
Radek Krejcia1911222019-07-22 17:24:50 +02005249 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005250 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01005251 if (((struct lysc_node_choice*)node)->dflt) {
5252 /* unset LYS_SET_DFLT from the current default case */
5253 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
5254 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005255 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 +01005256 }
5257 }
5258
Radek Krejci12fb9142019-01-08 09:45:30 +01005259 /* description */
5260 if (rfn->dsc) {
5261 FREE_STRING(ctx->ctx, node->dsc);
5262 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
5263 }
5264
5265 /* reference */
5266 if (rfn->ref) {
5267 FREE_STRING(ctx->ctx, node->ref);
5268 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
5269 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005270
5271 /* config */
5272 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005273 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005274 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005275 } else {
5276 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01005277 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005278 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005279 }
5280
5281 /* mandatory */
5282 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02005283 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005284 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005285
5286 /* presence */
5287 if (rfn->presence) {
5288 if (node->nodetype != LYS_CONTAINER) {
5289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005290 "Invalid refine of presence statement - %s cannot hold the presence statement.",
5291 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005292 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005293 }
5294 node->flags |= LYS_PRESENCE;
5295 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005296
5297 /* must */
5298 if (rfn->musts) {
5299 switch (node->nodetype) {
5300 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005301 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 +01005302 break;
5303 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005304 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 +01005305 break;
5306 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005307 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 +01005308 break;
5309 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005310 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 +01005311 break;
5312 case LYS_ANYXML:
5313 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005314 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 +01005315 break;
5316 default:
5317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005318 "Invalid refine of must statement - %s cannot hold any must statement.",
5319 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005320 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005321 }
Michal Vasko5d8756a2019-11-07 15:21:00 +01005322 ly_set_add(&ctx->unres, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01005323 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005324
5325 /* min/max-elements */
5326 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5327 switch (node->nodetype) {
5328 case LYS_LEAFLIST:
5329 if (rfn->flags & LYS_SET_MAX) {
5330 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5331 }
5332 if (rfn->flags & LYS_SET_MIN) {
5333 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005334 if (rfn->min) {
5335 node->flags |= LYS_MAND_TRUE;
5336 lys_compile_mandatory_parents(node->parent, 1);
5337 } else {
5338 node->flags &= ~LYS_MAND_TRUE;
5339 lys_compile_mandatory_parents(node->parent, 0);
5340 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005341 }
5342 break;
5343 case LYS_LIST:
5344 if (rfn->flags & LYS_SET_MAX) {
5345 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5346 }
5347 if (rfn->flags & LYS_SET_MIN) {
5348 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005349 if (rfn->min) {
5350 node->flags |= LYS_MAND_TRUE;
5351 lys_compile_mandatory_parents(node->parent, 1);
5352 } else {
5353 node->flags &= ~LYS_MAND_TRUE;
5354 lys_compile_mandatory_parents(node->parent, 0);
5355 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005356 }
5357 break;
5358 default:
5359 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005360 "Invalid refine of %s statement - %s cannot hold this statement.",
5361 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005362 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005363 }
5364 }
Radek Krejcif0089082019-01-07 16:42:01 +01005365
5366 /* if-feature */
5367 if (rfn->iffeatures) {
5368 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005369 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005370 }
Radek Krejci327de162019-06-14 12:52:07 +02005371
5372 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005373 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005374
Radek Krejcif2271f12019-01-07 16:42:23 +01005375 /* do some additional checks of the changed nodes when all the refines are applied */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005376 for (uint32_t i = 0; i < refined.count; ++i) {
5377 node = (struct lysc_node*)refined.objs[i];
5378 rfn = &uses_p->refines[i];
Radek Krejci327de162019-06-14 12:52:07 +02005379 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005380
5381 /* check possible conflict with default value (default added, mandatory left true) */
5382 if ((node->flags & LYS_MAND_TRUE) &&
5383 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5384 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5385 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005386 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005387 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005388 }
5389
5390 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5391 if (node->nodetype == LYS_LIST) {
5392 min = ((struct lysc_node_list*)node)->min;
5393 max = ((struct lysc_node_list*)node)->max;
5394 } else {
5395 min = ((struct lysc_node_leaflist*)node)->min;
5396 max = ((struct lysc_node_leaflist*)node)->max;
5397 }
5398 if (min > max) {
5399 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005400 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5401 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005402 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005403 }
5404 }
5405 }
5406
Radek Krejci327de162019-06-14 12:52:07 +02005407 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005408 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005409
5410cleanup:
5411 /* fix connection of the children nodes from fake context node back into the parent */
5412 if (context_node_fake.child) {
5413 context_node_fake.child->prev = child;
5414 }
5415 LY_LIST_FOR(context_node_fake.child, child) {
5416 child->parent = parent;
5417 }
5418
5419 if (uses_p->augments || uses_p->refines) {
5420 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005421 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005422 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5423 LY_ARRAY_FREE(context_node_fake.actions);
5424 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005425 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005426 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5427 LY_ARRAY_FREE(context_node_fake.notifs);
5428 }
5429 }
5430
Radek Krejcie86bf772018-12-14 11:39:53 +01005431 /* reload previous context's mod_def */
5432 ctx->mod_def = mod_old;
5433 /* remove the grouping from the stack for circular groupings dependency check */
5434 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5435 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005436 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005437 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005438
5439 return ret;
5440}
5441
Radek Krejci327de162019-06-14 12:52:07 +02005442static int
5443lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5444{
5445 struct lysp_node *iter;
5446 int len = 0;
5447
5448 *path = NULL;
5449 for (iter = node; iter && len >= 0; iter = iter->parent) {
5450 char *s = *path;
5451 char *id;
5452
5453 switch (iter->nodetype) {
5454 case LYS_USES:
5455 asprintf(&id, "{uses='%s'}", iter->name);
5456 break;
5457 case LYS_GROUPING:
5458 asprintf(&id, "{grouping='%s'}", iter->name);
5459 break;
5460 case LYS_AUGMENT:
5461 asprintf(&id, "{augment='%s'}", iter->name);
5462 break;
5463 default:
5464 id = strdup(iter->name);
5465 break;
5466 }
5467
5468 if (!iter->parent) {
5469 /* print prefix */
5470 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5471 } else {
5472 /* prefix is the same as in parent */
5473 len = asprintf(path, "/%s%s", id, s ? s : "");
5474 }
5475 free(s);
5476 free(id);
5477 }
5478
5479 if (len < 0) {
5480 free(*path);
5481 *path = NULL;
5482 } else if (len == 0) {
5483 *path = strdup("/");
5484 len = 1;
5485 }
5486 return len;
5487}
5488
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005489/**
5490 * @brief Validate groupings that were defined but not directly used in the schema itself.
5491 *
5492 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5493 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5494 */
5495static LY_ERR
5496lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5497{
5498 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005499 char *path;
5500 int len;
5501
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005502 struct lysp_node_uses fake_uses = {
5503 .parent = node_p,
5504 .nodetype = LYS_USES,
5505 .flags = 0, .next = NULL,
5506 .name = grp->name,
5507 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5508 .refines = NULL, .augments = NULL
5509 };
5510 struct lysc_node_container fake_container = {
5511 .nodetype = LYS_CONTAINER,
5512 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5513 .module = ctx->mod,
5514 .sp = NULL, .parent = NULL, .next = NULL,
5515 .prev = (struct lysc_node*)&fake_container,
5516 .name = "fake",
5517 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5518 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5519 };
5520
5521 if (grp->parent) {
5522 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5523 }
Radek Krejci327de162019-06-14 12:52:07 +02005524
5525 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5526 if (len < 0) {
5527 LOGMEM(ctx->ctx);
5528 return LY_EMEM;
5529 }
5530 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5531 ctx->path_len = (uint16_t)len;
5532 free(path);
5533
5534 lysc_update_path(ctx, NULL, "{grouping}");
5535 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005536 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005537 lysc_update_path(ctx, NULL, NULL);
5538 lysc_update_path(ctx, NULL, NULL);
5539
5540 ctx->path_len = 1;
5541 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005542
5543 /* cleanup */
5544 lysc_node_container_free(ctx->ctx, &fake_container);
5545
5546 return ret;
5547}
Radek Krejcife909632019-02-12 15:34:42 +01005548
Radek Krejcie86bf772018-12-14 11:39:53 +01005549/**
Radek Krejcia3045382018-11-22 14:30:31 +01005550 * @brief Compile parsed schema node information.
5551 * @param[in] ctx Compile context
5552 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005553 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5554 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5555 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005556 * @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).
5557 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005558 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5559 */
Radek Krejci19a96102018-11-15 13:38:09 +01005560static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005561lys_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 +01005562{
Radek Krejci1c54f462020-05-12 17:25:34 +02005563 LY_ERR ret = LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01005564 struct lysc_node *node;
5565 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005566 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005567 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005568 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005569
Radek Krejci327de162019-06-14 12:52:07 +02005570 if (node_p->nodetype != LYS_USES) {
5571 lysc_update_path(ctx, parent, node_p->name);
5572 } else {
5573 lysc_update_path(ctx, NULL, "{uses}");
5574 lysc_update_path(ctx, NULL, node_p->name);
5575 }
5576
Radek Krejci19a96102018-11-15 13:38:09 +01005577 switch (node_p->nodetype) {
5578 case LYS_CONTAINER:
5579 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5580 node_compile_spec = lys_compile_node_container;
5581 break;
5582 case LYS_LEAF:
5583 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5584 node_compile_spec = lys_compile_node_leaf;
5585 break;
5586 case LYS_LIST:
5587 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005588 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005589 break;
5590 case LYS_LEAFLIST:
5591 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005592 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005593 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005594 case LYS_CHOICE:
5595 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005596 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005597 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005598 case LYS_ANYXML:
5599 case LYS_ANYDATA:
5600 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005601 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005602 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005603 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005604 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5605 lysc_update_path(ctx, NULL, NULL);
5606 lysc_update_path(ctx, NULL, NULL);
5607 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005608 default:
5609 LOGINT(ctx->ctx);
5610 return LY_EINT;
5611 }
5612 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5613 node->nodetype = node_p->nodetype;
5614 node->module = ctx->mod;
5615 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005616 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005617
5618 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005619 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005620 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005621 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005622 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5623 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005624 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005625 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005626 node->flags |= LYS_CONFIG_R;
5627 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005628 /* config not explicitely set, inherit it from parent */
5629 if (parent) {
5630 node->flags |= parent->flags & LYS_CONFIG_MASK;
5631 } else {
5632 /* default is config true */
5633 node->flags |= LYS_CONFIG_W;
5634 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005635 } else {
5636 /* config set explicitely */
5637 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005638 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005639 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5640 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5641 "Configuration node cannot be child of any state data node.");
Radek Krejci1c54f462020-05-12 17:25:34 +02005642 ret = LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005643 goto error;
5644 }
Radek Krejci19a96102018-11-15 13:38:09 +01005645
Radek Krejcia6d57732018-11-29 13:40:37 +01005646 /* *list ordering */
5647 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5648 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005649 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005650 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5651 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005652 node->flags &= ~LYS_ORDBY_MASK;
5653 node->flags |= LYS_ORDBY_SYSTEM;
5654 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5655 /* default ordering is system */
5656 node->flags |= LYS_ORDBY_SYSTEM;
5657 }
5658 }
5659
Radek Krejci19a96102018-11-15 13:38:09 +01005660 /* status - it is not inherited by specification, but it does not make sense to have
5661 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005662 if (!parent || parent->nodetype != LYS_CHOICE) {
5663 /* in case of choice/case's children, postpone the check to the moment we know if
5664 * 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 +02005665 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 +01005666 }
5667
Radek Krejciec4da802019-05-02 13:02:41 +02005668 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005669 node->sp = node_p;
5670 }
5671 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005672 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5673 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005674 if (node_p->when) {
5675 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejci1c54f462020-05-12 17:25:34 +02005676 LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01005677
5678 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5679 /* do not check "when" semantics in a grouping */
5680 ly_set_add(&ctx->unres, node, 0);
5681 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005682 }
Radek Krejciec4da802019-05-02 13:02:41 +02005683 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005684
5685 /* nodetype-specific part */
Radek Krejci1c54f462020-05-12 17:25:34 +02005686 LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005687
Radek Krejci0935f412019-08-20 16:15:18 +02005688 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5689
Radek Krejcife909632019-02-12 15:34:42 +01005690 /* inherit LYS_MAND_TRUE in parent containers */
5691 if (node->flags & LYS_MAND_TRUE) {
5692 lys_compile_mandatory_parents(parent, 1);
5693 }
5694
Radek Krejci327de162019-06-14 12:52:07 +02005695 lysc_update_path(ctx, NULL, NULL);
5696
Radek Krejci19a96102018-11-15 13:38:09 +01005697 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005698 if (parent) {
5699 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005700 if (node_p->parent->nodetype == LYS_CASE) {
5701 lysc_update_path(ctx, parent, node_p->parent->name);
5702 } else {
5703 lysc_update_path(ctx, parent, node->name);
5704 }
Radek Krejciec4da802019-05-02 13:02:41 +02005705 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005706 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005707 if (uses_status) {
5708
5709 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005710 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005711 * it directly gets the same status flags as the choice;
5712 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci1c54f462020-05-12 17:25:34 +02005713 LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005714 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005715 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005716 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005717 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005718 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005719 }
Radek Krejciec4da802019-05-02 13:02:41 +02005720 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 +02005721
5722 if (parent->nodetype == LYS_CHOICE) {
5723 lysc_update_path(ctx, NULL, NULL);
5724 }
Radek Krejci19a96102018-11-15 13:38:09 +01005725 } else {
5726 /* top-level element */
5727 if (!ctx->mod->compiled->data) {
5728 ctx->mod->compiled->data = node;
5729 } else {
5730 /* insert at the end of the module's top-level nodes list */
5731 ctx->mod->compiled->data->prev->next = node;
5732 node->prev = ctx->mod->compiled->data->prev;
5733 ctx->mod->compiled->data->prev = node;
5734 }
Radek Krejci327de162019-06-14 12:52:07 +02005735 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005736 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5737 ctx->mod->compiled->notifs, node->name, node)) {
5738 return LY_EVALID;
5739 }
Radek Krejci19a96102018-11-15 13:38:09 +01005740 }
Radek Krejci327de162019-06-14 12:52:07 +02005741 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005742
5743 return LY_SUCCESS;
5744
5745error:
5746 lysc_node_free(ctx->ctx, node);
5747 return ret;
5748}
5749
Radek Krejciccd20f12019-02-15 14:12:27 +01005750static void
5751lysc_disconnect(struct lysc_node *node)
5752{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005753 struct lysc_node *parent, *child, *prev = NULL, *next;
5754 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005755 int remove_cs = 0;
5756
5757 parent = node->parent;
5758
5759 /* parent's first child */
5760 if (!parent) {
5761 return;
5762 }
5763 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005764 cs = (struct lysc_node_case*)node;
5765 } else if (parent->nodetype == LYS_CASE) {
5766 /* disconnecting some node in a case */
5767 cs = (struct lysc_node_case*)parent;
5768 parent = cs->parent;
5769 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5770 if (child == node) {
5771 if (cs->child == child) {
5772 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5773 /* case with a single child -> remove also the case */
5774 child->parent = NULL;
5775 remove_cs = 1;
5776 } else {
5777 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005778 }
5779 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005780 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005781 }
5782 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005783 if (!remove_cs) {
5784 cs = NULL;
5785 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005786 } else if (lysc_node_children(parent, node->flags) == node) {
5787 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005788 }
5789
5790 if (cs) {
5791 if (remove_cs) {
5792 /* cs has only one child which is being also removed */
5793 lysc_disconnect((struct lysc_node*)cs);
5794 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5795 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005796 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5797 /* default case removed */
5798 ((struct lysc_node_choice*)parent)->dflt = NULL;
5799 }
5800 if (((struct lysc_node_choice*)parent)->cases == cs) {
5801 /* first case removed */
5802 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5803 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005804 if (cs->child) {
5805 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5806 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5807 prev = cs->child->prev;
5808 } /* else all the children are under a single case */
5809 LY_LIST_FOR_SAFE(cs->child, next, child) {
5810 if (child->parent != (struct lysc_node*)cs) {
5811 break;
5812 }
5813 lysc_node_free(node->module->ctx, child);
5814 }
5815 if (prev) {
5816 if (prev->next) {
5817 prev->next = child;
5818 }
5819 if (child) {
5820 child->prev = prev;
5821 } else {
5822 /* link from the first child under the cases */
5823 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5824 }
5825 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005826 }
5827 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005828 }
5829
5830 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005831 if (node->prev->next) {
5832 node->prev->next = node->next;
5833 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005834 if (node->next) {
5835 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005836 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005837 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005838 if (child) {
5839 child->prev = node->prev;
5840 }
5841 } else if (((struct lysc_node_choice*)parent)->cases) {
5842 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005843 }
5844}
5845
5846LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005847lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005848{
Radek Krejcia1911222019-07-22 17:24:50 +02005849 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005850 struct ly_set devs_p = {0};
5851 struct ly_set targets = {0};
5852 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005853 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005854 struct lysc_action *rpcs;
5855 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005856 struct lysp_deviation *dev;
5857 struct lysp_deviate *d, **dp_new;
5858 struct lysp_deviate_add *d_add;
5859 struct lysp_deviate_del *d_del;
5860 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005861 LY_ARRAY_SIZE_TYPE u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005862 struct lysc_deviation {
5863 const char *nodeid;
5864 struct lysc_node *target; /* target node of the deviation */
5865 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005866 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005867 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5868 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005869 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005870 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005871 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005872 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005873 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005874 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005875
5876 /* get all deviations from the module and all its submodules ... */
5877 LY_ARRAY_FOR(mod_p->deviations, u) {
5878 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5879 }
5880 LY_ARRAY_FOR(mod_p->includes, v) {
5881 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5882 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5883 }
5884 }
5885 if (!devs_p.count) {
5886 /* nothing to do */
5887 return LY_SUCCESS;
5888 }
5889
Radek Krejci327de162019-06-14 12:52:07 +02005890 lysc_update_path(ctx, NULL, "{deviation}");
5891
Radek Krejciccd20f12019-02-15 14:12:27 +01005892 /* ... and group them by the target node */
5893 devs = calloc(devs_p.count, sizeof *devs);
5894 for (u = 0; u < devs_p.count; ++u) {
5895 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005896 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005897
5898 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005899 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5900 (const struct lysc_node**)&target, &flags), cleanup);
Michal Vasko1bf09392020-03-27 12:38:10 +01005901 if (target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005902 /* move the target pointer to input/output to make them different from the action and
5903 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5904 * back to the RPC/action node due to a better compatibility and decision code in this function.
5905 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5906 if (flags & LYSC_OPT_RPC_INPUT) {
5907 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5908 flags |= LYSC_OPT_INTERNAL;
5909 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5910 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5911 flags |= LYSC_OPT_INTERNAL;
5912 }
5913 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005914 /* insert into the set of targets with duplicity detection */
5915 i = ly_set_add(&targets, target, 0);
5916 if (!devs[i]) {
5917 /* new record */
5918 devs[i] = calloc(1, sizeof **devs);
5919 devs[i]->target = target;
5920 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005921 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005922 }
5923 /* add deviates into the deviation's list of deviates */
5924 for (d = dev->deviates; d; d = d->next) {
5925 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5926 *dp_new = d;
5927 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5928 devs[i]->not_supported = 1;
5929 }
5930 }
Radek Krejci327de162019-06-14 12:52:07 +02005931
5932 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005933 }
5934
5935 /* MACROS for deviates checking */
5936#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5937 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005938 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 +01005939 goto cleanup; \
5940 }
5941
5942#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5943 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci7eb54ba2020-05-18 16:30:04 +02005944 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 +02005945 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005946 goto cleanup; \
5947 }
5948
Radek Krejcia1911222019-07-22 17:24:50 +02005949
Radek Krejciccd20f12019-02-15 14:12:27 +01005950#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005951 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5952 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5953 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5954 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5955 goto cleanup; \
5956 }
5957
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005958#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005959 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005960 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005961 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5962 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005963 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005964 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5965 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005966 goto cleanup; \
5967 }
5968
Radek Krejci551b12c2019-02-19 16:11:21 +01005969#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5970 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5971 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005972 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5973 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005974 goto cleanup; \
5975 }
5976
Radek Krejciccd20f12019-02-15 14:12:27 +01005977#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5978 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005979 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005980 goto cleanup; \
5981 }
5982
Radek Krejci551b12c2019-02-19 16:11:21 +01005983#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5984 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5985 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005986 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005987 goto cleanup; \
5988 }
5989
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005990#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5991 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5992 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5993 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5994 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 +01005995 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005996 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005998 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5999 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01006000 goto cleanup; \
6001 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006002 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
6003 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
6004 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
6005 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
6006 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01006007 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006008 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
6009 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
6010 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01006011 }
6012
6013 /* apply deviations */
6014 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02006015 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
6016 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
6017 struct ly_err_item *err = NULL;
6018
6019 dflt = NULL;
6020 changed_type = 0;
6021
Radek Krejci327de162019-06-14 12:52:07 +02006022 lysc_update_path(ctx, NULL, devs[u]->nodeid);
6023
Radek Krejcif538ce52019-03-05 10:46:14 +01006024 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
6025 /* fix the target pointer in case of RPC's/action's input/output */
6026 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6027 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
6028 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6029 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
6030 }
6031 }
6032
Radek Krejciccd20f12019-02-15 14:12:27 +01006033 /* not-supported */
6034 if (devs[u]->not_supported) {
6035 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006036 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 +01006037 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
6038 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02006039
6040#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
6041 if (devs[u]->target->parent) { \
6042 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
6043 } else { \
6044 ARRAY = devs[u]->target->module->compiled->ARRAY; \
6045 } \
6046 LY_ARRAY_FOR(ARRAY, x) { \
6047 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
6048 } \
6049 if (x < LY_ARRAY_SIZE(ARRAY)) { \
6050 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6051 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6052 LY_ARRAY_DECREMENT(ARRAY); \
6053 }
6054
Michal Vasko1bf09392020-03-27 12:38:10 +01006055 if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejcif538ce52019-03-05 10:46:14 +01006056 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6057 /* remove RPC's/action's input */
6058 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
6059 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02006060 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
6061 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01006062 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6063 /* remove RPC's/action's output */
6064 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
6065 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02006066 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
6067 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01006068 } else {
6069 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02006070 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01006071 }
6072 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02006073 /* remove Notification */
6074 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01006075 } else {
6076 /* remove the target node */
6077 lysc_disconnect(devs[u]->target);
6078 lysc_node_free(ctx->ctx, devs[u]->target);
6079 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006080
Radek Krejci474f9b82019-07-24 11:36:37 +02006081 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6082 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01006083 continue;
6084 }
6085
6086 /* list of deviates (not-supported is not present in the list) */
6087 LY_ARRAY_FOR(devs[u]->deviates, v) {
6088 d = devs[u]->deviates[v];
6089
6090 switch (d->mod) {
6091 case LYS_DEV_ADD:
6092 d_add = (struct lysp_deviate_add*)d;
6093 /* [units-stmt] */
6094 if (d_add->units) {
6095 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
6096 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
6097
6098 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6099 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6100 }
6101
6102 /* *must-stmt */
6103 if (d_add->musts) {
6104 switch (devs[u]->target->nodetype) {
6105 case LYS_CONTAINER:
6106 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006107 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
6108 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006109 break;
6110 case LYS_LEAF:
6111 case LYS_LEAFLIST:
6112 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006113 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
6114 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006115 break;
6116 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006117 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
6118 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006119 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006120 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006121 case LYS_ACTION:
6122 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006123 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
6124 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006125 break;
Michal Vaskocc048b22020-03-27 15:52:38 +01006126 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006127 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
6128 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006129 break;
6130 }
6131 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006132 default:
6133 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006134 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006135 goto cleanup;
6136 }
Michal Vasko5d8756a2019-11-07 15:21:00 +01006137 ly_set_add(&ctx->unres, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01006138 }
6139
6140 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006141 if (d_add->uniques) {
6142 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
6143 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
6144 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006145
6146 /* *default-stmt */
6147 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006148 switch (devs[u]->target->nodetype) {
6149 case LYS_LEAF:
6150 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006151 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 +02006152 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006153 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006154 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006155 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6156 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6157 } else {
6158 /* prepare new default value storage */
6159 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01006160 }
Radek Krejcia1911222019-07-22 17:24:50 +02006161 dflt = d_add->dflts[0];
6162 /* parsing is done at the end after possible replace of the leaf's type */
6163
Radek Krejci551b12c2019-02-19 16:11:21 +01006164 /* mark the new default values as leaf's own */
6165 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006166 break;
6167 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006168 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006169 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02006170 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006171 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006172 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6173 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6174 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006175 }
Radek Krejcia1911222019-07-22 17:24:50 +02006176 LY_ARRAY_FREE(llist->dflts);
6177 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006178 LY_ARRAY_FREE(llist->dflts_mods);
6179 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006180 }
6181 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006182 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006183 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
6184 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01006185 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006186 LY_ARRAY_INCREMENT(llist->dflts_mods);
6187 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006188 LY_ARRAY_INCREMENT(llist->dflts);
6189 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
6190 llist->dflts[x]->realtype = llist->type;
6191 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 +02006192 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006193 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006194 llist->dflts[x]->realtype->refcount++;
6195 if (err) {
6196 ly_err_print(err);
6197 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6198 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
6199 d_add->dflts[x - y], err->msg);
6200 ly_err_free(err);
6201 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006202 if (rc == LY_EINCOMPLETE) {
6203 /* postpone default compilation when the tree is complete */
6204 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6205
6206 /* but in general result is so far ok */
6207 rc = LY_SUCCESS;
6208 }
Radek Krejcia1911222019-07-22 17:24:50 +02006209 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006210 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006211 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01006212 devs[u]->target->flags |= LYS_SET_DFLT;
6213 break;
6214 case LYS_CHOICE:
6215 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
6216 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
6217 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
6218 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02006219 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 +01006220 goto cleanup;
6221 }
6222 break;
6223 default:
6224 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006225 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006226 goto cleanup;
6227 }
6228 }
6229
6230 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006231 if (d_add->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01006232 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006233 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006234 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
6235 goto cleanup;
6236 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006237 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02006238 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
Michal Vaskoa3881362020-01-21 15:57:35 +01006239 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006240 }
Radek Krejci93dcc392019-02-19 10:43:38 +01006241 if (devs[u]->target->flags & LYS_SET_CONFIG) {
6242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006243 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
6244 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01006245 goto cleanup;
6246 }
Radek Krejci327de162019-06-14 12:52:07 +02006247 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006248 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006249
6250 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006251 if (d_add->flags & LYS_MAND_MASK) {
6252 if (devs[u]->target->flags & LYS_MAND_MASK) {
6253 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006254 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
6255 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01006256 goto cleanup;
6257 }
Radek Krejci327de162019-06-14 12:52:07 +02006258 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006259 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006260
6261 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006262 if (d_add->flags & LYS_SET_MIN) {
6263 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6264 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6265 /* change value */
6266 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
6267 } else if (devs[u]->target->nodetype == LYS_LIST) {
6268 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6269 /* change value */
6270 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
6271 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006272 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006273 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
6274 goto cleanup;
6275 }
6276 if (d_add->min) {
6277 devs[u]->target->flags |= LYS_MAND_TRUE;
6278 }
6279 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006280
6281 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006282 if (d_add->flags & LYS_SET_MAX) {
6283 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6284 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6285 /* change value */
6286 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6287 } else if (devs[u]->target->nodetype == LYS_LIST) {
6288 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6289 /* change value */
6290 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6291 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006292 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006293 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6294 goto cleanup;
6295 }
6296 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006297
6298 break;
6299 case LYS_DEV_DELETE:
6300 d_del = (struct lysp_deviate_del*)d;
6301
6302 /* [units-stmt] */
6303 if (d_del->units) {
6304 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006305 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6306 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6307 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6308 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6309 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6310 goto cleanup;
6311 }
6312 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6313 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006314 }
6315
6316 /* *must-stmt */
6317 if (d_del->musts) {
6318 switch (devs[u]->target->nodetype) {
6319 case LYS_CONTAINER:
6320 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006321 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006322 break;
6323 case LYS_LEAF:
6324 case LYS_LEAFLIST:
6325 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006326 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006327 break;
6328 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006329 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006330 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01006331 case LYS_RPC:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006332 case LYS_ACTION:
6333 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6334 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6335 break;
6336 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6337 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6338 break;
6339 }
6340 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006341 default:
6342 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006343 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006344 goto cleanup;
6345 }
6346 }
6347
6348 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006349 if (d_del->uniques) {
6350 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6351 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6352 LY_ARRAY_FOR(d_del->uniques, x) {
6353 LY_ARRAY_FOR(list->uniques, z) {
6354 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6355 nodeid = strpbrk(name, " \t\n");
6356 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02006357 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006358 break;
6359 }
6360 while (isspace(*nodeid)) {
6361 ++nodeid;
6362 }
6363 } else {
6364 if (strcmp(name, list->uniques[z][y]->name)) {
6365 break;
6366 }
6367 }
6368 }
6369 if (!name) {
6370 /* complete match - remove the unique */
6371 LY_ARRAY_DECREMENT(list->uniques);
6372 LY_ARRAY_FREE(list->uniques[z]);
6373 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6374 --z;
6375 break;
6376 }
6377 }
6378 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6379 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006380 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6381 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006382 goto cleanup;
6383 }
6384 }
6385 if (!LY_ARRAY_SIZE(list->uniques)) {
6386 LY_ARRAY_FREE(list->uniques);
6387 list->uniques = NULL;
6388 }
6389 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006390
6391 /* *default-stmt */
6392 if (d_del->dflts) {
6393 switch (devs[u]->target->nodetype) {
6394 case LYS_LEAF:
6395 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6396 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6397 dflt, "deleting", "default", d_del->dflts[0]);
6398
Radek Krejcia1911222019-07-22 17:24:50 +02006399 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006400 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006401 if (strcmp(dflt, d_del->dflts[0])) {
6402 if (i) {
6403 free((char*)dflt);
6404 }
6405 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6406 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6407 d_del->dflts[0], dflt);
6408 goto cleanup;
6409 }
6410 if (i) {
6411 free((char*)dflt);
6412 }
6413 dflt = NULL;
6414
Radek Krejci474f9b82019-07-24 11:36:37 +02006415 /* update the list of incomplete default values if needed */
6416 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6417
Radek Krejcia1911222019-07-22 17:24:50 +02006418 /* remove the default specification */
6419 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6420 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6421 free(leaf->dflt);
6422 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006423 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006424 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006425 break;
6426 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006427 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6428 LY_ARRAY_FOR(d_del->dflts, x) {
6429 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006430 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 +02006431 if (!strcmp(dflt, d_del->dflts[x])) {
6432 if (i) {
6433 free((char*)dflt);
6434 }
6435 dflt = NULL;
6436 break;
6437 }
6438 if (i) {
6439 free((char*)dflt);
6440 }
6441 dflt = NULL;
6442 }
6443 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6444 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6445 "which does not match any of the target's property values.", d_del->dflts[x]);
6446 goto cleanup;
6447 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006448
6449 /* update the list of incomplete default values if needed */
6450 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6451
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006452 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006453 LY_ARRAY_DECREMENT(llist->dflts);
6454 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6455 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6456 free(llist->dflts[y]);
6457 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006458 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 +02006459 }
6460 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006461 LY_ARRAY_FREE(llist->dflts_mods);
6462 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006463 LY_ARRAY_FREE(llist->dflts);
6464 llist->dflts = NULL;
6465 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006466 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006467 break;
6468 case LYS_CHOICE:
6469 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6470 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6471 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006472 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006473 if (prefix) {
6474 /* use module prefixes from the deviation module to match the module of the default case */
6475 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6476 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006477 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6478 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006479 goto cleanup;
6480 }
6481 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6482 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006483 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6484 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006485 goto cleanup;
6486 }
6487 }
6488 /* else {
6489 * strictly, the default prefix would point to the deviation module, but the value should actually
6490 * match the default string in the original module (usually unprefixed), so in this case we do not check
6491 * the module of the default case, just matching its name */
6492 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6493 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006494 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6495 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006496 goto cleanup;
6497 }
6498 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6499 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6500 break;
6501 default:
6502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006503 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006504 goto cleanup;
6505 }
6506 }
6507
6508 break;
6509 case LYS_DEV_REPLACE:
6510 d_rpl = (struct lysp_deviate_rpl*)d;
6511
6512 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006513 if (d_rpl->type) {
6514 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6515 /* type is mandatory, so checking for its presence is not necessary */
6516 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006517
6518 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6519 /* the target has default from the previous type - remove it */
6520 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006521 /* update the list of incomplete default values if needed */
6522 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6523
Radek Krejcia1911222019-07-22 17:24:50 +02006524 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6525 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6526 free(leaf->dflt);
6527 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006528 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006529 } else { /* LYS_LEAFLIST */
6530 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006531 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006532 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6533 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6534 free(llist->dflts[x]);
6535 }
6536 LY_ARRAY_FREE(llist->dflts);
6537 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006538 LY_ARRAY_FREE(llist->dflts_mods);
6539 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006540 }
6541 }
6542 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006543 /* there is no default value, do not set changed_type after type compilation
6544 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006545 changed_type = -1;
6546 }
Radek Krejciec4da802019-05-02 13:02:41 +02006547 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 +02006548 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006549 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006550
6551 /* [units-stmt] */
6552 if (d_rpl->units) {
6553 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6554 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6555 units, "replacing", "units", d_rpl->units);
6556
6557 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6558 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6559 }
6560
6561 /* [default-stmt] */
6562 if (d_rpl->dflt) {
6563 switch (devs[u]->target->nodetype) {
6564 case LYS_LEAF:
6565 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6566 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006567 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006568 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006569 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6570 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6571 dflt = d_rpl->dflt;
6572 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006573 break;
6574 case LYS_CHOICE:
6575 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006576 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 +01006577 goto cleanup;
6578 }
6579 break;
6580 default:
6581 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006582 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006583 goto cleanup;
6584 }
6585 }
6586
6587 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006588 if (d_rpl->flags & LYS_CONFIG_MASK) {
Michal Vasko1bf09392020-03-27 12:38:10 +01006589 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006590 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006591 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6592 goto cleanup;
6593 }
6594 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006595 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006596 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6597 goto cleanup;
6598 }
Radek Krejci327de162019-06-14 12:52:07 +02006599 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006600 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006601
6602 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006603 if (d_rpl->flags & LYS_MAND_MASK) {
6604 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006606 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6607 goto cleanup;
6608 }
Radek Krejci327de162019-06-14 12:52:07 +02006609 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006610 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006611
6612 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006613 if (d_rpl->flags & LYS_SET_MIN) {
6614 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6615 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6616 /* change value */
6617 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6618 } else if (devs[u]->target->nodetype == LYS_LIST) {
6619 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6620 /* change value */
6621 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6622 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006623 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006624 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6625 goto cleanup;
6626 }
6627 if (d_rpl->min) {
6628 devs[u]->target->flags |= LYS_MAND_TRUE;
6629 }
6630 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006631
6632 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006633 if (d_rpl->flags & LYS_SET_MAX) {
6634 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6635 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6636 /* change value */
6637 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6638 } else if (devs[u]->target->nodetype == LYS_LIST) {
6639 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6640 /* change value */
6641 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6642 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006644 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6645 goto cleanup;
6646 }
6647 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006648
6649 break;
6650 default:
6651 LOGINT(ctx->ctx);
6652 goto cleanup;
6653 }
6654 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006655
Radek Krejci33f72892019-02-21 10:36:58 +01006656 /* final check when all deviations of a single target node are applied */
6657
Radek Krejci551b12c2019-02-19 16:11:21 +01006658 /* check min-max compatibility */
6659 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6660 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6661 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6662 } else if (devs[u]->target->nodetype == LYS_LIST) {
6663 min = ((struct lysc_node_list*)devs[u]->target)->min;
6664 max = ((struct lysc_node_list*)devs[u]->target)->max;
6665 } else {
6666 min = max = 0;
6667 }
6668 if (min > max) {
6669 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 +02006670 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006671 goto cleanup;
6672 }
6673
Radek Krejcia1911222019-07-22 17:24:50 +02006674 if (dflt) {
6675 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006676 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006677 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006678 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6679 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006680 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006681 leaf->dflt->realtype->refcount++;
6682 if (err) {
6683 ly_err_print(err);
6684 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6685 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6686 ly_err_free(err);
6687 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006688 if (rc == LY_EINCOMPLETE) {
6689 /* postpone default compilation when the tree is complete */
6690 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6691
6692 /* but in general result is so far ok */
6693 rc = LY_SUCCESS;
6694 }
Radek Krejcia1911222019-07-22 17:24:50 +02006695 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006696 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006697 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6698 int dynamic;
6699 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006700 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006701
6702 /* update the list of incomplete default values if needed */
6703 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6704
6705 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006706 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6707 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6708 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006709 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6710 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006711 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006712 leaf->dflt->realtype->refcount++;
6713 if (err) {
6714 ly_err_print(err);
6715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6716 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6717 ly_err_free(err);
6718 }
6719 if (dynamic) {
6720 free((void*)dflt);
6721 }
Radek Krejcia1911222019-07-22 17:24:50 +02006722 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006723 if (rc == LY_EINCOMPLETE) {
6724 /* postpone default compilation when the tree is complete */
6725 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6726
6727 /* but in general result is so far ok */
6728 rc = LY_SUCCESS;
6729 }
6730 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006731 } else { /* LYS_LEAFLIST */
6732 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006733 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 +02006734 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6735 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6736 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006737 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6738 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006739 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6740 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006741 llist->dflts[x]->realtype->refcount++;
6742 if (err) {
6743 ly_err_print(err);
6744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6745 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6746 dflt, err->msg);
6747 ly_err_free(err);
6748 }
6749 if (dynamic) {
6750 free((void*)dflt);
6751 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006752 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006753 if (rc == LY_EINCOMPLETE) {
6754 /* postpone default compilation when the tree is complete */
6755 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6756
6757 /* but in general result is so far ok */
6758 rc = LY_SUCCESS;
6759 }
Radek Krejcia1911222019-07-22 17:24:50 +02006760 LY_CHECK_GOTO(rc, cleanup);
6761 }
6762 }
6763 }
6764
Radek Krejci551b12c2019-02-19 16:11:21 +01006765 /* check mandatory - default compatibility */
6766 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6767 && (devs[u]->target->flags & LYS_SET_DFLT)
6768 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6769 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006770 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006771 goto cleanup;
6772 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6773 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6774 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006775 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 +01006776 goto cleanup;
6777 }
6778 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6779 /* mandatory node under a default case */
6780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006781 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6782 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006783 goto cleanup;
6784 }
Radek Krejci33f72892019-02-21 10:36:58 +01006785
Radek Krejci327de162019-06-14 12:52:07 +02006786 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006787 }
6788
Radek Krejci327de162019-06-14 12:52:07 +02006789 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006790 ret = LY_SUCCESS;
6791
6792cleanup:
6793 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6794 LY_ARRAY_FREE(devs[u]->deviates);
6795 free(devs[u]);
6796 }
6797 free(devs);
6798 ly_set_erase(&targets, NULL);
6799 ly_set_erase(&devs_p, NULL);
6800
6801 return ret;
6802}
6803
Radek Krejcib56c7502019-02-13 14:19:54 +01006804/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006805 * @brief Compile the given YANG submodule into the main module.
6806 * @param[in] ctx Compile context
6807 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006808 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6809 */
6810LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006811lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006812{
6813 unsigned int u;
6814 LY_ERR ret = LY_SUCCESS;
6815 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006816 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006817 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006818 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006819
Radek Krejci0af46292019-01-11 16:02:31 +01006820 if (!mainmod->mod->off_features) {
6821 /* features are compiled directly into the compiled module structure,
6822 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6823 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006824 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6825 LY_CHECK_GOTO(ret, error);
6826 }
Radek Krejci0af46292019-01-11 16:02:31 +01006827
Radek Krejci327de162019-06-14 12:52:07 +02006828 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006829 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006830 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006831
Radek Krejci474f9b82019-07-24 11:36:37 +02006832 /* data nodes */
6833 LY_LIST_FOR(submod->data, node_p) {
6834 ret = lys_compile_node(ctx, node_p, NULL, 0);
6835 LY_CHECK_GOTO(ret, error);
6836 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006837
Radek Krejciec4da802019-05-02 13:02:41 +02006838 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6839 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006840
Radek Krejcid05cbd92018-12-05 14:26:40 +01006841error:
6842 return ret;
6843}
6844
Radek Krejci335332a2019-09-05 13:03:35 +02006845static void *
6846lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6847{
6848 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6849 if (substmts[u].stmt == stmt) {
6850 return substmts[u].storage;
6851 }
6852 }
6853 return NULL;
6854}
6855
6856LY_ERR
6857lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6858{
6859 LY_ERR ret = LY_EVALID, r;
6860 unsigned int u;
6861 struct lysp_stmt *stmt;
6862 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006863
6864 /* check for invalid substatements */
6865 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006866 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6867 continue;
6868 }
Radek Krejci335332a2019-09-05 13:03:35 +02006869 for (u = 0; substmts[u].stmt; ++u) {
6870 if (substmts[u].stmt == stmt->kw) {
6871 break;
6872 }
6873 }
6874 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006875 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6876 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006877 goto cleanup;
6878 }
Radek Krejci335332a2019-09-05 13:03:35 +02006879 }
6880
Radek Krejciad5963b2019-09-06 16:03:05 +02006881 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6882
Radek Krejci335332a2019-09-05 13:03:35 +02006883 /* keep order of the processing the same as the order in the defined substmts,
6884 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6885 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006886 int stmt_present = 0;
6887
Radek Krejci335332a2019-09-05 13:03:35 +02006888 for (stmt = ext->child; stmt; stmt = stmt->next) {
6889 if (substmts[u].stmt != stmt->kw) {
6890 continue;
6891 }
6892
Radek Krejciad5963b2019-09-06 16:03:05 +02006893 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006894 if (substmts[u].storage) {
6895 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006896 case LY_STMT_STATUS:
6897 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6898 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6899 break;
6900 case LY_STMT_UNITS: {
6901 const char **units;
6902
6903 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6904 /* single item */
6905 if (*((const char **)substmts[u].storage)) {
6906 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6907 goto cleanup;
6908 }
6909 units = (const char **)substmts[u].storage;
6910 } else {
6911 /* sized array */
6912 const char ***units_array = (const char ***)substmts[u].storage;
6913 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6914 }
6915 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6916 break;
6917 }
Radek Krejci335332a2019-09-05 13:03:35 +02006918 case LY_STMT_TYPE: {
6919 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6920 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6921
6922 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6923 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006924 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006925 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6926 goto cleanup;
6927 }
6928 compiled = substmts[u].storage;
6929 } else {
6930 /* sized array */
6931 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6932 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6933 compiled = (void*)type;
6934 }
6935
Radek Krejciad5963b2019-09-06 16:03:05 +02006936 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006937 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6938 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006939 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6940 lysp_type_free(ctx->ctx, parsed);
6941 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006942 break;
6943 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006944 case LY_STMT_IF_FEATURE: {
6945 struct lysc_iffeature *iff = NULL;
6946
6947 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6948 /* single item */
6949 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6950 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6951 goto cleanup;
6952 }
6953 iff = (struct lysc_iffeature*)substmts[u].storage;
6954 } else {
6955 /* sized array */
6956 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6957 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6958 }
6959 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6960 break;
6961 }
6962 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6963 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006964 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006965 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.",
6966 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006967 goto cleanup;
6968 }
6969 }
Radek Krejci335332a2019-09-05 13:03:35 +02006970 }
Radek Krejci335332a2019-09-05 13:03:35 +02006971
Radek Krejciad5963b2019-09-06 16:03:05 +02006972 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6973 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6974 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6975 goto cleanup;
6976 }
Radek Krejci335332a2019-09-05 13:03:35 +02006977 }
6978
6979 ret = LY_SUCCESS;
6980
6981cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006982 return ret;
6983}
6984
Michal Vasko175012e2019-11-06 15:49:14 +01006985/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006986 * @brief Check when for cyclic dependencies.
6987 * @param[in] set Set with all the referenced nodes.
6988 * @param[in] node Node whose "when" referenced nodes are in @p set.
6989 * @return LY_ERR value
6990 */
6991static LY_ERR
Michal Vasko5c4e5892019-11-14 12:31:38 +01006992lys_compile_check_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006993{
6994 struct lyxp_set tmp_set;
6995 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02006996 uint32_t i, j;
6997 LY_ARRAY_SIZE_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006998 int idx;
6999 struct lysc_when *when;
7000 LY_ERR ret = LY_SUCCESS;
7001
7002 memset(&tmp_set, 0, sizeof tmp_set);
7003
7004 /* prepare in_ctx of the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007005 for ( i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007006 xp_scnode = &set->val.scnodes[i];
7007
Michal Vasko5c4e5892019-11-14 12:31:38 +01007008 if (xp_scnode->in_ctx != -1) {
7009 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01007010 xp_scnode->in_ctx = 1;
7011 }
7012 }
7013
7014 for (i = 0; i < set->used; ++i) {
7015 xp_scnode = &set->val.scnodes[i];
7016 if (xp_scnode->in_ctx != 1) {
7017 /* already checked */
7018 continue;
7019 }
7020
Michal Vasko1bf09392020-03-27 12:38:10 +01007021 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01007022 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007023 /* no when to check */
7024 xp_scnode->in_ctx = 0;
7025 continue;
7026 }
7027
7028 node = xp_scnode->scnode;
7029 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007030 LY_ARRAY_FOR(node->when, u) {
7031 when = node->when[u];
Michal Vasko52927e22020-03-16 17:26:14 +01007032 ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01007033 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
7034 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007035 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007036 goto cleanup;
7037 }
7038
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007039 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007040 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007041 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007042 /* try to find this node in our set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007043 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 +01007044 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007045 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 +01007046 ret = LY_EVALID;
7047 goto cleanup;
7048 }
7049
7050 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007051 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007052 } else {
7053 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007054 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007055 }
7056 }
7057
7058 /* merge this set into the global when set */
7059 lyxp_set_scnode_merge(set, &tmp_set);
7060 }
7061
7062 /* check when of non-data parents as well */
7063 node = node->parent;
7064 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
7065
Michal Vasko251f56e2019-11-14 16:06:47 +01007066 /* this node when was checked (xp_scnode could have been reallocd) */
7067 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007068 }
7069
7070cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007071 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007072 return ret;
7073}
7074
7075/**
Michal Vasko175012e2019-11-06 15:49:14 +01007076 * @brief Check when/must expressions of a node on a compiled schema tree.
7077 * @param[in] ctx Compile context.
7078 * @param[in] node Node to check.
7079 * @return LY_ERR value
7080 */
7081static LY_ERR
7082lys_compile_check_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
7083{
Michal Vasko175012e2019-11-06 15:49:14 +01007084 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007085 uint32_t i;
7086 LY_ARRAY_SIZE_TYPE u;
Michal Vasko5d8756a2019-11-07 15:21:00 +01007087 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01007088 struct lysc_when **when = NULL;
7089 struct lysc_must *musts = NULL;
7090 LY_ERR ret = LY_SUCCESS;
7091
7092 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01007093 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko175012e2019-11-06 15:49:14 +01007094
7095 switch (node->nodetype) {
7096 case LYS_CONTAINER:
7097 when = ((struct lysc_node_container *)node)->when;
7098 musts = ((struct lysc_node_container *)node)->musts;
7099 break;
7100 case LYS_CHOICE:
7101 when = ((struct lysc_node_choice *)node)->when;
7102 break;
7103 case LYS_LEAF:
7104 when = ((struct lysc_node_leaf *)node)->when;
7105 musts = ((struct lysc_node_leaf *)node)->musts;
7106 break;
7107 case LYS_LEAFLIST:
7108 when = ((struct lysc_node_leaflist *)node)->when;
7109 musts = ((struct lysc_node_leaflist *)node)->musts;
7110 break;
7111 case LYS_LIST:
7112 when = ((struct lysc_node_list *)node)->when;
7113 musts = ((struct lysc_node_list *)node)->musts;
7114 break;
7115 case LYS_ANYXML:
7116 case LYS_ANYDATA:
7117 when = ((struct lysc_node_anydata *)node)->when;
7118 musts = ((struct lysc_node_anydata *)node)->musts;
7119 break;
7120 case LYS_CASE:
7121 when = ((struct lysc_node_case *)node)->when;
7122 break;
7123 case LYS_NOTIF:
7124 musts = ((struct lysc_notif *)node)->musts;
7125 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01007126 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01007127 case LYS_ACTION:
7128 /* first process input musts */
7129 musts = ((struct lysc_action *)node)->input.musts;
7130 break;
Michal Vasko175012e2019-11-06 15:49:14 +01007131 default:
7132 /* nothing to check */
7133 break;
7134 }
7135
Michal Vasko175012e2019-11-06 15:49:14 +01007136 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007137 LY_ARRAY_FOR(when, u) {
7138 ret = lyxp_atomize(when[u]->cond, LYD_SCHEMA, when[u]->module, when[u]->context,
7139 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01007140 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007141 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01007142 goto cleanup;
7143 }
7144
Michal Vaskodc052f32019-11-07 11:11:38 +01007145 ctx->path[0] = '\0';
7146 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007147 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01007148 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007149 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
7150 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01007151
Michal Vaskoecd62de2019-11-13 12:35:11 +01007152 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007153 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01007154 schema->name);
7155 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01007156
7157 /* check dummy node accessing */
7158 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007159 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01007160 ret = LY_EVALID;
7161 goto cleanup;
7162 }
Michal Vasko175012e2019-11-06 15:49:14 +01007163 }
7164 }
7165
Michal Vaskoecd62de2019-11-13 12:35:11 +01007166 /* check cyclic dependencies */
Michal Vasko5c4e5892019-11-14 12:31:38 +01007167 ret = lys_compile_check_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007168 LY_CHECK_GOTO(ret, cleanup);
7169
Michal Vaskod3678892020-05-21 10:06:58 +02007170 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007171 }
7172
Michal Vasko5d8756a2019-11-07 15:21:00 +01007173check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01007174 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007175 LY_ARRAY_FOR(musts, u) {
7176 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 +01007177 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007178 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01007179 goto cleanup;
7180 }
7181
Michal Vaskodc052f32019-11-07 11:11:38 +01007182 ctx->path[0] = '\0';
7183 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007184 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01007185 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007186 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01007187 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007188 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
7189 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01007190 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01007191 }
7192 }
7193
Michal Vaskod3678892020-05-21 10:06:58 +02007194 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007195 }
7196
Michal Vasko1bf09392020-03-27 12:38:10 +01007197 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01007198 /* now check output musts */
7199 input_done = 1;
7200 musts = ((struct lysc_action *)node)->output.musts;
7201 opts = LYXP_SCNODE_OUTPUT;
7202 goto check_musts;
7203 }
7204
Michal Vasko175012e2019-11-06 15:49:14 +01007205cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007206 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007207 return ret;
7208}
7209
Michal Vasko8d544252020-03-02 10:19:52 +01007210static LY_ERR
7211lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7212{
7213 struct lysc_ext_instance *ext;
7214 struct lysp_ext_instance *ext_p = NULL;
7215 struct lysp_stmt *stmt;
7216 const struct lys_module *ext_mod;
7217 LY_ERR ret = LY_SUCCESS;
7218
7219 /* create the parsed extension instance manually */
7220 ext_p = calloc(1, sizeof *ext_p);
7221 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
7222 ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0);
7223 ext_p->argument = lydict_insert(ctx->ctx, "default", 0);
7224 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7225 ext_p->insubstmt_index = 0;
7226
7227 stmt = calloc(1, sizeof *ext_p->child);
7228 stmt->stmt = lydict_insert(ctx->ctx, "type", 0);
7229 stmt->arg = lydict_insert(ctx->ctx, "boolean", 0);
7230 stmt->kw = LY_STMT_TYPE;
7231 ext_p->child = stmt;
7232
7233 /* allocate new extension instance */
7234 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7235
7236 /* manually get extension definition module */
7237 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7238
7239 /* compile the extension instance */
7240 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7241
7242cleanup:
7243 lysp_ext_instance_free(ctx->ctx, ext_p);
7244 free(ext_p);
7245 return ret;
7246}
7247
Radek Krejci19a96102018-11-15 13:38:09 +01007248LY_ERR
7249lys_compile(struct lys_module *mod, int options)
7250{
7251 struct lysc_ctx ctx = {0};
7252 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01007253 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01007254 struct lysp_module *sp;
7255 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007256 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007257 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007258 struct lys_module *m;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007259 LY_ARRAY_SIZE_TYPE u, v;
7260 uint32_t i;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007261 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007262
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007263 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007264
7265 if (!mod->implemented) {
7266 /* just imported modules are not compiled */
7267 return LY_SUCCESS;
7268 }
7269
Radek Krejci19a96102018-11-15 13:38:09 +01007270 sp = mod->parsed;
7271
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007272 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01007273 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01007274 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007275 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007276 ctx.path_len = 1;
7277 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007278
7279 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007280 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
7281 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007282
Radek Krejciec4da802019-05-02 13:02:41 +02007283 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007284 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007285 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007286 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7287 }
Radek Krejci0935f412019-08-20 16:15:18 +02007288
7289 /* features */
Radek Krejci0af46292019-01-11 16:02:31 +01007290 if (mod->off_features) {
7291 /* there is already precompiled array of features */
7292 mod_c->features = mod->off_features;
7293 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007294 } else {
7295 /* features are compiled directly into the compiled module structure,
7296 * 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 +02007297 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007298 LY_CHECK_GOTO(ret, error);
7299 }
7300 /* finish feature compilation, not only for the main module, but also for the submodules.
7301 * Due to possible forward references, it must be done when all the features (including submodules)
7302 * are present. */
7303 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007304 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007305 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7306 }
Radek Krejci327de162019-06-14 12:52:07 +02007307 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007308 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007309 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007310 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007311 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007312 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7313 }
Radek Krejci327de162019-06-14 12:52:07 +02007314 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007315 }
Radek Krejci327de162019-06-14 12:52:07 +02007316 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007317
Radek Krejci0935f412019-08-20 16:15:18 +02007318 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02007319 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02007320 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007321 if (sp->identities) {
7322 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
7323 }
Radek Krejci327de162019-06-14 12:52:07 +02007324 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007325
Radek Krejci95710c92019-02-11 15:49:55 +01007326 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007327 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007328 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007329 }
Radek Krejci95710c92019-02-11 15:49:55 +01007330
Radek Krejciec4da802019-05-02 13:02:41 +02007331 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7332 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007333
Radek Krejci95710c92019-02-11 15:49:55 +01007334 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007335 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007336 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007337 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007338 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007339
Radek Krejci474f9b82019-07-24 11:36:37 +02007340 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007341 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007342
Radek Krejci0935f412019-08-20 16:15:18 +02007343 /* extension instances TODO cover extension instances from submodules */
7344 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007345
Radek Krejcia3045382018-11-22 14:30:31 +01007346 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01007347 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7348 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7349 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007350 for (i = 0; i < ctx.unres.count; ++i) {
7351 if (((struct lysc_node*)ctx.unres.objs[i])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
7352 type = ((struct lysc_node_leaf*)ctx.unres.objs[i])->type;
Radek Krejcia3045382018-11-22 14:30:31 +01007353 if (type->basetype == LY_TYPE_LEAFREF) {
7354 /* validate the path */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007355 LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[i]), (struct lysc_type_leafref*)type, NULL), error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01007356 } else if (type->basetype == LY_TYPE_UNION) {
7357 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7358 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7359 /* validate the path */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007360 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[i]),
Michal Vasko175012e2019-11-06 15:49:14 +01007361 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v], NULL);
7362 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01007363 }
7364 }
Radek Krejcia3045382018-11-22 14:30:31 +01007365 }
7366 }
Michal Vasko175012e2019-11-06 15:49:14 +01007367
7368 /* check xpath */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007369 LY_CHECK_GOTO(ret = lys_compile_check_xpath(&ctx, ctx.unres.objs[i]), error);
Radek Krejcia3045382018-11-22 14:30:31 +01007370 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007371 for (i = 0; i < ctx.unres.count; ++i) {
7372 if (((struct lysc_node*)ctx.unres.objs[i])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
7373 type = ((struct lysc_node_leaf*)ctx.unres.objs[i])->type;
Radek Krejci412ddfa2018-11-23 11:44:11 +01007374 if (type->basetype == LY_TYPE_LEAFREF) {
7375 /* store pointer to the real type */
7376 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7377 typeiter->basetype == LY_TYPE_LEAFREF;
7378 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7379 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01007380 } else if (type->basetype == LY_TYPE_UNION) {
7381 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7382 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7383 /* store pointer to the real type */
7384 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7385 typeiter->basetype == LY_TYPE_LEAFREF;
7386 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7387 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7388 }
7389 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01007390 }
7391 }
7392 }
Radek Krejciec4da802019-05-02 13:02:41 +02007393
Radek Krejci474f9b82019-07-24 11:36:37 +02007394 /* finish incomplete default values compilation */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007395 for (i = 0; i < ctx.dflts.count; ++i) {
Radek Krejci474f9b82019-07-24 11:36:37 +02007396 struct ly_err_item *err = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007397 struct lysc_incomplete_dflt *r = ctx.dflts.objs[i];
Radek Krejci950f6a52019-09-12 17:15:32 +02007398 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
Radek Krejci474f9b82019-07-24 11:36:37 +02007399 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7400 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7401 if (err) {
7402 ly_err_print(err);
7403 ctx.path[0] = '\0';
Michal Vasko03ff5a72019-09-11 13:49:33 +02007404 lysc_path(r->context_node, LYSC_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
Radek Krejci474f9b82019-07-24 11:36:37 +02007405 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
7406 "Invalid default - value does not fit the type (%s).", err->msg);
7407 ly_err_free(err);
7408 }
7409 LY_CHECK_GOTO(ret, error);
7410 }
7411
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007412 /* validate non-instantiated groupings from the parsed schema,
7413 * without it we would accept even the schemas with invalid grouping specification */
7414 ctx.options |= LYSC_OPT_GROUPING;
7415 LY_ARRAY_FOR(sp->groupings, u) {
7416 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007417 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007418 }
7419 }
7420 LY_LIST_FOR(sp->data, node_p) {
7421 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7422 LY_ARRAY_FOR(grps, u) {
7423 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007424 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007425 }
7426 }
7427 }
7428
Radek Krejci474f9b82019-07-24 11:36:37 +02007429 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7430 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7431 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7432 }
7433
Michal Vasko8d544252020-03-02 10:19:52 +01007434#if 0
7435 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
7436 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
7437 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
7438 * the anotation definitions available in the internal schema structure. */
7439 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
7440 if (lyp_add_ietf_netconf_annotations(mod)) {
7441 lys_free(mod, NULL, 1, 1);
7442 return NULL;
7443 }
7444 }
7445#endif
7446
7447 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
7448 if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
7449 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error);
7450 }
7451
Radek Krejci1c0c3442019-07-23 16:08:47 +02007452 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007453 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007454 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007455 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007456 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007457
Radek Krejciec4da802019-05-02 13:02:41 +02007458 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01007459 lysp_module_free(mod->parsed);
7460 ((struct lys_module*)mod)->parsed = NULL;
7461 }
7462
Radek Krejciec4da802019-05-02 13:02:41 +02007463 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007464 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007465 for (i = 0; i < ctx.ctx->list.count; ++i) {
7466 m = ctx.ctx->list.objs[i];
Radek Krejci95710c92019-02-11 15:49:55 +01007467 if (m->implemented == 2) {
7468 m->implemented = 1;
7469 }
7470 }
7471 }
7472
Radek Krejci19a96102018-11-15 13:38:09 +01007473 ((struct lys_module*)mod)->compiled = mod_c;
7474 return LY_SUCCESS;
7475
7476error:
Radek Krejci95710c92019-02-11 15:49:55 +01007477 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007478 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007479 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007480 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007481 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007482 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007483 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007484 mod->compiled = NULL;
7485
7486 /* revert compilation of modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007487 for (i = 0; i < ctx.ctx->list.count; ++i) {
7488 m = ctx.ctx->list.objs[i];
Michal Vasko2947ce12019-12-16 10:37:19 +01007489 if ((m->implemented == 2) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007490 /* revert features list to the precompiled state */
7491 lys_feature_precompile_revert(&ctx, m);
7492 /* mark module as imported-only / not-implemented */
7493 m->implemented = 0;
7494 /* free the compiled version of the module */
7495 lysc_module_free(m->compiled, NULL);
7496 m->compiled = NULL;
7497 }
7498 }
7499
Radek Krejci19a96102018-11-15 13:38:09 +01007500 return ret;
7501}