blob: 3e50e1ee9f4bc2f2fff56b897885376f6e589729 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema compilation.
5 *
6 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#define _GNU_SOURCE
16
17#include "schema_compile.h"
18
19#include <assert.h>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020020#include <stddef.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include "common.h"
27#include "compat.h"
28#include "context.h"
29#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020030#include "in.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "log.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020032#include "parser_schema.h"
33#include "path.h"
34#include "plugins_exts.h"
35#include "plugins_exts_internal.h"
36#include "plugins_types.h"
37#include "schema_compile_amend.h"
38#include "schema_compile_node.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010039#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020040#include "set.h"
41#include "tree.h"
42#include "tree_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020043#include "tree_schema.h"
44#include "tree_schema_internal.h"
45#include "xpath.h"
46
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020047/**
48 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
49 */
50static LY_ERR
51lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
52{
53 LY_ERR ret = LY_SUCCESS;
54
55 if (!ext_p->compiled) {
56 lysc_update_path(ctx, NULL, "{extension}");
57 lysc_update_path(ctx, NULL, ext_p->name);
58
59 /* compile the extension definition */
60 ext_p->compiled = calloc(1, sizeof **ext);
61 ext_p->compiled->refcount = 1;
62 DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done);
63 DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext_p->compiled->argument, ret, done);
64 ext_p->compiled->module = (struct lys_module *)ext_mod;
Radek Krejciab430862021-03-02 20:13:40 +010065 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020066
67 lysc_update_path(ctx, NULL, NULL);
68 lysc_update_path(ctx, NULL, NULL);
69
70 /* find extension definition plugin */
71 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
72 }
73
74 *ext = lysc_ext_dup(ext_p->compiled);
75
76done:
Radek Krejci2efc45b2020-12-22 16:25:44 +010077 if (ret) {
78 lysc_update_path(ctx, NULL, NULL);
79 lysc_update_path(ctx, NULL, NULL);
80 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020081 return ret;
82}
83
84LY_ERR
85lys_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 +010086 const struct lys_module *ext_mod)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020087{
Michal Vaskofc2cd072021-02-24 13:17:17 +010088 LY_ERR r, ret = LY_SUCCESS;
89 const char *tmp, *name, *prefix;
90 size_t pref_len, name_len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020091 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020092
93 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret);
94 LY_CHECK_RET(ret);
95
Radek Krejciab430862021-03-02 20:13:40 +010096 ext->parent_stmt = ext_p->parent_stmt;
97 ext->parent_stmt_index = ext_p->parent_stmt_index;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020098 ext->module = ctx->cur_mod;
99 ext->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200100
Radek Krejciab430862021-03-02 20:13:40 +0100101 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 +0100102 lysc_update_path(ctx, NULL, ext_p->name);
103
104 /* parse the prefix */
105 tmp = ext_p->name;
106 r = ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
107 /* it was parsed already */
108 assert(!r && !tmp[0]);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200109
110 /* get module where the extension definition should be placed */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200111 if (!ext_mod) {
Michal Vaskofc2cd072021-02-24 13:17:17 +0100112 ext_mod = ly_resolve_prefix(ctx->ctx, prefix, pref_len, ext_p->format, ext_p->prefix_data);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200113 if (!ext_mod) {
Michal Vaskofc2cd072021-02-24 13:17:17 +0100114 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.",
115 pref_len, prefix);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200116 ret = LY_EVALID;
117 goto cleanup;
118 } else if (!ext_mod->parsed->extensions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100119 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200120 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Michal Vaskofc2cd072021-02-24 13:17:17 +0100121 ext_p->name, ext_mod->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200122 ret = LY_EVALID;
123 goto cleanup;
124 }
125 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200126
127 /* find the parsed extension definition there */
128 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
129 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
130 /* compile extension definition and assign it */
131 LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def), cleanup);
132 break;
133 }
134 }
135 if (!ext->def) {
Michal Vaskofc2cd072021-02-24 13:17:17 +0100136 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext_p->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200137 ret = LY_EVALID;
138 goto cleanup;
139 }
140
141 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
142 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
143 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Michal Vaskofc2cd072021-02-24 13:17:17 +0100144 if ((ext_p->format == LY_PREF_XML) && ext->def->argument && !ext->argument) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200145 /* Schema was parsed from YIN and an argument is expected, ... */
146 struct lysp_stmt *stmt = NULL;
147
148 if (ext->def->flags & LYS_YINELEM_TRUE) {
149 /* ... argument was the first XML child element */
150 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
151 /* TODO check namespace of the statement */
152 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
153 stmt = ext_p->child;
154 }
155 }
156 } else {
157 /* ... argument was one of the XML attributes which are represented as child stmt
158 * with LYS_YIN_ATTR flag */
159 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
160 if (!strcmp(stmt->stmt, ext->def->argument)) {
161 /* this is the extension's argument */
162 break;
163 }
164 }
165 }
166 if (!stmt) {
167 /* missing extension's argument */
Michal Vaskofc2cd072021-02-24 13:17:17 +0100168 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Extension instance \"%s\" misses argument \"%s\".",
169 ext_p->name, ext->def->argument);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200170 ret = LY_EVALID;
171 goto cleanup;
172
173 }
174 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup);
175 stmt->flags |= LYS_YIN_ARGUMENT;
176 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200177
178 if (ext->def->plugin && ext->def->plugin->compile) {
179 if (ext->argument) {
Radek Krejcia6016992021-03-03 10:13:41 +0100180 lysc_update_path(ctx, ext->module, ext->argument);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200181 }
Michal Vaskofc2cd072021-02-24 13:17:17 +0100182 ret = ext->def->plugin->compile(ctx, ext_p, ext);
Radek Krejcicc21a4f2021-02-09 15:23:31 +0100183 if (ret == LY_ENOT) {
184 lysc_ext_instance_free(ctx->ctx, ext);
185 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200186 if (ext->argument) {
187 lysc_update_path(ctx, NULL, NULL);
188 }
Michal Vaskofc2cd072021-02-24 13:17:17 +0100189 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200190 }
191 ext_p->compiled = ext;
192
193cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100194 lysc_update_path(ctx, NULL, NULL);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100195 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200196
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200197 return ret;
198}
199
200struct lysc_ext *
201lysc_ext_dup(struct lysc_ext *orig)
202{
203 ++orig->refcount;
204 return orig;
205}
206
Radek Krejci1b2eef82021-02-17 11:17:27 +0100207LY_ERR
208lysc_ext_substmt(const struct lysc_ext_instance *ext, enum ly_stmt substmt, void **instance_p, enum ly_stmt_cardinality *cardinality_p)
209{
210 LY_ARRAY_COUNT_TYPE u;
211
212 LY_ARRAY_FOR(ext->substmts, u) {
Radek Krejci61837f72021-03-02 19:53:59 +0100213 if (LY_STMT_IS_DATA_NODE(substmt)) {
214 if (!LY_STMT_IS_DATA_NODE(ext->substmts[u].stmt)) {
Radek Krejci1b2eef82021-02-17 11:17:27 +0100215 continue;
216 }
217 } else if (LY_STMT_IS_OP(substmt)) {
218 if (!LY_STMT_IS_OP(ext->substmts[u].stmt)) {
219 continue;
220 }
221 } else if (ext->substmts[u].stmt != substmt) {
222 continue;
223 }
224
225 /* match */
226 if (cardinality_p) {
227 *cardinality_p = ext->substmts[u].cardinality;
228 }
229 if (instance_p) {
230 *instance_p = ext->substmts[u].storage;
231 }
232 return LY_SUCCESS;
233 }
234
235 return LY_ENOT;
236}
237
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200238static void
239lysc_unres_dflt_free(const struct ly_ctx *ctx, struct lysc_unres_dflt *r)
240{
241 assert(!r->dflt || !r->dflts);
242 if (r->dflt) {
243 lysp_qname_free((struct ly_ctx *)ctx, r->dflt);
244 free(r->dflt);
245 } else {
246 FREE_ARRAY((struct ly_ctx *)ctx, r->dflts, lysp_qname_free);
247 }
248 free(r);
249}
250
251void
Radek Krejcia6016992021-03-03 10:13:41 +0100252lysc_update_path(struct lysc_ctx *ctx, struct lys_module *parent_module, const char *name)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200253{
254 int len;
255 uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
256
257 if (!name) {
258 /* removing last path segment */
259 if (ctx->path[ctx->path_len - 1] == '}') {
260 for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
261 if (ctx->path[ctx->path_len] == '=') {
262 ctx->path[ctx->path_len++] = '}';
263 } else {
264 /* not a top-level special tag, remove also preceiding '/' */
265 goto remove_nodelevel;
266 }
267 } else {
268remove_nodelevel:
269 for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
270 if (ctx->path_len == 0) {
271 /* top-level (last segment) */
272 ctx->path_len = 1;
273 }
274 }
275 /* set new terminating NULL-byte */
276 ctx->path[ctx->path_len] = '\0';
277 } else {
278 if (ctx->path_len > 1) {
Radek Krejcia6016992021-03-03 10:13:41 +0100279 if (!parent_module && (ctx->path[ctx->path_len - 1] == '}') && (ctx->path[ctx->path_len - 2] != '\'')) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200280 /* extension of the special tag */
281 nextlevel = 2;
282 --ctx->path_len;
283 } else {
284 /* there is already some path, so add next level */
285 nextlevel = 1;
286 }
287 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
288
289 if (nextlevel != 2) {
Radek Krejcia6016992021-03-03 10:13:41 +0100290 if ((parent_module && (parent_module == ctx->cur_mod)) || (!parent_module && (ctx->path_len > 1) && (name[0] == '{'))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200291 /* module not changed, print the name unprefixed */
292 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
293 } else {
294 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->cur_mod->name, name);
295 }
296 } else {
297 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
298 }
299 if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
300 /* output truncated */
301 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
302 } else {
303 ctx->path_len += len;
304 }
305 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100306
Radek Krejciddace2c2021-01-08 11:30:56 +0100307 LOG_LOCBACK(0, 0, 1, 0);
308 LOG_LOCSET(NULL, NULL, ctx->path, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200309}
310
311/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200312 * @brief Compile information in the import statement - make sure there is the target module
313 * @param[in] ctx Compile context.
314 * @param[in] imp_p The parsed import statement structure to fill the module to.
315 * @return LY_ERR value.
316 */
317static LY_ERR
318lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
319{
320 const struct lys_module *mod = NULL;
321 LY_ERR ret = LY_SUCCESS;
322
323 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
324 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
325 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
326 if (!imp_p->module->parsed) {
327 /* try to use filepath if present */
328 if (imp_p->module->filepath) {
329 struct ly_in *in;
330 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
331 LOGINT(ctx->ctx);
332 } else {
333 LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)],
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100334 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, NULL, &mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200335 if (mod != imp_p->module) {
336 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
337 imp_p->module->filepath, imp_p->module->name);
338 mod = NULL;
339 }
340 }
341 ly_in_free(in, 1);
342 }
343 if (!mod) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100344 if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, NULL, ctx->unres,
345 (struct lys_module **)&mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200346 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
347 imp_p->module->name, ctx->cur_mod->name);
348 return LY_ENOTFOUND;
349 }
350 }
351 }
352
353 return ret;
354}
355
356LY_ERR
357lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
358 struct lysp_ident *identities_p, struct lysc_ident **identities)
359{
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100360 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200361 struct lysc_ctx context = {0};
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100362 struct lysc_ident *ident;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200363 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100364 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200365
366 assert(ctx_sc || ctx);
367
368 if (!ctx_sc) {
369 context.ctx = ctx;
Radek Krejci3aac9a72020-12-01 12:24:26 +0100370 context.cur_mod = parsed_mod ? parsed_mod->mod : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200371 context.pmod = parsed_mod;
372 context.path_len = 1;
373 context.path[0] = '/';
374 ctx_sc = &context;
375 }
376
377 if (!identities_p) {
378 return LY_SUCCESS;
379 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200380
381 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200382 LY_ARRAY_FOR(identities_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100383 /* evaluate if-features */
384 LY_CHECK_RET(lys_eval_iffeatures(ctx, identities_p[u].iffeatures, &enabled));
385 if (!enabled) {
386 continue;
387 }
388
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200389 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
390
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100391 /* add new compiled identity */
392 LY_ARRAY_NEW_RET(ctx_sc->ctx, *identities, ident, LY_EMEM);
393
394 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, ident->name, ret, done);
395 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, ident->dsc, ret, done);
396 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, ident->ref, ret, done);
397 ident->module = ctx_sc->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200398 /* 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 +0100399 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, ident->exts, ident, ret, done);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100400 ident->flags = identities_p[u].flags;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200401
402 lysc_update_path(ctx_sc, NULL, NULL);
403 }
404 lysc_update_path(ctx_sc, NULL, NULL);
405done:
406 return ret;
407}
408
409/**
410 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
411 *
412 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
413 *
414 * @param[in] ctx Compile context for logging.
415 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
416 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
417 * @return LY_SUCCESS if everything is ok.
418 * @return LY_EVALID if the identity is derived from itself.
419 */
420static LY_ERR
421lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
422{
423 LY_ERR ret = LY_SUCCESS;
424 LY_ARRAY_COUNT_TYPE u, v;
425 struct ly_set recursion = {0};
426 struct lysc_ident *drv;
427
428 if (!derived) {
429 return LY_SUCCESS;
430 }
431
432 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
433 if (ident == derived[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100434 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200435 "Identity \"%s\" is indirectly derived from itself.", ident->name);
436 ret = LY_EVALID;
437 goto cleanup;
438 }
439 ret = ly_set_add(&recursion, derived[u], 0, NULL);
440 LY_CHECK_GOTO(ret, cleanup);
441 }
442
443 for (v = 0; v < recursion.count; ++v) {
444 drv = recursion.objs[v];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200445 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
446 if (ident == drv->derived[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100447 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200448 "Identity \"%s\" is indirectly derived from itself.", ident->name);
449 ret = LY_EVALID;
450 goto cleanup;
451 }
452 ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
453 LY_CHECK_GOTO(ret, cleanup);
454 }
455 }
456
457cleanup:
458 ly_set_erase(&recursion, NULL);
459 return ret;
460}
461
462LY_ERR
463lys_compile_identity_bases(struct lysc_ctx *ctx, const struct lysp_module *base_pmod, const char **bases_p,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100464 struct lysc_ident *ident, struct lysc_ident ***bases, ly_bool *enabled)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200465{
466 LY_ARRAY_COUNT_TYPE u, v;
467 const char *s, *name;
468 const struct lys_module *mod;
469 struct lysc_ident **idref;
470
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100471 assert((ident && enabled) || bases);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200472
473 if ((LY_ARRAY_COUNT(bases_p) > 1) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100474 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200475 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
476 return LY_EVALID;
477 }
478
479 LY_ARRAY_FOR(bases_p, u) {
480 s = strchr(bases_p[u], ':');
481 if (s) {
482 /* prefixed identity */
483 name = &s[1];
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100484 mod = ly_resolve_prefix(ctx->ctx, bases_p[u], s - bases_p[u], LY_PREF_SCHEMA, (void *)base_pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200485 } else {
486 name = bases_p[u];
487 mod = base_pmod->mod;
488 }
489 if (!mod) {
490 if (ident) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100491 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200492 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
493 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100494 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200495 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
496 }
497 return LY_EVALID;
498 }
499
500 idref = NULL;
501 LY_ARRAY_FOR(mod->identities, v) {
502 if (!strcmp(name, mod->identities[v].name)) {
503 if (ident) {
504 if (ident == &mod->identities[v]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100505 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200506 "Identity \"%s\" is derived from itself.", ident->name);
507 return LY_EVALID;
508 }
509 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived));
510 /* we have match! store the backlink */
511 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
512 *idref = ident;
513 } else {
514 /* we have match! store the found identity */
515 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
516 *idref = &mod->identities[v];
517 }
518 break;
519 }
520 }
521 if (!idref || !(*idref)) {
522 if (ident) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100523 /* look into the parsed module to check whether the identity is not merely disabled */
524 LY_ARRAY_FOR(mod->parsed->identities, v) {
525 if (!strcmp(mod->parsed->identities[v].name, name)) {
526 *enabled = 0;
527 return LY_SUCCESS;
528 }
529 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100530 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200531 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
532 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100533 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200534 "Unable to find base (%s) of identityref.", bases_p[u]);
535 }
536 return LY_EVALID;
537 }
538 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100539
540 if (ident) {
541 *enabled = 1;
542 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200543 return LY_SUCCESS;
544}
545
546/**
547 * @brief For the given array of identities, set the backlinks from all their base identities.
548 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
549 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100550 * @param[in,out] idents Array of referencing identities to which the backlinks are supposed to be set. Any
551 * identities with disabled bases are removed.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200552 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
553 */
554static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100555lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident **idents)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200556{
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100557 LY_ARRAY_COUNT_TYPE u, v;
558 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200559
560 lysc_update_path(ctx, NULL, "{identity}");
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100561
562restart:
Radek Krejcic7d13e32020-12-09 12:32:24 +0100563 for (u = 0, v = 0; u < LY_ARRAY_COUNT(*idents); ++u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100564 /* find matching parsed identity, the disabled ones are missing in the compiled array */
565 while (v < LY_ARRAY_COUNT(idents_p)) {
566 if (idents_p[v].name == (*idents)[u].name) {
567 break;
568 }
569 ++v;
570 }
571 assert(v < LY_ARRAY_COUNT(idents_p));
572
573 if (!idents_p[v].bases) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200574 continue;
575 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100576 lysc_update_path(ctx, NULL, (*idents)[u].name);
577 LY_CHECK_RET(lys_compile_identity_bases(ctx, (*idents)[u].module->parsed, idents_p[v].bases, &(*idents)[u], NULL,
578 &enabled));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200579 lysc_update_path(ctx, NULL, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100580
581 if (!enabled) {
582 /* remove the identity */
583 lysc_ident_free(ctx->ctx, &(*idents)[u]);
584 LY_ARRAY_DECREMENT(*idents);
585 if (u < LY_ARRAY_COUNT(*idents)) {
586 memmove(&(*idents)[u], &(*idents)[u + 1], (LY_ARRAY_COUNT(*idents) - u) * sizeof **idents);
587 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100588
589 /* revert compilation of all the previous identities */
590 for (v = 0; v < u; ++v) {
591 LY_ARRAY_FREE((*idents)[v].derived);
592 }
593
Michal Vasko74c82ae2020-11-03 17:36:53 +0100594 /* free the whole array if there are no identites left */
595 if (!LY_ARRAY_COUNT(*idents)) {
596 LY_ARRAY_FREE(*idents);
597 *idents = NULL;
598 }
599
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100600 /* restart the whole process without this identity */
601 goto restart;
602 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200603 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100604
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200605 lysc_update_path(ctx, NULL, NULL);
606 return LY_SUCCESS;
607}
608
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200609static void *
610lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
611{
612 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
613 if (substmts[u].stmt == stmt) {
614 return substmts[u].storage;
615 }
616 }
617 return NULL;
618}
619
620LY_ERR
Radek Krejci6b88a462021-02-17 12:39:34 +0100621lys_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 +0200622{
623 LY_ERR ret = LY_EVALID, r;
624 LY_ARRAY_COUNT_TYPE u;
625 struct lysp_stmt *stmt;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200626 void *parsed = NULL, **compiled = NULL;
627
628 /* check for invalid substatements */
Radek Krejci6b88a462021-02-17 12:39:34 +0100629 for (stmt = ext_p->child; stmt; stmt = stmt->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200630 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
631 continue;
632 }
Radek Krejci6b88a462021-02-17 12:39:34 +0100633 LY_ARRAY_FOR(ext->substmts, u) {
634 if (ext->substmts[u].stmt == stmt->kw) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200635 break;
636 }
637 }
Radek Krejci6b88a462021-02-17 12:39:34 +0100638 if (u == LY_ARRAY_COUNT(ext->substmts)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100639 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 +0100640 stmt->stmt, ext_p->name, ext_p->argument ? " " : "", ext_p->argument ? ext_p->argument : "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200641 goto cleanup;
642 }
643 }
644
645 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
646
Radek Krejci6b88a462021-02-17 12:39:34 +0100647 /* note into the compile context that we are processing extension now */
648 ctx->ext = ext;
649
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200650 /* keep order of the processing the same as the order in the defined substmts,
651 * 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 +0200652
Radek Krejci6b88a462021-02-17 12:39:34 +0100653 LY_ARRAY_FOR(ext->substmts, u) {
654 uint64_t stmt_counter = 0;
655
656 for (stmt = ext_p->child; stmt; stmt = stmt->next) {
657 if (ext->substmts[u].stmt != stmt->kw) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200658 continue;
659 }
660
Radek Krejci6b88a462021-02-17 12:39:34 +0100661 parsed = NULL;
662 stmt_counter++;
663 if (ext->substmts[u].storage) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200664 switch (stmt->kw) {
Radek Krejci6b88a462021-02-17 12:39:34 +0100665 case LY_STMT_ACTION:
666 case LY_STMT_ANYDATA:
667 case LY_STMT_ANYXML:
668 case LY_STMT_CONTAINER:
669 case LY_STMT_CHOICE:
670 case LY_STMT_LEAF:
671 case LY_STMT_LEAF_LIST:
672 case LY_STMT_LIST:
673 case LY_STMT_NOTIFICATION:
674 case LY_STMT_RPC:
675 case LY_STMT_USES:
676 r = lysp_stmt_parse(ctx, stmt, &parsed, NULL);
677 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
678
679 /* set storage as an alternative document root in the compile context */
680 r = lys_compile_node(ctx, parsed, NULL, 0, NULL);
681 lysp_node_free(ctx->ctx, parsed);
682 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200683 break;
Radek Krejci1b2eef82021-02-17 11:17:27 +0100684 case LY_STMT_DESCRIPTION:
685 case LY_STMT_REFERENCE:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200686 case LY_STMT_UNITS: {
Radek Krejci6b88a462021-02-17 12:39:34 +0100687 const char **str_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200688
Radek Krejci6b88a462021-02-17 12:39:34 +0100689 if (ext->substmts[u].cardinality < LY_STMT_CARD_SOME) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200690 /* single item */
Radek Krejci6b88a462021-02-17 12:39:34 +0100691 if (*((const char **)ext->substmts[u].storage)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100692 LOGVAL(ctx->ctx, LY_VCODE_DUPSTMT, stmt->stmt);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200693 goto cleanup;
694 }
Radek Krejci6b88a462021-02-17 12:39:34 +0100695 str_p = (const char **)ext->substmts[u].storage;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200696 } else {
697 /* sized array */
Radek Krejci6b88a462021-02-17 12:39:34 +0100698 const char ***strings_array = (const char ***)ext->substmts[u].storage;
699 LY_ARRAY_NEW_GOTO(ctx->ctx, *strings_array, str_p, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200700 }
Radek Krejci6b88a462021-02-17 12:39:34 +0100701 r = lydict_insert(ctx->ctx, stmt->arg, 0, str_p);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200702 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
703 break;
704 }
Radek Krejci6b88a462021-02-17 12:39:34 +0100705 case LY_STMT_IF_FEATURE: {
706 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200707
Radek Krejci76c8c4e2021-02-17 10:16:48 +0100708 r = lysp_stmt_parse(ctx, stmt, &parsed, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200709 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejci6b88a462021-02-17 12:39:34 +0100710
711 r = lys_eval_iffeatures(ctx->ctx, parsed, &enabled);
712 FREE_ARRAY(ctx->ctx, (struct lysp_qname *)parsed, lysp_qname_free);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100713 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
714 if (!enabled) {
715 /* it is disabled, remove the whole extension instance */
716 return LY_ENOT;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200717 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200718 break;
Radek Krejci6b88a462021-02-17 12:39:34 +0100719 }
720 case LY_STMT_STATUS:
721 assert(ext->substmts[u].cardinality < LY_STMT_CARD_SOME);
722 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, &ext->substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
723 break;
724 case LY_STMT_TYPE: {
725 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, ext->substmts);
726 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, ext->substmts);
727
728 if (ext->substmts[u].cardinality < LY_STMT_CARD_SOME) {
729 /* single item */
730 if (*(struct lysc_type **)ext->substmts[u].storage) {
731 LOGVAL(ctx->ctx, LY_VCODE_DUPSTMT, stmt->stmt);
732 goto cleanup;
733 }
734 compiled = ext->substmts[u].storage;
735 } else {
736 /* sized array */
737 struct lysc_type ***types = (struct lysc_type ***)ext->substmts[u].storage, **type = NULL;
738 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
739 compiled = (void *)type;
740 }
741
742 r = lysp_stmt_parse(ctx, stmt, &parsed, NULL);
743 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
744 r = lys_compile_type(ctx, NULL, flags ? *flags : 0, ext_p->name, parsed, (struct lysc_type **)compiled,
745 units && !*units ? units : NULL, NULL);
746 lysp_type_free(ctx->ctx, parsed);
747 free(parsed);
748 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
749 break;
750 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200751 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
752 * also note that in many statements their extensions are not taken into account */
753 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100754 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 +0100755 stmt->stmt, ext_p->name, ext_p->argument ? " " : "", ext_p->argument ? ext_p->argument : "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200756 goto cleanup;
757 }
758 }
759 }
760
Radek Krejci6b88a462021-02-17 12:39:34 +0100761 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 +0100762 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
Radek Krejci6b88a462021-02-17 12:39:34 +0100763 ly_stmt2str(ext->substmts[u].stmt), ext_p->name, ext_p->argument ? " " : "", ext_p->argument ? ext_p->argument : "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200764 goto cleanup;
765 }
766 }
767
768 ret = LY_SUCCESS;
769
770cleanup:
Radek Krejci6b88a462021-02-17 12:39:34 +0100771 ctx->ext = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200772 return ret;
773}
774
775/**
776 * @brief Check when for cyclic dependencies.
777 *
778 * @param[in] set Set with all the referenced nodes.
779 * @param[in] node Node whose "when" referenced nodes are in @p set.
780 * @return LY_ERR value
781 */
782static LY_ERR
783lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
784{
785 struct lyxp_set tmp_set;
786 struct lyxp_set_scnode *xp_scnode;
787 uint32_t i, j;
788 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200789 LY_ERR ret = LY_SUCCESS;
790
791 memset(&tmp_set, 0, sizeof tmp_set);
792
793 /* prepare in_ctx of the set */
794 for (i = 0; i < set->used; ++i) {
795 xp_scnode = &set->val.scnodes[i];
796
Radek Krejcif13b87b2020-12-01 22:02:17 +0100797 if (xp_scnode->in_ctx != LYXP_SET_SCNODE_START_USED) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200798 /* check node when, skip the context node (it was just checked) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100799 xp_scnode->in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200800 }
801 }
802
803 for (i = 0; i < set->used; ++i) {
804 xp_scnode = &set->val.scnodes[i];
Radek Krejcif13b87b2020-12-01 22:02:17 +0100805 if (xp_scnode->in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200806 /* already checked */
807 continue;
808 }
809
810 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) ||
Radek Krejci9a3823e2021-01-27 20:26:46 +0100811 !lysc_node_when(xp_scnode->scnode)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200812 /* no when to check */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100813 xp_scnode->in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200814 continue;
815 }
816
817 node = xp_scnode->scnode;
818 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100819 struct lysc_when **when_list, *when;
820
Radek Krejciddace2c2021-01-08 11:30:56 +0100821 LOG_LOCSET(node, NULL, NULL, NULL);
Radek Krejci9a3823e2021-01-27 20:26:46 +0100822 when_list = lysc_node_when(node);
823 LY_ARRAY_FOR(when_list, u) {
824 when = when_list[u];
Michal Vasko400e9672021-01-11 13:39:17 +0100825 ret = lyxp_atomize(set->ctx, when->cond, node->module, LY_PREF_SCHEMA_RESOLVED, when->prefixes,
826 when->context, &tmp_set, LYXP_SCNODE_SCHEMA);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200827 if (ret != LY_SUCCESS) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100828 LOGVAL(set->ctx, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200829 goto cleanup;
830 }
831
832 for (j = 0; j < tmp_set.used; ++j) {
833 /* skip roots'n'stuff */
834 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
835 /* try to find this node in our set */
836 uint32_t idx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100837 if (lyxp_set_scnode_contains(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1, &idx) &&
838 (set->val.scnodes[idx].in_ctx == LYXP_SET_SCNODE_START_USED)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100839 LOGVAL(set->ctx, LYVE_SEMANTICS, "When condition includes a self-reference.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200840 ret = LY_EVALID;
841 goto cleanup;
842 }
843
844 /* needs to be checked, if in both sets, will be ignored */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100845 tmp_set.val.scnodes[j].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200846 } else {
847 /* no when, nothing to check */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100848 tmp_set.val.scnodes[j].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200849 }
850 }
851
852 /* merge this set into the global when set */
853 lyxp_set_scnode_merge(set, &tmp_set);
854 }
855
856 /* check when of non-data parents as well */
857 node = node->parent;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100858
Radek Krejciddace2c2021-01-08 11:30:56 +0100859 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200860 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
861
862 /* this node when was checked (xp_scnode could have been reallocd) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100863 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200864 }
865
866cleanup:
867 lyxp_set_free_content(&tmp_set);
868 return ret;
869}
870
871LY_ERR
872lysc_check_status(struct lysc_ctx *ctx, uint16_t flags1, void *mod1, const char *name1, uint16_t flags2, void *mod2,
873 const char *name2)
874{
875 uint16_t flg1, flg2;
876
877 flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
878 flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
879
880 if ((flg1 < flg2) && (mod1 == mod2)) {
881 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100882 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200883 "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".",
884 flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1,
885 flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2);
886 }
887 return LY_EVALID;
888 }
889
890 return LY_SUCCESS;
891}
892
Michal Vasko25d6ad02020-10-22 12:20:22 +0200893LY_ERR
894lys_compile_expr_implement(const struct ly_ctx *ctx, const struct lyxp_expr *expr, LY_PREFIX_FORMAT format,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100895 void *prefix_data, ly_bool implement, struct lys_glob_unres *unres, const struct lys_module **mod_p)
Michal Vaskocfaff232020-10-20 09:35:14 +0200896{
897 uint32_t i;
898 const char *ptr, *start;
899 const struct lys_module *mod;
900
Michal Vasko25d6ad02020-10-22 12:20:22 +0200901 assert(implement || mod_p);
902
Michal Vaskocfaff232020-10-20 09:35:14 +0200903 for (i = 0; i < expr->used; ++i) {
904 if ((expr->tokens[i] != LYXP_TOKEN_NAMETEST) && (expr->tokens[i] != LYXP_TOKEN_LITERAL)) {
905 /* token cannot have a prefix */
906 continue;
907 }
908
909 start = expr->expr + expr->tok_pos[i];
910 if (!(ptr = ly_strnchr(start, ':', expr->tok_len[i]))) {
911 /* token without a prefix */
912 continue;
913 }
914
915 if (!(mod = ly_resolve_prefix(ctx, start, ptr - start, format, prefix_data))) {
916 /* unknown prefix, do not care right now */
917 continue;
918 }
919
920 if (!mod->implemented) {
921 /* unimplemented module found */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200922 if (implement) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100923 LY_CHECK_RET(lys_set_implemented_r((struct lys_module *)mod, NULL, unres));
Michal Vasko25d6ad02020-10-22 12:20:22 +0200924 } else {
Michal Vaskocfaff232020-10-20 09:35:14 +0200925 *mod_p = mod;
Michal Vasko25d6ad02020-10-22 12:20:22 +0200926 break;
Michal Vaskocfaff232020-10-20 09:35:14 +0200927 }
Michal Vaskocfaff232020-10-20 09:35:14 +0200928 }
929 }
930
Michal Vasko25d6ad02020-10-22 12:20:22 +0200931 return LY_SUCCESS;
Michal Vaskocfaff232020-10-20 09:35:14 +0200932}
933
934/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200935 * @brief Check when/must expressions of a node on a complete compiled schema tree.
936 *
937 * @param[in] ctx Compile context.
938 * @param[in] node Node to check.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100939 * @param[in,out] unres Global unres structure.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200940 * @return LY_ERR value
941 */
942static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +0100943lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200944{
945 struct lyxp_set tmp_set;
946 uint32_t i, opts;
947 LY_ARRAY_COUNT_TYPE u;
948 ly_bool input_done = 0;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100949 struct lysc_when **whens = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200950 struct lysc_must *musts = NULL;
951 LY_ERR ret = LY_SUCCESS;
Michal Vaskocfaff232020-10-20 09:35:14 +0200952 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200953
Radek Krejciddace2c2021-01-08 11:30:56 +0100954 LOG_LOCSET(node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100955
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200956 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100957 opts = LYXP_SCNODE_SCHEMA | ((node->flags & LYS_IS_OUTPUT) ? LYXP_SCNODE_OUTPUT : 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200958
Radek Krejci9a3823e2021-01-27 20:26:46 +0100959 whens = lysc_node_when(node);
960 musts = lysc_node_musts(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200961
Radek Krejci9a3823e2021-01-27 20:26:46 +0100962 LY_ARRAY_FOR(whens, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +0200963 /* first check whether all the referenced modules are implemented */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200964 mod = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100965 ret = lys_compile_expr_implement(ctx->ctx, whens[u]->cond, LY_PREF_SCHEMA_RESOLVED, whens[u]->prefixes,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100966 ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, unres, &mod);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200967 if (ret) {
968 goto cleanup;
969 } else if (mod) {
Michal Vaskocfaff232020-10-20 09:35:14 +0200970 LOGWRN(ctx->ctx, "When condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
Radek Krejci9a3823e2021-01-27 20:26:46 +0100971 whens[u]->cond->expr, mod->name);
Michal Vaskocfaff232020-10-20 09:35:14 +0200972 continue;
973 }
974
975 /* check "when" */
Radek Krejci9a3823e2021-01-27 20:26:46 +0100976 ret = lyxp_atomize(ctx->ctx, whens[u]->cond, node->module, LY_PREF_SCHEMA_RESOLVED, whens[u]->prefixes,
977 whens[u]->context, &tmp_set, opts);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200978 if (ret) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100979 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid when condition \"%s\".", whens[u]->cond->expr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200980 goto cleanup;
981 }
982
983 ctx->path[0] = '\0';
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100984 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200985 for (i = 0; i < tmp_set.used; ++i) {
986 /* skip roots'n'stuff */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100987 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 +0200988 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
989
990 /* XPath expression cannot reference "lower" status than the node that has the definition */
Radek Krejci9a3823e2021-01-27 20:26:46 +0100991 ret = lysc_check_status(ctx, whens[u]->flags, node->module, node->name, schema->flags, schema->module,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200992 schema->name);
993 LY_CHECK_GOTO(ret, cleanup);
994
995 /* check dummy node accessing */
996 if (schema == node) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100997 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "When condition is accessing its own conditional node.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200998 ret = LY_EVALID;
999 goto cleanup;
1000 }
1001 }
1002 }
1003
1004 /* check cyclic dependencies */
1005 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
1006 LY_CHECK_GOTO(ret, cleanup);
1007
1008 lyxp_set_free_content(&tmp_set);
1009 }
1010
1011check_musts:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001012 LY_ARRAY_FOR(musts, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +02001013 /* first check whether all the referenced modules are implemented */
Michal Vasko25d6ad02020-10-22 12:20:22 +02001014 mod = NULL;
1015 ret = lys_compile_expr_implement(ctx->ctx, musts[u].cond, LY_PREF_SCHEMA_RESOLVED, musts[u].prefixes,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001016 ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, unres, &mod);
Michal Vasko25d6ad02020-10-22 12:20:22 +02001017 if (ret) {
1018 goto cleanup;
1019 } else if (mod) {
Michal Vaskocfaff232020-10-20 09:35:14 +02001020 LOGWRN(ctx->ctx, "Must condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
1021 musts[u].cond->expr, mod->name);
1022 continue;
1023 }
1024
1025 /* check "must" */
Michal Vasko400e9672021-01-11 13:39:17 +01001026 ret = lyxp_atomize(ctx->ctx, musts[u].cond, node->module, LY_PREF_SCHEMA_RESOLVED, musts[u].prefixes, node,
1027 &tmp_set, opts);
Michal Vasko25d6ad02020-10-22 12:20:22 +02001028 if (ret) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001029 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001030 goto cleanup;
1031 }
1032
1033 ctx->path[0] = '\0';
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001034 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001035 for (i = 0; i < tmp_set.used; ++i) {
1036 /* skip roots'n'stuff */
1037 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
1038 /* XPath expression cannot reference "lower" status than the node that has the definition */
1039 ret = lysc_check_status(ctx, node->flags, node->module, node->name, tmp_set.val.scnodes[i].scnode->flags,
1040 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
1041 LY_CHECK_GOTO(ret, cleanup);
1042 }
1043 }
1044
1045 lyxp_set_free_content(&tmp_set);
1046 }
1047
1048 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
1049 /* now check output musts */
1050 input_done = 1;
Radek Krejci9a3823e2021-01-27 20:26:46 +01001051 whens = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001052 musts = ((struct lysc_node_action *)node)->output.musts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001053 opts = LYXP_SCNODE_OUTPUT;
1054 goto check_musts;
1055 }
1056
1057cleanup:
1058 lyxp_set_free_content(&tmp_set);
Radek Krejciddace2c2021-01-08 11:30:56 +01001059 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001060 return ret;
1061}
1062
1063/**
1064 * @brief Check leafref for its target existence on a complete compiled schema tree.
1065 *
1066 * @param[in] ctx Compile context.
1067 * @param[in] node Context node for the leafref.
1068 * @param[in] lref Leafref to check/resolve.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001069 * @param[in,out] unres Global unres structure.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001070 * @return LY_ERR value.
1071 */
1072static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001073lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref,
1074 struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001075{
Michal Vaskod1e53b92021-01-28 13:11:06 +01001076 const struct lysc_node *target = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001077 struct ly_path *p;
1078 struct lysc_type *type;
1079
1080 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
1081
1082 /* try to find the target */
Michal Vaskod1e53b92021-01-28 13:11:06 +01001083 LY_CHECK_RET(ly_path_compile(ctx->ctx, lref->cur_mod, node, lref->path, LY_PATH_LREF_TRUE,
1084 (node->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
1085 LY_PREF_SCHEMA_RESOLVED, lref->prefixes, unres, &p));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001086
1087 /* get the target node */
1088 target = p[LY_ARRAY_COUNT(p) - 1].node;
1089 ly_path_free(node->module->ctx, p);
1090
1091 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001092 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 +02001093 lref->path->expr, lys_nodetype2str(target->nodetype));
1094 return LY_EVALID;
1095 }
1096
1097 /* check status */
1098 ctx->path[0] = '\0';
1099 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1100 ctx->path_len = strlen(ctx->path);
1101 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
1102 return LY_EVALID;
1103 }
1104 ctx->path_len = 1;
1105 ctx->path[1] = '\0';
1106
1107 /* check config */
1108 if (lref->require_instance) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01001109 if ((node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001110 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001111 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
1112 return LY_EVALID;
1113 }
1114 }
1115
1116 /* store the target's type and check for circular chain of leafrefs */
1117 lref->realtype = ((struct lysc_node_leaf *)target)->type;
1118 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
1119 if (type == (struct lysc_type *)lref) {
1120 /* circular chain detected */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001121 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid leafref path \"%s\" - circular chain of leafrefs detected.",
1122 lref->path->expr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001123 return LY_EVALID;
1124 }
1125 }
1126
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001127 /* TODO check if leafref and its target are under common if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001128
1129 return LY_SUCCESS;
1130}
1131
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001132/**
1133 * @brief Compile default value(s) for leaf or leaf-list expecting a complete compiled schema tree.
1134 *
1135 * @param[in] ctx Compile context.
1136 * @param[in] node Leaf or leaf-list to compile the default value(s) for.
1137 * @param[in] type Type of the default value.
1138 * @param[in] dflt Default value.
1139 * @param[in] dflt_pmod Parsed module of the @p dflt to resolve possible prefixes.
1140 * @param[in,out] storage Storage for the compiled default value.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001141 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001142 * @return LY_ERR value.
1143 */
1144static LY_ERR
1145lys_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 +01001146 const struct lysp_module *dflt_pmod, struct lyd_value *storage, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001147{
1148 LY_ERR ret;
Michal Vasko25d6ad02020-10-22 12:20:22 +02001149 uint32_t options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001150 struct ly_err_item *err = NULL;
1151
Michal Vasko25d6ad02020-10-22 12:20:22 +02001152 options = (ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED) ? LY_TYPE_STORE_IMPLEMENT : 0;
1153 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), options, LY_PREF_SCHEMA, (void *)dflt_pmod,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001154 LYD_HINT_SCHEMA, node, storage, unres, &err);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001155 if (ret == LY_EINCOMPLETE) {
1156 /* we have no data so we will not be resolving it */
1157 ret = LY_SUCCESS;
1158 }
1159
1160 if (ret) {
Radek Krejciddace2c2021-01-08 11:30:56 +01001161 LOG_LOCSET(node, NULL, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001162 if (err) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001163 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid default - value does not fit the type (%s).", err->msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001164 ly_err_free(err);
1165 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001166 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid default - value does not fit the type.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001167 }
Radek Krejciddace2c2021-01-08 11:30:56 +01001168 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001169 return ret;
1170 }
1171
1172 ++((struct lysc_type *)storage->realtype)->refcount;
1173 return LY_SUCCESS;
1174}
1175
1176/**
1177 * @brief Compile default value of a leaf expecting a complete compiled schema tree.
1178 *
1179 * @param[in] ctx Compile context.
1180 * @param[in] leaf Leaf that the default value is for.
1181 * @param[in] dflt Default value to compile.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001182 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001183 * @return LY_ERR value.
1184 */
1185static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001186lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt,
1187 struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001188{
1189 LY_ERR ret;
1190
1191 assert(!leaf->dflt);
1192
1193 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
1194 /* ignore default values for keys and mandatory leaves */
1195 return LY_SUCCESS;
1196 }
1197
1198 /* allocate the default value */
1199 leaf->dflt = calloc(1, sizeof *leaf->dflt);
1200 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
1201
1202 /* store the default value */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001203 ret = lys_compile_unres_dflt(ctx, &leaf->node, leaf->type, dflt->str, dflt->mod, leaf->dflt, unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001204 if (ret) {
1205 free(leaf->dflt);
1206 leaf->dflt = NULL;
1207 }
1208
1209 return ret;
1210}
1211
1212/**
1213 * @brief Compile default values of a leaf-list expecting a complete compiled schema tree.
1214 *
1215 * @param[in] ctx Compile context.
1216 * @param[in] llist Leaf-list that the default value(s) are for.
1217 * @param[in] dflt Default value to compile, in case of a single value.
1218 * @param[in] dflts Sized array of default values, in case of more values.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001219 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001220 * @return LY_ERR value.
1221 */
1222static LY_ERR
1223lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflt,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001224 struct lysp_qname *dflts, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001225{
1226 LY_ERR ret;
1227 LY_ARRAY_COUNT_TYPE orig_count, u, v;
1228
1229 assert(dflt || dflts);
1230
Radek Krejcic7d13e32020-12-09 12:32:24 +01001231 /* in case there were already some defaults and we are adding new by deviations */
1232 orig_count = LY_ARRAY_COUNT(llist->dflts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001233
1234 /* allocate new items */
Radek Krejcic7d13e32020-12-09 12:32:24 +01001235 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 +02001236
1237 /* fill each new default value */
1238 if (dflts) {
1239 LY_ARRAY_FOR(dflts, u) {
1240 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001241 ret = lys_compile_unres_dflt(ctx, &llist->node, llist->type, dflts[u].str, dflts[u].mod,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001242 llist->dflts[orig_count + u], unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001243 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
1244 LY_ARRAY_INCREMENT(llist->dflts);
1245 }
1246 } else {
1247 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001248 ret = lys_compile_unres_dflt(ctx, &llist->node, llist->type, dflt->str, dflt->mod,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001249 llist->dflts[orig_count], unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001250 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
1251 LY_ARRAY_INCREMENT(llist->dflts);
1252 }
1253
1254 /* check default value uniqueness */
1255 if (llist->flags & LYS_CONFIG_W) {
1256 /* configuration data values must be unique - so check the default values */
1257 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
1258 for (v = 0; v < u; ++v) {
1259 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
Radek Krejcia6016992021-03-03 10:13:41 +01001260 lysc_update_path(ctx, llist->parent ? llist->parent->module : NULL, llist->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001261 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration leaf-list has multiple defaults of the same value \"%s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001262 llist->dflts[u]->canonical);
1263 lysc_update_path(ctx, NULL, NULL);
1264 return LY_EVALID;
1265 }
1266 }
1267 }
1268 }
1269
1270 return LY_SUCCESS;
1271}
1272
Michal Vasko405cc9e2020-12-01 12:01:27 +01001273LY_ERR
1274lys_compile_unres_glob(struct ly_ctx *ctx, struct lys_glob_unres *unres)
1275{
Radek Krejci2efc45b2020-12-22 16:25:44 +01001276 LY_ERR ret;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001277 struct lysc_node *node;
1278 struct lysc_type *type, *typeiter;
1279 struct lysc_type_leafref *lref;
1280 struct lysc_ctx cctx = {0};
1281 LY_ARRAY_COUNT_TYPE v;
1282 uint32_t i;
1283
1284 if (unres->recompile) {
1285 /* recompile all the modules and resolve the new unres instead (during recompilation) */
1286 unres->recompile = 0;
1287 return lys_recompile(ctx, 1);
1288 }
1289
1290 /* fake compile context */
1291 cctx.ctx = ctx;
1292 cctx.path_len = 1;
1293 cctx.path[0] = '/';
1294
1295 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
1296 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
1297 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
1298 for (i = 0; i < unres->leafrefs.count; ++i) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001299 LY_ERR ret = LY_SUCCESS;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001300 node = unres->leafrefs.objs[i];
1301 cctx.cur_mod = node->module;
1302 cctx.pmod = node->module->parsed;
1303
Radek Krejciddace2c2021-01-08 11:30:56 +01001304 LOG_LOCSET(node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001305
Michal Vasko405cc9e2020-12-01 12:01:27 +01001306 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
1307 type = ((struct lysc_node_leaf *)node)->type;
1308 if (type->basetype == LY_TYPE_LEAFREF) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001309 ret = lys_compile_unres_leafref(&cctx, node, (struct lysc_type_leafref *)type, unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001310 } else if (type->basetype == LY_TYPE_UNION) {
1311 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
1312 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
1313 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
Radek Krejci2efc45b2020-12-22 16:25:44 +01001314 ret = lys_compile_unres_leafref(&cctx, node, lref, unres);
1315 if (ret) {
1316 break;
1317 }
Michal Vasko405cc9e2020-12-01 12:01:27 +01001318 }
1319 }
1320 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001321
Radek Krejciddace2c2021-01-08 11:30:56 +01001322 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001323 if (ret) {
1324 return ret;
1325 }
Michal Vasko405cc9e2020-12-01 12:01:27 +01001326 }
1327 while (unres->leafrefs.count) {
1328 node = unres->leafrefs.objs[unres->leafrefs.count - 1];
1329 cctx.cur_mod = node->module;
1330 cctx.pmod = node->module->parsed;
1331
Radek Krejciddace2c2021-01-08 11:30:56 +01001332 LOG_LOCSET(node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001333
Michal Vasko405cc9e2020-12-01 12:01:27 +01001334 /* store pointer to the real type */
1335 type = ((struct lysc_node_leaf *)node)->type;
1336 if (type->basetype == LY_TYPE_LEAFREF) {
1337 for (typeiter = ((struct lysc_type_leafref *)type)->realtype;
1338 typeiter->basetype == LY_TYPE_LEAFREF;
1339 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
1340 ((struct lysc_type_leafref *)type)->realtype = typeiter;
1341 } else if (type->basetype == LY_TYPE_UNION) {
1342 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
1343 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
1344 for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype;
1345 typeiter->basetype == LY_TYPE_LEAFREF;
1346 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
1347 ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter;
1348 }
1349 }
1350 }
Radek Krejciddace2c2021-01-08 11:30:56 +01001351 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001352
1353 ly_set_rm_index(&unres->leafrefs, unres->leafrefs.count - 1, NULL);
1354 }
1355
1356 /* check xpath */
1357 while (unres->xpath.count) {
1358 node = unres->xpath.objs[unres->xpath.count - 1];
1359 cctx.cur_mod = node->module;
1360 cctx.pmod = node->module->parsed;
1361
Radek Krejciddace2c2021-01-08 11:30:56 +01001362 LOG_LOCSET(node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001363
1364 ret = lys_compile_unres_xpath(&cctx, node, unres);
Radek Krejciddace2c2021-01-08 11:30:56 +01001365 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001366 LY_CHECK_RET(ret);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001367
1368 ly_set_rm_index(&unres->xpath, unres->xpath.count - 1, NULL);
1369 }
1370
1371 /* finish incomplete default values compilation */
1372 while (unres->dflts.count) {
1373 struct lysc_unres_dflt *r = unres->dflts.objs[unres->dflts.count - 1];
1374 cctx.cur_mod = r->leaf->module;
1375 cctx.pmod = r->leaf->module->parsed;
1376
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001377 LOG_LOCSET(&r->leaf->node, NULL, NULL, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001378
Radek Krejci2efc45b2020-12-22 16:25:44 +01001379 if (r->leaf->nodetype == LYS_LEAF) {
1380 ret = lys_compile_unres_leaf_dlft(&cctx, r->leaf, r->dflt, unres);
1381 } else {
1382 ret = lys_compile_unres_llist_dflts(&cctx, r->llist, r->dflt, r->dflts, unres);
1383 }
Radek Krejciddace2c2021-01-08 11:30:56 +01001384 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001385 LY_CHECK_RET(ret);
1386
1387 lysc_unres_dflt_free(ctx, r);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001388 ly_set_rm_index(&unres->dflts, unres->dflts.count - 1, NULL);
1389 }
1390
1391 /* some unres items may have been added */
1392 if (unres->leafrefs.count || unres->xpath.count || unres->dflts.count) {
1393 return lys_compile_unres_glob(ctx, unres);
1394 }
1395
1396 return LY_SUCCESS;
1397}
1398
1399void
1400lys_compile_unres_glob_revert(struct ly_ctx *ctx, struct lys_glob_unres *unres)
1401{
1402 uint32_t i;
1403 struct lys_module *m;
1404
1405 for (i = 0; i < unres->implementing.count; ++i) {
1406 m = unres->implementing.objs[i];
1407 assert(m->implemented);
1408
1409 /* make the module correctly non-implemented again */
1410 m->implemented = 0;
1411 lys_precompile_augments_deviations_revert(ctx, m);
1412 }
1413
1414 for (i = 0; i < unres->creating.count; ++i) {
1415 m = unres->creating.objs[i];
1416
1417 /* remove the module from the context and free it */
1418 ly_set_rm(&ctx->list, m, NULL);
1419 lys_module_free(m, NULL);
1420 }
1421
1422 if (unres->implementing.count) {
1423 /* recompile because some implemented modules are no longer implemented */
1424 lys_recompile(ctx, 0);
1425 }
1426}
1427
1428void
1429lys_compile_unres_glob_erase(const struct ly_ctx *ctx, struct lys_glob_unres *unres)
1430{
1431 uint32_t i;
1432
1433 ly_set_erase(&unres->implementing, NULL);
1434 ly_set_erase(&unres->creating, NULL);
1435 for (i = 0; i < unres->dflts.count; ++i) {
1436 lysc_unres_dflt_free(ctx, unres->dflts.objs[i]);
1437 }
1438 ly_set_erase(&unres->dflts, NULL);
1439 ly_set_erase(&unres->xpath, NULL);
1440 ly_set_erase(&unres->leafrefs, NULL);
1441}
1442
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001443/**
Michal Vasko405cc9e2020-12-01 12:01:27 +01001444 * @brief Finish compilation of all the module unres sets in a compile context.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001445 *
1446 * @param[in] ctx Compile context with unres sets.
1447 * @return LY_ERR value.
1448 */
1449static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001450lys_compile_unres_mod(struct lysc_ctx *ctx)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001451{
1452 struct lysc_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001453 struct lysc_augment *aug;
1454 struct lysc_deviation *dev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001455 uint32_t i;
1456
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001457 /* remove all disabled nodes */
1458 for (i = 0; i < ctx->disabled.count; ++i) {
1459 node = ctx->disabled.snodes[i];
1460 if (node->flags & LYS_KEY) {
Radek Krejciddace2c2021-01-08 11:30:56 +01001461 LOG_LOCSET(node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001462 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Key \"%s\" is disabled by its if-features.", node->name);
Radek Krejciddace2c2021-01-08 11:30:56 +01001463 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001464 return LY_EVALID;
1465 }
1466
Radek Krejci2a9fc652021-01-22 17:44:34 +01001467 lysc_node_free(ctx->ctx, node, 1);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001468 }
1469
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001470 /* check that all augments were applied */
1471 for (i = 0; i < ctx->augs.count; ++i) {
1472 aug = ctx->augs.objs[i];
Radek Krejci2efc45b2020-12-22 16:25:44 +01001473 lysc_update_path(ctx, NULL, "{augment}");
1474 lysc_update_path(ctx, NULL, aug->nodeid->expr);
1475 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" from module \"%s\" was not found.",
Michal Vasko28fe5f62021-03-03 16:37:39 +01001476 aug->nodeid->expr, LYSP_MODULE_NAME(aug->aug_pmod));
Radek Krejci2efc45b2020-12-22 16:25:44 +01001477 lysc_update_path(ctx, NULL, NULL);
1478 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001479 }
1480 if (ctx->augs.count) {
1481 return LY_ENOTFOUND;
1482 }
1483
1484 /* check that all deviations were applied */
1485 for (i = 0; i < ctx->devs.count; ++i) {
1486 dev = ctx->devs.objs[i];
Radek Krejci2efc45b2020-12-22 16:25:44 +01001487 lysc_update_path(ctx, NULL, "{deviation}");
1488 lysc_update_path(ctx, NULL, dev->nodeid->expr);
1489 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Deviation(s) target node \"%s\" from module \"%s\" was not found.",
1490 dev->nodeid->expr, LYSP_MODULE_NAME(dev->dev_pmods[0]));
1491 lysc_update_path(ctx, NULL, NULL);
1492 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001493 }
1494 if (ctx->devs.count) {
1495 return LY_ENOTFOUND;
1496 }
1497
1498 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001499}
1500
1501/**
Michal Vasko405cc9e2020-12-01 12:01:27 +01001502 * @brief Erase all the module unres sets in a compile context.
1503 *
1504 * @param[in] ctx Compile context with unres sets.
1505 * @param[in] error Whether the compilation finished with an error or not.
1506 */
1507static void
1508lys_compile_unres_mod_erase(struct lysc_ctx *ctx, ly_bool error)
1509{
1510 uint32_t i;
1511
1512 ly_set_erase(&ctx->groupings, NULL);
1513 ly_set_erase(&ctx->tpdf_chain, NULL);
1514 ly_set_erase(&ctx->disabled, NULL);
1515
1516 if (!error) {
1517 /* there can be no leftover deviations or augments */
1518 LY_CHECK_ERR_RET(ctx->augs.count, LOGINT(ctx->ctx), );
1519 LY_CHECK_ERR_RET(ctx->devs.count, LOGINT(ctx->ctx), );
1520
1521 ly_set_erase(&ctx->augs, NULL);
1522 ly_set_erase(&ctx->devs, NULL);
1523 ly_set_erase(&ctx->uses_augs, NULL);
1524 ly_set_erase(&ctx->uses_rfns, NULL);
1525 } else {
1526 for (i = 0; i < ctx->augs.count; ++i) {
1527 lysc_augment_free(ctx->ctx, ctx->augs.objs[i]);
1528 }
1529 ly_set_erase(&ctx->augs, NULL);
1530 for (i = 0; i < ctx->devs.count; ++i) {
1531 lysc_deviation_free(ctx->ctx, ctx->devs.objs[i]);
1532 }
1533 ly_set_erase(&ctx->devs, NULL);
1534 for (i = 0; i < ctx->uses_augs.count; ++i) {
1535 lysc_augment_free(ctx->ctx, ctx->uses_augs.objs[i]);
1536 }
1537 ly_set_erase(&ctx->uses_augs, NULL);
1538 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1539 lysc_refine_free(ctx->ctx, ctx->uses_rfns.objs[i]);
1540 }
1541 ly_set_erase(&ctx->uses_rfns, NULL);
1542 }
1543}
1544
1545/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001546 * @brief Compile identites in the current module and all its submodules.
1547 *
1548 * @param[in] ctx Compile context.
1549 * @return LY_ERR value.
1550 */
1551static LY_ERR
1552lys_compile_identities(struct lysc_ctx *ctx)
1553{
1554 struct lysp_submodule *submod;
1555 LY_ARRAY_COUNT_TYPE u;
1556
1557 if (!ctx->cur_mod->identities) {
1558 LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, ctx->cur_mod->parsed->identities, &ctx->cur_mod->identities));
1559 LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, u) {
1560 submod = ctx->cur_mod->parsed->includes[u].submodule;
1561 LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, submod->identities, &ctx->cur_mod->identities));
1562 }
1563 }
1564
1565 if (ctx->cur_mod->parsed->identities) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001566 LY_CHECK_RET(lys_compile_identities_derived(ctx, ctx->cur_mod->parsed->identities, &ctx->cur_mod->identities));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001567 }
1568 lysc_update_path(ctx, NULL, "{submodule}");
1569 LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, u) {
1570
1571 submod = ctx->cur_mod->parsed->includes[u].submodule;
1572 if (submod->identities) {
1573 lysc_update_path(ctx, NULL, submod->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001574 LY_CHECK_RET(lys_compile_identities_derived(ctx, submod->identities, &ctx->cur_mod->identities));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001575 lysc_update_path(ctx, NULL, NULL);
1576 }
1577 }
1578 lysc_update_path(ctx, NULL, NULL);
1579
1580 return LY_SUCCESS;
1581}
1582
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001583LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001584lys_recompile(struct ly_ctx *ctx, ly_bool log)
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001585{
1586 uint32_t idx;
1587 struct lys_module *mod;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001588 struct lys_glob_unres unres = {0};
1589 LY_ERR ret = LY_SUCCESS;
Radek Krejci430a5582020-12-01 13:35:18 +01001590 uint32_t prev_lo = 0;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001591
1592 if (!log) {
1593 /* recompile, must succeed because the modules were already compiled; hide messages because any
1594 * warnings were already printed, are not really relevant, and would hide the real error */
1595 prev_lo = ly_log_options(0);
1596 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001597
1598 /* free all the modules */
1599 for (idx = 0; idx < ctx->list.count; ++idx) {
1600 mod = ctx->list.objs[idx];
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001601 if (mod->compiled) {
1602 /* free the module */
1603 lysc_module_free(mod->compiled, NULL);
1604 mod->compiled = NULL;
1605 }
1606
1607 /* free precompiled iffeatures */
1608 lys_free_feature_iffeatures(mod->parsed);
1609 }
1610
1611 /* recompile all the modules */
1612 for (idx = 0; idx < ctx->list.count; ++idx) {
1613 mod = ctx->list.objs[idx];
Michal Vasko405cc9e2020-12-01 12:01:27 +01001614 if (!mod->implemented || mod->compiled) {
1615 /* nothing to do */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001616 continue;
1617 }
1618
Michal Vasko405cc9e2020-12-01 12:01:27 +01001619 /* recompile */
1620 ret = lys_compile(mod, 0, &unres);
1621 if (ret) {
1622 if (!log) {
1623 LOGERR(mod->ctx, ret, "Recompilation of module \"%s\" failed.", mod->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001624 }
Michal Vasko405cc9e2020-12-01 12:01:27 +01001625 goto cleanup;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001626 }
1627 }
1628
Michal Vasko405cc9e2020-12-01 12:01:27 +01001629 /* resolve global unres */
1630 LY_CHECK_GOTO(ret = lys_compile_unres_glob(ctx, &unres), cleanup);
1631
1632cleanup:
1633 if (!log) {
1634 ly_log_options(prev_lo);
1635 }
1636 lys_compile_unres_glob_erase(ctx, &unres);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001637 return ret;
1638}
1639
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001640LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001641lys_compile(struct lys_module *mod, uint32_t options, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001642{
1643 struct lysc_ctx ctx = {0};
1644 struct lysc_module *mod_c;
1645 struct lysp_module *sp;
1646 struct lysp_submodule *submod;
1647 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001648 struct lysp_node_grp *grp;
1649 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001650 LY_ERR ret = LY_SUCCESS;
1651
1652 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
1653
1654 if (!mod->implemented) {
1655 /* just imported modules are not compiled */
1656 return LY_SUCCESS;
1657 }
1658
1659 /* context will be changed */
1660 ++mod->ctx->module_set_id;
1661
1662 sp = mod->parsed;
1663
1664 ctx.ctx = mod->ctx;
1665 ctx.cur_mod = mod;
1666 ctx.pmod = sp;
1667 ctx.options = options;
1668 ctx.path_len = 1;
1669 ctx.path[0] = '/';
Michal Vasko405cc9e2020-12-01 12:01:27 +01001670 ctx.unres = unres;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001671
1672 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
1673 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
1674 mod_c->mod = mod;
1675
1676 /* process imports */
1677 LY_ARRAY_FOR(sp->imports, u) {
1678 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
1679 }
1680
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001681 /* identities */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001682 LY_CHECK_GOTO(ret = lys_compile_identities(&ctx), error);
1683
1684 /* augments and deviations */
1685 LY_CHECK_GOTO(ret = lys_precompile_augments_deviations(&ctx), error);
1686
1687 /* compile augments and deviations of our module from other modules so they can be applied during compilation */
1688 LY_CHECK_GOTO(ret = lys_precompile_own_augments(&ctx), error);
1689 LY_CHECK_GOTO(ret = lys_precompile_own_deviations(&ctx), error);
1690
1691 /* data nodes */
1692 LY_LIST_FOR(sp->data, pnode) {
1693 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), error);
1694 }
1695
Radek Krejci2a9fc652021-01-22 17:44:34 +01001696 /* top-level RPCs */
1697 LY_LIST_FOR((struct lysp_node *)sp->rpcs, pnode) {
1698 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), error);
1699 }
1700
1701 /* top-level notifications */
1702 LY_LIST_FOR((struct lysp_node *)sp->notifs, pnode) {
1703 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), error);
1704 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001705
1706 /* extension instances */
Radek Krejciab430862021-03-02 20:13:40 +01001707 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001708
1709 /* the same for submodules */
1710 LY_ARRAY_FOR(sp->includes, u) {
1711 submod = sp->includes[u].submodule;
1712 ctx.pmod = (struct lysp_module *)submod;
1713
1714 LY_LIST_FOR(submod->data, pnode) {
1715 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
1716 LY_CHECK_GOTO(ret, error);
1717 }
1718
Radek Krejci2a9fc652021-01-22 17:44:34 +01001719 LY_LIST_FOR((struct lysp_node *)submod->rpcs, pnode) {
1720 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
1721 LY_CHECK_GOTO(ret, error);
1722 }
1723
1724 LY_LIST_FOR((struct lysp_node *)submod->notifs, pnode) {
1725 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
1726 LY_CHECK_GOTO(ret, error);
1727 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001728
Radek Krejciab430862021-03-02 20:13:40 +01001729 COMPILE_EXTS_GOTO(&ctx, submod->exts, mod_c->exts, mod_c, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001730 }
Michal Vasko12606ee2020-11-25 17:05:11 +01001731 ctx.pmod = sp;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001732
1733 /* validate non-instantiated groupings from the parsed schema,
1734 * without it we would accept even the schemas with invalid grouping specification */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001735 ctx.options |= LYS_COMPILE_GROUPING;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001736 LY_LIST_FOR(sp->groupings, grp) {
1737 if (!(grp->flags & LYS_USED_GRP)) {
1738 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, grp), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001739 }
1740 }
1741 LY_LIST_FOR(sp->data, pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001742 LY_LIST_FOR((struct lysp_node_grp *)lysp_node_groupings(pnode), grp) {
1743 if (!(grp->flags & LYS_USED_GRP)) {
1744 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, grp), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001745 }
1746 }
1747 }
1748 LY_ARRAY_FOR(sp->includes, u) {
1749 submod = sp->includes[u].submodule;
1750 ctx.pmod = (struct lysp_module *)submod;
1751
Radek Krejci2a9fc652021-01-22 17:44:34 +01001752 LY_LIST_FOR(submod->groupings, grp) {
1753 if (!(grp->flags & LYS_USED_GRP)) {
1754 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, grp), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001755 }
1756 }
1757 LY_LIST_FOR(submod->data, pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001758 LY_LIST_FOR((struct lysp_node_grp *)lysp_node_groupings(pnode), grp) {
1759 if (!(grp->flags & LYS_USED_GRP)) {
1760 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, grp), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001761 }
1762 }
1763 }
1764 }
1765 ctx.pmod = sp;
1766
Radek Krejciddace2c2021-01-08 11:30:56 +01001767 LOG_LOCBACK(0, 0, 1, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001768
Michal Vasko405cc9e2020-12-01 12:01:27 +01001769 /* finish compilation for all unresolved module items in the context */
1770 LY_CHECK_GOTO(ret = lys_compile_unres_mod(&ctx), error);
Michal Vasko12606ee2020-11-25 17:05:11 +01001771
Michal Vasko405cc9e2020-12-01 12:01:27 +01001772 lys_compile_unres_mod_erase(&ctx, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001773 return LY_SUCCESS;
1774
1775error:
Radek Krejciddace2c2021-01-08 11:30:56 +01001776 LOG_LOCBACK(0, 0, 1, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001777 lys_precompile_augments_deviations_revert(ctx.ctx, mod);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001778 lys_compile_unres_mod_erase(&ctx, 1);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001779 lysc_module_free(mod_c, NULL);
1780 mod->compiled = NULL;
1781
1782 return ret;
1783}