blob: 8be266d82f566580bf7897e8b2a0e7ac9fe74911 [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 Vasko19a09022021-06-15 11:54:08 +02007 * Copyright (c) 2015 - 2021 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"
47#include "tree_schema_internal.h"
48#include "xpath.h"
49
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020050/**
51 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
52 */
53static LY_ERR
54lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
55{
56 LY_ERR ret = LY_SUCCESS;
57
Radek Krejci720d2612021-03-03 19:44:22 +010058 if (ext_p->compiled && (ext_p->compiled->refcount == 1)) {
59 /* context recompilation - all the extension instances were previously freed (the last link to the compiled extension
60 * remains from the parsed extension definition) and now we are recompiling them again, to have the up-to-date
61 * extension definition, we have to recompile it as well now */
62 lysc_extension_free(ctx->ctx, &ext_p->compiled);
63 }
64
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020065 if (!ext_p->compiled) {
66 lysc_update_path(ctx, NULL, "{extension}");
67 lysc_update_path(ctx, NULL, ext_p->name);
68
69 /* compile the extension definition */
70 ext_p->compiled = calloc(1, sizeof **ext);
71 ext_p->compiled->refcount = 1;
72 DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done);
Radek Krejci9f87b0c2021-03-05 14:45:26 +010073 DUP_STRING_GOTO(ctx->ctx, ext_p->argname, ext_p->compiled->argname, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020074 ext_p->compiled->module = (struct lys_module *)ext_mod;
Radek Krejciab430862021-03-02 20:13:40 +010075 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020076
77 lysc_update_path(ctx, NULL, NULL);
78 lysc_update_path(ctx, NULL, NULL);
79
80 /* find extension definition plugin */
Radek Krejci3e6632f2021-03-22 22:08:21 +010081 ext_p->compiled->plugin = lyplg_find(LYPLG_EXTENSION, ext_p->compiled->module->name,
82 ext_p->compiled->module->revision, ext_p->compiled->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020083 }
84
85 *ext = lysc_ext_dup(ext_p->compiled);
86
87done:
Radek Krejci2efc45b2020-12-22 16:25:44 +010088 if (ret) {
89 lysc_update_path(ctx, NULL, NULL);
90 lysc_update_path(ctx, NULL, NULL);
91 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020092 return ret;
93}
94
95LY_ERR
96lys_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 +010097 const struct lys_module *ext_mod)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020098{
Radek Krejci85ac8312021-03-03 20:21:33 +010099 LY_ERR ret = LY_SUCCESS;
100 struct lysp_ext *ext_def;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200101
Radek Krejciab430862021-03-02 20:13:40 +0100102 ext->parent_stmt = ext_p->parent_stmt;
103 ext->parent_stmt_index = ext_p->parent_stmt_index;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200104 ext->module = ctx->cur_mod;
105 ext->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200106
Radek Krejciab430862021-03-02 20:13:40 +0100107 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 +0100108 lysc_update_path(ctx, NULL, ext_p->name);
109
Radek Krejci85ac8312021-03-03 20:21:33 +0100110 LY_CHECK_GOTO(ret = lysp_ext_find_definition(ctx->ctx, ext_p, &ext_mod, &ext_def), cleanup);
111 LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, ext_def, &ext->def), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200112
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100113 if (ext_def->argname) {
Radek Krejci85ac8312021-03-03 20:21:33 +0100114 LY_CHECK_GOTO(ret = lysp_ext_instance_resolve_argument(ctx->ctx, ext_p, ext_def), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200115 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200116
Radek Krejci85ac8312021-03-03 20:21:33 +0100117 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret);
118 LY_CHECK_RET(ret);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200119
120 if (ext->def->plugin && ext->def->plugin->compile) {
121 if (ext->argument) {
Radek Krejcia6016992021-03-03 10:13:41 +0100122 lysc_update_path(ctx, ext->module, ext->argument);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200123 }
Michal Vaskofc2cd072021-02-24 13:17:17 +0100124 ret = ext->def->plugin->compile(ctx, ext_p, ext);
Radek Krejcicc21a4f2021-02-09 15:23:31 +0100125 if (ret == LY_ENOT) {
126 lysc_ext_instance_free(ctx->ctx, ext);
127 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200128 if (ext->argument) {
129 lysc_update_path(ctx, NULL, NULL);
130 }
Michal Vaskofc2cd072021-02-24 13:17:17 +0100131 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200132 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200133
134cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100135 lysc_update_path(ctx, NULL, NULL);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100136 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200137
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200138 return ret;
139}
140
141struct lysc_ext *
142lysc_ext_dup(struct lysc_ext *orig)
143{
144 ++orig->refcount;
145 return orig;
146}
147
Radek Krejcidab4f0b2021-03-29 14:07:18 +0200148API LY_ERR
Radek Krejci1b2eef82021-02-17 11:17:27 +0100149lysc_ext_substmt(const struct lysc_ext_instance *ext, enum ly_stmt substmt, void **instance_p, enum ly_stmt_cardinality *cardinality_p)
150{
151 LY_ARRAY_COUNT_TYPE u;
152
153 LY_ARRAY_FOR(ext->substmts, u) {
Radek Krejci61837f72021-03-02 19:53:59 +0100154 if (LY_STMT_IS_DATA_NODE(substmt)) {
155 if (!LY_STMT_IS_DATA_NODE(ext->substmts[u].stmt)) {
Radek Krejci1b2eef82021-02-17 11:17:27 +0100156 continue;
157 }
158 } else if (LY_STMT_IS_OP(substmt)) {
159 if (!LY_STMT_IS_OP(ext->substmts[u].stmt)) {
160 continue;
161 }
162 } else if (ext->substmts[u].stmt != substmt) {
163 continue;
164 }
165
166 /* match */
167 if (cardinality_p) {
168 *cardinality_p = ext->substmts[u].cardinality;
169 }
170 if (instance_p) {
171 *instance_p = ext->substmts[u].storage;
172 }
173 return LY_SUCCESS;
174 }
175
176 return LY_ENOT;
177}
178
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200179static void
180lysc_unres_dflt_free(const struct ly_ctx *ctx, struct lysc_unres_dflt *r)
181{
182 assert(!r->dflt || !r->dflts);
183 if (r->dflt) {
184 lysp_qname_free((struct ly_ctx *)ctx, r->dflt);
185 free(r->dflt);
186 } else {
187 FREE_ARRAY((struct ly_ctx *)ctx, r->dflts, lysp_qname_free);
188 }
189 free(r);
190}
191
192void
Radek Krejcia6016992021-03-03 10:13:41 +0100193lysc_update_path(struct lysc_ctx *ctx, struct lys_module *parent_module, const char *name)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200194{
195 int len;
196 uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
197
198 if (!name) {
199 /* removing last path segment */
200 if (ctx->path[ctx->path_len - 1] == '}') {
201 for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
202 if (ctx->path[ctx->path_len] == '=') {
203 ctx->path[ctx->path_len++] = '}';
204 } else {
205 /* not a top-level special tag, remove also preceiding '/' */
206 goto remove_nodelevel;
207 }
208 } else {
209remove_nodelevel:
210 for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
211 if (ctx->path_len == 0) {
212 /* top-level (last segment) */
213 ctx->path_len = 1;
214 }
215 }
216 /* set new terminating NULL-byte */
217 ctx->path[ctx->path_len] = '\0';
218 } else {
219 if (ctx->path_len > 1) {
Radek Krejcia6016992021-03-03 10:13:41 +0100220 if (!parent_module && (ctx->path[ctx->path_len - 1] == '}') && (ctx->path[ctx->path_len - 2] != '\'')) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200221 /* extension of the special tag */
222 nextlevel = 2;
223 --ctx->path_len;
224 } else {
225 /* there is already some path, so add next level */
226 nextlevel = 1;
227 }
228 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
229
230 if (nextlevel != 2) {
Radek Krejcia6016992021-03-03 10:13:41 +0100231 if ((parent_module && (parent_module == ctx->cur_mod)) || (!parent_module && (ctx->path_len > 1) && (name[0] == '{'))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200232 /* module not changed, print the name unprefixed */
233 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
234 } else {
235 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->cur_mod->name, name);
236 }
237 } else {
238 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
239 }
240 if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
241 /* output truncated */
242 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
243 } else {
244 ctx->path_len += len;
245 }
246 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100247
Radek Krejciddace2c2021-01-08 11:30:56 +0100248 LOG_LOCBACK(0, 0, 1, 0);
249 LOG_LOCSET(NULL, NULL, ctx->path, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200250}
251
aPiecek6b3d5422021-07-30 15:55:43 +0200252/**
253 * @brief Compile information from the identity statement
254 *
255 * The backlinks to the identities derived from this one are supposed to be filled later via ::lys_compile_identity_bases().
256 *
257 * @param[in] ctx_sc Compile context - alternative to the combination of @p ctx and @p parsed_mod.
258 * @param[in] ctx libyang context.
259 * @param[in] parsed_mod Module with the identities.
260 * @param[in] identities_p Array of the parsed identity definitions to precompile.
261 * @param[in,out] identities Pointer to the storage of the (pre)compiled identities array where the new identities are
262 * supposed to be added. The storage is supposed to be initiated to NULL when the first parsed identities are going
263 * to be processed.
264 * @return LY_ERR value.
265 */
266static LY_ERR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200267lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
268 struct lysp_ident *identities_p, struct lysc_ident **identities)
269{
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100270 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200271 struct lysc_ctx context = {0};
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100272 struct lysc_ident *ident;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200273 LY_ERR ret = LY_SUCCESS;
274
275 assert(ctx_sc || ctx);
276
277 if (!ctx_sc) {
278 context.ctx = ctx;
Radek Krejci3aac9a72020-12-01 12:24:26 +0100279 context.cur_mod = parsed_mod ? parsed_mod->mod : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200280 context.pmod = parsed_mod;
281 context.path_len = 1;
282 context.path[0] = '/';
283 ctx_sc = &context;
284 }
285
286 if (!identities_p) {
287 return LY_SUCCESS;
288 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200289
290 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200291 LY_ARRAY_FOR(identities_p, u) {
292 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
293
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100294 /* add new compiled identity */
295 LY_ARRAY_NEW_RET(ctx_sc->ctx, *identities, ident, LY_EMEM);
296
297 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, ident->name, ret, done);
298 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, ident->dsc, ret, done);
299 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, ident->ref, ret, done);
300 ident->module = ctx_sc->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200301 /* 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 +0100302 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, ident->exts, ident, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100303 ident->flags = identities_p[u].flags;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200304
305 lysc_update_path(ctx_sc, NULL, NULL);
306 }
307 lysc_update_path(ctx_sc, NULL, NULL);
308done:
309 return ret;
310}
311
312/**
313 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
314 *
315 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
316 *
317 * @param[in] ctx Compile context for logging.
318 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
319 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
320 * @return LY_SUCCESS if everything is ok.
321 * @return LY_EVALID if the identity is derived from itself.
322 */
323static LY_ERR
324lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
325{
326 LY_ERR ret = LY_SUCCESS;
327 LY_ARRAY_COUNT_TYPE u, v;
328 struct ly_set recursion = {0};
329 struct lysc_ident *drv;
330
331 if (!derived) {
332 return LY_SUCCESS;
333 }
334
335 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
336 if (ident == derived[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100337 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200338 "Identity \"%s\" is indirectly derived from itself.", ident->name);
339 ret = LY_EVALID;
340 goto cleanup;
341 }
342 ret = ly_set_add(&recursion, derived[u], 0, NULL);
343 LY_CHECK_GOTO(ret, cleanup);
344 }
345
346 for (v = 0; v < recursion.count; ++v) {
347 drv = recursion.objs[v];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200348 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
349 if (ident == drv->derived[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100350 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200351 "Identity \"%s\" is indirectly derived from itself.", ident->name);
352 ret = LY_EVALID;
353 goto cleanup;
354 }
355 ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
356 LY_CHECK_GOTO(ret, cleanup);
357 }
358 }
359
360cleanup:
361 ly_set_erase(&recursion, NULL);
362 return ret;
363}
364
365LY_ERR
366lys_compile_identity_bases(struct lysc_ctx *ctx, const struct lysp_module *base_pmod, const char **bases_p,
aPiecekf4a0a192021-08-03 15:14:17 +0200367 struct lysc_ident *ident, struct lysc_ident ***bases)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200368{
369 LY_ARRAY_COUNT_TYPE u, v;
370 const char *s, *name;
371 const struct lys_module *mod;
372 struct lysc_ident **idref;
373
aPiecekf4a0a192021-08-03 15:14:17 +0200374 assert(ident || bases);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200375
376 if ((LY_ARRAY_COUNT(bases_p) > 1) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100377 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200378 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
379 return LY_EVALID;
380 }
381
382 LY_ARRAY_FOR(bases_p, u) {
383 s = strchr(bases_p[u], ':');
384 if (s) {
385 /* prefixed identity */
386 name = &s[1];
Radek Krejci8df109d2021-04-23 12:19:08 +0200387 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 +0200388 } else {
389 name = bases_p[u];
390 mod = base_pmod->mod;
391 }
392 if (!mod) {
393 if (ident) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100394 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200395 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
396 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100397 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200398 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
399 }
400 return LY_EVALID;
401 }
402
403 idref = NULL;
404 LY_ARRAY_FOR(mod->identities, v) {
405 if (!strcmp(name, mod->identities[v].name)) {
406 if (ident) {
407 if (ident == &mod->identities[v]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100408 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200409 "Identity \"%s\" is derived from itself.", ident->name);
410 return LY_EVALID;
411 }
412 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived));
413 /* we have match! store the backlink */
414 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
415 *idref = ident;
416 } else {
417 /* we have match! store the found identity */
418 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
419 *idref = &mod->identities[v];
420 }
421 break;
422 }
423 }
aPiecekf4a0a192021-08-03 15:14:17 +0200424 if (!idref) {
Radek Krejci10443f42021-03-28 17:40:35 +0200425 if (ident) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100426 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200427 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
428 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100429 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200430 "Unable to find base (%s) of identityref.", bases_p[u]);
431 }
432 return LY_EVALID;
433 }
434 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100435
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200436 return LY_SUCCESS;
437}
438
439/**
440 * @brief For the given array of identities, set the backlinks from all their base identities.
441 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
442 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
aPiecekf4a0a192021-08-03 15:14:17 +0200443 * @param[in,out] idents Array of referencing identities to which the backlinks are supposed to be set.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200444 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
445 */
446static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100447lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident **idents)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200448{
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100449 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200450
451 lysc_update_path(ctx, NULL, "{identity}");
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100452
Radek Krejcic7d13e32020-12-09 12:32:24 +0100453 for (u = 0, v = 0; u < LY_ARRAY_COUNT(*idents); ++u) {
aPiecekf4a0a192021-08-03 15:14:17 +0200454 /* find matching parsed identity */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100455 while (v < LY_ARRAY_COUNT(idents_p)) {
456 if (idents_p[v].name == (*idents)[u].name) {
457 break;
458 }
459 ++v;
460 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100461
Michal Vaskodc6f84c2021-06-15 14:50:27 +0200462 if ((v == LY_ARRAY_COUNT(idents_p)) || !idents_p[v].bases) {
463 /* identity not found (it may be from a submodule) or identity without bases */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200464 continue;
465 }
Michal Vaskodc6f84c2021-06-15 14:50:27 +0200466
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100467 lysc_update_path(ctx, NULL, (*idents)[u].name);
aPiecekf4a0a192021-08-03 15:14:17 +0200468 LY_CHECK_RET(lys_compile_identity_bases(ctx, ctx->pmod, idents_p[v].bases, &(*idents)[u], NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200469 lysc_update_path(ctx, NULL, NULL);
470 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100471
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200472 lysc_update_path(ctx, NULL, NULL);
473 return LY_SUCCESS;
474}
475
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200476static void *
477lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
478{
479 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
480 if (substmts[u].stmt == stmt) {
481 return substmts[u].storage;
482 }
483 }
484 return NULL;
485}
486
487LY_ERR
Radek Krejci6b88a462021-02-17 12:39:34 +0100488lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200489{
aPiecek1c4da362021-04-29 14:26:34 +0200490 LY_ERR ret = LY_SUCCESS, r;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200491 LY_ARRAY_COUNT_TYPE u;
492 struct lysp_stmt *stmt;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200493 void *parsed = NULL, **compiled = NULL;
494
495 /* check for invalid substatements */
Radek Krejci6b88a462021-02-17 12:39:34 +0100496 for (stmt = ext_p->child; stmt; stmt = stmt->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200497 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
498 continue;
499 }
Radek Krejci6b88a462021-02-17 12:39:34 +0100500 LY_ARRAY_FOR(ext->substmts, u) {
501 if (ext->substmts[u].stmt == stmt->kw) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200502 break;
503 }
504 }
Radek Krejci6b88a462021-02-17 12:39:34 +0100505 if (u == LY_ARRAY_COUNT(ext->substmts)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100506 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 +0100507 stmt->stmt, ext_p->name, ext_p->argument ? " " : "", ext_p->argument ? ext_p->argument : "");
aPiecek1c4da362021-04-29 14:26:34 +0200508 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200509 goto cleanup;
510 }
511 }
512
513 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
514
Radek Krejci6b88a462021-02-17 12:39:34 +0100515 /* note into the compile context that we are processing extension now */
516 ctx->ext = ext;
517
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200518 /* keep order of the processing the same as the order in the defined substmts,
519 * 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 +0200520
Radek Krejci6b88a462021-02-17 12:39:34 +0100521 LY_ARRAY_FOR(ext->substmts, u) {
522 uint64_t stmt_counter = 0;
523
524 for (stmt = ext_p->child; stmt; stmt = stmt->next) {
525 if (ext->substmts[u].stmt != stmt->kw) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200526 continue;
527 }
528
Radek Krejci6b88a462021-02-17 12:39:34 +0100529 parsed = NULL;
530 stmt_counter++;
531 if (ext->substmts[u].storage) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200532 switch (stmt->kw) {
Radek Krejci6b88a462021-02-17 12:39:34 +0100533 case LY_STMT_ACTION:
534 case LY_STMT_ANYDATA:
535 case LY_STMT_ANYXML:
536 case LY_STMT_CONTAINER:
537 case LY_STMT_CHOICE:
538 case LY_STMT_LEAF:
539 case LY_STMT_LEAF_LIST:
540 case LY_STMT_LIST:
541 case LY_STMT_NOTIFICATION:
542 case LY_STMT_RPC:
543 case LY_STMT_USES:
aPiecek60d9d672021-04-27 15:49:57 +0200544 if (!ext_p->parsed) {
545 struct lysp_ext_instance *unconst_ext_p;
546 r = lysp_stmt_parse(ctx, stmt, &parsed, NULL);
547 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
548 unconst_ext_p = (struct lysp_ext_instance *)ext_p;
549 unconst_ext_p->parsed = parsed;
550 } else {
Michal Vaskoa9309bb2021-07-09 09:31:55 +0200551 struct lysp_node *node, *last_node = NULL;
aPiecek60d9d672021-04-27 15:49:57 +0200552 /* get last parsed node */
553 LY_LIST_FOR(ext_p->parsed, node) {
554 last_node = node;
555 }
556 /* create and link sibling */
557 r = lysp_stmt_parse(ctx, stmt, &parsed, NULL);
558 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
559 last_node->next = parsed;
560 }
Radek Krejci6b88a462021-02-17 12:39:34 +0100561
562 /* set storage as an alternative document root in the compile context */
563 r = lys_compile_node(ctx, parsed, NULL, 0, NULL);
Radek Krejci6b88a462021-02-17 12:39:34 +0100564 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200565 break;
Radek Krejci1b2eef82021-02-17 11:17:27 +0100566 case LY_STMT_DESCRIPTION:
567 case LY_STMT_REFERENCE:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200568 case LY_STMT_UNITS: {
Radek Krejci6b88a462021-02-17 12:39:34 +0100569 const char **str_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200570
Radek Krejci6b88a462021-02-17 12:39:34 +0100571 if (ext->substmts[u].cardinality < LY_STMT_CARD_SOME) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200572 /* single item */
Radek Krejci6b88a462021-02-17 12:39:34 +0100573 if (*((const char **)ext->substmts[u].storage)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100574 LOGVAL(ctx->ctx, LY_VCODE_DUPSTMT, stmt->stmt);
aPiecek1c4da362021-04-29 14:26:34 +0200575 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200576 goto cleanup;
577 }
Radek Krejci6b88a462021-02-17 12:39:34 +0100578 str_p = (const char **)ext->substmts[u].storage;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200579 } else {
580 /* sized array */
Radek Krejci6b88a462021-02-17 12:39:34 +0100581 const char ***strings_array = (const char ***)ext->substmts[u].storage;
582 LY_ARRAY_NEW_GOTO(ctx->ctx, *strings_array, str_p, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200583 }
Radek Krejci6b88a462021-02-17 12:39:34 +0100584 r = lydict_insert(ctx->ctx, stmt->arg, 0, str_p);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200585 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
586 break;
587 }
Radek Krejci6b88a462021-02-17 12:39:34 +0100588 case LY_STMT_IF_FEATURE: {
589 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200590
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100591 r = lysp_stmt_parse(ctx, stmt, &parsed, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200592 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejci6b88a462021-02-17 12:39:34 +0100593
594 r = lys_eval_iffeatures(ctx->ctx, parsed, &enabled);
595 FREE_ARRAY(ctx->ctx, (struct lysp_qname *)parsed, lysp_qname_free);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100596 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
597 if (!enabled) {
598 /* it is disabled, remove the whole extension instance */
599 return LY_ENOT;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200600 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200601 break;
Radek Krejci6b88a462021-02-17 12:39:34 +0100602 }
603 case LY_STMT_STATUS:
604 assert(ext->substmts[u].cardinality < LY_STMT_CARD_SOME);
605 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, &ext->substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
606 break;
607 case LY_STMT_TYPE: {
608 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, ext->substmts);
609 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, ext->substmts);
610
611 if (ext->substmts[u].cardinality < LY_STMT_CARD_SOME) {
612 /* single item */
613 if (*(struct lysc_type **)ext->substmts[u].storage) {
614 LOGVAL(ctx->ctx, LY_VCODE_DUPSTMT, stmt->stmt);
aPiecek1c4da362021-04-29 14:26:34 +0200615 ret = LY_EVALID;
Radek Krejci6b88a462021-02-17 12:39:34 +0100616 goto cleanup;
617 }
618 compiled = ext->substmts[u].storage;
619 } else {
620 /* sized array */
621 struct lysc_type ***types = (struct lysc_type ***)ext->substmts[u].storage, **type = NULL;
622 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
623 compiled = (void *)type;
624 }
625
626 r = lysp_stmt_parse(ctx, stmt, &parsed, NULL);
627 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
628 r = lys_compile_type(ctx, NULL, flags ? *flags : 0, ext_p->name, parsed, (struct lysc_type **)compiled,
629 units && !*units ? units : NULL, NULL);
630 lysp_type_free(ctx->ctx, parsed);
631 free(parsed);
632 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
633 break;
634 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200635 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
636 * also note that in many statements their extensions are not taken into account */
637 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100638 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s%s%s\") substatement.",
Radek Krejci6b88a462021-02-17 12:39:34 +0100639 stmt->stmt, ext_p->name, ext_p->argument ? " " : "", ext_p->argument ? ext_p->argument : "");
aPiecek1c4da362021-04-29 14:26:34 +0200640 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200641 goto cleanup;
642 }
643 }
644 }
645
Radek Krejci6b88a462021-02-17 12:39:34 +0100646 if (((ext->substmts[u].cardinality == LY_STMT_CARD_MAND) || (ext->substmts[u].cardinality == LY_STMT_CARD_SOME)) && !stmt_counter) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100647 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
Radek Krejci6b88a462021-02-17 12:39:34 +0100648 ly_stmt2str(ext->substmts[u].stmt), ext_p->name, ext_p->argument ? " " : "", ext_p->argument ? ext_p->argument : "");
aPiecek1c4da362021-04-29 14:26:34 +0200649 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200650 goto cleanup;
651 }
652 }
653
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200654cleanup:
Radek Krejci6b88a462021-02-17 12:39:34 +0100655 ctx->ext = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200656 return ret;
657}
658
659/**
660 * @brief Check when for cyclic dependencies.
661 *
662 * @param[in] set Set with all the referenced nodes.
663 * @param[in] node Node whose "when" referenced nodes are in @p set.
664 * @return LY_ERR value
665 */
666static LY_ERR
667lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
668{
669 struct lyxp_set tmp_set;
670 struct lyxp_set_scnode *xp_scnode;
671 uint32_t i, j;
672 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200673 LY_ERR ret = LY_SUCCESS;
674
675 memset(&tmp_set, 0, sizeof tmp_set);
676
677 /* prepare in_ctx of the set */
678 for (i = 0; i < set->used; ++i) {
679 xp_scnode = &set->val.scnodes[i];
680
Radek Krejcif13b87b2020-12-01 22:02:17 +0100681 if (xp_scnode->in_ctx != LYXP_SET_SCNODE_START_USED) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200682 /* check node when, skip the context node (it was just checked) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100683 xp_scnode->in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200684 }
685 }
686
687 for (i = 0; i < set->used; ++i) {
688 xp_scnode = &set->val.scnodes[i];
Radek Krejcif13b87b2020-12-01 22:02:17 +0100689 if (xp_scnode->in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200690 /* already checked */
691 continue;
692 }
693
Michal Vasko1a09b212021-05-06 13:00:10 +0200694 if ((xp_scnode->type != LYXP_NODE_ELEM) || !lysc_node_when(xp_scnode->scnode)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200695 /* no when to check */
Michal Vasko1a09b212021-05-06 13:00:10 +0200696 xp_scnode->in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200697 continue;
698 }
699
700 node = xp_scnode->scnode;
701 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100702 struct lysc_when **when_list, *when;
703
Radek Krejciddace2c2021-01-08 11:30:56 +0100704 LOG_LOCSET(node, NULL, NULL, NULL);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100705 when_list = lysc_node_when(node);
706 LY_ARRAY_FOR(when_list, u) {
707 when = when_list[u];
Radek Krejci8df109d2021-04-23 12:19:08 +0200708 ret = lyxp_atomize(set->ctx, when->cond, node->module, LY_VALUE_SCHEMA_RESOLVED, when->prefixes,
Michal Vasko400e9672021-01-11 13:39:17 +0100709 when->context, &tmp_set, LYXP_SCNODE_SCHEMA);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200710 if (ret != LY_SUCCESS) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100711 LOGVAL(set->ctx, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200712 goto cleanup;
713 }
714
715 for (j = 0; j < tmp_set.used; ++j) {
716 /* skip roots'n'stuff */
717 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
718 /* try to find this node in our set */
719 uint32_t idx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100720 if (lyxp_set_scnode_contains(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1, &idx) &&
721 (set->val.scnodes[idx].in_ctx == LYXP_SET_SCNODE_START_USED)) {
Michal Vasko2c4d9152021-07-20 09:17:28 +0200722 LOGVAL(set->ctx, LYVE_SEMANTICS, "When condition cyclic dependency on the node \"%s\".",
723 tmp_set.val.scnodes[j].scnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200724 ret = LY_EVALID;
725 goto cleanup;
726 }
727
728 /* needs to be checked, if in both sets, will be ignored */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100729 tmp_set.val.scnodes[j].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200730 } else {
731 /* no when, nothing to check */
Michal Vasko1a09b212021-05-06 13:00:10 +0200732 tmp_set.val.scnodes[j].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200733 }
734 }
735
736 /* merge this set into the global when set */
737 lyxp_set_scnode_merge(set, &tmp_set);
738 }
739
740 /* check when of non-data parents as well */
741 node = node->parent;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100742
Radek Krejciddace2c2021-01-08 11:30:56 +0100743 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200744 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
745
746 /* this node when was checked (xp_scnode could have been reallocd) */
Michal Vasko60edc2f2021-07-20 09:18:02 +0200747 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200748 }
749
750cleanup:
751 lyxp_set_free_content(&tmp_set);
752 return ret;
753}
754
755LY_ERR
756lysc_check_status(struct lysc_ctx *ctx, uint16_t flags1, void *mod1, const char *name1, uint16_t flags2, void *mod2,
757 const char *name2)
758{
759 uint16_t flg1, flg2;
760
761 flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
762 flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
763
764 if ((flg1 < flg2) && (mod1 == mod2)) {
765 if (ctx) {
Michal Vasko23864e02021-06-24 09:23:58 +0200766 LOGVAL(ctx->ctx, LYVE_REFERENCE, "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200767 flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1,
768 flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2);
769 }
770 return LY_EVALID;
771 }
772
773 return LY_SUCCESS;
774}
775
Michal Vasko25d6ad02020-10-22 12:20:22 +0200776LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200777lys_compile_expr_implement(const struct ly_ctx *ctx, const struct lyxp_expr *expr, LY_VALUE_FORMAT format,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100778 void *prefix_data, ly_bool implement, struct lys_glob_unres *unres, const struct lys_module **mod_p)
Michal Vaskocfaff232020-10-20 09:35:14 +0200779{
780 uint32_t i;
781 const char *ptr, *start;
782 const struct lys_module *mod;
783
Michal Vasko25d6ad02020-10-22 12:20:22 +0200784 assert(implement || mod_p);
785
Michal Vaskocfaff232020-10-20 09:35:14 +0200786 for (i = 0; i < expr->used; ++i) {
787 if ((expr->tokens[i] != LYXP_TOKEN_NAMETEST) && (expr->tokens[i] != LYXP_TOKEN_LITERAL)) {
788 /* token cannot have a prefix */
789 continue;
790 }
791
792 start = expr->expr + expr->tok_pos[i];
793 if (!(ptr = ly_strnchr(start, ':', expr->tok_len[i]))) {
794 /* token without a prefix */
795 continue;
796 }
797
798 if (!(mod = ly_resolve_prefix(ctx, start, ptr - start, format, prefix_data))) {
799 /* unknown prefix, do not care right now */
800 continue;
801 }
802
Michal Vasko0c296722021-07-14 08:53:16 +0200803 /* unimplemented module found */
804 if (!mod->implemented && !implement) {
805 /* should not be implemented now */
806 *mod_p = mod;
807 break;
808 }
809
Michal Vaskocfaff232020-10-20 09:35:14 +0200810 if (!mod->implemented) {
Michal Vasko0c296722021-07-14 08:53:16 +0200811 /* implement if not implemented */
812 LY_CHECK_RET(lys_implement((struct lys_module *)mod, NULL, unres));
813 }
814 if (!mod->compiled) {
815 /* compile if not implemented before or only marked for compilation */
816 LY_CHECK_RET(lys_compile((struct lys_module *)mod, &unres->ds_unres));
Michal Vaskocfaff232020-10-20 09:35:14 +0200817 }
818 }
819
Michal Vasko25d6ad02020-10-22 12:20:22 +0200820 return LY_SUCCESS;
Michal Vaskocfaff232020-10-20 09:35:14 +0200821}
822
823/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200824 * @brief Check when/must expressions of a node on a complete compiled schema tree.
825 *
826 * @param[in] ctx Compile context.
827 * @param[in] node Node to check.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100828 * @param[in,out] unres Global unres structure.
Michal Vasko40c158c2021-04-28 17:01:03 +0200829 * @return LY_ERECOMPILE
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200830 * @return LY_ERR value
831 */
832static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +0100833lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200834{
835 struct lyxp_set tmp_set;
836 uint32_t i, opts;
837 LY_ARRAY_COUNT_TYPE u;
838 ly_bool input_done = 0;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100839 struct lysc_when **whens = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200840 struct lysc_must *musts = NULL;
841 LY_ERR ret = LY_SUCCESS;
Michal Vaskocfaff232020-10-20 09:35:14 +0200842 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200843
Radek Krejciddace2c2021-01-08 11:30:56 +0100844 LOG_LOCSET(node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100845
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200846 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100847 opts = LYXP_SCNODE_SCHEMA | ((node->flags & LYS_IS_OUTPUT) ? LYXP_SCNODE_OUTPUT : 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200848
Radek Krejci9a3823e2021-01-27 20:26:46 +0100849 whens = lysc_node_when(node);
850 musts = lysc_node_musts(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200851
Radek Krejci9a3823e2021-01-27 20:26:46 +0100852 LY_ARRAY_FOR(whens, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +0200853 /* first check whether all the referenced modules are implemented */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200854 mod = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200855 ret = lys_compile_expr_implement(ctx->ctx, whens[u]->cond, LY_VALUE_SCHEMA_RESOLVED, whens[u]->prefixes,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100856 ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, unres, &mod);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200857 if (ret) {
858 goto cleanup;
859 } else if (mod) {
Michal Vaskocfaff232020-10-20 09:35:14 +0200860 LOGWRN(ctx->ctx, "When condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
Radek Krejci9a3823e2021-01-27 20:26:46 +0100861 whens[u]->cond->expr, mod->name);
Michal Vaskocfaff232020-10-20 09:35:14 +0200862 continue;
863 }
864
865 /* check "when" */
Radek Krejci8df109d2021-04-23 12:19:08 +0200866 ret = lyxp_atomize(ctx->ctx, whens[u]->cond, node->module, LY_VALUE_SCHEMA_RESOLVED, whens[u]->prefixes,
Radek Krejci9a3823e2021-01-27 20:26:46 +0100867 whens[u]->context, &tmp_set, opts);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200868 if (ret) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100869 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid when condition \"%s\".", whens[u]->cond->expr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200870 goto cleanup;
871 }
872
873 ctx->path[0] = '\0';
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100874 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200875 for (i = 0; i < tmp_set.used; ++i) {
876 /* skip roots'n'stuff */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100877 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START_USED)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200878 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
879
880 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci9a3823e2021-01-27 20:26:46 +0100881 ret = lysc_check_status(ctx, whens[u]->flags, node->module, node->name, schema->flags, schema->module,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200882 schema->name);
883 LY_CHECK_GOTO(ret, cleanup);
884
Michal Vasko1a09b212021-05-06 13:00:10 +0200885 /* check dummy node children/value accessing */
886 if (lysc_data_parent(schema) == node) {
887 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "When condition is accessing its own conditional node children.");
888 ret = LY_EVALID;
889 goto cleanup;
890 } else if ((schema == node) && (tmp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
891 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "When condition is accessing its own conditional node value.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200892 ret = LY_EVALID;
893 goto cleanup;
894 }
895 }
896 }
897
898 /* check cyclic dependencies */
899 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
900 LY_CHECK_GOTO(ret, cleanup);
901
902 lyxp_set_free_content(&tmp_set);
903 }
904
905check_musts:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200906 LY_ARRAY_FOR(musts, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +0200907 /* first check whether all the referenced modules are implemented */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200908 mod = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200909 ret = lys_compile_expr_implement(ctx->ctx, musts[u].cond, LY_VALUE_SCHEMA_RESOLVED, musts[u].prefixes,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100910 ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, unres, &mod);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200911 if (ret) {
912 goto cleanup;
913 } else if (mod) {
Michal Vaskocfaff232020-10-20 09:35:14 +0200914 LOGWRN(ctx->ctx, "Must condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
915 musts[u].cond->expr, mod->name);
916 continue;
917 }
918
919 /* check "must" */
Radek Krejci8df109d2021-04-23 12:19:08 +0200920 ret = lyxp_atomize(ctx->ctx, musts[u].cond, node->module, LY_VALUE_SCHEMA_RESOLVED, musts[u].prefixes, node,
Michal Vasko400e9672021-01-11 13:39:17 +0100921 &tmp_set, opts);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200922 if (ret) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100923 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200924 goto cleanup;
925 }
926
927 ctx->path[0] = '\0';
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100928 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200929 for (i = 0; i < tmp_set.used; ++i) {
930 /* skip roots'n'stuff */
931 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
932 /* XPath expression cannot reference "lower" status than the node that has the definition */
933 ret = lysc_check_status(ctx, node->flags, node->module, node->name, tmp_set.val.scnodes[i].scnode->flags,
934 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
935 LY_CHECK_GOTO(ret, cleanup);
936 }
937 }
938
939 lyxp_set_free_content(&tmp_set);
940 }
941
942 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
943 /* now check output musts */
944 input_done = 1;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100945 whens = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100946 musts = ((struct lysc_node_action *)node)->output.musts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200947 opts = LYXP_SCNODE_OUTPUT;
948 goto check_musts;
949 }
950
951cleanup:
952 lyxp_set_free_content(&tmp_set);
Radek Krejciddace2c2021-01-08 11:30:56 +0100953 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200954 return ret;
955}
956
957/**
958 * @brief Check leafref for its target existence on a complete compiled schema tree.
959 *
960 * @param[in] ctx Compile context.
961 * @param[in] node Context node for the leafref.
962 * @param[in] lref Leafref to check/resolve.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100963 * @param[in,out] unres Global unres structure.
Michal Vasko40c158c2021-04-28 17:01:03 +0200964 * @return LY_ERECOMPILE if context recompilation is needed,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200965 * @return LY_ERR value.
966 */
967static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +0100968lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref,
969 struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200970{
Michal Vaskod1e53b92021-01-28 13:11:06 +0100971 const struct lysc_node *target = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200972 struct ly_path *p;
973 struct lysc_type *type;
974
975 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
976
Michal Vasko24fc4d12021-07-12 14:41:20 +0200977 /* first implement all the modules in the path */
978 LY_CHECK_RET(lys_compile_expr_implement(ctx->ctx, lref->path, LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, 1, unres, NULL));
979
Michal Vaskoed725d72021-06-23 12:03:45 +0200980 /* try to find the target, current module is that of the context node (RFC 7950 6.4.1 second bullet) */
981 LY_CHECK_RET(ly_path_compile_leafref(ctx->ctx, node, NULL, lref->path,
Michal Vaskod1e53b92021-01-28 13:11:06 +0100982 (node->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +0200983 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200984
985 /* get the target node */
986 target = p[LY_ARRAY_COUNT(p) - 1].node;
987 ly_path_free(node->module->ctx, p);
988
989 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100990 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 +0200991 lref->path->expr, lys_nodetype2str(target->nodetype));
992 return LY_EVALID;
993 }
994
995 /* check status */
996 ctx->path[0] = '\0';
997 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
998 ctx->path_len = strlen(ctx->path);
999 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
1000 return LY_EVALID;
1001 }
1002 ctx->path_len = 1;
1003 ctx->path[1] = '\0';
1004
1005 /* check config */
1006 if (lref->require_instance) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01001007 if ((node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001008 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001009 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
1010 return LY_EVALID;
1011 }
1012 }
1013
1014 /* store the target's type and check for circular chain of leafrefs */
1015 lref->realtype = ((struct lysc_node_leaf *)target)->type;
1016 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
1017 if (type == (struct lysc_type *)lref) {
1018 /* circular chain detected */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001019 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid leafref path \"%s\" - circular chain of leafrefs detected.",
1020 lref->path->expr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001021 return LY_EVALID;
1022 }
1023 }
1024
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001025 return LY_SUCCESS;
1026}
1027
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001028/**
1029 * @brief Compile default value(s) for leaf or leaf-list expecting a complete compiled schema tree.
1030 *
1031 * @param[in] ctx Compile context.
1032 * @param[in] node Leaf or leaf-list to compile the default value(s) for.
1033 * @param[in] type Type of the default value.
1034 * @param[in] dflt Default value.
1035 * @param[in] dflt_pmod Parsed module of the @p dflt to resolve possible prefixes.
1036 * @param[in,out] storage Storage for the compiled default value.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001037 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001038 * @return LY_ERR value.
1039 */
1040static LY_ERR
1041lys_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 +01001042 const struct lysp_module *dflt_pmod, struct lyd_value *storage, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001043{
1044 LY_ERR ret;
Michal Vasko25d6ad02020-10-22 12:20:22 +02001045 uint32_t options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001046 struct ly_err_item *err = NULL;
1047
Radek Krejci0b013302021-03-29 15:22:32 +02001048 options = (ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED) ? LYPLG_TYPE_STORE_IMPLEMENT : 0;
Radek Krejci8df109d2021-04-23 12:19:08 +02001049 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), options, LY_VALUE_SCHEMA, (void *)dflt_pmod,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001050 LYD_HINT_SCHEMA, node, storage, unres, &err);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001051 if (ret == LY_ERECOMPILE) {
1052 /* fine, but we need to recompile */
1053 return LY_ERECOMPILE;
1054 } else if (ret == LY_EINCOMPLETE) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001055 /* we have no data so we will not be resolving it */
1056 ret = LY_SUCCESS;
1057 }
1058
1059 if (ret) {
Radek Krejciddace2c2021-01-08 11:30:56 +01001060 LOG_LOCSET(node, NULL, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001061 if (err) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001062 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid default - value does not fit the type (%s).", err->msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001063 ly_err_free(err);
1064 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001065 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid default - value does not fit the type.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001066 }
Radek Krejciddace2c2021-01-08 11:30:56 +01001067 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001068 return ret;
1069 }
1070
1071 ++((struct lysc_type *)storage->realtype)->refcount;
1072 return LY_SUCCESS;
1073}
1074
1075/**
1076 * @brief Compile default value of a leaf expecting a complete compiled schema tree.
1077 *
1078 * @param[in] ctx Compile context.
1079 * @param[in] leaf Leaf that the default value is for.
1080 * @param[in] dflt Default value to compile.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001081 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001082 * @return LY_ERR value.
1083 */
1084static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001085lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt,
1086 struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001087{
1088 LY_ERR ret;
1089
1090 assert(!leaf->dflt);
1091
1092 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
1093 /* ignore default values for keys and mandatory leaves */
1094 return LY_SUCCESS;
1095 }
1096
1097 /* allocate the default value */
1098 leaf->dflt = calloc(1, sizeof *leaf->dflt);
1099 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
1100
1101 /* store the default value */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001102 ret = lys_compile_unres_dflt(ctx, &leaf->node, leaf->type, dflt->str, dflt->mod, leaf->dflt, unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001103 if (ret) {
1104 free(leaf->dflt);
1105 leaf->dflt = NULL;
1106 }
1107
1108 return ret;
1109}
1110
1111/**
1112 * @brief Compile default values of a leaf-list expecting a complete compiled schema tree.
1113 *
1114 * @param[in] ctx Compile context.
1115 * @param[in] llist Leaf-list that the default value(s) are for.
1116 * @param[in] dflt Default value to compile, in case of a single value.
1117 * @param[in] dflts Sized array of default values, in case of more values.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001118 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001119 * @return LY_ERR value.
1120 */
1121static LY_ERR
1122lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflt,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001123 struct lysp_qname *dflts, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001124{
1125 LY_ERR ret;
1126 LY_ARRAY_COUNT_TYPE orig_count, u, v;
1127
1128 assert(dflt || dflts);
1129
Radek Krejcic7d13e32020-12-09 12:32:24 +01001130 /* in case there were already some defaults and we are adding new by deviations */
1131 orig_count = LY_ARRAY_COUNT(llist->dflts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001132
1133 /* allocate new items */
Radek Krejcic7d13e32020-12-09 12:32:24 +01001134 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 +02001135
1136 /* fill each new default value */
1137 if (dflts) {
1138 LY_ARRAY_FOR(dflts, u) {
1139 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001140 ret = lys_compile_unres_dflt(ctx, &llist->node, llist->type, dflts[u].str, dflts[u].mod,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001141 llist->dflts[orig_count + u], unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001142 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
1143 LY_ARRAY_INCREMENT(llist->dflts);
1144 }
1145 } else {
1146 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001147 ret = lys_compile_unres_dflt(ctx, &llist->node, llist->type, dflt->str, dflt->mod,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001148 llist->dflts[orig_count], unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001149 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
1150 LY_ARRAY_INCREMENT(llist->dflts);
1151 }
1152
1153 /* check default value uniqueness */
1154 if (llist->flags & LYS_CONFIG_W) {
1155 /* configuration data values must be unique - so check the default values */
1156 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
1157 for (v = 0; v < u; ++v) {
1158 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
Radek Krejcia6016992021-03-03 10:13:41 +01001159 lysc_update_path(ctx, llist->parent ? llist->parent->module : NULL, llist->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001160 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration leaf-list has multiple defaults of the same value \"%s\".",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001161 llist->dflts[u]->realtype->plugin->print(ctx->ctx, llist->dflts[u], LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001162 lysc_update_path(ctx, NULL, NULL);
1163 return LY_EVALID;
1164 }
1165 }
1166 }
1167 }
1168
1169 return LY_SUCCESS;
1170}
1171
Michal Vaskof4258e12021-06-15 12:11:42 +02001172/**
aPiecek732cd142021-07-07 16:29:59 +02001173 * @brief Iteratively get all leafrefs from @p node
1174 * if the node is of type union, otherwise just return the leafref.
1175 *
1176 * @param[in] node Node that may contain the leafref.
1177 * @param[in,out] index Value that is passed between function calls.
1178 * For each new node, initialize value of the @p index to 0, otherwise
1179 * do not modify the value between calls.
1180 * @return Pointer to the leafref or next leafref, otherwise NULL.
1181 */
1182static struct lysc_type_leafref *
1183lys_type_leafref_next(const struct lysc_node *node, uint64_t *index)
1184{
1185 struct lysc_type_leafref *ret = NULL;
1186 struct lysc_type_union *uni;
1187 struct lysc_type *leaf_type;
1188
1189 assert(node->nodetype & LYD_NODE_TERM);
1190
1191 leaf_type = ((struct lysc_node_leaf *)node)->type;
1192 if (leaf_type->basetype == LY_TYPE_UNION) {
1193 uni = (struct lysc_type_union *)leaf_type;
1194
1195 /* find next union leafref */
1196 while (*index < LY_ARRAY_COUNT(uni->types)) {
1197 if (uni->types[*index]->basetype == LY_TYPE_LEAFREF) {
1198 ret = (struct lysc_type_leafref *)uni->types[*index];
1199 ++(*index);
1200 break;
1201 }
1202
1203 ++(*index);
1204 }
1205 } else {
1206 /* return just the single leafref */
1207 if (*index == 0) {
1208 ++(*index);
1209 assert(leaf_type->basetype == LY_TYPE_LEAFREF);
1210 ret = (struct lysc_type_leafref *)leaf_type;
1211 }
1212 }
1213
1214 return ret;
1215}
1216
1217/**
Michal Vaskof4258e12021-06-15 12:11:42 +02001218 * @brief Finish dependency set compilation by resolving all the unres sets.
1219 *
1220 * @param[in] ctx libyang context.
1221 * @param[in] unres Global unres structure with the sets to resolve.
1222 * @return LY_SUCCESS on success.
1223 * @return LY_ERECOMPILE if the dep set needs to be recompiled.
1224 * @return LY_ERR value on error.
1225 */
1226static LY_ERR
1227lys_compile_unres_depset(struct ly_ctx *ctx, struct lys_glob_unres *unres)
Michal Vasko405cc9e2020-12-01 12:01:27 +01001228{
aPiecek732cd142021-07-07 16:29:59 +02001229 LY_ERR ret = LY_SUCCESS;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001230 struct lysc_node *node;
aPiecek732cd142021-07-07 16:29:59 +02001231 struct lysc_type *typeiter;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001232 struct lysc_type_leafref *lref;
aPiecekd7bd52d2021-07-08 08:59:59 +02001233 struct lysc_ctx cctx;
Michal Vaskof4258e12021-06-15 12:11:42 +02001234 struct lys_depset_unres *ds_unres;
aPiecekd7bd52d2021-07-08 08:59:59 +02001235 struct ly_path *path;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001236 LY_ARRAY_COUNT_TYPE v;
aPiecekd7bd52d2021-07-08 08:59:59 +02001237 uint32_t i, processed_leafrefs = 0;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001238
Michal Vaskof4258e12021-06-15 12:11:42 +02001239 ds_unres = &unres->ds_unres;
1240
Michal Vasko405cc9e2020-12-01 12:01:27 +01001241 /* fake compile context */
aPiecekd7bd52d2021-07-08 08:59:59 +02001242resolve_all:
1243 memset(&cctx, 0, sizeof cctx);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001244 cctx.ctx = ctx;
1245 cctx.path_len = 1;
1246 cctx.path[0] = '/';
1247
1248 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
1249 * 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 +02001250 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation.
1251 * Also do the same check for set of the disabled leafrefs, but without the second round. */
1252 while (ds_unres->disabled_leafrefs.count) {
1253 node = ds_unres->disabled_leafrefs.objs[ds_unres->disabled_leafrefs.count - 1];
1254 cctx.cur_mod = node->module;
1255 cctx.pmod = node->module->parsed;
1256 LOG_LOCSET(node, NULL, NULL, NULL);
1257
aPiecekc6526b42021-07-12 15:21:39 +02001258 v = 0;
1259 while ((ret == LY_SUCCESS) && (lref = lys_type_leafref_next(node, &v))) {
1260 ret = lys_compile_unres_leafref(&cctx, node, lref, unres);
1261 }
1262
1263 LOG_LOCBACK(1, 0, 0, 0);
1264 LY_CHECK_RET(ret);
1265 ly_set_rm_index(&ds_unres->disabled_leafrefs, ds_unres->disabled_leafrefs.count - 1, NULL);
1266 }
Michal Vaskoa23e1d92021-07-13 12:04:16 +02001267
aPiecekd7bd52d2021-07-08 08:59:59 +02001268 for (i = processed_leafrefs; i < ds_unres->leafrefs.count; ++i) {
Michal Vaskof4258e12021-06-15 12:11:42 +02001269 node = ds_unres->leafrefs.objs[i];
Michal Vasko405cc9e2020-12-01 12:01:27 +01001270 cctx.cur_mod = node->module;
1271 cctx.pmod = node->module->parsed;
Radek Krejciddace2c2021-01-08 11:30:56 +01001272 LOG_LOCSET(node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001273
aPiecek732cd142021-07-07 16:29:59 +02001274 v = 0;
1275 while ((ret == LY_SUCCESS) && (lref = lys_type_leafref_next(node, &v))) {
1276 ret = lys_compile_unres_leafref(&cctx, node, lref, unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001277 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001278
Radek Krejciddace2c2021-01-08 11:30:56 +01001279 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko7213ac52021-06-15 11:52:13 +02001280 LY_CHECK_RET(ret);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001281 }
aPiecekd7bd52d2021-07-08 08:59:59 +02001282 for (i = processed_leafrefs; i < ds_unres->leafrefs.count; ++i) {
1283 node = ds_unres->leafrefs.objs[i];
Radek Krejci2efc45b2020-12-22 16:25:44 +01001284
Michal Vasko405cc9e2020-12-01 12:01:27 +01001285 /* store pointer to the real type */
aPiecek732cd142021-07-07 16:29:59 +02001286 v = 0;
1287 while ((lref = lys_type_leafref_next(node, &v))) {
1288 for (typeiter = lref->realtype;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001289 typeiter->basetype == LY_TYPE_LEAFREF;
1290 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
aPiecek732cd142021-07-07 16:29:59 +02001291 lref->realtype = typeiter;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001292 }
1293
Michal Vaskoa23e1d92021-07-13 12:04:16 +02001294 /* If 'goto' will be used on the 'resolve_all' label, then
aPiecekd7bd52d2021-07-08 08:59:59 +02001295 * the current leafref will not be processed again.
1296 */
1297 processed_leafrefs++;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001298 }
1299
1300 /* check xpath */
Michal Vaskof4258e12021-06-15 12:11:42 +02001301 while (ds_unres->xpath.count) {
1302 node = ds_unres->xpath.objs[ds_unres->xpath.count - 1];
Michal Vasko405cc9e2020-12-01 12:01:27 +01001303 cctx.cur_mod = node->module;
1304 cctx.pmod = node->module->parsed;
1305
Radek Krejciddace2c2021-01-08 11:30:56 +01001306 LOG_LOCSET(node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001307 ret = lys_compile_unres_xpath(&cctx, node, unres);
Radek Krejciddace2c2021-01-08 11:30:56 +01001308 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko7213ac52021-06-15 11:52:13 +02001309 LY_CHECK_RET(ret);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001310
Michal Vaskof4258e12021-06-15 12:11:42 +02001311 ly_set_rm_index(&ds_unres->xpath, ds_unres->xpath.count - 1, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001312 }
1313
1314 /* finish incomplete default values compilation */
Michal Vaskof4258e12021-06-15 12:11:42 +02001315 while (ds_unres->dflts.count) {
1316 struct lysc_unres_dflt *r = ds_unres->dflts.objs[ds_unres->dflts.count - 1];
Michal Vasko405cc9e2020-12-01 12:01:27 +01001317 cctx.cur_mod = r->leaf->module;
1318 cctx.pmod = r->leaf->module->parsed;
1319
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001320 LOG_LOCSET(&r->leaf->node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001321 if (r->leaf->nodetype == LYS_LEAF) {
1322 ret = lys_compile_unres_leaf_dlft(&cctx, r->leaf, r->dflt, unres);
1323 } else {
1324 ret = lys_compile_unres_llist_dflts(&cctx, r->llist, r->dflt, r->dflts, unres);
1325 }
Radek Krejciddace2c2021-01-08 11:30:56 +01001326 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko7213ac52021-06-15 11:52:13 +02001327 LY_CHECK_RET(ret);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001328
1329 lysc_unres_dflt_free(ctx, r);
Michal Vaskof4258e12021-06-15 12:11:42 +02001330 ly_set_rm_index(&ds_unres->dflts, ds_unres->dflts.count - 1, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001331 }
1332
Michal Vasko40c158c2021-04-28 17:01:03 +02001333 /* some unres items may have been added */
aPiecekc6526b42021-07-12 15:21:39 +02001334 if ((processed_leafrefs != ds_unres->leafrefs.count) || ds_unres->disabled_leafrefs.count ||
1335 ds_unres->xpath.count || ds_unres->dflts.count) {
aPiecekd7bd52d2021-07-08 08:59:59 +02001336 goto resolve_all;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001337 }
1338
Michal Vasko30ab8e72021-04-19 12:47:52 +02001339 /* finally, remove all disabled nodes */
Michal Vaskof4258e12021-06-15 12:11:42 +02001340 for (i = 0; i < ds_unres->disabled.count; ++i) {
1341 node = ds_unres->disabled.snodes[i];
Michal Vasko30ab8e72021-04-19 12:47:52 +02001342 if (node->flags & LYS_KEY) {
1343 LOG_LOCSET(node, NULL, NULL, NULL);
Michal Vaskoe02e7402021-07-23 08:29:28 +02001344 LOGVAL(ctx, LYVE_REFERENCE, "Key \"%s\" is disabled.", node->name);
Michal Vasko30ab8e72021-04-19 12:47:52 +02001345 LOG_LOCBACK(1, 0, 0, 0);
1346 return LY_EVALID;
1347 }
1348
1349 lysc_node_free(ctx, node, 1);
1350 }
1351
aPiecekd7bd52d2021-07-08 08:59:59 +02001352 /* also check if the leafref target has not been disabled */
1353 for (i = 0; i < ds_unres->leafrefs.count; ++i) {
1354 node = ds_unres->leafrefs.objs[i];
1355 cctx.cur_mod = node->module;
1356 cctx.pmod = node->module->parsed;
1357
1358 v = 0;
1359 while ((lref = lys_type_leafref_next(node, &v))) {
1360 ret = ly_path_compile_leafref(cctx.ctx, node, NULL, lref->path,
1361 (node->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
1362 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &path);
1363 ly_path_free(node->module->ctx, path);
1364
1365 assert(ret != LY_ERECOMPILE);
1366 if (ret) {
1367 LOG_LOCSET(node, NULL, NULL, NULL);
Michal Vaskoe02e7402021-07-23 08:29:28 +02001368 LOGVAL(ctx, LYVE_REFERENCE, "Target of leafref \"%s\" cannot be referenced because it is disabled.",
1369 node->name);
aPiecekd7bd52d2021-07-08 08:59:59 +02001370 LOG_LOCBACK(1, 0, 0, 0);
1371 return LY_EVALID;
1372 }
1373 }
1374 }
1375
Michal Vasko405cc9e2020-12-01 12:01:27 +01001376 return LY_SUCCESS;
1377}
1378
Michal Vaskof4258e12021-06-15 12:11:42 +02001379/**
1380 * @brief Erase dep set unres.
1381 *
1382 * @param[in] ctx libyang context.
1383 * @param[in] unres Global unres structure with the sets to resolve.
1384 */
1385static void
1386lys_compile_unres_depset_erase(const struct ly_ctx *ctx, struct lys_glob_unres *unres)
Michal Vasko405cc9e2020-12-01 12:01:27 +01001387{
1388 uint32_t i;
1389
Michal Vaskof4258e12021-06-15 12:11:42 +02001390 for (i = 0; i < unres->ds_unres.dflts.count; ++i) {
1391 lysc_unres_dflt_free(ctx, unres->ds_unres.dflts.objs[i]);
Michal Vaskodf6319c2021-06-10 14:41:05 +02001392 }
Michal Vaskof4258e12021-06-15 12:11:42 +02001393 ly_set_erase(&unres->ds_unres.dflts, NULL);
1394 ly_set_erase(&unres->ds_unres.xpath, NULL);
1395 ly_set_erase(&unres->ds_unres.leafrefs, NULL);
aPiecekc6526b42021-07-12 15:21:39 +02001396 ly_set_erase(&unres->ds_unres.disabled_leafrefs, NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +02001397 ly_set_erase(&unres->ds_unres.disabled, NULL);
1398}
1399
Michal Vasko709f9a52021-07-21 10:51:59 +02001400/**
1401 * @brief Compile all flagged modules in a dependency set, recursively if recompilation is needed.
1402 *
1403 * @param[in] ctx libyang context.
1404 * @param[in] dep_set Dependency set to compile.
1405 * @param[in,out] unres Global unres to use.
1406 * @return LY_ERR value.
1407 */
1408static LY_ERR
1409lys_compile_depset_r(struct ly_ctx *ctx, struct ly_set *dep_set, struct lys_glob_unres *unres)
Michal Vaskof4258e12021-06-15 12:11:42 +02001410{
1411 LY_ERR ret = LY_SUCCESS;
1412 struct lys_module *mod;
1413 uint32_t i;
1414
1415 for (i = 0; i < dep_set->count; ++i) {
1416 mod = dep_set->objs[i];
1417 if (!mod->to_compile) {
1418 /* skip */
1419 continue;
1420 }
1421 assert(mod->implemented);
1422
1423 /* free the compiled module, if any */
1424 lysc_module_free(mod->compiled);
1425 mod->compiled = NULL;
1426
1427 /* (re)compile the module */
1428 LY_CHECK_GOTO(ret = lys_compile(mod, &unres->ds_unres), cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001429 }
Michal Vaskof4258e12021-06-15 12:11:42 +02001430
1431 /* resolve dep set unres */
1432 ret = lys_compile_unres_depset(ctx, unres);
1433 if (ret == LY_ERECOMPILE) {
1434 /* new module is implemented, discard current dep set unres and recompile the whole dep set */
1435 lys_compile_unres_depset_erase(ctx, unres);
Michal Vasko709f9a52021-07-21 10:51:59 +02001436 return lys_compile_depset_r(ctx, dep_set, unres);
Michal Vaskof4258e12021-06-15 12:11:42 +02001437 } else if (ret) {
1438 /* error */
1439 goto cleanup;
1440 }
1441
1442 /* success, unset the flags of all the modules in the dep set */
1443 for (i = 0; i < dep_set->count; ++i) {
1444 mod = dep_set->objs[i];
1445 mod->to_compile = 0;
1446 }
1447
1448cleanup:
1449 lys_compile_unres_depset_erase(ctx, unres);
1450 return ret;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001451}
1452
Michal Vasko709f9a52021-07-21 10:51:59 +02001453LY_ERR
1454lys_compile_depset_all(struct ly_ctx *ctx, struct lys_glob_unres *unres)
1455{
1456 uint32_t i;
1457
1458 for (i = 0; i < unres->dep_sets.count; ++i) {
1459 LY_CHECK_RET(lys_compile_depset_r(ctx, unres->dep_sets.objs[i], unres));
1460 }
1461
1462 return LY_SUCCESS;
1463}
1464
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001465/**
Michal Vasko405cc9e2020-12-01 12:01:27 +01001466 * @brief Finish compilation of all the module unres sets in a compile context.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001467 *
1468 * @param[in] ctx Compile context with unres sets.
1469 * @return LY_ERR value.
1470 */
1471static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001472lys_compile_unres_mod(struct lysc_ctx *ctx)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001473{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001474 struct lysc_augment *aug;
1475 struct lysc_deviation *dev;
Michal Vasko1ade6f12021-04-19 11:32:45 +02001476 struct lys_module *orig_mod = ctx->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001477 uint32_t i;
1478
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001479 /* check that all augments were applied */
1480 for (i = 0; i < ctx->augs.count; ++i) {
1481 aug = ctx->augs.objs[i];
Michal Vasko1ade6f12021-04-19 11:32:45 +02001482 ctx->cur_mod = aug->aug_pmod->mod;
Radek Krejci2efc45b2020-12-22 16:25:44 +01001483 lysc_update_path(ctx, NULL, "{augment}");
1484 lysc_update_path(ctx, NULL, aug->nodeid->expr);
1485 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" from module \"%s\" was not found.",
Michal Vasko28fe5f62021-03-03 16:37:39 +01001486 aug->nodeid->expr, LYSP_MODULE_NAME(aug->aug_pmod));
Michal Vasko1ade6f12021-04-19 11:32:45 +02001487 ctx->cur_mod = orig_mod;
Radek Krejci2efc45b2020-12-22 16:25:44 +01001488 lysc_update_path(ctx, NULL, NULL);
1489 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001490 }
1491 if (ctx->augs.count) {
1492 return LY_ENOTFOUND;
1493 }
1494
1495 /* check that all deviations were applied */
1496 for (i = 0; i < ctx->devs.count; ++i) {
1497 dev = ctx->devs.objs[i];
Radek Krejci2efc45b2020-12-22 16:25:44 +01001498 lysc_update_path(ctx, NULL, "{deviation}");
1499 lysc_update_path(ctx, NULL, dev->nodeid->expr);
1500 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Deviation(s) target node \"%s\" from module \"%s\" was not found.",
1501 dev->nodeid->expr, LYSP_MODULE_NAME(dev->dev_pmods[0]));
1502 lysc_update_path(ctx, NULL, NULL);
1503 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001504 }
1505 if (ctx->devs.count) {
1506 return LY_ENOTFOUND;
1507 }
1508
1509 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001510}
1511
1512/**
Michal Vasko405cc9e2020-12-01 12:01:27 +01001513 * @brief Erase all the module unres sets in a compile context.
1514 *
1515 * @param[in] ctx Compile context with unres sets.
1516 * @param[in] error Whether the compilation finished with an error or not.
1517 */
1518static void
1519lys_compile_unres_mod_erase(struct lysc_ctx *ctx, ly_bool error)
1520{
1521 uint32_t i;
1522
1523 ly_set_erase(&ctx->groupings, NULL);
1524 ly_set_erase(&ctx->tpdf_chain, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001525
1526 if (!error) {
1527 /* there can be no leftover deviations or augments */
1528 LY_CHECK_ERR_RET(ctx->augs.count, LOGINT(ctx->ctx), );
1529 LY_CHECK_ERR_RET(ctx->devs.count, LOGINT(ctx->ctx), );
1530
1531 ly_set_erase(&ctx->augs, NULL);
1532 ly_set_erase(&ctx->devs, NULL);
1533 ly_set_erase(&ctx->uses_augs, NULL);
1534 ly_set_erase(&ctx->uses_rfns, NULL);
1535 } else {
1536 for (i = 0; i < ctx->augs.count; ++i) {
1537 lysc_augment_free(ctx->ctx, ctx->augs.objs[i]);
1538 }
1539 ly_set_erase(&ctx->augs, NULL);
1540 for (i = 0; i < ctx->devs.count; ++i) {
1541 lysc_deviation_free(ctx->ctx, ctx->devs.objs[i]);
1542 }
1543 ly_set_erase(&ctx->devs, NULL);
1544 for (i = 0; i < ctx->uses_augs.count; ++i) {
1545 lysc_augment_free(ctx->ctx, ctx->uses_augs.objs[i]);
1546 }
1547 ly_set_erase(&ctx->uses_augs, NULL);
1548 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1549 lysc_refine_free(ctx->ctx, ctx->uses_rfns.objs[i]);
1550 }
1551 ly_set_erase(&ctx->uses_rfns, NULL);
1552 }
1553}
1554
Michal Vaskod39bea42021-06-14 10:42:49 +02001555LY_ERR
Michal Vaskof4258e12021-06-15 12:11:42 +02001556lys_compile(struct lys_module *mod, struct lys_depset_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001557{
1558 struct lysc_ctx ctx = {0};
Michal Vasko0b395e12021-04-23 13:43:07 +02001559 struct lysc_module *mod_c = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001560 struct lysp_module *sp;
1561 struct lysp_submodule *submod;
1562 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001563 struct lysp_node_grp *grp;
1564 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001565 LY_ERR ret = LY_SUCCESS;
1566
1567 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
1568
Michal Vaskof4258e12021-06-15 12:11:42 +02001569 assert(mod->implemented && mod->to_compile);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001570
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001571 sp = mod->parsed;
1572
1573 ctx.ctx = mod->ctx;
1574 ctx.cur_mod = mod;
1575 ctx.pmod = sp;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001576 ctx.path_len = 1;
1577 ctx.path[0] = '/';
Michal Vasko405cc9e2020-12-01 12:01:27 +01001578 ctx.unres = unres;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001579
Michal Vasko0b395e12021-04-23 13:43:07 +02001580 ++mod->ctx->change_count;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001581 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
1582 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
1583 mod_c->mod = mod;
1584
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001585 /* compile augments and deviations of our module from other modules so they can be applied during compilation */
Michal Vasko1ccbf542021-04-19 11:35:00 +02001586 LY_CHECK_GOTO(ret = lys_precompile_own_augments(&ctx), cleanup);
1587 LY_CHECK_GOTO(ret = lys_precompile_own_deviations(&ctx), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001588
1589 /* data nodes */
1590 LY_LIST_FOR(sp->data, pnode) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001591 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001592 }
1593
Radek Krejci2a9fc652021-01-22 17:44:34 +01001594 /* top-level RPCs */
1595 LY_LIST_FOR((struct lysp_node *)sp->rpcs, pnode) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001596 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001597 }
1598
1599 /* top-level notifications */
1600 LY_LIST_FOR((struct lysp_node *)sp->notifs, pnode) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001601 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001602 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001603
1604 /* extension instances */
Michal Vasko1ccbf542021-04-19 11:35:00 +02001605 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001606
1607 /* the same for submodules */
1608 LY_ARRAY_FOR(sp->includes, u) {
1609 submod = sp->includes[u].submodule;
1610 ctx.pmod = (struct lysp_module *)submod;
1611
1612 LY_LIST_FOR(submod->data, pnode) {
1613 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001614 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001615 }
1616
Radek Krejci2a9fc652021-01-22 17:44:34 +01001617 LY_LIST_FOR((struct lysp_node *)submod->rpcs, pnode) {
1618 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001619 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001620 }
1621
1622 LY_LIST_FOR((struct lysp_node *)submod->notifs, pnode) {
1623 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001624 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001625 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001626
Michal Vasko1ccbf542021-04-19 11:35:00 +02001627 COMPILE_EXTS_GOTO(&ctx, submod->exts, mod_c->exts, mod_c, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001628 }
Michal Vasko12606ee2020-11-25 17:05:11 +01001629 ctx.pmod = sp;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001630
1631 /* validate non-instantiated groupings from the parsed schema,
1632 * without it we would accept even the schemas with invalid grouping specification */
Michal Vasko7c565922021-06-10 14:58:27 +02001633 ctx.compile_opts |= LYS_COMPILE_GROUPING;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001634 LY_LIST_FOR(sp->groupings, grp) {
1635 if (!(grp->flags & LYS_USED_GRP)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001636 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, grp), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001637 }
1638 }
1639 LY_LIST_FOR(sp->data, pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001640 LY_LIST_FOR((struct lysp_node_grp *)lysp_node_groupings(pnode), grp) {
1641 if (!(grp->flags & LYS_USED_GRP)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001642 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, grp), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001643 }
1644 }
1645 }
1646 LY_ARRAY_FOR(sp->includes, u) {
1647 submod = sp->includes[u].submodule;
1648 ctx.pmod = (struct lysp_module *)submod;
1649
Radek Krejci2a9fc652021-01-22 17:44:34 +01001650 LY_LIST_FOR(submod->groupings, grp) {
1651 if (!(grp->flags & LYS_USED_GRP)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001652 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, grp), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001653 }
1654 }
1655 LY_LIST_FOR(submod->data, pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001656 LY_LIST_FOR((struct lysp_node_grp *)lysp_node_groupings(pnode), grp) {
1657 if (!(grp->flags & LYS_USED_GRP)) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001658 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, grp), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001659 }
1660 }
1661 }
1662 }
1663 ctx.pmod = sp;
1664
Radek Krejciddace2c2021-01-08 11:30:56 +01001665 LOG_LOCBACK(0, 0, 1, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001666
Michal Vasko405cc9e2020-12-01 12:01:27 +01001667 /* finish compilation for all unresolved module items in the context */
Michal Vasko1ccbf542021-04-19 11:35:00 +02001668 LY_CHECK_GOTO(ret = lys_compile_unres_mod(&ctx), cleanup);
Michal Vasko12606ee2020-11-25 17:05:11 +01001669
Michal Vasko1ccbf542021-04-19 11:35:00 +02001670cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01001671 LOG_LOCBACK(0, 0, 1, 0);
Michal Vasko1ccbf542021-04-19 11:35:00 +02001672 lys_compile_unres_mod_erase(&ctx, ret);
1673 if (ret) {
Michal Vasko1ccbf542021-04-19 11:35:00 +02001674 lysc_module_free(mod_c);
1675 mod->compiled = NULL;
1676 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001677 return ret;
1678}
Michal Vasko65333882021-06-10 14:12:16 +02001679
aPiecek6b3d5422021-07-30 15:55:43 +02001680LY_ERR
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001681lys_compile_identities(struct lys_module *mod)
1682{
1683 struct lysc_ctx ctx = {0};
1684 struct lysp_submodule *submod;
1685 LY_ARRAY_COUNT_TYPE u;
1686
aPiecek6b3d5422021-07-30 15:55:43 +02001687 /* pre-compile identities of the module and any submodules */
1688 LY_CHECK_RET(lys_identity_precompile(NULL, mod->ctx, mod->parsed, mod->parsed->identities, &mod->identities));
1689 LY_ARRAY_FOR(mod->parsed->includes, u) {
1690 submod = mod->parsed->includes[u].submodule;
1691 LY_CHECK_RET(lys_identity_precompile(NULL, mod->ctx, (struct lysp_module *)submod, submod->identities, &mod->identities));
1692 }
1693
Michal Vaskofa5a7d72021-06-15 11:41:20 +02001694 /* prepare context */
1695 ctx.ctx = mod->ctx;
1696 ctx.cur_mod = mod;
1697 ctx.pmod = mod->parsed;
1698 ctx.path_len = 1;
1699 ctx.path[0] = '/';
1700
1701 if (mod->parsed->identities) {
1702 LY_CHECK_RET(lys_compile_identities_derived(&ctx, mod->parsed->identities, &mod->identities));
1703 }
1704 lysc_update_path(&ctx, NULL, "{submodule}");
1705 LY_ARRAY_FOR(mod->parsed->includes, u) {
1706 submod = mod->parsed->includes[u].submodule;
1707 if (submod->identities) {
1708 ctx.pmod = (struct lysp_module *)submod;
1709 lysc_update_path(&ctx, NULL, submod->name);
1710 LY_CHECK_RET(lys_compile_identities_derived(&ctx, submod->identities, &mod->identities));
1711 lysc_update_path(&ctx, NULL, NULL);
1712 }
1713 }
1714 lysc_update_path(&ctx, NULL, NULL);
1715
1716 return LY_SUCCESS;
1717}
1718
1719/**
Michal Vasko65333882021-06-10 14:12:16 +02001720 * @brief Check whether a module does not have any (recursive) compiled import.
1721 *
1722 * @param[in] mod Module to examine.
1723 * @return LY_SUCCESS on success.
Michal Vaskof4258e12021-06-15 12:11:42 +02001724 * @return LY_ERECOMPILE on required recompilation of the dep set.
Michal Vasko65333882021-06-10 14:12:16 +02001725 * @return LY_ERR on error.
1726 */
1727static LY_ERR
1728lys_has_compiled_import_r(struct lys_module *mod)
1729{
1730 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoe7927122021-06-15 12:00:04 +02001731 struct lys_module *m;
Michal Vasko65333882021-06-10 14:12:16 +02001732
1733 LY_ARRAY_FOR(mod->parsed->imports, u) {
Michal Vaskoe7927122021-06-15 12:00:04 +02001734 m = mod->parsed->imports[u].module;
1735 if (!m->implemented) {
Michal Vasko65333882021-06-10 14:12:16 +02001736 continue;
1737 }
1738
Michal Vaskoe7927122021-06-15 12:00:04 +02001739 if (!m->to_compile) {
Michal Vaskod39bea42021-06-14 10:42:49 +02001740 /* module was not/will not be compiled in this compilation (so disabled nodes are not present) */
Michal Vaskof4258e12021-06-15 12:11:42 +02001741 m->to_compile = 1;
Michal Vasko65333882021-06-10 14:12:16 +02001742 return LY_ERECOMPILE;
1743 }
1744
1745 /* recursive */
Michal Vaskoe7927122021-06-15 12:00:04 +02001746 LY_CHECK_RET(lys_has_compiled_import_r(m));
Michal Vasko65333882021-06-10 14:12:16 +02001747 }
1748
1749 return LY_SUCCESS;
1750}
1751
1752LY_ERR
1753lys_implement(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
1754{
1755 LY_ERR ret;
1756 struct lys_module *m;
1757
1758 assert(!mod->implemented);
1759
1760 /* we have module from the current context */
1761 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
1762 if (m) {
1763 assert(m != mod);
1764
1765 /* check collision with other implemented revision */
1766 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
1767 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
1768 return LY_EDENIED;
1769 }
1770
1771 /* set features */
1772 ret = lys_set_features(mod->parsed, features);
1773 if (ret && (ret != LY_EEXIST)) {
1774 return ret;
1775 }
1776
Michal Vasko65333882021-06-10 14:12:16 +02001777 /*
1778 * mark the module implemented, which means
1779 * 1) to (re)compile it only ::lys_compile() call is needed
1780 * 2) its compilation will never cause new modules to be implemented (::lys_compile() does not return ::LY_ERECOMPILE)
1781 * but there can be some unres items added that do
1782 */
1783 mod->implemented = 1;
1784
Michal Vaskoa9f807e2021-06-15 12:07:16 +02001785 /* this module is compiled in this compilation */
1786 mod->to_compile = 1;
1787
Michal Vasko65333882021-06-10 14:12:16 +02001788 /* add the module into newly implemented module set */
1789 LY_CHECK_RET(ly_set_add(&unres->implementing, mod, 1, NULL));
1790
Michal Vaskoa9f807e2021-06-15 12:07:16 +02001791 /* mark target modules with our augments and deviations */
1792 LY_CHECK_RET(lys_precompile_augments_deviations(mod, unres));
Michal Vasko65333882021-06-10 14:12:16 +02001793
Michal Vaskod39bea42021-06-14 10:42:49 +02001794 /* check whether this module may reference any modules compiled previously */
1795 LY_CHECK_RET(lys_has_compiled_import_r(mod));
Michal Vasko65333882021-06-10 14:12:16 +02001796
Michal Vasko65333882021-06-10 14:12:16 +02001797 return LY_SUCCESS;
1798}