blob: 526409521bf8276d17493c1f2d38027ca27892fe [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 Krejci77114102021-03-10 15:21:57 +010037#include "plugins_exts_compile.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010038#include "plugins_internal.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020039#include "plugins_types.h"
40#include "schema_compile_amend.h"
41#include "schema_compile_node.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010042#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020043#include "set.h"
44#include "tree.h"
45#include "tree_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020046#include "tree_schema.h"
Michal Vaskoc636ea42022-09-16 10:20:31 +020047#include "tree_schema_free.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020048#include "tree_schema_internal.h"
49#include "xpath.h"
50
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020051/**
52 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
53 */
54static LY_ERR
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020055lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p,
56 const struct lyplg_ext_record *record, struct lysc_ext **ext)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020057{
58 LY_ERR ret = LY_SUCCESS;
59
60 if (!ext_p->compiled) {
61 lysc_update_path(ctx, NULL, "{extension}");
62 lysc_update_path(ctx, NULL, ext_p->name);
63
64 /* compile the extension definition */
Michal Vaskoc636ea42022-09-16 10:20:31 +020065 *ext = ext_p->compiled = calloc(1, sizeof **ext);
66 (*ext)->refcount = 1;
67 DUP_STRING_GOTO(ctx->ctx, ext_p->name, (*ext)->name, ret, done);
68 DUP_STRING_GOTO(ctx->ctx, ext_p->argname, (*ext)->argname, ret, done);
69 (*ext)->module = (struct lys_module *)ext_mod;
70
71 /* compile nested extensions */
72 COMPILE_EXTS_GOTO(ctx, ext_p->exts, (*ext)->exts, *ext, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020073
74 lysc_update_path(ctx, NULL, NULL);
75 lysc_update_path(ctx, NULL, NULL);
76
77 /* find extension definition plugin */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020078 (*ext)->plugin = record ? (struct lyplg_ext *)&record->plugin : NULL;
Michal Vasko54b11952022-06-08 12:29:39 +020079 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020080
Michal Vaskoc636ea42022-09-16 10:20:31 +020081 *ext = ext_p->compiled;
82
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020083done:
Radek Krejci2efc45b2020-12-22 16:25:44 +010084 if (ret) {
85 lysc_update_path(ctx, NULL, NULL);
86 lysc_update_path(ctx, NULL, NULL);
87 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020088 return ret;
89}
90
91LY_ERR
92lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
Radek Krejciab430862021-03-02 20:13:40 +010093 const struct lys_module *ext_mod)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020094{
Radek Krejci85ac8312021-03-03 20:21:33 +010095 LY_ERR ret = LY_SUCCESS;
96 struct lysp_ext *ext_def;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020097
Radek Krejciab430862021-03-02 20:13:40 +010098 ext->parent_stmt = ext_p->parent_stmt;
99 ext->parent_stmt_index = ext_p->parent_stmt_index;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200100 ext->module = ctx->cur_mod;
101 ext->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200102
Radek Krejciab430862021-03-02 20:13:40 +0100103 lysc_update_path(ctx, LY_STMT_IS_NODE(ext->parent_stmt) ? ((struct lysc_node *)ext->parent)->module : NULL, "{extension}");
Michal Vaskofc2cd072021-02-24 13:17:17 +0100104 lysc_update_path(ctx, NULL, ext_p->name);
105
Radek Krejci85ac8312021-03-03 20:21:33 +0100106 LY_CHECK_GOTO(ret = lysp_ext_find_definition(ctx->ctx, ext_p, &ext_mod, &ext_def), cleanup);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200107 LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, ext_def, ext_p->record, &ext->def), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200108
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100109 if (ext_def->argname) {
Radek Krejci85ac8312021-03-03 20:21:33 +0100110 LY_CHECK_GOTO(ret = lysp_ext_instance_resolve_argument(ctx->ctx, ext_p, ext_def), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200111 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200112
Michal Vasko9b232082022-06-07 10:59:31 +0200113 DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext->argument, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200114
115 if (ext->def->plugin && ext->def->plugin->compile) {
116 if (ext->argument) {
Radek Krejcia6016992021-03-03 10:13:41 +0100117 lysc_update_path(ctx, ext->module, ext->argument);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200118 }
Michal Vaskofc2cd072021-02-24 13:17:17 +0100119 ret = ext->def->plugin->compile(ctx, ext_p, ext);
Radek Krejcicc21a4f2021-02-09 15:23:31 +0100120 if (ret == LY_ENOT) {
Michal Vaskoc636ea42022-09-16 10:20:31 +0200121 lysc_ext_instance_free(&ctx->free_ctx, ext);
Radek Krejcicc21a4f2021-02-09 15:23:31 +0100122 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200123 if (ext->argument) {
124 lysc_update_path(ctx, NULL, NULL);
125 }
Michal Vaskofc2cd072021-02-24 13:17:17 +0100126 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200127 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200128
129cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100130 lysc_update_path(ctx, NULL, NULL);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100131 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200132 return ret;
133}
134
Michal Vaskocc28b152022-08-23 14:44:54 +0200135LIBYANG_API_DEF struct lysc_ext *
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200136lysc_ext_dup(struct lysc_ext *orig)
137{
138 ++orig->refcount;
139 return orig;
140}
141
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100142LIBYANG_API_DEF LY_ERR
Michal Vasko9c3556a2022-10-06 16:08:47 +0200143lysc_ext_substmt(const struct lysc_ext_instance *ext, enum ly_stmt substmt, void **instance_p)
Radek Krejci1b2eef82021-02-17 11:17:27 +0100144{
145 LY_ARRAY_COUNT_TYPE u;
146
Michal Vasko55bce0e2022-02-15 10:46:08 +0100147 if (instance_p) {
148 *instance_p = NULL;
149 }
Michal Vasko55bce0e2022-02-15 10:46:08 +0100150
Radek Krejci1b2eef82021-02-17 11:17:27 +0100151 LY_ARRAY_FOR(ext->substmts, u) {
Radek Krejci61837f72021-03-02 19:53:59 +0100152 if (LY_STMT_IS_DATA_NODE(substmt)) {
153 if (!LY_STMT_IS_DATA_NODE(ext->substmts[u].stmt)) {
Radek Krejci1b2eef82021-02-17 11:17:27 +0100154 continue;
155 }
156 } else if (LY_STMT_IS_OP(substmt)) {
157 if (!LY_STMT_IS_OP(ext->substmts[u].stmt)) {
158 continue;
159 }
160 } else if (ext->substmts[u].stmt != substmt) {
161 continue;
162 }
163
164 /* match */
Radek Krejci1b2eef82021-02-17 11:17:27 +0100165 if (instance_p) {
166 *instance_p = ext->substmts[u].storage;
167 }
168 return LY_SUCCESS;
169 }
170
171 return LY_ENOT;
172}
173
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200174static void
Michal Vaskoc130e162021-10-19 11:30:00 +0200175lysc_unres_must_free(struct lysc_unres_must *m)
176{
177 LY_ARRAY_FREE(m->local_mods);
178 free(m);
179}
180
181static void
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200182lysc_unres_dflt_free(const struct ly_ctx *ctx, struct lysc_unres_dflt *r)
183{
184 assert(!r->dflt || !r->dflts);
185 if (r->dflt) {
186 lysp_qname_free((struct ly_ctx *)ctx, r->dflt);
187 free(r->dflt);
188 } else {
189 FREE_ARRAY((struct ly_ctx *)ctx, r->dflts, lysp_qname_free);
190 }
191 free(r);
192}
193
Michal Vaskocc28b152022-08-23 14:44:54 +0200194LIBYANG_API_DEF void
Radek Krejcia6016992021-03-03 10:13:41 +0100195lysc_update_path(struct lysc_ctx *ctx, struct lys_module *parent_module, const char *name)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200196{
197 int len;
198 uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
199
200 if (!name) {
201 /* removing last path segment */
202 if (ctx->path[ctx->path_len - 1] == '}') {
203 for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
204 if (ctx->path[ctx->path_len] == '=') {
205 ctx->path[ctx->path_len++] = '}';
206 } else {
207 /* not a top-level special tag, remove also preceiding '/' */
208 goto remove_nodelevel;
209 }
210 } else {
211remove_nodelevel:
212 for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
213 if (ctx->path_len == 0) {
214 /* top-level (last segment) */
215 ctx->path_len = 1;
216 }
217 }
218 /* set new terminating NULL-byte */
219 ctx->path[ctx->path_len] = '\0';
220 } else {
221 if (ctx->path_len > 1) {
Radek Krejcia6016992021-03-03 10:13:41 +0100222 if (!parent_module && (ctx->path[ctx->path_len - 1] == '}') && (ctx->path[ctx->path_len - 2] != '\'')) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200223 /* extension of the special tag */
224 nextlevel = 2;
225 --ctx->path_len;
226 } else {
227 /* there is already some path, so add next level */
228 nextlevel = 1;
229 }
230 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
231
232 if (nextlevel != 2) {
Radek Krejcia6016992021-03-03 10:13:41 +0100233 if ((parent_module && (parent_module == ctx->cur_mod)) || (!parent_module && (ctx->path_len > 1) && (name[0] == '{'))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200234 /* module not changed, print the name unprefixed */
235 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
236 } else {
237 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->cur_mod->name, name);
238 }
239 } else {
240 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
241 }
242 if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
243 /* output truncated */
244 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
245 } else {
246 ctx->path_len += len;
247 }
248 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100249
Radek Krejciddace2c2021-01-08 11:30:56 +0100250 LOG_LOCBACK(0, 0, 1, 0);
251 LOG_LOCSET(NULL, NULL, ctx->path, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200252}
253
aPiecek6b3d5422021-07-30 15:55:43 +0200254/**
255 * @brief Compile information from the identity statement
256 *
257 * The backlinks to the identities derived from this one are supposed to be filled later via ::lys_compile_identity_bases().
258 *
259 * @param[in] ctx_sc Compile context - alternative to the combination of @p ctx and @p parsed_mod.
260 * @param[in] ctx libyang context.
261 * @param[in] parsed_mod Module with the identities.
262 * @param[in] identities_p Array of the parsed identity definitions to precompile.
263 * @param[in,out] identities Pointer to the storage of the (pre)compiled identities array where the new identities are
264 * supposed to be added. The storage is supposed to be initiated to NULL when the first parsed identities are going
265 * to be processed.
266 * @return LY_ERR value.
267 */
268static LY_ERR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200269lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
Michal Vasko9b232082022-06-07 10:59:31 +0200270 const struct lysp_ident *identities_p, struct lysc_ident **identities)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200271{
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100272 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoc636ea42022-09-16 10:20:31 +0200273 struct lysc_ctx cctx;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100274 struct lysc_ident *ident;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200275 LY_ERR ret = LY_SUCCESS;
276
277 assert(ctx_sc || ctx);
278
279 if (!ctx_sc) {
Michal Vaskoc636ea42022-09-16 10:20:31 +0200280 if (parsed_mod) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200281 LYSC_CTX_INIT_PMOD(cctx, parsed_mod, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200282 } else {
283 LYSC_CTX_INIT_CTX(cctx, ctx);
284 }
285 ctx_sc = &cctx;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200286 }
287
288 if (!identities_p) {
289 return LY_SUCCESS;
290 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200291
292 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200293 LY_ARRAY_FOR(identities_p, u) {
294 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
295
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100296 /* add new compiled identity */
Michal Vasko9b232082022-06-07 10:59:31 +0200297 LY_ARRAY_NEW_GOTO(ctx_sc->ctx, *identities, ident, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100298
299 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, ident->name, ret, done);
300 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, ident->dsc, ret, done);
301 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, ident->ref, ret, done);
302 ident->module = ctx_sc->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200303 /* 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 +0100304 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, ident->exts, ident, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100305 ident->flags = identities_p[u].flags;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200306
307 lysc_update_path(ctx_sc, NULL, NULL);
308 }
309 lysc_update_path(ctx_sc, NULL, NULL);
Michal Vasko9b232082022-06-07 10:59:31 +0200310
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200311done:
Michal Vasko9b232082022-06-07 10:59:31 +0200312 if (ret) {
313 lysc_update_path(ctx_sc, NULL, NULL);
314 lysc_update_path(ctx_sc, NULL, NULL);
315 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200316 return ret;
317}
318
319/**
320 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
321 *
322 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
323 *
324 * @param[in] ctx Compile context for logging.
325 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
326 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
327 * @return LY_SUCCESS if everything is ok.
328 * @return LY_EVALID if the identity is derived from itself.
329 */
330static LY_ERR
331lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
332{
333 LY_ERR ret = LY_SUCCESS;
334 LY_ARRAY_COUNT_TYPE u, v;
335 struct ly_set recursion = {0};
336 struct lysc_ident *drv;
337
338 if (!derived) {
339 return LY_SUCCESS;
340 }
341
342 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
343 if (ident == derived[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100344 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200345 "Identity \"%s\" is indirectly derived from itself.", ident->name);
346 ret = LY_EVALID;
347 goto cleanup;
348 }
349 ret = ly_set_add(&recursion, derived[u], 0, NULL);
350 LY_CHECK_GOTO(ret, cleanup);
351 }
352
353 for (v = 0; v < recursion.count; ++v) {
354 drv = recursion.objs[v];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200355 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
356 if (ident == drv->derived[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100357 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200358 "Identity \"%s\" is indirectly derived from itself.", ident->name);
359 ret = LY_EVALID;
360 goto cleanup;
361 }
362 ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
363 LY_CHECK_GOTO(ret, cleanup);
364 }
365 }
366
367cleanup:
368 ly_set_erase(&recursion, NULL);
369 return ret;
370}
371
372LY_ERR
373lys_compile_identity_bases(struct lysc_ctx *ctx, const struct lysp_module *base_pmod, const char **bases_p,
aPiecekf4a0a192021-08-03 15:14:17 +0200374 struct lysc_ident *ident, struct lysc_ident ***bases)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200375{
376 LY_ARRAY_COUNT_TYPE u, v;
377 const char *s, *name;
378 const struct lys_module *mod;
379 struct lysc_ident **idref;
380
aPiecekf4a0a192021-08-03 15:14:17 +0200381 assert(ident || bases);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200382
383 if ((LY_ARRAY_COUNT(bases_p) > 1) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100384 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200385 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
386 return LY_EVALID;
387 }
388
389 LY_ARRAY_FOR(bases_p, u) {
390 s = strchr(bases_p[u], ':');
391 if (s) {
392 /* prefixed identity */
393 name = &s[1];
Radek Krejci8df109d2021-04-23 12:19:08 +0200394 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 +0200395 } else {
396 name = bases_p[u];
397 mod = base_pmod->mod;
398 }
399 if (!mod) {
400 if (ident) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100401 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200402 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
403 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100404 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200405 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
406 }
407 return LY_EVALID;
408 }
409
410 idref = NULL;
411 LY_ARRAY_FOR(mod->identities, v) {
412 if (!strcmp(name, mod->identities[v].name)) {
413 if (ident) {
414 if (ident == &mod->identities[v]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100415 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200416 "Identity \"%s\" is derived from itself.", ident->name);
417 return LY_EVALID;
418 }
419 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived));
420 /* we have match! store the backlink */
421 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
422 *idref = ident;
423 } else {
424 /* we have match! store the found identity */
425 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
426 *idref = &mod->identities[v];
427 }
428 break;
429 }
430 }
aPiecekf4a0a192021-08-03 15:14:17 +0200431 if (!idref) {
Radek Krejci10443f42021-03-28 17:40:35 +0200432 if (ident) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100433 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200434 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
435 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100436 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200437 "Unable to find base (%s) of identityref.", bases_p[u]);
438 }
439 return LY_EVALID;
440 }
441 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100442
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200443 return LY_SUCCESS;
444}
445
446/**
447 * @brief For the given array of identities, set the backlinks from all their base identities.
448 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
449 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
aPiecekf4a0a192021-08-03 15:14:17 +0200450 * @param[in,out] idents Array of referencing identities to which the backlinks are supposed to be set.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200451 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
452 */
453static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100454lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident **idents)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200455{
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100456 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200457
458 lysc_update_path(ctx, NULL, "{identity}");
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100459
aPiecekf3e2db82021-08-05 10:18:43 +0200460 for (u = 0; u < LY_ARRAY_COUNT(*idents); ++u) {
aPiecekf4a0a192021-08-03 15:14:17 +0200461 /* find matching parsed identity */
aPiecekf3e2db82021-08-05 10:18:43 +0200462 for (v = 0; v < LY_ARRAY_COUNT(idents_p); ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100463 if (idents_p[v].name == (*idents)[u].name) {
464 break;
465 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100466 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100467
Michal Vaskodc6f84c2021-06-15 14:50:27 +0200468 if ((v == LY_ARRAY_COUNT(idents_p)) || !idents_p[v].bases) {
469 /* identity not found (it may be from a submodule) or identity without bases */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200470 continue;
471 }
Michal Vaskodc6f84c2021-06-15 14:50:27 +0200472
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100473 lysc_update_path(ctx, NULL, (*idents)[u].name);
aPiecekf4a0a192021-08-03 15:14:17 +0200474 LY_CHECK_RET(lys_compile_identity_bases(ctx, ctx->pmod, idents_p[v].bases, &(*idents)[u], NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200475 lysc_update_path(ctx, NULL, NULL);
476 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100477
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200478 lysc_update_path(ctx, NULL, NULL);
479 return LY_SUCCESS;
480}
481
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200482const void *
483lys_compile_ext_instance_get_storage(const struct lysc_ext_instance *ext, enum ly_stmt stmt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200484{
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200485 LY_ARRAY_COUNT_TYPE u;
486
487 LY_ARRAY_FOR(ext->substmts, u) {
488 if (ext->substmts[u].stmt == stmt) {
489 return ext->substmts[u].storage;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200490 }
491 }
492 return NULL;
493}
494
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200495/**
496 * @brief Store (parse/compile) an instance extension statement.
497 *
498 * @param[in] ctx Compile context.
499 * @param[in] ext_p Parsed ext instance.
500 * @param[in] ext Compiled ext instance.
501 * @param[in] substmt Compled ext instance substatement info.
502 * @param[in] stmt Parsed statement to process.
Michal Vasko0b50f6b2022-10-05 15:07:55 +0200503 * @param[in,out] aug_target Optional augment target where to append all schema data nodes.
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200504 * @return LY_ERR value.
505 */
506static LY_ERR
507lys_compile_ext_instance_stmt(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
Michal Vasko0b50f6b2022-10-05 15:07:55 +0200508 struct lysc_ext_substmt *substmt, struct lysp_stmt *stmt, struct lysc_node *aug_target)
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200509{
510 LY_ERR rc = LY_SUCCESS;
511 struct lysf_ctx fctx = {.ctx = ctx->ctx};
512 struct lysp_restr *restrs = NULL;
513 struct lysp_qname *qname = NULL;
514 struct lysp_type *ptype = NULL;
515
516 if (!substmt->storage) {
517 /* nothing to store (parse/compile) */
518 goto cleanup;
519 }
520
521 switch (stmt->kw) {
522 case LY_STMT_ACTION:
523 case LY_STMT_ANYDATA:
524 case LY_STMT_ANYXML:
525 case LY_STMT_CONTAINER:
526 case LY_STMT_CHOICE:
527 case LY_STMT_LEAF:
528 case LY_STMT_LEAF_LIST:
529 case LY_STMT_LIST:
530 case LY_STMT_NOTIFICATION:
531 case LY_STMT_RPC:
532 case LY_STMT_USES: {
533 struct lysp_node **pnodes_p, *pnode = NULL;
534 const uint16_t *flags = lys_compile_ext_instance_get_storage(ext, LY_STMT_STATUS);
535
536 /* parse the node */
537 LY_CHECK_GOTO(rc = lysp_stmt_parse(ctx, stmt, (void **)&pnode, NULL), cleanup);
538
539 /* store it together with all the parsed schema nodes */
540 pnodes_p = &((struct lysp_ext_instance *)ext_p)->parsed;
541 while (*pnodes_p) {
542 pnodes_p = &(*pnodes_p)->next;
543 }
544 *pnodes_p = pnode;
545
Michal Vasko0b50f6b2022-10-05 15:07:55 +0200546 if (aug_target) {
547 /* augment nodes */
548 ((struct lysp_ext_instance *)ext_p)->flags |= LYS_EXT_PARSED_AUGMENT;
549
550 /* compile augmented nodes */
551 LY_CHECK_GOTO(rc = lys_compile_augment_children(ctx, NULL, 0, pnode, aug_target, 0), cleanup);
552 } else {
553 /* compile nodes, ctx->ext substatement storage is used as the document root */
554 LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, NULL, flags, NULL), cleanup);
555 }
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200556 break;
557 }
558 case LY_STMT_GROUPING: {
559 struct lysp_node_grp **groupings_p, *grp = NULL;
560
561 /* parse the grouping */
562 LY_CHECK_GOTO(rc = lysp_stmt_parse(ctx, stmt, (void **)&grp, NULL), cleanup);
563
564 /* store it with all the other groupings */
565 groupings_p = substmt->storage;
566 while (*groupings_p) {
567 groupings_p = &(*groupings_p)->next;
568 }
569 *groupings_p = grp;
570 break;
571 }
572 case LY_STMT_CONTACT:
573 case LY_STMT_DESCRIPTION:
574 case LY_STMT_ERROR_APP_TAG:
575 case LY_STMT_ERROR_MESSAGE:
576 case LY_STMT_KEY:
577 case LY_STMT_NAMESPACE:
578 case LY_STMT_ORGANIZATION:
579 case LY_STMT_PRESENCE:
580 case LY_STMT_REFERENCE:
581 case LY_STMT_UNITS: {
Michal Vasko9c3556a2022-10-06 16:08:47 +0200582 const char **str_p;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200583
Michal Vasko9c3556a2022-10-06 16:08:47 +0200584 /* single item */
585 str_p = substmt->storage;
586 if (*str_p) {
587 LOGVAL(ctx->ctx, LY_VCODE_DUPSTMT, stmt->stmt);
588 rc = LY_EVALID;
589 goto cleanup;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200590 }
591
592 /* called instead of lysp_stmt_parse() to skip validation and not parse nested ext instances */
593 LY_CHECK_GOTO(rc = lydict_insert(ctx->ctx, stmt->arg, 0, str_p), cleanup);
594 break;
595 }
596 case LY_STMT_MUST: {
Michal Vasko9c3556a2022-10-06 16:08:47 +0200597 struct lysc_must **musts_p, *must;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200598
599 /* parse */
600 LY_CHECK_GOTO(rc = lysp_stmt_parse(ctx, stmt, (void **)&restrs, NULL), cleanup);
601
Michal Vasko9c3556a2022-10-06 16:08:47 +0200602 /* sized array */
603 musts_p = substmt->storage;
604 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts_p, must, rc, cleanup);
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200605
606 /* compile */
607 LY_CHECK_GOTO(rc = lys_compile_must(ctx, restrs, must), cleanup);
608 break;
609 }
610 case LY_STMT_IF_FEATURE: {
611 ly_bool enabled;
612
613 LY_CHECK_GOTO(rc = lysp_stmt_parse(ctx, stmt, (void **)&qname, NULL), cleanup);
614 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, qname, &enabled), cleanup);
615 if (!enabled) {
616 /* it is disabled, remove the whole extension instance */
617 return LY_ENOT;
618 }
619 break;
620 }
621 case LY_STMT_STATUS:
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200622 /* result needs to be a pointer to pointer */
623 LY_CHECK_GOTO(rc = lysp_stmt_parse(ctx, stmt, &substmt->storage, NULL), cleanup);
624 break;
625
626 case LY_STMT_TYPEDEF:
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200627 /* parse */
628 LY_CHECK_GOTO(rc = lysp_stmt_parse(ctx, stmt, substmt->storage, NULL), cleanup);
629 break;
630
631 case LY_STMT_TYPE: {
Michal Vasko9c3556a2022-10-06 16:08:47 +0200632 struct lysc_type **type_p;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200633 const uint16_t *flags = lys_compile_ext_instance_get_storage(ext, LY_STMT_STATUS);
634 const char **units = (void *)lys_compile_ext_instance_get_storage(ext, LY_STMT_UNITS);
635
Michal Vasko9c3556a2022-10-06 16:08:47 +0200636 /* single item */
637 type_p = substmt->storage;
638 if (*type_p) {
639 LOGVAL(ctx->ctx, LY_VCODE_DUPSTMT, stmt->stmt);
640 rc = LY_EVALID;
641 goto cleanup;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200642 }
643
644 LY_CHECK_GOTO(rc = lysp_stmt_parse(ctx, stmt, (void **)&ptype, NULL), cleanup);
645 LY_CHECK_GOTO(rc = lys_compile_type(ctx, NULL, flags ? *flags : 0, ext_p->name, ptype, type_p,
646 (units && !*units) ? units : NULL, NULL), cleanup);
647 break;
648 }
649 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
650 * also note that in many statements their extensions are not taken into account */
651 default:
Michal Vasko9c3556a2022-10-06 16:08:47 +0200652 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension "
653 "(found in \"%s%s%s\") substatement.", stmt->stmt, ext_p->name, ext_p->argument ? " " : "",
654 ext_p->argument ? ext_p->argument : "");
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200655 rc = LY_EVALID;
656 goto cleanup;
657 }
658
659cleanup:
660 FREE_ARRAY(&fctx, restrs, lysp_restr_free);
661 FREE_ARRAY(ctx->ctx, qname, lysp_qname_free);
662 lysp_type_free(&ctx->free_ctx, ptype);
663 free(ptype);
664 return rc;
665}
666
Michal Vasko0b50f6b2022-10-05 15:07:55 +0200667static LY_ERR
668lys_compile_extension_instance_(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext,
669 struct lysc_node *aug_target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200670{
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200671 LY_ERR rc = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200672 LY_ARRAY_COUNT_TYPE u;
673 struct lysp_stmt *stmt;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200674
675 /* check for invalid substatements */
Radek Krejci6b88a462021-02-17 12:39:34 +0100676 for (stmt = ext_p->child; stmt; stmt = stmt->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200677 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
678 continue;
679 }
Radek Krejci6b88a462021-02-17 12:39:34 +0100680 LY_ARRAY_FOR(ext->substmts, u) {
681 if (ext->substmts[u].stmt == stmt->kw) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200682 break;
683 }
684 }
Radek Krejci6b88a462021-02-17 12:39:34 +0100685 if (u == LY_ARRAY_COUNT(ext->substmts)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100686 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
Radek Krejci6b88a462021-02-17 12:39:34 +0100687 stmt->stmt, ext_p->name, ext_p->argument ? " " : "", ext_p->argument ? ext_p->argument : "");
Michal Vasko633ae8a2022-08-25 09:52:02 +0200688 rc = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200689 goto cleanup;
690 }
691 }
692
693 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
694
Radek Krejci6b88a462021-02-17 12:39:34 +0100695 /* note into the compile context that we are processing extension now */
696 ctx->ext = ext;
697
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200698 /* keep order of the processing the same as the order in the defined substmts,
699 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200700
Radek Krejci6b88a462021-02-17 12:39:34 +0100701 LY_ARRAY_FOR(ext->substmts, u) {
Radek Krejci6b88a462021-02-17 12:39:34 +0100702 for (stmt = ext_p->child; stmt; stmt = stmt->next) {
703 if (ext->substmts[u].stmt != stmt->kw) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200704 continue;
705 }
706
Michal Vasko0b50f6b2022-10-05 15:07:55 +0200707 if ((rc = lys_compile_ext_instance_stmt(ctx, ext_p, ext, &ext->substmts[u], stmt, aug_target))) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200708 goto cleanup;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200709 }
710 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200711 }
712
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200713cleanup:
Radek Krejci6b88a462021-02-17 12:39:34 +0100714 ctx->ext = NULL;
Michal Vasko633ae8a2022-08-25 09:52:02 +0200715 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200716}
717
Michal Vasko0b50f6b2022-10-05 15:07:55 +0200718LIBYANG_API_DEF LY_ERR
719lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext)
720{
721 LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, ctx, ext_p, ext, LY_EINVAL);
722
723 return lys_compile_extension_instance_(ctx, ext_p, ext, NULL);
724}
725
726LIBYANG_API_DEF LY_ERR
727lys_compile_extension_instance_augment(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext_p,
728 struct lysc_ext_instance *ext, struct lysc_node *aug_target)
729{
730 LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, ctx, ext_p, ext, aug_target, LY_EINVAL);
731
732 return lys_compile_extension_instance_(ctx, ext_p, ext, aug_target);
733}
734
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200735/**
736 * @brief Check when for cyclic dependencies.
737 *
738 * @param[in] set Set with all the referenced nodes.
739 * @param[in] node Node whose "when" referenced nodes are in @p set.
740 * @return LY_ERR value
741 */
742static LY_ERR
743lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
744{
745 struct lyxp_set tmp_set;
746 struct lyxp_set_scnode *xp_scnode;
747 uint32_t i, j;
748 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200749 LY_ERR ret = LY_SUCCESS;
750
751 memset(&tmp_set, 0, sizeof tmp_set);
752
753 /* prepare in_ctx of the set */
754 for (i = 0; i < set->used; ++i) {
755 xp_scnode = &set->val.scnodes[i];
756
Radek Krejcif13b87b2020-12-01 22:02:17 +0100757 if (xp_scnode->in_ctx != LYXP_SET_SCNODE_START_USED) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200758 /* check node when, skip the context node (it was just checked) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100759 xp_scnode->in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200760 }
761 }
762
763 for (i = 0; i < set->used; ++i) {
764 xp_scnode = &set->val.scnodes[i];
Radek Krejcif13b87b2020-12-01 22:02:17 +0100765 if (xp_scnode->in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200766 /* already checked */
767 continue;
768 }
769
Michal Vasko1a09b212021-05-06 13:00:10 +0200770 if ((xp_scnode->type != LYXP_NODE_ELEM) || !lysc_node_when(xp_scnode->scnode)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200771 /* no when to check */
Michal Vasko1a09b212021-05-06 13:00:10 +0200772 xp_scnode->in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200773 continue;
774 }
775
776 node = xp_scnode->scnode;
777 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100778 struct lysc_when **when_list, *when;
779
Radek Krejciddace2c2021-01-08 11:30:56 +0100780 LOG_LOCSET(node, NULL, NULL, NULL);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100781 when_list = lysc_node_when(node);
782 LY_ARRAY_FOR(when_list, u) {
783 when = when_list[u];
Radek Krejci8df109d2021-04-23 12:19:08 +0200784 ret = lyxp_atomize(set->ctx, when->cond, node->module, LY_VALUE_SCHEMA_RESOLVED, when->prefixes,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +0200785 when->context, when->context, &tmp_set, LYXP_SCNODE_SCHEMA);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200786 if (ret != LY_SUCCESS) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100787 LOGVAL(set->ctx, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vasko59e69e72022-02-18 09:18:21 +0100788 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200789 goto cleanup;
790 }
791
792 for (j = 0; j < tmp_set.used; ++j) {
793 /* skip roots'n'stuff */
794 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
795 /* try to find this node in our set */
796 uint32_t idx;
Michal Vasko26bbb272022-08-02 14:54:33 +0200797
Radek Krejcif13b87b2020-12-01 22:02:17 +0100798 if (lyxp_set_scnode_contains(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1, &idx) &&
799 (set->val.scnodes[idx].in_ctx == LYXP_SET_SCNODE_START_USED)) {
Michal Vasko2c4d9152021-07-20 09:17:28 +0200800 LOGVAL(set->ctx, LYVE_SEMANTICS, "When condition cyclic dependency on the node \"%s\".",
801 tmp_set.val.scnodes[j].scnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200802 ret = LY_EVALID;
Michal Vasko59e69e72022-02-18 09:18:21 +0100803 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200804 goto cleanup;
805 }
806
807 /* needs to be checked, if in both sets, will be ignored */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100808 tmp_set.val.scnodes[j].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200809 } else {
810 /* no when, nothing to check */
Michal Vasko1a09b212021-05-06 13:00:10 +0200811 tmp_set.val.scnodes[j].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200812 }
813 }
814
815 /* merge this set into the global when set */
816 lyxp_set_scnode_merge(set, &tmp_set);
817 }
818
819 /* check when of non-data parents as well */
820 node = node->parent;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100821
Radek Krejciddace2c2021-01-08 11:30:56 +0100822 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200823 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
824
825 /* this node when was checked (xp_scnode could have been reallocd) */
Michal Vasko60edc2f2021-07-20 09:18:02 +0200826 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200827 }
828
829cleanup:
830 lyxp_set_free_content(&tmp_set);
831 return ret;
832}
833
834LY_ERR
835lysc_check_status(struct lysc_ctx *ctx, uint16_t flags1, void *mod1, const char *name1, uint16_t flags2, void *mod2,
836 const char *name2)
837{
838 uint16_t flg1, flg2;
839
840 flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
841 flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
842
843 if ((flg1 < flg2) && (mod1 == mod2)) {
844 if (ctx) {
Michal Vasko23864e02021-06-24 09:23:58 +0200845 LOGVAL(ctx->ctx, LYVE_REFERENCE, "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200846 flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1,
847 flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2);
848 }
849 return LY_EVALID;
850 }
851
852 return LY_SUCCESS;
853}
854
Michal Vasko25d6ad02020-10-22 12:20:22 +0200855LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200856lys_compile_expr_implement(const struct ly_ctx *ctx, const struct lyxp_expr *expr, LY_VALUE_FORMAT format,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100857 void *prefix_data, ly_bool implement, struct lys_glob_unres *unres, const struct lys_module **mod_p)
Michal Vaskocfaff232020-10-20 09:35:14 +0200858{
859 uint32_t i;
Michal Vaskoc56d6372021-10-19 12:29:00 +0200860 const char *ptr, *start, **imp_f, *all_f[] = {"*", NULL};
Michal Vaskocfaff232020-10-20 09:35:14 +0200861 const struct lys_module *mod;
862
Michal Vasko25d6ad02020-10-22 12:20:22 +0200863 assert(implement || mod_p);
864
Michal Vaskocfaff232020-10-20 09:35:14 +0200865 for (i = 0; i < expr->used; ++i) {
866 if ((expr->tokens[i] != LYXP_TOKEN_NAMETEST) && (expr->tokens[i] != LYXP_TOKEN_LITERAL)) {
867 /* token cannot have a prefix */
868 continue;
869 }
870
871 start = expr->expr + expr->tok_pos[i];
872 if (!(ptr = ly_strnchr(start, ':', expr->tok_len[i]))) {
873 /* token without a prefix */
874 continue;
875 }
876
877 if (!(mod = ly_resolve_prefix(ctx, start, ptr - start, format, prefix_data))) {
878 /* unknown prefix, do not care right now */
879 continue;
880 }
881
Michal Vasko0c296722021-07-14 08:53:16 +0200882 /* unimplemented module found */
883 if (!mod->implemented && !implement) {
884 /* should not be implemented now */
885 *mod_p = mod;
886 break;
887 }
888
Michal Vaskocfaff232020-10-20 09:35:14 +0200889 if (!mod->implemented) {
Michal Vasko0c296722021-07-14 08:53:16 +0200890 /* implement if not implemented */
Michal Vaskoc56d6372021-10-19 12:29:00 +0200891 imp_f = (ctx->flags & LY_CTX_ENABLE_IMP_FEATURES) ? all_f : NULL;
892 LY_CHECK_RET(lys_implement((struct lys_module *)mod, imp_f, unres));
Michal Vasko0c296722021-07-14 08:53:16 +0200893 }
894 if (!mod->compiled) {
895 /* compile if not implemented before or only marked for compilation */
896 LY_CHECK_RET(lys_compile((struct lys_module *)mod, &unres->ds_unres));
Michal Vaskocfaff232020-10-20 09:35:14 +0200897 }
898 }
899
Michal Vasko25d6ad02020-10-22 12:20:22 +0200900 return LY_SUCCESS;
Michal Vaskocfaff232020-10-20 09:35:14 +0200901}
902
903/**
Michal Vaskoc130e162021-10-19 11:30:00 +0200904 * @brief Check when expressions of a node on a complete compiled schema tree.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200905 *
906 * @param[in] ctx Compile context.
907 * @param[in] node Node to check.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100908 * @param[in,out] unres Global unres structure.
Michal Vasko40c158c2021-04-28 17:01:03 +0200909 * @return LY_ERECOMPILE
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200910 * @return LY_ERR value
911 */
912static LY_ERR
Michal Vaskoc130e162021-10-19 11:30:00 +0200913lys_compile_unres_when(struct lysc_ctx *ctx, const struct lysc_node *node, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200914{
915 struct lyxp_set tmp_set;
916 uint32_t i, opts;
917 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100918 struct lysc_when **whens = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200919 LY_ERR ret = LY_SUCCESS;
Michal Vaskocfaff232020-10-20 09:35:14 +0200920 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200921
Radek Krejciddace2c2021-01-08 11:30:56 +0100922 LOG_LOCSET(node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100923
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200924 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100925 opts = LYXP_SCNODE_SCHEMA | ((node->flags & LYS_IS_OUTPUT) ? LYXP_SCNODE_OUTPUT : 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200926
Radek Krejci9a3823e2021-01-27 20:26:46 +0100927 whens = lysc_node_when(node);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100928 LY_ARRAY_FOR(whens, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +0200929 /* first check whether all the referenced modules are implemented */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200930 mod = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200931 ret = lys_compile_expr_implement(ctx->ctx, whens[u]->cond, LY_VALUE_SCHEMA_RESOLVED, whens[u]->prefixes,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100932 ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, unres, &mod);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200933 if (ret) {
934 goto cleanup;
935 } else if (mod) {
Michal Vaskocfaff232020-10-20 09:35:14 +0200936 LOGWRN(ctx->ctx, "When condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
Radek Krejci9a3823e2021-01-27 20:26:46 +0100937 whens[u]->cond->expr, mod->name);
Michal Vaskocfaff232020-10-20 09:35:14 +0200938 continue;
939 }
940
941 /* check "when" */
Radek Krejci8df109d2021-04-23 12:19:08 +0200942 ret = lyxp_atomize(ctx->ctx, whens[u]->cond, node->module, LY_VALUE_SCHEMA_RESOLVED, whens[u]->prefixes,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +0200943 whens[u]->context, whens[u]->context, &tmp_set, opts);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200944 if (ret) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100945 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid when condition \"%s\".", whens[u]->cond->expr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200946 goto cleanup;
947 }
948
949 ctx->path[0] = '\0';
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100950 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200951 for (i = 0; i < tmp_set.used; ++i) {
952 /* skip roots'n'stuff */
Michal Vasko6b8c5102021-10-19 11:29:32 +0200953 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) &&
954 (tmp_set.val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START_USED)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200955 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
956
957 /* XPath expression cannot reference "lower" status than the node that has the definition */
Michal Vasko7c3134d2022-07-14 10:57:59 +0200958 if (lysc_check_status(NULL, whens[u]->flags, node->module, node->name, schema->flags, schema->module,
959 schema->name)) {
960 LOGWRN(ctx->ctx, "When condition \"%s\" may be referencing %s node \"%s\".", whens[u]->cond->expr,
961 (schema->flags == LYS_STATUS_OBSLT) ? "obsolete" : "deprecated", schema->name);
962 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200963
Michal Vasko1a09b212021-05-06 13:00:10 +0200964 /* check dummy node children/value accessing */
965 if (lysc_data_parent(schema) == node) {
966 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "When condition is accessing its own conditional node children.");
967 ret = LY_EVALID;
968 goto cleanup;
969 } else if ((schema == node) && (tmp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
970 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "When condition is accessing its own conditional node value.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200971 ret = LY_EVALID;
972 goto cleanup;
973 }
974 }
975 }
976
977 /* check cyclic dependencies */
978 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
979 LY_CHECK_GOTO(ret, cleanup);
980
981 lyxp_set_free_content(&tmp_set);
982 }
983
Michal Vaskoc130e162021-10-19 11:30:00 +0200984cleanup:
985 lyxp_set_free_content(&tmp_set);
986 LOG_LOCBACK(1, 0, 0, 0);
987 return ret;
988}
989
990/**
991 * @brief Check must expressions of a node on a complete compiled schema tree.
992 *
993 * @param[in] ctx Compile context.
994 * @param[in] node Node to check.
995 * @param[in] local_mods Sized array of local modules for musts of @p node at the same index.
996 * @param[in,out] unres Global unres structure.
997 * @return LY_ERECOMPILE
998 * @return LY_ERR value
999 */
1000static LY_ERR
1001lys_compile_unres_must(struct lysc_ctx *ctx, const struct lysc_node *node, const struct lysp_module **local_mods,
1002 struct lys_glob_unres *unres)
1003{
1004 struct lyxp_set tmp_set;
1005 uint32_t i, opts;
1006 LY_ARRAY_COUNT_TYPE u;
1007 struct lysc_must *musts = NULL;
1008 LY_ERR ret = LY_SUCCESS;
1009 const struct lys_module *mod;
1010 uint16_t flg;
1011
1012 LOG_LOCSET(node, NULL, NULL, NULL);
1013
1014 memset(&tmp_set, 0, sizeof tmp_set);
1015 opts = LYXP_SCNODE_SCHEMA | ((node->flags & LYS_IS_OUTPUT) ? LYXP_SCNODE_OUTPUT : 0);
1016
1017 musts = lysc_node_musts(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001018 LY_ARRAY_FOR(musts, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +02001019 /* first check whether all the referenced modules are implemented */
Michal Vasko25d6ad02020-10-22 12:20:22 +02001020 mod = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +02001021 ret = lys_compile_expr_implement(ctx->ctx, musts[u].cond, LY_VALUE_SCHEMA_RESOLVED, musts[u].prefixes,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001022 ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, unres, &mod);
Michal Vasko25d6ad02020-10-22 12:20:22 +02001023 if (ret) {
1024 goto cleanup;
1025 } else if (mod) {
Michal Vaskocfaff232020-10-20 09:35:14 +02001026 LOGWRN(ctx->ctx, "Must condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
1027 musts[u].cond->expr, mod->name);
1028 continue;
1029 }
1030
1031 /* check "must" */
Radek Krejci8df109d2021-04-23 12:19:08 +02001032 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 +02001033 node, &tmp_set, opts);
Michal Vasko25d6ad02020-10-22 12:20:22 +02001034 if (ret) {
Michal Vasko7c3134d2022-07-14 10:57:59 +02001035 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid must condition \"%s\".", musts[u].cond->expr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001036 goto cleanup;
1037 }
1038
1039 ctx->path[0] = '\0';
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001040 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001041 for (i = 0; i < tmp_set.used; ++i) {
1042 /* skip roots'n'stuff */
1043 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko7c3134d2022-07-14 10:57:59 +02001044 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
1045
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001046 /* XPath expression cannot reference "lower" status than the node that has the definition */
Michal Vaskoc130e162021-10-19 11:30:00 +02001047 if (local_mods[u]->mod == node->module) {
1048 /* use flags of the context node since the definition is local */
1049 flg = node->flags;
1050 } else {
1051 /* definition is foreign (deviation, refine), always current */
1052 flg = LYS_STATUS_CURR;
1053 }
Michal Vasko7c3134d2022-07-14 10:57:59 +02001054 if (lysc_check_status(NULL, flg, local_mods[u]->mod, node->name, schema->flags, schema->module,
1055 schema->name)) {
1056 LOGWRN(ctx->ctx, "Must condition \"%s\" may be referencing %s node \"%s\".", musts[u].cond->expr,
1057 (schema->flags == LYS_STATUS_OBSLT) ? "obsolete" : "deprecated", schema->name);
1058 break;
1059 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001060 }
1061 }
1062
1063 lyxp_set_free_content(&tmp_set);
1064 }
1065
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001066cleanup:
1067 lyxp_set_free_content(&tmp_set);
Radek Krejciddace2c2021-01-08 11:30:56 +01001068 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001069 return ret;
1070}
1071
1072/**
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001073 * @brief Remove all disabled bits/enums from a sized array.
1074 *
1075 * @param[in] ctx Context with the dictionary.
1076 * @param[in] items Sized array of bits/enums.
1077 */
1078static void
Michal Vaskoc636ea42022-09-16 10:20:31 +02001079lys_compile_unres_disabled_bitenum_remove(struct lysf_ctx *ctx, struct lysc_type_bitenum_item *items)
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001080{
1081 LY_ARRAY_COUNT_TYPE u = 0, last_u;
1082
1083 while (u < LY_ARRAY_COUNT(items)) {
1084 if (items[u].flags & LYS_DISABLED) {
1085 /* free the disabled item */
1086 lysc_enum_item_free(ctx, &items[u]);
1087
1088 /* replace it with the following items */
1089 last_u = LY_ARRAY_COUNT(items) - 1;
1090 if (u < last_u) {
1091 memmove(items + u, items + u + 1, (last_u - u) * sizeof *items);
1092 }
1093
1094 /* one item less */
1095 LY_ARRAY_DECREMENT(items);
1096 continue;
1097 }
1098
1099 ++u;
1100 }
1101}
1102
1103/**
1104 * @brief Find and remove all disabled bits/enums in a leaf/leaf-list type.
1105 *
1106 * @param[in] ctx Compile context.
1107 * @param[in] leaf Leaf/leaf-list to check.
1108 * @return LY_ERR value
1109 */
1110static LY_ERR
1111lys_compile_unres_disabled_bitenum(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf)
1112{
1113 struct lysc_type **t;
1114 LY_ARRAY_COUNT_TYPE u, count;
1115 struct lysc_type_enum *ent;
1116
1117 if (leaf->type->basetype == LY_TYPE_UNION) {
1118 t = ((struct lysc_type_union *)leaf->type)->types;
1119 count = LY_ARRAY_COUNT(t);
1120 } else {
1121 t = &leaf->type;
1122 count = 1;
1123 }
1124 for (u = 0; u < count; ++u) {
1125 if ((t[u]->basetype == LY_TYPE_BITS) || (t[u]->basetype == LY_TYPE_ENUM)) {
1126 /* remove all disabled items */
1127 ent = (struct lysc_type_enum *)(t[u]);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001128 lys_compile_unres_disabled_bitenum_remove(&ctx->free_ctx, ent->enums);
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001129
1130 if (!LY_ARRAY_COUNT(ent->enums)) {
1131 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "%s type of node \"%s\" without any (or all disabled) valid values.",
1132 (ent->basetype == LY_TYPE_BITS) ? "Bits" : "Enumeration", leaf->name);
1133 return LY_EVALID;
1134 }
1135 }
1136 }
1137
1138 return LY_SUCCESS;
1139}
1140
1141/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001142 * @brief Check leafref for its target existence on a complete compiled schema tree.
1143 *
1144 * @param[in] ctx Compile context.
1145 * @param[in] node Context node for the leafref.
1146 * @param[in] lref Leafref to check/resolve.
Michal Vaskoc130e162021-10-19 11:30:00 +02001147 * @param[in] local_mod Local module for the leafref type.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001148 * @param[in,out] unres Global unres structure.
Michal Vasko40c158c2021-04-28 17:01:03 +02001149 * @return LY_ERECOMPILE if context recompilation is needed,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001150 * @return LY_ERR value.
1151 */
1152static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001153lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref,
Michal Vaskoc130e162021-10-19 11:30:00 +02001154 const struct lysp_module *local_mod, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001155{
Michal Vaskod1e53b92021-01-28 13:11:06 +01001156 const struct lysc_node *target = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001157 struct ly_path *p;
1158 struct lysc_type *type;
Michal Vaskoc130e162021-10-19 11:30:00 +02001159 uint16_t flg;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001160
1161 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
1162
Michal Vasko24fc4d12021-07-12 14:41:20 +02001163 /* first implement all the modules in the path */
1164 LY_CHECK_RET(lys_compile_expr_implement(ctx->ctx, lref->path, LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, 1, unres, NULL));
1165
Michal Vaskoed725d72021-06-23 12:03:45 +02001166 /* 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 +02001167 LY_CHECK_RET(ly_path_compile_leafref(ctx->ctx, node, ctx->ext, lref->path,
Michal Vaskod1e53b92021-01-28 13:11:06 +01001168 (node->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02001169 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001170
1171 /* get the target node */
1172 target = p[LY_ARRAY_COUNT(p) - 1].node;
1173 ly_path_free(node->module->ctx, p);
1174
1175 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001176 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 +02001177 lref->path->expr, lys_nodetype2str(target->nodetype));
1178 return LY_EVALID;
1179 }
1180
1181 /* check status */
1182 ctx->path[0] = '\0';
1183 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1184 ctx->path_len = strlen(ctx->path);
Michal Vaskoc130e162021-10-19 11:30:00 +02001185 if (node->module == local_mod->mod) {
1186 /* use flags of the context node since the definition is local */
1187 flg = node->flags;
1188 } else {
1189 /* definition is foreign (deviation), always current */
1190 flg = LYS_STATUS_CURR;
1191 }
1192 if (lysc_check_status(ctx, flg, local_mod->mod, node->name, target->flags, target->module, target->name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001193 return LY_EVALID;
1194 }
1195 ctx->path_len = 1;
1196 ctx->path[1] = '\0';
1197
1198 /* check config */
1199 if (lref->require_instance) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01001200 if ((node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001201 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001202 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
1203 return LY_EVALID;
1204 }
1205 }
1206
Michal Vaskod15c7de2022-05-09 15:34:09 +02001207 /* check for circular chain of leafrefs */
1208 for (type = ((struct lysc_node_leaf *)target)->type;
1209 type && (type->basetype == LY_TYPE_LEAFREF);
1210 type = ((struct lysc_type_leafref *)type)->realtype) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001211 if (type == (struct lysc_type *)lref) {
1212 /* circular chain detected */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001213 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid leafref path \"%s\" - circular chain of leafrefs detected.",
1214 lref->path->expr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001215 return LY_EVALID;
1216 }
1217 }
1218
Michal Vaskod15c7de2022-05-09 15:34:09 +02001219 /* store the type */
1220 lref->realtype = ((struct lysc_node_leaf *)target)->type;
1221 ++lref->realtype->refcount;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001222 return LY_SUCCESS;
1223}
1224
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001225/**
1226 * @brief Compile default value(s) for leaf or leaf-list expecting a complete compiled schema tree.
1227 *
1228 * @param[in] ctx Compile context.
1229 * @param[in] node Leaf or leaf-list to compile the default value(s) for.
1230 * @param[in] type Type of the default value.
1231 * @param[in] dflt Default value.
1232 * @param[in] dflt_pmod Parsed module of the @p dflt to resolve possible prefixes.
1233 * @param[in,out] storage Storage for the compiled default value.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001234 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001235 * @return LY_ERR value.
1236 */
1237static LY_ERR
1238lys_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 +01001239 const struct lysp_module *dflt_pmod, struct lyd_value *storage, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001240{
1241 LY_ERR ret;
Michal Vasko25d6ad02020-10-22 12:20:22 +02001242 uint32_t options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001243 struct ly_err_item *err = NULL;
1244
Radek Krejci0b013302021-03-29 15:22:32 +02001245 options = (ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED) ? LYPLG_TYPE_STORE_IMPLEMENT : 0;
Radek Krejci8df109d2021-04-23 12:19:08 +02001246 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), options, LY_VALUE_SCHEMA, (void *)dflt_pmod,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001247 LYD_HINT_SCHEMA, node, storage, unres, &err);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001248 if (ret == LY_ERECOMPILE) {
1249 /* fine, but we need to recompile */
1250 return LY_ERECOMPILE;
1251 } else if (ret == LY_EINCOMPLETE) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001252 /* we have no data so we will not be resolving it */
1253 ret = LY_SUCCESS;
1254 }
1255
1256 if (ret) {
Radek Krejciddace2c2021-01-08 11:30:56 +01001257 LOG_LOCSET(node, NULL, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001258 if (err) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001259 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid default - value does not fit the type (%s).", err->msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001260 ly_err_free(err);
1261 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001262 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid default - value does not fit the type.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001263 }
Radek Krejciddace2c2021-01-08 11:30:56 +01001264 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001265 return ret;
1266 }
1267
Michal Vasko04338d92021-09-01 07:58:14 +02001268 LY_ATOMIC_INC_BARRIER(((struct lysc_type *)storage->realtype)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001269 return LY_SUCCESS;
1270}
1271
1272/**
1273 * @brief Compile default value of a leaf expecting a complete compiled schema tree.
1274 *
1275 * @param[in] ctx Compile context.
1276 * @param[in] leaf Leaf that the default value is for.
1277 * @param[in] dflt Default value to compile.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001278 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001279 * @return LY_ERR value.
1280 */
1281static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001282lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt,
1283 struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001284{
1285 LY_ERR ret;
1286
1287 assert(!leaf->dflt);
1288
1289 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
1290 /* ignore default values for keys and mandatory leaves */
1291 return LY_SUCCESS;
1292 }
1293
1294 /* allocate the default value */
1295 leaf->dflt = calloc(1, sizeof *leaf->dflt);
1296 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
1297
1298 /* store the default value */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001299 ret = lys_compile_unres_dflt(ctx, &leaf->node, leaf->type, dflt->str, dflt->mod, leaf->dflt, unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001300 if (ret) {
1301 free(leaf->dflt);
1302 leaf->dflt = NULL;
1303 }
1304
1305 return ret;
1306}
1307
1308/**
1309 * @brief Compile default values of a leaf-list expecting a complete compiled schema tree.
1310 *
1311 * @param[in] ctx Compile context.
1312 * @param[in] llist Leaf-list that the default value(s) are for.
1313 * @param[in] dflt Default value to compile, in case of a single value.
1314 * @param[in] dflts Sized array of default values, in case of more values.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001315 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001316 * @return LY_ERR value.
1317 */
1318static LY_ERR
1319lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflt,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001320 struct lysp_qname *dflts, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001321{
1322 LY_ERR ret;
1323 LY_ARRAY_COUNT_TYPE orig_count, u, v;
1324
1325 assert(dflt || dflts);
1326
Radek Krejcic7d13e32020-12-09 12:32:24 +01001327 /* in case there were already some defaults and we are adding new by deviations */
1328 orig_count = LY_ARRAY_COUNT(llist->dflts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001329
1330 /* allocate new items */
Radek Krejcic7d13e32020-12-09 12:32:24 +01001331 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 +02001332
1333 /* fill each new default value */
1334 if (dflts) {
1335 LY_ARRAY_FOR(dflts, u) {
1336 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001337 ret = lys_compile_unres_dflt(ctx, &llist->node, llist->type, dflts[u].str, dflts[u].mod,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001338 llist->dflts[orig_count + u], unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001339 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
1340 LY_ARRAY_INCREMENT(llist->dflts);
1341 }
1342 } else {
1343 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001344 ret = lys_compile_unres_dflt(ctx, &llist->node, llist->type, dflt->str, dflt->mod,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001345 llist->dflts[orig_count], unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001346 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
1347 LY_ARRAY_INCREMENT(llist->dflts);
1348 }
1349
1350 /* check default value uniqueness */
1351 if (llist->flags & LYS_CONFIG_W) {
1352 /* configuration data values must be unique - so check the default values */
1353 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
1354 for (v = 0; v < u; ++v) {
1355 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
Radek Krejcia6016992021-03-03 10:13:41 +01001356 lysc_update_path(ctx, llist->parent ? llist->parent->module : NULL, llist->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001357 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration leaf-list has multiple defaults of the same value \"%s\".",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001358 llist->dflts[u]->realtype->plugin->print(ctx->ctx, llist->dflts[u], LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001359 lysc_update_path(ctx, NULL, NULL);
1360 return LY_EVALID;
1361 }
1362 }
1363 }
1364 }
1365
1366 return LY_SUCCESS;
1367}
1368
Michal Vaskof4258e12021-06-15 12:11:42 +02001369/**
aPiecek732cd142021-07-07 16:29:59 +02001370 * @brief Iteratively get all leafrefs from @p node
1371 * if the node is of type union, otherwise just return the leafref.
1372 *
1373 * @param[in] node Node that may contain the leafref.
1374 * @param[in,out] index Value that is passed between function calls.
1375 * For each new node, initialize value of the @p index to 0, otherwise
1376 * do not modify the value between calls.
1377 * @return Pointer to the leafref or next leafref, otherwise NULL.
1378 */
1379static struct lysc_type_leafref *
1380lys_type_leafref_next(const struct lysc_node *node, uint64_t *index)
1381{
1382 struct lysc_type_leafref *ret = NULL;
1383 struct lysc_type_union *uni;
1384 struct lysc_type *leaf_type;
1385
1386 assert(node->nodetype & LYD_NODE_TERM);
1387
1388 leaf_type = ((struct lysc_node_leaf *)node)->type;
1389 if (leaf_type->basetype == LY_TYPE_UNION) {
1390 uni = (struct lysc_type_union *)leaf_type;
1391
1392 /* find next union leafref */
1393 while (*index < LY_ARRAY_COUNT(uni->types)) {
1394 if (uni->types[*index]->basetype == LY_TYPE_LEAFREF) {
1395 ret = (struct lysc_type_leafref *)uni->types[*index];
1396 ++(*index);
1397 break;
1398 }
1399
1400 ++(*index);
1401 }
1402 } else {
1403 /* return just the single leafref */
1404 if (*index == 0) {
1405 ++(*index);
1406 assert(leaf_type->basetype == LY_TYPE_LEAFREF);
1407 ret = (struct lysc_type_leafref *)leaf_type;
1408 }
1409 }
1410
1411 return ret;
1412}
1413
1414/**
Michal Vaskof4258e12021-06-15 12:11:42 +02001415 * @brief Finish dependency set compilation by resolving all the unres sets.
1416 *
1417 * @param[in] ctx libyang context.
1418 * @param[in] unres Global unres structure with the sets to resolve.
1419 * @return LY_SUCCESS on success.
1420 * @return LY_ERECOMPILE if the dep set needs to be recompiled.
1421 * @return LY_ERR value on error.
1422 */
1423static LY_ERR
1424lys_compile_unres_depset(struct ly_ctx *ctx, struct lys_glob_unres *unres)
Michal Vasko405cc9e2020-12-01 12:01:27 +01001425{
aPiecek732cd142021-07-07 16:29:59 +02001426 LY_ERR ret = LY_SUCCESS;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001427 struct lysc_node *node;
aPiecek732cd142021-07-07 16:29:59 +02001428 struct lysc_type *typeiter;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001429 struct lysc_type_leafref *lref;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001430 struct lysc_ctx cctx = {0};
1431 struct lys_depset_unres *ds_unres = &unres->ds_unres;
aPiecekd7bd52d2021-07-08 08:59:59 +02001432 struct ly_path *path;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001433 LY_ARRAY_COUNT_TYPE v;
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001434 struct lysc_unres_leafref *l;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001435 struct lysc_unres_must *m;
1436 struct lysc_unres_dflt *r;
aPiecekd7bd52d2021-07-08 08:59:59 +02001437 uint32_t i, processed_leafrefs = 0;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001438
aPiecekd7bd52d2021-07-08 08:59:59 +02001439resolve_all:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001440 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
1441 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
aPiecekc6526b42021-07-12 15:21:39 +02001442 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation.
1443 * Also do the same check for set of the disabled leafrefs, but without the second round. */
1444 while (ds_unres->disabled_leafrefs.count) {
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001445 /* remember index, it can change before we get to free this item */
1446 i = ds_unres->disabled_leafrefs.count - 1;
1447 l = ds_unres->disabled_leafrefs.objs[i];
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001448 LYSC_CTX_INIT_PMOD(cctx, l->node->module->parsed, l->ext);
aPiecekc6526b42021-07-12 15:21:39 +02001449
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001450 LOG_LOCSET(l->node, NULL, NULL, NULL);
aPiecekc6526b42021-07-12 15:21:39 +02001451 v = 0;
Michal Vaskoc130e162021-10-19 11:30:00 +02001452 while ((ret == LY_SUCCESS) && (lref = lys_type_leafref_next(l->node, &v))) {
1453 ret = lys_compile_unres_leafref(&cctx, l->node, lref, l->local_mod, unres);
aPiecekc6526b42021-07-12 15:21:39 +02001454 }
aPiecekc6526b42021-07-12 15:21:39 +02001455 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001456 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001457
1458 ly_set_rm_index(&ds_unres->disabled_leafrefs, i, free);
aPiecekc6526b42021-07-12 15:21:39 +02001459 }
Michal Vaskoa23e1d92021-07-13 12:04:16 +02001460
aPiecekd7bd52d2021-07-08 08:59:59 +02001461 for (i = processed_leafrefs; i < ds_unres->leafrefs.count; ++i) {
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001462 l = ds_unres->leafrefs.objs[i];
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001463 LYSC_CTX_INIT_PMOD(cctx, l->node->module->parsed, l->ext);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001464
Michal Vaskoc636ea42022-09-16 10:20:31 +02001465 LOG_LOCSET(l->node, NULL, NULL, NULL);
aPiecek732cd142021-07-07 16:29:59 +02001466 v = 0;
Michal Vaskoc130e162021-10-19 11:30:00 +02001467 while ((ret == LY_SUCCESS) && (lref = lys_type_leafref_next(l->node, &v))) {
1468 ret = lys_compile_unres_leafref(&cctx, l->node, lref, l->local_mod, unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001469 }
Radek Krejciddace2c2021-01-08 11:30:56 +01001470 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001471 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001472 }
aPiecekd7bd52d2021-07-08 08:59:59 +02001473 for (i = processed_leafrefs; i < ds_unres->leafrefs.count; ++i) {
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001474 l = ds_unres->leafrefs.objs[i];
Radek Krejci2efc45b2020-12-22 16:25:44 +01001475
Michal Vasko405cc9e2020-12-01 12:01:27 +01001476 /* store pointer to the real type */
aPiecek732cd142021-07-07 16:29:59 +02001477 v = 0;
Michal Vaskoc130e162021-10-19 11:30:00 +02001478 while ((lref = lys_type_leafref_next(l->node, &v))) {
aPiecek732cd142021-07-07 16:29:59 +02001479 for (typeiter = lref->realtype;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001480 typeiter->basetype == LY_TYPE_LEAFREF;
1481 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
Michal Vaskod15c7de2022-05-09 15:34:09 +02001482
Michal Vaskoc636ea42022-09-16 10:20:31 +02001483 lysc_type_free(&cctx.free_ctx, lref->realtype);
aPiecek732cd142021-07-07 16:29:59 +02001484 lref->realtype = typeiter;
Michal Vaskod15c7de2022-05-09 15:34:09 +02001485 ++lref->realtype->refcount;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001486 }
1487
Michal Vaskoa23e1d92021-07-13 12:04:16 +02001488 /* If 'goto' will be used on the 'resolve_all' label, then
aPiecekd7bd52d2021-07-08 08:59:59 +02001489 * the current leafref will not be processed again.
1490 */
1491 processed_leafrefs++;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001492 }
1493
Michal Vaskoc130e162021-10-19 11:30:00 +02001494 /* check when */
1495 while (ds_unres->whens.count) {
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001496 i = ds_unres->whens.count - 1;
1497 node = ds_unres->whens.objs[i];
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001498 LYSC_CTX_INIT_PMOD(cctx, node->module->parsed, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001499
Radek Krejciddace2c2021-01-08 11:30:56 +01001500 LOG_LOCSET(node, NULL, NULL, NULL);
Michal Vaskoc130e162021-10-19 11:30:00 +02001501 ret = lys_compile_unres_when(&cctx, node, unres);
Radek Krejciddace2c2021-01-08 11:30:56 +01001502 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001503 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001504
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001505 ly_set_rm_index(&ds_unres->whens, i, NULL);
Michal Vaskoc130e162021-10-19 11:30:00 +02001506 }
1507
1508 /* check must */
1509 while (ds_unres->musts.count) {
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001510 i = ds_unres->musts.count - 1;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001511 m = ds_unres->musts.objs[i];
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001512 LYSC_CTX_INIT_PMOD(cctx, m->node->module->parsed, m->ext);
Michal Vaskoc130e162021-10-19 11:30:00 +02001513
1514 LOG_LOCSET(m->node, NULL, NULL, NULL);
1515 ret = lys_compile_unres_must(&cctx, m->node, m->local_mods, unres);
1516 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001517 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoc130e162021-10-19 11:30:00 +02001518
1519 lysc_unres_must_free(m);
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001520 ly_set_rm_index(&ds_unres->musts, i, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001521 }
1522
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001523 /* remove disabled enums/bits */
Michal Vasko5eaf7322021-11-22 15:58:26 +01001524 while (ds_unres->disabled_bitenums.count) {
1525 i = ds_unres->disabled_bitenums.count - 1;
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001526 node = ds_unres->disabled_bitenums.objs[i];
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001527 LYSC_CTX_INIT_PMOD(cctx, node->module->parsed, NULL);
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001528
1529 LOG_LOCSET(node, NULL, NULL, NULL);
1530 ret = lys_compile_unres_disabled_bitenum(&cctx, (struct lysc_node_leaf *)node);
1531 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001532 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001533
1534 ly_set_rm_index(&ds_unres->disabled_bitenums, i, NULL);
1535 }
1536
Michal Vasko405cc9e2020-12-01 12:01:27 +01001537 /* finish incomplete default values compilation */
Michal Vaskof4258e12021-06-15 12:11:42 +02001538 while (ds_unres->dflts.count) {
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001539 i = ds_unres->dflts.count - 1;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001540 r = ds_unres->dflts.objs[i];
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001541 LYSC_CTX_INIT_PMOD(cctx, r->leaf->module->parsed, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001542
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001543 LOG_LOCSET(&r->leaf->node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001544 if (r->leaf->nodetype == LYS_LEAF) {
1545 ret = lys_compile_unres_leaf_dlft(&cctx, r->leaf, r->dflt, unres);
1546 } else {
1547 ret = lys_compile_unres_llist_dflts(&cctx, r->llist, r->dflt, r->dflts, unres);
1548 }
Radek Krejciddace2c2021-01-08 11:30:56 +01001549 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001550 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001551
1552 lysc_unres_dflt_free(ctx, r);
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001553 ly_set_rm_index(&ds_unres->dflts, i, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001554 }
1555
Michal Vasko40c158c2021-04-28 17:01:03 +02001556 /* some unres items may have been added */
aPiecekc6526b42021-07-12 15:21:39 +02001557 if ((processed_leafrefs != ds_unres->leafrefs.count) || ds_unres->disabled_leafrefs.count ||
Michal Vaskoc130e162021-10-19 11:30:00 +02001558 ds_unres->whens.count || ds_unres->musts.count || ds_unres->dflts.count) {
aPiecekd7bd52d2021-07-08 08:59:59 +02001559 goto resolve_all;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001560 }
1561
Michal Vasko30ab8e72021-04-19 12:47:52 +02001562 /* finally, remove all disabled nodes */
Michal Vaskof4258e12021-06-15 12:11:42 +02001563 for (i = 0; i < ds_unres->disabled.count; ++i) {
1564 node = ds_unres->disabled.snodes[i];
Michal Vasko30ab8e72021-04-19 12:47:52 +02001565 if (node->flags & LYS_KEY) {
1566 LOG_LOCSET(node, NULL, NULL, NULL);
Michal Vaskoe02e7402021-07-23 08:29:28 +02001567 LOGVAL(ctx, LYVE_REFERENCE, "Key \"%s\" is disabled.", node->name);
Michal Vasko30ab8e72021-04-19 12:47:52 +02001568 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001569 ret = LY_EVALID;
1570 goto cleanup;
Michal Vasko30ab8e72021-04-19 12:47:52 +02001571 }
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001572 LYSC_CTX_INIT_PMOD(cctx, node->module->parsed, NULL);
Michal Vasko30ab8e72021-04-19 12:47:52 +02001573
Michal Vaskoc636ea42022-09-16 10:20:31 +02001574 lysc_node_free(&cctx.free_ctx, node, 1);
Michal Vasko30ab8e72021-04-19 12:47:52 +02001575 }
1576
aPiecekd7bd52d2021-07-08 08:59:59 +02001577 /* also check if the leafref target has not been disabled */
1578 for (i = 0; i < ds_unres->leafrefs.count; ++i) {
Michal Vaskoa91d87e2021-10-19 11:58:57 +02001579 l = ds_unres->leafrefs.objs[i];
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001580 LYSC_CTX_INIT_PMOD(cctx, l->node->module->parsed, l->ext);
aPiecekd7bd52d2021-07-08 08:59:59 +02001581
1582 v = 0;
Michal Vaskoc130e162021-10-19 11:30:00 +02001583 while ((lref = lys_type_leafref_next(l->node, &v))) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001584 ret = ly_path_compile_leafref(cctx.ctx, l->node, cctx.ext, lref->path,
Michal Vaskoc130e162021-10-19 11:30:00 +02001585 (l->node->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
aPiecekd7bd52d2021-07-08 08:59:59 +02001586 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &path);
Michal Vaskoc130e162021-10-19 11:30:00 +02001587 ly_path_free(l->node->module->ctx, path);
aPiecekd7bd52d2021-07-08 08:59:59 +02001588
1589 assert(ret != LY_ERECOMPILE);
1590 if (ret) {
Michal Vaskoc130e162021-10-19 11:30:00 +02001591 LOG_LOCSET(l->node, NULL, NULL, NULL);
Michal Vaskoe02e7402021-07-23 08:29:28 +02001592 LOGVAL(ctx, LYVE_REFERENCE, "Target of leafref \"%s\" cannot be referenced because it is disabled.",
Michal Vaskoc130e162021-10-19 11:30:00 +02001593 l->node->name);
aPiecekd7bd52d2021-07-08 08:59:59 +02001594 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001595 ret = LY_EVALID;
1596 goto cleanup;
aPiecekd7bd52d2021-07-08 08:59:59 +02001597 }
1598 }
1599 }
1600
Michal Vaskoc636ea42022-09-16 10:20:31 +02001601cleanup:
1602 lysf_ctx_erase(&cctx.free_ctx);
1603 return ret;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001604}
1605
Michal Vaskof4258e12021-06-15 12:11:42 +02001606/**
1607 * @brief Erase dep set unres.
1608 *
1609 * @param[in] ctx libyang context.
1610 * @param[in] unres Global unres structure with the sets to resolve.
1611 */
1612static void
1613lys_compile_unres_depset_erase(const struct ly_ctx *ctx, struct lys_glob_unres *unres)
Michal Vasko405cc9e2020-12-01 12:01:27 +01001614{
1615 uint32_t i;
1616
Michal Vaskoc130e162021-10-19 11:30:00 +02001617 ly_set_erase(&unres->ds_unres.whens, NULL);
1618 for (i = 0; i < unres->ds_unres.musts.count; ++i) {
1619 lysc_unres_must_free(unres->ds_unres.musts.objs[i]);
1620 }
1621 ly_set_erase(&unres->ds_unres.musts, NULL);
1622 ly_set_erase(&unres->ds_unres.leafrefs, free);
Michal Vaskof4258e12021-06-15 12:11:42 +02001623 for (i = 0; i < unres->ds_unres.dflts.count; ++i) {
1624 lysc_unres_dflt_free(ctx, unres->ds_unres.dflts.objs[i]);
Michal Vaskodf6319c2021-06-10 14:41:05 +02001625 }
Michal Vaskof4258e12021-06-15 12:11:42 +02001626 ly_set_erase(&unres->ds_unres.dflts, NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +02001627 ly_set_erase(&unres->ds_unres.disabled, NULL);
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001628 ly_set_erase(&unres->ds_unres.disabled_leafrefs, free);
1629 ly_set_erase(&unres->ds_unres.disabled_bitenums, NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +02001630}
1631
Michal Vasko709f9a52021-07-21 10:51:59 +02001632/**
1633 * @brief Compile all flagged modules in a dependency set, recursively if recompilation is needed.
1634 *
1635 * @param[in] ctx libyang context.
1636 * @param[in] dep_set Dependency set to compile.
1637 * @param[in,out] unres Global unres to use.
1638 * @return LY_ERR value.
1639 */
1640static LY_ERR
1641lys_compile_depset_r(struct ly_ctx *ctx, struct ly_set *dep_set, struct lys_glob_unres *unres)
Michal Vaskof4258e12021-06-15 12:11:42 +02001642{
1643 LY_ERR ret = LY_SUCCESS;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001644 struct lysf_ctx fctx = {.ctx = ctx};
Michal Vaskof4258e12021-06-15 12:11:42 +02001645 struct lys_module *mod;
1646 uint32_t i;
1647
1648 for (i = 0; i < dep_set->count; ++i) {
1649 mod = dep_set->objs[i];
1650 if (!mod->to_compile) {
1651 /* skip */
1652 continue;
1653 }
1654 assert(mod->implemented);
1655
1656 /* free the compiled module, if any */
Michal Vaskoc636ea42022-09-16 10:20:31 +02001657 lysc_module_free(&fctx, mod->compiled);
Michal Vaskof4258e12021-06-15 12:11:42 +02001658 mod->compiled = NULL;
1659
1660 /* (re)compile the module */
1661 LY_CHECK_GOTO(ret = lys_compile(mod, &unres->ds_unres), cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001662 }
Michal Vaskof4258e12021-06-15 12:11:42 +02001663
1664 /* resolve dep set unres */
1665 ret = lys_compile_unres_depset(ctx, unres);
1666 if (ret == LY_ERECOMPILE) {
1667 /* new module is implemented, discard current dep set unres and recompile the whole dep set */
1668 lys_compile_unres_depset_erase(ctx, unres);
Michal Vasko709f9a52021-07-21 10:51:59 +02001669 return lys_compile_depset_r(ctx, dep_set, unres);
Michal Vaskof4258e12021-06-15 12:11:42 +02001670 } else if (ret) {
1671 /* error */
1672 goto cleanup;
1673 }
1674
1675 /* success, unset the flags of all the modules in the dep set */
1676 for (i = 0; i < dep_set->count; ++i) {
1677 mod = dep_set->objs[i];
1678 mod->to_compile = 0;
1679 }
1680
1681cleanup:
1682 lys_compile_unres_depset_erase(ctx, unres);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001683 lysf_ctx_erase(&fctx);
Michal Vaskof4258e12021-06-15 12:11:42 +02001684 return ret;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001685}
1686
Michal Vasko6a4ef772022-02-08 15:07:44 +01001687/**
1688 * @brief Check if-feature of all features of all modules in a dep set.
1689 *
1690 * @param[in] dep_set Dep set to check.
1691 * @return LY_ERR value.
1692 */
1693static LY_ERR
1694lys_compile_depset_check_features(struct ly_set *dep_set)
1695{
1696 struct lys_module *mod;
1697 uint32_t i;
1698
1699 for (i = 0; i < dep_set->count; ++i) {
1700 mod = dep_set->objs[i];
1701 if (!mod->to_compile) {
1702 /* skip */
1703 continue;
1704 }
1705
1706 /* check features of this module */
1707 LY_CHECK_RET(lys_check_features(mod->parsed));
1708 }
1709
1710 return LY_SUCCESS;
1711}
1712
Michal Vasko709f9a52021-07-21 10:51:59 +02001713LY_ERR
1714lys_compile_depset_all(struct ly_ctx *ctx, struct lys_glob_unres *unres)
1715{
1716 uint32_t i;
1717
1718 for (i = 0; i < unres->dep_sets.count; ++i) {
Michal Vasko6a4ef772022-02-08 15:07:44 +01001719 LY_CHECK_RET(lys_compile_depset_check_features(unres->dep_sets.objs[i]));
Michal Vasko709f9a52021-07-21 10:51:59 +02001720 LY_CHECK_RET(lys_compile_depset_r(ctx, unres->dep_sets.objs[i], unres));
1721 }
1722
1723 return LY_SUCCESS;
1724}
1725
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001726/**
Michal Vasko405cc9e2020-12-01 12:01:27 +01001727 * @brief Finish compilation of all the module unres sets in a compile context.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001728 *
1729 * @param[in] ctx Compile context with unres sets.
1730 * @return LY_ERR value.
1731 */
1732static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001733lys_compile_unres_mod(struct lysc_ctx *ctx)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001734{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001735 struct lysc_augment *aug;
1736 struct lysc_deviation *dev;
Michal Vasko1ade6f12021-04-19 11:32:45 +02001737 struct lys_module *orig_mod = ctx->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001738 uint32_t i;
1739
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001740 /* check that all augments were applied */
1741 for (i = 0; i < ctx->augs.count; ++i) {
1742 aug = ctx->augs.objs[i];
Michal Vasko1ade6f12021-04-19 11:32:45 +02001743 ctx->cur_mod = aug->aug_pmod->mod;
Radek Krejci2efc45b2020-12-22 16:25:44 +01001744 lysc_update_path(ctx, NULL, "{augment}");
1745 lysc_update_path(ctx, NULL, aug->nodeid->expr);
1746 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" from module \"%s\" was not found.",
Michal Vasko28fe5f62021-03-03 16:37:39 +01001747 aug->nodeid->expr, LYSP_MODULE_NAME(aug->aug_pmod));
Michal Vasko1ade6f12021-04-19 11:32:45 +02001748 ctx->cur_mod = orig_mod;
Radek Krejci2efc45b2020-12-22 16:25:44 +01001749 lysc_update_path(ctx, NULL, NULL);
1750 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001751 }
1752 if (ctx->augs.count) {
1753 return LY_ENOTFOUND;
1754 }
1755
1756 /* check that all deviations were applied */
1757 for (i = 0; i < ctx->devs.count; ++i) {
1758 dev = ctx->devs.objs[i];
Radek Krejci2efc45b2020-12-22 16:25:44 +01001759 lysc_update_path(ctx, NULL, "{deviation}");
1760 lysc_update_path(ctx, NULL, dev->nodeid->expr);
1761 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Deviation(s) target node \"%s\" from module \"%s\" was not found.",
1762 dev->nodeid->expr, LYSP_MODULE_NAME(dev->dev_pmods[0]));
1763 lysc_update_path(ctx, NULL, NULL);
1764 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001765 }
1766 if (ctx->devs.count) {
1767 return LY_ENOTFOUND;
1768 }
1769
1770 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001771}
1772
1773/**
Michal Vasko405cc9e2020-12-01 12:01:27 +01001774 * @brief Erase all the module unres sets in a compile context.
1775 *
1776 * @param[in] ctx Compile context with unres sets.
1777 * @param[in] error Whether the compilation finished with an error or not.
1778 */
1779static void
1780lys_compile_unres_mod_erase(struct lysc_ctx *ctx, ly_bool error)
1781{
1782 uint32_t i;
1783
1784 ly_set_erase(&ctx->groupings, NULL);
1785 ly_set_erase(&ctx->tpdf_chain, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001786
1787 if (!error) {
1788 /* there can be no leftover deviations or augments */
1789 LY_CHECK_ERR_RET(ctx->augs.count, LOGINT(ctx->ctx), );
1790 LY_CHECK_ERR_RET(ctx->devs.count, LOGINT(ctx->ctx), );
1791
1792 ly_set_erase(&ctx->augs, NULL);
1793 ly_set_erase(&ctx->devs, NULL);
1794 ly_set_erase(&ctx->uses_augs, NULL);
1795 ly_set_erase(&ctx->uses_rfns, NULL);
1796 } else {
1797 for (i = 0; i < ctx->augs.count; ++i) {
1798 lysc_augment_free(ctx->ctx, ctx->augs.objs[i]);
1799 }
1800 ly_set_erase(&ctx->augs, NULL);
1801 for (i = 0; i < ctx->devs.count; ++i) {
1802 lysc_deviation_free(ctx->ctx, ctx->devs.objs[i]);
1803 }
1804 ly_set_erase(&ctx->devs, NULL);
1805 for (i = 0; i < ctx->uses_augs.count; ++i) {
1806 lysc_augment_free(ctx->ctx, ctx->uses_augs.objs[i]);
1807 }
1808 ly_set_erase(&ctx->uses_augs, NULL);
1809 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1810 lysc_refine_free(ctx->ctx, ctx->uses_rfns.objs[i]);
1811 }
1812 ly_set_erase(&ctx->uses_rfns, NULL);
1813 }
1814}
1815
Michal Vaskod39bea42021-06-14 10:42:49 +02001816LY_ERR
Michal Vaskof4258e12021-06-15 12:11:42 +02001817lys_compile(struct lys_module *mod, struct lys_depset_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001818{
Michal Vaskoc636ea42022-09-16 10:20:31 +02001819 struct lysc_ctx ctx;
Michal Vasko0b395e12021-04-23 13:43:07 +02001820 struct lysc_module *mod_c = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001821 struct lysp_module *sp;
1822 struct lysp_submodule *submod;
1823 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001824 struct lysp_node_grp *grp;
1825 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001826 LY_ERR ret = LY_SUCCESS;
1827
1828 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
1829
Michal Vaskof4258e12021-06-15 12:11:42 +02001830 assert(mod->implemented && mod->to_compile);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001831
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001832 sp = mod->parsed;
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001833 LYSC_CTX_INIT_PMOD(ctx, sp, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001834 ctx.unres = unres;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001835
Michal Vasko0b395e12021-04-23 13:43:07 +02001836 ++mod->ctx->change_count;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001837 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
1838 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
1839 mod_c->mod = mod;
1840
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001841 /* compile augments and deviations of our module from other modules so they can be applied during compilation */
Michal Vasko1ccbf542021-04-19 11:35:00 +02001842 LY_CHECK_GOTO(ret = lys_precompile_own_augments(&ctx), cleanup);
1843 LY_CHECK_GOTO(ret = lys_precompile_own_deviations(&ctx), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001844
1845 /* data nodes */
1846 LY_LIST_FOR(sp->data, pnode) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001847 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001848 }
1849
Radek Krejci2a9fc652021-01-22 17:44:34 +01001850 /* top-level RPCs */
1851 LY_LIST_FOR((struct lysp_node *)sp->rpcs, pnode) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001852 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001853 }
1854
1855 /* top-level notifications */
1856 LY_LIST_FOR((struct lysp_node *)sp->notifs, pnode) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001857 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001858 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001859
1860 /* extension instances */
Michal Vasko1ccbf542021-04-19 11:35:00 +02001861 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001862
1863 /* the same for submodules */
1864 LY_ARRAY_FOR(sp->includes, u) {
1865 submod = sp->includes[u].submodule;
1866 ctx.pmod = (struct lysp_module *)submod;
1867
1868 LY_LIST_FOR(submod->data, pnode) {
1869 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001870 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001871 }
1872
Radek Krejci2a9fc652021-01-22 17:44:34 +01001873 LY_LIST_FOR((struct lysp_node *)submod->rpcs, pnode) {
1874 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001875 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001876 }
1877
1878 LY_LIST_FOR((struct lysp_node *)submod->notifs, pnode) {
1879 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001880 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001881 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001882
Michal Vasko1ccbf542021-04-19 11:35:00 +02001883 COMPILE_EXTS_GOTO(&ctx, submod->exts, mod_c->exts, mod_c, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001884 }
Michal Vasko12606ee2020-11-25 17:05:11 +01001885 ctx.pmod = sp;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001886
1887 /* validate non-instantiated groupings from the parsed schema,
1888 * without it we would accept even the schemas with invalid grouping specification */
Michal Vasko7c565922021-06-10 14:58:27 +02001889 ctx.compile_opts |= LYS_COMPILE_GROUPING;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001890 LY_LIST_FOR(sp->groupings, grp) {
1891 if (!(grp->flags & LYS_USED_GRP)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001892 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, grp), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001893 }
1894 }
1895 LY_LIST_FOR(sp->data, pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001896 LY_LIST_FOR((struct lysp_node_grp *)lysp_node_groupings(pnode), grp) {
1897 if (!(grp->flags & LYS_USED_GRP)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001898 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, grp), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001899 }
1900 }
1901 }
1902 LY_ARRAY_FOR(sp->includes, u) {
1903 submod = sp->includes[u].submodule;
1904 ctx.pmod = (struct lysp_module *)submod;
1905
Radek Krejci2a9fc652021-01-22 17:44:34 +01001906 LY_LIST_FOR(submod->groupings, grp) {
1907 if (!(grp->flags & LYS_USED_GRP)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001908 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, grp), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001909 }
1910 }
1911 LY_LIST_FOR(submod->data, pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001912 LY_LIST_FOR((struct lysp_node_grp *)lysp_node_groupings(pnode), grp) {
1913 if (!(grp->flags & LYS_USED_GRP)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001914 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, grp), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001915 }
1916 }
1917 }
1918 }
1919 ctx.pmod = sp;
1920
Radek Krejciddace2c2021-01-08 11:30:56 +01001921 LOG_LOCBACK(0, 0, 1, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001922
Michal Vasko405cc9e2020-12-01 12:01:27 +01001923 /* finish compilation for all unresolved module items in the context */
Michal Vasko1ccbf542021-04-19 11:35:00 +02001924 LY_CHECK_GOTO(ret = lys_compile_unres_mod(&ctx), cleanup);
Michal Vasko12606ee2020-11-25 17:05:11 +01001925
Michal Vasko1ccbf542021-04-19 11:35:00 +02001926cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01001927 LOG_LOCBACK(0, 0, 1, 0);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001928 lys_compile_unres_mod_erase(&ctx, ret);
1929 if (ret) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001930 lysc_module_free(&ctx.free_ctx, mod_c);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001931 mod->compiled = NULL;
1932 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001933 return ret;
1934}
Michal Vasko65333882021-06-10 14:12:16 +02001935
aPiecek6b3d5422021-07-30 15:55:43 +02001936LY_ERR
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001937lys_compile_identities(struct lys_module *mod)
1938{
Michal Vasko776ae742022-08-04 11:08:21 +02001939 LY_ERR rc = LY_SUCCESS;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001940 struct lysc_ctx ctx;
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001941 struct lysp_submodule *submod;
1942 LY_ARRAY_COUNT_TYPE u;
1943
aPiecek6b3d5422021-07-30 15:55:43 +02001944 /* pre-compile identities of the module and any submodules */
Michal Vasko776ae742022-08-04 11:08:21 +02001945 rc = lys_identity_precompile(NULL, mod->ctx, mod->parsed, mod->parsed->identities, &mod->identities);
1946 LY_CHECK_GOTO(rc, cleanup);
aPiecek6b3d5422021-07-30 15:55:43 +02001947 LY_ARRAY_FOR(mod->parsed->includes, u) {
1948 submod = mod->parsed->includes[u].submodule;
Michal Vasko776ae742022-08-04 11:08:21 +02001949 rc = lys_identity_precompile(NULL, mod->ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
1950 LY_CHECK_GOTO(rc, cleanup);
aPiecek6b3d5422021-07-30 15:55:43 +02001951 }
1952
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001953 /* prepare context */
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001954 LYSC_CTX_INIT_PMOD(ctx, mod->parsed, NULL);
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001955
1956 if (mod->parsed->identities) {
Michal Vasko776ae742022-08-04 11:08:21 +02001957 rc = lys_compile_identities_derived(&ctx, mod->parsed->identities, &mod->identities);
1958 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001959 }
1960 lysc_update_path(&ctx, NULL, "{submodule}");
1961 LY_ARRAY_FOR(mod->parsed->includes, u) {
1962 submod = mod->parsed->includes[u].submodule;
1963 if (submod->identities) {
1964 ctx.pmod = (struct lysp_module *)submod;
1965 lysc_update_path(&ctx, NULL, submod->name);
Michal Vasko776ae742022-08-04 11:08:21 +02001966 rc = lys_compile_identities_derived(&ctx, submod->identities, &mod->identities);
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001967 lysc_update_path(&ctx, NULL, NULL);
1968 }
Michal Vasko776ae742022-08-04 11:08:21 +02001969
1970 if (rc) {
1971 break;
1972 }
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001973 }
1974 lysc_update_path(&ctx, NULL, NULL);
1975
Michal Vasko776ae742022-08-04 11:08:21 +02001976cleanup:
1977 /* always needed when using lysc_update_path() */
1978 LOG_LOCBACK(0, 0, 1, 0);
1979 return rc;
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001980}
1981
1982/**
Michal Vasko65333882021-06-10 14:12:16 +02001983 * @brief Check whether a module does not have any (recursive) compiled import.
1984 *
1985 * @param[in] mod Module to examine.
1986 * @return LY_SUCCESS on success.
Michal Vaskof4258e12021-06-15 12:11:42 +02001987 * @return LY_ERECOMPILE on required recompilation of the dep set.
Michal Vasko65333882021-06-10 14:12:16 +02001988 * @return LY_ERR on error.
1989 */
1990static LY_ERR
1991lys_has_compiled_import_r(struct lys_module *mod)
1992{
1993 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoe7927122021-06-15 12:00:04 +02001994 struct lys_module *m;
Michal Vasko65333882021-06-10 14:12:16 +02001995
1996 LY_ARRAY_FOR(mod->parsed->imports, u) {
Michal Vaskoe7927122021-06-15 12:00:04 +02001997 m = mod->parsed->imports[u].module;
1998 if (!m->implemented) {
Michal Vasko65333882021-06-10 14:12:16 +02001999 continue;
2000 }
2001
Michal Vaskoe7927122021-06-15 12:00:04 +02002002 if (!m->to_compile) {
Michal Vaskod39bea42021-06-14 10:42:49 +02002003 /* module was not/will not be compiled in this compilation (so disabled nodes are not present) */
Michal Vaskof4258e12021-06-15 12:11:42 +02002004 m->to_compile = 1;
Michal Vasko65333882021-06-10 14:12:16 +02002005 return LY_ERECOMPILE;
2006 }
2007
2008 /* recursive */
Michal Vaskoe7927122021-06-15 12:00:04 +02002009 LY_CHECK_RET(lys_has_compiled_import_r(m));
Michal Vasko65333882021-06-10 14:12:16 +02002010 }
2011
2012 return LY_SUCCESS;
2013}
2014
2015LY_ERR
2016lys_implement(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
2017{
2018 LY_ERR ret;
2019 struct lys_module *m;
2020
2021 assert(!mod->implemented);
2022
Michal Vasko978532b2022-01-21 14:13:40 +01002023 /* check collision with other implemented revision */
Michal Vasko65333882021-06-10 14:12:16 +02002024 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
2025 if (m) {
2026 assert(m != mod);
Michal Vasko978532b2022-01-21 14:13:40 +01002027 if (!strcmp(mod->name, "yang") && (strcmp(m->revision, mod->revision) > 0)) {
2028 /* special case for newer internal module, continue */
2029 LOGVRB("Internal module \"%s@%s\" is already implemented in revision \"%s\", using it instead.",
2030 mod->name, mod->revision ? mod->revision : "<none>", m->revision ? m->revision : "<none>");
2031 } else {
2032 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s@%s\" is already implemented in revision \"%s\".",
2033 mod->name, mod->revision ? mod->revision : "<none>", m->revision ? m->revision : "<none>");
2034 return LY_EDENIED;
2035 }
Michal Vasko65333882021-06-10 14:12:16 +02002036 }
2037
2038 /* set features */
2039 ret = lys_set_features(mod->parsed, features);
2040 if (ret && (ret != LY_EEXIST)) {
2041 return ret;
2042 }
2043
Michal Vasko65333882021-06-10 14:12:16 +02002044 /*
2045 * mark the module implemented, which means
2046 * 1) to (re)compile it only ::lys_compile() call is needed
2047 * 2) its compilation will never cause new modules to be implemented (::lys_compile() does not return ::LY_ERECOMPILE)
2048 * but there can be some unres items added that do
2049 */
2050 mod->implemented = 1;
2051
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002052 /* this module is compiled in this compilation */
2053 mod->to_compile = 1;
2054
Michal Vasko65333882021-06-10 14:12:16 +02002055 /* add the module into newly implemented module set */
2056 LY_CHECK_RET(ly_set_add(&unres->implementing, mod, 1, NULL));
2057
Michal Vaskoa9f807e2021-06-15 12:07:16 +02002058 /* mark target modules with our augments and deviations */
2059 LY_CHECK_RET(lys_precompile_augments_deviations(mod, unres));
Michal Vasko65333882021-06-10 14:12:16 +02002060
Michal Vaskod39bea42021-06-14 10:42:49 +02002061 /* check whether this module may reference any modules compiled previously */
2062 LY_CHECK_RET(lys_has_compiled_import_r(mod));
Michal Vasko65333882021-06-10 14:12:16 +02002063
Michal Vasko65333882021-06-10 14:12:16 +02002064 return LY_SUCCESS;
2065}