blob: 71a3fedbf1eeabefa40a5b73c7164ef975da4b81 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko19a09022021-06-15 11:54:08 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02005 * @brief Schema compilation.
6 *
Michal Vaskoedb0fa52022-10-04 10:36:00 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
16#define _GNU_SOURCE
17
18#include "schema_compile.h"
19
20#include <assert.h>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020021#include <stddef.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include "common.h"
28#include "compat.h"
29#include "context.h"
30#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020031#include "in.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "log.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020033#include "parser_schema.h"
34#include "path.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020035#include "plugins.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020036#include "plugins_exts.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010037#include "plugins_internal.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "plugins_types.h"
39#include "schema_compile_amend.h"
40#include "schema_compile_node.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010041#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020042#include "set.h"
43#include "tree.h"
44#include "tree_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020045#include "tree_schema.h"
Michal Vaskoc636ea42022-09-16 10:20:31 +020046#include "tree_schema_free.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020047#include "tree_schema_internal.h"
48#include "xpath.h"
49
Michal Vaskoa0ba01e2022-10-19 13:26:57 +020050void
51lysc_update_path(struct lysc_ctx *ctx, const struct lys_module *parent_module, const char *name)
52{
53 int len;
54 uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
55
56 if (!name) {
57 /* removing last path segment */
58 if (ctx->path[ctx->path_len - 1] == '}') {
59 for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
60 if (ctx->path[ctx->path_len] == '=') {
61 ctx->path[ctx->path_len++] = '}';
62 } else {
63 /* not a top-level special tag, remove also preceiding '/' */
64 goto remove_nodelevel;
65 }
66 } else {
67remove_nodelevel:
68 for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
69 if (ctx->path_len == 0) {
70 /* top-level (last segment) */
71 ctx->path_len = 1;
72 }
73 }
74 /* set new terminating NULL-byte */
75 ctx->path[ctx->path_len] = '\0';
76 } else {
77 if (ctx->path_len > 1) {
78 if (!parent_module && (ctx->path[ctx->path_len - 1] == '}') && (ctx->path[ctx->path_len - 2] != '\'')) {
79 /* extension of the special tag */
80 nextlevel = 2;
81 --ctx->path_len;
82 } else {
83 /* there is already some path, so add next level */
84 nextlevel = 1;
85 }
86 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
87
88 if (nextlevel != 2) {
89 if ((parent_module && (parent_module == ctx->cur_mod)) || (!parent_module && (ctx->path_len > 1) && (name[0] == '{'))) {
90 /* module not changed, print the name unprefixed */
91 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
92 } else {
93 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->cur_mod->name, name);
94 }
95 } else {
96 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
97 }
98 if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
99 /* output truncated */
100 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
101 } else {
102 ctx->path_len += len;
103 }
104 }
105
106 LOG_LOCBACK(0, 0, 1, 0);
107 LOG_LOCSET(NULL, NULL, ctx->path, NULL);
108}
109
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200110/**
111 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
Michal Vasko193dacd2022-10-13 08:43:05 +0200112 *
113 * @param[in] ctx Compile context.
114 * @param[in] extp Parsed extension instance.
115 * @param[out] ext Compiled extension definition.
116 * @return LY_ERR value.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200117 */
118static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200119lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext_instance *extp, struct lysc_ext **ext)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200120{
121 LY_ERR ret = LY_SUCCESS;
Michal Vasko193dacd2022-10-13 08:43:05 +0200122 struct lysp_ext *ep = extp->def;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200123
Michal Vasko193dacd2022-10-13 08:43:05 +0200124 if (!ep->compiled) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200125 lysc_update_path(ctx, NULL, "{extension}");
Michal Vasko193dacd2022-10-13 08:43:05 +0200126 lysc_update_path(ctx, NULL, ep->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200127
128 /* compile the extension definition */
Michal Vasko193dacd2022-10-13 08:43:05 +0200129 *ext = ep->compiled = calloc(1, sizeof **ext);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200130 (*ext)->refcount = 1;
Michal Vasko193dacd2022-10-13 08:43:05 +0200131 DUP_STRING_GOTO(ctx->ctx, ep->name, (*ext)->name, ret, cleanup);
132 DUP_STRING_GOTO(ctx->ctx, ep->argname, (*ext)->argname, ret, cleanup);
133 LY_CHECK_GOTO(ret = lysp_ext_find_definition(ctx->ctx, extp, (const struct lys_module **)&(*ext)->module, NULL),
134 cleanup);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200135
136 /* compile nested extensions */
Michal Vasko193dacd2022-10-13 08:43:05 +0200137 COMPILE_EXTS_GOTO(ctx, ep->exts, (*ext)->exts, *ext, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200138
139 lysc_update_path(ctx, NULL, NULL);
140 lysc_update_path(ctx, NULL, NULL);
141
142 /* find extension definition plugin */
Michal Vasko193dacd2022-10-13 08:43:05 +0200143 (*ext)->plugin = extp->record ? (struct lyplg_ext *)&extp->record->plugin : NULL;
Michal Vasko54b11952022-06-08 12:29:39 +0200144 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200145
Michal Vasko193dacd2022-10-13 08:43:05 +0200146 *ext = ep->compiled;
Michal Vaskoc636ea42022-09-16 10:20:31 +0200147
Michal Vasko193dacd2022-10-13 08:43:05 +0200148cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100149 if (ret) {
150 lysc_update_path(ctx, NULL, NULL);
151 lysc_update_path(ctx, NULL, NULL);
152 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200153 return ret;
154}
155
156LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200157lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *extp, struct lysc_ext_instance *ext, void *parent)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200158{
Radek Krejci85ac8312021-03-03 20:21:33 +0100159 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200160
Michal Vasko193dacd2022-10-13 08:43:05 +0200161 DUP_STRING_GOTO(ctx->ctx, extp->argument, ext->argument, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200162 ext->module = ctx->cur_mod;
163 ext->parent = parent;
Michal Vasko193dacd2022-10-13 08:43:05 +0200164 ext->parent_stmt = extp->parent_stmt;
165 ext->parent_stmt_index = extp->parent_stmt_index;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200166
Michal Vasko193dacd2022-10-13 08:43:05 +0200167 lysc_update_path(ctx, (ext->parent_stmt & LY_STMT_NODE_MASK) ? ((struct lysc_node *)ext->parent)->module : NULL,
168 "{extension}");
169 lysc_update_path(ctx, NULL, extp->name);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100170
Michal Vasko193dacd2022-10-13 08:43:05 +0200171 /* compile extension if not already */
172 LY_CHECK_GOTO(ret = lys_compile_extension(ctx, extp, &ext->def), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200173
Michal Vasko193dacd2022-10-13 08:43:05 +0200174 /* compile */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200175 if (ext->def->plugin && ext->def->plugin->compile) {
176 if (ext->argument) {
Radek Krejcia6016992021-03-03 10:13:41 +0100177 lysc_update_path(ctx, ext->module, ext->argument);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200178 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200179 ret = ext->def->plugin->compile(ctx, extp, ext);
Radek Krejcicc21a4f2021-02-09 15:23:31 +0100180 if (ret == LY_ENOT) {
Michal Vaskoc636ea42022-09-16 10:20:31 +0200181 lysc_ext_instance_free(&ctx->free_ctx, ext);
Radek Krejcicc21a4f2021-02-09 15:23:31 +0100182 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200183 if (ext->argument) {
184 lysc_update_path(ctx, NULL, NULL);
185 }
Michal Vaskofc2cd072021-02-24 13:17:17 +0100186 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200187 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200188
189cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100190 lysc_update_path(ctx, NULL, NULL);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100191 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200192 return ret;
193}
194
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200195static void
Michal Vaskoc130e162021-10-19 11:30:00 +0200196lysc_unres_must_free(struct lysc_unres_must *m)
197{
198 LY_ARRAY_FREE(m->local_mods);
199 free(m);
200}
201
202static void
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200203lysc_unres_dflt_free(const struct ly_ctx *ctx, struct lysc_unres_dflt *r)
204{
205 assert(!r->dflt || !r->dflts);
206 if (r->dflt) {
207 lysp_qname_free((struct ly_ctx *)ctx, r->dflt);
208 free(r->dflt);
209 } else {
210 FREE_ARRAY((struct ly_ctx *)ctx, r->dflts, lysp_qname_free);
211 }
212 free(r);
213}
214
Michal Vasko193dacd2022-10-13 08:43:05 +0200215LY_ERR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200216lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
Michal Vasko9b232082022-06-07 10:59:31 +0200217 const struct lysp_ident *identities_p, struct lysc_ident **identities)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200218{
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100219 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoc636ea42022-09-16 10:20:31 +0200220 struct lysc_ctx cctx;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100221 struct lysc_ident *ident;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200222 LY_ERR ret = LY_SUCCESS;
223
224 assert(ctx_sc || ctx);
225
226 if (!ctx_sc) {
Michal Vaskoc636ea42022-09-16 10:20:31 +0200227 if (parsed_mod) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200228 LYSC_CTX_INIT_PMOD(cctx, parsed_mod, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200229 } else {
230 LYSC_CTX_INIT_CTX(cctx, ctx);
231 }
232 ctx_sc = &cctx;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200233 }
234
235 if (!identities_p) {
236 return LY_SUCCESS;
237 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200238
239 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200240 LY_ARRAY_FOR(identities_p, u) {
241 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
242
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100243 /* add new compiled identity */
Michal Vasko9b232082022-06-07 10:59:31 +0200244 LY_ARRAY_NEW_GOTO(ctx_sc->ctx, *identities, ident, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100245
246 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, ident->name, ret, done);
247 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, ident->dsc, ret, done);
248 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, ident->ref, ret, done);
249 ident->module = ctx_sc->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200250 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
Radek Krejciab430862021-03-02 20:13:40 +0100251 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, ident->exts, ident, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100252 ident->flags = identities_p[u].flags;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200253
254 lysc_update_path(ctx_sc, NULL, NULL);
255 }
256 lysc_update_path(ctx_sc, NULL, NULL);
Michal Vasko9b232082022-06-07 10:59:31 +0200257
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200258done:
Michal Vasko9b232082022-06-07 10:59:31 +0200259 if (ret) {
260 lysc_update_path(ctx_sc, NULL, NULL);
261 lysc_update_path(ctx_sc, NULL, NULL);
262 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200263 return ret;
264}
265
266/**
267 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
268 *
269 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
270 *
271 * @param[in] ctx Compile context for logging.
272 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
273 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
274 * @return LY_SUCCESS if everything is ok.
275 * @return LY_EVALID if the identity is derived from itself.
276 */
277static LY_ERR
278lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
279{
280 LY_ERR ret = LY_SUCCESS;
281 LY_ARRAY_COUNT_TYPE u, v;
282 struct ly_set recursion = {0};
283 struct lysc_ident *drv;
284
285 if (!derived) {
286 return LY_SUCCESS;
287 }
288
289 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
290 if (ident == derived[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100291 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200292 "Identity \"%s\" is indirectly derived from itself.", ident->name);
293 ret = LY_EVALID;
294 goto cleanup;
295 }
296 ret = ly_set_add(&recursion, derived[u], 0, NULL);
297 LY_CHECK_GOTO(ret, cleanup);
298 }
299
300 for (v = 0; v < recursion.count; ++v) {
301 drv = recursion.objs[v];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200302 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
303 if (ident == drv->derived[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100304 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200305 "Identity \"%s\" is indirectly derived from itself.", ident->name);
306 ret = LY_EVALID;
307 goto cleanup;
308 }
309 ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
310 LY_CHECK_GOTO(ret, cleanup);
311 }
312 }
313
314cleanup:
315 ly_set_erase(&recursion, NULL);
316 return ret;
317}
318
319LY_ERR
320lys_compile_identity_bases(struct lysc_ctx *ctx, const struct lysp_module *base_pmod, const char **bases_p,
aPiecekf4a0a192021-08-03 15:14:17 +0200321 struct lysc_ident *ident, struct lysc_ident ***bases)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200322{
323 LY_ARRAY_COUNT_TYPE u, v;
324 const char *s, *name;
325 const struct lys_module *mod;
326 struct lysc_ident **idref;
327
aPiecekf4a0a192021-08-03 15:14:17 +0200328 assert(ident || bases);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200329
330 if ((LY_ARRAY_COUNT(bases_p) > 1) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100331 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200332 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
333 return LY_EVALID;
334 }
335
336 LY_ARRAY_FOR(bases_p, u) {
337 s = strchr(bases_p[u], ':');
338 if (s) {
339 /* prefixed identity */
340 name = &s[1];
Radek Krejci8df109d2021-04-23 12:19:08 +0200341 mod = ly_resolve_prefix(ctx->ctx, bases_p[u], s - bases_p[u], LY_VALUE_SCHEMA, (void *)base_pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200342 } else {
343 name = bases_p[u];
344 mod = base_pmod->mod;
345 }
346 if (!mod) {
347 if (ident) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100348 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200349 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
350 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100351 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200352 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
353 }
354 return LY_EVALID;
355 }
356
357 idref = NULL;
358 LY_ARRAY_FOR(mod->identities, v) {
359 if (!strcmp(name, mod->identities[v].name)) {
360 if (ident) {
361 if (ident == &mod->identities[v]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100362 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200363 "Identity \"%s\" is derived from itself.", ident->name);
364 return LY_EVALID;
365 }
366 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived));
367 /* we have match! store the backlink */
368 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
369 *idref = ident;
370 } else {
371 /* we have match! store the found identity */
372 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
373 *idref = &mod->identities[v];
374 }
375 break;
376 }
377 }
aPiecekf4a0a192021-08-03 15:14:17 +0200378 if (!idref) {
Radek Krejci10443f42021-03-28 17:40:35 +0200379 if (ident) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100380 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200381 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
382 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100383 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200384 "Unable to find base (%s) of identityref.", bases_p[u]);
385 }
386 return LY_EVALID;
387 }
388 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100389
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200390 return LY_SUCCESS;
391}
392
393/**
394 * @brief For the given array of identities, set the backlinks from all their base identities.
Michal Vasko193dacd2022-10-13 08:43:05 +0200395 *
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200396 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
397 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
aPiecekf4a0a192021-08-03 15:14:17 +0200398 * @param[in,out] idents Array of referencing identities to which the backlinks are supposed to be set.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200399 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
400 */
401static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100402lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident **idents)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200403{
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100404 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200405
406 lysc_update_path(ctx, NULL, "{identity}");
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100407
aPiecekf3e2db82021-08-05 10:18:43 +0200408 for (u = 0; u < LY_ARRAY_COUNT(*idents); ++u) {
aPiecekf4a0a192021-08-03 15:14:17 +0200409 /* find matching parsed identity */
aPiecekf3e2db82021-08-05 10:18:43 +0200410 for (v = 0; v < LY_ARRAY_COUNT(idents_p); ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100411 if (idents_p[v].name == (*idents)[u].name) {
412 break;
413 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100414 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100415
Michal Vaskodc6f84c2021-06-15 14:50:27 +0200416 if ((v == LY_ARRAY_COUNT(idents_p)) || !idents_p[v].bases) {
417 /* identity not found (it may be from a submodule) or identity without bases */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200418 continue;
419 }
Michal Vaskodc6f84c2021-06-15 14:50:27 +0200420
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100421 lysc_update_path(ctx, NULL, (*idents)[u].name);
aPiecekf4a0a192021-08-03 15:14:17 +0200422 LY_CHECK_RET(lys_compile_identity_bases(ctx, ctx->pmod, idents_p[v].bases, &(*idents)[u], NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200423 lysc_update_path(ctx, NULL, NULL);
424 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100425
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200426 lysc_update_path(ctx, NULL, NULL);
427 return LY_SUCCESS;
428}
429
Michal Vaskoe20599c2022-12-02 10:45:01 +0100430LY_ERR
431lys_compile_expr_implement(const struct ly_ctx *ctx, const struct lyxp_expr *expr, LY_VALUE_FORMAT format,
432 void *prefix_data, ly_bool implement, struct lys_glob_unres *unres, const struct lys_module **mod_p)
433{
434 uint32_t i;
435 const char *ptr, *start, **imp_f, *all_f[] = {"*", NULL};
436 const struct lys_module *mod;
437
438 assert(implement || mod_p);
439
440 for (i = 0; i < expr->used; ++i) {
441 if ((expr->tokens[i] != LYXP_TOKEN_NAMETEST) && (expr->tokens[i] != LYXP_TOKEN_LITERAL)) {
442 /* token cannot have a prefix */
443 continue;
444 }
445
446 start = expr->expr + expr->tok_pos[i];
447 if (!(ptr = ly_strnchr(start, ':', expr->tok_len[i]))) {
448 /* token without a prefix */
449 continue;
450 }
451
452 if (!(mod = ly_resolve_prefix(ctx, start, ptr - start, format, prefix_data))) {
453 /* unknown prefix, do not care right now */
454 continue;
455 }
456
457 /* unimplemented module found */
458 if (!mod->implemented && !implement) {
459 /* should not be implemented now */
460 *mod_p = mod;
461 break;
462 }
463
464 if (!mod->implemented) {
465 /* implement if not implemented */
466 imp_f = (ctx->flags & LY_CTX_ENABLE_IMP_FEATURES) ? all_f : NULL;
467 LY_CHECK_RET(lys_implement((struct lys_module *)mod, imp_f, unres));
468 }
469 if (!mod->compiled) {
470 /* compile if not implemented before or only marked for compilation */
471 LY_CHECK_RET(lys_compile((struct lys_module *)mod, &unres->ds_unres));
472 }
473 }
474
475 return LY_SUCCESS;
476}
477
478/**
479 * @brief Check and optionally implement modules referenced by a when expression.
480 *
481 * @param[in] ctx Compile context.
482 * @param[in] when When to check.
483 * @param[in,out] unres Global unres structure.
484 * @return LY_ERECOMPILE if the whole dep set needs to be recompiled for these whens to evaluate.
485 * @return LY_ENOT if full check of this when should be skipped.
486 * @return LY_ERR value on error.
487 */
488static LY_ERR
489lys_compile_unres_when_implement(struct lysc_ctx *ctx, const struct lysc_when *when, struct lys_glob_unres *unres)
490{
491 LY_ERR rc = LY_SUCCESS;
492 const struct lys_module *mod = NULL;
493
494 /* check whether all the referenced modules are implemented */
495 rc = lys_compile_expr_implement(ctx->ctx, when->cond, LY_VALUE_SCHEMA_RESOLVED, when->prefixes,
496 ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, unres, &mod);
497 if (rc) {
498 goto cleanup;
499 } else if (mod) {
500 LOGWRN(ctx->ctx, "When condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
501 when->cond->expr, mod->name);
502 rc = LY_ENOT;
503 goto cleanup;
504 }
505
506cleanup:
507 return rc;
508}
509
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200510/**
511 * @brief Check when for cyclic dependencies.
512 *
513 * @param[in] set Set with all the referenced nodes.
514 * @param[in] node Node whose "when" referenced nodes are in @p set.
515 * @return LY_ERR value
516 */
517static LY_ERR
518lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
519{
520 struct lyxp_set tmp_set;
521 struct lyxp_set_scnode *xp_scnode;
Michal Vaskoe4a6d012023-05-22 14:34:52 +0200522 uint32_t i, j, idx;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200523 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200524 LY_ERR ret = LY_SUCCESS;
525
526 memset(&tmp_set, 0, sizeof tmp_set);
527
528 /* prepare in_ctx of the set */
529 for (i = 0; i < set->used; ++i) {
530 xp_scnode = &set->val.scnodes[i];
531
Radek Krejcif13b87b2020-12-01 22:02:17 +0100532 if (xp_scnode->in_ctx != LYXP_SET_SCNODE_START_USED) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200533 /* check node when, skip the context node (it was just checked) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100534 xp_scnode->in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200535 }
536 }
537
538 for (i = 0; i < set->used; ++i) {
539 xp_scnode = &set->val.scnodes[i];
Radek Krejcif13b87b2020-12-01 22:02:17 +0100540 if (xp_scnode->in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200541 /* already checked */
542 continue;
543 }
544
Michal Vasko1a09b212021-05-06 13:00:10 +0200545 if ((xp_scnode->type != LYXP_NODE_ELEM) || !lysc_node_when(xp_scnode->scnode)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200546 /* no when to check */
Michal Vasko1a09b212021-05-06 13:00:10 +0200547 xp_scnode->in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200548 continue;
549 }
550
551 node = xp_scnode->scnode;
552 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100553 struct lysc_when **when_list, *when;
554
Radek Krejciddace2c2021-01-08 11:30:56 +0100555 LOG_LOCSET(node, NULL, NULL, NULL);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100556 when_list = lysc_node_when(node);
557 LY_ARRAY_FOR(when_list, u) {
558 when = when_list[u];
Radek Krejci8df109d2021-04-23 12:19:08 +0200559 ret = lyxp_atomize(set->ctx, when->cond, node->module, LY_VALUE_SCHEMA_RESOLVED, when->prefixes,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +0200560 when->context, when->context, &tmp_set, LYXP_SCNODE_SCHEMA);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200561 if (ret != LY_SUCCESS) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100562 LOGVAL(set->ctx, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vasko59e69e72022-02-18 09:18:21 +0100563 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200564 goto cleanup;
565 }
566
567 for (j = 0; j < tmp_set.used; ++j) {
Michal Vaskoe4a6d012023-05-22 14:34:52 +0200568 if (tmp_set.val.scnodes[j].type != LYXP_NODE_ELEM) {
569 /* skip roots'n'stuff, no when, nothing to check */
Michal Vasko1a09b212021-05-06 13:00:10 +0200570 tmp_set.val.scnodes[j].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vaskoe4a6d012023-05-22 14:34:52 +0200571 continue;
572 }
573
574 /* try to find this node in our set */
575 if (lyxp_set_scnode_contains(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1, &idx) &&
576 (set->val.scnodes[idx].in_ctx == LYXP_SET_SCNODE_START_USED)) {
577 LOGVAL(set->ctx, LYVE_SEMANTICS, "When condition cyclic dependency on the node \"%s\".",
578 tmp_set.val.scnodes[j].scnode->name);
579 ret = LY_EVALID;
580 LOG_LOCBACK(1, 0, 0, 0);
581 goto cleanup;
582 }
583
584 /* needs to be checked, if in both sets, will be ignored */
585 tmp_set.val.scnodes[j].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
586 }
587
588 if (when->context != node) {
589 /* node actually depends on this "when", not the context node */
590 assert(tmp_set.val.scnodes[0].scnode == when->context);
591 if (tmp_set.val.scnodes[0].in_ctx == LYXP_SET_SCNODE_START_USED) {
592 /* replace the non-traversed context node with the dependent node */
593 tmp_set.val.scnodes[0].scnode = (struct lysc_node *)node;
594 } else {
595 /* context node was traversed, so just add the dependent node */
596 ret = lyxp_set_scnode_insert_node(&tmp_set, node, LYXP_SET_SCNODE_START_USED, LYXP_AXIS_CHILD, NULL);
597 LY_CHECK_ERR_GOTO(ret, LOG_LOCBACK(1, 0, 0, 0), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200598 }
599 }
600
601 /* merge this set into the global when set */
602 lyxp_set_scnode_merge(set, &tmp_set);
603 }
Michal Vaskoe4a6d012023-05-22 14:34:52 +0200604 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200605
606 /* check when of non-data parents as well */
607 node = node->parent;
608 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
609
610 /* this node when was checked (xp_scnode could have been reallocd) */
Michal Vasko60edc2f2021-07-20 09:18:02 +0200611 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200612 }
613
614cleanup:
615 lyxp_set_free_content(&tmp_set);
616 return ret;
617}
618
619LY_ERR
620lysc_check_status(struct lysc_ctx *ctx, uint16_t flags1, void *mod1, const char *name1, uint16_t flags2, void *mod2,
621 const char *name2)
622{
623 uint16_t flg1, flg2;
624
625 flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
626 flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
627
628 if ((flg1 < flg2) && (mod1 == mod2)) {
629 if (ctx) {
Michal Vasko23864e02021-06-24 09:23:58 +0200630 LOGVAL(ctx->ctx, LYVE_REFERENCE, "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200631 flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1,
632 flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2);
633 }
634 return LY_EVALID;
635 }
636
637 return LY_SUCCESS;
638}
639
Michal Vaskocfaff232020-10-20 09:35:14 +0200640/**
Michal Vaskoc130e162021-10-19 11:30:00 +0200641 * @brief Check when expressions of a node on a complete compiled schema tree.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200642 *
643 * @param[in] ctx Compile context.
Michal Vasko4ed3bd52022-12-02 10:28:04 +0100644 * @param[in] when When to check.
645 * @param[in] node Node with @p when.
Michal Vasko4ed3bd52022-12-02 10:28:04 +0100646 * @return LY_ERR value.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200647 */
648static LY_ERR
Michal Vaskoe20599c2022-12-02 10:45:01 +0100649lys_compile_unres_when(struct lysc_ctx *ctx, const struct lysc_when *when, const struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200650{
Michal Vasko4ed3bd52022-12-02 10:28:04 +0100651 struct lyxp_set tmp_set = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200652 uint32_t i, opts;
Michal Vaskoe4a6d012023-05-22 14:34:52 +0200653 struct lysc_node *schema;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200654 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200655
Michal Vaskod1e53b92021-01-28 13:11:06 +0100656 opts = LYXP_SCNODE_SCHEMA | ((node->flags & LYS_IS_OUTPUT) ? LYXP_SCNODE_OUTPUT : 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200657
Michal Vasko4ed3bd52022-12-02 10:28:04 +0100658 /* check "when" */
659 ret = lyxp_atomize(ctx->ctx, when->cond, node->module, LY_VALUE_SCHEMA_RESOLVED, when->prefixes, when->context,
660 when->context, &tmp_set, opts);
661 if (ret) {
662 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
663 goto cleanup;
664 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200665
Michal Vasko4ed3bd52022-12-02 10:28:04 +0100666 ctx->path[0] = '\0';
667 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
668 for (i = 0; i < tmp_set.used; ++i) {
Michal Vaskoe4a6d012023-05-22 14:34:52 +0200669 if (tmp_set.val.scnodes[i].type != LYXP_NODE_ELEM) {
670 /* skip roots'n'stuff */
671 continue;
672 } else if (tmp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START_USED) {
673 /* context node not actually traversed */
674 continue;
675 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200676
Michal Vaskoe4a6d012023-05-22 14:34:52 +0200677 schema = tmp_set.val.scnodes[i].scnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200678
Michal Vaskoe4a6d012023-05-22 14:34:52 +0200679 /* XPath expression cannot reference "lower" status than the node that has the definition */
680 if (lysc_check_status(NULL, when->flags, node->module, node->name, schema->flags, schema->module,
681 schema->name)) {
682 LOGWRN(ctx->ctx, "When condition \"%s\" may be referencing %s node \"%s\".", when->cond->expr,
683 (schema->flags == LYS_STATUS_OBSLT) ? "obsolete" : "deprecated", schema->name);
684 }
685
686 /* check dummy node children/value accessing */
687 if (lysc_data_parent(schema) == node) {
688 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "When condition is accessing its own conditional node children.");
689 ret = LY_EVALID;
690 goto cleanup;
691 } else if ((schema == node) && (tmp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
692 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "When condition is accessing its own conditional node value.");
693 ret = LY_EVALID;
694 goto cleanup;
695 }
696 }
697
698 if (when->context != node) {
699 /* node actually depends on this "when", not the context node */
700 assert(tmp_set.val.scnodes[0].scnode == when->context);
701 if (tmp_set.val.scnodes[0].in_ctx == LYXP_SET_SCNODE_START_USED) {
702 /* replace the non-traversed context node with the dependent node */
703 tmp_set.val.scnodes[0].scnode = (struct lysc_node *)node;
704 } else {
705 /* context node was traversed, so just add the dependent node */
706 ret = lyxp_set_scnode_insert_node(&tmp_set, node, LYXP_SET_SCNODE_START_USED, LYXP_AXIS_CHILD, NULL);
707 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200708 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200709 }
710
Michal Vasko4ed3bd52022-12-02 10:28:04 +0100711 /* check cyclic dependencies */
712 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
713 LY_CHECK_GOTO(ret, cleanup);
714
Michal Vaskoc130e162021-10-19 11:30:00 +0200715cleanup:
716 lyxp_set_free_content(&tmp_set);
Michal Vaskoc130e162021-10-19 11:30:00 +0200717 return ret;
718}
719
720/**
721 * @brief Check must expressions of a node on a complete compiled schema tree.
722 *
723 * @param[in] ctx Compile context.
724 * @param[in] node Node to check.
725 * @param[in] local_mods Sized array of local modules for musts of @p node at the same index.
726 * @param[in,out] unres Global unres structure.
727 * @return LY_ERECOMPILE
728 * @return LY_ERR value
729 */
730static LY_ERR
731lys_compile_unres_must(struct lysc_ctx *ctx, const struct lysc_node *node, const struct lysp_module **local_mods,
732 struct lys_glob_unres *unres)
733{
734 struct lyxp_set tmp_set;
735 uint32_t i, opts;
736 LY_ARRAY_COUNT_TYPE u;
737 struct lysc_must *musts = NULL;
738 LY_ERR ret = LY_SUCCESS;
739 const struct lys_module *mod;
740 uint16_t flg;
741
742 LOG_LOCSET(node, NULL, NULL, NULL);
743
744 memset(&tmp_set, 0, sizeof tmp_set);
745 opts = LYXP_SCNODE_SCHEMA | ((node->flags & LYS_IS_OUTPUT) ? LYXP_SCNODE_OUTPUT : 0);
746
747 musts = lysc_node_musts(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200748 LY_ARRAY_FOR(musts, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +0200749 /* first check whether all the referenced modules are implemented */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200750 mod = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200751 ret = lys_compile_expr_implement(ctx->ctx, musts[u].cond, LY_VALUE_SCHEMA_RESOLVED, musts[u].prefixes,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100752 ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, unres, &mod);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200753 if (ret) {
754 goto cleanup;
755 } else if (mod) {
Michal Vaskocfaff232020-10-20 09:35:14 +0200756 LOGWRN(ctx->ctx, "Must condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
757 musts[u].cond->expr, mod->name);
758 continue;
759 }
760
761 /* check "must" */
Radek Krejci8df109d2021-04-23 12:19:08 +0200762 ret = lyxp_atomize(ctx->ctx, musts[u].cond, node->module, LY_VALUE_SCHEMA_RESOLVED, musts[u].prefixes, node,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +0200763 node, &tmp_set, opts);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200764 if (ret) {
Michal Vasko7c3134d2022-07-14 10:57:59 +0200765 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid must condition \"%s\".", musts[u].cond->expr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200766 goto cleanup;
767 }
768
769 ctx->path[0] = '\0';
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100770 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200771 for (i = 0; i < tmp_set.used; ++i) {
772 /* skip roots'n'stuff */
773 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko7c3134d2022-07-14 10:57:59 +0200774 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
775
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200776 /* XPath expression cannot reference "lower" status than the node that has the definition */
Michal Vaskoc130e162021-10-19 11:30:00 +0200777 if (local_mods[u]->mod == node->module) {
778 /* use flags of the context node since the definition is local */
779 flg = node->flags;
780 } else {
781 /* definition is foreign (deviation, refine), always current */
782 flg = LYS_STATUS_CURR;
783 }
Michal Vasko7c3134d2022-07-14 10:57:59 +0200784 if (lysc_check_status(NULL, flg, local_mods[u]->mod, node->name, schema->flags, schema->module,
785 schema->name)) {
786 LOGWRN(ctx->ctx, "Must condition \"%s\" may be referencing %s node \"%s\".", musts[u].cond->expr,
787 (schema->flags == LYS_STATUS_OBSLT) ? "obsolete" : "deprecated", schema->name);
788 break;
789 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200790 }
791 }
792
793 lyxp_set_free_content(&tmp_set);
794 }
795
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200796cleanup:
797 lyxp_set_free_content(&tmp_set);
Radek Krejciddace2c2021-01-08 11:30:56 +0100798 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200799 return ret;
800}
801
802/**
Michal Vaskof4fa90d2021-11-11 15:05:19 +0100803 * @brief Remove all disabled bits/enums from a sized array.
804 *
805 * @param[in] ctx Context with the dictionary.
806 * @param[in] items Sized array of bits/enums.
807 */
808static void
Michal Vaskoc636ea42022-09-16 10:20:31 +0200809lys_compile_unres_disabled_bitenum_remove(struct lysf_ctx *ctx, struct lysc_type_bitenum_item *items)
Michal Vaskof4fa90d2021-11-11 15:05:19 +0100810{
811 LY_ARRAY_COUNT_TYPE u = 0, last_u;
812
813 while (u < LY_ARRAY_COUNT(items)) {
814 if (items[u].flags & LYS_DISABLED) {
815 /* free the disabled item */
816 lysc_enum_item_free(ctx, &items[u]);
817
818 /* replace it with the following items */
819 last_u = LY_ARRAY_COUNT(items) - 1;
820 if (u < last_u) {
821 memmove(items + u, items + u + 1, (last_u - u) * sizeof *items);
822 }
823
824 /* one item less */
825 LY_ARRAY_DECREMENT(items);
826 continue;
827 }
828
829 ++u;
830 }
831}
832
833/**
834 * @brief Find and remove all disabled bits/enums in a leaf/leaf-list type.
835 *
836 * @param[in] ctx Compile context.
837 * @param[in] leaf Leaf/leaf-list to check.
838 * @return LY_ERR value
839 */
840static LY_ERR
841lys_compile_unres_disabled_bitenum(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf)
842{
843 struct lysc_type **t;
844 LY_ARRAY_COUNT_TYPE u, count;
845 struct lysc_type_enum *ent;
Michal Vasko28864d52023-03-23 08:07:46 +0100846 ly_bool has_value = 0;
Michal Vaskof4fa90d2021-11-11 15:05:19 +0100847
848 if (leaf->type->basetype == LY_TYPE_UNION) {
849 t = ((struct lysc_type_union *)leaf->type)->types;
850 count = LY_ARRAY_COUNT(t);
851 } else {
852 t = &leaf->type;
853 count = 1;
854 }
855 for (u = 0; u < count; ++u) {
856 if ((t[u]->basetype == LY_TYPE_BITS) || (t[u]->basetype == LY_TYPE_ENUM)) {
857 /* remove all disabled items */
858 ent = (struct lysc_type_enum *)(t[u]);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200859 lys_compile_unres_disabled_bitenum_remove(&ctx->free_ctx, ent->enums);
Michal Vaskof4fa90d2021-11-11 15:05:19 +0100860
Michal Vasko28864d52023-03-23 08:07:46 +0100861 if (LY_ARRAY_COUNT(ent->enums)) {
862 has_value = 1;
Michal Vaskof4fa90d2021-11-11 15:05:19 +0100863 }
Michal Vasko28864d52023-03-23 08:07:46 +0100864 } else {
865 has_value = 1;
Michal Vaskof4fa90d2021-11-11 15:05:19 +0100866 }
867 }
868
Michal Vasko28864d52023-03-23 08:07:46 +0100869 if (!has_value) {
870 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Node \"%s\" without any (or all disabled) valid values.", leaf->name);
871 return LY_EVALID;
872 }
873
Michal Vaskof4fa90d2021-11-11 15:05:19 +0100874 return LY_SUCCESS;
875}
876
877/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200878 * @brief Check leafref for its target existence on a complete compiled schema tree.
879 *
880 * @param[in] ctx Compile context.
881 * @param[in] node Context node for the leafref.
882 * @param[in] lref Leafref to check/resolve.
Michal Vaskoc130e162021-10-19 11:30:00 +0200883 * @param[in] local_mod Local module for the leafref type.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100884 * @param[in,out] unres Global unres structure.
Michal Vasko40c158c2021-04-28 17:01:03 +0200885 * @return LY_ERECOMPILE if context recompilation is needed,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200886 * @return LY_ERR value.
887 */
888static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +0100889lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref,
Michal Vaskoc130e162021-10-19 11:30:00 +0200890 const struct lysp_module *local_mod, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200891{
Michal Vaskod1e53b92021-01-28 13:11:06 +0100892 const struct lysc_node *target = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200893 struct ly_path *p;
894 struct lysc_type *type;
Michal Vaskoc130e162021-10-19 11:30:00 +0200895 uint16_t flg;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200896
897 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
898
Michal Vaskof2a793b2023-03-13 11:56:50 +0100899 if (lref->realtype) {
900 /* already resolved, may happen (shared union typedef with a leafref) */
901 return LY_SUCCESS;
902 }
903
Michal Vasko24fc4d12021-07-12 14:41:20 +0200904 /* first implement all the modules in the path */
905 LY_CHECK_RET(lys_compile_expr_implement(ctx->ctx, lref->path, LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, 1, unres, NULL));
906
Michal Vaskoed725d72021-06-23 12:03:45 +0200907 /* try to find the target, current module is that of the context node (RFC 7950 6.4.1 second bullet) */
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200908 LY_CHECK_RET(ly_path_compile_leafref(ctx->ctx, node, ctx->ext, lref->path,
Michal Vaskod1e53b92021-01-28 13:11:06 +0100909 (node->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +0200910 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200911
912 /* get the target node */
913 target = p[LY_ARRAY_COUNT(p) - 1].node;
914 ly_path_free(node->module->ctx, p);
915
916 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100917 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200918 lref->path->expr, lys_nodetype2str(target->nodetype));
919 return LY_EVALID;
920 }
921
922 /* check status */
923 ctx->path[0] = '\0';
924 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
925 ctx->path_len = strlen(ctx->path);
Michal Vaskoc130e162021-10-19 11:30:00 +0200926 if (node->module == local_mod->mod) {
927 /* use flags of the context node since the definition is local */
928 flg = node->flags;
929 } else {
930 /* definition is foreign (deviation), always current */
931 flg = LYS_STATUS_CURR;
932 }
933 if (lysc_check_status(ctx, flg, local_mod->mod, node->name, target->flags, target->module, target->name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200934 return LY_EVALID;
935 }
936 ctx->path_len = 1;
937 ctx->path[1] = '\0';
938
939 /* check config */
940 if (lref->require_instance) {
Michal Vaskod1e53b92021-01-28 13:11:06 +0100941 if ((node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100942 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200943 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
944 return LY_EVALID;
945 }
946 }
947
Michal Vaskod15c7de2022-05-09 15:34:09 +0200948 /* check for circular chain of leafrefs */
949 for (type = ((struct lysc_node_leaf *)target)->type;
950 type && (type->basetype == LY_TYPE_LEAFREF);
951 type = ((struct lysc_type_leafref *)type)->realtype) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200952 if (type == (struct lysc_type *)lref) {
953 /* circular chain detected */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100954 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid leafref path \"%s\" - circular chain of leafrefs detected.",
955 lref->path->expr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200956 return LY_EVALID;
957 }
958 }
959
Michal Vaskod15c7de2022-05-09 15:34:09 +0200960 /* store the type */
961 lref->realtype = ((struct lysc_node_leaf *)target)->type;
962 ++lref->realtype->refcount;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200963 return LY_SUCCESS;
964}
965
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200966/**
967 * @brief Compile default value(s) for leaf or leaf-list expecting a complete compiled schema tree.
968 *
969 * @param[in] ctx Compile context.
970 * @param[in] node Leaf or leaf-list to compile the default value(s) for.
971 * @param[in] type Type of the default value.
972 * @param[in] dflt Default value.
973 * @param[in] dflt_pmod Parsed module of the @p dflt to resolve possible prefixes.
974 * @param[in,out] storage Storage for the compiled default value.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100975 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200976 * @return LY_ERR value.
977 */
978static LY_ERR
979lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_type *type, const char *dflt,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100980 const struct lysp_module *dflt_pmod, struct lyd_value *storage, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200981{
982 LY_ERR ret;
Michal Vasko25d6ad02020-10-22 12:20:22 +0200983 uint32_t options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200984 struct ly_err_item *err = NULL;
985
Radek Krejci0b013302021-03-29 15:22:32 +0200986 options = (ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED) ? LYPLG_TYPE_STORE_IMPLEMENT : 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200987 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), options, LY_VALUE_SCHEMA, (void *)dflt_pmod,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100988 LYD_HINT_SCHEMA, node, storage, unres, &err);
Michal Vasko1ccbf542021-04-19 11:35:00 +0200989 if (ret == LY_ERECOMPILE) {
990 /* fine, but we need to recompile */
991 return LY_ERECOMPILE;
992 } else if (ret == LY_EINCOMPLETE) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200993 /* we have no data so we will not be resolving it */
994 ret = LY_SUCCESS;
995 }
996
997 if (ret) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100998 LOG_LOCSET(node, NULL, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200999 if (err) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001000 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid default - value does not fit the type (%s).", err->msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001001 ly_err_free(err);
1002 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001003 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid default - value does not fit the type.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001004 }
Radek Krejciddace2c2021-01-08 11:30:56 +01001005 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001006 return ret;
1007 }
1008
Michal Vasko04338d92021-09-01 07:58:14 +02001009 LY_ATOMIC_INC_BARRIER(((struct lysc_type *)storage->realtype)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001010 return LY_SUCCESS;
1011}
1012
1013/**
1014 * @brief Compile default value of a leaf expecting a complete compiled schema tree.
1015 *
1016 * @param[in] ctx Compile context.
1017 * @param[in] leaf Leaf that the default value is for.
1018 * @param[in] dflt Default value to compile.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001019 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001020 * @return LY_ERR value.
1021 */
1022static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001023lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt,
1024 struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001025{
1026 LY_ERR ret;
1027
1028 assert(!leaf->dflt);
1029
1030 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
1031 /* ignore default values for keys and mandatory leaves */
1032 return LY_SUCCESS;
1033 }
1034
1035 /* allocate the default value */
1036 leaf->dflt = calloc(1, sizeof *leaf->dflt);
1037 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
1038
1039 /* store the default value */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001040 ret = lys_compile_unres_dflt(ctx, &leaf->node, leaf->type, dflt->str, dflt->mod, leaf->dflt, unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001041 if (ret) {
1042 free(leaf->dflt);
1043 leaf->dflt = NULL;
1044 }
1045
1046 return ret;
1047}
1048
1049/**
1050 * @brief Compile default values of a leaf-list expecting a complete compiled schema tree.
1051 *
1052 * @param[in] ctx Compile context.
1053 * @param[in] llist Leaf-list that the default value(s) are for.
1054 * @param[in] dflt Default value to compile, in case of a single value.
1055 * @param[in] dflts Sized array of default values, in case of more values.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001056 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001057 * @return LY_ERR value.
1058 */
1059static LY_ERR
1060lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflt,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001061 struct lysp_qname *dflts, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001062{
1063 LY_ERR ret;
1064 LY_ARRAY_COUNT_TYPE orig_count, u, v;
1065
1066 assert(dflt || dflts);
1067
Radek Krejcic7d13e32020-12-09 12:32:24 +01001068 /* in case there were already some defaults and we are adding new by deviations */
1069 orig_count = LY_ARRAY_COUNT(llist->dflts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001070
1071 /* allocate new items */
Radek Krejcic7d13e32020-12-09 12:32:24 +01001072 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + (dflts ? LY_ARRAY_COUNT(dflts) : 1), LY_EMEM);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001073
1074 /* fill each new default value */
1075 if (dflts) {
1076 LY_ARRAY_FOR(dflts, u) {
1077 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001078 ret = lys_compile_unres_dflt(ctx, &llist->node, llist->type, dflts[u].str, dflts[u].mod,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001079 llist->dflts[orig_count + u], unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001080 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
1081 LY_ARRAY_INCREMENT(llist->dflts);
1082 }
1083 } else {
1084 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001085 ret = lys_compile_unres_dflt(ctx, &llist->node, llist->type, dflt->str, dflt->mod,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001086 llist->dflts[orig_count], unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001087 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
1088 LY_ARRAY_INCREMENT(llist->dflts);
1089 }
1090
1091 /* check default value uniqueness */
1092 if (llist->flags & LYS_CONFIG_W) {
1093 /* configuration data values must be unique - so check the default values */
1094 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
1095 for (v = 0; v < u; ++v) {
1096 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
Radek Krejcia6016992021-03-03 10:13:41 +01001097 lysc_update_path(ctx, llist->parent ? llist->parent->module : NULL, llist->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001098 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration leaf-list has multiple defaults of the same value \"%s\".",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001099 llist->dflts[u]->realtype->plugin->print(ctx->ctx, llist->dflts[u], LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001100 lysc_update_path(ctx, NULL, NULL);
1101 return LY_EVALID;
1102 }
1103 }
1104 }
1105 }
1106
1107 return LY_SUCCESS;
1108}
1109
Michal Vaskof4258e12021-06-15 12:11:42 +02001110/**
aPiecek732cd142021-07-07 16:29:59 +02001111 * @brief Iteratively get all leafrefs from @p node
1112 * if the node is of type union, otherwise just return the leafref.
1113 *
1114 * @param[in] node Node that may contain the leafref.
1115 * @param[in,out] index Value that is passed between function calls.
1116 * For each new node, initialize value of the @p index to 0, otherwise
1117 * do not modify the value between calls.
1118 * @return Pointer to the leafref or next leafref, otherwise NULL.
1119 */
1120static struct lysc_type_leafref *
1121lys_type_leafref_next(const struct lysc_node *node, uint64_t *index)
1122{
1123 struct lysc_type_leafref *ret = NULL;
1124 struct lysc_type_union *uni;
1125 struct lysc_type *leaf_type;
1126
1127 assert(node->nodetype & LYD_NODE_TERM);
1128
1129 leaf_type = ((struct lysc_node_leaf *)node)->type;
1130 if (leaf_type->basetype == LY_TYPE_UNION) {
1131 uni = (struct lysc_type_union *)leaf_type;
1132
1133 /* find next union leafref */
1134 while (*index < LY_ARRAY_COUNT(uni->types)) {
1135 if (uni->types[*index]->basetype == LY_TYPE_LEAFREF) {
1136 ret = (struct lysc_type_leafref *)uni->types[*index];
1137 ++(*index);
1138 break;
1139 }
1140
1141 ++(*index);
1142 }
1143 } else {
1144 /* return just the single leafref */
1145 if (*index == 0) {
1146 ++(*index);
1147 assert(leaf_type->basetype == LY_TYPE_LEAFREF);
1148 ret = (struct lysc_type_leafref *)leaf_type;
1149 }
1150 }
1151
1152 return ret;
1153}
1154
1155/**
Michal Vaskof4258e12021-06-15 12:11:42 +02001156 * @brief Finish dependency set compilation by resolving all the unres sets.
1157 *
1158 * @param[in] ctx libyang context.
1159 * @param[in] unres Global unres structure with the sets to resolve.
1160 * @return LY_SUCCESS on success.
1161 * @return LY_ERECOMPILE if the dep set needs to be recompiled.
1162 * @return LY_ERR value on error.
1163 */
1164static LY_ERR
1165lys_compile_unres_depset(struct ly_ctx *ctx, struct lys_glob_unres *unres)
Michal Vasko405cc9e2020-12-01 12:01:27 +01001166{
Michal Vaskoe20599c2022-12-02 10:45:01 +01001167 LY_ERR ret = LY_SUCCESS, r;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001168 struct lysc_node *node;
aPiecek732cd142021-07-07 16:29:59 +02001169 struct lysc_type *typeiter;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001170 struct lysc_type_leafref *lref;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001171 struct lysc_ctx cctx = {0};
1172 struct lys_depset_unres *ds_unres = &unres->ds_unres;
aPiecekd7bd52d2021-07-08 08:59:59 +02001173 struct ly_path *path;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001174 LY_ARRAY_COUNT_TYPE v;
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001175 struct lysc_unres_leafref *l;
Michal Vasko4ed3bd52022-12-02 10:28:04 +01001176 struct lysc_unres_when *w;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001177 struct lysc_unres_must *m;
Michal Vaskoe20599c2022-12-02 10:45:01 +01001178 struct lysc_unres_dflt *d;
aPiecekd7bd52d2021-07-08 08:59:59 +02001179 uint32_t i, processed_leafrefs = 0;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001180
aPiecekd7bd52d2021-07-08 08:59:59 +02001181resolve_all:
Michal Vasko4ed3bd52022-12-02 10:28:04 +01001182 /* check disabled leafrefs first */
aPiecekc6526b42021-07-12 15:21:39 +02001183 while (ds_unres->disabled_leafrefs.count) {
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001184 /* remember index, it can change before we get to free this item */
1185 i = ds_unres->disabled_leafrefs.count - 1;
1186 l = ds_unres->disabled_leafrefs.objs[i];
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001187 LYSC_CTX_INIT_PMOD(cctx, l->node->module->parsed, l->ext);
aPiecekc6526b42021-07-12 15:21:39 +02001188
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001189 LOG_LOCSET(l->node, NULL, NULL, NULL);
aPiecekc6526b42021-07-12 15:21:39 +02001190 v = 0;
Michal Vaskoc130e162021-10-19 11:30:00 +02001191 while ((ret == LY_SUCCESS) && (lref = lys_type_leafref_next(l->node, &v))) {
1192 ret = lys_compile_unres_leafref(&cctx, l->node, lref, l->local_mod, unres);
aPiecekc6526b42021-07-12 15:21:39 +02001193 }
aPiecekc6526b42021-07-12 15:21:39 +02001194 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001195 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001196
1197 ly_set_rm_index(&ds_unres->disabled_leafrefs, i, free);
aPiecekc6526b42021-07-12 15:21:39 +02001198 }
Michal Vaskoa23e1d92021-07-13 12:04:16 +02001199
Michal Vasko4ed3bd52022-12-02 10:28:04 +01001200 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
1201 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
1202 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation.
1203 * Also do the same check for set of the disabled leafrefs, but without the second round. */
aPiecekd7bd52d2021-07-08 08:59:59 +02001204 for (i = processed_leafrefs; i < ds_unres->leafrefs.count; ++i) {
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001205 l = ds_unres->leafrefs.objs[i];
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001206 LYSC_CTX_INIT_PMOD(cctx, l->node->module->parsed, l->ext);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001207
Michal Vaskoc636ea42022-09-16 10:20:31 +02001208 LOG_LOCSET(l->node, NULL, NULL, NULL);
aPiecek732cd142021-07-07 16:29:59 +02001209 v = 0;
Michal Vaskoc130e162021-10-19 11:30:00 +02001210 while ((ret == LY_SUCCESS) && (lref = lys_type_leafref_next(l->node, &v))) {
1211 ret = lys_compile_unres_leafref(&cctx, l->node, lref, l->local_mod, unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001212 }
Radek Krejciddace2c2021-01-08 11:30:56 +01001213 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001214 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001215 }
aPiecekd7bd52d2021-07-08 08:59:59 +02001216 for (i = processed_leafrefs; i < ds_unres->leafrefs.count; ++i) {
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001217 l = ds_unres->leafrefs.objs[i];
Radek Krejci2efc45b2020-12-22 16:25:44 +01001218
Michal Vasko405cc9e2020-12-01 12:01:27 +01001219 /* store pointer to the real type */
aPiecek732cd142021-07-07 16:29:59 +02001220 v = 0;
Michal Vaskoc130e162021-10-19 11:30:00 +02001221 while ((lref = lys_type_leafref_next(l->node, &v))) {
aPiecek732cd142021-07-07 16:29:59 +02001222 for (typeiter = lref->realtype;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001223 typeiter->basetype == LY_TYPE_LEAFREF;
1224 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
Michal Vaskod15c7de2022-05-09 15:34:09 +02001225
Michal Vaskoc636ea42022-09-16 10:20:31 +02001226 lysc_type_free(&cctx.free_ctx, lref->realtype);
aPiecek732cd142021-07-07 16:29:59 +02001227 lref->realtype = typeiter;
Michal Vaskod15c7de2022-05-09 15:34:09 +02001228 ++lref->realtype->refcount;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001229 }
1230
Michal Vaskoe20599c2022-12-02 10:45:01 +01001231 /* if 'goto' will be used on the 'resolve_all' label, then the current leafref will not be processed again */
aPiecekd7bd52d2021-07-08 08:59:59 +02001232 processed_leafrefs++;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001233 }
1234
Michal Vaskoe20599c2022-12-02 10:45:01 +01001235 /* check when, first implement all the referenced modules (for the cyclic check in the next loop to work) */
1236 i = 0;
1237 while (i < ds_unres->whens.count) {
1238 w = ds_unres->whens.objs[i];
1239 LYSC_CTX_INIT_PMOD(cctx, w->node->module->parsed, NULL);
1240
1241 LOG_LOCSET(w->node, NULL, NULL, NULL);
1242 r = lys_compile_unres_when_implement(&cctx, w->when, unres);
1243 LOG_LOCBACK(w->node ? 1 : 0, 0, 0, 0);
1244
1245 if (r == LY_ENOT) {
1246 /* skip full when check, remove from the set */
1247 free(w);
1248 ly_set_rm_index(&ds_unres->whens, i, NULL);
1249 continue;
1250 } else if (r) {
1251 /* error */
1252 ret = r;
1253 goto cleanup;
1254 }
1255
1256 ++i;
1257 }
Michal Vaskoc130e162021-10-19 11:30:00 +02001258 while (ds_unres->whens.count) {
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001259 i = ds_unres->whens.count - 1;
Michal Vasko4ed3bd52022-12-02 10:28:04 +01001260 w = ds_unres->whens.objs[i];
1261 LYSC_CTX_INIT_PMOD(cctx, w->node->module->parsed, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001262
Michal Vasko4ed3bd52022-12-02 10:28:04 +01001263 LOG_LOCSET(w->node, NULL, NULL, NULL);
Michal Vaskoe20599c2022-12-02 10:45:01 +01001264 ret = lys_compile_unres_when(&cctx, w->when, w->node);
1265 LOG_LOCBACK(w->node ? 1 : 0, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001266 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001267
Michal Vasko4ed3bd52022-12-02 10:28:04 +01001268 free(w);
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001269 ly_set_rm_index(&ds_unres->whens, i, NULL);
Michal Vaskoc130e162021-10-19 11:30:00 +02001270 }
1271
1272 /* check must */
1273 while (ds_unres->musts.count) {
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001274 i = ds_unres->musts.count - 1;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001275 m = ds_unres->musts.objs[i];
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001276 LYSC_CTX_INIT_PMOD(cctx, m->node->module->parsed, m->ext);
Michal Vaskoc130e162021-10-19 11:30:00 +02001277
1278 LOG_LOCSET(m->node, NULL, NULL, NULL);
1279 ret = lys_compile_unres_must(&cctx, m->node, m->local_mods, unres);
1280 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001281 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoc130e162021-10-19 11:30:00 +02001282
1283 lysc_unres_must_free(m);
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001284 ly_set_rm_index(&ds_unres->musts, i, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001285 }
1286
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001287 /* remove disabled enums/bits */
Michal Vasko5eaf7322021-11-22 15:58:26 +01001288 while (ds_unres->disabled_bitenums.count) {
1289 i = ds_unres->disabled_bitenums.count - 1;
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001290 node = ds_unres->disabled_bitenums.objs[i];
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001291 LYSC_CTX_INIT_PMOD(cctx, node->module->parsed, NULL);
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001292
1293 LOG_LOCSET(node, NULL, NULL, NULL);
1294 ret = lys_compile_unres_disabled_bitenum(&cctx, (struct lysc_node_leaf *)node);
1295 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001296 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001297
1298 ly_set_rm_index(&ds_unres->disabled_bitenums, i, NULL);
1299 }
1300
Michal Vasko405cc9e2020-12-01 12:01:27 +01001301 /* finish incomplete default values compilation */
Michal Vaskof4258e12021-06-15 12:11:42 +02001302 while (ds_unres->dflts.count) {
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001303 i = ds_unres->dflts.count - 1;
Michal Vaskoe20599c2022-12-02 10:45:01 +01001304 d = ds_unres->dflts.objs[i];
1305 LYSC_CTX_INIT_PMOD(cctx, d->leaf->module->parsed, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001306
Michal Vaskoe20599c2022-12-02 10:45:01 +01001307 LOG_LOCSET(&d->leaf->node, NULL, NULL, NULL);
1308 if (d->leaf->nodetype == LYS_LEAF) {
1309 ret = lys_compile_unres_leaf_dlft(&cctx, d->leaf, d->dflt, unres);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001310 } else {
Michal Vaskoe20599c2022-12-02 10:45:01 +01001311 ret = lys_compile_unres_llist_dflts(&cctx, d->llist, d->dflt, d->dflts, unres);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001312 }
Radek Krejciddace2c2021-01-08 11:30:56 +01001313 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001314 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001315
Michal Vaskoe20599c2022-12-02 10:45:01 +01001316 lysc_unres_dflt_free(ctx, d);
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001317 ly_set_rm_index(&ds_unres->dflts, i, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001318 }
1319
Michal Vasko40c158c2021-04-28 17:01:03 +02001320 /* some unres items may have been added */
aPiecekc6526b42021-07-12 15:21:39 +02001321 if ((processed_leafrefs != ds_unres->leafrefs.count) || ds_unres->disabled_leafrefs.count ||
Michal Vaskoc130e162021-10-19 11:30:00 +02001322 ds_unres->whens.count || ds_unres->musts.count || ds_unres->dflts.count) {
aPiecekd7bd52d2021-07-08 08:59:59 +02001323 goto resolve_all;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001324 }
1325
Michal Vasko30ab8e72021-04-19 12:47:52 +02001326 /* finally, remove all disabled nodes */
Michal Vaskof4258e12021-06-15 12:11:42 +02001327 for (i = 0; i < ds_unres->disabled.count; ++i) {
1328 node = ds_unres->disabled.snodes[i];
Michal Vasko30ab8e72021-04-19 12:47:52 +02001329 if (node->flags & LYS_KEY) {
1330 LOG_LOCSET(node, NULL, NULL, NULL);
Michal Vaskoe02e7402021-07-23 08:29:28 +02001331 LOGVAL(ctx, LYVE_REFERENCE, "Key \"%s\" is disabled.", node->name);
Michal Vasko30ab8e72021-04-19 12:47:52 +02001332 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001333 ret = LY_EVALID;
1334 goto cleanup;
Michal Vasko30ab8e72021-04-19 12:47:52 +02001335 }
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001336 LYSC_CTX_INIT_PMOD(cctx, node->module->parsed, NULL);
Michal Vasko30ab8e72021-04-19 12:47:52 +02001337
Michal Vaskoc636ea42022-09-16 10:20:31 +02001338 lysc_node_free(&cctx.free_ctx, node, 1);
Michal Vasko30ab8e72021-04-19 12:47:52 +02001339 }
1340
aPiecekd7bd52d2021-07-08 08:59:59 +02001341 /* also check if the leafref target has not been disabled */
1342 for (i = 0; i < ds_unres->leafrefs.count; ++i) {
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001343 l = ds_unres->leafrefs.objs[i];
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001344 LYSC_CTX_INIT_PMOD(cctx, l->node->module->parsed, l->ext);
aPiecekd7bd52d2021-07-08 08:59:59 +02001345
1346 v = 0;
Michal Vaskoc130e162021-10-19 11:30:00 +02001347 while ((lref = lys_type_leafref_next(l->node, &v))) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001348 ret = ly_path_compile_leafref(cctx.ctx, l->node, cctx.ext, lref->path,
Michal Vaskoc130e162021-10-19 11:30:00 +02001349 (l->node->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
aPiecekd7bd52d2021-07-08 08:59:59 +02001350 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &path);
Michal Vaskoc130e162021-10-19 11:30:00 +02001351 ly_path_free(l->node->module->ctx, path);
aPiecekd7bd52d2021-07-08 08:59:59 +02001352
1353 assert(ret != LY_ERECOMPILE);
1354 if (ret) {
Michal Vaskoc130e162021-10-19 11:30:00 +02001355 LOG_LOCSET(l->node, NULL, NULL, NULL);
Michal Vaskoe02e7402021-07-23 08:29:28 +02001356 LOGVAL(ctx, LYVE_REFERENCE, "Target of leafref \"%s\" cannot be referenced because it is disabled.",
Michal Vaskoc130e162021-10-19 11:30:00 +02001357 l->node->name);
aPiecekd7bd52d2021-07-08 08:59:59 +02001358 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001359 ret = LY_EVALID;
1360 goto cleanup;
aPiecekd7bd52d2021-07-08 08:59:59 +02001361 }
1362 }
1363 }
1364
Michal Vaskoc636ea42022-09-16 10:20:31 +02001365cleanup:
1366 lysf_ctx_erase(&cctx.free_ctx);
1367 return ret;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001368}
1369
Michal Vaskof4258e12021-06-15 12:11:42 +02001370/**
1371 * @brief Erase dep set unres.
1372 *
1373 * @param[in] ctx libyang context.
1374 * @param[in] unres Global unres structure with the sets to resolve.
1375 */
1376static void
1377lys_compile_unres_depset_erase(const struct ly_ctx *ctx, struct lys_glob_unres *unres)
Michal Vasko405cc9e2020-12-01 12:01:27 +01001378{
1379 uint32_t i;
1380
Michal Vasko4ed3bd52022-12-02 10:28:04 +01001381 ly_set_erase(&unres->ds_unres.whens, free);
Michal Vaskoc130e162021-10-19 11:30:00 +02001382 for (i = 0; i < unres->ds_unres.musts.count; ++i) {
1383 lysc_unres_must_free(unres->ds_unres.musts.objs[i]);
1384 }
1385 ly_set_erase(&unres->ds_unres.musts, NULL);
1386 ly_set_erase(&unres->ds_unres.leafrefs, free);
Michal Vaskof4258e12021-06-15 12:11:42 +02001387 for (i = 0; i < unres->ds_unres.dflts.count; ++i) {
1388 lysc_unres_dflt_free(ctx, unres->ds_unres.dflts.objs[i]);
Michal Vaskodf6319c2021-06-10 14:41:05 +02001389 }
Michal Vaskof4258e12021-06-15 12:11:42 +02001390 ly_set_erase(&unres->ds_unres.dflts, NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +02001391 ly_set_erase(&unres->ds_unres.disabled, NULL);
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001392 ly_set_erase(&unres->ds_unres.disabled_leafrefs, free);
1393 ly_set_erase(&unres->ds_unres.disabled_bitenums, NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +02001394}
1395
Michal Vasko709f9a52021-07-21 10:51:59 +02001396/**
1397 * @brief Compile all flagged modules in a dependency set, recursively if recompilation is needed.
1398 *
1399 * @param[in] ctx libyang context.
1400 * @param[in] dep_set Dependency set to compile.
1401 * @param[in,out] unres Global unres to use.
1402 * @return LY_ERR value.
1403 */
1404static LY_ERR
1405lys_compile_depset_r(struct ly_ctx *ctx, struct ly_set *dep_set, struct lys_glob_unres *unres)
Michal Vaskof4258e12021-06-15 12:11:42 +02001406{
1407 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001408 struct lysf_ctx fctx = {.ctx = ctx};
Michal Vaskof4258e12021-06-15 12:11:42 +02001409 struct lys_module *mod;
1410 uint32_t i;
1411
1412 for (i = 0; i < dep_set->count; ++i) {
1413 mod = dep_set->objs[i];
1414 if (!mod->to_compile) {
1415 /* skip */
1416 continue;
1417 }
1418 assert(mod->implemented);
1419
1420 /* free the compiled module, if any */
Michal Vaskoc636ea42022-09-16 10:20:31 +02001421 lysc_module_free(&fctx, mod->compiled);
Michal Vaskof4258e12021-06-15 12:11:42 +02001422 mod->compiled = NULL;
1423
1424 /* (re)compile the module */
1425 LY_CHECK_GOTO(ret = lys_compile(mod, &unres->ds_unres), cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001426 }
Michal Vaskof4258e12021-06-15 12:11:42 +02001427
1428 /* resolve dep set unres */
1429 ret = lys_compile_unres_depset(ctx, unres);
1430 if (ret == LY_ERECOMPILE) {
1431 /* new module is implemented, discard current dep set unres and recompile the whole dep set */
1432 lys_compile_unres_depset_erase(ctx, unres);
Michal Vasko709f9a52021-07-21 10:51:59 +02001433 return lys_compile_depset_r(ctx, dep_set, unres);
Michal Vaskof4258e12021-06-15 12:11:42 +02001434 } else if (ret) {
1435 /* error */
1436 goto cleanup;
1437 }
1438
1439 /* success, unset the flags of all the modules in the dep set */
1440 for (i = 0; i < dep_set->count; ++i) {
1441 mod = dep_set->objs[i];
1442 mod->to_compile = 0;
1443 }
1444
1445cleanup:
1446 lys_compile_unres_depset_erase(ctx, unres);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001447 lysf_ctx_erase(&fctx);
Michal Vaskof4258e12021-06-15 12:11:42 +02001448 return ret;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001449}
1450
Michal Vasko6a4ef772022-02-08 15:07:44 +01001451/**
1452 * @brief Check if-feature of all features of all modules in a dep set.
1453 *
1454 * @param[in] dep_set Dep set to check.
1455 * @return LY_ERR value.
1456 */
1457static LY_ERR
1458lys_compile_depset_check_features(struct ly_set *dep_set)
1459{
1460 struct lys_module *mod;
1461 uint32_t i;
1462
1463 for (i = 0; i < dep_set->count; ++i) {
1464 mod = dep_set->objs[i];
1465 if (!mod->to_compile) {
1466 /* skip */
1467 continue;
1468 }
1469
1470 /* check features of this module */
1471 LY_CHECK_RET(lys_check_features(mod->parsed));
1472 }
1473
1474 return LY_SUCCESS;
1475}
1476
Michal Vasko709f9a52021-07-21 10:51:59 +02001477LY_ERR
1478lys_compile_depset_all(struct ly_ctx *ctx, struct lys_glob_unres *unres)
1479{
1480 uint32_t i;
1481
1482 for (i = 0; i < unres->dep_sets.count; ++i) {
Michal Vasko6a4ef772022-02-08 15:07:44 +01001483 LY_CHECK_RET(lys_compile_depset_check_features(unres->dep_sets.objs[i]));
Michal Vasko709f9a52021-07-21 10:51:59 +02001484 LY_CHECK_RET(lys_compile_depset_r(ctx, unres->dep_sets.objs[i], unres));
1485 }
1486
1487 return LY_SUCCESS;
1488}
1489
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001490/**
Michal Vasko405cc9e2020-12-01 12:01:27 +01001491 * @brief Finish compilation of all the module unres sets in a compile context.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001492 *
1493 * @param[in] ctx Compile context with unres sets.
1494 * @return LY_ERR value.
1495 */
1496static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001497lys_compile_unres_mod(struct lysc_ctx *ctx)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001498{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001499 struct lysc_augment *aug;
1500 struct lysc_deviation *dev;
Michal Vasko1ade6f12021-04-19 11:32:45 +02001501 struct lys_module *orig_mod = ctx->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001502 uint32_t i;
1503
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001504 /* check that all augments were applied */
1505 for (i = 0; i < ctx->augs.count; ++i) {
1506 aug = ctx->augs.objs[i];
Michal Vasko1ade6f12021-04-19 11:32:45 +02001507 ctx->cur_mod = aug->aug_pmod->mod;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001508 if (aug->ext) {
1509 lysc_update_path(ctx, NULL, "{extension}");
1510 lysc_update_path(ctx, NULL, aug->ext->name);
1511 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001512 lysc_update_path(ctx, NULL, "{augment}");
1513 lysc_update_path(ctx, NULL, aug->nodeid->expr);
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001514 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment%s target node \"%s\" from module \"%s\" was not found.",
1515 aug->ext ? " extension" : "", aug->nodeid->expr, LYSP_MODULE_NAME(aug->aug_pmod));
Michal Vasko1ade6f12021-04-19 11:32:45 +02001516 ctx->cur_mod = orig_mod;
Radek Krejci2efc45b2020-12-22 16:25:44 +01001517 lysc_update_path(ctx, NULL, NULL);
1518 lysc_update_path(ctx, NULL, NULL);
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02001519 if (aug->ext) {
1520 lysc_update_path(ctx, NULL, NULL);
1521 lysc_update_path(ctx, NULL, NULL);
1522 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001523 }
1524 if (ctx->augs.count) {
1525 return LY_ENOTFOUND;
1526 }
1527
1528 /* check that all deviations were applied */
1529 for (i = 0; i < ctx->devs.count; ++i) {
1530 dev = ctx->devs.objs[i];
Radek Krejci2efc45b2020-12-22 16:25:44 +01001531 lysc_update_path(ctx, NULL, "{deviation}");
1532 lysc_update_path(ctx, NULL, dev->nodeid->expr);
1533 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Deviation(s) target node \"%s\" from module \"%s\" was not found.",
1534 dev->nodeid->expr, LYSP_MODULE_NAME(dev->dev_pmods[0]));
1535 lysc_update_path(ctx, NULL, NULL);
1536 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001537 }
1538 if (ctx->devs.count) {
1539 return LY_ENOTFOUND;
1540 }
1541
1542 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001543}
1544
1545/**
Michal Vasko405cc9e2020-12-01 12:01:27 +01001546 * @brief Erase all the module unres sets in a compile context.
1547 *
1548 * @param[in] ctx Compile context with unres sets.
1549 * @param[in] error Whether the compilation finished with an error or not.
1550 */
1551static void
1552lys_compile_unres_mod_erase(struct lysc_ctx *ctx, ly_bool error)
1553{
1554 uint32_t i;
1555
1556 ly_set_erase(&ctx->groupings, NULL);
1557 ly_set_erase(&ctx->tpdf_chain, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001558
1559 if (!error) {
1560 /* there can be no leftover deviations or augments */
1561 LY_CHECK_ERR_RET(ctx->augs.count, LOGINT(ctx->ctx), );
1562 LY_CHECK_ERR_RET(ctx->devs.count, LOGINT(ctx->ctx), );
1563
1564 ly_set_erase(&ctx->augs, NULL);
1565 ly_set_erase(&ctx->devs, NULL);
1566 ly_set_erase(&ctx->uses_augs, NULL);
1567 ly_set_erase(&ctx->uses_rfns, NULL);
1568 } else {
1569 for (i = 0; i < ctx->augs.count; ++i) {
1570 lysc_augment_free(ctx->ctx, ctx->augs.objs[i]);
1571 }
1572 ly_set_erase(&ctx->augs, NULL);
1573 for (i = 0; i < ctx->devs.count; ++i) {
1574 lysc_deviation_free(ctx->ctx, ctx->devs.objs[i]);
1575 }
1576 ly_set_erase(&ctx->devs, NULL);
1577 for (i = 0; i < ctx->uses_augs.count; ++i) {
1578 lysc_augment_free(ctx->ctx, ctx->uses_augs.objs[i]);
1579 }
1580 ly_set_erase(&ctx->uses_augs, NULL);
1581 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1582 lysc_refine_free(ctx->ctx, ctx->uses_rfns.objs[i]);
1583 }
1584 ly_set_erase(&ctx->uses_rfns, NULL);
1585 }
1586}
1587
Michal Vaskod39bea42021-06-14 10:42:49 +02001588LY_ERR
Michal Vaskof4258e12021-06-15 12:11:42 +02001589lys_compile(struct lys_module *mod, struct lys_depset_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001590{
Michal Vaskoc636ea42022-09-16 10:20:31 +02001591 struct lysc_ctx ctx;
Michal Vasko0b395e12021-04-23 13:43:07 +02001592 struct lysc_module *mod_c = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001593 struct lysp_module *sp;
1594 struct lysp_submodule *submod;
1595 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001596 struct lysp_node_grp *grp;
1597 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001598 LY_ERR ret = LY_SUCCESS;
1599
1600 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
1601
Michal Vaskof4258e12021-06-15 12:11:42 +02001602 assert(mod->implemented && mod->to_compile);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001603
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001604 sp = mod->parsed;
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001605 LYSC_CTX_INIT_PMOD(ctx, sp, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001606 ctx.unres = unres;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001607
Michal Vasko0b395e12021-04-23 13:43:07 +02001608 ++mod->ctx->change_count;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001609 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
1610 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
1611 mod_c->mod = mod;
1612
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001613 /* compile augments and deviations of our module from other modules so they can be applied during compilation */
Michal Vasko1ccbf542021-04-19 11:35:00 +02001614 LY_CHECK_GOTO(ret = lys_precompile_own_augments(&ctx), cleanup);
1615 LY_CHECK_GOTO(ret = lys_precompile_own_deviations(&ctx), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001616
1617 /* data nodes */
1618 LY_LIST_FOR(sp->data, pnode) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001619 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001620 }
1621
Radek Krejci2a9fc652021-01-22 17:44:34 +01001622 /* top-level RPCs */
1623 LY_LIST_FOR((struct lysp_node *)sp->rpcs, pnode) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001624 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001625 }
1626
1627 /* top-level notifications */
1628 LY_LIST_FOR((struct lysp_node *)sp->notifs, pnode) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001629 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001630 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001631
Michal Vasko193dacd2022-10-13 08:43:05 +02001632 /* module extension instances */
Michal Vasko1ccbf542021-04-19 11:35:00 +02001633 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001634
1635 /* the same for submodules */
1636 LY_ARRAY_FOR(sp->includes, u) {
1637 submod = sp->includes[u].submodule;
1638 ctx.pmod = (struct lysp_module *)submod;
1639
1640 LY_LIST_FOR(submod->data, pnode) {
1641 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001642 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001643 }
1644
Radek Krejci2a9fc652021-01-22 17:44:34 +01001645 LY_LIST_FOR((struct lysp_node *)submod->rpcs, pnode) {
1646 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001647 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001648 }
1649
1650 LY_LIST_FOR((struct lysp_node *)submod->notifs, pnode) {
1651 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001652 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001653 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001654
Michal Vasko1ccbf542021-04-19 11:35:00 +02001655 COMPILE_EXTS_GOTO(&ctx, submod->exts, mod_c->exts, mod_c, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001656 }
Michal Vasko12606ee2020-11-25 17:05:11 +01001657 ctx.pmod = sp;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001658
1659 /* validate non-instantiated groupings from the parsed schema,
1660 * without it we would accept even the schemas with invalid grouping specification */
Michal Vasko7c565922021-06-10 14:58:27 +02001661 ctx.compile_opts |= LYS_COMPILE_GROUPING;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001662 LY_LIST_FOR(sp->groupings, grp) {
1663 if (!(grp->flags & LYS_USED_GRP)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001664 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, grp), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001665 }
1666 }
1667 LY_LIST_FOR(sp->data, pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001668 LY_LIST_FOR((struct lysp_node_grp *)lysp_node_groupings(pnode), grp) {
1669 if (!(grp->flags & LYS_USED_GRP)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001670 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, grp), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001671 }
1672 }
1673 }
1674 LY_ARRAY_FOR(sp->includes, u) {
1675 submod = sp->includes[u].submodule;
1676 ctx.pmod = (struct lysp_module *)submod;
1677
Radek Krejci2a9fc652021-01-22 17:44:34 +01001678 LY_LIST_FOR(submod->groupings, grp) {
1679 if (!(grp->flags & LYS_USED_GRP)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001680 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, grp), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001681 }
1682 }
1683 LY_LIST_FOR(submod->data, pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001684 LY_LIST_FOR((struct lysp_node_grp *)lysp_node_groupings(pnode), grp) {
1685 if (!(grp->flags & LYS_USED_GRP)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001686 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, grp), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001687 }
1688 }
1689 }
1690 }
1691 ctx.pmod = sp;
1692
Radek Krejciddace2c2021-01-08 11:30:56 +01001693 LOG_LOCBACK(0, 0, 1, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001694
Michal Vasko405cc9e2020-12-01 12:01:27 +01001695 /* finish compilation for all unresolved module items in the context */
Michal Vasko1ccbf542021-04-19 11:35:00 +02001696 LY_CHECK_GOTO(ret = lys_compile_unres_mod(&ctx), cleanup);
Michal Vasko12606ee2020-11-25 17:05:11 +01001697
Michal Vasko1ccbf542021-04-19 11:35:00 +02001698cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01001699 LOG_LOCBACK(0, 0, 1, 0);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001700 lys_compile_unres_mod_erase(&ctx, ret);
1701 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001702 lysc_module_free(&ctx.free_ctx, mod_c);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001703 mod->compiled = NULL;
1704 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001705 return ret;
1706}
Michal Vasko65333882021-06-10 14:12:16 +02001707
aPiecek6b3d5422021-07-30 15:55:43 +02001708LY_ERR
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001709lys_compile_identities(struct lys_module *mod)
1710{
Michal Vasko776ae742022-08-04 11:08:21 +02001711 LY_ERR rc = LY_SUCCESS;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001712 struct lysc_ctx ctx;
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001713 struct lysp_submodule *submod;
1714 LY_ARRAY_COUNT_TYPE u;
1715
aPiecek6b3d5422021-07-30 15:55:43 +02001716 /* pre-compile identities of the module and any submodules */
Michal Vasko776ae742022-08-04 11:08:21 +02001717 rc = lys_identity_precompile(NULL, mod->ctx, mod->parsed, mod->parsed->identities, &mod->identities);
1718 LY_CHECK_GOTO(rc, cleanup);
aPiecek6b3d5422021-07-30 15:55:43 +02001719 LY_ARRAY_FOR(mod->parsed->includes, u) {
1720 submod = mod->parsed->includes[u].submodule;
Michal Vasko776ae742022-08-04 11:08:21 +02001721 rc = lys_identity_precompile(NULL, mod->ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
1722 LY_CHECK_GOTO(rc, cleanup);
aPiecek6b3d5422021-07-30 15:55:43 +02001723 }
1724
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001725 /* prepare context */
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001726 LYSC_CTX_INIT_PMOD(ctx, mod->parsed, NULL);
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001727
1728 if (mod->parsed->identities) {
Michal Vasko776ae742022-08-04 11:08:21 +02001729 rc = lys_compile_identities_derived(&ctx, mod->parsed->identities, &mod->identities);
1730 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001731 }
1732 lysc_update_path(&ctx, NULL, "{submodule}");
1733 LY_ARRAY_FOR(mod->parsed->includes, u) {
1734 submod = mod->parsed->includes[u].submodule;
1735 if (submod->identities) {
1736 ctx.pmod = (struct lysp_module *)submod;
1737 lysc_update_path(&ctx, NULL, submod->name);
Michal Vasko776ae742022-08-04 11:08:21 +02001738 rc = lys_compile_identities_derived(&ctx, submod->identities, &mod->identities);
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001739 lysc_update_path(&ctx, NULL, NULL);
1740 }
Michal Vasko776ae742022-08-04 11:08:21 +02001741
1742 if (rc) {
1743 break;
1744 }
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001745 }
1746 lysc_update_path(&ctx, NULL, NULL);
1747
Michal Vasko776ae742022-08-04 11:08:21 +02001748cleanup:
1749 /* always needed when using lysc_update_path() */
1750 LOG_LOCBACK(0, 0, 1, 0);
1751 return rc;
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001752}
1753
1754/**
Michal Vasko65333882021-06-10 14:12:16 +02001755 * @brief Check whether a module does not have any (recursive) compiled import.
1756 *
1757 * @param[in] mod Module to examine.
1758 * @return LY_SUCCESS on success.
Michal Vaskof4258e12021-06-15 12:11:42 +02001759 * @return LY_ERECOMPILE on required recompilation of the dep set.
Michal Vasko65333882021-06-10 14:12:16 +02001760 * @return LY_ERR on error.
1761 */
1762static LY_ERR
1763lys_has_compiled_import_r(struct lys_module *mod)
1764{
1765 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoe7927122021-06-15 12:00:04 +02001766 struct lys_module *m;
Michal Vasko65333882021-06-10 14:12:16 +02001767
1768 LY_ARRAY_FOR(mod->parsed->imports, u) {
Michal Vaskoe7927122021-06-15 12:00:04 +02001769 m = mod->parsed->imports[u].module;
1770 if (!m->implemented) {
Michal Vasko65333882021-06-10 14:12:16 +02001771 continue;
1772 }
1773
Michal Vaskoe7927122021-06-15 12:00:04 +02001774 if (!m->to_compile) {
Michal Vaskod39bea42021-06-14 10:42:49 +02001775 /* module was not/will not be compiled in this compilation (so disabled nodes are not present) */
Michal Vaskof4258e12021-06-15 12:11:42 +02001776 m->to_compile = 1;
Michal Vasko65333882021-06-10 14:12:16 +02001777 return LY_ERECOMPILE;
1778 }
1779
1780 /* recursive */
Michal Vaskoe7927122021-06-15 12:00:04 +02001781 LY_CHECK_RET(lys_has_compiled_import_r(m));
Michal Vasko65333882021-06-10 14:12:16 +02001782 }
1783
1784 return LY_SUCCESS;
1785}
1786
1787LY_ERR
1788lys_implement(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
1789{
Michal Vaskodc668d22023-02-13 10:23:13 +01001790 LY_ERR r;
Michal Vasko65333882021-06-10 14:12:16 +02001791 struct lys_module *m;
1792
1793 assert(!mod->implemented);
1794
Michal Vasko978532b2022-01-21 14:13:40 +01001795 /* check collision with other implemented revision */
Michal Vasko65333882021-06-10 14:12:16 +02001796 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
1797 if (m) {
1798 assert(m != mod);
Michal Vaskodc668d22023-02-13 10:23:13 +01001799 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s@%s\" is already implemented in revision \"%s\".",
1800 mod->name, mod->revision ? mod->revision : "<none>", m->revision ? m->revision : "<none>");
1801 return LY_EDENIED;
Michal Vasko65333882021-06-10 14:12:16 +02001802 }
1803
1804 /* set features */
Michal Vaskodc668d22023-02-13 10:23:13 +01001805 r = lys_set_features(mod->parsed, features);
1806 if (r && (r != LY_EEXIST)) {
1807 return r;
Michal Vasko65333882021-06-10 14:12:16 +02001808 }
1809
Michal Vasko65333882021-06-10 14:12:16 +02001810 /*
1811 * mark the module implemented, which means
1812 * 1) to (re)compile it only ::lys_compile() call is needed
1813 * 2) its compilation will never cause new modules to be implemented (::lys_compile() does not return ::LY_ERECOMPILE)
1814 * but there can be some unres items added that do
1815 */
1816 mod->implemented = 1;
1817
Michal Vaskoa9f807e2021-06-15 12:07:16 +02001818 /* this module is compiled in this compilation */
1819 mod->to_compile = 1;
1820
Michal Vasko65333882021-06-10 14:12:16 +02001821 /* add the module into newly implemented module set */
1822 LY_CHECK_RET(ly_set_add(&unres->implementing, mod, 1, NULL));
1823
Michal Vaskoa9f807e2021-06-15 12:07:16 +02001824 /* mark target modules with our augments and deviations */
1825 LY_CHECK_RET(lys_precompile_augments_deviations(mod, unres));
Michal Vasko65333882021-06-10 14:12:16 +02001826
Michal Vaskod39bea42021-06-14 10:42:49 +02001827 /* check whether this module may reference any modules compiled previously */
1828 LY_CHECK_RET(lys_has_compiled_import_r(mod));
Michal Vasko65333882021-06-10 14:12:16 +02001829
Michal Vasko65333882021-06-10 14:12:16 +02001830 return LY_SUCCESS;
1831}