blob: 2fcea598b7b9b16d35ef80242440bd6d4c2b95c2 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejci19a96102018-11-15 13:38:09 +010016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020026#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "dict.h"
29#include "log.h"
Michal Vasko004d3152020-06-11 19:59:22 +020030#include "path.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020031#include "parser.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020032#include "parser_schema.h"
Radek Krejci0935f412019-08-20 16:15:18 +020033#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020035#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree_data.h"
Michal Vasko7c8439f2020-08-05 13:25:19 +020039#include "tree_data_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020040#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010041#include "tree_schema_internal.h"
42#include "xpath.h"
43
Michal Vasko5fe75f12020-03-02 13:52:37 +010044static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
Radek Krejci0f969882020-08-21 16:56:47 +020045 void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
Michal Vasko5fe75f12020-03-02 13:52:37 +010046
Michal Vasko7f45cf22020-10-01 12:49:44 +020047static LY_ERR lysp_qname_dup(const struct ly_ctx *ctx, struct lysp_qname *qname, const struct lysp_qname *orig_qname);
48
Radek Krejci19a96102018-11-15 13:38:09 +010049/**
50 * @brief Duplicate string into dictionary
51 * @param[in] CTX libyang context of the dictionary.
52 * @param[in] ORIG String to duplicate.
53 * @param[out] DUP Where to store the result.
54 */
Radek Krejci011e4aa2020-09-04 15:22:31 +020055#define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);}
56
57#define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);}
Radek Krejci19a96102018-11-15 13:38:09 +010058
Michal Vasko7f45cf22020-10-01 12:49:44 +020059#define DUP_ARRAY(CTX, ORIG_ARRAY, NEW_ARRAY, DUP_FUNC) \
60 if (ORIG_ARRAY) { \
61 LY_ARRAY_COUNT_TYPE u; \
62 LY_ARRAY_CREATE_RET(CTX, NEW_ARRAY, LY_ARRAY_COUNT(ORIG_ARRAY), LY_EMEM); \
63 LY_ARRAY_FOR(ORIG_ARRAY, u) { \
64 LY_ARRAY_INCREMENT(NEW_ARRAY); \
65 LY_CHECK_RET(DUP_FUNC(CTX, &(NEW_ARRAY)[u], &(ORIG_ARRAY)[u])); \
66 } \
67 }
68
Radek Krejciec4da802019-05-02 13:02:41 +020069#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010070 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020071 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
72 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
73 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci19a96102018-11-15 13:38:09 +010074 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020075 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010076 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
77 } \
78 }
79
Michal Vasko7f45cf22020-10-01 12:49:44 +020080#define COMPILE_OP_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010081 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020082 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
83 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
84 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010085 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020086 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Michal Vasko7f45cf22020-10-01 12:49:44 +020087 if (RET == LY_EDENIED) { \
88 LY_ARRAY_DECREMENT(ARRAY_C); \
89 } else if (RET != LY_SUCCESS) { \
90 goto GOTO; \
91 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010092 } \
93 }
94
Radek Krejci0935f412019-08-20 16:15:18 +020095#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
96 if (EXTS_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +020097 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
98 for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \
Radek Krejci0935f412019-08-20 16:15:18 +020099 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko8d544252020-03-02 10:19:52 +0100100 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 +0200101 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
102 } \
103 }
104
Radek Krejciec4da802019-05-02 13:02:41 +0200105#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100106 if (ARRAY_P) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200107 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
108 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
109 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100110 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200111 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +0100112 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
113 } \
114 }
115
Radek Krejciec4da802019-05-02 13:02:41 +0200116#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +0100117 if (MEMBER_P) { \
118 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
119 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200120 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100121 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
122 }
123
Radek Krejciec4da802019-05-02 13:02:41 +0200124#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100125 if (MEMBER_P) { \
126 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200127 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100128 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200129 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100130 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
131 }
132
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100133#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100134 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200135 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100136 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100137 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
138 return LY_EVALID; \
139 } \
140 } \
141 }
142
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100143#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
144 if (ARRAY) { \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200145 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100146 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
147 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
148 return LY_EVALID; \
149 } \
150 } \
151 }
152
153struct lysc_ext *
154lysc_ext_dup(struct lysc_ext *orig)
155{
156 ++orig->refcount;
157 return orig;
158}
159
Radek Krejci19a96102018-11-15 13:38:09 +0100160static struct lysc_ext_instance *
161lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
162{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100163 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100164 (void) ctx;
165 (void) orig;
166 return NULL;
167}
168
Michal Vasko7f45cf22020-10-01 12:49:44 +0200169/**
170 * @brief Add/replace a leaf default value in unres.
171 * Can also be used for a single leaf-list default value.
172 *
173 * @param[in] ctx Compile context.
174 * @param[in] leaf Leaf with the default value.
175 * @param[in] dflt Default value to use.
176 * @return LY_ERR value.
177 */
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200178static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +0200179lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200180{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200181 struct lysc_unres_dflt *r = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200182 uint32_t i;
183
184 for (i = 0; i < ctx->dflts.count; ++i) {
Michal Vasko7f45cf22020-10-01 12:49:44 +0200185 if (((struct lysc_unres_dflt *)ctx->dflts.objs[i])->leaf == leaf) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200186 /* just replace the default */
Michal Vasko7f45cf22020-10-01 12:49:44 +0200187 r = ctx->dflts.objs[i];
188 lysp_qname_free(ctx->ctx, r->dflt);
189 free(r->dflt);
190 break;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200191 }
192 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200193 if (!r) {
194 /* add new unres item */
195 r = calloc(1, sizeof *r);
196 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
197 r->leaf = leaf;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200198
Michal Vasko7f45cf22020-10-01 12:49:44 +0200199 LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL));
200 }
201
202 r->dflt = malloc(sizeof *r->dflt);
Michal Vaskoaf702452020-10-02 09:02:55 +0200203 LY_CHECK_GOTO(!r->dflt, error);
204 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200205
206 return LY_SUCCESS;
Michal Vaskoaf702452020-10-02 09:02:55 +0200207
208error:
209 free(r->dflt);
210 LOGMEM(ctx->ctx);
211 return LY_EMEM;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200212}
213
Radek Krejcib56c7502019-02-13 14:19:54 +0100214/**
Michal Vasko7f45cf22020-10-01 12:49:44 +0200215 * @brief Add/replace a leaf-list default value(s) in unres.
216 *
217 * @param[in] ctx Compile context.
218 * @param[in] llist Leaf-list with the default value.
219 * @param[in] dflts Sized array of the default values.
220 * @return LY_ERR value.
Radek Krejci474f9b82019-07-24 11:36:37 +0200221 */
222static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +0200223lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
Radek Krejci474f9b82019-07-24 11:36:37 +0200224{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200225 struct lysc_unres_dflt *r = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200226 uint32_t i;
227
228 for (i = 0; i < ctx->dflts.count; ++i) {
Michal Vasko7f45cf22020-10-01 12:49:44 +0200229 if (((struct lysc_unres_dflt *)ctx->dflts.objs[i])->llist == llist) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200230 /* just replace the defaults */
Michal Vasko7f45cf22020-10-01 12:49:44 +0200231 r = ctx->dflts.objs[i];
232 lysp_qname_free(ctx->ctx, r->dflt);
233 free(r->dflt);
234 r->dflt = NULL;
235 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
236 r->dflts = NULL;
237 break;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200238 }
239 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200240 if (!r) {
241 r = calloc(1, sizeof *r);
242 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
243 r->llist = llist;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200244
Michal Vasko7f45cf22020-10-01 12:49:44 +0200245 LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL));
246 }
247
248 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
Radek Krejci474f9b82019-07-24 11:36:37 +0200249
250 return LY_SUCCESS;
251}
252
Radek Krejci474f9b82019-07-24 11:36:37 +0200253static void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200254lysc_unres_dflt_free(const struct ly_ctx *ctx, struct lysc_unres_dflt *r)
Radek Krejci474f9b82019-07-24 11:36:37 +0200255{
Michal Vasko7f45cf22020-10-01 12:49:44 +0200256 assert(!r->dflt || !r->dflts);
257 if (r->dflt) {
258 lysp_qname_free((struct ly_ctx *)ctx, r->dflt);
259 free(r->dflt);
260 } else {
261 FREE_ARRAY((struct ly_ctx *)ctx, r->dflts, lysp_qname_free);
Radek Krejci474f9b82019-07-24 11:36:37 +0200262 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200263 free(r);
Radek Krejci474f9b82019-07-24 11:36:37 +0200264}
265
Radek Krejci0e59c312019-08-15 15:34:15 +0200266void
Radek Krejci327de162019-06-14 12:52:07 +0200267lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
268{
269 int len;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200270 uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
Radek Krejci327de162019-06-14 12:52:07 +0200271
272 if (!name) {
273 /* removing last path segment */
274 if (ctx->path[ctx->path_len - 1] == '}') {
Michal Vaskod989ba02020-08-24 10:59:24 +0200275 for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200276 if (ctx->path[ctx->path_len] == '=') {
277 ctx->path[ctx->path_len++] = '}';
278 } else {
279 /* not a top-level special tag, remove also preceiding '/' */
280 goto remove_nodelevel;
281 }
282 } else {
283remove_nodelevel:
Michal Vaskod989ba02020-08-24 10:59:24 +0200284 for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
Radek Krejci327de162019-06-14 12:52:07 +0200285 if (ctx->path_len == 0) {
286 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200287 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200288 }
289 }
290 /* set new terminating NULL-byte */
291 ctx->path[ctx->path_len] = '\0';
292 } else {
293 if (ctx->path_len > 1) {
294 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
295 /* extension of the special tag */
296 nextlevel = 2;
297 --ctx->path_len;
298 } else {
299 /* there is already some path, so add next level */
300 nextlevel = 1;
301 }
302 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
303
304 if (nextlevel != 2) {
305 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
306 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200307 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200308 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200309 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 +0200310 }
311 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200312 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200313 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200314 if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
Radek Krejciacc79042019-07-25 14:14:57 +0200315 /* output truncated */
316 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
317 } else {
318 ctx->path_len += len;
319 }
Radek Krejci327de162019-06-14 12:52:07 +0200320 }
321}
322
323/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100324 * @brief Duplicate the compiled pattern structure.
325 *
326 * Instead of duplicating memory, the reference counter in the @p orig is increased.
327 *
328 * @param[in] orig The pattern structure to duplicate.
329 * @return The duplicated structure to use.
330 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200331static struct lysc_pattern *
Radek Krejci19a96102018-11-15 13:38:09 +0100332lysc_pattern_dup(struct lysc_pattern *orig)
333{
334 ++orig->refcount;
335 return orig;
336}
337
Radek Krejcib56c7502019-02-13 14:19:54 +0100338/**
339 * @brief Duplicate the array of compiled patterns.
340 *
341 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
342 *
343 * @param[in] ctx Libyang context for logging.
344 * @param[in] orig The patterns sized array to duplicate.
345 * @return New sized array as a copy of @p orig.
346 * @return NULL in case of memory allocation error.
347 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200348static struct lysc_pattern **
Radek Krejci19a96102018-11-15 13:38:09 +0100349lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
350{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100351 struct lysc_pattern **dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200352 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +0100353
Radek Krejcib56c7502019-02-13 14:19:54 +0100354 assert(orig);
355
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200356 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100357 LY_ARRAY_FOR(orig, u) {
358 dup[u] = lysc_pattern_dup(orig[u]);
359 LY_ARRAY_INCREMENT(dup);
360 }
361 return dup;
362}
363
Radek Krejcib56c7502019-02-13 14:19:54 +0100364/**
365 * @brief Duplicate compiled range structure.
366 *
367 * @param[in] ctx Libyang context for logging.
368 * @param[in] orig The range structure to be duplicated.
369 * @return New compiled range structure as a copy of @p orig.
370 * @return NULL in case of memory allocation error.
371 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200372struct lysc_range *
Radek Krejci19a96102018-11-15 13:38:09 +0100373lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
374{
375 struct lysc_range *dup;
376 LY_ERR ret;
377
Radek Krejcib56c7502019-02-13 14:19:54 +0100378 assert(orig);
379
Radek Krejci19a96102018-11-15 13:38:09 +0100380 dup = calloc(1, sizeof *dup);
381 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
382 if (orig->parts) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200383 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
384 LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
385 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
Radek Krejci19a96102018-11-15 13:38:09 +0100386 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200387 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
388 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100389 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
390
391 return dup;
392cleanup:
393 free(dup);
394 (void) ret; /* set but not used due to the return type */
395 return NULL;
396}
397
Radek Krejcib56c7502019-02-13 14:19:54 +0100398/**
399 * @brief Stack for processing if-feature expressions.
400 */
Radek Krejci19a96102018-11-15 13:38:09 +0100401struct iff_stack {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200402 size_t size; /**< number of items in the stack */
403 size_t index; /**< first empty item */
404 uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100405};
406
Radek Krejcib56c7502019-02-13 14:19:54 +0100407/**
408 * @brief Add @ref ifftokens into the stack.
409 * @param[in] stack The if-feature stack to use.
410 * @param[in] value One of the @ref ifftokens to store in the stack.
411 * @return LY_EMEM in case of memory allocation error
412 * @return LY_ESUCCESS if the value successfully stored.
413 */
Radek Krejci19a96102018-11-15 13:38:09 +0100414static LY_ERR
415iff_stack_push(struct iff_stack *stack, uint8_t value)
416{
417 if (stack->index == stack->size) {
418 stack->size += 4;
419 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
420 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
421 }
422 stack->stack[stack->index++] = value;
423 return LY_SUCCESS;
424}
425
Radek Krejcib56c7502019-02-13 14:19:54 +0100426/**
427 * @brief Get (and remove) the last item form the stack.
428 * @param[in] stack The if-feature stack to use.
429 * @return The value from the top of the stack.
430 */
Radek Krejci19a96102018-11-15 13:38:09 +0100431static uint8_t
432iff_stack_pop(struct iff_stack *stack)
433{
Radek Krejcib56c7502019-02-13 14:19:54 +0100434 assert(stack && stack->index);
435
Radek Krejci19a96102018-11-15 13:38:09 +0100436 stack->index--;
437 return stack->stack[stack->index];
438}
439
Radek Krejcib56c7502019-02-13 14:19:54 +0100440/**
441 * @brief Clean up the stack.
442 * @param[in] stack The if-feature stack to use.
443 */
Radek Krejci19a96102018-11-15 13:38:09 +0100444static void
445iff_stack_clean(struct iff_stack *stack)
446{
447 stack->size = 0;
448 free(stack->stack);
449}
450
Radek Krejcib56c7502019-02-13 14:19:54 +0100451/**
452 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
453 * (libyang format of the if-feature expression).
454 * @param[in,out] list The 2bits array to modify.
455 * @param[in] op The operand (@ref ifftokens) to store.
456 * @param[in] pos Position (0-based) where to store the given @p op.
457 */
Radek Krejci19a96102018-11-15 13:38:09 +0100458static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200459iff_setop(uint8_t *list, uint8_t op, size_t pos)
Radek Krejci19a96102018-11-15 13:38:09 +0100460{
461 uint8_t *item;
462 uint8_t mask = 3;
463
Radek Krejci19a96102018-11-15 13:38:09 +0100464 assert(op <= 3); /* max 2 bits */
465
466 item = &list[pos / 4];
467 mask = mask << 2 * (pos % 4);
468 *item = (*item) & ~mask;
469 *item = (*item) | (op << 2 * (pos % 4));
470}
471
Radek Krejcib56c7502019-02-13 14:19:54 +0100472#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
473#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100474
Radek Krejci0af46292019-01-11 16:02:31 +0100475/**
476 * @brief Find a feature of the given name and referenced in the given module.
477 *
478 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
479 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
480 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
481 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
482 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
483 * assigned till that time will be still valid.
484 *
485 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
486 * @param[in] name Name of the feature including possible prefix.
487 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
488 * @return Pointer to the feature structure if found, NULL otherwise.
489 */
Radek Krejci19a96102018-11-15 13:38:09 +0100490static struct lysc_feature *
Michal Vasko7f45cf22020-10-01 12:49:44 +0200491lys_feature_find(const struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100492{
493 size_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200494 LY_ARRAY_COUNT_TYPE u;
Radek Krejci14915cc2020-09-14 17:28:13 +0200495 struct lysc_feature *f;
Radek Krejci19a96102018-11-15 13:38:09 +0100496
Radek Krejci120d8542020-08-12 09:29:16 +0200497 assert(mod);
498
Radek Krejci19a96102018-11-15 13:38:09 +0100499 for (i = 0; i < len; ++i) {
500 if (name[i] == ':') {
501 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100502 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100503 LY_CHECK_RET(!mod, NULL);
504
505 name = &name[i + 1];
506 len = len - i - 1;
507 }
508 }
509
510 /* we have the correct module, get the feature */
Radek Krejci14915cc2020-09-14 17:28:13 +0200511 LY_ARRAY_FOR(mod->features, u) {
512 f = &mod->features[u];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200513 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100514 return f;
515 }
516 }
517
518 return NULL;
519}
520
Michal Vasko8d544252020-03-02 10:19:52 +0100521/**
Michal Vasko5fe75f12020-03-02 13:52:37 +0100522 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
523 */
524static LY_ERR
525lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
526{
527 LY_ERR ret = LY_SUCCESS;
528
529 if (!ext_p->compiled) {
530 lysc_update_path(ctx, NULL, "{extension}");
531 lysc_update_path(ctx, NULL, ext_p->name);
532
533 /* compile the extension definition */
534 ext_p->compiled = calloc(1, sizeof **ext);
535 ext_p->compiled->refcount = 1;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200536 DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done);
537 DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext_p->compiled->argument, ret, done);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100538 ext_p->compiled->module = (struct lys_module *)ext_mod;
539 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
540
541 lysc_update_path(ctx, NULL, NULL);
542 lysc_update_path(ctx, NULL, NULL);
543
544 /* find extension definition plugin */
545 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
546 }
547
548 *ext = lysc_ext_dup(ext_p->compiled);
549
550done:
551 return ret;
552}
553
554/**
Michal Vasko8d544252020-03-02 10:19:52 +0100555 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
556 *
557 * @param[in] ctx Compilation context.
558 * @param[in] ext_p Parsed extension instance.
559 * @param[in,out] ext Prepared compiled extension instance.
560 * @param[in] parent Extension instance parent.
561 * @param[in] parent_type Extension instance parent type.
562 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
563 */
Radek Krejci19a96102018-11-15 13:38:09 +0100564static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +0100565lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
Radek Krejci0f969882020-08-21 16:56:47 +0200566 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
Radek Krejci19a96102018-11-15 13:38:09 +0100567{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200568 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100569 const char *name;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200570 size_t u;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200571 LY_ARRAY_COUNT_TYPE v;
Radek Krejci7c960162019-09-18 14:16:12 +0200572 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100573
Radek Krejci011e4aa2020-09-04 15:22:31 +0200574 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret);
575 LY_CHECK_RET(ret);
576
Radek Krejci19a96102018-11-15 13:38:09 +0100577 ext->insubstmt = ext_p->insubstmt;
578 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200579 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200580 ext->parent = parent;
581 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100582
Michal Vasko22df3f02020-08-24 13:29:22 +0200583 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node *)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200584
Radek Krejci19a96102018-11-15 13:38:09 +0100585 /* get module where the extension definition should be placed */
Radek Krejci1e008d22020-08-17 11:37:37 +0200586 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {}
Radek Krejci7c960162019-09-18 14:16:12 +0200587 if (ext_p->yin) {
588 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
589 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
590 * YANG (import) prefix must be done here. */
591 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200592 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, &ext_p->name[u], 0, &prefixed_name), cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200593 u = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200594 } else {
595 assert(ctx->mod_def->parsed);
596 LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) {
597 if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) {
Radek Krejci7c960162019-09-18 14:16:12 +0200598 char *s;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200599 LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->parsed->imports[v].prefix, &ext_p->name[u]) == -1,
Radek Krejci200f1062020-07-11 22:51:03 +0200600 ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200601 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->ctx, s, &prefixed_name), cleanup);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200602 u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */
Radek Krejci7c960162019-09-18 14:16:12 +0200603 break;
604 }
605 }
606 }
607 if (!prefixed_name) {
608 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
609 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200610 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200611 goto cleanup;
612 }
613 } else {
614 prefixed_name = ext_p->name;
615 }
616 lysc_update_path(ctx, NULL, prefixed_name);
617
Michal Vasko8d544252020-03-02 10:19:52 +0100618 if (!ext_mod) {
Radek Krejci63f55512020-05-20 14:37:18 +0200619 ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def;
Michal Vasko8d544252020-03-02 10:19:52 +0100620 if (!ext_mod) {
621 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
622 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200623 ret = LY_EVALID;
Michal Vasko8d544252020-03-02 10:19:52 +0100624 goto cleanup;
625 } else if (!ext_mod->parsed->extensions) {
626 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
627 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
628 prefixed_name, ext_mod->name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200629 ret = LY_EVALID;
Michal Vasko8d544252020-03-02 10:19:52 +0100630 goto cleanup;
631 }
Radek Krejci7c960162019-09-18 14:16:12 +0200632 }
633 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200634
Michal Vasko5fe75f12020-03-02 13:52:37 +0100635 /* find the parsed extension definition there */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200636 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
637 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100638 /* compile extension definition and assign it */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200639 LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100640 break;
641 }
642 }
Radek Krejci7c960162019-09-18 14:16:12 +0200643 if (!ext->def) {
644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
645 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200646 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200647 goto cleanup;
648 }
Radek Krejci0935f412019-08-20 16:15:18 +0200649
Radek Krejcif56e2a42019-09-09 14:15:25 +0200650 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
651 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
652 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200653 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200654 /* Schema was parsed from YIN and an argument is expected, ... */
655 struct lysp_stmt *stmt = NULL;
656
657 if (ext->def->flags & LYS_YINELEM_TRUE) {
658 /* ... argument was the first XML child element */
659 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
660 /* TODO check namespace of the statement */
661 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
662 stmt = ext_p->child;
663 }
664 }
665 } else {
666 /* ... argument was one of the XML attributes which are represented as child stmt
667 * with LYS_YIN_ATTR flag */
668 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
669 if (!strcmp(stmt->stmt, ext->def->argument)) {
670 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200671 break;
672 }
673 }
674 }
675 if (!stmt) {
676 /* missing extension's argument */
677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200678 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200679 ret = LY_EVALID;
Radek Krejci7c960162019-09-18 14:16:12 +0200680 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200681
682 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200683 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup);
Radek Krejcif56e2a42019-09-09 14:15:25 +0200684 stmt->flags |= LYS_YIN_ARGUMENT;
685 }
Radek Krejci7c960162019-09-18 14:16:12 +0200686 if (prefixed_name != ext_p->name) {
687 lydict_remove(ctx->ctx, ext_p->name);
688 ext_p->name = prefixed_name;
689 if (!ext_p->argument && ext->argument) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200690 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, ext->argument, 0, &ext_p->argument), cleanup);
Radek Krejci7c960162019-09-18 14:16:12 +0200691 }
692 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200693
Radek Krejci0935f412019-08-20 16:15:18 +0200694 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200695 if (ext->argument) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200696 lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument);
Radek Krejciad5963b2019-09-06 16:03:05 +0200697 }
Radek Krejci011e4aa2020-09-04 15:22:31 +0200698 LY_CHECK_GOTO(ret = ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200699 if (ext->argument) {
700 lysc_update_path(ctx, NULL, NULL);
701 }
Radek Krejci0935f412019-08-20 16:15:18 +0200702 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200703 ext_p->compiled = ext;
704
Radek Krejci7c960162019-09-18 14:16:12 +0200705cleanup:
706 if (prefixed_name && prefixed_name != ext_p->name) {
707 lydict_remove(ctx->ctx, prefixed_name);
708 }
709
Radek Krejcif56e2a42019-09-09 14:15:25 +0200710 lysc_update_path(ctx, NULL, NULL);
711 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200712
Radek Krejci7c960162019-09-18 14:16:12 +0200713 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200714}
715
716/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100717 * @brief Compile information from the if-feature statement
718 * @param[in] ctx Compile context.
Michal Vasko7f45cf22020-10-01 12:49:44 +0200719 * @param[in] qname The if-feature argument to process. It is pointer-to-qname just to unify the compile functions.
Radek Krejcib56c7502019-02-13 14:19:54 +0100720 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
721 * @return LY_ERR value.
722 */
Radek Krejci19a96102018-11-15 13:38:09 +0100723static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +0200724lys_compile_iffeature(struct lysc_ctx *ctx, struct lysp_qname *qname, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100725{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200726 LY_ERR rc = LY_SUCCESS;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200727 const char *c = qname->str;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200728 int64_t i, j;
729 int8_t op_len, last_not = 0, checkversion = 0;
730 LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
Radek Krejci19a96102018-11-15 13:38:09 +0100731 uint8_t op;
732 struct iff_stack stack = {0, 0, NULL};
733 struct lysc_feature *f;
734
735 assert(c);
736
737 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200738 for (i = j = 0; c[i]; i++) {
Radek Krejci19a96102018-11-15 13:38:09 +0100739 if (c[i] == '(') {
740 j++;
741 checkversion = 1;
742 continue;
743 } else if (c[i] == ')') {
744 j--;
745 continue;
746 } else if (isspace(c[i])) {
747 checkversion = 1;
748 continue;
749 }
750
Radek Krejci1deb5be2020-08-26 16:43:36 +0200751 if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) {
752 uint64_t spaces;
753 for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++);
754 if (c[i + op_len + spaces] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100755 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Michal Vasko7f45cf22020-10-01 12:49:44 +0200756 "Invalid value \"%s\" of if-feature - unexpected end of expression.", qname->str);
Radek Krejci19a96102018-11-15 13:38:09 +0100757 return LY_EVALID;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200758 } else if (!isspace(c[i + op_len])) {
Radek Krejci19a96102018-11-15 13:38:09 +0100759 /* feature name starting with the not/and/or */
760 last_not = 0;
761 f_size++;
762 } else if (c[i] == 'n') { /* not operation */
763 if (last_not) {
764 /* double not */
765 expr_size = expr_size - 2;
766 last_not = 0;
767 } else {
768 last_not = 1;
769 }
770 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200771 if (f_exp != f_size) {
772 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Michal Vasko7f45cf22020-10-01 12:49:44 +0200773 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.",
774 qname->str, op_len, &c[i]);
Radek Krejci6788abc2019-06-14 13:56:49 +0200775 return LY_EVALID;
776 }
Radek Krejci19a96102018-11-15 13:38:09 +0100777 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200778
Radek Krejci19a96102018-11-15 13:38:09 +0100779 /* not a not operation */
780 last_not = 0;
781 }
Radek Krejci1deb5be2020-08-26 16:43:36 +0200782 i += op_len;
Radek Krejci19a96102018-11-15 13:38:09 +0100783 } else {
784 f_size++;
785 last_not = 0;
786 }
787 expr_size++;
788
789 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200790 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100791 i--;
792 break;
793 }
794 i++;
795 }
796 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200797 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100798 /* not matching count of ( and ) */
799 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Michal Vasko7f45cf22020-10-01 12:49:44 +0200800 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", qname->str);
Radek Krejci19a96102018-11-15 13:38:09 +0100801 return LY_EVALID;
802 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200803 if (f_exp != f_size) {
804 /* features do not match the needed arguments for the logical operations */
805 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
806 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
Michal Vasko7f45cf22020-10-01 12:49:44 +0200807 "the required number of operands for the operations.", qname->str);
Radek Krejci6788abc2019-06-14 13:56:49 +0200808 return LY_EVALID;
809 }
Radek Krejci19a96102018-11-15 13:38:09 +0100810
811 if (checkversion || expr_size > 1) {
812 /* check that we have 1.1 module */
Michal Vasko7f45cf22020-10-01 12:49:44 +0200813 if (qname->mod->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Michal Vasko7f45cf22020-10-01 12:49:44 +0200815 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", qname->str);
Radek Krejci19a96102018-11-15 13:38:09 +0100816 return LY_EVALID;
817 }
818 }
819
820 /* allocate the memory */
821 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
822 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
823 stack.stack = malloc(expr_size * sizeof *stack.stack);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200824 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx); rc = LY_EMEM, error);
Radek Krejci19a96102018-11-15 13:38:09 +0100825
826 stack.size = expr_size;
827 f_size--; expr_size--; /* used as indexes from now */
828
829 for (i--; i >= 0; i--) {
830 if (c[i] == ')') {
831 /* push it on stack */
832 iff_stack_push(&stack, LYS_IFF_RP);
833 continue;
834 } else if (c[i] == '(') {
835 /* pop from the stack into result all operators until ) */
Michal Vaskod989ba02020-08-24 10:59:24 +0200836 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
Radek Krejci19a96102018-11-15 13:38:09 +0100837 iff_setop(iff->expr, op, expr_size--);
838 }
839 continue;
840 } else if (isspace(c[i])) {
841 continue;
842 }
843
844 /* end of operator or operand -> find beginning and get what is it */
845 j = i + 1;
846 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
847 i--;
848 }
849 i++; /* go back by one step */
850
851 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
852 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
853 /* double not */
854 iff_stack_pop(&stack);
855 } else {
856 /* not has the highest priority, so do not pop from the stack
857 * as in case of AND and OR */
858 iff_stack_push(&stack, LYS_IFF_NOT);
859 }
860 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
861 /* as for OR - pop from the stack all operators with the same or higher
862 * priority and store them to the result, then push the AND to the stack */
863 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
864 op = iff_stack_pop(&stack);
865 iff_setop(iff->expr, op, expr_size--);
866 }
867 iff_stack_push(&stack, LYS_IFF_AND);
868 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
869 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
870 op = iff_stack_pop(&stack);
871 iff_setop(iff->expr, op, expr_size--);
872 }
873 iff_stack_push(&stack, LYS_IFF_OR);
874 } else {
875 /* feature name, length is j - i */
876
877 /* add it to the expression */
878 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
879
880 /* now get the link to the feature definition */
Michal Vasko7f45cf22020-10-01 12:49:44 +0200881 f = lys_feature_find(qname->mod, &c[i], j - i);
Radek Krejci0af46292019-01-11 16:02:31 +0100882 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Michal Vasko7f45cf22020-10-01 12:49:44 +0200883 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", qname->str, j - i, &c[i]);
884 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100885 iff->features[f_size] = f;
886 LY_ARRAY_INCREMENT(iff->features);
887 f_size--;
888 }
889 }
890 while (stack.index) {
891 op = iff_stack_pop(&stack);
892 iff_setop(iff->expr, op, expr_size--);
893 }
894
895 if (++expr_size || ++f_size) {
896 /* not all expected operators and operands found */
897 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Michal Vasko7f45cf22020-10-01 12:49:44 +0200898 "Invalid value \"%s\" of if-feature - processing error.", qname->str);
Radek Krejci19a96102018-11-15 13:38:09 +0100899 rc = LY_EINT;
900 } else {
901 rc = LY_SUCCESS;
902 }
903
904error:
905 /* cleanup */
906 iff_stack_clean(&stack);
907
908 return rc;
909}
910
Radek Krejcib56c7502019-02-13 14:19:54 +0100911/**
Michal Vasko175012e2019-11-06 15:49:14 +0100912 * @brief Get the XPath context node for the given schema node.
913 * @param[in] start The schema node where the XPath expression appears.
914 * @return The context node to evaluate XPath expression in given schema node.
915 * @return NULL in case the context node is the root node.
916 */
917static struct lysc_node *
918lysc_xpath_context(struct lysc_node *start)
919{
Michal Vasko1bf09392020-03-27 12:38:10 +0100920 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF));
Radek Krejci1e008d22020-08-17 11:37:37 +0200921 start = start->parent) {}
Michal Vasko175012e2019-11-06 15:49:14 +0100922 return start;
923}
924
925/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100926 * @brief Compile information from the when statement
927 * @param[in] ctx Compile context.
928 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100929 * @param[in] flags Flags of the node with the "when" defiition.
930 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100931 * @param[out] when Pointer where to store pointer to the created compiled when structure.
932 * @return LY_ERR value.
933 */
Radek Krejci19a96102018-11-15 13:38:09 +0100934static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100935lys_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 +0100936{
Radek Krejci19a96102018-11-15 13:38:09 +0100937 LY_ERR ret = LY_SUCCESS;
938
Radek Krejci00b874b2019-02-12 10:54:50 +0100939 *when = calloc(1, sizeof **when);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200940 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejci00b874b2019-02-12 10:54:50 +0100941 (*when)->refcount = 1;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200942 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejcia0f704a2019-09-09 16:12:23 +0200943 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100944 (*when)->context = lysc_xpath_context(node);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200945 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
946 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +0200947 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100948 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100949
950done:
951 return ret;
952}
953
Radek Krejcib56c7502019-02-13 14:19:54 +0100954/**
955 * @brief Compile information from the must statement
956 * @param[in] ctx Compile context.
957 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100958 * @param[in,out] must Prepared (empty) compiled must structure to fill.
959 * @return LY_ERR value.
960 */
Radek Krejci19a96102018-11-15 13:38:09 +0100961static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200962lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100963{
Radek Krejci19a96102018-11-15 13:38:09 +0100964 LY_ERR ret = LY_SUCCESS;
965
Michal Vasko7f45cf22020-10-01 12:49:44 +0200966 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
967 must->module = (struct lys_module *)must_p->arg.mod;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200968 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
969 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
970 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
971 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +0200972 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100973
974done:
975 return ret;
976}
977
Radek Krejcib56c7502019-02-13 14:19:54 +0100978/**
Michal Vasko7c8439f2020-08-05 13:25:19 +0200979 * @brief Compile information in the import statement - make sure there is the target module
Radek Krejcib56c7502019-02-13 14:19:54 +0100980 * @param[in] ctx Compile context.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200981 * @param[in] imp_p The parsed import statement structure to fill the module to.
Radek Krejcib56c7502019-02-13 14:19:54 +0100982 * @return LY_ERR value.
983 */
Radek Krejci19a96102018-11-15 13:38:09 +0100984static LY_ERR
Michal Vasko7c8439f2020-08-05 13:25:19 +0200985lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
Radek Krejci19a96102018-11-15 13:38:09 +0100986{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200987 const struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100988 LY_ERR ret = LY_SUCCESS;
989
Radek Krejci7f2a5362018-11-28 13:05:37 +0100990 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
991 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100992 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200993 if (!imp_p->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100994 /* try to use filepath if present */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200995 if (imp_p->module->filepath) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200996 struct ly_in *in;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200997 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200998 LOGINT(ctx->ctx);
999 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001000 LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)],
Michal Vasko3a41dff2020-07-15 14:30:28 +02001001 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +02001002 if (mod != imp_p->module) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001003 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Michal Vasko7c8439f2020-08-05 13:25:19 +02001004 imp_p->module->filepath, imp_p->module->name);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001005 mod = NULL;
1006 }
Radek Krejci19a96102018-11-15 13:38:09 +01001007 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001008 ly_in_free(in, 1);
Radek Krejci19a96102018-11-15 13:38:09 +01001009 }
1010 if (!mod) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001011 if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, (struct lys_module **)&mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001012 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Michal Vasko7c8439f2020-08-05 13:25:19 +02001013 imp_p->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001014 return LY_ENOTFOUND;
1015 }
1016 }
Radek Krejci19a96102018-11-15 13:38:09 +01001017 }
1018
Radek Krejci19a96102018-11-15 13:38:09 +01001019 return ret;
1020}
1021
Michal Vasko33ff9422020-07-03 09:50:39 +02001022LY_ERR
1023lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +02001024 struct lysp_ident *identities_p, struct lysc_ident **identities)
Radek Krejci19a96102018-11-15 13:38:09 +01001025{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001026 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
Michal Vasko33ff9422020-07-03 09:50:39 +02001027 struct lysc_ctx context = {0};
Radek Krejci19a96102018-11-15 13:38:09 +01001028 LY_ERR ret = LY_SUCCESS;
1029
Michal Vasko33ff9422020-07-03 09:50:39 +02001030 assert(ctx_sc || ctx);
Radek Krejci327de162019-06-14 12:52:07 +02001031
Michal Vasko33ff9422020-07-03 09:50:39 +02001032 if (!ctx_sc) {
1033 context.ctx = ctx;
1034 context.mod = module;
Radek Krejci120d8542020-08-12 09:29:16 +02001035 context.mod_def = module;
Michal Vasko33ff9422020-07-03 09:50:39 +02001036 context.path_len = 1;
1037 context.path[0] = '/';
1038 ctx_sc = &context;
1039 }
Radek Krejci19a96102018-11-15 13:38:09 +01001040
Michal Vasko33ff9422020-07-03 09:50:39 +02001041 if (!identities_p) {
1042 return LY_SUCCESS;
1043 }
1044 if (*identities) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001045 offset = LY_ARRAY_COUNT(*identities);
Michal Vasko33ff9422020-07-03 09:50:39 +02001046 }
1047
1048 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001049 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +02001050 LY_ARRAY_FOR(identities_p, u) {
1051 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
1052
1053 LY_ARRAY_INCREMENT(*identities);
1054 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001055 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done);
1056 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done);
1057 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref, ret, done);
Michal Vasko33ff9422020-07-03 09:50:39 +02001058 (*identities)[offset + u].module = ctx_sc->mod;
1059 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
1060 lys_compile_iffeature, ret, done);
1061 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
1062 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
1063 LYEXT_PAR_IDENT, ret, done);
1064 (*identities)[offset + u].flags = identities_p[u].flags;
1065
1066 lysc_update_path(ctx_sc, NULL, NULL);
1067 }
1068 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001069done:
1070 return ret;
1071}
1072
Radek Krejcib56c7502019-02-13 14:19:54 +01001073/**
1074 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1075 *
1076 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1077 *
1078 * @param[in] ctx Compile context for logging.
1079 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1080 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1081 * @return LY_SUCCESS if everything is ok.
1082 * @return LY_EVALID if the identity is derived from itself.
1083 */
Radek Krejci38222632019-02-12 16:55:05 +01001084static LY_ERR
1085lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1086{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001087 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001088 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci38222632019-02-12 16:55:05 +01001089 struct ly_set recursion = {0};
1090 struct lysc_ident *drv;
1091
1092 if (!derived) {
1093 return LY_SUCCESS;
1094 }
1095
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001096 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001097 if (ident == derived[u]) {
1098 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1099 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001100 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001101 goto cleanup;
1102 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001103 ret = ly_set_add(&recursion, derived[u], 0, NULL);
1104 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001105 }
1106
1107 for (v = 0; v < recursion.count; ++v) {
1108 drv = recursion.objs[v];
1109 if (!drv->derived) {
1110 continue;
1111 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001112 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
Radek Krejci38222632019-02-12 16:55:05 +01001113 if (ident == drv->derived[u]) {
1114 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1115 "Identity \"%s\" is indirectly derived from itself.", ident->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001116 ret = LY_EVALID;
Radek Krejci38222632019-02-12 16:55:05 +01001117 goto cleanup;
1118 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001119 ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
1120 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci38222632019-02-12 16:55:05 +01001121 }
1122 }
Radek Krejci38222632019-02-12 16:55:05 +01001123
1124cleanup:
1125 ly_set_erase(&recursion, NULL);
1126 return ret;
1127}
1128
Radek Krejcia3045382018-11-22 14:30:31 +01001129/**
1130 * @brief Find and process the referenced base identities from another identity or identityref
1131 *
Radek Krejciaca74032019-06-04 08:53:06 +02001132 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001133 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1134 * to distinguish these two use cases.
1135 *
1136 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1137 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
Michal Vasko33ff9422020-07-03 09:50:39 +02001138 * @param[in] ident Referencing identity to work with, NULL for identityref.
Radek Krejcia3045382018-11-22 14:30:31 +01001139 * @param[in] bases Array of bases of identityref to fill in.
1140 * @return LY_ERR value.
1141 */
Radek Krejci19a96102018-11-15 13:38:09 +01001142static LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001143lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001144 struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001145{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001146 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001147 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001148 struct lys_module *mod;
Radek Krejci80d281e2020-09-14 17:42:54 +02001149 struct lysc_ident **idref;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001150
1151 assert(ident || bases);
1152
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001153 if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1155 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1156 return LY_EVALID;
1157 }
1158
Michal Vasko33ff9422020-07-03 09:50:39 +02001159 LY_ARRAY_FOR(bases_p, u) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001160 s = strchr(bases_p[u], ':');
1161 if (s) {
1162 /* prefixed identity */
1163 name = &s[1];
Radek Krejci0a33b042020-05-27 10:05:06 +02001164 mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001165 } else {
1166 name = bases_p[u];
Radek Krejci0a33b042020-05-27 10:05:06 +02001167 mod = context_module;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001168 }
1169 if (!mod) {
1170 if (ident) {
1171 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1172 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1173 } else {
1174 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1175 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1176 }
1177 return LY_EVALID;
1178 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001179
Radek Krejci555cb5b2018-11-16 14:54:33 +01001180 idref = NULL;
Radek Krejci80d281e2020-09-14 17:42:54 +02001181 LY_ARRAY_FOR(mod->identities, v) {
1182 if (!strcmp(name, mod->identities[v].name)) {
Michal Vasko33ff9422020-07-03 09:50:39 +02001183 if (ident) {
Radek Krejci80d281e2020-09-14 17:42:54 +02001184 if (ident == &mod->identities[v]) {
Michal Vasko33ff9422020-07-03 09:50:39 +02001185 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1186 "Identity \"%s\" is derived from itself.", ident->name);
1187 return LY_EVALID;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001188 }
Radek Krejci80d281e2020-09-14 17:42:54 +02001189 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived));
Michal Vasko33ff9422020-07-03 09:50:39 +02001190 /* we have match! store the backlink */
Radek Krejci80d281e2020-09-14 17:42:54 +02001191 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
Michal Vasko33ff9422020-07-03 09:50:39 +02001192 *idref = ident;
1193 } else {
1194 /* we have match! store the found identity */
1195 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejci80d281e2020-09-14 17:42:54 +02001196 *idref = &mod->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001197 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001198 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001199 }
1200 }
1201 if (!idref || !(*idref)) {
1202 if (ident) {
1203 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1204 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1205 } else {
1206 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1207 "Unable to find base (%s) of identityref.", bases_p[u]);
1208 }
1209 return LY_EVALID;
1210 }
1211 }
1212 return LY_SUCCESS;
1213}
1214
Radek Krejcia3045382018-11-22 14:30:31 +01001215/**
1216 * @brief For the given array of identities, set the backlinks from all their base identities.
1217 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1218 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1219 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1220 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1221 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001222static LY_ERR
1223lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1224{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001225 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01001226
Michal Vasko33ff9422020-07-03 09:50:39 +02001227 lysc_update_path(ctx, NULL, "{identity}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001228 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001229 if (!idents_p[u].bases) {
Radek Krejci19a96102018-11-15 13:38:09 +01001230 continue;
1231 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001232 lysc_update_path(ctx, NULL, idents[u].name);
Radek Krejci0a33b042020-05-27 10:05:06 +02001233 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 +02001234 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001235 }
Michal Vasko33ff9422020-07-03 09:50:39 +02001236 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001237 return LY_SUCCESS;
1238}
1239
Radek Krejci0af46292019-01-11 16:02:31 +01001240LY_ERR
Michal Vasko33ff9422020-07-03 09:50:39 +02001241lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +02001242 struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001243{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001244 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001245 LY_ARRAY_COUNT_TYPE offset = 0, u;
Radek Krejci0af46292019-01-11 16:02:31 +01001246 struct lysc_ctx context = {0};
1247
Radek Krejci327de162019-06-14 12:52:07 +02001248 assert(ctx_sc || ctx);
1249
1250 if (!ctx_sc) {
1251 context.ctx = ctx;
1252 context.mod = module;
1253 context.path_len = 1;
1254 context.path[0] = '/';
1255 ctx_sc = &context;
1256 }
Radek Krejci0af46292019-01-11 16:02:31 +01001257
1258 if (!features_p) {
1259 return LY_SUCCESS;
1260 }
1261 if (*features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001262 offset = LY_ARRAY_COUNT(*features);
Radek Krejci0af46292019-01-11 16:02:31 +01001263 }
1264
Radek Krejci327de162019-06-14 12:52:07 +02001265 lysc_update_path(ctx_sc, NULL, "{feature}");
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001266 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001267 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001268 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1269
Radek Krejci0af46292019-01-11 16:02:31 +01001270 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001271 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001272 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done);
1273 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done);
1274 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001275 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001276 (*features)[offset + u].module = ctx_sc->mod;
1277
1278 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001279 }
Radek Krejci327de162019-06-14 12:52:07 +02001280 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001281
Radek Krejci011e4aa2020-09-04 15:22:31 +02001282done:
1283 return ret;
Radek Krejci0af46292019-01-11 16:02:31 +01001284}
1285
Radek Krejcia3045382018-11-22 14:30:31 +01001286/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001287 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001288 *
1289 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1290 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001291 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001292 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1293 * being currently processed).
1294 * @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 +01001295 * @return LY_SUCCESS if everything is ok.
1296 * @return LY_EVALID if the feature references indirectly itself.
1297 */
1298static LY_ERR
1299lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1300{
Radek Krejciba03a5a2020-08-27 14:40:41 +02001301 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001302 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001303 struct ly_set recursion = {0};
1304 struct lysc_feature *drv;
1305
1306 if (!depfeatures) {
1307 return LY_SUCCESS;
1308 }
1309
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001310 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001311 if (feature == depfeatures[u]) {
1312 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1313 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001314 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001315 goto cleanup;
1316 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001317 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
1318 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001319 }
1320
1321 for (v = 0; v < recursion.count; ++v) {
1322 drv = recursion.objs[v];
1323 if (!drv->depfeatures) {
1324 continue;
1325 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001326 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001327 if (feature == drv->depfeatures[u]) {
1328 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1329 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Radek Krejciba03a5a2020-08-27 14:40:41 +02001330 ret = LY_EVALID;
Radek Krejci09a1fc52019-02-13 10:55:17 +01001331 goto cleanup;
1332 }
Radek Krejciba03a5a2020-08-27 14:40:41 +02001333 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
1334 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci09a1fc52019-02-13 10:55:17 +01001335 }
1336 }
Radek Krejci09a1fc52019-02-13 10:55:17 +01001337
1338cleanup:
1339 ly_set_erase(&recursion, NULL);
1340 return ret;
1341}
1342
1343/**
Radek Krejci0af46292019-01-11 16:02:31 +01001344 * @brief Create pre-compiled features array.
1345 *
1346 * See lys_feature_precompile() for more details.
1347 *
Radek Krejcia3045382018-11-22 14:30:31 +01001348 * @param[in] ctx Compile context.
1349 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001350 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001351 * @return LY_ERR value.
1352 */
Radek Krejci19a96102018-11-15 13:38:09 +01001353static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001354lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001355{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001356 LY_ARRAY_COUNT_TYPE u, v, x;
Radek Krejci0af46292019-01-11 16:02:31 +01001357 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001358 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001359
Radek Krejci0af46292019-01-11 16:02:31 +01001360 /* find the preprecompiled feature */
1361 LY_ARRAY_FOR(features, x) {
1362 if (strcmp(features[x].name, feature_p->name)) {
1363 continue;
1364 }
1365 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001366 lysc_update_path(ctx, NULL, "{feature}");
1367 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001368
Radek Krejci0af46292019-01-11 16:02:31 +01001369 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001370 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001371 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001372 if (feature->iffeatures) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001373 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +01001374 if (feature->iffeatures[u].features) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001375 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001376 /* check for circular dependency - direct reference first,... */
1377 if (feature == feature->iffeatures[u].features[v]) {
1378 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1379 "Feature \"%s\" is referenced from itself.", feature->name);
1380 return LY_EVALID;
1381 }
1382 /* ... and indirect circular reference */
1383 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1384
Radek Krejci0af46292019-01-11 16:02:31 +01001385 /* add itself into the dependants list */
1386 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1387 *df = feature;
1388 }
Radek Krejci19a96102018-11-15 13:38:09 +01001389 }
Radek Krejci19a96102018-11-15 13:38:09 +01001390 }
1391 }
Radek Krejci327de162019-06-14 12:52:07 +02001392 lysc_update_path(ctx, NULL, NULL);
1393 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001394 done:
1395 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001396 }
Radek Krejci0af46292019-01-11 16:02:31 +01001397
1398 LOGINT(ctx->ctx);
1399 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001400}
1401
Radek Krejcib56c7502019-02-13 14:19:54 +01001402/**
1403 * @brief Revert compiled list of features back to the precompiled state.
1404 *
1405 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
Radek Krejcib56c7502019-02-13 14:19:54 +01001406 *
1407 * @param[in] ctx Compilation context.
Michal Vasko7f45cf22020-10-01 12:49:44 +02001408 * @param[in] mod The module structure with the features to decompile.
Radek Krejcib56c7502019-02-13 14:19:54 +01001409 */
Radek Krejci95710c92019-02-11 15:49:55 +01001410static void
1411lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1412{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001413 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci95710c92019-02-11 15:49:55 +01001414
Michal Vasko33ff9422020-07-03 09:50:39 +02001415 /* in the dis_features list, remove all the parts (from finished compiling process)
Radek Krejci95710c92019-02-11 15:49:55 +01001416 * which may points into the data being freed here */
Radek Krejci14915cc2020-09-14 17:28:13 +02001417 LY_ARRAY_FOR(mod->features, u) {
1418 LY_ARRAY_FOR(mod->features[u].iffeatures, v) {
1419 lysc_iffeature_free(ctx->ctx, &mod->features[u].iffeatures[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001420 }
Radek Krejci14915cc2020-09-14 17:28:13 +02001421 LY_ARRAY_FREE(mod->features[u].iffeatures);
1422 mod->features[u].iffeatures = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001423
Radek Krejci14915cc2020-09-14 17:28:13 +02001424 LY_ARRAY_FOR(mod->features[u].exts, v) {
1425 lysc_ext_instance_free(ctx->ctx, &(mod->features[u].exts)[v]);
Radek Krejci95710c92019-02-11 15:49:55 +01001426 }
Radek Krejci14915cc2020-09-14 17:28:13 +02001427 LY_ARRAY_FREE(mod->features[u].exts);
1428 mod->features[u].exts = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01001429 }
1430}
1431
Radek Krejcia3045382018-11-22 14:30:31 +01001432/**
1433 * @brief Validate and normalize numeric value from a range definition.
1434 * @param[in] ctx Compile context.
1435 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1436 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1437 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1438 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1439 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1440 * @param[in] value String value of the range boundary.
1441 * @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.
1442 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1443 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1444 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001445LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001446range_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 +01001447{
Radek Krejci6cba4292018-11-15 17:33:29 +01001448 size_t fraction = 0, size;
1449
Radek Krejci19a96102018-11-15 13:38:09 +01001450 *len = 0;
1451
1452 assert(value);
1453 /* parse value */
1454 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1455 return LY_EVALID;
1456 }
1457
1458 if ((value[*len] == '-') || (value[*len] == '+')) {
1459 ++(*len);
1460 }
1461
1462 while (isdigit(value[*len])) {
1463 ++(*len);
1464 }
1465
1466 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001467 if (basetype == LY_TYPE_DEC64) {
1468 goto decimal;
1469 } else {
1470 *valcopy = strndup(value, *len);
1471 return LY_SUCCESS;
1472 }
Radek Krejci19a96102018-11-15 13:38:09 +01001473 }
1474 fraction = *len;
1475
1476 ++(*len);
1477 while (isdigit(value[*len])) {
1478 ++(*len);
1479 }
1480
Radek Krejci6cba4292018-11-15 17:33:29 +01001481 if (basetype == LY_TYPE_DEC64) {
1482decimal:
1483 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001484 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001485 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1486 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1487 *len, value, frdigits);
1488 return LY_EINVAL;
1489 }
1490 if (fraction) {
1491 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1492 } else {
1493 size = (*len) + frdigits + 1;
1494 }
1495 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001496 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1497
Radek Krejci6cba4292018-11-15 17:33:29 +01001498 (*valcopy)[size - 1] = '\0';
1499 if (fraction) {
1500 memcpy(&(*valcopy)[0], &value[0], fraction);
1501 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1502 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1503 } else {
1504 memcpy(&(*valcopy)[0], &value[0], *len);
1505 memset(&(*valcopy)[*len], '0', frdigits);
1506 }
Radek Krejci19a96102018-11-15 13:38:09 +01001507 }
1508 return LY_SUCCESS;
1509}
1510
Radek Krejcia3045382018-11-22 14:30:31 +01001511/**
1512 * @brief Check that values in range are in ascendant order.
1513 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001514 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1515 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001516 * @param[in] value Current value to check.
1517 * @param[in] prev_value The last seen value.
1518 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1519 */
Radek Krejci19a96102018-11-15 13:38:09 +01001520static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001521range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001522{
1523 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001524 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001525 return LY_EEXIST;
1526 }
1527 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001528 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001529 return LY_EEXIST;
1530 }
1531 }
1532 return LY_SUCCESS;
1533}
1534
Radek Krejcia3045382018-11-22 14:30:31 +01001535/**
1536 * @brief Set min/max value of the range part.
1537 * @param[in] ctx Compile context.
1538 * @param[in] part Range part structure to fill.
1539 * @param[in] max Flag to distinguish if storing min or max value.
1540 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1541 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1542 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1543 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1544 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001545 * @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 +01001546 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1547 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1548 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1549 * frdigits value), LY_EMEM.
1550 */
Radek Krejci19a96102018-11-15 13:38:09 +01001551static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001552range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
1553 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001554{
1555 LY_ERR ret = LY_SUCCESS;
1556 char *valcopy = NULL;
1557 size_t len;
1558
1559 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001560 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001561 LY_CHECK_GOTO(ret, finalize);
1562 }
1563 if (!valcopy && base_range) {
1564 if (max) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001565 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
Radek Krejci5969f272018-11-23 10:03:58 +01001566 } else {
1567 part->min_64 = base_range->parts[0].min_64;
1568 }
1569 if (!first) {
1570 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1571 }
1572 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001573 }
1574
1575 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001576 case LY_TYPE_INT8: /* range */
1577 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001578 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 +01001579 } else if (max) {
1580 part->max_64 = INT64_C(127);
1581 } else {
1582 part->min_64 = INT64_C(-128);
1583 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001584 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001585 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001586 }
1587 break;
1588 case LY_TYPE_INT16: /* range */
1589 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001590 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 +01001591 } else if (max) {
1592 part->max_64 = INT64_C(32767);
1593 } else {
1594 part->min_64 = INT64_C(-32768);
1595 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001596 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001597 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001598 }
1599 break;
1600 case LY_TYPE_INT32: /* range */
1601 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001602 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 +01001603 } else if (max) {
1604 part->max_64 = INT64_C(2147483647);
1605 } else {
1606 part->min_64 = INT64_C(-2147483648);
1607 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001608 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001609 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001610 }
1611 break;
1612 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001613 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001614 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001615 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001616 max ? &part->max_64 : &part->min_64);
1617 } else if (max) {
1618 part->max_64 = INT64_C(9223372036854775807);
1619 } else {
1620 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1621 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001622 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001623 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001624 }
1625 break;
1626 case LY_TYPE_UINT8: /* range */
1627 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001628 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 +01001629 } else if (max) {
1630 part->max_u64 = UINT64_C(255);
1631 } else {
1632 part->min_u64 = UINT64_C(0);
1633 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001634 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001635 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001636 }
1637 break;
1638 case LY_TYPE_UINT16: /* range */
1639 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001640 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 +01001641 } else if (max) {
1642 part->max_u64 = UINT64_C(65535);
1643 } else {
1644 part->min_u64 = UINT64_C(0);
1645 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001646 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001647 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001648 }
1649 break;
1650 case LY_TYPE_UINT32: /* range */
1651 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001652 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 +01001653 } else if (max) {
1654 part->max_u64 = UINT64_C(4294967295);
1655 } else {
1656 part->min_u64 = UINT64_C(0);
1657 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001658 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001659 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001660 }
1661 break;
1662 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001663 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001664 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001665 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001666 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 +01001667 } else if (max) {
1668 part->max_u64 = UINT64_C(18446744073709551615);
1669 } else {
1670 part->min_u64 = UINT64_C(0);
1671 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001672 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001673 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001674 }
1675 break;
1676 default:
1677 LOGINT(ctx->ctx);
1678 ret = LY_EINT;
1679 }
1680
Radek Krejci5969f272018-11-23 10:03:58 +01001681finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001682 if (ret == LY_EDENIED) {
1683 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1684 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1685 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1686 } else if (ret == LY_EVALID) {
1687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1688 "Invalid %s restriction - invalid value \"%s\".",
1689 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1690 } else if (ret == LY_EEXIST) {
1691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1692 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001693 length_restr ? "length" : "range",
Radek Krejci0f969882020-08-21 16:56:47 +02001694 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001695 } else if (!ret && value) {
1696 *value = *value + len;
1697 }
1698 free(valcopy);
1699 return ret;
1700}
1701
Radek Krejcia3045382018-11-22 14:30:31 +01001702/**
1703 * @brief Compile the parsed range restriction.
1704 * @param[in] ctx Compile context.
1705 * @param[in] range_p Parsed range structure to compile.
1706 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1707 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1708 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1709 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1710 * range restriction must be more restrictive than the base_range.
1711 * @param[in,out] range Pointer to the created current range structure.
1712 * @return LY_ERR value.
1713 */
Radek Krejci19a96102018-11-15 13:38:09 +01001714static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001715lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
Radek Krejci0f969882020-08-21 16:56:47 +02001716 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
Radek Krejci19a96102018-11-15 13:38:09 +01001717{
1718 LY_ERR ret = LY_EVALID;
1719 const char *expr;
1720 struct lysc_range_part *parts = NULL, *part;
Radek Krejci857189e2020-09-01 13:26:36 +02001721 ly_bool range_expected = 0, uns;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001722 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001723
1724 assert(range);
1725 assert(range_p);
1726
Michal Vasko7f45cf22020-10-01 12:49:44 +02001727 expr = range_p->arg.str;
Michal Vaskod989ba02020-08-24 10:59:24 +02001728 while (1) {
Radek Krejci19a96102018-11-15 13:38:09 +01001729 if (isspace(*expr)) {
1730 ++expr;
1731 } else if (*expr == '\0') {
1732 if (range_expected) {
1733 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1734 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1735 length_restr ? "length" : "range", range_p->arg);
1736 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001737 } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001738 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1739 "Invalid %s restriction - unexpected end of the expression (%s).",
1740 length_restr ? "length" : "range", range_p->arg);
1741 goto cleanup;
1742 }
1743 parts_done++;
1744 break;
1745 } else if (!strncmp(expr, "min", 3)) {
1746 if (parts) {
1747 /* min cannot be used elsewhere than in the first part */
1748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1749 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Michal Vasko7f45cf22020-10-01 12:49:44 +02001750 expr - range_p->arg.str, range_p->arg.str);
Radek Krejci19a96102018-11-15 13:38:09 +01001751 goto cleanup;
1752 }
1753 expr += 3;
1754
1755 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001756 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 +01001757 part->max_64 = part->min_64;
1758 } else if (*expr == '|') {
1759 if (!parts || range_expected) {
1760 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1761 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1762 goto cleanup;
1763 }
1764 expr++;
1765 parts_done++;
1766 /* process next part of the expression */
1767 } else if (!strncmp(expr, "..", 2)) {
1768 expr += 2;
1769 while (isspace(*expr)) {
1770 expr++;
1771 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001772 if (!parts || LY_ARRAY_COUNT(parts) == parts_done) {
Radek Krejci19a96102018-11-15 13:38:09 +01001773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1774 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1775 goto cleanup;
1776 }
1777 /* continue expecting the upper boundary */
1778 range_expected = 1;
1779 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1780 /* number */
1781 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001782 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001783 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 +01001784 range_expected = 0;
1785 } else {
1786 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001787 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001788 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001789 part->max_64 = part->min_64;
1790 }
1791
1792 /* continue with possible another expression part */
1793 } else if (!strncmp(expr, "max", 3)) {
1794 expr += 3;
1795 while (isspace(*expr)) {
1796 expr++;
1797 }
1798 if (*expr != '\0') {
1799 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1800 length_restr ? "length" : "range", expr);
1801 goto cleanup;
1802 }
1803 if (range_expected) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001804 part = &parts[LY_ARRAY_COUNT(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001805 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 +01001806 range_expected = 0;
1807 } else {
1808 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001809 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001810 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001811 part->min_64 = part->max_64;
1812 }
1813 } else {
1814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1815 length_restr ? "length" : "range", expr);
1816 goto cleanup;
1817 }
1818 }
1819
1820 /* check with the previous range/length restriction */
1821 if (base_range) {
1822 switch (basetype) {
1823 case LY_TYPE_BINARY:
1824 case LY_TYPE_UINT8:
1825 case LY_TYPE_UINT16:
1826 case LY_TYPE_UINT32:
1827 case LY_TYPE_UINT64:
1828 case LY_TYPE_STRING:
1829 uns = 1;
1830 break;
1831 case LY_TYPE_DEC64:
1832 case LY_TYPE_INT8:
1833 case LY_TYPE_INT16:
1834 case LY_TYPE_INT32:
1835 case LY_TYPE_INT64:
1836 uns = 0;
1837 break;
1838 default:
1839 LOGINT(ctx->ctx);
1840 ret = LY_EINT;
1841 goto cleanup;
1842 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001843 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
Radek Krejci19a96102018-11-15 13:38:09 +01001844 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1845 goto baseerror;
1846 }
1847 /* current lower bound is not lower than the base */
1848 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1849 /* base has single value */
1850 if (base_range->parts[v].min_64 == parts[u].min_64) {
1851 /* both lower bounds are the same */
1852 if (parts[u].min_64 != parts[u].max_64) {
1853 /* current continues with a range */
1854 goto baseerror;
1855 } else {
1856 /* equal single values, move both forward */
1857 ++v;
1858 continue;
1859 }
1860 } else {
1861 /* base is single value lower than current range, so the
1862 * value from base range is removed in the current,
1863 * move only base and repeat checking */
1864 ++v;
1865 --u;
1866 continue;
1867 }
1868 } else {
1869 /* base is the range */
1870 if (parts[u].min_64 == parts[u].max_64) {
1871 /* current is a single value */
1872 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1873 /* current is behind the base range, so base range is omitted,
1874 * move the base and keep the current for further check */
1875 ++v;
1876 --u;
1877 } /* else it is within the base range, so move the current, but keep the base */
1878 continue;
1879 } else {
1880 /* both are ranges - check the higher bound, the lower was already checked */
1881 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1882 /* higher bound is higher than the current higher bound */
1883 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1884 /* but the current lower bound is also higher, so the base range is omitted,
1885 * continue with the same current, but move the base */
1886 --u;
1887 ++v;
1888 continue;
1889 }
1890 /* current range starts within the base range but end behind it */
1891 goto baseerror;
1892 } else {
1893 /* current range is smaller than the base,
1894 * move current, but stay with the base */
1895 continue;
1896 }
1897 }
1898 }
1899 }
1900 if (u != parts_done) {
1901baseerror:
1902 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1903 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1904 length_restr ? "length" : "range", range_p->arg);
1905 goto cleanup;
1906 }
1907 }
1908
1909 if (!(*range)) {
1910 *range = calloc(1, sizeof **range);
1911 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1912 }
1913
Radek Krejcic8b31002019-01-08 10:24:45 +01001914 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001915 if (range_p->eapptag) {
1916 lydict_remove(ctx->ctx, (*range)->eapptag);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001917 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001918 }
1919 if (range_p->emsg) {
1920 lydict_remove(ctx->ctx, (*range)->emsg);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001921 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001922 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001923 if (range_p->dsc) {
1924 lydict_remove(ctx->ctx, (*range)->dsc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001925 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
Radek Krejcic8b31002019-01-08 10:24:45 +01001926 }
1927 if (range_p->ref) {
1928 lydict_remove(ctx->ctx, (*range)->ref);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001929 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
Radek Krejcic8b31002019-01-08 10:24:45 +01001930 }
Radek Krejci19a96102018-11-15 13:38:09 +01001931 /* extensions are taken only from the last range by the caller */
1932
1933 (*range)->parts = parts;
1934 parts = NULL;
1935 ret = LY_SUCCESS;
1936cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001937 LY_ARRAY_FREE(parts);
1938
1939 return ret;
1940}
1941
1942/**
1943 * @brief Checks pattern syntax.
1944 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001945 * @param[in] ctx Context.
1946 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001947 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001948 * @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 +01001949 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001950 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001951LY_ERR
1952lys_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 +01001953{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001954 size_t idx, idx2, start, end, size, brack;
Radek Krejci19a96102018-11-15 13:38:09 +01001955 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001956 int err_code;
1957 const char *orig_ptr;
1958 PCRE2_SIZE err_offset;
1959 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001960#define URANGE_LEN 19
1961 char *ublock2urange[][2] = {
1962 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1963 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1964 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1965 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1966 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1967 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1968 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1969 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1970 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1971 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1972 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1973 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1974 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1975 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1976 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1977 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1978 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1979 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1980 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1981 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1982 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1983 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1984 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1985 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1986 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1987 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1988 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1989 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1990 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1991 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1992 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1993 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1994 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1995 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1996 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1997 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1998 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1999 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
2000 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
2001 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
2002 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
2003 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
2004 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
2005 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
2006 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
2007 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
2008 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
2009 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
2010 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
2011 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
2012 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
2013 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
2014 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
2015 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
2016 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
2017 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
2018 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
2019 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
2020 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
2021 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
2022 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
2023 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
2024 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
2025 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
2026 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
2027 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
2028 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
2029 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
2030 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
2031 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
2032 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
2033 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
2034 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
2035 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
2036 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
2037 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
2038 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
2039 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
2040 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
2041 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
2042 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
2043 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
2044 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
2045 {NULL, NULL}
2046 };
2047
2048 /* adjust the expression to a Perl equivalent
2049 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
2050
Michal Vasko40a00082020-05-27 15:20:01 +02002051 /* allocate space for the transformed pattern */
2052 size = strlen(pattern) + 1;
2053 perl_regex = malloc(size);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002054 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002055 perl_regex[0] = '\0';
2056
Michal Vasko40a00082020-05-27 15:20:01 +02002057 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
2058 brack = 0;
2059 idx = 0;
2060 orig_ptr = pattern;
2061 while (orig_ptr[0]) {
2062 switch (orig_ptr[0]) {
2063 case '$':
2064 case '^':
2065 if (!brack) {
2066 /* make space for the extra character */
2067 ++size;
2068 perl_regex = ly_realloc(perl_regex, size);
2069 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002070
Michal Vasko40a00082020-05-27 15:20:01 +02002071 /* print escape slash */
2072 perl_regex[idx] = '\\';
2073 ++idx;
2074 }
2075 break;
2076 case '[':
2077 /* must not be escaped */
2078 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2079 ++brack;
2080 }
2081 break;
2082 case ']':
2083 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
2084 /* pattern was checked and compiled already */
2085 assert(brack);
2086 --brack;
2087 }
2088 break;
2089 default:
2090 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002091 }
Michal Vasko40a00082020-05-27 15:20:01 +02002092
2093 /* copy char */
2094 perl_regex[idx] = orig_ptr[0];
2095
2096 ++idx;
2097 ++orig_ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002098 }
Michal Vasko40a00082020-05-27 15:20:01 +02002099 perl_regex[idx] = '\0';
Radek Krejci19a96102018-11-15 13:38:09 +01002100
2101 /* substitute Unicode Character Blocks with exact Character Ranges */
2102 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2103 start = ptr - perl_regex;
2104
2105 ptr = strchr(ptr, '}');
2106 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002108 pattern, perl_regex + start + 2, "unterminated character property");
2109 free(perl_regex);
2110 return LY_EVALID;
2111 }
2112 end = (ptr - perl_regex) + 1;
2113
2114 /* need more space */
2115 if (end - start < URANGE_LEN) {
2116 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002118 }
2119
2120 /* find our range */
2121 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2122 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2123 break;
2124 }
2125 }
2126 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002127 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002128 pattern, perl_regex + start + 5, "unknown block name");
2129 free(perl_regex);
2130 return LY_EVALID;
2131 }
2132
2133 /* 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 +02002134 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002135 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002136 ++idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002137 }
2138 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
Michal Vasko40a00082020-05-27 15:20:01 +02002139 --idx;
Radek Krejci19a96102018-11-15 13:38:09 +01002140 }
2141 }
Michal Vasko40a00082020-05-27 15:20:01 +02002142 if (idx) {
Radek Krejci19a96102018-11-15 13:38:09 +01002143 /* skip brackets */
2144 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2145 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2146 } else {
2147 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2148 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2149 }
2150 }
2151
2152 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002153 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2154 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002155 &err_code, &err_offset, NULL);
2156 if (!code_local) {
2157 PCRE2_UCHAR err_msg[256] = {0};
2158 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002160 free(perl_regex);
2161 return LY_EVALID;
2162 }
2163 free(perl_regex);
2164
Radek Krejci54579462019-04-30 12:47:06 +02002165 if (code) {
2166 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002167 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002168 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002169 }
2170
2171 return LY_SUCCESS;
2172
2173#undef URANGE_LEN
2174}
2175
Radek Krejcia3045382018-11-22 14:30:31 +01002176/**
2177 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2178 * @param[in] ctx Compile context.
2179 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002180 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2181 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2182 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2183 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2184 */
Radek Krejci19a96102018-11-15 13:38:09 +01002185static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002186lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci0f969882020-08-21 16:56:47 +02002187 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
Radek Krejci19a96102018-11-15 13:38:09 +01002188{
2189 struct lysc_pattern **pattern;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002190 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01002191 LY_ERR ret = LY_SUCCESS;
2192
2193 /* first, copy the patterns from the base type */
2194 if (base_patterns) {
2195 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2196 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2197 }
2198
2199 LY_ARRAY_FOR(patterns_p, u) {
2200 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2201 *pattern = calloc(1, sizeof **pattern);
2202 ++(*pattern)->refcount;
2203
Michal Vasko7f45cf22020-10-01 12:49:44 +02002204 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg.str[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002205 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002206
Michal Vasko7f45cf22020-10-01 12:49:44 +02002207 if (patterns_p[u].arg.str[0] == 0x15) {
Radek Krejci19a96102018-11-15 13:38:09 +01002208 (*pattern)->inverted = 1;
2209 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002210 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002211 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
2212 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
2213 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
2214 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002215 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002216 }
2217done:
2218 return ret;
2219}
2220
Radek Krejcia3045382018-11-22 14:30:31 +01002221/**
2222 * @brief map of the possible restrictions combination for the specific built-in type.
2223 */
Radek Krejci19a96102018-11-15 13:38:09 +01002224static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2225 0 /* LY_TYPE_UNKNOWN */,
2226 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002227 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2228 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2229 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2230 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2231 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002232 LYS_SET_BIT /* LY_TYPE_BITS */,
2233 0 /* LY_TYPE_BOOL */,
2234 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2235 0 /* LY_TYPE_EMPTY */,
2236 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2237 LYS_SET_BASE /* LY_TYPE_IDENT */,
2238 LYS_SET_REQINST /* LY_TYPE_INST */,
2239 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002240 LYS_SET_TYPE /* LY_TYPE_UNION */,
2241 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002242 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002243 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002244 LYS_SET_RANGE /* LY_TYPE_INT64 */
2245};
2246
2247/**
2248 * @brief stringification of the YANG built-in data types
2249 */
Michal Vasko22df3f02020-08-24 13:29:22 +02002250const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
Radek Krejci5969f272018-11-23 10:03:58 +01002251 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
Radek Krejci0f969882020-08-21 16:56:47 +02002252 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"};
Radek Krejci19a96102018-11-15 13:38:09 +01002253
Radek Krejcia3045382018-11-22 14:30:31 +01002254/**
2255 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2256 * @param[in] ctx Compile context.
2257 * @param[in] enums_p Array of the parsed enum structures to compile.
2258 * @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 +01002259 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2260 * @param[out] enums Newly created array of the compiled enums information for the current type.
2261 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2262 */
Radek Krejci19a96102018-11-15 13:38:09 +01002263static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002264lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci0f969882020-08-21 16:56:47 +02002265 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002266{
2267 LY_ERR ret = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002268 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002269 int32_t value = 0;
2270 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002271 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002272
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002273 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002274 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2275 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2276 return LY_EVALID;
2277 }
2278
2279 LY_ARRAY_FOR(enums_p, u) {
2280 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002281 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
2282 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->dsc, ret, done);
2283 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Radek Krejci693262f2019-04-29 15:23:20 +02002284 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002285 if (base_enums) {
2286 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2287 LY_ARRAY_FOR(base_enums, v) {
2288 if (!strcmp(e->name, base_enums[v].name)) {
2289 break;
2290 }
2291 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002292 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002293 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2294 "Invalid %s - derived type adds new item \"%s\".",
2295 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2296 return LY_EVALID;
2297 }
2298 match = v;
2299 }
2300
2301 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002302 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002303 if (enums_p[u].flags & LYS_SET_VALUE) {
2304 e->value = (int32_t)enums_p[u].value;
2305 if (!u || e->value >= value) {
2306 value = e->value + 1;
2307 }
2308 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002309 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002310 if (e->value == (*enums)[v].value) {
2311 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2312 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2313 e->value, e->name, (*enums)[v].name);
2314 return LY_EVALID;
2315 }
2316 }
2317 } else if (base_enums) {
2318 /* inherit the assigned value */
2319 e->value = base_enums[match].value;
2320 if (!u || e->value >= value) {
2321 value = e->value + 1;
2322 }
2323 } else {
2324 /* assign value automatically */
2325 if (u && value == INT32_MIN) {
2326 /* counter overflow */
2327 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2328 "Invalid enumeration - it is not possible to auto-assign enum value for "
2329 "\"%s\" since the highest value is already 2147483647.", e->name);
2330 return LY_EVALID;
2331 }
2332 e->value = value++;
2333 }
2334 } else { /* LY_TYPE_BITS */
2335 if (enums_p[u].flags & LYS_SET_VALUE) {
2336 e->value = (int32_t)enums_p[u].value;
2337 if (!u || (uint32_t)e->value >= position) {
2338 position = (uint32_t)e->value + 1;
2339 }
2340 /* check collision with other values */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002341 for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) {
Radek Krejci19a96102018-11-15 13:38:09 +01002342 if (e->value == (*enums)[v].value) {
2343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2344 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek Krejci0f969882020-08-21 16:56:47 +02002345 (uint32_t)e->value, e->name, (*enums)[v].name);
Radek Krejci19a96102018-11-15 13:38:09 +01002346 return LY_EVALID;
2347 }
2348 }
2349 } else if (base_enums) {
2350 /* inherit the assigned value */
2351 e->value = base_enums[match].value;
2352 if (!u || (uint32_t)e->value >= position) {
2353 position = (uint32_t)e->value + 1;
2354 }
2355 } else {
2356 /* assign value automatically */
2357 if (u && position == 0) {
2358 /* counter overflow */
2359 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2360 "Invalid bits - it is not possible to auto-assign bit position for "
2361 "\"%s\" since the highest value is already 4294967295.", e->name);
2362 return LY_EVALID;
2363 }
2364 e->value = position++;
2365 }
2366 }
2367
2368 if (base_enums) {
2369 /* the assigned values must not change from the derived type */
2370 if (e->value != base_enums[match].value) {
2371 if (basetype == LY_TYPE_ENUM) {
2372 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2373 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2374 e->name, base_enums[match].value, e->value);
2375 } else {
2376 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2377 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2378 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2379 }
2380 return LY_EVALID;
2381 }
2382 }
2383
Radek Krejciec4da802019-05-02 13:02:41 +02002384 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002385 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 +01002386
2387 if (basetype == LY_TYPE_BITS) {
2388 /* keep bits ordered by position */
Radek Krejci1e008d22020-08-17 11:37:37 +02002389 for (v = u; v && (*enums)[v - 1].value > e->value; --v) {}
Radek Krejci19a96102018-11-15 13:38:09 +01002390 if (v != u) {
2391 memcpy(&storage, e, sizeof *e);
2392 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2393 memcpy(&(*enums)[v], &storage, sizeof storage);
2394 }
2395 }
2396 }
2397
2398done:
2399 return ret;
2400}
2401
Radek Krejcia3045382018-11-22 14:30:31 +01002402/**
2403 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2404 *
2405 * path-arg = absolute-path / relative-path
2406 * absolute-path = 1*("/" (node-identifier *path-predicate))
2407 * relative-path = 1*(".." "/") descendant-path
2408 *
2409 * @param[in,out] path Path to parse.
2410 * @param[out] prefix Prefix of the token, NULL if there is not any.
2411 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2412 * @param[out] name Name of the token.
2413 * @param[out] nam_len Length of the name.
2414 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2415 * must not be changed between consecutive calls. -1 if the
2416 * path is absolute.
2417 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2418 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2419 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002420LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002421lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
Radek Krejci857189e2020-09-01 13:26:36 +02002422 int32_t *parent_times, ly_bool *has_predicate)
Radek Krejcia3045382018-11-22 14:30:31 +01002423{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002424 int32_t par_times = 0;
Radek Krejcia3045382018-11-22 14:30:31 +01002425
2426 assert(path && *path);
2427 assert(parent_times);
2428 assert(prefix);
2429 assert(prefix_len);
2430 assert(name);
2431 assert(name_len);
2432 assert(has_predicate);
2433
2434 *prefix = NULL;
2435 *prefix_len = 0;
2436 *name = NULL;
2437 *name_len = 0;
2438 *has_predicate = 0;
2439
2440 if (!*parent_times) {
2441 if (!strncmp(*path, "..", 2)) {
2442 *path += 2;
2443 ++par_times;
2444 while (!strncmp(*path, "/..", 3)) {
2445 *path += 3;
2446 ++par_times;
2447 }
2448 }
2449 if (par_times) {
2450 *parent_times = par_times;
2451 } else {
2452 *parent_times = -1;
2453 }
2454 }
2455
2456 if (**path != '/') {
2457 return LY_EINVAL;
2458 }
2459 /* skip '/' */
2460 ++(*path);
2461
2462 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002463 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002464
2465 if ((**path == '/' && (*path)[1]) || !**path) {
2466 /* path continues by another token or this is the last token */
2467 return LY_SUCCESS;
2468 } else if ((*path)[0] != '[') {
2469 /* unexpected character */
2470 return LY_EINVAL;
2471 } else {
2472 /* predicate starting with [ */
2473 *has_predicate = 1;
2474 return LY_SUCCESS;
2475 }
2476}
2477
2478/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002479 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2480 *
2481 * The set of features used for target must be a subset of features used for the leafref.
2482 * This is not a perfect, we should compare the truth tables but it could require too much resources
2483 * and RFC 7950 does not require it explicitely, so we simplify that.
2484 *
2485 * @param[in] refnode The leafref node.
2486 * @param[in] target Tha target node of the leafref.
2487 * @return LY_SUCCESS or LY_EVALID;
2488 */
2489static LY_ERR
2490lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2491{
2492 LY_ERR ret = LY_EVALID;
2493 const struct lysc_node *iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002494 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci58d171e2018-11-23 13:50:55 +01002495 struct ly_set features = {0};
2496
2497 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002498 if (iter->iffeatures) {
2499 LY_ARRAY_FOR(iter->iffeatures, u) {
2500 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002501 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0, NULL), cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002502 }
2503 }
2504 }
2505 }
2506
2507 /* we should have, in features set, a superset of features applicable to the target node.
Radek Krejciba03a5a2020-08-27 14:40:41 +02002508 * If the feature is not present, we don;t have a subset of features applicable
Radek Krejci58d171e2018-11-23 13:50:55 +01002509 * to the leafref itself. */
Radek Krejci58d171e2018-11-23 13:50:55 +01002510 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002511 if (iter->iffeatures) {
2512 LY_ARRAY_FOR(iter->iffeatures, u) {
2513 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02002514 if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) {
2515 /* feature not present */
Radek Krejci58d171e2018-11-23 13:50:55 +01002516 goto cleanup;
2517 }
2518 }
2519 }
2520 }
2521 }
2522 ret = LY_SUCCESS;
2523
2524cleanup:
2525 ly_set_erase(&features, NULL);
2526 return ret;
2527}
2528
Michal Vasko7f45cf22020-10-01 12:49:44 +02002529static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002530 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
Michal Vasko7f45cf22020-10-01 12:49:44 +02002531 struct lysc_type **type, const char **units, struct lysp_qname **dflt);
Radek Krejcia3045382018-11-22 14:30:31 +01002532
Radek Krejcia3045382018-11-22 14:30:31 +01002533/**
2534 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2535 * @param[in] ctx Compile context.
Michal Vasko7f45cf22020-10-01 12:49:44 +02002536 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002537 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2538 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2539 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002540 * @param[in] type_p Parsed type to compile.
Radek Krejci0a33b042020-05-27 10:05:06 +02002541 * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes)
Radek Krejcia3045382018-11-22 14:30:31 +01002542 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002543 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2544 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2545 * @param[out] type Newly created type structure with the filled information about the type.
2546 * @return LY_ERR value.
2547 */
Radek Krejci19a96102018-11-15 13:38:09 +01002548static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02002549lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags,
Michal Vasko1734be92020-09-22 08:55:10 +02002550 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, struct lys_module *module,
2551 LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002552{
2553 LY_ERR ret = LY_SUCCESS;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002554 struct lysc_type_bin *bin;
2555 struct lysc_type_num *num;
2556 struct lysc_type_str *str;
2557 struct lysc_type_bits *bits;
2558 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002559 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002560 struct lysc_type_identityref *idref;
Michal Vasko004d3152020-06-11 19:59:22 +02002561 struct lysc_type_leafref *lref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002562 struct lysc_type_union *un, *un_aux;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002563
2564 switch (basetype) {
2565 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +02002566 bin = (struct lysc_type_bin *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002567
2568 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002569 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002570 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002571 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002572 if (!tpdfname) {
Michal Vasko1734be92020-09-22 08:55:10 +02002573 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, cleanup);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002574 }
2575 }
Radek Krejcic5c27e52018-11-15 14:38:11 +01002576 break;
2577 case LY_TYPE_BITS:
2578 /* RFC 7950 9.7 - bits */
Michal Vasko22df3f02020-08-24 13:29:22 +02002579 bits = (struct lysc_type_bits *)(*type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002580 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002581 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002582 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
2583 (struct lysc_type_bitenum_item **)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002584 }
2585
Radek Krejci555cb5b2018-11-16 14:54:33 +01002586 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002587 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002588 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002589 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002590 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejcic5c27e52018-11-15 14:38:11 +01002592 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002593 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002594 }
Radek Krejcic5c27e52018-11-15 14:38:11 +01002595 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002596 case LY_TYPE_DEC64:
Radek Krejci115a74d2020-08-14 22:18:12 +02002597 dec = (struct lysc_type_dec *)(*type);
Radek Krejci6cba4292018-11-15 17:33:29 +01002598
2599 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002600 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002601 if (!type_p->fraction_digits) {
2602 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002603 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002604 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002606 }
2607 return LY_EVALID;
2608 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002609 dec->fraction_digits = type_p->fraction_digits;
2610 } else {
2611 if (type_p->fraction_digits) {
2612 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
2613 if (tpdfname) {
2614 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2615 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
2616 tpdfname);
2617 } else {
2618 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2619 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci115a74d2020-08-14 22:18:12 +02002620 }
2621 return LY_EVALID;
Radek Krejci6cba4292018-11-15 17:33:29 +01002622 }
Radek Krejci115a74d2020-08-14 22:18:12 +02002623 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
Radek Krejci6cba4292018-11-15 17:33:29 +01002624 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002625
2626 /* RFC 7950 9.2.4 - range */
2627 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002628 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
Radek Krejci115a74d2020-08-14 22:18:12 +02002629 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002630 if (!tpdfname) {
Michal Vasko1734be92020-09-22 08:55:10 +02002631 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, cleanup);
Radek Krejci6cba4292018-11-15 17:33:29 +01002632 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002633 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002634 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002635 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +02002636 str = (struct lysc_type_str *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002637
2638 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002639 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002640 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002641 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002642 if (!tpdfname) {
Michal Vasko1734be92020-09-22 08:55:10 +02002643 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, cleanup);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002644 }
Michal Vasko22df3f02020-08-24 13:29:22 +02002645 } else if (base && ((struct lysc_type_str *)base)->length) {
2646 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002647 }
2648
2649 /* RFC 7950 9.4.5 - pattern */
2650 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002651 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Michal Vasko22df3f02020-08-24 13:29:22 +02002652 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
2653 } else if (base && ((struct lysc_type_str *)base)->patterns) {
2654 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002655 }
Radek Krejcic5c27e52018-11-15 14:38:11 +01002656 break;
2657 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +02002658 enumeration = (struct lysc_type_enum *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002659
2660 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002661 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002662 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Michal Vasko22df3f02020-08-24 13:29:22 +02002663 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002664 }
2665
Radek Krejci555cb5b2018-11-16 14:54:33 +01002666 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002667 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002668 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002670 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejcic5c27e52018-11-15 14:38:11 +01002672 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002673 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002674 }
Radek Krejcic5c27e52018-11-15 14:38:11 +01002675 break;
2676 case LY_TYPE_INT8:
2677 case LY_TYPE_UINT8:
2678 case LY_TYPE_INT16:
2679 case LY_TYPE_UINT16:
2680 case LY_TYPE_INT32:
2681 case LY_TYPE_UINT32:
2682 case LY_TYPE_INT64:
2683 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +02002684 num = (struct lysc_type_num *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002685
2686 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002687 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002688 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Michal Vasko22df3f02020-08-24 13:29:22 +02002689 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002690 if (!tpdfname) {
Michal Vasko1734be92020-09-22 08:55:10 +02002691 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, cleanup);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002692 }
2693 }
Radek Krejcic5c27e52018-11-15 14:38:11 +01002694 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002695 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +02002696 idref = (struct lysc_type_identityref *)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002697
2698 /* RFC 7950 9.10.2 - base */
2699 if (type_p->bases) {
2700 if (base) {
2701 /* only the directly derived identityrefs can contain base specification */
2702 if (tpdfname) {
2703 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002704 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002705 tpdfname);
2706 } else {
2707 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002708 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002709 }
2710 return LY_EVALID;
2711 }
Radek Krejci0a33b042020-05-27 10:05:06 +02002712 LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002713 }
2714
2715 if (!base && !type_p->flags) {
2716 /* type derived from identityref built-in type must contain at least one base */
2717 if (tpdfname) {
2718 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2719 } else {
2720 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002721 }
2722 return LY_EVALID;
2723 }
Radek Krejci555cb5b2018-11-16 14:54:33 +01002724 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002725 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +02002726 lref = (struct lysc_type_leafref *)*type;
Michal Vasko004d3152020-06-11 19:59:22 +02002727
Radek Krejcia3045382018-11-22 14:30:31 +01002728 /* RFC 7950 9.9.3 - require-instance */
2729 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002730 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002731 if (tpdfname) {
2732 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2733 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2734 } else {
2735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2736 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002737 }
2738 return LY_EVALID;
2739 }
Michal Vasko004d3152020-06-11 19:59:22 +02002740 lref->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002741 } else if (base) {
2742 /* inherit */
Michal Vasko004d3152020-06-11 19:59:22 +02002743 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002744 } else {
2745 /* default is true */
Michal Vasko004d3152020-06-11 19:59:22 +02002746 lref->require_instance = 1;
Radek Krejcia3045382018-11-22 14:30:31 +01002747 }
2748 if (type_p->path) {
Michal Vasko1734be92020-09-22 08:55:10 +02002749 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
Michal Vasko004d3152020-06-11 19:59:22 +02002750 lref->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002751 } else if (base) {
Michal Vasko1734be92020-09-22 08:55:10 +02002752 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
Michal Vasko22df3f02020-08-24 13:29:22 +02002753 lref->path_context = ((struct lysc_type_leafref *)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002754 } else if (tpdfname) {
2755 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2756 return LY_EVALID;
2757 } else {
2758 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Radek Krejcia3045382018-11-22 14:30:31 +01002759 return LY_EVALID;
2760 }
Radek Krejcia3045382018-11-22 14:30:31 +01002761 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002762 case LY_TYPE_INST:
2763 /* RFC 7950 9.9.3 - require-instance */
2764 if (type_p->flags & LYS_SET_REQINST) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002765 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
Radek Krejci16c0f822018-11-16 10:46:10 +01002766 } else {
2767 /* default is true */
Michal Vasko22df3f02020-08-24 13:29:22 +02002768 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
Radek Krejci16c0f822018-11-16 10:46:10 +01002769 }
Radek Krejci16c0f822018-11-16 10:46:10 +01002770 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002771 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +02002772 un = (struct lysc_type_union *)(*type);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002773
2774 /* RFC 7950 7.4 - type */
2775 if (type_p->types) {
2776 if (base) {
2777 /* only the directly derived union can contain types specification */
2778 if (tpdfname) {
2779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2780 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2781 tpdfname);
2782 } else {
2783 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2784 "Invalid type substatement for the type not directly derived from union built-in type.");
Radek Krejcicdfecd92018-11-26 11:27:32 +01002785 }
2786 return LY_EVALID;
2787 }
2788 /* compile the type */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002789 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID);
2790 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02002791 LY_CHECK_RET(lys_compile_type(ctx, context_pnode, context_flags, context_mod, context_name,
2792 &type_p->types[u], &un->types[u + additional], NULL, NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002793 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2794 /* add space for additional types from the union subtype */
2795 un_aux = (struct lysc_type_union *)un->types[u + additional];
Michal Vasko22df3f02020-08-24 13:29:22 +02002796 LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1,
2797 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002798
2799 /* copy subtypes of the subtype union */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002800 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002801 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2802 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2803 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
Michal Vasko22df3f02020-08-24 13:29:22 +02002804 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx); lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +02002805 lref = (struct lysc_type_leafref *)un->types[u + additional];
2806
2807 lref->basetype = LY_TYPE_LEAFREF;
Michal Vasko1734be92020-09-22 08:55:10 +02002808 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path));
Michal Vasko004d3152020-06-11 19:59:22 +02002809 lref->refcount = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +02002810 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
2811 lref->path_context = ((struct lysc_type_leafref *)un_aux->types[v])->path_context;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002812 /* TODO extensions */
2813
2814 } else {
2815 un->types[u + additional] = un_aux->types[v];
2816 ++un_aux->types[v]->refcount;
2817 }
2818 ++additional;
2819 LY_ARRAY_INCREMENT(un->types);
2820 }
2821 /* compensate u increment in main loop */
2822 --additional;
2823
2824 /* free the replaced union subtype */
Michal Vasko22df3f02020-08-24 13:29:22 +02002825 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002826 } else {
2827 LY_ARRAY_INCREMENT(un->types);
2828 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002829 }
2830 }
2831
2832 if (!base && !type_p->flags) {
2833 /* type derived from union built-in type must contain at least one type */
2834 if (tpdfname) {
2835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2836 } else {
2837 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Radek Krejcicdfecd92018-11-26 11:27:32 +01002838 }
2839 return LY_EVALID;
2840 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002841 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002842 case LY_TYPE_BOOL:
2843 case LY_TYPE_EMPTY:
2844 case LY_TYPE_UNKNOWN: /* just to complete switch */
2845 break;
2846 }
Michal Vasko1734be92020-09-22 08:55:10 +02002847
2848 if (tpdfname) {
2849 switch (basetype) {
2850 case LY_TYPE_BINARY:
2851 type_p->compiled = *type;
2852 *type = calloc(1, sizeof(struct lysc_type_bin));
2853 break;
2854 case LY_TYPE_BITS:
2855 type_p->compiled = *type;
2856 *type = calloc(1, sizeof(struct lysc_type_bits));
2857 break;
2858 case LY_TYPE_DEC64:
2859 type_p->compiled = *type;
2860 *type = calloc(1, sizeof(struct lysc_type_dec));
2861 break;
2862 case LY_TYPE_STRING:
2863 type_p->compiled = *type;
2864 *type = calloc(1, sizeof(struct lysc_type_str));
2865 break;
2866 case LY_TYPE_ENUM:
2867 type_p->compiled = *type;
2868 *type = calloc(1, sizeof(struct lysc_type_enum));
2869 break;
2870 case LY_TYPE_INT8:
2871 case LY_TYPE_UINT8:
2872 case LY_TYPE_INT16:
2873 case LY_TYPE_UINT16:
2874 case LY_TYPE_INT32:
2875 case LY_TYPE_UINT32:
2876 case LY_TYPE_INT64:
2877 case LY_TYPE_UINT64:
2878 type_p->compiled = *type;
2879 *type = calloc(1, sizeof(struct lysc_type_num));
2880 break;
2881 case LY_TYPE_IDENT:
2882 type_p->compiled = *type;
2883 *type = calloc(1, sizeof(struct lysc_type_identityref));
2884 break;
2885 case LY_TYPE_LEAFREF:
2886 type_p->compiled = *type;
2887 *type = calloc(1, sizeof(struct lysc_type_leafref));
2888 break;
2889 case LY_TYPE_INST:
2890 type_p->compiled = *type;
2891 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2892 break;
2893 case LY_TYPE_UNION:
2894 type_p->compiled = *type;
2895 *type = calloc(1, sizeof(struct lysc_type_union));
2896 break;
2897 case LY_TYPE_BOOL:
2898 case LY_TYPE_EMPTY:
2899 case LY_TYPE_UNKNOWN: /* just to complete switch */
2900 break;
2901 }
2902 }
Radek Krejcic5c27e52018-11-15 14:38:11 +01002903 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko1734be92020-09-22 08:55:10 +02002904
2905cleanup:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002906 return ret;
2907}
2908
Radek Krejcia3045382018-11-22 14:30:31 +01002909/**
2910 * @brief Compile information about the leaf/leaf-list's type.
2911 * @param[in] ctx Compile context.
Michal Vasko7f45cf22020-10-01 12:49:44 +02002912 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002913 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2914 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2915 * @param[in] context_name Name of the context node or referencing typedef for logging.
2916 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002917 * @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 +01002918 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002919 * @param[out] dflt Default value for the type.
Radek Krejcia3045382018-11-22 14:30:31 +01002920 * @return LY_ERR value.
2921 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002922static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02002923lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags,
Radek Krejci0f969882020-08-21 16:56:47 +02002924 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
Michal Vasko7f45cf22020-10-01 12:49:44 +02002925 struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Radek Krejci19a96102018-11-15 13:38:09 +01002926{
2927 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02002928 ly_bool dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002929 struct type_context {
2930 const struct lysp_tpdf *tpdf;
2931 struct lysp_node *node;
2932 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002933 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002934 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002935 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002936 struct ly_set tpdf_chain = {0};
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002937
Radek Krejci19a96102018-11-15 13:38:09 +01002938 (*type) = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002939 if (dflt) {
2940 *dflt = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002941 }
Radek Krejci19a96102018-11-15 13:38:09 +01002942
2943 tctx = calloc(1, sizeof *tctx);
2944 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002945 for (ret = lysp_type_find(type_p->name, context_pnode, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002946 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2947 ret == LY_SUCCESS;
2948 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2949 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2950 if (basetype) {
2951 break;
2952 }
2953
2954 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002955 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2956 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci87e25252020-09-15 13:28:31 +02002957 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01002958
Radek Krejcicdfecd92018-11-26 11:27:32 +01002959 if (units && !*units) {
2960 /* inherit units */
Radek Krejci87e25252020-09-15 13:28:31 +02002961 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
2962 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002963 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02002964 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002965 /* inherit default */
Michal Vasko7f45cf22020-10-01 12:49:44 +02002966 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002967 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002968 if (dummyloops && (!units || *units) && dflt && *dflt) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002969 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002970 break;
2971 }
2972
Radek Krejci19a96102018-11-15 13:38:09 +01002973 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002974 /* it is not necessary to continue, the rest of the chain was already compiled,
2975 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002976 basetype = tctx->tpdf->type.compiled->basetype;
Radek Krejciba03a5a2020-08-27 14:40:41 +02002977 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
Radek Krejci87e25252020-09-15 13:28:31 +02002978 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejciba03a5a2020-08-27 14:40:41 +02002979
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002980 if ((units && !*units) || (dflt && !*dflt)) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002981 dummyloops = 1;
2982 goto preparenext;
2983 } else {
2984 tctx = NULL;
2985 break;
2986 }
Radek Krejci19a96102018-11-15 13:38:09 +01002987 }
2988
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002989 /* circular typedef reference detection */
Radek Krejci1deb5be2020-08-26 16:43:36 +02002990 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002991 /* local part */
Michal Vasko22df3f02020-08-24 13:29:22 +02002992 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002993 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2994 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2995 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2996 free(tctx);
2997 ret = LY_EVALID;
2998 goto cleanup;
2999 }
3000 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02003001 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003002 /* global part for unions corner case */
Michal Vasko22df3f02020-08-24 13:29:22 +02003003 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003004 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3005 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3006 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3007 free(tctx);
3008 ret = LY_EVALID;
3009 goto cleanup;
3010 }
3011 }
3012
Radek Krejci19a96102018-11-15 13:38:09 +01003013 /* store information for the following processing */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003014 ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
Radek Krejci87e25252020-09-15 13:28:31 +02003015 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003016
Radek Krejcicdfecd92018-11-26 11:27:32 +01003017preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003018 /* prepare next loop */
3019 tctx_prev = tctx;
3020 tctx = calloc(1, sizeof *tctx);
3021 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3022 }
3023 free(tctx);
3024
3025 /* allocate type according to the basetype */
3026 switch (basetype) {
3027 case LY_TYPE_BINARY:
3028 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003029 break;
3030 case LY_TYPE_BITS:
3031 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003032 break;
3033 case LY_TYPE_BOOL:
3034 case LY_TYPE_EMPTY:
3035 *type = calloc(1, sizeof(struct lysc_type));
3036 break;
3037 case LY_TYPE_DEC64:
3038 *type = calloc(1, sizeof(struct lysc_type_dec));
3039 break;
3040 case LY_TYPE_ENUM:
3041 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003042 break;
3043 case LY_TYPE_IDENT:
3044 *type = calloc(1, sizeof(struct lysc_type_identityref));
3045 break;
3046 case LY_TYPE_INST:
3047 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3048 break;
3049 case LY_TYPE_LEAFREF:
3050 *type = calloc(1, sizeof(struct lysc_type_leafref));
3051 break;
3052 case LY_TYPE_STRING:
3053 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003054 break;
3055 case LY_TYPE_UNION:
3056 *type = calloc(1, sizeof(struct lysc_type_union));
3057 break;
3058 case LY_TYPE_INT8:
3059 case LY_TYPE_UINT8:
3060 case LY_TYPE_INT16:
3061 case LY_TYPE_UINT16:
3062 case LY_TYPE_INT32:
3063 case LY_TYPE_UINT32:
3064 case LY_TYPE_INT64:
3065 case LY_TYPE_UINT64:
3066 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003067 break;
3068 case LY_TYPE_UNKNOWN:
3069 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3070 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3071 ret = LY_EVALID;
3072 goto cleanup;
3073 }
3074 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003075 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003076 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3077 ly_data_type2str[basetype]);
3078 free(*type);
3079 (*type) = NULL;
3080 ret = LY_EVALID;
3081 goto cleanup;
3082 }
3083
3084 /* get restrictions from the referred typedefs */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003085 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003086 tctx = (struct type_context *)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003087
3088 /* remember the typedef context for circular check */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003089 ret = ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL);
3090 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003091
Radek Krejci43699232018-11-23 14:59:46 +01003092 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003093 base = tctx->tpdf->type.compiled;
3094 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003095 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003096 /* no change, just use the type information from the base */
Michal Vasko22df3f02020-08-24 13:29:22 +02003097 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
Radek Krejci19a96102018-11-15 13:38:09 +01003098 ++base->refcount;
3099 continue;
3100 }
3101
3102 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003103 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3104 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3105 tctx->tpdf->name, ly_data_type2str[basetype]);
3106 ret = LY_EVALID;
3107 goto cleanup;
Michal Vasko7f45cf22020-10-01 12:49:44 +02003108 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt.str) {
Radek Krejci43699232018-11-23 14:59:46 +01003109 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3110 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
Michal Vasko7f45cf22020-10-01 12:49:44 +02003111 tctx->tpdf->name, tctx->tpdf->dflt.str);
Radek Krejci43699232018-11-23 14:59:46 +01003112 ret = LY_EVALID;
3113 goto cleanup;
3114 }
3115
Radek Krejci19a96102018-11-15 13:38:09 +01003116 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003117 /* TODO user type plugins */
3118 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003119 prev_type = *type;
Michal Vasko22df3f02020-08-24 13:29:22 +02003120 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 +01003121 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003122 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003123 LY_CHECK_GOTO(ret, cleanup);
3124 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003125 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003126 /* remove the processed typedef contexts from the stack for circular check */
3127 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003128
Radek Krejcic5c27e52018-11-15 14:38:11 +01003129 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003130 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003131 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003132 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003133 /* TODO user type plugins */
3134 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003135 ++(*type)->refcount;
Michal Vasko7f45cf22020-10-01 12:49:44 +02003136 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003137 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci90b148f2020-05-06 17:49:40 +02003138 } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) {
Radek Krejci19a96102018-11-15 13:38:09 +01003139 /* no specific restriction in leaf's type definition, copy from the base */
3140 free(*type);
3141 (*type) = base;
3142 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003143 }
3144
Radek Krejci0935f412019-08-20 16:15:18 +02003145 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003146
3147cleanup:
3148 ly_set_erase(&tpdf_chain, free);
3149 return ret;
3150}
3151
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003152/**
3153 * @brief Compile status information of the given node.
3154 *
3155 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3156 * has the status correctly set during the compilation.
3157 *
3158 * @param[in] ctx Compile context
3159 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3160 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3161 * the compatibility with the parent's status value.
3162 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3163 * @return LY_ERR value.
3164 */
3165static LY_ERR
3166lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3167{
3168 /* status - it is not inherited by specification, but it does not make sense to have
3169 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3170 if (!((*node_flags) & LYS_STATUS_MASK)) {
3171 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3172 if ((parent_flags & 0x3) != 0x3) {
3173 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3174 * combination of bits (0x3) which marks the uses_status value */
3175 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
Radek Krejci0f969882020-08-21 16:56:47 +02003176 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003177 }
3178 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3179 } else {
3180 (*node_flags) |= LYS_STATUS_CURR;
3181 }
3182 } else if (parent_flags & LYS_STATUS_MASK) {
3183 /* check status compatibility with the parent */
3184 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3185 if ((*node_flags) & LYS_STATUS_CURR) {
3186 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3187 "A \"current\" status is in conflict with the parent's \"%s\" status.",
Radek Krejci0f969882020-08-21 16:56:47 +02003188 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003189 } else { /* LYS_STATUS_DEPRC */
3190 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3191 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3192 }
3193 return LY_EVALID;
3194 }
3195 }
3196 return LY_SUCCESS;
3197}
3198
Radek Krejci8cce8532019-03-05 11:27:45 +01003199/**
3200 * @brief Check uniqness of the node/action/notification name.
3201 *
3202 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3203 * structures, but they share the namespace so we need to check their name collisions.
3204 *
3205 * @param[in] ctx Compile context.
Michal Vasko20424b42020-08-31 12:29:38 +02003206 * @param[in] parent Parent of the nodes to check, can be NULL.
Radek Krejci8cce8532019-03-05 11:27:45 +01003207 * @param[in] name Name of the item to find in the given lists.
Michal Vasko20424b42020-08-31 12:29:38 +02003208 * @param[in] exclude Node that was just added that should be excluded from the name checking.
Radek Krejci8cce8532019-03-05 11:27:45 +01003209 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3210 */
3211static LY_ERR
Michal Vasko20424b42020-08-31 12:29:38 +02003212lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
3213 const struct lysc_node *exclude)
Radek Krejci8cce8532019-03-05 11:27:45 +01003214{
Michal Vasko20424b42020-08-31 12:29:38 +02003215 const struct lysc_node *iter, *iter2;
3216 const struct lysc_action *actions;
3217 const struct lysc_notif *notifs;
3218 uint32_t getnext_flags;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003219 LY_ARRAY_COUNT_TYPE u;
Radek Krejci8cce8532019-03-05 11:27:45 +01003220
Michal Vasko20424b42020-08-31 12:29:38 +02003221#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
3222
3223 if (exclude->nodetype == LYS_CASE) {
3224 /* check restricted only to all the cases */
3225 assert(parent->nodetype == LYS_CHOICE);
3226 LY_LIST_FOR(lysc_node_children(parent, 0), iter) {
3227 if (CHECK_NODE(iter, exclude, name)) {
3228 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case");
3229 return LY_EEXIST;
3230 }
3231 }
3232
3233 return LY_SUCCESS;
3234 }
3235
3236 /* no reason for our parent to be choice anymore */
3237 assert(!parent || (parent->nodetype != LYS_CHOICE));
3238
3239 if (parent && (parent->nodetype == LYS_CASE)) {
3240 /* move to the first data definition parent */
3241 parent = lysc_data_parent(parent);
3242 }
3243
3244 getnext_flags = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE;
3245 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) {
3246 getnext_flags |= LYS_GETNEXT_OUTPUT;
3247 }
3248
3249 iter = NULL;
3250 while ((iter = lys_getnext(iter, parent, ctx->mod->compiled, getnext_flags))) {
3251 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003252 goto error;
3253 }
Michal Vasko20424b42020-08-31 12:29:38 +02003254
3255 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
3256 if (iter->nodetype == LYS_CHOICE) {
3257 iter2 = NULL;
3258 while ((iter2 = lys_getnext(iter2, iter, NULL, LYS_GETNEXT_NOSTATECHECK))) {
3259 if (CHECK_NODE(iter2, exclude, name)) {
3260 goto error;
3261 }
3262 }
3263 }
Radek Krejci8cce8532019-03-05 11:27:45 +01003264 }
Michal Vasko20424b42020-08-31 12:29:38 +02003265
3266 actions = parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs;
Radek Krejci8cce8532019-03-05 11:27:45 +01003267 LY_ARRAY_FOR(actions, u) {
Michal Vasko20424b42020-08-31 12:29:38 +02003268 if (CHECK_NODE(&actions[u], exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003269 goto error;
3270 }
3271 }
Michal Vasko20424b42020-08-31 12:29:38 +02003272
3273 notifs = parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs;
Radek Krejci8cce8532019-03-05 11:27:45 +01003274 LY_ARRAY_FOR(notifs, u) {
Michal Vasko20424b42020-08-31 12:29:38 +02003275 if (CHECK_NODE(&notifs[u], exclude, name)) {
Radek Krejci8cce8532019-03-05 11:27:45 +01003276 goto error;
3277 }
3278 }
3279 return LY_SUCCESS;
Michal Vasko20424b42020-08-31 12:29:38 +02003280
Radek Krejci8cce8532019-03-05 11:27:45 +01003281error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003282 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003283 return LY_EEXIST;
Michal Vasko20424b42020-08-31 12:29:38 +02003284
3285#undef CHECK_NODE
Radek Krejci8cce8532019-03-05 11:27:45 +01003286}
3287
Michal Vasko7f45cf22020-10-01 12:49:44 +02003288static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent,
3289 uint16_t uses_status, struct ly_set *child_set);
3290
3291static LY_ERR lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode,
3292 const struct lysc_node *parent, struct lysp_node **dev_pnode, ly_bool *not_supported);
3293
3294static LY_ERR lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node);
3295
3296static void lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode);
Radek Krejci19a96102018-11-15 13:38:09 +01003297
Radek Krejcia3045382018-11-22 14:30:31 +01003298/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003299 * @brief Compile parsed RPC/action schema node information.
3300 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003301 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003302 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003303 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3304 * @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).
3305 * Zero means no uses, non-zero value with no status bit set mean the default status.
Michal Vasko7f45cf22020-10-01 12:49:44 +02003306 * @return LY_SUCCESS on success,
3307 * @return LY_EVALID on validation error,
3308 * @return LY_EDENIED on not-supported deviation.
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003309 */
3310static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003311lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003312 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003313{
3314 LY_ERR ret = LY_SUCCESS;
Michal Vasko7f45cf22020-10-01 12:49:44 +02003315 struct lysp_node *child_p, *dev_pnode = NULL, *dev_input_p = NULL, *dev_output_p = NULL;
3316 struct lysp_action *orig_action_p = action_p;
3317 struct lysp_action_inout *inout_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003318 LY_ARRAY_COUNT_TYPE u;
Michal Vasko7f45cf22020-10-01 12:49:44 +02003319 ly_bool not_supported;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003320 uint32_t opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003321
Radek Krejci327de162019-06-14 12:52:07 +02003322 lysc_update_path(ctx, parent, action_p->name);
3323
Michal Vasko7f45cf22020-10-01 12:49:44 +02003324 /* apply deviation on the action/RPC */
3325 LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)action_p, parent, &dev_pnode, &not_supported));
3326 if (not_supported) {
3327 lysc_update_path(ctx, NULL, NULL);
3328 return LY_EDENIED;
3329 } else if (dev_pnode) {
3330 action_p = (struct lysp_action *)dev_pnode;
3331 }
3332
Michal Vasko20424b42020-08-31 12:29:38 +02003333 /* member needed for uniqueness check lys_getnext() */
3334 action->nodetype = parent ? LYS_ACTION : LYS_RPC;
3335 action->module = ctx->mod;
3336 action->parent = parent;
3337
3338 LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, action_p->name, (struct lysc_node *)action));
Radek Krejci8cce8532019-03-05 11:27:45 +01003339
Radek Krejciec4da802019-05-02 13:02:41 +02003340 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003341 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003342 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003343 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003344 return LY_EVALID;
3345 }
3346
Radek Krejciec4da802019-05-02 13:02:41 +02003347 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02003348 action->sp = orig_action_p;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003349 }
3350 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3351
3352 /* status - it is not inherited by specification, but it does not make sense to have
3353 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskocc048b22020-03-27 15:52:38 +01003354 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003355
Radek Krejci011e4aa2020-09-04 15:22:31 +02003356 DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup);
3357 DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup);
3358 DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003359 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003360 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003361
Michal Vasko7f45cf22020-10-01 12:49:44 +02003362 /* connect any action augments */
3363 LY_CHECK_RET(lys_compile_node_augments(ctx, (struct lysc_node *)action));
3364
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003365 /* input */
Michal Vasko22df3f02020-08-24 13:29:22 +02003366 lysc_update_path(ctx, (struct lysc_node *)action, "input");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003367
3368 /* apply deviations on input */
3369 LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)&action_p->input, (struct lysc_node *)action,
3370 &dev_input_p, &not_supported));
3371 if (not_supported) {
3372 inout_p = NULL;
3373 } else if (dev_input_p) {
3374 inout_p = (struct lysp_action_inout *)dev_input_p;
3375 } else {
3376 inout_p = &action_p->input;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003377 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02003378
3379 if (inout_p) {
3380 action->input.nodetype = LYS_INPUT;
3381 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, action->input.musts, u, lys_compile_must, ret, cleanup);
3382 COMPILE_EXTS_GOTO(ctx, inout_p->exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup);
3383 ctx->options |= LYSC_OPT_RPC_INPUT;
3384
3385 /* connect any input augments */
3386 LY_CHECK_RET(lys_compile_node_augments(ctx, (struct lysc_node *)&action->input));
3387
3388 LY_LIST_FOR(inout_p->data, child_p) {
3389 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status, NULL));
3390 }
3391 ctx->options = opt_prev;
3392 }
3393
Radek Krejci327de162019-06-14 12:52:07 +02003394 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003395
3396 /* output */
Michal Vasko22df3f02020-08-24 13:29:22 +02003397 lysc_update_path(ctx, (struct lysc_node *)action, "output");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003398
3399 /* apply deviations on output */
3400 LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)&action_p->output, (struct lysc_node *)action,
3401 &dev_output_p, &not_supported));
3402 if (not_supported) {
3403 inout_p = NULL;
3404 } else if (dev_output_p) {
3405 inout_p = (struct lysp_action_inout *)dev_output_p;
3406 } else {
3407 inout_p = &action_p->output;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003408 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02003409
3410 if (inout_p) {
3411 action->output.nodetype = LYS_OUTPUT;
3412 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, action->output.musts, u, lys_compile_must, ret, cleanup);
3413 COMPILE_EXTS_GOTO(ctx, inout_p->exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup);
3414 ctx->options |= LYSC_OPT_RPC_OUTPUT;
3415
3416 /* connect any output augments */
3417 LY_CHECK_RET(lys_compile_node_augments(ctx, (struct lysc_node *)&action->output));
3418
3419 LY_LIST_FOR(inout_p->data, child_p) {
3420 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status, NULL));
3421 }
3422 ctx->options = opt_prev;
3423 }
3424
Radek Krejci327de162019-06-14 12:52:07 +02003425 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003426
Michal Vasko7f45cf22020-10-01 12:49:44 +02003427 if ((action->input.musts || action->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01003428 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003429 ret = ly_set_add(&ctx->xpath, action, 0, NULL);
3430 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003431 }
3432
Michal Vasko7f45cf22020-10-01 12:49:44 +02003433 lysc_update_path(ctx, NULL, NULL);
3434
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003435cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003436 lysp_dev_node_free(ctx->ctx, dev_pnode);
3437 lysp_dev_node_free(ctx->ctx, dev_input_p);
3438 lysp_dev_node_free(ctx->ctx, dev_output_p);
Radek Krejciec4da802019-05-02 13:02:41 +02003439 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003440 return ret;
3441}
3442
3443/**
Radek Krejci43981a32019-04-12 09:44:11 +02003444 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003445 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003446 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003447 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3448 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3449 * @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 +02003450 * Zero means no uses, non-zero value with no status bit set mean the default status.
Michal Vasko7f45cf22020-10-01 12:49:44 +02003451 * @return LY_SUCCESS on success,
3452 * @return LY_EVALID on validation error,
3453 * @return LY_EDENIED on not-supported deviation.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003454 */
3455static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003456lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003457 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
Radek Krejcifc11bd72019-04-11 16:00:05 +02003458{
3459 LY_ERR ret = LY_SUCCESS;
Michal Vasko7f45cf22020-10-01 12:49:44 +02003460 struct lysp_node *child_p, *dev_pnode = NULL;
3461 struct lysp_notif *orig_notif_p = notif_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003462 LY_ARRAY_COUNT_TYPE u;
Michal Vasko7f45cf22020-10-01 12:49:44 +02003463 ly_bool not_supported;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003464 uint32_t opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003465
Radek Krejci327de162019-06-14 12:52:07 +02003466 lysc_update_path(ctx, parent, notif_p->name);
3467
Michal Vasko7f45cf22020-10-01 12:49:44 +02003468 LY_CHECK_RET(lys_compile_node_deviations_refines(ctx, (struct lysp_node *)notif_p, parent, &dev_pnode, &not_supported));
3469 if (not_supported) {
3470 lysc_update_path(ctx, NULL, NULL);
3471 return LY_EDENIED;
3472 } else if (dev_pnode) {
3473 notif_p = (struct lysp_notif *)dev_pnode;
3474 }
3475
Michal Vasko20424b42020-08-31 12:29:38 +02003476 /* member needed for uniqueness check lys_getnext() */
3477 notif->nodetype = LYS_NOTIF;
3478 notif->module = ctx->mod;
3479 notif->parent = parent;
3480
3481 LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, notif_p->name, (struct lysc_node *)notif));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003482
Radek Krejciec4da802019-05-02 13:02:41 +02003483 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003484 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3485 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003486 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003487 return LY_EVALID;
3488 }
3489
Radek Krejciec4da802019-05-02 13:02:41 +02003490 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02003491 notif->sp = orig_notif_p;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003492 }
3493 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3494
3495 /* status - it is not inherited by specification, but it does not make sense to have
3496 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003497 ret = lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0));
3498 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003499
Radek Krejci011e4aa2020-09-04 15:22:31 +02003500 DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup);
3501 DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup);
3502 DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003503 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003504 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003505 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3506 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003507 ret = ly_set_add(&ctx->xpath, notif, 0, NULL);
3508 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003509 }
Radek Krejci0935f412019-08-20 16:15:18 +02003510 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003511
Radek Krejciec4da802019-05-02 13:02:41 +02003512 ctx->options |= LYSC_OPT_NOTIFICATION;
Michal Vasko7f45cf22020-10-01 12:49:44 +02003513
3514 /* connect any notification augments */
3515 LY_CHECK_RET(lys_compile_node_augments(ctx, (struct lysc_node *)notif));
3516
Radek Krejcifc11bd72019-04-11 16:00:05 +02003517 LY_LIST_FOR(notif_p->data, child_p) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02003518 ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02003519 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003520 }
3521
Radek Krejci327de162019-06-14 12:52:07 +02003522 lysc_update_path(ctx, NULL, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003523
Radek Krejcifc11bd72019-04-11 16:00:05 +02003524cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003525 lysp_dev_node_free(ctx->ctx, dev_pnode);
Radek Krejciec4da802019-05-02 13:02:41 +02003526 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003527 return ret;
3528}
3529
3530/**
Radek Krejcia3045382018-11-22 14:30:31 +01003531 * @brief Compile parsed container node information.
3532 * @param[in] ctx Compile context
Michal Vasko7f45cf22020-10-01 12:49:44 +02003533 * @param[in] pnode Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003534 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3535 * is enriched with the container-specific information.
3536 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3537 */
Radek Krejci19a96102018-11-15 13:38:09 +01003538static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02003539lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003540{
Michal Vasko7f45cf22020-10-01 12:49:44 +02003541 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
Michal Vasko22df3f02020-08-24 13:29:22 +02003542 struct lysc_node_container *cont = (struct lysc_node_container *)node;
Radek Krejci19a96102018-11-15 13:38:09 +01003543 struct lysp_node *child_p;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003544 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003545 LY_ERR ret = LY_SUCCESS;
3546
Radek Krejcife909632019-02-12 15:34:42 +01003547 if (cont_p->presence) {
Michal Vaskoba417ac2020-08-06 14:48:20 +02003548 /* explicit presence */
Radek Krejcife909632019-02-12 15:34:42 +01003549 cont->flags |= LYS_PRESENCE;
Michal Vaskoba417ac2020-08-06 14:48:20 +02003550 } else if (cont_p->musts) {
3551 /* container with a must condition */
Radek Krejci175f25b2020-08-13 12:02:36 +02003552 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name);
3553 cont->flags |= LYS_PRESENCE;
3554 } else if (cont_p->when) {
3555 /* container with a when condition */
3556 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"when\" condition.", cont_p->name);
Michal Vaskoba417ac2020-08-06 14:48:20 +02003557 cont->flags |= LYS_PRESENCE;
3558 } else if (cont_p->parent) {
3559 if (cont_p->parent->nodetype == LYS_CHOICE) {
3560 /* container is an implicit case, so its existence decides the existence of the whole case */
Radek Krejci175f25b2020-08-13 12:02:36 +02003561 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
Michal Vaskoba417ac2020-08-06 14:48:20 +02003562 cont_p->name, cont_p->parent->name);
3563 cont->flags |= LYS_PRESENCE;
3564 } else if ((cont_p->parent->nodetype == LYS_CASE)
Michal Vasko7f45cf22020-10-01 12:49:44 +02003565 && (((struct lysp_node_case *)cont_p->parent)->child == pnode) && !cont_p->next) {
Michal Vaskoba417ac2020-08-06 14:48:20 +02003566 /* container is the only node in a case, so its existence decides the existence of the whole case */
Radek Krejci175f25b2020-08-13 12:02:36 +02003567 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".",
Michal Vaskoba417ac2020-08-06 14:48:20 +02003568 cont_p->name, cont_p->parent->name);
3569 cont->flags |= LYS_PRESENCE;
3570 }
Radek Krejcife909632019-02-12 15:34:42 +01003571 }
3572
Michal Vaskoba417ac2020-08-06 14:48:20 +02003573 /* more cases when the container has meaning but is kept NP for convenience:
3574 * - when condition
3575 * - direct child action/notification
3576 */
3577
Radek Krejci19a96102018-11-15 13:38:09 +01003578 LY_LIST_FOR(cont_p->child, child_p) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02003579 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02003580 LY_CHECK_GOTO(ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003581 }
3582
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003583 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003584 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3585 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003586 ret = ly_set_add(&ctx->xpath, cont, 0, NULL);
3587 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003588 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02003589 COMPILE_OP_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3590 COMPILE_OP_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003591
3592done:
3593 return ret;
3594}
3595
Radek Krejci33f72892019-02-21 10:36:58 +01003596/*
3597 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3598 * @param[in] ctx Compile context.
3599 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3600 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003601 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3602 * @return LY_ERR value.
3603 */
3604static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003605lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
Radek Krejci0f969882020-08-21 16:56:47 +02003606 struct lysc_node_leaf *leaf)
Radek Krejci33f72892019-02-21 10:36:58 +01003607{
Michal Vasko7f45cf22020-10-01 12:49:44 +02003608 struct lysp_qname *dflt;
Radek Krejci33f72892019-02-21 10:36:58 +01003609
Radek Krejciec4da802019-05-02 13:02:41 +02003610 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, &leaf->type,
Michal Vasko7f45cf22020-10-01 12:49:44 +02003611 leaf->units ? NULL : &leaf->units, &dflt));
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003612
3613 /* store default value, if any */
3614 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02003615 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
Radek Krejci33f72892019-02-21 10:36:58 +01003616 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003617
Radek Krejci33f72892019-02-21 10:36:58 +01003618 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3619 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003620 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003621 } else if (leaf->type->basetype == LY_TYPE_UNION) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003622 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003623 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
3624 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
Radek Krejci33f72892019-02-21 10:36:58 +01003625 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003626 LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL));
Radek Krejci33f72892019-02-21 10:36:58 +01003627 }
3628 }
3629 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003630 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3631 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3632 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3633 return LY_EVALID;
3634 }
3635 }
3636
Radek Krejci33f72892019-02-21 10:36:58 +01003637 return LY_SUCCESS;
3638}
3639
Radek Krejcia3045382018-11-22 14:30:31 +01003640/**
3641 * @brief Compile parsed leaf node information.
3642 * @param[in] ctx Compile context
Michal Vasko7f45cf22020-10-01 12:49:44 +02003643 * @param[in] pnode Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003644 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3645 * is enriched with the leaf-specific information.
3646 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3647 */
Radek Krejci19a96102018-11-15 13:38:09 +01003648static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02003649lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003650{
Michal Vasko7f45cf22020-10-01 12:49:44 +02003651 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
Michal Vasko22df3f02020-08-24 13:29:22 +02003652 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko7c8439f2020-08-05 13:25:19 +02003653 LY_ARRAY_COUNT_TYPE u;
Radek Krejci19a96102018-11-15 13:38:09 +01003654 LY_ERR ret = LY_SUCCESS;
3655
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003656 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003657 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3658 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003659 ret = ly_set_add(&ctx->xpath, leaf, 0, NULL);
3660 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003661 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003662 if (leaf_p->units) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003663 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003664 leaf->flags |= LYS_SET_UNITS;
3665 }
Radek Krejcia1911222019-07-22 17:24:50 +02003666
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003667 /* compile type */
Michal Vasko7f45cf22020-10-01 12:49:44 +02003668 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
Radek Krejciba03a5a2020-08-27 14:40:41 +02003669 LY_CHECK_GOTO(ret, done);
Radek Krejcia1911222019-07-22 17:24:50 +02003670
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003671 /* store/update default value */
Michal Vasko7f45cf22020-10-01 12:49:44 +02003672 if (leaf_p->dflt.str) {
3673 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
Radek Krejci76b3e962018-12-14 17:01:25 +01003674 leaf->flags |= LYS_SET_DFLT;
3675 }
Radek Krejci43699232018-11-23 14:59:46 +01003676
Michal Vasko7f45cf22020-10-01 12:49:44 +02003677 /* checks */
3678 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
3679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3680 "Invalid mandatory leaf with a default value.");
3681 return LY_EVALID;
3682 }
3683
Radek Krejci19a96102018-11-15 13:38:09 +01003684done:
3685 return ret;
3686}
3687
Radek Krejcia3045382018-11-22 14:30:31 +01003688/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003689 * @brief Compile parsed leaf-list node information.
3690 * @param[in] ctx Compile context
Michal Vasko7f45cf22020-10-01 12:49:44 +02003691 * @param[in] pnode Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003692 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3693 * is enriched with the leaf-list-specific information.
3694 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3695 */
3696static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02003697lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003698{
Michal Vasko7f45cf22020-10-01 12:49:44 +02003699 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
Michal Vasko22df3f02020-08-24 13:29:22 +02003700 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003701 LY_ARRAY_COUNT_TYPE u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003702 LY_ERR ret = LY_SUCCESS;
3703
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003704 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003705 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3706 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003707 ret = ly_set_add(&ctx->xpath, llist, 0, NULL);
3708 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003709 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003710 if (llist_p->units) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003711 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003712 llist->flags |= LYS_SET_UNITS;
3713 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003714
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003715 /* compile type */
Michal Vasko7f45cf22020-10-01 12:49:44 +02003716 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
Radek Krejciba03a5a2020-08-27 14:40:41 +02003717 LY_CHECK_GOTO(ret, done);
Michal Vasko6a044b22020-01-15 12:25:39 +01003718
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003719 /* store/update default values */
Radek Krejci0e5d8382018-11-28 16:37:53 +01003720 if (llist_p->dflts) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02003721 if (ctx->mod_def->version < LYS_VERSION_1_1) {
3722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3723 "Leaf-list default values are allowed only in YANG 1.1 modules.");
3724 return LY_EVALID;
3725 }
3726
3727 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003728 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003729 }
3730
3731 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003732 if (llist->min) {
3733 llist->flags |= LYS_MAND_TRUE;
3734 }
Radek Krejcib7408632018-11-28 17:12:11 +01003735 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003736
Michal Vasko7f45cf22020-10-01 12:49:44 +02003737 /* checks */
3738 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
3739 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3740 "Invalid mandatory leaf-list with default value(s).");
3741 return LY_EVALID;
3742 }
3743
3744 if (llist->min > llist->max) {
3745 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
3746 llist->min, llist->max);
3747 return LY_EVALID;
3748 }
3749
Radek Krejci0e5d8382018-11-28 16:37:53 +01003750done:
3751 return ret;
3752}
3753
3754/**
Radek Krejci7af64242019-02-18 13:07:53 +01003755 * @brief Compile information about list's uniques.
3756 * @param[in] ctx Compile context.
Radek Krejci7af64242019-02-18 13:07:53 +01003757 * @param[in] uniques Sized array list of unique statements.
3758 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3759 * @return LY_ERR value.
3760 */
3761static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02003762lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
Radek Krejci7af64242019-02-18 13:07:53 +01003763{
3764 LY_ERR ret = LY_SUCCESS;
3765 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003766 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003767 const char *keystr, *delim;
3768 size_t len;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003769 LY_ARRAY_COUNT_TYPE v;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003770 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003771 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003772
Michal Vasko7f45cf22020-10-01 12:49:44 +02003773 LY_ARRAY_FOR(uniques, v) {
Radek Krejci7af64242019-02-18 13:07:53 +01003774 config = -1;
3775 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003776 keystr = uniques[v].str;
Radek Krejci7af64242019-02-18 13:07:53 +01003777 while (keystr) {
3778 delim = strpbrk(keystr, " \t\n");
3779 if (delim) {
3780 len = delim - keystr;
3781 while (isspace(*delim)) {
3782 ++delim;
3783 }
3784 } else {
3785 len = strlen(keystr);
3786 }
3787
3788 /* unique node must be present */
3789 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003790 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, uniques[v].mod, LYS_LEAF,
Michal Vasko22df3f02020-08-24 13:29:22 +02003791 (const struct lysc_node **)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003792 if (ret != LY_SUCCESS) {
3793 if (ret == LY_EDENIED) {
3794 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003795 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003796 len, keystr, lys_nodetype2str((*key)->nodetype));
3797 }
3798 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003799 } else if (flags) {
3800 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3801 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003802 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003803 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003804 }
3805
3806 /* all referenced leafs must be of the same config type */
3807 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3808 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko7f45cf22020-10-01 12:49:44 +02003809 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
Radek Krejci7af64242019-02-18 13:07:53 +01003810 return LY_EVALID;
3811 } else if ((*key)->flags & LYS_CONFIG_W) {
3812 config = 1;
3813 } else { /* LYS_CONFIG_R */
3814 config = 0;
3815 }
3816
Michal Vasko14654712020-02-06 08:35:21 +01003817 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3818 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3819 if (parent->nodetype == LYS_LIST) {
3820 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko7f45cf22020-10-01 12:49:44 +02003821 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
Michal Vasko14654712020-02-06 08:35:21 +01003822 return LY_EVALID;
3823 }
3824 }
3825
Radek Krejci7af64242019-02-18 13:07:53 +01003826 /* check status */
3827 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0f969882020-08-21 16:56:47 +02003828 (*key)->flags, (*key)->module, (*key)->name));
Radek Krejci7af64242019-02-18 13:07:53 +01003829
3830 /* mark leaf as unique */
3831 (*key)->flags |= LYS_UNIQUE;
3832
3833 /* next unique value in line */
3834 keystr = delim;
3835 }
3836 /* next unique definition */
3837 }
3838
3839 return LY_SUCCESS;
3840}
3841
3842/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003843 * @brief Compile parsed list node information.
3844 * @param[in] ctx Compile context
Michal Vasko7f45cf22020-10-01 12:49:44 +02003845 * @param[in] pnode Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003846 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3847 * is enriched with the list-specific information.
3848 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3849 */
3850static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02003851lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003852{
Michal Vasko7f45cf22020-10-01 12:49:44 +02003853 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
Michal Vasko22df3f02020-08-24 13:29:22 +02003854 struct lysc_node_list *list = (struct lysc_node_list *)node;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003855 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003856 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003857 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003858 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003859 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003860 LY_ERR ret = LY_SUCCESS;
3861
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003862 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003863 if (list->min) {
3864 list->flags |= LYS_MAND_TRUE;
3865 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003866 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3867
3868 LY_LIST_FOR(list_p->child, child_p) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02003869 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003870 }
3871
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003872 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003873 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3874 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003875 LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL));
Michal Vasko5d8756a2019-11-07 15:21:00 +01003876 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003877
3878 /* keys */
3879 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3880 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3881 return LY_EVALID;
3882 }
3883
3884 /* find all the keys (must be direct children) */
3885 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003886 if (!keystr) {
3887 /* keyless list */
3888 list->flags |= LYS_KEYLESS;
3889 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003890 while (keystr) {
3891 delim = strpbrk(keystr, " \t\n");
3892 if (delim) {
3893 len = delim - keystr;
3894 while (isspace(*delim)) {
3895 ++delim;
3896 }
3897 } else {
3898 len = strlen(keystr);
3899 }
3900
3901 /* key node must be present */
Michal Vasko22df3f02020-08-24 13:29:22 +02003902 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 +02003903 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003904 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3905 "The list's key \"%.*s\" not found.", len, keystr);
3906 return LY_EVALID;
3907 }
3908 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003909 if (key->flags & LYS_KEY) {
3910 /* the node was already marked as a key */
3911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3912 "Duplicated key identifier \"%.*s\".", len, keystr);
3913 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003914 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003915
Michal Vasko22df3f02020-08-24 13:29:22 +02003916 lysc_update_path(ctx, (struct lysc_node *)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003917 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003918 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003919 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3920 return LY_EVALID;
3921 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003922 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003923 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003924 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003925 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003926 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003927 return LY_EVALID;
3928 }
3929 } else {
3930 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003931 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003932 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003933 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003934 return LY_EVALID;
3935 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003936 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003937 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003938 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003939 return LY_EVALID;
3940 }
3941 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003942
3943 /* check status */
3944 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003945 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003946
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003947 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003948 if (key->dflt) {
3949 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3950 lysc_type_free(ctx->ctx, key->dflt->realtype);
3951 free(key->dflt);
3952 key->dflt = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003953 }
3954 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003955 key->flags |= LYS_KEY;
3956
3957 /* move it to the correct position */
Michal Vasko22df3f02020-08-24 13:29:22 +02003958 if ((prev_key && (struct lysc_node *)prev_key != key->prev) || (!prev_key && key->prev->next)) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02003959 /* fix links in closest previous siblings of the key */
3960 if (key->next) {
3961 key->next->prev = key->prev;
3962 } else {
3963 /* last child */
3964 list->child->prev = key->prev;
3965 }
3966 if (key->prev->next) {
3967 key->prev->next = key->next;
3968 }
3969 /* fix links in the key */
3970 if (prev_key) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003971 key->prev = (struct lysc_node *)prev_key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003972 key->next = prev_key->next;
3973 } else {
3974 key->prev = list->child->prev;
3975 key->next = list->child;
3976 }
3977 /* fix links in closes future siblings of the key */
3978 if (prev_key) {
3979 if (prev_key->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003980 prev_key->next->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003981 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003982 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003983 }
Michal Vasko22df3f02020-08-24 13:29:22 +02003984 prev_key->next = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003985 } else {
Michal Vasko22df3f02020-08-24 13:29:22 +02003986 list->child->prev = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003987 }
3988 /* fix links in parent */
3989 if (!key->prev->next) {
Michal Vasko22df3f02020-08-24 13:29:22 +02003990 list->child = (struct lysc_node *)key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003991 }
3992 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003993
3994 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003995 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003996 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003997 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003998 }
3999
4000 /* uniques */
4001 if (list_p->uniques) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02004002 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004003 }
4004
Michal Vasko7f45cf22020-10-01 12:49:44 +02004005 COMPILE_OP_ARRAY_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
4006 COMPILE_OP_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
4007
4008 /* checks */
4009 if (list->min > list->max) {
4010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
4011 list->min, list->max);
4012 return LY_EVALID;
4013 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004014
4015done:
4016 return ret;
4017}
4018
Radek Krejcib56c7502019-02-13 14:19:54 +01004019/**
4020 * @brief Do some checks and set the default choice's case.
4021 *
4022 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
4023 *
4024 * @param[in] ctx Compile context.
Michal Vasko7f45cf22020-10-01 12:49:44 +02004025 * @param[in] dflt Name of the default branch. Can even contain a prefix.
Radek Krejcib56c7502019-02-13 14:19:54 +01004026 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
4027 * @return LY_ERR value.
4028 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004029static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02004030lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
Radek Krejci76b3e962018-12-14 17:01:25 +01004031{
Michal Vasko22df3f02020-08-24 13:29:22 +02004032 struct lysc_node *iter, *node = (struct lysc_node *)ch;
Michal Vasko7f45cf22020-10-01 12:49:44 +02004033 const struct lys_module *mod;
Radek Krejci76b3e962018-12-14 17:01:25 +01004034 const char *prefix = NULL, *name;
4035 size_t prefix_len = 0;
4036
4037 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
Michal Vasko7f45cf22020-10-01 12:49:44 +02004038 name = strchr(dflt->str, ':');
Radek Krejci76b3e962018-12-14 17:01:25 +01004039 if (name) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02004040 prefix = dflt->str;
Radek Krejci76b3e962018-12-14 17:01:25 +01004041 prefix_len = name - prefix;
4042 ++name;
4043 } else {
Michal Vasko7f45cf22020-10-01 12:49:44 +02004044 name = dflt->str;
Radek Krejci76b3e962018-12-14 17:01:25 +01004045 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02004046 if (prefix) {
4047 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_PREF_SCHEMA, (void *)dflt->mod);
4048 if (!mod) {
4049 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4050 "Default case prefix \"%.*s\" not found in imports of \"%s\".", prefix_len, prefix, dflt->mod->name);
4051 return LY_EVALID;
4052 }
4053 } else {
4054 mod = node->module;
Radek Krejci76b3e962018-12-14 17:01:25 +01004055 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02004056
4057 ch->dflt = (struct lysc_node_case *)lys_find_child(node, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
Radek Krejci76b3e962018-12-14 17:01:25 +01004058 if (!ch->dflt) {
4059 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko7f45cf22020-10-01 12:49:44 +02004060 "Default case \"%s\" not found.", dflt->str);
Radek Krejci76b3e962018-12-14 17:01:25 +01004061 return LY_EVALID;
4062 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02004063
Radek Krejci76b3e962018-12-14 17:01:25 +01004064 /* no mandatory nodes directly under the default case */
4065 LY_LIST_FOR(ch->dflt->child, iter) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004066 if (iter->parent != (struct lysc_node *)ch->dflt) {
Radek Krejcife13da42019-02-15 14:51:01 +01004067 break;
4068 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004069 if (iter->flags & LYS_MAND_TRUE) {
4070 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko7f45cf22020-10-01 12:49:44 +02004071 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
Radek Krejci76b3e962018-12-14 17:01:25 +01004072 return LY_EVALID;
4073 }
4074 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004075
Michal Vasko7f45cf22020-10-01 12:49:44 +02004076 if (ch->flags & LYS_MAND_TRUE) {
4077 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Radek Krejciccd20f12019-02-15 14:12:27 +01004078 return LY_EVALID;
4079 }
4080
Michal Vasko7f45cf22020-10-01 12:49:44 +02004081 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01004082 return LY_SUCCESS;
4083}
4084
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004085/**
Michal Vasko20424b42020-08-31 12:29:38 +02004086 * @brief Compile choice children.
4087 *
4088 * @param[in] ctx Compile context
4089 * @param[in] child_p Parsed choice children nodes.
4090 * @param[in] node Compiled choice node to compile and add children to.
4091 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4092 */
4093static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02004094lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
4095 struct ly_set *child_set)
Michal Vasko20424b42020-08-31 12:29:38 +02004096{
4097 LY_ERR ret = LY_SUCCESS;
Radek Krejci8f5fad22020-09-15 16:50:54 +02004098 struct lysp_node *child_p_next = child_p->next;
Michal Vasko20424b42020-08-31 12:29:38 +02004099 struct lysp_node_case *cs_p;
4100
4101 if (child_p->nodetype == LYS_CASE) {
4102 /* standard case under choice */
Michal Vasko7f45cf22020-10-01 12:49:44 +02004103 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
Michal Vasko20424b42020-08-31 12:29:38 +02004104 } else {
4105 /* we need the implicit case first, so create a fake parsed case */
4106 cs_p = calloc(1, sizeof *cs_p);
4107 cs_p->nodetype = LYS_CASE;
Radek Krejci87e25252020-09-15 13:28:31 +02004108 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
Michal Vasko20424b42020-08-31 12:29:38 +02004109 cs_p->child = child_p;
4110
4111 /* make the child the only case child */
Michal Vasko20424b42020-08-31 12:29:38 +02004112 child_p->next = NULL;
4113
4114 /* compile it normally */
Michal Vasko7f45cf22020-10-01 12:49:44 +02004115 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
Michal Vasko20424b42020-08-31 12:29:38 +02004116
Radek Krejci87e25252020-09-15 13:28:31 +02004117free_fake_node:
Michal Vasko20424b42020-08-31 12:29:38 +02004118 /* free the fake parsed node and correct pointers back */
4119 cs_p->child = NULL;
4120 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
Radek Krejci8f5fad22020-09-15 16:50:54 +02004121 child_p->next = child_p_next;
Michal Vasko20424b42020-08-31 12:29:38 +02004122 }
4123
4124 return ret;
4125}
4126
4127/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004128 * @brief Compile parsed choice node information.
Michal Vasko20424b42020-08-31 12:29:38 +02004129 *
Radek Krejci056d0a82018-12-06 16:57:25 +01004130 * @param[in] ctx Compile context
Michal Vasko7f45cf22020-10-01 12:49:44 +02004131 * @param[in] pnode Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004132 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004133 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004134 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4135 */
4136static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02004137lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004138{
Michal Vasko7f45cf22020-10-01 12:49:44 +02004139 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
Michal Vasko22df3f02020-08-24 13:29:22 +02004140 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
Michal Vasko20424b42020-08-31 12:29:38 +02004141 struct lysp_node *child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004142 LY_ERR ret = LY_SUCCESS;
4143
Michal Vasko20424b42020-08-31 12:29:38 +02004144 assert(node->nodetype == LYS_CHOICE);
4145
Radek Krejci056d0a82018-12-06 16:57:25 +01004146 LY_LIST_FOR(ch_p->child, child_p) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02004147 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
Radek Krejci056d0a82018-12-06 16:57:25 +01004148 }
4149
4150 /* default branch */
Michal Vasko7f45cf22020-10-01 12:49:44 +02004151 if (ch_p->dflt.str) {
4152 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004153 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004154
Radek Krejci9800fb82018-12-13 14:26:23 +01004155 return ret;
4156}
4157
4158/**
4159 * @brief Compile parsed anydata or anyxml node information.
4160 * @param[in] ctx Compile context
Michal Vasko7f45cf22020-10-01 12:49:44 +02004161 * @param[in] pnode Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004162 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4163 * is enriched with the any-specific information.
4164 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4165 */
4166static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02004167lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004168{
Michal Vasko7f45cf22020-10-01 12:49:44 +02004169 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
Michal Vasko22df3f02020-08-24 13:29:22 +02004170 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004171 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9800fb82018-12-13 14:26:23 +01004172 LY_ERR ret = LY_SUCCESS;
4173
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004174 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004175 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4176 /* do not check "must" semantics in a grouping */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004177 ret = ly_set_add(&ctx->xpath, any, 0, NULL);
4178 LY_CHECK_GOTO(ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004179 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004180
4181 if (any->flags & LYS_CONFIG_W) {
Radek Krejci5c4ed7b2020-08-12 11:29:44 +02004182 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s",
4183 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Radek Krejci9800fb82018-12-13 14:26:23 +01004184 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004185done:
4186 return ret;
4187}
4188
Radek Krejcib56c7502019-02-13 14:19:54 +01004189/**
Michal Vasko795b3752020-07-13 15:24:27 +02004190 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
4191 * keep specific order of augments targetting the same node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004192 *
4193 * @param[in] ctx Compile context
4194 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4195 * the choice itself is expected instead of a specific case node.
4196 * @param[in] node Schema node to connect into the list.
4197 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
Radek Krejci1c54f462020-05-12 17:25:34 +02004198 * 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 +01004199 */
4200static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004201lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004202{
Michal Vasko7f45cf22020-10-01 12:49:44 +02004203 struct lysc_node **children, *anchor = NULL;
4204 int insert_after = 0;
Radek Krejci056d0a82018-12-06 16:57:25 +01004205
Michal Vasko20424b42020-08-31 12:29:38 +02004206 node->parent = parent;
4207
4208 if (parent) {
4209 if (parent->nodetype == LYS_CHOICE) {
4210 assert(node->nodetype == LYS_CASE);
4211 children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases;
4212 } else {
4213 children = lysc_node_children_p(parent, ctx->options);
4214 }
4215 assert(children);
4216
Radek Krejci056d0a82018-12-06 16:57:25 +01004217 if (!(*children)) {
4218 /* first child */
4219 *children = node;
Michal Vasko7f45cf22020-10-01 12:49:44 +02004220 } else if (node->flags & LYS_KEY) {
4221 /* special handling of adding keys */
4222 assert(node->module == parent->module);
4223 anchor = *children;
4224 if (anchor->flags & LYS_KEY) {
4225 while ((anchor->flags & LYS_KEY) && anchor->next) {
4226 anchor = anchor->next;
4227 }
4228 /* insert after the last key */
4229 insert_after = 1;
4230 } /* else insert before anchor (at the beginning) */
4231 } else if ((*children)->prev->module == node->module) {
4232 /* last child is from the same module, keep the order and insert at the end */
4233 anchor = (*children)->prev;
4234 insert_after = 1;
4235 } else if (parent->module == node->module) {
4236 /* adding module child after some augments were connected */
4237 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
4238 } else {
4239 /* some augments are already connected and we are connecting new ones,
4240 * keep module name order and insert the node into the children list */
4241 anchor = *children;
4242 do {
4243 anchor = anchor->prev;
Michal Vasko795b3752020-07-13 15:24:27 +02004244
Michal Vasko7f45cf22020-10-01 12:49:44 +02004245 /* check that we have not found the last augment node from our module or
4246 * the first augment node from a "smaller" module or
4247 * the first node from a local module */
4248 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0)
4249 || (anchor->module == parent->module)) {
4250 /* insert after */
4251 insert_after = 1;
4252 break;
4253 }
4254
4255 /* we have traversed all the nodes, insert before anchor (as the first node) */
4256 } while (anchor->prev->next);
4257 }
4258
4259 /* insert */
4260 if (anchor) {
4261 if (insert_after) {
4262 node->next = anchor->next;
4263 node->prev = anchor;
4264 anchor->next = node;
4265 if (node->next) {
4266 /* middle node */
4267 node->next->prev = node;
4268 } else {
4269 /* last node */
4270 (*children)->prev = node;
4271 }
Michal Vasko795b3752020-07-13 15:24:27 +02004272 } else {
Michal Vasko7f45cf22020-10-01 12:49:44 +02004273 node->next = anchor;
4274 node->prev = anchor->prev;
4275 anchor->prev = node;
4276 if (anchor == *children) {
4277 /* first node */
4278 *children = node;
4279 } else {
4280 /* middle node */
4281 node->prev->next = node;
4282 }
Michal Vasko795b3752020-07-13 15:24:27 +02004283 }
Michal Vasko20424b42020-08-31 12:29:38 +02004284 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004285
Michal Vasko20424b42020-08-31 12:29:38 +02004286 /* check the name uniqueness (even for an only child, it may be in case) */
4287 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
4288 return LY_EEXIST;
4289 }
4290 } else {
4291 /* top-level element */
4292 if (!ctx->mod->compiled->data) {
4293 ctx->mod->compiled->data = node;
4294 } else {
4295 /* insert at the end of the module's top-level nodes list */
4296 ctx->mod->compiled->data->prev->next = node;
4297 node->prev = ctx->mod->compiled->data->prev;
4298 ctx->mod->compiled->data->prev = node;
4299 }
4300
4301 /* check the name uniqueness on top-level */
4302 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
4303 return LY_EEXIST;
Radek Krejci056d0a82018-12-06 16:57:25 +01004304 }
4305 }
Michal Vasko20424b42020-08-31 12:29:38 +02004306
Radek Krejci056d0a82018-12-06 16:57:25 +01004307 return LY_SUCCESS;
4308}
4309
Radek Krejci95710c92019-02-11 15:49:55 +01004310/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004311 * @brief Prepare the case structure in choice node for the new data node.
4312 *
4313 * 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
4314 * created in the choice when the first child was processed.
4315 *
4316 * @param[in] ctx Compile context.
Michal Vasko7f45cf22020-10-01 12:49:44 +02004317 * @param[in] pnode Node image from the parsed tree. If the case is explicit, it is the LYS_CASE node, but in case of implicit case,
Radek Krejci39424802020-08-12 09:31:00 +02004318 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004319 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4320 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4321 * @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,
4322 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004323 */
Michal Vasko20424b42020-08-31 12:29:38 +02004324static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02004325lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004326{
Michal Vasko20424b42020-08-31 12:29:38 +02004327 struct lysp_node *child_p;
Michal Vasko7f45cf22020-10-01 12:49:44 +02004328 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
Radek Krejci056d0a82018-12-06 16:57:25 +01004329
Michal Vasko7f45cf22020-10-01 12:49:44 +02004330 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004331 /* we have to add an implicit case node into the parent choice */
Michal Vasko7f45cf22020-10-01 12:49:44 +02004332 } else if (pnode->nodetype == LYS_CASE) {
Michal Vasko20424b42020-08-31 12:29:38 +02004333 /* explicit parent case */
4334 LY_LIST_FOR(cs_p->child, child_p) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02004335 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
Radek Krejci056d0a82018-12-06 16:57:25 +01004336 }
Radek Krejci95710c92019-02-11 15:49:55 +01004337 } else {
Michal Vasko20424b42020-08-31 12:29:38 +02004338 LOGINT_RET(ctx->ctx);
Radek Krejci056d0a82018-12-06 16:57:25 +01004339 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004340
Michal Vasko20424b42020-08-31 12:29:38 +02004341 return LY_SUCCESS;
Radek Krejci056d0a82018-12-06 16:57:25 +01004342}
4343
Radek Krejcib56c7502019-02-13 14:19:54 +01004344/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004345 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4346 *
4347 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4348 * the flag to such parents from a mandatory children.
4349 *
4350 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4351 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4352 * (mandatory children was removed).
4353 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02004354static void
Radek Krejci857189e2020-09-01 13:26:36 +02004355lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
Radek Krejcife909632019-02-12 15:34:42 +01004356{
4357 struct lysc_node *iter;
4358
4359 if (add) { /* set flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004360 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
Radek Krejcife909632019-02-12 15:34:42 +01004361 parent = parent->parent) {
4362 parent->flags |= LYS_MAND_TRUE;
4363 }
4364 } else { /* unset flag */
Michal Vaskod989ba02020-08-24 10:59:24 +02004365 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko22df3f02020-08-24 13:29:22 +02004366 for (iter = (struct lysc_node *)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004367 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004368 /* there is another mandatory node */
4369 return;
4370 }
4371 }
4372 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4373 parent->flags &= ~LYS_MAND_TRUE;
4374 }
4375 }
4376}
4377
Radek Krejci056d0a82018-12-06 16:57:25 +01004378/**
Radek Krejci3641f562019-02-13 15:38:40 +01004379 * @brief Compile the parsed augment connecting it into its target.
4380 *
4381 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4382 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4383 * are already implemented and compiled.
4384 *
4385 * @param[in] ctx Compile context.
4386 * @param[in] aug_p Parsed augment to compile.
Michal Vasko7f45cf22020-10-01 12:49:44 +02004387 * @param[in] target Target node of the augment.
Radek Krejci3641f562019-02-13 15:38:40 +01004388 * @return LY_SUCCESS on success.
4389 * @return LY_EVALID on failure.
4390 */
Michal Vasko7f45cf22020-10-01 12:49:44 +02004391static LY_ERR
4392lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysc_node *target)
Radek Krejci3641f562019-02-13 15:38:40 +01004393{
Michal Vasko7f45cf22020-10-01 12:49:44 +02004394 LY_ERR ret = LY_SUCCESS;
4395 struct lysp_node *pnode;
Radek Krejci3641f562019-02-13 15:38:40 +01004396 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004397 struct lysc_when **when, *when_shared;
Radek Krejci857189e2020-09-01 13:26:36 +02004398 ly_bool allow_mandatory = 0;
Michal Vasko7f45cf22020-10-01 12:49:44 +02004399 LY_ARRAY_COUNT_TYPE u;
4400 struct ly_set child_set = {0};
4401 uint32_t i;
Radek Krejci3641f562019-02-13 15:38:40 +01004402
Michal Vasko7f45cf22020-10-01 12:49:44 +02004403 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
4404 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4405 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4406 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4407 ret = LY_EVALID;
4408 goto cleanup;
Radek Krejci3641f562019-02-13 15:38:40 +01004409 }
4410
4411 /* check for mandatory nodes
4412 * - new cases augmenting some choice can have mandatory nodes
4413 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4414 */
Radek Krejci733988a2019-02-15 15:12:44 +01004415 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004416 allow_mandatory = 1;
4417 }
4418
4419 when_shared = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +02004420 LY_LIST_FOR(aug_p->child, pnode) {
Radek Krejci3641f562019-02-13 15:38:40 +01004421 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
Michal Vasko7f45cf22020-10-01 12:49:44 +02004422 if ((pnode->nodetype == LYS_CASE && target->nodetype != LYS_CHOICE)
4423 || ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST)))
4424 || (pnode->nodetype == LYS_USES && target->nodetype == LYS_CHOICE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004425 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004426 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
Michal Vasko7f45cf22020-10-01 12:49:44 +02004427 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
4428 ret = LY_EVALID;
4429 goto cleanup;
Radek Krejci3641f562019-02-13 15:38:40 +01004430 }
4431
4432 /* compile the children */
Michal Vasko20424b42020-08-31 12:29:38 +02004433 if (target->nodetype == LYS_CHOICE) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02004434 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004435 } else {
Michal Vasko7f45cf22020-10-01 12:49:44 +02004436 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004437 }
4438
Michal Vasko7f45cf22020-10-01 12:49:44 +02004439 /* since the augment node is not present in the compiled tree, we need to pass some of its
4440 * statements to all its children */
4441 for (i = 0; i < child_set.count; ++i) {
4442 node = child_set.snodes[i];
4443 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
4444 node->flags &= ~LYS_MAND_TRUE;
4445 lys_compile_mandatory_parents(target, 0);
4446 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4447 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
4448 ret = LY_EVALID;
4449 goto cleanup;
Radek Krejci7c7783d2020-04-08 15:34:39 +02004450 }
Radek Krejci3641f562019-02-13 15:38:40 +01004451
Michal Vasko7f45cf22020-10-01 12:49:44 +02004452 /* pass augment's when to all the children TODO this way even action and notif should have "when" (code below) */
4453 if (aug_p->when) {
4454 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, cleanup);
4455 if (!when_shared) {
4456 LY_CHECK_GOTO(ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004457
Michal Vasko7f45cf22020-10-01 12:49:44 +02004458 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4459 /* do not check "when" semantics in a grouping */
4460 LY_CHECK_GOTO(ret = ly_set_add(&ctx->xpath, node, 0, NULL), cleanup);
4461 }
Michal Vasko175012e2019-11-06 15:49:14 +01004462
Michal Vasko7f45cf22020-10-01 12:49:44 +02004463 when_shared = *when;
4464 } else {
4465 ++when_shared->refcount;
4466 (*when) = when_shared;
Michal Vasko175012e2019-11-06 15:49:14 +01004467
Michal Vasko7f45cf22020-10-01 12:49:44 +02004468 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4469 /* in this case check "when" again for all children because of dummy node check */
4470 LY_CHECK_GOTO(ret = ly_set_add(&ctx->xpath, node, 0, NULL), cleanup);
4471 }
Michal Vasko5c4e5892019-11-14 12:31:38 +01004472 }
Radek Krejci3641f562019-02-13 15:38:40 +01004473 }
4474 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02004475 ly_set_erase(&child_set, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004476 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004477
4478 switch (target->nodetype) {
4479 case LYS_CONTAINER:
Michal Vasko7f45cf22020-10-01 12:49:44 +02004480 COMPILE_OP_ARRAY_GOTO(ctx, aug_p->actions, ((struct lysc_node_container *)target)->actions, target,
4481 u, lys_compile_action, 0, ret, cleanup);
4482 COMPILE_OP_ARRAY_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container *)target)->notifs, target,
4483 u, lys_compile_notif, 0, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004484 break;
4485 case LYS_LIST:
Michal Vasko7f45cf22020-10-01 12:49:44 +02004486 COMPILE_OP_ARRAY_GOTO(ctx, aug_p->actions, ((struct lysc_node_list *)target)->actions, target,
4487 u, lys_compile_action, 0, ret, cleanup);
4488 COMPILE_OP_ARRAY_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list *)target)->notifs, target,
4489 u, lys_compile_notif, 0, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004490 break;
4491 default:
4492 if (aug_p->actions) {
4493 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004494 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4495 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Michal Vasko7f45cf22020-10-01 12:49:44 +02004496 ret = LY_EVALID;
4497 goto cleanup;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004498 }
4499 if (aug_p->notifs) {
4500 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004501 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004502 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Michal Vasko7f45cf22020-10-01 12:49:44 +02004503 ret = LY_EVALID;
4504 goto cleanup;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004505 }
4506 }
Radek Krejci3641f562019-02-13 15:38:40 +01004507
Michal Vasko7f45cf22020-10-01 12:49:44 +02004508cleanup:
4509 ly_set_erase(&child_set, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004510 return ret;
4511}
4512
4513/**
Michal Vasko601ddb32020-08-31 16:25:35 +02004514 * @brief Find grouping for a uses.
Radek Krejcie86bf772018-12-14 11:39:53 +01004515 *
Michal Vasko601ddb32020-08-31 16:25:35 +02004516 * @param[in] ctx Compile context.
4517 * @param[in] uses_p Parsed uses node.
4518 * @param[out] gpr_p Found grouping on success.
4519 * @param[out] grp_mod Module of @p grp_p on success.
4520 * @return LY_ERR value.
Radek Krejcie86bf772018-12-14 11:39:53 +01004521 */
4522static LY_ERR
Michal Vasko601ddb32020-08-31 16:25:35 +02004523lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p,
4524 struct lys_module **grp_mod)
Radek Krejcie86bf772018-12-14 11:39:53 +01004525{
Michal Vasko7f45cf22020-10-01 12:49:44 +02004526 struct lysp_node *pnode;
Michal Vasko601ddb32020-08-31 16:25:35 +02004527 struct lysp_grp *grp;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02004528 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci857189e2020-09-01 13:26:36 +02004529 ly_bool found = 0;
Radek Krejcie86bf772018-12-14 11:39:53 +01004530 const char *id, *name, *prefix;
4531 size_t prefix_len, name_len;
Michal Vasko601ddb32020-08-31 16:25:35 +02004532 struct lys_module *mod;
4533
4534 *grp_p = NULL;
4535 *grp_mod = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004536
4537 /* search for the grouping definition */
Radek Krejcie86bf772018-12-14 11:39:53 +01004538 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004539 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004540 if (prefix) {
4541 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4542 if (!mod) {
4543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004544 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004545 return LY_EVALID;
4546 }
4547 } else {
4548 mod = ctx->mod_def;
4549 }
4550 if (mod == ctx->mod_def) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02004551 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
4552 grp = (struct lysp_grp *)lysp_node_groupings(pnode);
Radek Krejcie86bf772018-12-14 11:39:53 +01004553 LY_ARRAY_FOR(grp, u) {
4554 if (!strcmp(grp[u].name, name)) {
4555 grp = &grp[u];
4556 found = 1;
4557 break;
4558 }
4559 }
4560 }
4561 }
4562 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004563 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004564 grp = mod->parsed->groupings;
Michal Vasko601ddb32020-08-31 16:25:35 +02004565 LY_ARRAY_FOR(grp, u) {
4566 if (!strcmp(grp[u].name, name)) {
4567 grp = &grp[u];
4568 found = 1;
4569 break;
Radek Krejci76b3e962018-12-14 17:01:25 +01004570 }
4571 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004572 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004573 /* ... and all the submodules */
Michal Vasko601ddb32020-08-31 16:25:35 +02004574 LY_ARRAY_FOR(mod->parsed->includes, u) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004575 grp = mod->parsed->includes[u].submodule->groupings;
Michal Vasko601ddb32020-08-31 16:25:35 +02004576 LY_ARRAY_FOR(grp, v) {
4577 if (!strcmp(grp[v].name, name)) {
4578 grp = &grp[v];
4579 found = 1;
4580 break;
Radek Krejci76b3e962018-12-14 17:01:25 +01004581 }
4582 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004583 if (found) {
4584 break;
4585 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004586 }
4587 }
4588 }
4589 if (!found) {
4590 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4591 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4592 return LY_EVALID;
4593 }
4594
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004595 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4596 /* remember that the grouping is instantiated to avoid its standalone validation */
4597 grp->flags |= LYS_USED_GRP;
4598 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004599
Michal Vasko601ddb32020-08-31 16:25:35 +02004600 *grp_p = grp;
4601 *grp_mod = mod;
4602 return LY_SUCCESS;
4603}
Radek Krejcie86bf772018-12-14 11:39:53 +01004604
Michal Vasko7f45cf22020-10-01 12:49:44 +02004605static const struct lys_module *lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest,
4606 size_t nametest_len, const struct lys_module *local_mod, const char **name, size_t *name_len);
4607
Michal Vasko601ddb32020-08-31 16:25:35 +02004608static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02004609lys_nodeid_check(struct lysc_ctx *ctx, const char *nodeid, ly_bool abs, struct lys_module **target_mod,
4610 struct lyxp_expr **expr)
Michal Vasko601ddb32020-08-31 16:25:35 +02004611{
Michal Vasko601ddb32020-08-31 16:25:35 +02004612 LY_ERR ret = LY_SUCCESS;
Michal Vasko7f45cf22020-10-01 12:49:44 +02004613 struct lyxp_expr *e = NULL;
4614 struct lys_module *tmod = NULL, *mod;
4615 const char *nodeid_type = abs ? "absolute-schema-nodeid" : "descendant-schema-nodeid";
4616 uint32_t i;
Radek Krejcie86bf772018-12-14 11:39:53 +01004617
Michal Vasko7f45cf22020-10-01 12:49:44 +02004618 /* parse */
4619 ret = lyxp_expr_parse(ctx->ctx, nodeid, strlen(nodeid), 0, &e);
4620 if (ret) {
4621 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s value \"%s\" - invalid syntax.",
4622 nodeid_type, nodeid);
4623 ret = LY_EVALID;
4624 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004625 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004626
Michal Vasko7f45cf22020-10-01 12:49:44 +02004627 if (abs) {
4628 /* absolute schema nodeid */
4629 i = 0;
4630 } else {
4631 /* descendant schema nodeid */
4632 if (e->tokens[0] != LYXP_TOKEN_NAMETEST) {
4633 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
4634 nodeid_type, nodeid, e->tok_len[0], e->expr + e->tok_pos[0]);
Michal Vasko20424b42020-08-31 12:29:38 +02004635 ret = LY_EVALID;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004636 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004637 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02004638 i = 1;
4639 }
Radek Krejcif2271f12019-01-07 16:42:23 +01004640
Michal Vasko7f45cf22020-10-01 12:49:44 +02004641 /* check all the tokens */
4642 for ( ; i < e->used; i += 2) {
4643 if (e->tokens[i] != LYXP_TOKEN_OPER_PATH) {
4644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s value \"%s\" - \"/\" expected instead of \"%.*s\".",
4645 nodeid_type, nodeid, e->tok_len[i], e->expr + e->tok_pos[i]);
4646 ret = LY_EVALID;
4647 goto cleanup;
4648 } else if (e->used == i + 1) {
4649 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4650 "Invalid %s value \"%s\" - unexpected end of expression.", nodeid_type, e->expr);
4651 ret = LY_EVALID;
4652 goto cleanup;
4653 } else if (e->tokens[i + 1] != LYXP_TOKEN_NAMETEST) {
4654 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
4655 nodeid_type, nodeid, e->tok_len[i + 1], e->expr + e->tok_pos[i + 1]);
4656 ret = LY_EVALID;
4657 goto cleanup;
4658 } else if (abs) {
4659 mod = (struct lys_module *)lys_schema_node_get_module(ctx->ctx, e->expr + e->tok_pos[i + 1],
4660 e->tok_len[i + 1], ctx->mod_def, NULL, NULL);
4661 LY_CHECK_ERR_GOTO(!mod, ret = LY_EVALID, cleanup);
4662
4663 /* only keep the first module */
4664 if (!tmod) {
4665 tmod = mod;
Radek Krejcif2271f12019-01-07 16:42:23 +01004666 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02004667
4668 /* all the modules must be implemented */
4669 if (!mod->implemented) {
4670 ret = lys_set_implemented_internal(mod, ctx->ctx->module_set_id);
4671 LY_CHECK_GOTO(ret, cleanup);
4672 }
4673 }
4674 }
4675
4676cleanup:
4677 if (ret || !expr) {
4678 lyxp_expr_free(ctx->ctx, e);
4679 e = NULL;
4680 }
4681 if (expr) {
4682 *expr = ret ? NULL : e;
4683 }
4684 if (target_mod) {
4685 *target_mod = ret ? NULL : tmod;
4686 }
4687 return ret;
4688}
4689
4690/**
4691 * @brief Check whether 2 schema nodeids match.
4692 *
4693 * @param[in] ctx libyang context.
4694 * @param[in] exp1 First schema nodeid.
4695 * @param[in] exp1_mod Module of @p exp1 nodes without any prefix.
4696 * @param[in] exp2 Second schema nodeid.
4697 * @param[in] exp2_mod Module of @p exp2 nodes without any prefix.
4698 * @return Whether the schema nodeids match or not.
4699 */
4700static ly_bool
4701lys_abs_schema_nodeid_match(const struct ly_ctx *ctx, const struct lyxp_expr *exp1, const struct lys_module *exp1_mod,
4702 const struct lyxp_expr *exp2, const struct lys_module *exp2_mod)
4703{
4704 uint32_t i;
4705 const struct lys_module *mod1, *mod2;
4706 const char *name1, *name2;
4707 size_t name1_len, name2_len;
4708
4709 if (exp1->used != exp2->used) {
4710 return 0;
4711 }
4712
4713 for (i = 0; i < exp1->used; ++i) {
4714 assert(exp1->tokens[i] == exp2->tokens[i]);
4715
4716 if (exp1->tokens[i] == LYXP_TOKEN_NAMETEST) {
4717 /* check modules of all the nodes in the node ID */
4718 mod1 = lys_schema_node_get_module(ctx, exp1->expr + exp1->tok_pos[i], exp1->tok_len[i], exp1_mod,
4719 &name1, &name1_len);
4720 assert(mod1);
4721 mod2 = lys_schema_node_get_module(ctx, exp2->expr + exp2->tok_pos[i], exp2->tok_len[i], exp2_mod,
4722 &name2, &name2_len);
4723 assert(mod2);
4724
4725 /* compare modules */
4726 if (mod1 != mod2) {
4727 return 0;
4728 }
4729
4730 /* compare names */
4731 if ((name1_len != name2_len) || strncmp(name1, name2, name1_len)) {
4732 return 0;
4733 }
4734 }
4735 }
4736
4737 return 1;
4738}
4739
4740/**
4741 * @brief Prepare any uses augments and refines in the context to be applied during uses descendant node compilation.
4742 *
4743 * @param[in] ctx Compile context.
4744 * @param[in] uses_p Parsed uses structure with augments and refines.
4745 * @param[in] ctx_node Context node of @p uses_p meaning its first data definiition parent.
4746 * @return LY_ERR value.
4747 */
4748static LY_ERR
4749lys_precompile_uses_augments_refines(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, const struct lysc_node *ctx_node)
4750{
4751 LY_ERR ret = LY_SUCCESS;
4752 LY_ARRAY_COUNT_TYPE u;
4753 struct lyxp_expr *exp = NULL;
4754 struct lysc_augment *aug;
4755 struct lysc_refine *rfn;
4756 struct lysp_refine **new_rfn;
4757 uint32_t i;
4758
4759 LY_ARRAY_FOR(uses_p->augments, u) {
4760 lysc_update_path(ctx, NULL, "{augment}");
4761 lysc_update_path(ctx, NULL, uses_p->augments[u].nodeid);
4762
4763 /* parse the nodeid */
4764 LY_CHECK_GOTO(ret = lys_nodeid_check(ctx, uses_p->augments[u].nodeid, 0, NULL, &exp), cleanup);
4765
4766 /* allocate new compiled augment and store it in the set */
4767 aug = calloc(1, sizeof *aug);
4768 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
4769 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_augs, aug, LY_SET_OPT_USEASLIST, NULL), cleanup);
4770
4771 aug->nodeid = exp;
4772 exp = NULL;
4773 aug->nodeid_ctx_node = ctx_node;
4774 aug->aug_p = &uses_p->augments[u];
4775
4776 lysc_update_path(ctx, NULL, NULL);
4777 lysc_update_path(ctx, NULL, NULL);
4778 }
4779
4780 LY_ARRAY_FOR(uses_p->refines, u) {
4781 lysc_update_path(ctx, NULL, "{refine}");
4782 lysc_update_path(ctx, NULL, uses_p->refines[u].nodeid);
4783
4784 /* parse the nodeid */
4785 LY_CHECK_GOTO(ret = lys_nodeid_check(ctx, uses_p->refines[u].nodeid, 0, NULL, &exp), cleanup);
4786
4787 /* try to find the node in already compiled refines */
4788 rfn = NULL;
4789 for (i = 0; i < ctx->uses_rfns.count; ++i) {
4790 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, ctx->mod_def, ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid,
4791 ctx->mod_def)) {
4792 rfn = ctx->uses_rfns.objs[i];
4793 break;
Radek Krejcif2271f12019-01-07 16:42:23 +01004794 }
4795 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004796
Michal Vasko7f45cf22020-10-01 12:49:44 +02004797 if (!rfn) {
4798 /* allocate new compiled refine */
4799 rfn = calloc(1, sizeof *rfn);
4800 LY_CHECK_ERR_GOTO(!rfn, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
4801 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_rfns, rfn, LY_SET_OPT_USEASLIST, NULL), cleanup);
4802
4803 rfn->nodeid = exp;
4804 exp = NULL;
4805 rfn->nodeid_ctx_node = ctx_node;
4806 } else {
4807 /* just free exp */
4808 lyxp_expr_free(ctx->ctx, exp);
4809 exp = NULL;
4810 }
4811
4812 /* add new parsed refine structure */
4813 LY_ARRAY_NEW_GOTO(ctx->ctx, rfn->rfns, new_rfn, ret, cleanup);
4814 *new_rfn = &uses_p->refines[u];
4815
4816 lysc_update_path(ctx, NULL, NULL);
Michal Vasko601ddb32020-08-31 16:25:35 +02004817 lysc_update_path(ctx, NULL, NULL);
Radek Krejcif2271f12019-01-07 16:42:23 +01004818 }
4819
Michal Vasko601ddb32020-08-31 16:25:35 +02004820cleanup:
Michal Vasko7f45cf22020-10-01 12:49:44 +02004821 lyxp_expr_free(ctx->ctx, exp);
Michal Vasko601ddb32020-08-31 16:25:35 +02004822 return ret;
4823}
4824
4825/**
4826 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4827 * If present, also apply uses's modificators.
4828 *
4829 * @param[in] ctx Compile context
4830 * @param[in] uses_p Parsed uses schema node.
4831 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4832 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4833 * the compile context.
4834 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4835 */
4836static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02004837lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
Michal Vasko601ddb32020-08-31 16:25:35 +02004838{
Michal Vasko7f45cf22020-10-01 12:49:44 +02004839 struct lysp_node *pnode;
4840 struct lysc_node *child;
Michal Vasko601ddb32020-08-31 16:25:35 +02004841 struct lysp_grp *grp = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +02004842 uint32_t i, grp_stack_count;
Michal Vaskoaf702452020-10-02 09:02:55 +02004843 struct lys_module *grp_mod, *mod_old = ctx->mod_def;
Michal Vasko601ddb32020-08-31 16:25:35 +02004844 LY_ERR ret = LY_SUCCESS;
4845 struct lysc_when **when, *when_shared;
Michal Vasko7f45cf22020-10-01 12:49:44 +02004846 LY_ARRAY_COUNT_TYPE u;
Michal Vasko601ddb32020-08-31 16:25:35 +02004847 struct lysc_notif **notifs = NULL;
4848 struct lysc_action **actions = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +02004849 struct ly_set uses_child_set = {0};
Michal Vasko601ddb32020-08-31 16:25:35 +02004850
4851 /* find the referenced grouping */
4852 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
4853
4854 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4855 grp_stack_count = ctx->groupings.count;
4856 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
4857 if (grp_stack_count == ctx->groupings.count) {
4858 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4859 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4860 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4861 return LY_EVALID;
4862 }
4863
Michal Vasko7f45cf22020-10-01 12:49:44 +02004864 /* check status */
4865 ret = lysc_check_status(ctx, uses_p->flags, ctx->mod_def, uses_p->name, grp->flags, grp_mod, grp->name);
4866 LY_CHECK_GOTO(ret, cleanup);
4867
4868 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
4869 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
4870 LY_CHECK_GOTO(ret, cleanup);
4871
Michal Vasko601ddb32020-08-31 16:25:35 +02004872 /* switch context's mod_def */
Michal Vasko601ddb32020-08-31 16:25:35 +02004873 ctx->mod_def = grp_mod;
4874
Michal Vasko601ddb32020-08-31 16:25:35 +02004875 /* compile data nodes */
Michal Vasko7f45cf22020-10-01 12:49:44 +02004876 LY_LIST_FOR(grp->data, pnode) {
Michal Vasko601ddb32020-08-31 16:25:35 +02004877 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
Michal Vasko7f45cf22020-10-01 12:49:44 +02004878 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3, &uses_child_set);
Michal Vasko601ddb32020-08-31 16:25:35 +02004879 LY_CHECK_GOTO(ret, cleanup);
4880 }
4881
Michal Vasko7f45cf22020-10-01 12:49:44 +02004882 if (child_set) {
4883 /* add these children to our compiled child_set as well since uses is a schema-only node */
4884 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, LY_SET_OPT_USEASLIST, NULL), cleanup);
Michal Vasko601ddb32020-08-31 16:25:35 +02004885 }
4886
Michal Vasko7f45cf22020-10-01 12:49:44 +02004887 if (uses_p->when) {
Michal Vasko601ddb32020-08-31 16:25:35 +02004888 /* pass uses's when to all the data children, actions and notifications are ignored */
Michal Vasko7f45cf22020-10-01 12:49:44 +02004889 when_shared = NULL;
4890 for (i = 0; i < uses_child_set.count; ++i) {
4891 child = uses_child_set.snodes[i];
4892
4893 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Michal Vasko601ddb32020-08-31 16:25:35 +02004894 if (!when_shared) {
4895 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when);
4896 LY_CHECK_GOTO(ret, cleanup);
4897
4898 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4899 /* do not check "when" semantics in a grouping */
Michal Vasko7f45cf22020-10-01 12:49:44 +02004900 ret = ly_set_add(&ctx->xpath, child, 0, NULL);
Michal Vasko601ddb32020-08-31 16:25:35 +02004901 LY_CHECK_GOTO(ret, cleanup);
4902 }
4903
4904 when_shared = *when;
4905 } else {
4906 ++when_shared->refcount;
4907 (*when) = when_shared;
4908
4909 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4910 /* in this case check "when" again for all children because of dummy node check */
Michal Vasko7f45cf22020-10-01 12:49:44 +02004911 ret = ly_set_add(&ctx->xpath, child, 0, NULL);
Michal Vasko601ddb32020-08-31 16:25:35 +02004912 LY_CHECK_GOTO(ret, cleanup);
4913 }
4914 }
4915 }
4916 }
4917
4918 /* compile actions */
Michal Vasko7f45cf22020-10-01 12:49:44 +02004919 if (grp->actions) {
4920 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4921 if (!actions) {
4922 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
4923 grp->actions[0].name, lys_nodetype2str(grp->actions[0].nodetype), parent->name,
4924 lys_nodetype2str(parent->nodetype));
4925 ret = LY_EVALID;
4926 goto cleanup;
Michal Vasko601ddb32020-08-31 16:25:35 +02004927 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02004928 COMPILE_OP_ARRAY_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Michal Vasko601ddb32020-08-31 16:25:35 +02004929 }
4930
4931 /* compile notifications */
Michal Vasko7f45cf22020-10-01 12:49:44 +02004932 if (grp->notifs) {
4933 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4934 if (!notifs) {
4935 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
4936 grp->notifs[0].name, lys_nodetype2str(grp->notifs[0].nodetype), parent->name,
4937 lys_nodetype2str(parent->nodetype));
4938 ret = LY_EVALID;
4939 goto cleanup;
Michal Vasko601ddb32020-08-31 16:25:35 +02004940 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02004941 COMPILE_OP_ARRAY_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Michal Vasko601ddb32020-08-31 16:25:35 +02004942 }
4943
Michal Vasko7f45cf22020-10-01 12:49:44 +02004944 /* check that all augments were applied */
4945 for (i = 0; i < ctx->uses_augs.count; ++i) {
4946 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4947 "Augment target node \"%s\" in grouping \"%s\" was not found.",
4948 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
4949 ret = LY_ENOTFOUND;
Michal Vasko601ddb32020-08-31 16:25:35 +02004950 }
Michal Vasko601ddb32020-08-31 16:25:35 +02004951 LY_CHECK_GOTO(ret, cleanup);
4952
Michal Vasko7f45cf22020-10-01 12:49:44 +02004953 /* check that all refines were applied */
4954 for (i = 0; i < ctx->uses_rfns.count; ++i) {
4955 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4956 "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
4957 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
4958 ret = LY_ENOTFOUND;
Radek Krejcic6b4f442020-08-12 14:45:18 +02004959 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02004960 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004961
4962cleanup:
Radek Krejcie86bf772018-12-14 11:39:53 +01004963 /* reload previous context's mod_def */
4964 ctx->mod_def = mod_old;
Michal Vasko7f45cf22020-10-01 12:49:44 +02004965
Radek Krejcie86bf772018-12-14 11:39:53 +01004966 /* remove the grouping from the stack for circular groupings dependency check */
4967 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4968 assert(ctx->groupings.count == grp_stack_count);
4969
Michal Vasko7f45cf22020-10-01 12:49:44 +02004970 ly_set_erase(&uses_child_set, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01004971 return ret;
4972}
4973
Radek Krejci327de162019-06-14 12:52:07 +02004974static int
4975lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
4976{
4977 struct lysp_node *iter;
4978 int len = 0;
4979
4980 *path = NULL;
4981 for (iter = node; iter && len >= 0; iter = iter->parent) {
4982 char *s = *path;
4983 char *id;
4984
4985 switch (iter->nodetype) {
4986 case LYS_USES:
Radek Krejci200f1062020-07-11 22:51:03 +02004987 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02004988 break;
4989 case LYS_GROUPING:
Radek Krejci200f1062020-07-11 22:51:03 +02004990 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02004991 break;
4992 case LYS_AUGMENT:
Radek Krejci200f1062020-07-11 22:51:03 +02004993 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
Radek Krejci327de162019-06-14 12:52:07 +02004994 break;
4995 default:
4996 id = strdup(iter->name);
4997 break;
4998 }
4999
5000 if (!iter->parent) {
5001 /* print prefix */
5002 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5003 } else {
5004 /* prefix is the same as in parent */
5005 len = asprintf(path, "/%s%s", id, s ? s : "");
5006 }
5007 free(s);
5008 free(id);
5009 }
5010
5011 if (len < 0) {
5012 free(*path);
5013 *path = NULL;
5014 } else if (len == 0) {
5015 *path = strdup("/");
5016 len = 1;
5017 }
5018 return len;
5019}
5020
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005021/**
5022 * @brief Validate groupings that were defined but not directly used in the schema itself.
5023 *
5024 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5025 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5026 */
5027static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02005028lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_grp *grp)
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005029{
5030 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005031 char *path;
5032 int len;
5033
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005034 struct lysp_node_uses fake_uses = {
Michal Vasko7f45cf22020-10-01 12:49:44 +02005035 .parent = pnode,
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005036 .nodetype = LYS_USES,
5037 .flags = 0, .next = NULL,
5038 .name = grp->name,
5039 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5040 .refines = NULL, .augments = NULL
5041 };
5042 struct lysc_node_container fake_container = {
5043 .nodetype = LYS_CONTAINER,
Michal Vasko7f45cf22020-10-01 12:49:44 +02005044 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005045 .module = ctx->mod,
5046 .sp = NULL, .parent = NULL, .next = NULL,
Michal Vasko22df3f02020-08-24 13:29:22 +02005047 .prev = (struct lysc_node *)&fake_container,
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005048 .name = "fake",
5049 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5050 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5051 };
5052
5053 if (grp->parent) {
5054 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5055 }
Radek Krejci327de162019-06-14 12:52:07 +02005056
5057 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5058 if (len < 0) {
5059 LOGMEM(ctx->ctx);
5060 return LY_EMEM;
5061 }
5062 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005063 ctx->path_len = (uint32_t)len;
Radek Krejci327de162019-06-14 12:52:07 +02005064 free(path);
5065
5066 lysc_update_path(ctx, NULL, "{grouping}");
5067 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko22df3f02020-08-24 13:29:22 +02005068 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node *)&fake_container, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02005069 lysc_update_path(ctx, NULL, NULL);
5070 lysc_update_path(ctx, NULL, NULL);
5071
5072 ctx->path_len = 1;
5073 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005074
5075 /* cleanup */
5076 lysc_node_container_free(ctx->ctx, &fake_container);
5077
5078 return ret;
5079}
Radek Krejcife909632019-02-12 15:34:42 +01005080
Radek Krejcie86bf772018-12-14 11:39:53 +01005081/**
Michal Vasko20424b42020-08-31 12:29:38 +02005082 * @brief Set config flags for a node.
5083 *
5084 * @param[in] ctx Compile context.
5085 * @param[in] node Compiled node config to set.
5086 * @param[in] parent Parent of @p node.
5087 * @return LY_ERR value.
5088 */
5089static LY_ERR
5090lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent)
5091{
5092 if (node->nodetype == LYS_CASE) {
5093 /* case never has any config */
5094 assert(!(node->flags & LYS_CONFIG_MASK));
5095 return LY_SUCCESS;
5096 }
5097
5098 /* adjust parent to always get the ancestor with config */
5099 if (parent && (parent->nodetype == LYS_CASE)) {
5100 parent = parent->parent;
5101 assert(parent);
5102 }
5103
5104 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
5105 /* ignore config statements inside RPC/action data */
5106 node->flags &= ~LYS_CONFIG_MASK;
5107 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5108 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
5109 /* ignore config statements inside Notification data */
5110 node->flags &= ~LYS_CONFIG_MASK;
5111 node->flags |= LYS_CONFIG_R;
5112 } else if (!(node->flags & LYS_CONFIG_MASK)) {
5113 /* config not explicitely set, inherit it from parent */
5114 if (parent) {
5115 node->flags |= parent->flags & LYS_CONFIG_MASK;
5116 } else {
5117 /* default is config true */
5118 node->flags |= LYS_CONFIG_W;
5119 }
5120 } else {
5121 /* config set explicitely */
5122 node->flags |= LYS_SET_CONFIG;
5123 }
5124
5125 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5127 "Configuration node cannot be child of any state data node.");
5128 return LY_EVALID;
5129 }
5130
5131 return LY_SUCCESS;
5132}
5133
Michal Vasko7f45cf22020-10-01 12:49:44 +02005134static LY_ERR
5135lysp_ext_dup(const struct ly_ctx *ctx, struct lysp_ext_instance *ext, const struct lysp_ext_instance *orig_ext)
5136{
5137 LY_ERR ret = LY_SUCCESS;
5138
5139 *ext = *orig_ext;
5140 DUP_STRING(ctx, orig_ext->name, ext->name, ret);
5141 DUP_STRING(ctx, orig_ext->argument, ext->argument, ret);
5142
5143 return ret;
5144}
5145
5146static LY_ERR
5147lysp_restr_dup(const struct ly_ctx *ctx, struct lysp_restr *restr, const struct lysp_restr *orig_restr)
5148{
5149 LY_ERR ret = LY_SUCCESS;
5150
5151 if (orig_restr) {
5152 DUP_STRING(ctx, orig_restr->arg.str, restr->arg.str, ret);
5153 restr->arg.mod = orig_restr->arg.mod;
5154 DUP_STRING(ctx, orig_restr->emsg, restr->emsg, ret);
5155 DUP_STRING(ctx, orig_restr->eapptag, restr->eapptag, ret);
5156 DUP_STRING(ctx, orig_restr->dsc, restr->dsc, ret);
5157 DUP_STRING(ctx, orig_restr->ref, restr->ref, ret);
5158 DUP_ARRAY(ctx, orig_restr->exts, restr->exts, lysp_ext_dup);
5159 }
5160
5161 return ret;
5162}
5163
5164static LY_ERR
5165lysp_string_dup(const struct ly_ctx *ctx, const char **str, const char **orig_str)
5166{
5167 LY_ERR ret = LY_SUCCESS;
5168
5169 DUP_STRING(ctx, *orig_str, *str, ret);
5170
5171 return ret;
5172}
5173
5174static LY_ERR
5175lysp_qname_dup(const struct ly_ctx *ctx, struct lysp_qname *qname, const struct lysp_qname *orig_qname)
5176{
5177 LY_ERR ret = LY_SUCCESS;
5178
5179 DUP_STRING(ctx, orig_qname->str, qname->str, ret);
5180 qname->mod = orig_qname->mod;
5181
5182 return ret;
5183}
5184
5185static LY_ERR
5186lysp_type_enum_dup(const struct ly_ctx *ctx, struct lysp_type_enum *enm, const struct lysp_type_enum *orig_enm)
5187{
5188 LY_ERR ret = LY_SUCCESS;
5189
5190 DUP_STRING(ctx, orig_enm->name, enm->name, ret);
5191 DUP_STRING(ctx, orig_enm->dsc, enm->dsc, ret);
5192 DUP_STRING(ctx, orig_enm->ref, enm->ref, ret);
5193 enm->value = orig_enm->value;
5194 DUP_ARRAY(ctx, orig_enm->iffeatures, enm->iffeatures, lysp_qname_dup);
5195 DUP_ARRAY(ctx, orig_enm->exts, enm->exts, lysp_ext_dup);
5196 enm->flags = orig_enm->flags;
5197
5198 return ret;
5199}
5200
5201static LY_ERR
5202lysp_type_dup(const struct ly_ctx *ctx, struct lysp_type *type, const struct lysp_type *orig_type)
5203{
5204 LY_ERR ret = LY_SUCCESS;
5205
5206 DUP_STRING_GOTO(ctx, orig_type->name, type->name, ret, done);
5207
5208 if (orig_type->range) {
5209 type->range = calloc(1, sizeof *type->range);
5210 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx), LY_EMEM);
5211 LY_CHECK_RET(lysp_restr_dup(ctx, type->range, orig_type->range));
5212 }
5213
5214 if (orig_type->length) {
5215 type->length = calloc(1, sizeof *type->length);
5216 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx), LY_EMEM);
5217 LY_CHECK_RET(lysp_restr_dup(ctx, type->length, orig_type->length));
5218 }
5219
5220 DUP_ARRAY(ctx, orig_type->patterns, type->patterns, lysp_restr_dup);
5221 DUP_ARRAY(ctx, orig_type->enums, type->enums, lysp_type_enum_dup);
5222 DUP_ARRAY(ctx, orig_type->bits, type->bits, lysp_type_enum_dup);
5223 LY_CHECK_GOTO(ret = lyxp_expr_dup(ctx, orig_type->path, &type->path), done);
5224 DUP_ARRAY(ctx, orig_type->bases, type->bases, lysp_string_dup);
5225 DUP_ARRAY(ctx, orig_type->types, type->types, lysp_type_dup);
5226 DUP_ARRAY(ctx, orig_type->exts, type->exts, lysp_ext_dup);
5227
5228 type->compiled = orig_type->compiled;
5229
5230 type->fraction_digits = orig_type->fraction_digits;
5231 type->require_instance = orig_type->require_instance;
5232 type->flags = orig_type->flags;
5233
5234done:
5235 return ret;
5236}
5237
5238static LY_ERR
5239lysp_when_dup(const struct ly_ctx *ctx, struct lysp_when *when, const struct lysp_when *orig_when)
5240{
5241 LY_ERR ret = LY_SUCCESS;
5242
5243 DUP_STRING(ctx, orig_when->cond, when->cond, ret);
5244 DUP_STRING(ctx, orig_when->dsc, when->dsc, ret);
5245 DUP_STRING(ctx, orig_when->ref, when->ref, ret);
5246 DUP_ARRAY(ctx, orig_when->exts, when->exts, lysp_ext_dup);
5247
5248 return ret;
5249}
5250
5251static LY_ERR
5252lysp_node_common_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
5253{
5254 LY_ERR ret = LY_SUCCESS;
5255
5256 node->parent = NULL;
5257 node->nodetype = orig->nodetype;
5258 node->flags = orig->flags;
5259 node->next = NULL;
5260 DUP_STRING(ctx, orig->name, node->name, ret);
5261 DUP_STRING(ctx, orig->dsc, node->dsc, ret);
5262 DUP_STRING(ctx, orig->ref, node->ref, ret);
5263
5264 if (orig->when) {
5265 node->when = calloc(1, sizeof *node->when);
5266 LY_CHECK_ERR_RET(!node->when, LOGMEM(ctx), LY_EMEM);
5267 LY_CHECK_RET(lysp_when_dup(ctx, node->when, orig->when));
5268 }
5269
5270 DUP_ARRAY(ctx, orig->iffeatures, node->iffeatures, lysp_qname_dup);
5271 DUP_ARRAY(ctx, orig->exts, node->exts, lysp_ext_dup);
5272
5273 return ret;
5274}
5275
5276static LY_ERR
5277lysp_node_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
5278{
5279 LY_ERR ret = LY_SUCCESS;
5280 struct lysp_node_container *cont;
5281 const struct lysp_node_container *orig_cont;
5282 struct lysp_node_leaf *leaf;
5283 const struct lysp_node_leaf *orig_leaf;
5284 struct lysp_node_leaflist *llist;
5285 const struct lysp_node_leaflist *orig_llist;
5286 struct lysp_node_list *list;
5287 const struct lysp_node_list *orig_list;
5288 struct lysp_node_choice *choice;
5289 const struct lysp_node_choice *orig_choice;
5290 struct lysp_node_case *cas;
5291 const struct lysp_node_case *orig_cas;
5292 struct lysp_node_anydata *any;
5293 const struct lysp_node_anydata *orig_any;
5294
5295 assert(orig->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_ANYDATA));
5296
5297 /* common part */
5298 LY_CHECK_RET(lysp_node_common_dup(ctx, node, orig));
5299
5300 /* specific part */
5301 switch (node->nodetype) {
5302 case LYS_CONTAINER:
5303 cont = (struct lysp_node_container *)node;
5304 orig_cont = (const struct lysp_node_container *)orig;
5305
5306 DUP_ARRAY(ctx, orig_cont->musts, cont->musts, lysp_restr_dup);
5307 DUP_STRING(ctx, orig_cont->presence, cont->presence, ret);
5308 /* we do not need the rest */
5309 break;
5310 case LYS_LEAF:
5311 leaf = (struct lysp_node_leaf *)node;
5312 orig_leaf = (const struct lysp_node_leaf *)orig;
5313
5314 DUP_ARRAY(ctx, orig_leaf->musts, leaf->musts, lysp_restr_dup);
5315 LY_CHECK_RET(lysp_type_dup(ctx, &leaf->type, &orig_leaf->type));
5316 DUP_STRING(ctx, orig_leaf->units, leaf->units, ret);
5317 DUP_STRING(ctx, orig_leaf->dflt.str, leaf->dflt.str, ret);
5318 break;
5319 case LYS_LEAFLIST:
5320 llist = (struct lysp_node_leaflist *)node;
5321 orig_llist = (const struct lysp_node_leaflist *)orig;
5322
5323 DUP_ARRAY(ctx, orig_llist->musts, llist->musts, lysp_restr_dup);
5324 LY_CHECK_RET(lysp_type_dup(ctx, &llist->type, &orig_llist->type));
5325 DUP_STRING(ctx, orig_llist->units, llist->units, ret);
5326 DUP_ARRAY(ctx, orig_llist->dflts, llist->dflts, lysp_qname_dup);
5327 llist->min = orig_llist->min;
5328 llist->max = orig_llist->max;
5329 break;
5330 case LYS_LIST:
5331 list = (struct lysp_node_list *)node;
5332 orig_list = (const struct lysp_node_list *)orig;
5333
5334 DUP_ARRAY(ctx, orig_list->musts, list->musts, lysp_restr_dup);
5335 DUP_STRING(ctx, orig_list->key, list->key, ret);
5336 /* we do not need these arrays */
5337 DUP_ARRAY(ctx, orig_list->uniques, list->uniques, lysp_qname_dup);
5338 list->min = orig_list->min;
5339 list->max = orig_list->max;
5340 break;
5341 case LYS_CHOICE:
5342 choice = (struct lysp_node_choice *)node;
5343 orig_choice = (const struct lysp_node_choice *)orig;
5344
5345 /* we do not need children */
5346 LY_CHECK_RET(lysp_qname_dup(ctx, &choice->dflt, &orig_choice->dflt));
5347 break;
5348 case LYS_CASE:
5349 cas = (struct lysp_node_case *)node;
5350 orig_cas = (const struct lysp_node_case *)orig;
5351
5352 /* we do not need children */
5353 (void)cas;
5354 (void)orig_cas;
5355 break;
5356 case LYS_ANYDATA:
5357 case LYS_ANYXML:
5358 any = (struct lysp_node_anydata *)node;
5359 orig_any = (const struct lysp_node_anydata *)orig;
5360
5361 DUP_ARRAY(ctx, orig_any->musts, any->musts, lysp_restr_dup);
5362 break;
5363 default:
5364 LOGINT_RET(ctx);
5365 }
5366
5367 return ret;
5368}
5369
5370static LY_ERR
5371lysp_action_inout_dup(const struct ly_ctx *ctx, struct lysp_action_inout *inout, const struct lysp_action_inout *orig)
5372{
5373 inout->parent = NULL;
5374 inout->nodetype = orig->nodetype;
5375 DUP_ARRAY(ctx, orig->musts, inout->musts, lysp_restr_dup);
5376 /* we dot need these arrays */
5377 DUP_ARRAY(ctx, orig->exts, inout->exts, lysp_ext_dup);
5378
5379 return LY_SUCCESS;
5380}
5381
5382static LY_ERR
5383lysp_action_dup(const struct ly_ctx *ctx, struct lysp_action *act, const struct lysp_action *orig)
5384{
5385 LY_ERR ret = LY_SUCCESS;
5386
5387 act->parent = NULL;
5388 act->nodetype = orig->nodetype;
5389 act->flags = orig->flags;
5390 DUP_STRING(ctx, orig->name, act->name, ret);
5391 DUP_STRING(ctx, orig->dsc, act->dsc, ret);
5392 DUP_STRING(ctx, orig->ref, act->ref, ret);
5393 DUP_ARRAY(ctx, orig->iffeatures, act->iffeatures, lysp_qname_dup);
5394
5395 act->input.nodetype = orig->input.nodetype;
5396 act->output.nodetype = orig->output.nodetype;
5397 /* we do not need choldren of in/out */
5398 DUP_ARRAY(ctx, orig->exts, act->exts, lysp_ext_dup);
5399
5400 return ret;
5401}
5402
5403static LY_ERR
5404lysp_notif_dup(const struct ly_ctx *ctx, struct lysp_notif *notif, const struct lysp_notif *orig)
5405{
5406 LY_ERR ret = LY_SUCCESS;
5407
5408 notif->parent = NULL;
5409 notif->nodetype = orig->nodetype;
5410 notif->flags = orig->flags;
5411 DUP_STRING(ctx, orig->name, notif->name, ret);
5412 DUP_STRING(ctx, orig->dsc, notif->dsc, ret);
5413 DUP_STRING(ctx, orig->ref, notif->ref, ret);
5414 DUP_ARRAY(ctx, orig->iffeatures, notif->iffeatures, lysp_qname_dup);
5415 DUP_ARRAY(ctx, orig->musts, notif->musts, lysp_restr_dup);
5416 /* we do not need these arrays */
5417 DUP_ARRAY(ctx, orig->exts, notif->exts, lysp_ext_dup);
5418
5419 return ret;
5420}
5421
5422/**
5423 * @brief Duplicate a single parsed node. Only attributes that are used in compilation are copied.
5424 *
5425 * @param[in] ctx libyang context.
5426 * @param[in] pnode Node to duplicate.
5427 * @param[in] with_links Whether to also copy any links (child, parent pointers).
5428 * @param[out] dup_p Duplicated parsed node.
5429 * @return LY_ERR value.
5430 */
5431static LY_ERR
5432lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_node *pnode, ly_bool with_links, struct lysp_node **dup_p)
5433{
Michal Vaskoaf702452020-10-02 09:02:55 +02005434 LY_ERR ret = LY_SUCCESS;
5435 void *mem = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +02005436
5437 if (!pnode) {
5438 *dup_p = NULL;
5439 return LY_SUCCESS;
5440 }
5441
5442 switch (pnode->nodetype) {
5443 case LYS_CONTAINER:
5444 mem = calloc(1, sizeof(struct lysp_node_container));
Michal Vaskoaf702452020-10-02 09:02:55 +02005445 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
5446 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02005447 break;
5448 case LYS_LEAF:
5449 mem = calloc(1, sizeof(struct lysp_node_leaf));
Michal Vaskoaf702452020-10-02 09:02:55 +02005450 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
5451 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02005452 break;
5453 case LYS_LEAFLIST:
5454 mem = calloc(1, sizeof(struct lysp_node_leaflist));
Michal Vaskoaf702452020-10-02 09:02:55 +02005455 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
5456 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02005457 break;
5458 case LYS_LIST:
5459 mem = calloc(1, sizeof(struct lysp_node_list));
Michal Vaskoaf702452020-10-02 09:02:55 +02005460 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
5461 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02005462 break;
5463 case LYS_CHOICE:
5464 mem = calloc(1, sizeof(struct lysp_node_choice));
Michal Vaskoaf702452020-10-02 09:02:55 +02005465 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
5466 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02005467 break;
5468 case LYS_CASE:
5469 mem = calloc(1, sizeof(struct lysp_node_case));
Michal Vaskoaf702452020-10-02 09:02:55 +02005470 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
5471 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02005472 break;
5473 case LYS_ANYDATA:
5474 case LYS_ANYXML:
5475 mem = calloc(1, sizeof(struct lysp_node_anydata));
Michal Vaskoaf702452020-10-02 09:02:55 +02005476 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
5477 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02005478 break;
5479 case LYS_INPUT:
5480 case LYS_OUTPUT:
5481 mem = calloc(1, sizeof(struct lysp_action_inout));
Michal Vaskoaf702452020-10-02 09:02:55 +02005482 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
5483 LY_CHECK_GOTO(ret = lysp_action_inout_dup(ctx, mem, (struct lysp_action_inout *)pnode), cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02005484 break;
5485 case LYS_ACTION:
5486 case LYS_RPC:
5487 mem = calloc(1, sizeof(struct lysp_action));
Michal Vaskoaf702452020-10-02 09:02:55 +02005488 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
5489 LY_CHECK_GOTO(ret = lysp_action_dup(ctx, mem, (struct lysp_action *)pnode), cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02005490 break;
5491 case LYS_NOTIF:
5492 mem = calloc(1, sizeof(struct lysp_notif));
Michal Vaskoaf702452020-10-02 09:02:55 +02005493 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
5494 LY_CHECK_GOTO(ret = lysp_notif_dup(ctx, mem, (struct lysp_notif *)pnode), cleanup);
Michal Vasko7f45cf22020-10-01 12:49:44 +02005495 break;
5496 default:
5497 LOGINT_RET(ctx);
5498 }
5499
5500 if (with_links) {
5501 /* copy also parent and child pointers */
5502 ((struct lysp_node *)mem)->parent = pnode->parent;
5503 switch (pnode->nodetype) {
5504 case LYS_CONTAINER:
5505 ((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child;
5506 break;
5507 case LYS_LIST:
5508 ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child;
5509 break;
5510 case LYS_CHOICE:
5511 ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child;
5512 break;
5513 case LYS_CASE:
5514 ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child;
5515 break;
5516 default:
5517 break;
5518 }
5519 }
5520
Michal Vaskoaf702452020-10-02 09:02:55 +02005521cleanup:
5522 if (ret) {
5523 free(mem);
5524 } else {
5525 *dup_p = mem;
5526 }
5527 return ret;
Michal Vasko7f45cf22020-10-01 12:49:44 +02005528}
5529
5530#define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \
5531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid %s of %s node - it is not possible to %s \"%s\" property.", \
5532 AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\
5533 ret = LY_EVALID; \
5534 goto cleanup;
5535
5536#define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \
5537 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
5538 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid %s of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
5539 AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
5540 ret = LY_EVALID; \
5541 goto cleanup; \
5542 }
5543
5544/**
5545 * @brief Apply refine.
5546 *
5547 * @param[in] ctx Compile context.
5548 * @param[in] rfn Refine to apply.
5549 * @param[in,out] target Refine target.
5550 * @return LY_ERR value.
5551 */
5552static LY_ERR
5553lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, struct lysp_node *target)
5554{
5555 LY_ERR ret = LY_SUCCESS;
5556 LY_ARRAY_COUNT_TYPE u;
5557 struct lysp_qname *qname;
5558 struct lysp_restr **musts, *must;
5559 uint32_t *num;
5560
5561 /* default value */
5562 if (rfn->dflts) {
5563 switch (target->nodetype) {
5564 case LYS_LEAF:
5565 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
5566
5567 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
5568 DUP_STRING_GOTO(ctx->ctx, rfn->dflts[0], ((struct lysp_node_leaf *)target)->dflt.str, ret, cleanup);
5569 ((struct lysp_node_leaf *)target)->dflt.mod = ctx->mod;
5570 break;
5571 case LYS_LEAFLIST:
5572 if (ctx->mod->version < LYS_VERSION_1_1) {
5573 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5574 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
5575 ret = LY_EVALID;
5576 goto cleanup;
5577 }
5578
5579 FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free);
5580 ((struct lysp_node_leaflist *)target)->dflts = NULL;
5581 LY_ARRAY_FOR(rfn->dflts, u) {
5582 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
5583 DUP_STRING_GOTO(ctx->ctx, rfn->dflts[u], qname->str, ret, cleanup);
5584 qname->mod = ctx->mod;
5585 }
5586 break;
5587 case LYS_CHOICE:
5588 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
5589
5590 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
5591 DUP_STRING_GOTO(ctx->ctx, rfn->dflts[0], ((struct lysp_node_choice *)target)->dflt.str, ret, cleanup);
5592 ((struct lysp_node_choice *)target)->dflt.mod = ctx->mod;
5593 break;
5594 default:
5595 AMEND_WRONG_NODETYPE("refine", "replace", "default");
5596 }
5597 }
5598
5599 /* description */
5600 if (rfn->dsc) {
5601 FREE_STRING(ctx->ctx, target->dsc);
5602 DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup);
5603 }
5604
5605 /* reference */
5606 if (rfn->ref) {
5607 FREE_STRING(ctx->ctx, target->ref);
5608 DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup);
5609 }
5610
5611 /* config */
5612 if (rfn->flags & LYS_CONFIG_MASK) {
5613 if (ctx->options & (LYSC_OPT_NOTIFICATION | LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
5614 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
5615 ctx->options & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
5616 } else {
5617 target->flags &= ~LYS_CONFIG_MASK;
5618 target->flags |= rfn->flags & LYS_CONFIG_MASK;
5619 }
5620 }
5621
5622 /* mandatory */
5623 if (rfn->flags & LYS_MAND_MASK) {
5624 switch (target->nodetype) {
5625 case LYS_LEAF:
5626 case LYS_CHOICE:
5627 case LYS_ANYDATA:
5628 case LYS_ANYXML:
5629 break;
5630 default:
5631 AMEND_WRONG_NODETYPE("refine", "replace", "mandatory");
5632 }
5633
5634 target->flags &= ~LYS_MAND_MASK;
5635 target->flags |= rfn->flags & LYS_MAND_MASK;
5636 }
5637
5638 /* presence */
5639 if (rfn->presence) {
5640 switch (target->nodetype) {
5641 case LYS_CONTAINER:
5642 break;
5643 default:
5644 AMEND_WRONG_NODETYPE("refine", "replace", "presence");
5645 }
5646
5647 FREE_STRING(ctx->ctx, ((struct lysp_node_container *)target)->presence);
5648 DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup);
5649 }
5650
5651 /* must */
5652 if (rfn->musts) {
5653 switch (target->nodetype) {
5654 case LYS_CONTAINER:
5655 case LYS_LIST:
5656 case LYS_LEAF:
5657 case LYS_LEAFLIST:
5658 case LYS_ANYDATA:
5659 case LYS_ANYXML:
5660 musts = &((struct lysp_node_container *)target)->musts;
5661 break;
5662 default:
5663 AMEND_WRONG_NODETYPE("refine", "add", "must");
5664 }
5665
5666 LY_ARRAY_FOR(rfn->musts, u) {
5667 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
5668 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &rfn->musts[u]), cleanup);
5669 }
5670 }
5671
5672 /* min-elements */
5673 if (rfn->flags & LYS_SET_MIN) {
5674 switch (target->nodetype) {
5675 case LYS_LEAFLIST:
5676 num = &((struct lysp_node_leaflist *)target)->min;
5677 break;
5678 case LYS_LIST:
5679 num = &((struct lysp_node_list *)target)->min;
5680 break;
5681 default:
5682 AMEND_WRONG_NODETYPE("refine", "replace", "min-elements");
5683 }
5684
5685 *num = rfn->min;
5686 }
5687
5688 /* max-elements */
5689 if (rfn->flags & LYS_SET_MAX) {
5690 switch (target->nodetype) {
5691 case LYS_LEAFLIST:
5692 num = &((struct lysp_node_leaflist *)target)->max;
5693 break;
5694 case LYS_LIST:
5695 num = &((struct lysp_node_list *)target)->max;
5696 break;
5697 default:
5698 AMEND_WRONG_NODETYPE("refine", "replace", "max-elements");
5699 }
5700
5701 *num = rfn->max;
5702 }
5703
5704 /* if-feature */
5705 if (rfn->iffeatures) {
5706 switch (target->nodetype) {
5707 case LYS_LEAF:
5708 case LYS_LEAFLIST:
5709 case LYS_LIST:
5710 case LYS_CONTAINER:
5711 case LYS_CHOICE:
5712 case LYS_CASE:
5713 case LYS_ANYDATA:
5714 case LYS_ANYXML:
5715 break;
5716 default:
5717 AMEND_WRONG_NODETYPE("refine", "add", "if-feature");
5718 }
5719
5720 LY_ARRAY_FOR(rfn->iffeatures, u) {
5721 LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup);
5722 DUP_STRING_GOTO(ctx->ctx, rfn->iffeatures[u].str, qname->str, ret, cleanup);
5723 qname->mod = ctx->mod;
5724 }
5725 }
5726
5727 /* extension */
5728 /* TODO refine extensions */
5729
5730cleanup:
5731 return ret;
5732}
5733
5734/**
5735 * @brief Apply deviate add.
5736 *
5737 * @param[in] ctx Compile context.
5738 * @param[in] d Deviate add to apply.
5739 * @param[in,out] target Deviation target.
5740 * @return LY_ERR value.
5741 */
5742static LY_ERR
5743lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target)
5744{
5745 LY_ERR ret = LY_SUCCESS;
5746 LY_ARRAY_COUNT_TYPE u;
5747 struct lysp_qname *qname;
5748 uint32_t *num;
5749 struct lysp_restr **musts, *must;
5750
5751#define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \
5752 if (((TYPE)target)->MEMBER) { \
5753 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5754 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5755 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
5756 ret = LY_EVALID; \
5757 goto cleanup; \
5758 }
5759
5760 /* [units-stmt] */
5761 if (d->units) {
5762 switch (target->nodetype) {
5763 case LYS_LEAF:
5764 case LYS_LEAFLIST:
5765 break;
5766 default:
5767 AMEND_WRONG_NODETYPE("deviation", "add", "units");
5768 }
5769
5770 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units);
5771 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
5772 }
5773
5774 /* *must-stmt */
5775 if (d->musts) {
5776 switch (target->nodetype) {
5777 case LYS_CONTAINER:
5778 case LYS_LIST:
5779 case LYS_LEAF:
5780 case LYS_LEAFLIST:
5781 case LYS_ANYDATA:
5782 case LYS_ANYXML:
5783 musts = &((struct lysp_node_container *)target)->musts;
5784 break;
5785 case LYS_NOTIF:
5786 musts = &((struct lysp_notif *)target)->musts;
5787 break;
5788 case LYS_INPUT:
5789 case LYS_OUTPUT:
5790 musts = &((struct lysp_action_inout *)target)->musts;
5791 break;
5792 default:
5793 AMEND_WRONG_NODETYPE("deviation", "add", "must");
5794 }
5795
5796 LY_ARRAY_FOR(d->musts, u) {
5797 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
5798 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup);
5799 }
5800 }
5801
5802 /* *unique-stmt */
5803 if (d->uniques) {
5804 switch (target->nodetype) {
5805 case LYS_LIST:
5806 break;
5807 default:
5808 AMEND_WRONG_NODETYPE("deviation", "add", "unique");
5809 }
5810
5811 LY_ARRAY_FOR(d->uniques, u) {
5812 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup);
5813 DUP_STRING_GOTO(ctx->ctx, d->uniques[u], qname->str, ret, cleanup);
5814 qname->mod = ctx->mod;
5815 }
5816 }
5817
5818 /* *default-stmt */
5819 if (d->dflts) {
5820 switch (target->nodetype) {
5821 case LYS_LEAF:
5822 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
5823 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str);
5824
5825 DUP_STRING_GOTO(ctx->ctx, d->dflts[0], ((struct lysp_node_leaf *)target)->dflt.str, ret, cleanup);
5826 ((struct lysp_node_leaf *)target)->dflt.mod = ctx->mod;
5827 break;
5828 case LYS_LEAFLIST:
5829 LY_ARRAY_FOR(d->dflts, u) {
5830 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
5831 DUP_STRING_GOTO(ctx->ctx, d->dflts[u], qname->str, ret, cleanup);
5832 qname->mod = ctx->mod;
5833 }
5834 break;
5835 case LYS_CHOICE:
5836 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
5837 DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str);
5838
5839 DUP_STRING_GOTO(ctx->ctx, d->dflts[0], ((struct lysp_node_choice *)target)->dflt.str, ret, cleanup);
5840 ((struct lysp_node_choice *)target)->dflt.mod = ctx->mod;
5841 break;
5842 default:
5843 AMEND_WRONG_NODETYPE("deviation", "add", "default");
5844 }
5845 }
5846
5847 /* [config-stmt] */
5848 if (d->flags & LYS_CONFIG_MASK) {
5849 switch (target->nodetype) {
5850 case LYS_CONTAINER:
5851 case LYS_LEAF:
5852 case LYS_LEAFLIST:
5853 case LYS_LIST:
5854 case LYS_CHOICE:
5855 case LYS_ANYDATA:
5856 case LYS_ANYXML:
5857 break;
5858 default:
5859 AMEND_WRONG_NODETYPE("deviation", "add", "config");
5860 }
5861
5862 if (target->flags & LYS_CONFIG_MASK) {
5863 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5864 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5865 target->flags & LYS_CONFIG_W ? "true" : "false");
5866 ret = LY_EVALID;
5867 goto cleanup;
5868 }
5869
5870 target->flags |= d->flags & LYS_CONFIG_MASK;
5871 }
5872
5873 /* [mandatory-stmt] */
5874 if (d->flags & LYS_MAND_MASK) {
5875 switch (target->nodetype) {
5876 case LYS_LEAF:
5877 case LYS_CHOICE:
5878 case LYS_ANYDATA:
5879 case LYS_ANYXML:
5880 break;
5881 default:
5882 AMEND_WRONG_NODETYPE("deviation", "add", "mandatory");
5883 }
5884
5885 if (target->flags & LYS_MAND_MASK) {
5886 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5887 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5888 target->flags & LYS_MAND_TRUE ? "true" : "false");
5889 ret = LY_EVALID;
5890 goto cleanup;
5891 }
5892
5893 target->flags |= d->flags & LYS_MAND_MASK;
5894 }
5895
5896 /* [min-elements-stmt] */
5897 if (d->flags & LYS_SET_MIN) {
5898 switch (target->nodetype) {
5899 case LYS_LEAFLIST:
5900 num = &((struct lysp_node_leaflist *)target)->min;
5901 break;
5902 case LYS_LIST:
5903 num = &((struct lysp_node_list *)target)->min;
5904 break;
5905 default:
5906 AMEND_WRONG_NODETYPE("deviation", "add", "min-elements");
5907 }
5908
5909 if (target->flags & LYS_SET_MIN) {
5910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5911 "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
5912 ret = LY_EVALID;
5913 goto cleanup;
5914 }
5915
5916 *num = d->min;
5917 }
5918
5919 /* [max-elements-stmt] */
5920 if (d->flags & LYS_SET_MAX) {
5921 switch (target->nodetype) {
5922 case LYS_LEAFLIST:
5923 num = &((struct lysp_node_leaflist *)target)->max;
5924 break;
5925 case LYS_LIST:
5926 num = &((struct lysp_node_list *)target)->max;
5927 break;
5928 default:
5929 AMEND_WRONG_NODETYPE("deviation", "add", "max-elements");
5930 }
5931
5932 if (target->flags & LYS_SET_MAX) {
5933 if (*num) {
5934 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5935 "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").",
5936 *num);
5937 } else {
5938 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5939 "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\").");
5940 }
5941 ret = LY_EVALID;
5942 goto cleanup;
5943 }
5944
5945 *num = d->max;
5946 }
5947
5948cleanup:
5949 return ret;
5950}
5951
5952/**
5953 * @brief Apply deviate delete.
5954 *
5955 * @param[in] ctx Compile context.
5956 * @param[in] d Deviate delete to apply.
5957 * @param[in,out] target Deviation target.
5958 * @return LY_ERR value.
5959 */
5960static LY_ERR
5961lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target)
5962{
5963 LY_ERR ret = LY_SUCCESS;
5964 struct lysp_restr **musts;
5965 LY_ARRAY_COUNT_TYPE u, v;
5966 struct lysp_qname **uniques, **dflts;
5967
5968#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \
5969 LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
5970 int found = 0; \
5971 LY_ARRAY_FOR(ORIG_ARRAY, v) { \
5972 if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \
5973 found = 1; \
5974 break; \
5975 } \
5976 } \
5977 if (!found) { \
5978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5979 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5980 PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \
5981 ret = LY_EVALID; \
5982 goto cleanup; \
5983 } \
5984 LY_ARRAY_DECREMENT(ORIG_ARRAY); \
5985 FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \
5986 memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
5987 } \
5988 if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \
5989 LY_ARRAY_FREE(ORIG_ARRAY); \
5990 ORIG_ARRAY = NULL; \
5991 }
5992
5993#define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5994 if (!((TYPE)target)->MEMBER) { \
5995 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
5996 ret = LY_EVALID; \
5997 goto cleanup; \
5998 } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \
5999 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
6000 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
6001 PROPERTY, VALUE, ((TYPE)target)->MEMBER); \
6002 ret = LY_EVALID; \
6003 goto cleanup; \
6004 }
6005
6006 /* [units-stmt] */
6007 if (d->units) {
6008 switch (target->nodetype) {
6009 case LYS_LEAF:
6010 case LYS_LEAFLIST:
6011 break;
6012 default:
6013 AMEND_WRONG_NODETYPE("deviation", "delete", "units");
6014 }
6015
6016 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units);
6017 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
6018 ((struct lysp_node_leaf *)target)->units = NULL;
6019 }
6020
6021 /* *must-stmt */
6022 if (d->musts) {
6023 switch (target->nodetype) {
6024 case LYS_CONTAINER:
6025 case LYS_LIST:
6026 case LYS_LEAF:
6027 case LYS_LEAFLIST:
6028 case LYS_ANYDATA:
6029 case LYS_ANYXML:
6030 musts = &((struct lysp_node_container *)target)->musts;
6031 break;
6032 case LYS_NOTIF:
6033 musts = &((struct lysp_notif *)target)->musts;
6034 break;
6035 case LYS_INPUT:
6036 case LYS_OUTPUT:
6037 musts = &((struct lysp_action_inout *)target)->musts;
6038 break;
6039 default:
6040 AMEND_WRONG_NODETYPE("deviation", "delete", "must");
6041 }
6042
6043 DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must");
6044 }
6045
6046 /* *unique-stmt */
6047 if (d->uniques) {
6048 switch (target->nodetype) {
6049 case LYS_LIST:
6050 break;
6051 default:
6052 AMEND_WRONG_NODETYPE("deviation", "delete", "unique");
6053 }
6054
6055 uniques = &((struct lysp_node_list *)target)->uniques;
6056 DEV_DEL_ARRAY(uniques, *uniques, , .str, lysp_qname_free, "unique");
6057 }
6058
6059 /* *default-stmt */
6060 if (d->dflts) {
6061 switch (target->nodetype) {
6062 case LYS_LEAF:
6063 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
6064 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0]);
6065
6066 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
6067 ((struct lysp_node_leaf *)target)->dflt.str = NULL;
6068 break;
6069 case LYS_LEAFLIST:
6070 dflts = &((struct lysp_node_leaflist *)target)->dflts;
6071 DEV_DEL_ARRAY(dflts, *dflts, , .str, lysp_qname_free, "default");
6072 break;
6073 case LYS_CHOICE:
6074 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
6075 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0]);
6076
6077 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
6078 ((struct lysp_node_choice *)target)->dflt.str = NULL;
6079 break;
6080 default:
6081 AMEND_WRONG_NODETYPE("deviation", "delete", "default");
6082 }
6083 }
6084
6085cleanup:
6086 return ret;
6087}
6088
6089/**
6090 * @brief Apply deviate replace.
6091 *
6092 * @param[in] ctx Compile context.
6093 * @param[in] d Deviate replace to apply.
6094 * @param[in,out] target Deviation target.
6095 * @return LY_ERR value.
6096 */
6097static LY_ERR
6098lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target)
6099{
6100 LY_ERR ret = LY_SUCCESS;
6101 uint32_t *num;
6102
6103#define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
6104 if (!((TYPE)target)->MEMBER) { \
6105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
6106 ret = LY_EVALID; \
6107 goto cleanup; \
6108 }
6109
6110 /* [type-stmt] */
6111 if (d->type) {
6112 switch (target->nodetype) {
6113 case LYS_LEAF:
6114 case LYS_LEAFLIST:
6115 break;
6116 default:
6117 AMEND_WRONG_NODETYPE("deviation", "replace", "type");
6118 }
6119
6120 lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type);
6121 lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type);
6122 }
6123
6124 /* [units-stmt] */
6125 if (d->units) {
6126 switch (target->nodetype) {
6127 case LYS_LEAF:
6128 case LYS_LEAFLIST:
6129 break;
6130 default:
6131 AMEND_WRONG_NODETYPE("deviation", "replace", "units");
6132 }
6133
6134 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units);
6135 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
6136 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
6137 }
6138
6139 /* [default-stmt] */
6140 if (d->dflt) {
6141 switch (target->nodetype) {
6142 case LYS_LEAF:
6143 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt);
6144
6145 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
6146 DUP_STRING_GOTO(ctx->ctx, d->dflt, ((struct lysp_node_leaf *)target)->dflt.str, ret, cleanup);
6147 ((struct lysp_node_leaf *)target)->dflt.mod = ctx->mod;
6148 break;
6149 case LYS_CHOICE:
6150 DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt);
6151
6152 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
6153 DUP_STRING_GOTO(ctx->ctx, d->dflt, ((struct lysp_node_choice *)target)->dflt.str, ret, cleanup);
6154 ((struct lysp_node_choice *)target)->dflt.mod = ctx->mod;
6155 break;
6156 default:
6157 AMEND_WRONG_NODETYPE("deviation", "replace", "default");
6158 }
6159 }
6160
6161 /* [config-stmt] */
6162 if (d->flags & LYS_CONFIG_MASK) {
6163 switch (target->nodetype) {
6164 case LYS_CONTAINER:
6165 case LYS_LEAF:
6166 case LYS_LEAFLIST:
6167 case LYS_LIST:
6168 case LYS_CHOICE:
6169 case LYS_ANYDATA:
6170 case LYS_ANYXML:
6171 break;
6172 default:
6173 AMEND_WRONG_NODETYPE("deviation", "replace", "config");
6174 }
6175
6176 if (!(target->flags & LYS_CONFIG_MASK)) {
6177 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6178 "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false");
6179 ret = LY_EVALID;
6180 goto cleanup;
6181 }
6182
6183 target->flags &= ~LYS_CONFIG_MASK;
6184 target->flags |= d->flags & LYS_CONFIG_MASK;
6185 }
6186
6187 /* [mandatory-stmt] */
6188 if (d->flags & LYS_MAND_MASK) {
6189 switch (target->nodetype) {
6190 case LYS_LEAF:
6191 case LYS_CHOICE:
6192 case LYS_ANYDATA:
6193 case LYS_ANYXML:
6194 break;
6195 default:
6196 AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory");
6197 }
6198
6199 if (!(target->flags & LYS_MAND_MASK)) {
6200 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
6201 "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6202 ret = LY_EVALID;
6203 goto cleanup;
6204 }
6205
6206 target->flags &= ~LYS_MAND_MASK;
6207 target->flags |= d->flags & LYS_MAND_MASK;
6208 }
6209
6210 /* [min-elements-stmt] */
6211 if (d->flags & LYS_SET_MIN) {
6212 switch (target->nodetype) {
6213 case LYS_LEAFLIST:
6214 num = &((struct lysp_node_leaflist *)target)->min;
6215 break;
6216 case LYS_LIST:
6217 num = &((struct lysp_node_list *)target)->min;
6218 break;
6219 default:
6220 AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements");
6221 }
6222
6223 if (!(target->flags & LYS_SET_MIN)) {
6224 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6225 "Invalid deviation replacing \"min-elements\" property which is not present.");
6226 ret = LY_EVALID;
6227 goto cleanup;
6228 }
6229
6230 *num = d->min;
6231 }
6232
6233 /* [max-elements-stmt] */
6234 if (d->flags & LYS_SET_MAX) {
6235 switch (target->nodetype) {
6236 case LYS_LEAFLIST:
6237 num = &((struct lysp_node_leaflist *)target)->max;
6238 break;
6239 case LYS_LIST:
6240 num = &((struct lysp_node_list *)target)->max;
6241 break;
6242 default:
6243 AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements");
6244 }
6245
6246 if (!(target->flags & LYS_SET_MAX)) {
6247 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6248 "Invalid deviation replacing \"max-elements\" property which is not present.");
6249 ret = LY_EVALID;
6250 goto cleanup;
6251 }
6252
6253 *num = d->max;
6254 }
6255
6256cleanup:
6257 return ret;
6258}
6259
6260/**
6261 * @brief Get module of a single nodeid node name test.
6262 *
6263 * @param[in] ctx libyang context.
6264 * @param[in] nametest Nametest with an optional prefix.
6265 * @param[in] nametest_len Length of @p nametest.
6266 * @param[in] local_mod Module to return in case of no prefix.
6267 * @param[out] name Optional pointer to the name test without the prefix.
6268 * @param[out] name_len Length of @p name.
6269 * @return Resolved module.
6270 */
6271static const struct lys_module *
6272lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len,
6273 const struct lys_module *local_mod, const char **name, size_t *name_len)
6274{
6275 const struct lys_module *target_mod;
6276 const char *ptr;
6277
6278 ptr = ly_strnchr(nametest, ':', nametest_len);
6279 if (ptr) {
6280 target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_PREF_SCHEMA, (void *)local_mod);
6281 if (!target_mod) {
6282 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
6283 "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
6284 nametest_len, nametest, ptr - nametest, nametest, local_mod->name);
6285 return NULL;
6286 }
6287
6288 if (name) {
6289 *name = ptr + 1;
6290 *name_len = nametest_len - ((ptr - nametest) + 1);
6291 }
6292 } else {
6293 target_mod = local_mod;
6294 if (name) {
6295 *name = nametest;
6296 *name_len = nametest_len;
6297 }
6298 }
6299
6300 return target_mod;
6301}
6302
6303/**
6304 * @brief Check whether a parsed node matches a single schema nodeid name test.
6305 *
6306 * @param[in] pnode Parsed node to consider.
6307 * @param[in] pnode_mod Compiled @p pnode to-be module.
6308 * @param[in] mod Expected module.
6309 * @param[in] name Expected name.
6310 * @param[in] name_len Length of @p name.
6311 * @return Whether it is a match or not.
6312 */
6313static ly_bool
6314lysp_schema_nodeid_match_pnode(const struct lysp_node *pnode, const struct lys_module *pnode_mod,
6315 const struct lys_module *mod, const char *name, size_t name_len)
6316{
6317 const char *pname;
6318
6319 /* compare with the module of the node */
6320 if (pnode_mod != mod) {
6321 return 0;
6322 }
6323
6324 /* compare names */
6325 if (pnode->nodetype & (LYS_ACTION | LYS_RPC)) {
6326 pname = ((struct lysp_action *)pnode)->name;
6327 } else if (pnode->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
6328 pname = (pnode->nodetype & LYS_INPUT) ? "input" : "output";
6329 } else {
6330 pname = pnode->name;
6331 }
6332 if (ly_strncmp(pname, name, name_len)) {
6333 return 0;
6334 }
6335
6336 return 1;
6337}
6338
6339/**
6340 * @brief Check whether a compiled node matches a single schema nodeid name test.
6341 *
6342 * @param[in,out] node Compiled node to consider. On a match it is moved to its parent.
6343 * @param[in] mod Expected module.
6344 * @param[in] name Expected name.
6345 * @param[in] name_len Length of @p name.
6346 * @return Whether it is a match or not.
6347 */
6348static ly_bool
6349lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name,
6350 size_t name_len)
6351{
6352 const struct lys_module *node_mod;
6353 const char *node_name;
6354
6355 /* compare with the module of the node */
6356 if ((*node)->nodetype == LYS_INPUT) {
6357 node_mod = ((struct lysc_node *)(((char *)*node) - offsetof(struct lysc_action, input)))->module;
6358 } else if ((*node)->nodetype == LYS_OUTPUT) {
6359 node_mod = ((struct lysc_node *)(((char *)*node) - offsetof(struct lysc_action, output)))->module;
6360 } else {
6361 node_mod = (*node)->module;
6362 }
6363 if (node_mod != mod) {
6364 return 0;
6365 }
6366
6367 /* compare names */
6368 if ((*node)->nodetype == LYS_INPUT) {
6369 node_name = "input";
6370 } else if ((*node)->nodetype == LYS_OUTPUT) {
6371 node_name = "output";
6372 } else {
6373 node_name = (*node)->name;
6374 }
6375 if (ly_strncmp(node_name, name, name_len)) {
6376 return 0;
6377 }
6378
6379 if ((*node)->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
6380 /* move up from input/output */
6381 if ((*node)->nodetype == LYS_INPUT) {
6382 (*node) = (struct lysc_node *)(((char *)*node) - offsetof(struct lysc_action, input));
6383 } else {
6384 (*node) = (struct lysc_node *)(((char *)*node) - offsetof(struct lysc_action, output));
6385 }
6386 } else if ((*node)->parent && ((*node)->parent->nodetype & (LYS_RPC | LYS_ACTION))) {
6387 /* move to the input/output */
6388 if ((*node)->flags & LYS_CONFIG_W) {
6389 *node = (struct lysc_node *)&((struct lysc_action *)(*node)->parent)->input;
6390 } else {
6391 *node = (struct lysc_node *)&((struct lysc_action *)(*node)->parent)->output;
6392 }
6393 } else {
6394 /* move to next parent */
6395 *node = (*node)->parent;
6396 }
6397
6398 return 1;
6399}
6400
6401/**
6402 * @brief Check whether a node matches specific schema nodeid.
6403 *
6404 * @param[in] exp Parsed nodeid to match.
6405 * @param[in] exp_mod Module to use for nodes in @p exp without a prefix.
6406 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
6407 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
6408 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
6409 * @param[in] pnode_mod Compiled @p pnode to-be module.
6410 * @return Whether it is a match or not.
6411 */
6412static ly_bool
6413lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lys_module *exp_mod, const struct lysc_node *ctx_node,
6414 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
6415{
6416 uint32_t i;
6417 const struct lys_module *mod;
6418 const char *name;
6419 size_t name_len;
6420
6421 /* compare last node in the node ID */
6422 i = exp->used - 1;
6423
6424 /* get exp node ID module */
6425 mod = lys_schema_node_get_module(exp_mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_mod, &name, &name_len);
6426 assert(mod);
6427
6428 if (pnode) {
6429 /* compare on the last parsed-only node */
6430 if (!lysp_schema_nodeid_match_pnode(pnode, pnode_mod, mod, name, name_len)) {
6431 return 0;
6432 }
6433 } else {
6434 /* using parent directly */
6435 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
6436 return 0;
6437 }
6438 }
6439
6440 /* now compare all the compiled parents */
6441 while (i > 1) {
6442 i -= 2;
6443 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
6444
6445 if (!parent) {
6446 /* no more parents but path continues */
6447 return 0;
6448 }
6449
6450 /* get exp node ID module */
6451 mod = lys_schema_node_get_module(exp_mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_mod, &name,
6452 &name_len);
6453 assert(mod);
6454
6455 /* compare with the parent */
6456 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
6457 return 0;
6458 }
6459 }
6460
6461 if (ctx_node && (ctx_node != parent)) {
6462 /* descendant path has not finished in the context node */
6463 return 0;
6464 } else if (!ctx_node && parent) {
6465 /* some parent was not matched */
6466 return 0;
6467 }
6468
6469 return 1;
6470}
6471
6472static void
6473lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
6474{
6475 if (aug) {
6476 lyxp_expr_free(ctx, aug->nodeid);
6477
6478 free(aug);
6479 }
6480}
6481
6482static void
6483lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
6484{
6485 if (dev) {
6486 lyxp_expr_free(ctx, dev->nodeid);
6487 LY_ARRAY_FREE(dev->devs);
6488 LY_ARRAY_FREE(dev->dev_mods);
6489
6490 free(dev);
6491 }
6492}
6493
6494static void
6495lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
6496{
6497 if (rfn) {
6498 lyxp_expr_free(ctx, rfn->nodeid);
6499 LY_ARRAY_FREE(rfn->rfns);
6500
6501 free(rfn);
6502 }
6503}
6504
6505static void
6506lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
6507{
6508 if (!dev_pnode) {
6509 return;
6510 }
6511
6512 switch (dev_pnode->nodetype) {
6513 case LYS_CONTAINER:
6514 ((struct lysp_node_container *)dev_pnode)->child = NULL;
6515 break;
6516 case LYS_LIST:
6517 ((struct lysp_node_list *)dev_pnode)->child = NULL;
6518 break;
6519 case LYS_CHOICE:
6520 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
6521 break;
6522 case LYS_CASE:
6523 ((struct lysp_node_case *)dev_pnode)->child = NULL;
6524 break;
6525 case LYS_LEAF:
6526 case LYS_LEAFLIST:
6527 case LYS_ANYXML:
6528 case LYS_ANYDATA:
6529 /* no children */
6530 break;
6531 case LYS_NOTIF:
6532 ((struct lysp_notif *)dev_pnode)->data = NULL;
6533 lysp_notif_free((struct ly_ctx *)ctx, (struct lysp_notif *)dev_pnode);
6534 free(dev_pnode);
6535 return;
6536 case LYS_RPC:
6537 case LYS_ACTION:
6538 ((struct lysp_action *)dev_pnode)->input.data = NULL;
6539 ((struct lysp_action *)dev_pnode)->output.data = NULL;
6540 lysp_action_free((struct ly_ctx *)ctx, (struct lysp_action *)dev_pnode);
6541 free(dev_pnode);
6542 return;
6543 case LYS_INPUT:
6544 case LYS_OUTPUT:
6545 ((struct lysp_action_inout *)dev_pnode)->data = NULL;
6546 lysp_action_inout_free((struct ly_ctx *)ctx, (struct lysp_action_inout *)dev_pnode);
6547 free(dev_pnode);
6548 return;
6549 default:
6550 LOGINT(ctx);
6551 return;
6552 }
6553
6554 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
6555}
6556
6557/**
6558 * @brief Compile and apply any precompiled deviations and refines targetting a node.
6559 *
6560 * @param[in] ctx Compile context.
6561 * @param[in] pnode Parsed node to consider.
6562 * @param[in] parent First compiled parent of @p pnode.
6563 * @param[out] dev_pnode Copy of parsed node @p pnode with deviations and refines, if any. NULL if there are none.
6564 * @param[out] no_supported Whether a not-supported deviation is defined for the node.
6565 * @return LY_ERR value.
6566 */
6567static LY_ERR
6568lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
6569 struct lysp_node **dev_pnode, ly_bool *not_supported)
6570{
6571 LY_ERR ret = LY_SUCCESS;
6572 uint32_t i;
6573 LY_ARRAY_COUNT_TYPE u;
6574 struct lys_module *orig_mod = ctx->mod, *orig_mod_def = ctx->mod_def;
6575 char orig_path[LYSC_CTX_BUFSIZE];
6576 struct lysc_refine *rfn;
6577 struct lysc_deviation *dev;
6578 struct lysp_deviation *dev_p;
6579 struct lysp_deviate *d;
6580
6581 *dev_pnode = NULL;
6582 *not_supported = 0;
6583
6584 for (i = 0; i < ctx->uses_rfns.count; ++i) {
6585 rfn = ctx->uses_rfns.objs[i];
6586
6587 if (!lysp_schema_nodeid_match(rfn->nodeid, ctx->mod, rfn->nodeid_ctx_node, parent, pnode, ctx->mod)) {
6588 /* not our target node */
6589 continue;
6590 }
6591
6592 if (!*dev_pnode) {
6593 /* first refine on this node, create a copy first */
6594 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
6595 }
6596
6597 /* apply all the refines by changing (the copy of) the parsed node */
6598 LY_ARRAY_FOR(rfn->rfns, u) {
6599 /* apply refine, keep the current path and add to it */
6600 lysc_update_path(ctx, NULL, "{refine}");
6601 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
6602 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
6603 lysc_update_path(ctx, NULL, NULL);
6604 lysc_update_path(ctx, NULL, NULL);
6605 LY_CHECK_GOTO(ret, cleanup);
6606 }
6607
6608 /* refine was applied, remove it */
6609 lysc_refine_free(ctx->ctx, rfn);
6610 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
6611
6612 /* all the refines for one target node are in one structure, we are done */
6613 break;
6614 }
6615
6616 for (i = 0; i < ctx->devs.count; ++i) {
6617 dev = ctx->devs.objs[i];
6618
6619 if (!lysp_schema_nodeid_match(dev->nodeid, dev->nodeid_mod, NULL, parent, pnode, ctx->mod_def)) {
6620 /* not our target node */
6621 continue;
6622 }
6623
6624 if (dev->not_supported) {
6625 /* it is not supported, no more deviations */
6626 *not_supported = 1;
6627 goto dev_applied;
6628 }
6629
6630 if (!*dev_pnode) {
6631 /* first deviation on this node, create a copy first */
6632 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
6633 }
6634
6635 /* apply all the deviates by changing (the copy of) the parsed node */
6636 LY_ARRAY_FOR(dev->devs, u) {
6637 dev_p = dev->devs[u];
6638 LY_LIST_FOR(dev_p->deviates, d) {
6639 /* generate correct path */
6640 strcpy(orig_path, ctx->path);
6641 ctx->path_len = 1;
6642 ctx->mod = (struct lys_module *)dev->dev_mods[u];
6643 ctx->mod_def = (struct lys_module *)dev->dev_mods[u];
6644 lysc_update_path(ctx, NULL, "{deviation}");
6645 lysc_update_path(ctx, NULL, dev_p->nodeid);
6646
6647 switch (d->mod) {
6648 case LYS_DEV_ADD:
6649 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
6650 break;
6651 case LYS_DEV_DELETE:
6652 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
6653 break;
6654 case LYS_DEV_REPLACE:
6655 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
6656 break;
6657 default:
6658 LOGINT(ctx->ctx);
6659 ret = LY_EINT;
6660 }
6661
6662 /* restore previous path */
6663 strcpy(ctx->path, orig_path);
6664 ctx->path_len = strlen(ctx->path);
6665 ctx->mod = orig_mod;
6666 ctx->mod_def = orig_mod_def;
6667
6668 LY_CHECK_GOTO(ret, cleanup);
6669 }
6670 }
6671
6672dev_applied:
6673 /* deviation was applied, remove it */
6674 lysc_deviation_free(ctx->ctx, dev);
6675 ly_set_rm_index(&ctx->devs, i, NULL);
6676
6677 /* all the deviations for one target node are in one structure, we are done */
6678 break;
6679 }
6680
6681cleanup:
6682 if (ret) {
6683 lysp_dev_node_free(ctx->ctx, *dev_pnode);
6684 *dev_pnode = NULL;
6685 *not_supported = 0;
6686 }
6687 return ret;
6688}
6689
6690/**
6691 * @brief Compile and apply any precompiled top-level or uses augments targetting a node.
6692 *
6693 * @param[in] ctx Compile context.
6694 * @param[in] node Compiled node to consider.
6695 * @return LY_ERR value.
6696 */
6697static LY_ERR
6698lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
6699{
6700 LY_ERR ret = LY_SUCCESS;
6701 struct lys_module *orig_mod = ctx->mod, *orig_mod_def = ctx->mod_def;
6702 uint32_t i;
6703 char orig_path[LYSC_CTX_BUFSIZE];
6704 struct lysc_augment *aug;
6705
6706 /* uses augments */
6707 for (i = 0; i < ctx->uses_augs.count; ) {
6708 aug = ctx->uses_augs.objs[i];
6709
6710 if (!lysp_schema_nodeid_match(aug->nodeid, ctx->mod, aug->nodeid_ctx_node, node, NULL, NULL)) {
6711 /* not our target node */
6712 ++i;
6713 continue;
6714 }
6715
6716 /* apply augment, keep the current path and add to it */
6717 lysc_update_path(ctx, NULL, "{augment}");
6718 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
6719 ret = lys_compile_augment(ctx, aug->aug_p, node);
6720 lysc_update_path(ctx, NULL, NULL);
6721 lysc_update_path(ctx, NULL, NULL);
6722 LY_CHECK_GOTO(ret, cleanup);
6723
6724 /* augment was applied, remove it (index may have changed because other augments could have been applied) */
Michal Vasko7f45cf22020-10-01 12:49:44 +02006725 ly_set_rm(&ctx->uses_augs, aug, NULL);
Michal Vaskoaf702452020-10-02 09:02:55 +02006726 lysc_augment_free(ctx->ctx, aug);
Michal Vasko7f45cf22020-10-01 12:49:44 +02006727 }
6728
6729 /* top-level augments */
6730 for (i = 0; i < ctx->augs.count; ) {
6731 aug = ctx->augs.objs[i];
6732
6733 if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_mod, NULL, node, NULL, NULL)) {
6734 /* not our target node */
6735 ++i;
6736 continue;
6737 }
6738
6739 /* apply augment, use the path and modules from the augment */
6740 strcpy(orig_path, ctx->path);
6741 ctx->path_len = 1;
6742 lysc_update_path(ctx, NULL, "{augment}");
6743 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
6744 ctx->mod = (struct lys_module *)aug->nodeid_mod;
6745 ctx->mod_def = (struct lys_module *)aug->nodeid_mod;
6746 ret = lys_compile_augment(ctx, aug->aug_p, node);
6747 strcpy(ctx->path, orig_path);
6748 ctx->path_len = strlen(ctx->path);
6749 LY_CHECK_GOTO(ret, cleanup);
6750
6751 /* augment was applied, remove it */
Michal Vasko7f45cf22020-10-01 12:49:44 +02006752 ly_set_rm(&ctx->augs, aug, NULL);
Michal Vaskoaf702452020-10-02 09:02:55 +02006753 lysc_augment_free(ctx->ctx, aug);
Michal Vasko7f45cf22020-10-01 12:49:44 +02006754 }
6755
6756cleanup:
6757 ctx->mod = orig_mod;
6758 ctx->mod_def = orig_mod_def;
6759 return ret;
6760}
6761
6762/**
6763 * @brief Prepare a top-level augment to be applied during data nodes compilation.
6764 *
6765 * @param[in] ctx Compile context.
6766 * @param[in] aug_p Parsed augment to be applied.
6767 * @param[in] mod_def Local module for @p aug_p.
6768 * @return LY_ERR value.
6769 */
6770static LY_ERR
6771lys_precompile_own_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lys_module *mod_def)
6772{
6773 LY_ERR ret = LY_SUCCESS;
6774 struct lyxp_expr *exp = NULL;
6775 struct lysc_augment *aug;
6776 const struct lys_module *mod;
6777
6778 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
6779 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
6780 LY_CHECK_GOTO(ret, cleanup);
6781
6782 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], mod_def, NULL, NULL);
6783 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
6784 if (mod != ctx->mod) {
6785 /* augment for another module, ignore */
6786 goto cleanup;
6787 }
6788
6789 /* allocate new compiled augment and store it in the set */
6790 aug = calloc(1, sizeof *aug);
6791 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
6792 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, LY_SET_OPT_USEASLIST, NULL), cleanup);
6793
6794 aug->nodeid = exp;
6795 exp = NULL;
6796 aug->nodeid_mod = mod_def;
6797 aug->aug_p = aug_p;
6798
6799cleanup:
6800 lyxp_expr_free(ctx->ctx, exp);
6801 return ret;
6802}
6803
6804/**
6805 * @brief Prepare all top-level augments for the current module to be applied during data nodes compilation.
6806 *
6807 * @param[in] ctx Compile context.
6808 * @return LY_ERR value.
6809 */
6810static LY_ERR
6811lys_precompile_own_augments(struct lysc_ctx *ctx)
6812{
6813 LY_ARRAY_COUNT_TYPE u, v, w;
6814 const struct lys_module *aug_mod;
6815
6816 LY_ARRAY_FOR(ctx->mod->augmented_by, u) {
6817 aug_mod = ctx->mod->augmented_by[u];
6818
6819 /* collect all module augments */
6820 LY_ARRAY_FOR(aug_mod->parsed->augments, v) {
6821 LY_CHECK_RET(lys_precompile_own_augment(ctx, &aug_mod->parsed->augments[v], aug_mod));
6822 }
6823
6824 /* collect all submodules augments */
6825 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
6826 LY_ARRAY_FOR(aug_mod->parsed->includes[v].submodule->augments, w) {
6827 LY_CHECK_RET(lys_precompile_own_augment(ctx, &aug_mod->parsed->includes[v].submodule->augments[w], aug_mod));
6828 }
6829 }
6830 }
6831
6832 return LY_SUCCESS;
6833}
6834
6835/**
6836 * @brief Prepare a deviation to be applied during data nodes compilation.
6837 *
6838 * @param[in] ctx Compile context.
6839 * @param[in] dev_p Parsed deviation to be applied.
6840 * @param[in] mod_def Local module for @p dev_p.
6841 * @return LY_ERR value.
6842 */
6843static LY_ERR
6844lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lys_module *mod_def)
6845{
6846 LY_ERR ret = LY_SUCCESS;
6847 struct lysc_deviation *dev = NULL;
6848 struct lyxp_expr *exp = NULL;
6849 struct lysp_deviation **new_dev;
6850 const struct lys_module *mod, **new_dev_mod;
6851 uint32_t i;
6852
6853 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
6854 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
6855 LY_CHECK_GOTO(ret, cleanup);
6856
6857 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], mod_def, NULL, NULL);
6858 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
6859 if (mod != ctx->mod) {
6860 /* deviation for another module, ignore */
6861 goto cleanup;
6862 }
6863
6864 /* try to find the node in already compiled deviations */
6865 for (i = 0; i < ctx->devs.count; ++i) {
6866 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, mod_def, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
6867 ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid_mod)) {
6868 dev = ctx->devs.objs[i];
6869 break;
6870 }
6871 }
6872
6873 if (!dev) {
6874 /* allocate new compiled deviation */
6875 dev = calloc(1, sizeof *dev);
6876 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
6877 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, LY_SET_OPT_USEASLIST, NULL), cleanup);
6878
6879 dev->nodeid = exp;
6880 exp = NULL;
6881 dev->nodeid_mod = mod_def;
6882 }
6883
6884 /* add new parsed deviation structure */
6885 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
6886 *new_dev = dev_p;
6887 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_mods, new_dev_mod, ret, cleanup);
6888 *new_dev_mod = mod_def;
6889
6890cleanup:
6891 lyxp_expr_free(ctx->ctx, exp);
6892 return ret;
6893}
6894
6895/**
6896 * @brief Prepare all deviations for the current module to be applied during data nodes compilation.
6897 *
6898 * @param[in] ctx Compile context.
6899 * @return LY_ERR value.
6900 */
6901static LY_ERR
6902lys_precompile_own_deviations(struct lysc_ctx *ctx)
6903{
6904 LY_ARRAY_COUNT_TYPE u, v, w;
6905 const struct lys_module *dev_mod;
6906 struct lysc_deviation *dev;
6907 struct lysp_deviate *d;
6908 int not_supported;
6909 uint32_t i;
6910
6911 LY_ARRAY_FOR(ctx->mod->deviated_by, u) {
6912 dev_mod = ctx->mod->deviated_by[u];
6913
6914 /* compile all module deviations */
6915 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
6916 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod));
6917 }
6918
6919 /* compile all submodules deviations */
6920 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
6921 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
6922 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w], dev_mod));
6923 }
6924 }
6925 }
6926
6927 /* set not-supported flags for all the deviations */
6928 for (i = 0; i < ctx->devs.count; ++i) {
6929 dev = ctx->devs.objs[i];
6930 not_supported = 0;
6931
6932 LY_ARRAY_FOR(dev->devs, u) {
6933 LY_LIST_FOR(dev->devs[u]->deviates, d) {
6934 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
6935 not_supported = 1;
6936 break;
6937 }
6938 }
6939 if (not_supported) {
6940 break;
6941 }
6942 }
6943 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
6944 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6945 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
6946 return LY_EVALID;
6947 }
6948
6949 dev->not_supported = not_supported;
6950 }
6951
6952 return LY_SUCCESS;
6953}
6954
Michal Vasko20424b42020-08-31 12:29:38 +02006955/**
Radek Krejcia3045382018-11-22 14:30:31 +01006956 * @brief Compile parsed schema node information.
6957 * @param[in] ctx Compile context
Michal Vasko7f45cf22020-10-01 12:49:44 +02006958 * @param[in] pnode Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01006959 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
6960 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
6961 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01006962 * @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).
6963 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01006964 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6965 */
Radek Krejci19a96102018-11-15 13:38:09 +01006966static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02006967lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
6968 struct ly_set *child_set)
Radek Krejci19a96102018-11-15 13:38:09 +01006969{
Radek Krejci1c54f462020-05-12 17:25:34 +02006970 LY_ERR ret = LY_SUCCESS;
Radek Krejcic6b4f442020-08-12 14:45:18 +02006971 struct lysc_node *node = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01006972 struct lysc_when **when;
Michal Vasko7f45cf22020-10-01 12:49:44 +02006973 struct lysp_node *dev_pnode = NULL, *orig_pnode = pnode;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006974 LY_ARRAY_COUNT_TYPE u;
Michal Vasko7f45cf22020-10-01 12:49:44 +02006975 ly_bool not_supported;
Michal Vasko22df3f02020-08-24 13:29:22 +02006976 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
Radek Krejci19a96102018-11-15 13:38:09 +01006977
Michal Vasko7f45cf22020-10-01 12:49:44 +02006978 if (pnode->nodetype != LYS_USES) {
6979 lysc_update_path(ctx, parent, pnode->name);
Radek Krejci327de162019-06-14 12:52:07 +02006980 } else {
6981 lysc_update_path(ctx, NULL, "{uses}");
Michal Vasko7f45cf22020-10-01 12:49:44 +02006982 lysc_update_path(ctx, NULL, pnode->name);
Radek Krejci327de162019-06-14 12:52:07 +02006983 }
6984
Michal Vasko7f45cf22020-10-01 12:49:44 +02006985 switch (pnode->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01006986 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +02006987 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
Radek Krejci19a96102018-11-15 13:38:09 +01006988 node_compile_spec = lys_compile_node_container;
6989 break;
6990 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +02006991 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
Radek Krejci19a96102018-11-15 13:38:09 +01006992 node_compile_spec = lys_compile_node_leaf;
6993 break;
6994 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02006995 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01006996 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01006997 break;
6998 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02006999 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01007000 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01007001 break;
Radek Krejci19a96102018-11-15 13:38:09 +01007002 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +02007003 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01007004 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01007005 break;
Michal Vasko7f45cf22020-10-01 12:49:44 +02007006 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +02007007 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
7008 node_compile_spec = lys_compile_node_case;
7009 break;
Radek Krejci19a96102018-11-15 13:38:09 +01007010 case LYS_ANYXML:
7011 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02007012 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01007013 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01007014 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01007015 case LYS_USES:
Michal Vasko7f45cf22020-10-01 12:49:44 +02007016 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
Radek Krejci327de162019-06-14 12:52:07 +02007017 lysc_update_path(ctx, NULL, NULL);
7018 lysc_update_path(ctx, NULL, NULL);
7019 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01007020 default:
7021 LOGINT(ctx->ctx);
7022 return LY_EINT;
7023 }
7024 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02007025
7026 /* compile any deviations for this node */
7027 LY_CHECK_ERR_RET(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported),
7028 free(node), ret);
7029 if (not_supported) {
7030 free(node);
7031 lysc_update_path(ctx, NULL, NULL);
7032 return LY_SUCCESS;
7033 } else if (dev_pnode) {
7034 pnode = dev_pnode;
7035 }
7036
7037 node->nodetype = pnode->nodetype;
Radek Krejci19a96102018-11-15 13:38:09 +01007038 node->module = ctx->mod;
7039 node->prev = node;
Michal Vasko7f45cf22020-10-01 12:49:44 +02007040 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01007041
7042 /* config */
Michal Vasko20424b42020-08-31 12:29:38 +02007043 ret = lys_compile_config(ctx, node, parent);
7044 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007045
Michal Vasko20424b42020-08-31 12:29:38 +02007046 /* list ordering */
Radek Krejcia6d57732018-11-29 13:40:37 +01007047 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
7048 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02007049 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci0f969882020-08-21 16:56:47 +02007050 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
7051 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01007052 node->flags &= ~LYS_ORDBY_MASK;
7053 node->flags |= LYS_ORDBY_SYSTEM;
7054 } else if (!(node->flags & LYS_ORDBY_MASK)) {
7055 /* default ordering is system */
7056 node->flags |= LYS_ORDBY_SYSTEM;
7057 }
7058 }
7059
Radek Krejci19a96102018-11-15 13:38:09 +01007060 /* status - it is not inherited by specification, but it does not make sense to have
7061 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vasko20424b42020-08-31 12:29:38 +02007062 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 +01007063
Radek Krejciec4da802019-05-02 13:02:41 +02007064 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02007065 node->sp = orig_pnode;
Radek Krejci19a96102018-11-15 13:38:09 +01007066 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02007067 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
7068 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
7069 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
7070 if (pnode->when) {
Radek Krejci00b874b2019-02-12 10:54:50 +01007071 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Michal Vasko7f45cf22020-10-01 12:49:44 +02007072 LY_CHECK_GOTO(ret = lys_compile_when(ctx, pnode->when, pnode->flags, node, when), error);
Michal Vasko175012e2019-11-06 15:49:14 +01007073
7074 if (!(ctx->options & LYSC_OPT_GROUPING)) {
7075 /* do not check "when" semantics in a grouping */
Michal Vasko7f45cf22020-10-01 12:49:44 +02007076 LY_CHECK_GOTO(ret = ly_set_add(&ctx->xpath, node, 0, NULL), error);
Michal Vasko175012e2019-11-06 15:49:14 +01007077 }
Radek Krejci00b874b2019-02-12 10:54:50 +01007078 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02007079 COMPILE_ARRAY_GOTO(ctx, pnode->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007080
Michal Vasko20424b42020-08-31 12:29:38 +02007081 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
Michal Vasko7f45cf22020-10-01 12:49:44 +02007082 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
7083
7084 /* connect any augments */
7085 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
Michal Vasko20424b42020-08-31 12:29:38 +02007086
Radek Krejci19a96102018-11-15 13:38:09 +01007087 /* nodetype-specific part */
Michal Vasko7f45cf22020-10-01 12:49:44 +02007088 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01007089
Michal Vasko20424b42020-08-31 12:29:38 +02007090 /* final compilation tasks that require the node to be connected */
Michal Vasko7f45cf22020-10-01 12:49:44 +02007091 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcife909632019-02-12 15:34:42 +01007092 if (node->flags & LYS_MAND_TRUE) {
Michal Vasko20424b42020-08-31 12:29:38 +02007093 /* inherit LYS_MAND_TRUE in parent containers */
Radek Krejcife909632019-02-12 15:34:42 +01007094 lys_compile_mandatory_parents(parent, 1);
7095 }
7096
Michal Vasko7f45cf22020-10-01 12:49:44 +02007097 if (child_set) {
7098 /* add the new node into set */
7099 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, LY_SET_OPT_USEASLIST, NULL), cleanup);
7100 }
7101
Radek Krejci327de162019-06-14 12:52:07 +02007102 lysc_update_path(ctx, NULL, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02007103 lysp_dev_node_free(ctx->ctx, dev_pnode);
Radek Krejci19a96102018-11-15 13:38:09 +01007104 return LY_SUCCESS;
7105
7106error:
7107 lysc_node_free(ctx->ctx, node);
Michal Vasko7f45cf22020-10-01 12:49:44 +02007108cleanup:
7109 if (dev_pnode) {
7110 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
7111 lysp_dev_node_free(ctx->ctx, dev_pnode);
7112 }
Radek Krejci19a96102018-11-15 13:38:09 +01007113 return ret;
7114}
7115
Michal Vaskoccc062a2020-08-13 08:34:50 +02007116/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02007117 * @brief Add a module reference into an array, checks for duplicities.
Michal Vaskoccc062a2020-08-13 08:34:50 +02007118 *
7119 * @param[in] ctx Compile context.
Michal Vasko7f45cf22020-10-01 12:49:44 +02007120 * @param[in] mod Module reference to add.
7121 * @param[in,out] mod_array Module sized array to add to.
Michal Vaskoccc062a2020-08-13 08:34:50 +02007122 * @return LY_ERR value.
7123 */
7124static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02007125lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
Michal Vaskoccc062a2020-08-13 08:34:50 +02007126{
Michal Vasko7f45cf22020-10-01 12:49:44 +02007127 LY_ARRAY_COUNT_TYPE u;
7128 struct lys_module **new_mod;
Michal Vaskoccc062a2020-08-13 08:34:50 +02007129
Michal Vasko7f45cf22020-10-01 12:49:44 +02007130 LY_ARRAY_FOR(*mod_array, u) {
7131 if ((*mod_array)[u] == mod) {
7132 /* already there */
7133 return LY_EEXIST;
Michal Vaskoccc062a2020-08-13 08:34:50 +02007134 }
7135 }
7136
Michal Vasko7f45cf22020-10-01 12:49:44 +02007137 /* add the new module ref */
7138 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
7139 *new_mod = mod;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007140
7141 return LY_SUCCESS;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007142}
7143
Michal Vaskoccc062a2020-08-13 08:34:50 +02007144/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02007145 * @brief Compile top-level augments and deviations defined in the current module.
7146 * Generally, just add the module refence to the target modules.
Michal Vaskoccc062a2020-08-13 08:34:50 +02007147 *
7148 * @param[in] ctx Compile context.
Michal Vaskoccc062a2020-08-13 08:34:50 +02007149 * @return LY_ERR value.
7150 */
7151static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02007152lys_precompile_augments_deviations(struct lysc_ctx *ctx)
Michal Vaskoccc062a2020-08-13 08:34:50 +02007153{
Michal Vasko7f45cf22020-10-01 12:49:44 +02007154 LY_ERR ret = LY_SUCCESS;
7155 LY_ARRAY_COUNT_TYPE u, v;
7156 const struct lysp_module *mod_p;
7157 const struct lysc_node *target;
Michal Vaskoccc062a2020-08-13 08:34:50 +02007158 struct lys_module *mod;
Michal Vasko7f45cf22020-10-01 12:49:44 +02007159 struct lysp_submodule *submod;
7160 ly_bool has_dev = 0;
7161 uint16_t flags;
7162 uint32_t idx, opt_prev = ctx->options;
Michal Vaskoccc062a2020-08-13 08:34:50 +02007163
Michal Vasko7f45cf22020-10-01 12:49:44 +02007164 mod_p = ctx->mod->parsed;
7165
7166 if (mod_p->mod->implemented == 1) {
7167 /* it was already implemented and all the augments and deviations fully applied */
7168 return LY_SUCCESS;
Michal Vaskoccc062a2020-08-13 08:34:50 +02007169 }
7170
Michal Vasko7f45cf22020-10-01 12:49:44 +02007171 LY_ARRAY_FOR(mod_p->augments, u) {
7172 lysc_update_path(ctx, NULL, "{augment}");
7173 lysc_update_path(ctx, NULL, mod_p->augments[u].nodeid);
Michal Vaskoccc062a2020-08-13 08:34:50 +02007174
Michal Vasko7f45cf22020-10-01 12:49:44 +02007175 /* get target module */
7176 ret = lys_nodeid_check(ctx, mod_p->augments[u].nodeid, 1, &mod, NULL);
7177 LY_CHECK_RET(ret);
Michal Vaskoccc062a2020-08-13 08:34:50 +02007178
Michal Vasko7f45cf22020-10-01 12:49:44 +02007179 /* add this module into the target module augmented_by, if not there already from previous augments */
7180 lys_array_add_mod_ref(ctx, ctx->mod, &mod->augmented_by);
Michal Vaskoccc062a2020-08-13 08:34:50 +02007181
Michal Vasko7f45cf22020-10-01 12:49:44 +02007182 /* if we are compiling this module, we cannot add augments to it yet */
7183 if (mod != ctx->mod) {
7184 /* apply the augment, find the target node first */
7185 flags = 0;
7186 ret = lysc_resolve_schema_nodeid(ctx, mod_p->augments[u].nodeid, 0, NULL, ctx->mod_def, 0, &target, &flags);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007187 LY_CHECK_RET(ret);
7188
Michal Vasko7f45cf22020-10-01 12:49:44 +02007189 /* apply the augment */
7190 ctx->options |= flags;
7191 ret = lys_compile_augment(ctx, &mod_p->augments[u], (struct lysc_node *)target);
7192 ctx->options = opt_prev;
7193 LY_CHECK_RET(ret);
Radek Krejciccd20f12019-02-15 14:12:27 +01007194 }
Radek Krejci327de162019-06-14 12:52:07 +02007195
7196 lysc_update_path(ctx, NULL, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02007197 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01007198 }
7199
Michal Vasko7f45cf22020-10-01 12:49:44 +02007200 LY_ARRAY_FOR(mod_p->deviations, u) {
7201 /* get target module */
7202 lysc_update_path(ctx, NULL, "{deviation}");
7203 lysc_update_path(ctx, NULL, mod_p->deviations[u].nodeid);
7204 ret = lys_nodeid_check(ctx, mod_p->deviations[u].nodeid, 1, &mod, NULL);
7205 lysc_update_path(ctx, NULL, NULL);
7206 lysc_update_path(ctx, NULL, NULL);
7207 LY_CHECK_RET(ret);
Radek Krejciba03a5a2020-08-27 14:40:41 +02007208
Michal Vasko7f45cf22020-10-01 12:49:44 +02007209 /* add this module into the target module deviated_by, if not there already from previous deviations */
7210 lys_array_add_mod_ref(ctx, ctx->mod, &mod->deviated_by);
7211
7212 /* new deviation added to the target module */
7213 has_dev = 1;
7214 }
7215
7216 /* the same for augments and deviations in submodules */
7217 LY_ARRAY_FOR(mod_p->includes, v) {
7218 submod = mod_p->includes[v].submodule;
7219 LY_ARRAY_FOR(submod->augments, u) {
7220 lysc_update_path(ctx, NULL, "{augment}");
7221 lysc_update_path(ctx, NULL, submod->augments[u].nodeid);
7222
7223 ret = lys_nodeid_check(ctx, submod->augments[u].nodeid, 1, &mod, NULL);
7224 LY_CHECK_RET(ret);
7225
7226 lys_array_add_mod_ref(ctx, ctx->mod, &mod->augmented_by);
7227 if (mod != ctx->mod) {
7228 flags = 0;
7229 ret = lysc_resolve_schema_nodeid(ctx, mod_p->augments[u].nodeid, 0, NULL, ctx->mod_def, 0, &target, &flags);
7230 LY_CHECK_RET(ret);
7231
7232 ctx->options |= flags;
7233 ret = lys_compile_augment(ctx, &submod->augments[u], (struct lysc_node *)target);
7234 ctx->options = opt_prev;
7235 LY_CHECK_RET(ret);
Radek Krejcif538ce52019-03-05 10:46:14 +01007236 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02007237
7238 lysc_update_path(ctx, NULL, NULL);
7239 lysc_update_path(ctx, NULL, NULL);
Radek Krejcif538ce52019-03-05 10:46:14 +01007240 }
7241
Michal Vasko7f45cf22020-10-01 12:49:44 +02007242 LY_ARRAY_FOR(submod->deviations, u) {
7243 lysc_update_path(ctx, NULL, "{deviation}");
7244 lysc_update_path(ctx, NULL, submod->deviations[u].nodeid);
7245 ret = lys_nodeid_check(ctx, submod->deviations[u].nodeid, 1, &mod, NULL);
7246 lysc_update_path(ctx, NULL, NULL);
7247 lysc_update_path(ctx, NULL, NULL);
7248 LY_CHECK_RET(ret);
Radek Krejcifc11bd72019-04-11 16:00:05 +02007249
Michal Vasko7f45cf22020-10-01 12:49:44 +02007250 lys_array_add_mod_ref(ctx, ctx->mod, &mod->deviated_by);
7251 has_dev = 1;
Michal Vaskoe6143202020-07-03 13:02:08 +02007252 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007253 }
7254
Michal Vasko7f45cf22020-10-01 12:49:44 +02007255 if (!has_dev) {
7256 /* no need to recompile any modules */
7257 return LY_SUCCESS;
7258 }
7259
7260 /* free all the modules in descending order */
7261 idx = ctx->ctx->list.count;
7262 do {
7263 --idx;
7264 mod = ctx->ctx->list.objs[idx];
7265 /* skip this module */
7266 if (mod == mod_p->mod) {
7267 continue;
7268 }
7269
7270 if (mod->implemented && mod->compiled) {
7271 /* keep information about features state in the module */
7272 lys_feature_precompile_revert(ctx, mod);
7273
7274 /* free the module */
7275 lysc_module_free(mod->compiled, NULL);
7276 mod->compiled = NULL;
7277 }
7278 } while (idx);
7279
7280 /* recompile all the modules in ascending order */
7281 for (idx = 0; idx < ctx->ctx->list.count; ++idx) {
7282 mod = ctx->ctx->list.objs[idx];
7283
7284 /* skip this module */
7285 if (mod == mod_p->mod) {
7286 continue;
7287 }
7288
7289 if (mod->implemented) {
7290 /* compile */
7291 LY_CHECK_GOTO(ret = lys_compile(mod, LYSC_OPT_INTERNAL), cleanup);
7292 }
7293 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007294
7295cleanup:
Radek Krejcid05cbd92018-12-05 14:26:40 +01007296 return ret;
7297}
7298
Radek Krejci335332a2019-09-05 13:03:35 +02007299static void *
7300lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
7301{
Radek Krejci1deb5be2020-08-26 16:43:36 +02007302 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
Radek Krejci335332a2019-09-05 13:03:35 +02007303 if (substmts[u].stmt == stmt) {
7304 return substmts[u].storage;
7305 }
7306 }
7307 return NULL;
7308}
7309
7310LY_ERR
7311lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
7312{
7313 LY_ERR ret = LY_EVALID, r;
Radek Krejci1deb5be2020-08-26 16:43:36 +02007314 LY_ARRAY_COUNT_TYPE u;
Radek Krejci335332a2019-09-05 13:03:35 +02007315 struct lysp_stmt *stmt;
Michal Vasko7f45cf22020-10-01 12:49:44 +02007316 struct lysp_qname qname;
Radek Krejci335332a2019-09-05 13:03:35 +02007317 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02007318
7319 /* check for invalid substatements */
7320 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02007321 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
7322 continue;
7323 }
Radek Krejci335332a2019-09-05 13:03:35 +02007324 for (u = 0; substmts[u].stmt; ++u) {
7325 if (substmts[u].stmt == stmt->kw) {
7326 break;
7327 }
7328 }
7329 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02007330 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
7331 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02007332 goto cleanup;
7333 }
Radek Krejci335332a2019-09-05 13:03:35 +02007334 }
7335
Radek Krejciad5963b2019-09-06 16:03:05 +02007336 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
7337
Radek Krejci335332a2019-09-05 13:03:35 +02007338 /* keep order of the processing the same as the order in the defined substmts,
7339 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
7340 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejci857189e2020-09-01 13:26:36 +02007341 ly_bool stmt_present = 0;
Radek Krejciad5963b2019-09-06 16:03:05 +02007342
Radek Krejci335332a2019-09-05 13:03:35 +02007343 for (stmt = ext->child; stmt; stmt = stmt->next) {
7344 if (substmts[u].stmt != stmt->kw) {
7345 continue;
7346 }
7347
Radek Krejciad5963b2019-09-06 16:03:05 +02007348 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02007349 if (substmts[u].storage) {
7350 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02007351 case LY_STMT_STATUS:
7352 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
7353 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
7354 break;
7355 case LY_STMT_UNITS: {
7356 const char **units;
7357
7358 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
7359 /* single item */
7360 if (*((const char **)substmts[u].storage)) {
7361 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
7362 goto cleanup;
7363 }
7364 units = (const char **)substmts[u].storage;
7365 } else {
7366 /* sized array */
7367 const char ***units_array = (const char ***)substmts[u].storage;
7368 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
7369 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02007370 r = lydict_insert(ctx->ctx, stmt->arg, 0, units);
7371 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +02007372 break;
7373 }
Radek Krejci335332a2019-09-05 13:03:35 +02007374 case LY_STMT_TYPE: {
7375 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
7376 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
7377
7378 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
7379 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02007380 if (*(struct lysc_type **)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02007381 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
7382 goto cleanup;
7383 }
7384 compiled = substmts[u].storage;
7385 } else {
7386 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02007387 struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02007388 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02007389 compiled = (void *)type;
Radek Krejci335332a2019-09-05 13:03:35 +02007390 }
7391
Radek Krejciad5963b2019-09-06 16:03:05 +02007392 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Michal Vasko22df3f02020-08-24 13:29:22 +02007393 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL,
7394 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type **)compiled,
Michal Vasko7f45cf22020-10-01 12:49:44 +02007395 units && !*units ? units : NULL, NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
Radek Krejci38d85362019-09-05 16:26:38 +02007396 lysp_type_free(ctx->ctx, parsed);
7397 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02007398 break;
7399 }
Radek Krejciad5963b2019-09-06 16:03:05 +02007400 case LY_STMT_IF_FEATURE: {
7401 struct lysc_iffeature *iff = NULL;
7402
7403 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
7404 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02007405 if (((struct lysc_iffeature *)substmts[u].storage)->features) {
Radek Krejciad5963b2019-09-06 16:03:05 +02007406 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
7407 goto cleanup;
7408 }
Michal Vasko22df3f02020-08-24 13:29:22 +02007409 iff = (struct lysc_iffeature *)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02007410 } else {
7411 /* sized array */
Michal Vasko22df3f02020-08-24 13:29:22 +02007412 struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage;
Radek Krejciad5963b2019-09-06 16:03:05 +02007413 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
7414 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02007415 qname.str = stmt->arg;
7416 qname.mod = ctx->mod_def;
7417 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &qname, iff), ret = r, cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +02007418 break;
7419 }
7420 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
7421 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02007422 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02007423 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.",
7424 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02007425 goto cleanup;
7426 }
7427 }
Radek Krejci335332a2019-09-05 13:03:35 +02007428 }
Radek Krejci335332a2019-09-05 13:03:35 +02007429
Radek Krejciad5963b2019-09-06 16:03:05 +02007430 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
7431 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
7432 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
7433 goto cleanup;
7434 }
Radek Krejci335332a2019-09-05 13:03:35 +02007435 }
7436
7437 ret = LY_SUCCESS;
7438
7439cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02007440 return ret;
7441}
7442
Michal Vasko175012e2019-11-06 15:49:14 +01007443/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01007444 * @brief Check when for cyclic dependencies.
Michal Vasko7f45cf22020-10-01 12:49:44 +02007445 *
Michal Vaskoecd62de2019-11-13 12:35:11 +01007446 * @param[in] set Set with all the referenced nodes.
7447 * @param[in] node Node whose "when" referenced nodes are in @p set.
7448 * @return LY_ERR value
7449 */
7450static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007451lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01007452{
7453 struct lyxp_set tmp_set;
7454 struct lyxp_set_scnode *xp_scnode;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007455 uint32_t i, j;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007456 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007457 struct lysc_when *when;
7458 LY_ERR ret = LY_SUCCESS;
7459
7460 memset(&tmp_set, 0, sizeof tmp_set);
7461
7462 /* prepare in_ctx of the set */
Michal Vaskod989ba02020-08-24 10:59:24 +02007463 for (i = 0; i < set->used; ++i) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007464 xp_scnode = &set->val.scnodes[i];
7465
Michal Vasko5c4e5892019-11-14 12:31:38 +01007466 if (xp_scnode->in_ctx != -1) {
7467 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01007468 xp_scnode->in_ctx = 1;
7469 }
7470 }
7471
7472 for (i = 0; i < set->used; ++i) {
7473 xp_scnode = &set->val.scnodes[i];
7474 if (xp_scnode->in_ctx != 1) {
7475 /* already checked */
7476 continue;
7477 }
7478
Michal Vasko1bf09392020-03-27 12:38:10 +01007479 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko2ff7efe2019-12-10 14:50:59 +01007480 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007481 /* no when to check */
7482 xp_scnode->in_ctx = 0;
7483 continue;
7484 }
7485
7486 node = xp_scnode->scnode;
7487 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007488 LY_ARRAY_FOR(node->when, u) {
7489 when = node->when[u];
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007490 ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context,
Michal Vaskoecd62de2019-11-13 12:35:11 +01007491 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
7492 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007493 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007494 goto cleanup;
7495 }
7496
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007497 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007498 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007499 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007500 /* try to find this node in our set */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02007501 uint32_t idx;
7502 if (lyxp_set_scnode_contains(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1, &idx) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007503 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 +01007504 ret = LY_EVALID;
7505 goto cleanup;
7506 }
7507
7508 /* needs to be checked, if in both sets, will be ignored */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007509 tmp_set.val.scnodes[j].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007510 } else {
7511 /* no when, nothing to check */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007512 tmp_set.val.scnodes[j].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007513 }
7514 }
7515
7516 /* merge this set into the global when set */
7517 lyxp_set_scnode_merge(set, &tmp_set);
7518 }
7519
7520 /* check when of non-data parents as well */
7521 node = node->parent;
7522 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
7523
Michal Vasko251f56e2019-11-14 16:06:47 +01007524 /* this node when was checked (xp_scnode could have been reallocd) */
7525 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007526 }
7527
7528cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007529 lyxp_set_free_content(&tmp_set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007530 return ret;
7531}
7532
7533/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02007534 * @brief Check when/must expressions of a node on a complete compiled schema tree.
7535 *
Michal Vasko175012e2019-11-06 15:49:14 +01007536 * @param[in] ctx Compile context.
7537 * @param[in] node Node to check.
7538 * @return LY_ERR value
7539 */
7540static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007541lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
Michal Vasko175012e2019-11-06 15:49:14 +01007542{
Michal Vasko175012e2019-11-06 15:49:14 +01007543 struct lyxp_set tmp_set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007544 uint32_t i;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007545 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02007546 uint32_t opts;
Radek Krejci857189e2020-09-01 13:26:36 +02007547 ly_bool input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01007548 struct lysc_when **when = NULL;
7549 struct lysc_must *musts = NULL;
7550 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b26e742020-07-17 15:02:10 +02007551 const struct lysc_node *op;
Michal Vasko175012e2019-11-06 15:49:14 +01007552
7553 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01007554 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko6b26e742020-07-17 15:02:10 +02007555 if (node->flags & LYS_CONFIG_R) {
7556 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent);
7557 if (op) {
7558 /* we are actually in output */
7559 opts = LYXP_SCNODE_OUTPUT;
7560 }
7561 }
Michal Vasko175012e2019-11-06 15:49:14 +01007562
7563 switch (node->nodetype) {
7564 case LYS_CONTAINER:
7565 when = ((struct lysc_node_container *)node)->when;
7566 musts = ((struct lysc_node_container *)node)->musts;
7567 break;
7568 case LYS_CHOICE:
7569 when = ((struct lysc_node_choice *)node)->when;
7570 break;
7571 case LYS_LEAF:
7572 when = ((struct lysc_node_leaf *)node)->when;
7573 musts = ((struct lysc_node_leaf *)node)->musts;
7574 break;
7575 case LYS_LEAFLIST:
7576 when = ((struct lysc_node_leaflist *)node)->when;
7577 musts = ((struct lysc_node_leaflist *)node)->musts;
7578 break;
7579 case LYS_LIST:
7580 when = ((struct lysc_node_list *)node)->when;
7581 musts = ((struct lysc_node_list *)node)->musts;
7582 break;
7583 case LYS_ANYXML:
7584 case LYS_ANYDATA:
7585 when = ((struct lysc_node_anydata *)node)->when;
7586 musts = ((struct lysc_node_anydata *)node)->musts;
7587 break;
7588 case LYS_CASE:
7589 when = ((struct lysc_node_case *)node)->when;
7590 break;
7591 case LYS_NOTIF:
7592 musts = ((struct lysc_notif *)node)->musts;
7593 break;
Michal Vasko1bf09392020-03-27 12:38:10 +01007594 case LYS_RPC:
Michal Vasko5d8756a2019-11-07 15:21:00 +01007595 case LYS_ACTION:
7596 /* first process input musts */
7597 musts = ((struct lysc_action *)node)->input.musts;
7598 break;
Michal Vasko175012e2019-11-06 15:49:14 +01007599 default:
7600 /* nothing to check */
7601 break;
7602 }
7603
Michal Vasko175012e2019-11-06 15:49:14 +01007604 /* check "when" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007605 LY_ARRAY_FOR(when, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007606 ret = lyxp_atomize(when[u]->cond, LY_PREF_SCHEMA, when[u]->module, when[u]->context ? when[u]->context : node,
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007607 when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01007608 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007609 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01007610 goto cleanup;
7611 }
7612
Michal Vaskodc052f32019-11-07 11:11:38 +01007613 ctx->path[0] = '\0';
7614 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007615 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01007616 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007617 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
7618 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01007619
Michal Vaskoecd62de2019-11-13 12:35:11 +01007620 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007621 ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module,
Michal Vaskoecd62de2019-11-13 12:35:11 +01007622 schema->name);
7623 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01007624
7625 /* check dummy node accessing */
7626 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007627 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01007628 ret = LY_EVALID;
7629 goto cleanup;
7630 }
Michal Vasko175012e2019-11-06 15:49:14 +01007631 }
7632 }
7633
Michal Vaskoecd62de2019-11-13 12:35:11 +01007634 /* check cyclic dependencies */
Michal Vasko004d3152020-06-11 19:59:22 +02007635 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007636 LY_CHECK_GOTO(ret, cleanup);
7637
Michal Vaskod3678892020-05-21 10:06:58 +02007638 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007639 }
7640
Michal Vasko5d8756a2019-11-07 15:21:00 +01007641check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01007642 /* check "must" */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007643 LY_ARRAY_FOR(musts, u) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007644 ret = lyxp_atomize(musts[u].cond, LY_PREF_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, &tmp_set, opts);
Michal Vasko175012e2019-11-06 15:49:14 +01007645 if (ret != LY_SUCCESS) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007646 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01007647 goto cleanup;
7648 }
7649
Michal Vaskodc052f32019-11-07 11:11:38 +01007650 ctx->path[0] = '\0';
7651 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007652 for (i = 0; i < tmp_set.used; ++i) {
Michal Vasko175012e2019-11-06 15:49:14 +01007653 /* skip roots'n'stuff */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007654 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko175012e2019-11-06 15:49:14 +01007655 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02007656 ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags,
7657 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
Michal Vasko175012e2019-11-06 15:49:14 +01007658 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01007659 }
7660 }
7661
Michal Vaskod3678892020-05-21 10:06:58 +02007662 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007663 }
7664
Michal Vasko1bf09392020-03-27 12:38:10 +01007665 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
Michal Vasko5d8756a2019-11-07 15:21:00 +01007666 /* now check output musts */
7667 input_done = 1;
7668 musts = ((struct lysc_action *)node)->output.musts;
7669 opts = LYXP_SCNODE_OUTPUT;
7670 goto check_musts;
7671 }
7672
Michal Vasko175012e2019-11-06 15:49:14 +01007673cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007674 lyxp_set_free_content(&tmp_set);
Michal Vasko175012e2019-11-06 15:49:14 +01007675 return ret;
7676}
7677
Michal Vasko7f45cf22020-10-01 12:49:44 +02007678/**
7679 * @brief Check leafref for its target existence on a complete compiled schema tree.
7680 *
7681 * @param[in] ctx Compile context.
7682 * @param[in] node Context node for the leafref.
7683 * @param[in] lref Leafref to resolve.
7684 * @return LY_ERR value.
7685 */
Michal Vasko8d544252020-03-02 10:19:52 +01007686static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007687lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
7688{
Michal Vasko6b26e742020-07-17 15:02:10 +02007689 const struct lysc_node *target = NULL, *siter;
Michal Vasko004d3152020-06-11 19:59:22 +02007690 struct ly_path *p;
7691 struct lysc_type *type;
7692
7693 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7694
7695 /* try to find the target */
Michal Vasko00cbf532020-06-15 13:58:47 +02007696 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE,
7697 lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02007698 LY_PREF_SCHEMA, lref->path_context, &p));
Michal Vasko004d3152020-06-11 19:59:22 +02007699
7700 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007701 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02007702 ly_path_free(node->module->ctx, p);
7703
7704 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
7705 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7706 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
7707 lref->path->expr, lys_nodetype2str(target->nodetype));
7708 return LY_EVALID;
7709 }
7710
7711 /* check status */
7712 ctx->path[0] = '\0';
7713 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7714 ctx->path_len = strlen(ctx->path);
7715 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
7716 return LY_EVALID;
7717 }
7718 ctx->path_len = 1;
7719 ctx->path[1] = '\0';
7720
7721 /* check config */
Michal Vasko6b26e742020-07-17 15:02:10 +02007722 if (lref->require_instance) {
Radek Krejci1e008d22020-08-17 11:37:37 +02007723 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02007724 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007725 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
7726 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
7727 return LY_EVALID;
7728 }
7729 }
7730
7731 /* store the target's type and check for circular chain of leafrefs */
7732 lref->realtype = ((struct lysc_node_leaf *)target)->type;
7733 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
7734 if (type == (struct lysc_type *)lref) {
7735 /* circular chain detected */
7736 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7737 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
7738 return LY_EVALID;
7739 }
7740 }
7741
7742 /* check if leafref and its target are under common if-features */
7743 if (lys_compile_leafref_features_validate(node, target)) {
7744 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
7745 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
7746 " features applicable to the leafref itself.", lref->path->expr);
7747 return LY_EVALID;
7748 }
7749
7750 return LY_SUCCESS;
7751}
7752
7753static LY_ERR
Michal Vasko8d544252020-03-02 10:19:52 +01007754lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
7755{
7756 struct lysc_ext_instance *ext;
7757 struct lysp_ext_instance *ext_p = NULL;
7758 struct lysp_stmt *stmt;
7759 const struct lys_module *ext_mod;
7760 LY_ERR ret = LY_SUCCESS;
7761
7762 /* create the parsed extension instance manually */
7763 ext_p = calloc(1, sizeof *ext_p);
7764 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +02007765 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup);
7766 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "default", 0, &ext_p->argument), cleanup);
Michal Vasko8d544252020-03-02 10:19:52 +01007767 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
7768 ext_p->insubstmt_index = 0;
7769
Radek Krejci87e25252020-09-15 13:28:31 +02007770 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
7771 LY_CHECK_ERR_GOTO(!stmt, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci011e4aa2020-09-04 15:22:31 +02007772 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup);
7773 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "boolean", 0, &stmt->arg), cleanup);
Michal Vasko8d544252020-03-02 10:19:52 +01007774 stmt->kw = LY_STMT_TYPE;
Michal Vasko8d544252020-03-02 10:19:52 +01007775
7776 /* allocate new extension instance */
7777 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
7778
7779 /* manually get extension definition module */
7780 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
7781
7782 /* compile the extension instance */
7783 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
7784
7785cleanup:
7786 lysp_ext_instance_free(ctx->ctx, ext_p);
7787 free(ext_p);
7788 return ret;
7789}
7790
Michal Vasko7f45cf22020-10-01 12:49:44 +02007791/**
7792 * @brief Compile default value(s) for leaf or leaf-list expecting a complete compiled schema tree.
7793 *
7794 * @param[in] ctx Compile context.
7795 * @param[in] node Leaf or leaf-list to compile the default value(s) for.
7796 * @param[in] type Type of the default value.
7797 * @param[in] dflt Default value.
7798 * @param[in] dflt_mod Local module for @p dflt.
7799 * @param[in,out] storage Storage for the compiled default value.
7800 * @return LY_ERR value.
7801 */
Michal Vasko004d3152020-06-11 19:59:22 +02007802static LY_ERR
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007803lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_type *type, const char *dflt,
Radek Krejci0f969882020-08-21 16:56:47 +02007804 const struct lys_module *dflt_mod, struct lyd_value *storage)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007805{
7806 LY_ERR ret;
7807 struct ly_err_item *err = NULL;
7808
7809 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA,
7810 LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err);
7811 if (err) {
7812 ly_err_print(err);
7813 ctx->path[0] = '\0';
7814 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
7815 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
7816 "Invalid default - value does not fit the type (%s).", err->msg);
7817 ly_err_free(err);
7818 }
7819 if (!ret) {
7820 ++storage->realtype->refcount;
7821 return LY_SUCCESS;
7822 }
7823 return ret;
7824}
7825
Michal Vasko7f45cf22020-10-01 12:49:44 +02007826/**
7827 * @brief Compile default value of a leaf expecting a complete compiled schema tree.
7828 *
7829 * @param[in] ctx Compile context.
7830 * @param[in] leaf Leaf that the default value is for.
7831 * @param[in] dflt Default value to compile.
7832 * @return LY_ERR value.
7833 */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007834static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02007835lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007836{
7837 LY_ERR ret;
7838
7839 assert(!leaf->dflt);
7840
7841 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
7842 /* ignore default values for keys and mandatory leaves */
7843 return LY_SUCCESS;
7844 }
7845
7846 /* allocate the default value */
7847 leaf->dflt = calloc(1, sizeof *leaf->dflt);
7848 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
7849
7850 /* store the default value */
Michal Vasko7f45cf22020-10-01 12:49:44 +02007851 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt->str, dflt->mod, leaf->dflt);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007852 if (ret) {
7853 free(leaf->dflt);
7854 leaf->dflt = NULL;
7855 }
7856
7857 return ret;
7858}
7859
Michal Vasko7f45cf22020-10-01 12:49:44 +02007860/**
7861 * @brief Compile default values of a leaf-list expecting a complete compiled schema tree.
7862 *
7863 * @param[in] ctx Compile context.
7864 * @param[in] llist Leaf-list that the default value(s) are for.
7865 * @param[in] dflt Default value to compile, in case of a single value.
7866 * @param[in] dflts Sized array of default values, in case of more values.
7867 * @return LY_ERR value.
7868 */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007869static LY_ERR
Michal Vasko7f45cf22020-10-01 12:49:44 +02007870lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflt,
7871 struct lysp_qname *dflts)
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007872{
7873 LY_ERR ret;
7874 LY_ARRAY_COUNT_TYPE orig_count, u, v;
7875
7876 assert(dflt || dflts);
7877
7878 if (llist->dflts) {
7879 /* there were already some defaults and we are adding new by deviations */
7880 assert(dflts);
7881 orig_count = LY_ARRAY_COUNT(llist->dflts);
7882 } else {
7883 orig_count = 0;
7884 }
7885
7886 /* allocate new items */
7887 if (dflts) {
7888 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
7889 } else {
7890 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
7891 }
7892
7893 /* fill each new default value */
7894 if (dflts) {
7895 LY_ARRAY_FOR(dflts, u) {
7896 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
Michal Vasko7f45cf22020-10-01 12:49:44 +02007897 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u].str, dflts[u].mod,
7898 llist->dflts[orig_count + u]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007899 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
7900 LY_ARRAY_INCREMENT(llist->dflts);
7901 }
7902 } else {
7903 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
Michal Vasko7f45cf22020-10-01 12:49:44 +02007904 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt->str, dflt->mod,
7905 llist->dflts[orig_count]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007906 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
7907 LY_ARRAY_INCREMENT(llist->dflts);
7908 }
7909
7910 /* check default value uniqueness */
7911 if (llist->flags & LYS_CONFIG_W) {
7912 /* configuration data values must be unique - so check the default values */
7913 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
7914 for (v = 0; v < u; ++v) {
7915 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007916 lysc_update_path(ctx, llist->parent, llist->name);
7917 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko7f45cf22020-10-01 12:49:44 +02007918 "Configuration leaf-list has multiple defaults of the same value \"%s\".",
7919 llist->dflts[u]->canonical);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007920 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007921 return LY_EVALID;
7922 }
7923 }
7924 }
7925 }
7926
7927 return LY_SUCCESS;
7928}
7929
Michal Vasko7f45cf22020-10-01 12:49:44 +02007930/**
7931 * @brief Finish compilation of all the unres sets of a compile context.
7932 *
7933 * @param[in] ctx Compile context with unres sets.
7934 * @return LY_ERR value.
7935 */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007936static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007937lys_compile_unres(struct lysc_ctx *ctx)
7938{
7939 struct lysc_node *node;
7940 struct lysc_type *type, *typeiter;
7941 struct lysc_type_leafref *lref;
Michal Vasko7f45cf22020-10-01 12:49:44 +02007942 struct lysc_augment *aug;
7943 struct lysc_deviation *dev;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02007944 LY_ARRAY_COUNT_TYPE v;
Michal Vasko004d3152020-06-11 19:59:22 +02007945 uint32_t i;
7946
7947 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7948 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7949 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
7950 for (i = 0; i < ctx->leafrefs.count; ++i) {
7951 node = ctx->leafrefs.objs[i];
7952 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
7953 type = ((struct lysc_node_leaf *)node)->type;
7954 if (type->basetype == LY_TYPE_LEAFREF) {
7955 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
7956 } else if (type->basetype == LY_TYPE_UNION) {
7957 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7958 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7959 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
7960 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
7961 }
7962 }
7963 }
7964 }
7965 for (i = 0; i < ctx->leafrefs.count; ++i) {
7966 /* store pointer to the real type */
7967 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
7968 if (type->basetype == LY_TYPE_LEAFREF) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007969 for (typeiter = ((struct lysc_type_leafref *)type)->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007970 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007971 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7972 ((struct lysc_type_leafref *)type)->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007973 } else if (type->basetype == LY_TYPE_UNION) {
Michal Vasko22df3f02020-08-24 13:29:22 +02007974 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
7975 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7976 for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype;
Michal Vasko004d3152020-06-11 19:59:22 +02007977 typeiter->basetype == LY_TYPE_LEAFREF;
Michal Vasko22df3f02020-08-24 13:29:22 +02007978 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
7979 ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter;
Michal Vasko004d3152020-06-11 19:59:22 +02007980 }
7981 }
7982 }
7983 }
7984
7985 /* check xpath */
7986 for (i = 0; i < ctx->xpath.count; ++i) {
7987 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
7988 }
7989
7990 /* finish incomplete default values compilation */
7991 for (i = 0; i < ctx->dflts.count; ++i) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02007992 struct lysc_unres_dflt *r = ctx->dflts.objs[i];
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007993 if (r->leaf->nodetype == LYS_LEAF) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02007994 LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt));
Michal Vaskoba99a3e2020-08-18 15:50:05 +02007995 } else {
Michal Vasko7f45cf22020-10-01 12:49:44 +02007996 LY_CHECK_RET(lys_compile_unres_llist_dflts(ctx, r->llist, r->dflt, r->dflts));
Michal Vasko004d3152020-06-11 19:59:22 +02007997 }
Michal Vasko004d3152020-06-11 19:59:22 +02007998 }
7999
Michal Vasko7f45cf22020-10-01 12:49:44 +02008000 /* check that all augments were applied */
8001 for (i = 0; i < ctx->augs.count; ++i) {
8002 aug = ctx->augs.objs[i];
8003 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
8004 "Augment target node \"%s\" from module \"%s\" was not found.", aug->nodeid->expr,
8005 aug->nodeid_mod->name);
8006 }
8007 if (ctx->augs.count) {
8008 return LY_ENOTFOUND;
8009 }
8010
8011 /* check that all deviations were applied */
8012 for (i = 0; i < ctx->devs.count; ++i) {
8013 dev = ctx->devs.objs[i];
8014 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
8015 "Deviation(s) target node \"%s\" from module \"%s\" was not found.", dev->nodeid->expr,
8016 dev->nodeid_mod->name);
8017 }
8018 if (ctx->devs.count) {
8019 return LY_ENOTFOUND;
8020 }
8021
8022 return LY_SUCCESS;
8023}
8024
8025/**
8026 * @brief Revert precompilation of module augments and deviations. Meaning remove its reference from
8027 * all the target modules.
8028 *
8029 * @param[in] ctx Compile context.
8030 * @param[in] mod Mod whose precompilation to revert.
8031 */
8032static void
8033lys_precompile_augments_deviations_revert(struct lysc_ctx *ctx, const struct lys_module *mod)
8034{
8035 uint32_t i;
8036 LY_ARRAY_COUNT_TYPE u, count;
8037 struct lys_module *m;
8038
8039 for (i = 0; i < ctx->ctx->list.count; ++i) {
8040 m = ctx->ctx->list.objs[i];
8041
8042 if (m->augmented_by) {
8043 count = LY_ARRAY_COUNT(m->augmented_by);
8044 for (u = 0; u < count; ++u) {
8045 if (m->augmented_by[u] == mod) {
8046 /* keep the order */
8047 if (u < count - 1) {
8048 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u) * sizeof *m->augmented_by);
8049 }
8050 LY_ARRAY_DECREMENT(m->augmented_by);
8051 break;
8052 }
8053 }
8054 if (!LY_ARRAY_COUNT(m->augmented_by)) {
8055 LY_ARRAY_FREE(m->augmented_by);
8056 m->augmented_by = NULL;
8057 }
8058 }
8059
8060 if (m->deviated_by) {
8061 count = LY_ARRAY_COUNT(m->deviated_by);
8062 for (u = 0; u < count; ++u) {
8063 if (m->deviated_by[u] == mod) {
8064 /* keep the order */
8065 if (u < count - 1) {
8066 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u) * sizeof *m->deviated_by);
8067 }
8068 LY_ARRAY_DECREMENT(m->deviated_by);
8069 break;
8070 }
8071 }
8072 if (!LY_ARRAY_COUNT(m->deviated_by)) {
8073 LY_ARRAY_FREE(m->deviated_by);
8074 m->deviated_by = NULL;
8075 }
8076 }
8077 }
8078}
8079
8080/**
8081 * @brief Compile features in the current module and all its submodules.
8082 *
8083 * @param[in] ctx Compile context.
8084 * @return LY_ERR value.
8085 */
8086static LY_ERR
8087lys_compile_features(struct lysc_ctx *ctx)
8088{
8089 struct lysp_submodule *submod;
8090 LY_ARRAY_COUNT_TYPE u, v;
8091
8092 if (!ctx->mod->features) {
8093 /* features are compiled directly into the module structure,
8094 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
8095 LY_CHECK_RET(lys_feature_precompile(ctx, NULL, NULL, ctx->mod->parsed->features, &ctx->mod->features));
8096 LY_ARRAY_FOR(ctx->mod->parsed->includes, v) {
8097 submod = ctx->mod->parsed->includes[v].submodule;
8098 LY_CHECK_RET(lys_feature_precompile(ctx, NULL, NULL, submod->features, &ctx->mod->features));
8099 }
8100 }
8101
8102 /* finish feature compilation, not only for the main module, but also for the submodules.
8103 * Due to possible forward references, it must be done when all the features (including submodules)
8104 * are present. */
8105 LY_ARRAY_FOR(ctx->mod->parsed->features, u) {
8106 LY_CHECK_RET(lys_feature_precompile_finish(ctx, &ctx->mod->parsed->features[u], ctx->mod->features));
8107 }
8108
8109 lysc_update_path(ctx, NULL, "{submodule}");
8110 LY_ARRAY_FOR(ctx->mod->parsed->includes, v) {
8111 submod = ctx->mod->parsed->includes[v].submodule;
8112
8113 lysc_update_path(ctx, NULL, submod->name);
8114 LY_ARRAY_FOR(submod->features, u) {
8115 LY_CHECK_RET(lys_feature_precompile_finish(ctx, &submod->features[u], ctx->mod->features));
8116 }
8117 lysc_update_path(ctx, NULL, NULL);
8118 }
8119 lysc_update_path(ctx, NULL, NULL);
8120
8121 return LY_SUCCESS;
8122}
8123
8124/**
8125 * @brief Compile identites in the current module and all its submodules.
8126 *
8127 * @param[in] ctx Compile context.
8128 * @return LY_ERR value.
8129 */
8130static LY_ERR
8131lys_compile_identities(struct lysc_ctx *ctx)
8132{
8133 struct lysp_submodule *submod;
8134 LY_ARRAY_COUNT_TYPE u;
8135
8136 if (!ctx->mod->identities) {
8137 LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, ctx->mod->parsed->identities, &ctx->mod->identities));
8138 LY_ARRAY_FOR(ctx->mod->parsed->includes, u) {
8139 submod = ctx->mod->parsed->includes[u].submodule;
8140 LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, submod->identities, &ctx->mod->identities));
8141 }
8142 }
8143
8144 if (ctx->mod->parsed->identities) {
8145 LY_CHECK_RET(lys_compile_identities_derived(ctx, ctx->mod->parsed->identities, ctx->mod->identities));
8146 }
8147 lysc_update_path(ctx, NULL, "{submodule}");
8148 LY_ARRAY_FOR(ctx->mod->parsed->includes, u) {
8149
8150 submod = ctx->mod->parsed->includes[u].submodule;
8151 if (submod->identities) {
8152 lysc_update_path(ctx, NULL, submod->name);
8153 LY_CHECK_RET(lys_compile_identities_derived(ctx, submod->identities, ctx->mod->identities));
8154 lysc_update_path(ctx, NULL, NULL);
8155 }
8156 }
8157 lysc_update_path(ctx, NULL, NULL);
8158
Michal Vasko004d3152020-06-11 19:59:22 +02008159 return LY_SUCCESS;
8160}
8161
Radek Krejci19a96102018-11-15 13:38:09 +01008162LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02008163lys_compile(struct lys_module *mod, uint32_t options)
Radek Krejci19a96102018-11-15 13:38:09 +01008164{
8165 struct lysc_ctx ctx = {0};
8166 struct lysc_module *mod_c;
8167 struct lysp_module *sp;
Michal Vasko7f45cf22020-10-01 12:49:44 +02008168 struct lysp_submodule *submod;
8169 struct lysp_node *pnode;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02008170 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01008171 struct lys_module *m;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02008172 LY_ARRAY_COUNT_TYPE u, v;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02008173 uint32_t i;
Radek Krejcid05cbd92018-12-05 14:26:40 +01008174 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01008175
Michal Vasko7a0b0762020-09-02 16:37:01 +02008176 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01008177
Michal Vasko7a0b0762020-09-02 16:37:01 +02008178 if (!mod->implemented) {
Radek Krejci096235c2019-01-11 11:12:19 +01008179 /* just imported modules are not compiled */
8180 return LY_SUCCESS;
8181 }
8182
Michal Vasko7f45cf22020-10-01 12:49:44 +02008183 /* context will be changed */
8184 ++mod->ctx->module_set_id;
8185
Michal Vasko7a0b0762020-09-02 16:37:01 +02008186 sp = mod->parsed;
Radek Krejci19a96102018-11-15 13:38:09 +01008187
Michal Vasko7a0b0762020-09-02 16:37:01 +02008188 ctx.ctx = mod->ctx;
8189 ctx.mod = mod;
8190 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02008191 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02008192 ctx.path_len = 1;
8193 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01008194
Michal Vasko7a0b0762020-09-02 16:37:01 +02008195 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
8196 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
8197 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01008198
Michal Vasko7f45cf22020-10-01 12:49:44 +02008199 /* process imports */
Michal Vasko7c8439f2020-08-05 13:25:19 +02008200 LY_ARRAY_FOR(sp->imports, u) {
8201 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
8202 }
Radek Krejci0935f412019-08-20 16:15:18 +02008203
Michal Vasko7f45cf22020-10-01 12:49:44 +02008204 /* features */
8205 LY_CHECK_GOTO(ret = lys_compile_features(&ctx), error);
Radek Krejci14915cc2020-09-14 17:28:13 +02008206
8207 /* identities, work similarly to features with the precompilation */
Michal Vasko7f45cf22020-10-01 12:49:44 +02008208 LY_CHECK_GOTO(ret = lys_compile_identities(&ctx), error);
8209
8210 /* augments and deviations */
8211 LY_CHECK_GOTO(ret = lys_precompile_augments_deviations(&ctx), error);
8212
8213 /* compile augments and deviations of our module from other modules so they can be applied during compilation */
8214 LY_CHECK_GOTO(ret = lys_precompile_own_augments(&ctx), error);
8215 LY_CHECK_GOTO(ret = lys_precompile_own_deviations(&ctx), error);
Radek Krejci19a96102018-11-15 13:38:09 +01008216
Radek Krejci95710c92019-02-11 15:49:55 +01008217 /* data nodes */
Michal Vasko7f45cf22020-10-01 12:49:44 +02008218 LY_LIST_FOR(sp->data, pnode) {
8219 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), error);
Radek Krejci19a96102018-11-15 13:38:09 +01008220 }
Radek Krejci95710c92019-02-11 15:49:55 +01008221
Michal Vasko7f45cf22020-10-01 12:49:44 +02008222 /* top-level RPCs and notifications */
8223 COMPILE_OP_ARRAY_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
8224 COMPILE_OP_ARRAY_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01008225
Michal Vasko7f45cf22020-10-01 12:49:44 +02008226 /* extension instances */
Radek Krejci0935f412019-08-20 16:15:18 +02008227 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01008228
Michal Vasko7f45cf22020-10-01 12:49:44 +02008229 /* the same for submodules */
8230 LY_ARRAY_FOR(sp->includes, u) {
8231 submod = sp->includes[u].submodule;
8232 LY_LIST_FOR(submod->data, pnode) {
8233 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
8234 LY_CHECK_GOTO(ret, error);
8235 }
8236
8237 COMPILE_OP_ARRAY_GOTO(&ctx, submod->rpcs, mod_c->rpcs, NULL, v, lys_compile_action, 0, ret, error);
8238 COMPILE_OP_ARRAY_GOTO(&ctx, submod->notifs, mod_c->notifs, NULL, v, lys_compile_notif, 0, ret, error);
8239
8240 COMPILE_EXTS_GOTO(&ctx, submod->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
8241 }
8242
Michal Vasko004d3152020-06-11 19:59:22 +02008243 /* finish compilation for all unresolved items in the context */
8244 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
Radek Krejci474f9b82019-07-24 11:36:37 +02008245
Radek Krejcif2de0ed2019-05-02 14:13:18 +02008246 /* validate non-instantiated groupings from the parsed schema,
8247 * without it we would accept even the schemas with invalid grouping specification */
8248 ctx.options |= LYSC_OPT_GROUPING;
8249 LY_ARRAY_FOR(sp->groupings, u) {
8250 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02008251 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02008252 }
8253 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02008254 LY_LIST_FOR(sp->data, pnode) {
8255 grps = (struct lysp_grp *)lysp_node_groupings(pnode);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02008256 LY_ARRAY_FOR(grps, u) {
8257 if (!(grps[u].flags & LYS_USED_GRP)) {
Michal Vasko7f45cf22020-10-01 12:49:44 +02008258 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02008259 }
8260 }
8261 }
Michal Vasko7f45cf22020-10-01 12:49:44 +02008262 LY_ARRAY_FOR(sp->includes, u) {
8263 submod = sp->includes[u].submodule;
8264 LY_ARRAY_FOR(submod->groupings, u) {
8265 if (!(submod->groupings[u].flags & LYS_USED_GRP)) {
8266 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &submod->groupings[u]), error);
8267 }
8268 }
8269 LY_LIST_FOR(submod->data, pnode) {
8270 grps = (struct lysp_grp *)lysp_node_groupings(pnode);
8271 LY_ARRAY_FOR(grps, u) {
8272 if (!(grps[u].flags & LYS_USED_GRP)) {
8273 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &grps[u]), error);
8274 }
8275 }
8276 }
Radek Krejci474f9b82019-07-24 11:36:37 +02008277 }
8278
Michal Vasko8d544252020-03-02 10:19:52 +01008279#if 0
8280 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
8281 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
8282 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
8283 * the anotation definitions available in the internal schema structure. */
8284 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
8285 if (lyp_add_ietf_netconf_annotations(mod)) {
8286 lys_free(mod, NULL, 1, 1);
8287 return NULL;
8288 }
8289 }
8290#endif
8291
8292 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
Michal Vasko7a0b0762020-09-02 16:37:01 +02008293 if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
8294 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error);
Michal Vasko8d544252020-03-02 10:19:52 +01008295 }
8296
Michal Vasko7f45cf22020-10-01 12:49:44 +02008297 /* there can be no leftover deviations */
8298 LY_CHECK_ERR_GOTO(ctx.devs.count, LOGINT(ctx.ctx); ret = LY_EINT, error);
8299
8300 for (i = 0; i < ctx.dflts.count; ++i) {
8301 lysc_unres_dflt_free(ctx.ctx, ctx.dflts.objs[i]);
8302 }
8303 ly_set_erase(&ctx.dflts, NULL);
Michal Vasko004d3152020-06-11 19:59:22 +02008304 ly_set_erase(&ctx.xpath, NULL);
8305 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01008306 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02008307 ly_set_erase(&ctx.tpdf_chain, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02008308 ly_set_erase(&ctx.augs, NULL);
8309 ly_set_erase(&ctx.devs, NULL);
8310 ly_set_erase(&ctx.uses_augs, NULL);
8311 ly_set_erase(&ctx.uses_rfns, NULL);
Radek Krejcia3045382018-11-22 14:30:31 +01008312
Radek Krejciec4da802019-05-02 13:02:41 +02008313 if (ctx.options & LYSC_OPT_FREE_SP) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02008314 lysp_module_free(mod->parsed);
8315 mod->parsed = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01008316 }
8317
Radek Krejciec4da802019-05-02 13:02:41 +02008318 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01008319 /* remove flag of the modules implemented by dependency */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02008320 for (i = 0; i < ctx.ctx->list.count; ++i) {
8321 m = ctx.ctx->list.objs[i];
Radek Krejcia46012b2020-08-12 15:41:04 +02008322 if (m->implemented > 1) {
Radek Krejci95710c92019-02-11 15:49:55 +01008323 m->implemented = 1;
8324 }
8325 }
8326 }
8327
Radek Krejci19a96102018-11-15 13:38:09 +01008328 return LY_SUCCESS;
8329
8330error:
Michal Vasko7f45cf22020-10-01 12:49:44 +02008331 lys_precompile_augments_deviations_revert(&ctx, mod);
Michal Vasko7a0b0762020-09-02 16:37:01 +02008332 lys_feature_precompile_revert(&ctx, mod);
Michal Vasko7f45cf22020-10-01 12:49:44 +02008333 for (i = 0; i < ctx.dflts.count; ++i) {
8334 lysc_unres_dflt_free(ctx.ctx, ctx.dflts.objs[i]);
8335 }
8336 ly_set_erase(&ctx.dflts, NULL);
Michal Vasko004d3152020-06-11 19:59:22 +02008337 ly_set_erase(&ctx.xpath, NULL);
8338 ly_set_erase(&ctx.leafrefs, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01008339 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02008340 ly_set_erase(&ctx.tpdf_chain, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02008341 for (i = 0; i < ctx.augs.count; ++i) {
8342 lysc_augment_free(ctx.ctx, ctx.augs.objs[i]);
8343 }
8344 ly_set_erase(&ctx.augs, NULL);
8345 for (i = 0; i < ctx.devs.count; ++i) {
8346 lysc_deviation_free(ctx.ctx, ctx.devs.objs[i]);
8347 }
8348 ly_set_erase(&ctx.devs, NULL);
8349 for (i = 0; i < ctx.uses_augs.count; ++i) {
8350 lysc_augment_free(ctx.ctx, ctx.uses_augs.objs[i]);
8351 }
8352 ly_set_erase(&ctx.uses_augs, NULL);
8353 for (i = 0; i < ctx.uses_rfns.count; ++i) {
8354 lysc_refine_free(ctx.ctx, ctx.uses_rfns.objs[i]);
8355 }
8356 ly_set_erase(&ctx.uses_rfns, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01008357 lysc_module_free(mod_c, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02008358 mod->compiled = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01008359
Michal Vasko7f45cf22020-10-01 12:49:44 +02008360 /* revert compilation of modules implemented by dependency */
8361 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
8362 for (i = 0; i < ctx.ctx->list.count; ++i) {
8363 m = ctx.ctx->list.objs[i];
8364 if (m->implemented > 1) {
8365 /* make the module non-implemented */
8366 m->implemented = 0;
8367 }
8368
8369 /* free the compiled version of the module, if any */
Radek Krejci95710c92019-02-11 15:49:55 +01008370 lysc_module_free(m->compiled, NULL);
8371 m->compiled = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +02008372
8373 if (m->implemented) {
8374 /* recompile, must succeed because it was already compiled; hide messages because any
8375 * warnings were already printed, are not really relevant, and would hide the real error */
8376 uint32_t prev_lo = ly_log_options(0);
8377 LY_ERR r = lys_compile(m, LYSC_OPT_INTERNAL);
8378 ly_log_options(prev_lo);
8379 if (r) {
8380 LOGERR(ctx.ctx, r, "Recompilation of module \"%s\" failed.", m->name);
8381 }
8382 }
Radek Krejci95710c92019-02-11 15:49:55 +01008383 }
8384 }
8385
Radek Krejci19a96102018-11-15 13:38:09 +01008386 return ret;
8387}