blob: d2efc50a61892c797b96fca1cf449fbdc01b0dfa [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;
65 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
66
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:
77 return ret;
78}
79
80LY_ERR
81lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
82 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
83{
84 LY_ERR ret = LY_SUCCESS;
85 const char *name;
86 size_t u;
87 LY_ARRAY_COUNT_TYPE v;
88 const char *prefixed_name = NULL;
89
90 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret);
91 LY_CHECK_RET(ret);
92
93 ext->insubstmt = ext_p->insubstmt;
94 ext->insubstmt_index = ext_p->insubstmt_index;
95 ext->module = ctx->cur_mod;
96 ext->parent = parent;
97 ext->parent_type = parent_type;
98
99 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node *)ext->parent : NULL, "{extension}");
100
101 /* get module where the extension definition should be placed */
102 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {}
103 if (ext_p->yin) {
104 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
105 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
106 * YANG (import) prefix must be done here. */
107 if (!ly_strncmp(ctx->pmod->mod->ns, ext_p->name, u - 1)) {
108 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, &ext_p->name[u], 0, &prefixed_name), cleanup);
109 u = 0;
110 } else {
111 LY_ARRAY_FOR(ctx->pmod->imports, v) {
112 if (!ly_strncmp(ctx->pmod->imports[v].module->ns, ext_p->name, u - 1)) {
113 char *s;
114 LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->pmod->imports[v].prefix, &ext_p->name[u]) == -1,
115 ret = LY_EMEM, cleanup);
116 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->ctx, s, &prefixed_name), cleanup);
117 u = strlen(ctx->pmod->imports[v].prefix) + 1; /* add semicolon */
118 break;
119 }
120 }
121 }
122 if (!prefixed_name) {
123 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
124 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
125 ret = LY_EVALID;
126 goto cleanup;
127 }
128 } else {
129 prefixed_name = ext_p->name;
130 }
131 lysc_update_path(ctx, NULL, prefixed_name);
132
133 if (!ext_mod) {
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100134 ext_mod = u ? ly_resolve_prefix(ctx->ctx, prefixed_name, u - 1, LY_PREF_SCHEMA, ctx->pmod) : ctx->pmod->mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200135 if (!ext_mod) {
136 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
137 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
138 ret = LY_EVALID;
139 goto cleanup;
140 } else if (!ext_mod->parsed->extensions) {
141 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
142 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
143 prefixed_name, ext_mod->name);
144 ret = LY_EVALID;
145 goto cleanup;
146 }
147 }
148 name = &prefixed_name[u];
149
150 /* find the parsed extension definition there */
151 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
152 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
153 /* compile extension definition and assign it */
154 LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def), cleanup);
155 break;
156 }
157 }
158 if (!ext->def) {
159 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
160 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
161 ret = LY_EVALID;
162 goto cleanup;
163 }
164
165 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
166 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
167 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
168 if (ext_p->yin && ext->def->argument && !ext->argument) {
169 /* Schema was parsed from YIN and an argument is expected, ... */
170 struct lysp_stmt *stmt = NULL;
171
172 if (ext->def->flags & LYS_YINELEM_TRUE) {
173 /* ... argument was the first XML child element */
174 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
175 /* TODO check namespace of the statement */
176 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
177 stmt = ext_p->child;
178 }
179 }
180 } else {
181 /* ... argument was one of the XML attributes which are represented as child stmt
182 * with LYS_YIN_ATTR flag */
183 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
184 if (!strcmp(stmt->stmt, ext->def->argument)) {
185 /* this is the extension's argument */
186 break;
187 }
188 }
189 }
190 if (!stmt) {
191 /* missing extension's argument */
192 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
193 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
194 ret = LY_EVALID;
195 goto cleanup;
196
197 }
198 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup);
199 stmt->flags |= LYS_YIN_ARGUMENT;
200 }
201 if (prefixed_name != ext_p->name) {
202 lydict_remove(ctx->ctx, ext_p->name);
203 ext_p->name = prefixed_name;
204 if (!ext_p->argument && ext->argument) {
205 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, ext->argument, 0, &ext_p->argument), cleanup);
206 }
207 }
208
209 if (ext->def->plugin && ext->def->plugin->compile) {
210 if (ext->argument) {
211 lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument);
212 }
213 LY_CHECK_GOTO(ret = ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
214 if (ext->argument) {
215 lysc_update_path(ctx, NULL, NULL);
216 }
217 }
218 ext_p->compiled = ext;
219
220cleanup:
221 if (prefixed_name && (prefixed_name != ext_p->name)) {
222 lydict_remove(ctx->ctx, prefixed_name);
223 }
224
225 lysc_update_path(ctx, NULL, NULL);
226 lysc_update_path(ctx, NULL, NULL);
227
228 return ret;
229}
230
231struct lysc_ext *
232lysc_ext_dup(struct lysc_ext *orig)
233{
234 ++orig->refcount;
235 return orig;
236}
237
238static 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
252lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
253{
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) {
279 if (!parent && (ctx->path[ctx->path_len - 1] == '}') && (ctx->path[ctx->path_len - 2] != '\'')) {
280 /* 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) {
290 if ((parent && (parent->module == ctx->cur_mod)) || (!parent && (ctx->path_len > 1) && (name[0] == '{'))) {
291 /* 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 }
306}
307
308/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200309 * @brief Compile information in the import statement - make sure there is the target module
310 * @param[in] ctx Compile context.
311 * @param[in] imp_p The parsed import statement structure to fill the module to.
312 * @return LY_ERR value.
313 */
314static LY_ERR
315lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
316{
317 const struct lys_module *mod = NULL;
318 LY_ERR ret = LY_SUCCESS;
319
320 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
321 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
322 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
323 if (!imp_p->module->parsed) {
324 /* try to use filepath if present */
325 if (imp_p->module->filepath) {
326 struct ly_in *in;
327 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
328 LOGINT(ctx->ctx);
329 } else {
330 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 +0100331 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, NULL, &mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200332 if (mod != imp_p->module) {
333 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
334 imp_p->module->filepath, imp_p->module->name);
335 mod = NULL;
336 }
337 }
338 ly_in_free(in, 1);
339 }
340 if (!mod) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100341 if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, NULL, ctx->unres,
342 (struct lys_module **)&mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200343 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
344 imp_p->module->name, ctx->cur_mod->name);
345 return LY_ENOTFOUND;
346 }
347 }
348 }
349
350 return ret;
351}
352
353LY_ERR
354lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
355 struct lysp_ident *identities_p, struct lysc_ident **identities)
356{
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100357 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200358 struct lysc_ctx context = {0};
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100359 struct lysc_ident *ident;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200360 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100361 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200362
363 assert(ctx_sc || ctx);
364
365 if (!ctx_sc) {
366 context.ctx = ctx;
Radek Krejci3aac9a72020-12-01 12:24:26 +0100367 context.cur_mod = parsed_mod ? parsed_mod->mod : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200368 context.pmod = parsed_mod;
369 context.path_len = 1;
370 context.path[0] = '/';
371 ctx_sc = &context;
372 }
373
374 if (!identities_p) {
375 return LY_SUCCESS;
376 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200377
378 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200379 LY_ARRAY_FOR(identities_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100380 /* evaluate if-features */
381 LY_CHECK_RET(lys_eval_iffeatures(ctx, identities_p[u].iffeatures, &enabled));
382 if (!enabled) {
383 continue;
384 }
385
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200386 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
387
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100388 /* add new compiled identity */
389 LY_ARRAY_NEW_RET(ctx_sc->ctx, *identities, ident, LY_EMEM);
390
391 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, ident->name, ret, done);
392 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, ident->dsc, ret, done);
393 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, ident->ref, ret, done);
394 ident->module = ctx_sc->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200395 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100396 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
397 ident->flags = identities_p[u].flags;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200398
399 lysc_update_path(ctx_sc, NULL, NULL);
400 }
401 lysc_update_path(ctx_sc, NULL, NULL);
402done:
403 return ret;
404}
405
406/**
407 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
408 *
409 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
410 *
411 * @param[in] ctx Compile context for logging.
412 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
413 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
414 * @return LY_SUCCESS if everything is ok.
415 * @return LY_EVALID if the identity is derived from itself.
416 */
417static LY_ERR
418lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
419{
420 LY_ERR ret = LY_SUCCESS;
421 LY_ARRAY_COUNT_TYPE u, v;
422 struct ly_set recursion = {0};
423 struct lysc_ident *drv;
424
425 if (!derived) {
426 return LY_SUCCESS;
427 }
428
429 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
430 if (ident == derived[u]) {
431 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
432 "Identity \"%s\" is indirectly derived from itself.", ident->name);
433 ret = LY_EVALID;
434 goto cleanup;
435 }
436 ret = ly_set_add(&recursion, derived[u], 0, NULL);
437 LY_CHECK_GOTO(ret, cleanup);
438 }
439
440 for (v = 0; v < recursion.count; ++v) {
441 drv = recursion.objs[v];
442 if (!drv->derived) {
443 continue;
444 }
445 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
446 if (ident == drv->derived[u]) {
447 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
448 "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)) {
474 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
475 "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) {
491 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
492 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
493 } else {
494 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
495 "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]) {
505 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
506 "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 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200530 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
531 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
532 } else {
533 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
534 "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:
563 for (u = 0, v = 0; *idents && (u < LY_ARRAY_COUNT(*idents)); ++u) {
564 /* 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
621lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
622{
623 LY_ERR ret = LY_EVALID, r;
624 LY_ARRAY_COUNT_TYPE u;
625 struct lysp_stmt *stmt;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100626 struct lysp_qname *iffeatures, *iffeat;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200627 void *parsed = NULL, **compiled = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100628 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200629
630 /* check for invalid substatements */
631 for (stmt = ext->child; stmt; stmt = stmt->next) {
632 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
633 continue;
634 }
635 for (u = 0; substmts[u].stmt; ++u) {
636 if (substmts[u].stmt == stmt->kw) {
637 break;
638 }
639 }
640 if (!substmts[u].stmt) {
641 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
642 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
643 goto cleanup;
644 }
645 }
646
647 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
648
649 /* keep order of the processing the same as the order in the defined substmts,
650 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
651 for (u = 0; substmts[u].stmt; ++u) {
652 ly_bool stmt_present = 0;
653
654 for (stmt = ext->child; stmt; stmt = stmt->next) {
655 if (substmts[u].stmt != stmt->kw) {
656 continue;
657 }
658
659 stmt_present = 1;
660 if (substmts[u].storage) {
661 switch (stmt->kw) {
662 case LY_STMT_STATUS:
663 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
664 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
665 break;
666 case LY_STMT_UNITS: {
667 const char **units;
668
669 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
670 /* single item */
671 if (*((const char **)substmts[u].storage)) {
672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
673 goto cleanup;
674 }
675 units = (const char **)substmts[u].storage;
676 } else {
677 /* sized array */
678 const char ***units_array = (const char ***)substmts[u].storage;
679 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
680 }
681 r = lydict_insert(ctx->ctx, stmt->arg, 0, units);
682 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
683 break;
684 }
685 case LY_STMT_TYPE: {
686 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
687 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
688
689 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
690 /* single item */
691 if (*(struct lysc_type **)substmts[u].storage) {
692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
693 goto cleanup;
694 }
695 compiled = substmts[u].storage;
696 } else {
697 /* sized array */
698 struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL;
699 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
700 compiled = (void *)type;
701 }
702
703 r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL);
704 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Michal Vaskobd6de2b2020-10-22 14:45:03 +0200705 r = lys_compile_type(ctx, NULL, flags ? *flags : 0, ctx->pmod, ext->name, parsed,
706 (struct lysc_type **)compiled, units && !*units ? units : NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200707 lysp_type_free(ctx->ctx, parsed);
708 free(parsed);
709 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
710 break;
711 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100712 case LY_STMT_IF_FEATURE:
713 iffeatures = NULL;
714 LY_ARRAY_NEW_GOTO(ctx->ctx, iffeatures, iffeat, ret, cleanup);
715 iffeat->str = stmt->arg;
716 iffeat->mod = ctx->pmod;
717 r = lys_eval_iffeatures(ctx->ctx, iffeatures, &enabled);
718 LY_ARRAY_FREE(iffeatures);
719 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
720 if (!enabled) {
721 /* it is disabled, remove the whole extension instance */
722 return LY_ENOT;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200723 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200724 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200725 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
726 * also note that in many statements their extensions are not taken into account */
727 default:
728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s%s%s\") substatement.",
729 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
730 goto cleanup;
731 }
732 }
733 }
734
735 if (((substmts[u].cardinality == LY_STMT_CARD_MAND) || (substmts[u].cardinality == LY_STMT_CARD_SOME)) && !stmt_present) {
736 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
737 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
738 goto cleanup;
739 }
740 }
741
742 ret = LY_SUCCESS;
743
744cleanup:
745 return ret;
746}
747
748/**
749 * @brief Check when for cyclic dependencies.
750 *
751 * @param[in] set Set with all the referenced nodes.
752 * @param[in] node Node whose "when" referenced nodes are in @p set.
753 * @return LY_ERR value
754 */
755static LY_ERR
756lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
757{
758 struct lyxp_set tmp_set;
759 struct lyxp_set_scnode *xp_scnode;
760 uint32_t i, j;
761 LY_ARRAY_COUNT_TYPE u;
762 struct lysc_when *when;
763 LY_ERR ret = LY_SUCCESS;
764
765 memset(&tmp_set, 0, sizeof tmp_set);
766
767 /* prepare in_ctx of the set */
768 for (i = 0; i < set->used; ++i) {
769 xp_scnode = &set->val.scnodes[i];
770
Radek Krejcif13b87b2020-12-01 22:02:17 +0100771 if (xp_scnode->in_ctx != LYXP_SET_SCNODE_START_USED) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200772 /* check node when, skip the context node (it was just checked) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100773 xp_scnode->in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200774 }
775 }
776
777 for (i = 0; i < set->used; ++i) {
778 xp_scnode = &set->val.scnodes[i];
Radek Krejcif13b87b2020-12-01 22:02:17 +0100779 if (xp_scnode->in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200780 /* already checked */
781 continue;
782 }
783
784 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) ||
785 !xp_scnode->scnode->when) {
786 /* no when to check */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100787 xp_scnode->in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200788 continue;
789 }
790
791 node = xp_scnode->scnode;
792 do {
793 LY_ARRAY_FOR(node->when, u) {
794 when = node->when[u];
795 ret = lyxp_atomize(when->cond, node->module, LY_PREF_SCHEMA_RESOLVED, when->prefixes, when->context,
796 &tmp_set, LYXP_SCNODE_SCHEMA);
797 if (ret != LY_SUCCESS) {
798 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
799 goto cleanup;
800 }
801
802 for (j = 0; j < tmp_set.used; ++j) {
803 /* skip roots'n'stuff */
804 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
805 /* try to find this node in our set */
806 uint32_t idx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100807 if (lyxp_set_scnode_contains(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1, &idx) &&
808 (set->val.scnodes[idx].in_ctx == LYXP_SET_SCNODE_START_USED)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200809 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LY_VCODE_CIRC_WHEN, node->name, set->val.scnodes[idx].scnode->name);
810 ret = LY_EVALID;
811 goto cleanup;
812 }
813
814 /* needs to be checked, if in both sets, will be ignored */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100815 tmp_set.val.scnodes[j].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200816 } else {
817 /* no when, nothing to check */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100818 tmp_set.val.scnodes[j].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200819 }
820 }
821
822 /* merge this set into the global when set */
823 lyxp_set_scnode_merge(set, &tmp_set);
824 }
825
826 /* check when of non-data parents as well */
827 node = node->parent;
828 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
829
830 /* this node when was checked (xp_scnode could have been reallocd) */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100831 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200832 }
833
834cleanup:
835 lyxp_set_free_content(&tmp_set);
836 return ret;
837}
838
839LY_ERR
840lysc_check_status(struct lysc_ctx *ctx, uint16_t flags1, void *mod1, const char *name1, uint16_t flags2, void *mod2,
841 const char *name2)
842{
843 uint16_t flg1, flg2;
844
845 flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
846 flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
847
848 if ((flg1 < flg2) && (mod1 == mod2)) {
849 if (ctx) {
850 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
851 "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".",
852 flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1,
853 flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2);
854 }
855 return LY_EVALID;
856 }
857
858 return LY_SUCCESS;
859}
860
Michal Vasko25d6ad02020-10-22 12:20:22 +0200861LY_ERR
862lys_compile_expr_implement(const struct ly_ctx *ctx, const struct lyxp_expr *expr, LY_PREFIX_FORMAT format,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100863 void *prefix_data, ly_bool implement, struct lys_glob_unres *unres, const struct lys_module **mod_p)
Michal Vaskocfaff232020-10-20 09:35:14 +0200864{
865 uint32_t i;
866 const char *ptr, *start;
867 const struct lys_module *mod;
868
Michal Vasko25d6ad02020-10-22 12:20:22 +0200869 assert(implement || mod_p);
870
Michal Vaskocfaff232020-10-20 09:35:14 +0200871 for (i = 0; i < expr->used; ++i) {
872 if ((expr->tokens[i] != LYXP_TOKEN_NAMETEST) && (expr->tokens[i] != LYXP_TOKEN_LITERAL)) {
873 /* token cannot have a prefix */
874 continue;
875 }
876
877 start = expr->expr + expr->tok_pos[i];
878 if (!(ptr = ly_strnchr(start, ':', expr->tok_len[i]))) {
879 /* token without a prefix */
880 continue;
881 }
882
883 if (!(mod = ly_resolve_prefix(ctx, start, ptr - start, format, prefix_data))) {
884 /* unknown prefix, do not care right now */
885 continue;
886 }
887
888 if (!mod->implemented) {
889 /* unimplemented module found */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200890 if (implement) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100891 LY_CHECK_RET(lys_set_implemented_r((struct lys_module *)mod, NULL, unres));
Michal Vasko25d6ad02020-10-22 12:20:22 +0200892 } else {
Michal Vaskocfaff232020-10-20 09:35:14 +0200893 *mod_p = mod;
Michal Vasko25d6ad02020-10-22 12:20:22 +0200894 break;
Michal Vaskocfaff232020-10-20 09:35:14 +0200895 }
Michal Vaskocfaff232020-10-20 09:35:14 +0200896 }
897 }
898
Michal Vasko25d6ad02020-10-22 12:20:22 +0200899 return LY_SUCCESS;
Michal Vaskocfaff232020-10-20 09:35:14 +0200900}
901
902/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200903 * @brief Check when/must expressions of a node on a complete compiled schema tree.
904 *
905 * @param[in] ctx Compile context.
906 * @param[in] node Node to check.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100907 * @param[in,out] unres Global unres structure.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200908 * @return LY_ERR value
909 */
910static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +0100911lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200912{
913 struct lyxp_set tmp_set;
914 uint32_t i, opts;
915 LY_ARRAY_COUNT_TYPE u;
916 ly_bool input_done = 0;
917 struct lysc_when **when = NULL;
918 struct lysc_must *musts = NULL;
919 LY_ERR ret = LY_SUCCESS;
920 const struct lysc_node *op;
Michal Vaskocfaff232020-10-20 09:35:14 +0200921 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200922
923 memset(&tmp_set, 0, sizeof tmp_set);
924 opts = LYXP_SCNODE_SCHEMA;
925 if (node->flags & LYS_CONFIG_R) {
926 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent) {}
927 if (op) {
928 /* we are actually in output */
929 opts = LYXP_SCNODE_OUTPUT;
930 }
931 }
932
933 switch (node->nodetype) {
934 case LYS_CONTAINER:
935 when = ((struct lysc_node_container *)node)->when;
936 musts = ((struct lysc_node_container *)node)->musts;
937 break;
938 case LYS_CHOICE:
939 when = ((struct lysc_node_choice *)node)->when;
940 break;
941 case LYS_LEAF:
942 when = ((struct lysc_node_leaf *)node)->when;
943 musts = ((struct lysc_node_leaf *)node)->musts;
944 break;
945 case LYS_LEAFLIST:
946 when = ((struct lysc_node_leaflist *)node)->when;
947 musts = ((struct lysc_node_leaflist *)node)->musts;
948 break;
949 case LYS_LIST:
950 when = ((struct lysc_node_list *)node)->when;
951 musts = ((struct lysc_node_list *)node)->musts;
952 break;
953 case LYS_ANYXML:
954 case LYS_ANYDATA:
955 when = ((struct lysc_node_anydata *)node)->when;
956 musts = ((struct lysc_node_anydata *)node)->musts;
957 break;
958 case LYS_CASE:
959 when = ((struct lysc_node_case *)node)->when;
960 break;
961 case LYS_NOTIF:
962 when = ((struct lysc_notif *)node)->when;
963 musts = ((struct lysc_notif *)node)->musts;
964 break;
965 case LYS_RPC:
966 case LYS_ACTION:
967 /* first process when and input musts */
968 when = ((struct lysc_action *)node)->when;
969 musts = ((struct lysc_action *)node)->input.musts;
970 break;
971 default:
972 /* nothing to check */
973 break;
974 }
975
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200976 LY_ARRAY_FOR(when, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +0200977 /* first check whether all the referenced modules are implemented */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200978 mod = NULL;
979 ret = lys_compile_expr_implement(ctx->ctx, when[u]->cond, LY_PREF_SCHEMA_RESOLVED, when[u]->prefixes,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100980 ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, unres, &mod);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200981 if (ret) {
982 goto cleanup;
983 } else if (mod) {
Michal Vaskocfaff232020-10-20 09:35:14 +0200984 LOGWRN(ctx->ctx, "When condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
985 when[u]->cond->expr, mod->name);
986 continue;
987 }
988
989 /* check "when" */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200990 ret = lyxp_atomize(when[u]->cond, node->module, LY_PREF_SCHEMA_RESOLVED, when[u]->prefixes, when[u]->context,
991 &tmp_set, opts);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200992 if (ret) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200993 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
994 goto cleanup;
995 }
996
997 ctx->path[0] = '\0';
998 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
999 for (i = 0; i < tmp_set.used; ++i) {
1000 /* skip roots'n'stuff */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001001 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 +02001002 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
1003
1004 /* XPath expression cannot reference "lower" status than the node that has the definition */
1005 ret = lysc_check_status(ctx, when[u]->flags, node->module, node->name, schema->flags, schema->module,
1006 schema->name);
1007 LY_CHECK_GOTO(ret, cleanup);
1008
1009 /* check dummy node accessing */
1010 if (schema == node) {
1011 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
1012 ret = LY_EVALID;
1013 goto cleanup;
1014 }
1015 }
1016 }
1017
1018 /* check cyclic dependencies */
1019 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
1020 LY_CHECK_GOTO(ret, cleanup);
1021
1022 lyxp_set_free_content(&tmp_set);
1023 }
1024
1025check_musts:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001026 LY_ARRAY_FOR(musts, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +02001027 /* first check whether all the referenced modules are implemented */
Michal Vasko25d6ad02020-10-22 12:20:22 +02001028 mod = NULL;
1029 ret = lys_compile_expr_implement(ctx->ctx, musts[u].cond, LY_PREF_SCHEMA_RESOLVED, musts[u].prefixes,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001030 ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, unres, &mod);
Michal Vasko25d6ad02020-10-22 12:20:22 +02001031 if (ret) {
1032 goto cleanup;
1033 } else if (mod) {
Michal Vaskocfaff232020-10-20 09:35:14 +02001034 LOGWRN(ctx->ctx, "Must condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
1035 musts[u].cond->expr, mod->name);
1036 continue;
1037 }
1038
1039 /* check "must" */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001040 ret = lyxp_atomize(musts[u].cond, node->module, LY_PREF_SCHEMA_RESOLVED, musts[u].prefixes, node, &tmp_set, opts);
Michal Vasko25d6ad02020-10-22 12:20:22 +02001041 if (ret) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001042 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
1043 goto cleanup;
1044 }
1045
1046 ctx->path[0] = '\0';
1047 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1048 for (i = 0; i < tmp_set.used; ++i) {
1049 /* skip roots'n'stuff */
1050 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
1051 /* XPath expression cannot reference "lower" status than the node that has the definition */
1052 ret = lysc_check_status(ctx, node->flags, node->module, node->name, tmp_set.val.scnodes[i].scnode->flags,
1053 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
1054 LY_CHECK_GOTO(ret, cleanup);
1055 }
1056 }
1057
1058 lyxp_set_free_content(&tmp_set);
1059 }
1060
1061 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
1062 /* now check output musts */
1063 input_done = 1;
1064 when = NULL;
1065 musts = ((struct lysc_action *)node)->output.musts;
1066 opts = LYXP_SCNODE_OUTPUT;
1067 goto check_musts;
1068 }
1069
1070cleanup:
1071 lyxp_set_free_content(&tmp_set);
1072 return ret;
1073}
1074
1075/**
1076 * @brief Check leafref for its target existence on a complete compiled schema tree.
1077 *
1078 * @param[in] ctx Compile context.
1079 * @param[in] node Context node for the leafref.
1080 * @param[in] lref Leafref to check/resolve.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001081 * @param[in,out] unres Global unres structure.
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_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref,
1086 struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001087{
1088 const struct lysc_node *target = NULL, *siter;
1089 struct ly_path *p;
1090 struct lysc_type *type;
1091
1092 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
1093
1094 /* try to find the target */
Michal Vaskoc0792ca2020-12-01 12:03:21 +01001095 LY_CHECK_RET(ly_path_compile(ctx->ctx, lref->cur_mod, node, lref->path, LY_PATH_LREF_TRUE, lysc_is_output(node) ?
Michal Vasko405cc9e2020-12-01 12:01:27 +01001096 LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes,
1097 unres, &p));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001098
1099 /* get the target node */
1100 target = p[LY_ARRAY_COUNT(p) - 1].node;
1101 ly_path_free(node->module->ctx, p);
1102
1103 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1104 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
1105 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
1106 lref->path->expr, lys_nodetype2str(target->nodetype));
1107 return LY_EVALID;
1108 }
1109
1110 /* check status */
1111 ctx->path[0] = '\0';
1112 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1113 ctx->path_len = strlen(ctx->path);
1114 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
1115 return LY_EVALID;
1116 }
1117 ctx->path_len = 1;
1118 ctx->path[1] = '\0';
1119
1120 /* check config */
1121 if (lref->require_instance) {
1122 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {}
1123 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
1124 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
1125 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
1126 return LY_EVALID;
1127 }
1128 }
1129
1130 /* store the target's type and check for circular chain of leafrefs */
1131 lref->realtype = ((struct lysc_node_leaf *)target)->type;
1132 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
1133 if (type == (struct lysc_type *)lref) {
1134 /* circular chain detected */
1135 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
1136 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
1137 return LY_EVALID;
1138 }
1139 }
1140
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001141 /* TODO check if leafref and its target are under common if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001142
1143 return LY_SUCCESS;
1144}
1145
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001146/**
1147 * @brief Compile default value(s) for leaf or leaf-list expecting a complete compiled schema tree.
1148 *
1149 * @param[in] ctx Compile context.
1150 * @param[in] node Leaf or leaf-list to compile the default value(s) for.
1151 * @param[in] type Type of the default value.
1152 * @param[in] dflt Default value.
1153 * @param[in] dflt_pmod Parsed module of the @p dflt to resolve possible prefixes.
1154 * @param[in,out] storage Storage for the compiled default value.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001155 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001156 * @return LY_ERR value.
1157 */
1158static LY_ERR
1159lys_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 +01001160 const struct lysp_module *dflt_pmod, struct lyd_value *storage, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001161{
1162 LY_ERR ret;
Michal Vasko25d6ad02020-10-22 12:20:22 +02001163 uint32_t options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001164 struct ly_err_item *err = NULL;
1165
Michal Vasko25d6ad02020-10-22 12:20:22 +02001166 options = (ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED) ? LY_TYPE_STORE_IMPLEMENT : 0;
1167 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), options, LY_PREF_SCHEMA, (void *)dflt_pmod,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001168 LYD_HINT_SCHEMA, node, storage, unres, &err);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001169 if (ret == LY_EINCOMPLETE) {
1170 /* we have no data so we will not be resolving it */
1171 ret = LY_SUCCESS;
1172 }
1173
1174 if (ret) {
1175 ctx->path[0] = '\0';
1176 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1177 if (err) {
1178 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1179 "Invalid default - value does not fit the type (%s).", err->msg);
1180 ly_err_free(err);
1181 } else {
Michal Vasko25d6ad02020-10-22 12:20:22 +02001182 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid default - value does not fit the type.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001183 }
1184 return ret;
1185 }
1186
1187 ++((struct lysc_type *)storage->realtype)->refcount;
1188 return LY_SUCCESS;
1189}
1190
1191/**
1192 * @brief Compile default value of a leaf expecting a complete compiled schema tree.
1193 *
1194 * @param[in] ctx Compile context.
1195 * @param[in] leaf Leaf that the default value is for.
1196 * @param[in] dflt Default value to compile.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001197 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001198 * @return LY_ERR value.
1199 */
1200static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001201lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt,
1202 struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001203{
1204 LY_ERR ret;
1205
1206 assert(!leaf->dflt);
1207
1208 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
1209 /* ignore default values for keys and mandatory leaves */
1210 return LY_SUCCESS;
1211 }
1212
1213 /* allocate the default value */
1214 leaf->dflt = calloc(1, sizeof *leaf->dflt);
1215 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
1216
1217 /* store the default value */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001218 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt->str, dflt->mod, leaf->dflt, unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001219 if (ret) {
1220 free(leaf->dflt);
1221 leaf->dflt = NULL;
1222 }
1223
1224 return ret;
1225}
1226
1227/**
1228 * @brief Compile default values of a leaf-list expecting a complete compiled schema tree.
1229 *
1230 * @param[in] ctx Compile context.
1231 * @param[in] llist Leaf-list that the default value(s) are for.
1232 * @param[in] dflt Default value to compile, in case of a single value.
1233 * @param[in] dflts Sized array of default values, in case of more values.
Michal Vasko405cc9e2020-12-01 12:01:27 +01001234 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001235 * @return LY_ERR value.
1236 */
1237static LY_ERR
1238lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflt,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001239 struct lysp_qname *dflts, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001240{
1241 LY_ERR ret;
1242 LY_ARRAY_COUNT_TYPE orig_count, u, v;
1243
1244 assert(dflt || dflts);
1245
1246 if (llist->dflts) {
1247 /* there were already some defaults and we are adding new by deviations */
1248 assert(dflts);
1249 orig_count = LY_ARRAY_COUNT(llist->dflts);
1250 } else {
1251 orig_count = 0;
1252 }
1253
1254 /* allocate new items */
1255 if (dflts) {
1256 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
1257 } else {
1258 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
1259 }
1260
1261 /* fill each new default value */
1262 if (dflts) {
1263 LY_ARRAY_FOR(dflts, u) {
1264 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
1265 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u].str, dflts[u].mod,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001266 llist->dflts[orig_count + u], unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001267 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
1268 LY_ARRAY_INCREMENT(llist->dflts);
1269 }
1270 } else {
1271 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
1272 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt->str, dflt->mod,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001273 llist->dflts[orig_count], unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001274 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
1275 LY_ARRAY_INCREMENT(llist->dflts);
1276 }
1277
1278 /* check default value uniqueness */
1279 if (llist->flags & LYS_CONFIG_W) {
1280 /* configuration data values must be unique - so check the default values */
1281 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
1282 for (v = 0; v < u; ++v) {
1283 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
1284 lysc_update_path(ctx, llist->parent, llist->name);
1285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1286 "Configuration leaf-list has multiple defaults of the same value \"%s\".",
1287 llist->dflts[u]->canonical);
1288 lysc_update_path(ctx, NULL, NULL);
1289 return LY_EVALID;
1290 }
1291 }
1292 }
1293 }
1294
1295 return LY_SUCCESS;
1296}
1297
Michal Vasko405cc9e2020-12-01 12:01:27 +01001298LY_ERR
1299lys_compile_unres_glob(struct ly_ctx *ctx, struct lys_glob_unres *unres)
1300{
1301 struct lysc_node *node;
1302 struct lysc_type *type, *typeiter;
1303 struct lysc_type_leafref *lref;
1304 struct lysc_ctx cctx = {0};
1305 LY_ARRAY_COUNT_TYPE v;
1306 uint32_t i;
1307
1308 if (unres->recompile) {
1309 /* recompile all the modules and resolve the new unres instead (during recompilation) */
1310 unres->recompile = 0;
1311 return lys_recompile(ctx, 1);
1312 }
1313
1314 /* fake compile context */
1315 cctx.ctx = ctx;
1316 cctx.path_len = 1;
1317 cctx.path[0] = '/';
1318
1319 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
1320 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
1321 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
1322 for (i = 0; i < unres->leafrefs.count; ++i) {
1323 node = unres->leafrefs.objs[i];
1324 cctx.cur_mod = node->module;
1325 cctx.pmod = node->module->parsed;
1326
1327 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
1328 type = ((struct lysc_node_leaf *)node)->type;
1329 if (type->basetype == LY_TYPE_LEAFREF) {
1330 LY_CHECK_RET(lys_compile_unres_leafref(&cctx, node, (struct lysc_type_leafref *)type, unres));
1331 } else if (type->basetype == LY_TYPE_UNION) {
1332 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
1333 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
1334 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
1335 LY_CHECK_RET(lys_compile_unres_leafref(&cctx, node, lref, unres));
1336 }
1337 }
1338 }
1339 }
1340 while (unres->leafrefs.count) {
1341 node = unres->leafrefs.objs[unres->leafrefs.count - 1];
1342 cctx.cur_mod = node->module;
1343 cctx.pmod = node->module->parsed;
1344
1345 /* store pointer to the real type */
1346 type = ((struct lysc_node_leaf *)node)->type;
1347 if (type->basetype == LY_TYPE_LEAFREF) {
1348 for (typeiter = ((struct lysc_type_leafref *)type)->realtype;
1349 typeiter->basetype == LY_TYPE_LEAFREF;
1350 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
1351 ((struct lysc_type_leafref *)type)->realtype = typeiter;
1352 } else if (type->basetype == LY_TYPE_UNION) {
1353 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
1354 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
1355 for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype;
1356 typeiter->basetype == LY_TYPE_LEAFREF;
1357 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
1358 ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter;
1359 }
1360 }
1361 }
1362
1363 ly_set_rm_index(&unres->leafrefs, unres->leafrefs.count - 1, NULL);
1364 }
1365
1366 /* check xpath */
1367 while (unres->xpath.count) {
1368 node = unres->xpath.objs[unres->xpath.count - 1];
1369 cctx.cur_mod = node->module;
1370 cctx.pmod = node->module->parsed;
1371
1372 LY_CHECK_RET(lys_compile_unres_xpath(&cctx, node, unres));
1373
1374 ly_set_rm_index(&unres->xpath, unres->xpath.count - 1, NULL);
1375 }
1376
1377 /* finish incomplete default values compilation */
1378 while (unres->dflts.count) {
1379 struct lysc_unres_dflt *r = unres->dflts.objs[unres->dflts.count - 1];
1380 cctx.cur_mod = r->leaf->module;
1381 cctx.pmod = r->leaf->module->parsed;
1382
1383 if (r->leaf->nodetype == LYS_LEAF) {
1384 LY_CHECK_RET(lys_compile_unres_leaf_dlft(&cctx, r->leaf, r->dflt, unres));
1385 } else {
1386 LY_CHECK_RET(lys_compile_unres_llist_dflts(&cctx, r->llist, r->dflt, r->dflts, unres));
1387 }
1388 lysc_unres_dflt_free(ctx, r);
1389
1390 ly_set_rm_index(&unres->dflts, unres->dflts.count - 1, NULL);
1391 }
1392
1393 /* some unres items may have been added */
1394 if (unres->leafrefs.count || unres->xpath.count || unres->dflts.count) {
1395 return lys_compile_unres_glob(ctx, unres);
1396 }
1397
1398 return LY_SUCCESS;
1399}
1400
1401void
1402lys_compile_unres_glob_revert(struct ly_ctx *ctx, struct lys_glob_unres *unres)
1403{
1404 uint32_t i;
1405 struct lys_module *m;
1406
1407 for (i = 0; i < unres->implementing.count; ++i) {
1408 m = unres->implementing.objs[i];
1409 assert(m->implemented);
1410
1411 /* make the module correctly non-implemented again */
1412 m->implemented = 0;
1413 lys_precompile_augments_deviations_revert(ctx, m);
1414 }
1415
1416 for (i = 0; i < unres->creating.count; ++i) {
1417 m = unres->creating.objs[i];
1418
1419 /* remove the module from the context and free it */
1420 ly_set_rm(&ctx->list, m, NULL);
1421 lys_module_free(m, NULL);
1422 }
1423
1424 if (unres->implementing.count) {
1425 /* recompile because some implemented modules are no longer implemented */
1426 lys_recompile(ctx, 0);
1427 }
1428}
1429
1430void
1431lys_compile_unres_glob_erase(const struct ly_ctx *ctx, struct lys_glob_unres *unres)
1432{
1433 uint32_t i;
1434
1435 ly_set_erase(&unres->implementing, NULL);
1436 ly_set_erase(&unres->creating, NULL);
1437 for (i = 0; i < unres->dflts.count; ++i) {
1438 lysc_unres_dflt_free(ctx, unres->dflts.objs[i]);
1439 }
1440 ly_set_erase(&unres->dflts, NULL);
1441 ly_set_erase(&unres->xpath, NULL);
1442 ly_set_erase(&unres->leafrefs, NULL);
1443}
1444
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001445/**
Michal Vasko405cc9e2020-12-01 12:01:27 +01001446 * @brief Finish compilation of all the module unres sets in a compile context.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001447 *
1448 * @param[in] ctx Compile context with unres sets.
1449 * @return LY_ERR value.
1450 */
1451static LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001452lys_compile_unres_mod(struct lysc_ctx *ctx)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001453{
1454 struct lysc_node *node;
Michal Vasko14c67892020-11-23 16:51:31 +01001455 struct lysc_action **actions;
1456 struct lysc_notif **notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001457 struct lysc_augment *aug;
1458 struct lysc_deviation *dev;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001459 struct ly_set disabled_op = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001460 uint32_t i;
1461
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001462#define ARRAY_DEL_ITEM(array, item) \
1463 { \
1464 LY_ARRAY_COUNT_TYPE u__; \
1465 LY_ARRAY_FOR(array, u__) { \
1466 if ((array) + u__ == item) { \
1467 LY_ARRAY_DECREMENT(array); \
1468 if (u__ < LY_ARRAY_COUNT(array)) { \
1469 memmove((array) + u__, (array) + u__ + 1, (LY_ARRAY_COUNT(array) - u__) * sizeof *(array)); \
1470 } \
1471 if (!LY_ARRAY_COUNT(array)) { \
1472 LY_ARRAY_FREE(array); \
1473 (array) = NULL; \
1474 } \
1475 break; \
1476 } \
1477 } \
1478 }
1479
1480 /* remove all disabled nodes */
1481 for (i = 0; i < ctx->disabled.count; ++i) {
1482 node = ctx->disabled.snodes[i];
1483 if (node->flags & LYS_KEY) {
1484 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Key \"%s\" is disabled by its if-features.",
1485 node->name);
1486 return LY_EVALID;
1487 }
1488
1489 if (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Michal Vasko14c67892020-11-23 16:51:31 +01001490 /* just remember all RPCs/actions/notifs for now */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001491 ly_set_add(&disabled_op, node, 1, NULL);
1492 } else {
1493 lysc_node_free(ctx->ctx, node, 1);
1494 }
1495 }
1496
1497 /* remove ops also from their arrays, from end so as not to move other items and change these pointer targets */
1498 i = disabled_op.count;
1499 while (i) {
1500 --i;
1501 node = disabled_op.snodes[i];
1502 if (node->nodetype == LYS_RPC) {
Michal Vasko14c67892020-11-23 16:51:31 +01001503 actions = &node->module->compiled->rpcs;
1504 assert(actions);
1505 notifs = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001506 } else if (node->nodetype == LYS_ACTION) {
Michal Vasko14c67892020-11-23 16:51:31 +01001507 actions = lysc_node_actions_p(node->parent);
1508 assert(actions);
1509 notifs = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001510 } else if (node->parent) {
Michal Vasko14c67892020-11-23 16:51:31 +01001511 actions = NULL;
1512 notifs = lysc_node_notifs_p(node->parent);
1513 assert(notifs);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001514 } else {
Michal Vasko14c67892020-11-23 16:51:31 +01001515 actions = NULL;
1516 notifs = &node->module->compiled->notifs;
1517 assert(notifs);
1518 }
1519
1520 if (actions) {
1521 lysc_action_free(ctx->ctx, (struct lysc_action *)node);
1522 ARRAY_DEL_ITEM(*actions, (struct lysc_action *)node);
1523 } else {
1524 lysc_notif_free(ctx->ctx, (struct lysc_notif *)node);
1525 ARRAY_DEL_ITEM(*notifs, (struct lysc_notif *)node);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001526 }
1527 }
1528 ly_set_erase(&disabled_op, NULL);
1529
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001530 /* check that all augments were applied */
1531 for (i = 0; i < ctx->augs.count; ++i) {
1532 aug = ctx->augs.objs[i];
1533 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
1534 "Augment target node \"%s\" from module \"%s\" was not found.", aug->nodeid->expr,
1535 LYSP_MODULE_NAME(aug->nodeid_pmod));
1536 }
1537 if (ctx->augs.count) {
1538 return LY_ENOTFOUND;
1539 }
1540
1541 /* check that all deviations were applied */
1542 for (i = 0; i < ctx->devs.count; ++i) {
1543 dev = ctx->devs.objs[i];
1544 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
1545 "Deviation(s) target node \"%s\" from module \"%s\" was not found.", dev->nodeid->expr,
1546 LYSP_MODULE_NAME(dev->dev_pmods[0]));
1547 }
1548 if (ctx->devs.count) {
1549 return LY_ENOTFOUND;
1550 }
1551
1552 return LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001553#undef ARRAY_DEL_ITEM
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001554}
1555
1556/**
Michal Vasko405cc9e2020-12-01 12:01:27 +01001557 * @brief Erase all the module unres sets in a compile context.
1558 *
1559 * @param[in] ctx Compile context with unres sets.
1560 * @param[in] error Whether the compilation finished with an error or not.
1561 */
1562static void
1563lys_compile_unres_mod_erase(struct lysc_ctx *ctx, ly_bool error)
1564{
1565 uint32_t i;
1566
1567 ly_set_erase(&ctx->groupings, NULL);
1568 ly_set_erase(&ctx->tpdf_chain, NULL);
1569 ly_set_erase(&ctx->disabled, NULL);
1570
1571 if (!error) {
1572 /* there can be no leftover deviations or augments */
1573 LY_CHECK_ERR_RET(ctx->augs.count, LOGINT(ctx->ctx), );
1574 LY_CHECK_ERR_RET(ctx->devs.count, LOGINT(ctx->ctx), );
1575
1576 ly_set_erase(&ctx->augs, NULL);
1577 ly_set_erase(&ctx->devs, NULL);
1578 ly_set_erase(&ctx->uses_augs, NULL);
1579 ly_set_erase(&ctx->uses_rfns, NULL);
1580 } else {
1581 for (i = 0; i < ctx->augs.count; ++i) {
1582 lysc_augment_free(ctx->ctx, ctx->augs.objs[i]);
1583 }
1584 ly_set_erase(&ctx->augs, NULL);
1585 for (i = 0; i < ctx->devs.count; ++i) {
1586 lysc_deviation_free(ctx->ctx, ctx->devs.objs[i]);
1587 }
1588 ly_set_erase(&ctx->devs, NULL);
1589 for (i = 0; i < ctx->uses_augs.count; ++i) {
1590 lysc_augment_free(ctx->ctx, ctx->uses_augs.objs[i]);
1591 }
1592 ly_set_erase(&ctx->uses_augs, NULL);
1593 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1594 lysc_refine_free(ctx->ctx, ctx->uses_rfns.objs[i]);
1595 }
1596 ly_set_erase(&ctx->uses_rfns, NULL);
1597 }
1598}
1599
1600/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001601 * @brief Compile identites in the current module and all its submodules.
1602 *
1603 * @param[in] ctx Compile context.
1604 * @return LY_ERR value.
1605 */
1606static LY_ERR
1607lys_compile_identities(struct lysc_ctx *ctx)
1608{
1609 struct lysp_submodule *submod;
1610 LY_ARRAY_COUNT_TYPE u;
1611
1612 if (!ctx->cur_mod->identities) {
1613 LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, ctx->cur_mod->parsed->identities, &ctx->cur_mod->identities));
1614 LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, u) {
1615 submod = ctx->cur_mod->parsed->includes[u].submodule;
1616 LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, submod->identities, &ctx->cur_mod->identities));
1617 }
1618 }
1619
1620 if (ctx->cur_mod->parsed->identities) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001621 LY_CHECK_RET(lys_compile_identities_derived(ctx, ctx->cur_mod->parsed->identities, &ctx->cur_mod->identities));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001622 }
1623 lysc_update_path(ctx, NULL, "{submodule}");
1624 LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, u) {
1625
1626 submod = ctx->cur_mod->parsed->includes[u].submodule;
1627 if (submod->identities) {
1628 lysc_update_path(ctx, NULL, submod->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001629 LY_CHECK_RET(lys_compile_identities_derived(ctx, submod->identities, &ctx->cur_mod->identities));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001630 lysc_update_path(ctx, NULL, NULL);
1631 }
1632 }
1633 lysc_update_path(ctx, NULL, NULL);
1634
1635 return LY_SUCCESS;
1636}
1637
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001638LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001639lys_recompile(struct ly_ctx *ctx, ly_bool log)
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001640{
1641 uint32_t idx;
1642 struct lys_module *mod;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001643 struct lys_glob_unres unres = {0};
1644 LY_ERR ret = LY_SUCCESS;
Radek Krejci430a5582020-12-01 13:35:18 +01001645 uint32_t prev_lo = 0;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001646
1647 if (!log) {
1648 /* recompile, must succeed because the modules were already compiled; hide messages because any
1649 * warnings were already printed, are not really relevant, and would hide the real error */
1650 prev_lo = ly_log_options(0);
1651 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001652
1653 /* free all the modules */
1654 for (idx = 0; idx < ctx->list.count; ++idx) {
1655 mod = ctx->list.objs[idx];
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001656 if (mod->compiled) {
1657 /* free the module */
1658 lysc_module_free(mod->compiled, NULL);
1659 mod->compiled = NULL;
1660 }
1661
1662 /* free precompiled iffeatures */
1663 lys_free_feature_iffeatures(mod->parsed);
1664 }
1665
1666 /* recompile all the modules */
1667 for (idx = 0; idx < ctx->list.count; ++idx) {
1668 mod = ctx->list.objs[idx];
Michal Vasko405cc9e2020-12-01 12:01:27 +01001669 if (!mod->implemented || mod->compiled) {
1670 /* nothing to do */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001671 continue;
1672 }
1673
Michal Vasko405cc9e2020-12-01 12:01:27 +01001674 /* recompile */
1675 ret = lys_compile(mod, 0, &unres);
1676 if (ret) {
1677 if (!log) {
1678 LOGERR(mod->ctx, ret, "Recompilation of module \"%s\" failed.", mod->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001679 }
Michal Vasko405cc9e2020-12-01 12:01:27 +01001680 goto cleanup;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001681 }
1682 }
1683
Michal Vasko405cc9e2020-12-01 12:01:27 +01001684 /* resolve global unres */
1685 LY_CHECK_GOTO(ret = lys_compile_unres_glob(ctx, &unres), cleanup);
1686
1687cleanup:
1688 if (!log) {
1689 ly_log_options(prev_lo);
1690 }
1691 lys_compile_unres_glob_erase(ctx, &unres);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001692 return ret;
1693}
1694
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001695LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01001696lys_compile(struct lys_module *mod, uint32_t options, struct lys_glob_unres *unres)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001697{
1698 struct lysc_ctx ctx = {0};
1699 struct lysc_module *mod_c;
1700 struct lysp_module *sp;
1701 struct lysp_submodule *submod;
1702 struct lysp_node *pnode;
1703 struct lysp_grp *grps;
Michal Vasko5347e3a2020-11-03 17:14:57 +01001704 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001705 LY_ERR ret = LY_SUCCESS;
1706
1707 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
1708
1709 if (!mod->implemented) {
1710 /* just imported modules are not compiled */
1711 return LY_SUCCESS;
1712 }
1713
1714 /* context will be changed */
1715 ++mod->ctx->module_set_id;
1716
1717 sp = mod->parsed;
1718
1719 ctx.ctx = mod->ctx;
1720 ctx.cur_mod = mod;
1721 ctx.pmod = sp;
1722 ctx.options = options;
1723 ctx.path_len = 1;
1724 ctx.path[0] = '/';
Michal Vasko405cc9e2020-12-01 12:01:27 +01001725 ctx.unres = unres;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001726
1727 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
1728 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
1729 mod_c->mod = mod;
1730
1731 /* process imports */
1732 LY_ARRAY_FOR(sp->imports, u) {
1733 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
1734 }
1735
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001736 /* identities */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001737 LY_CHECK_GOTO(ret = lys_compile_identities(&ctx), error);
1738
1739 /* augments and deviations */
1740 LY_CHECK_GOTO(ret = lys_precompile_augments_deviations(&ctx), error);
1741
1742 /* compile augments and deviations of our module from other modules so they can be applied during compilation */
1743 LY_CHECK_GOTO(ret = lys_precompile_own_augments(&ctx), error);
1744 LY_CHECK_GOTO(ret = lys_precompile_own_deviations(&ctx), error);
1745
1746 /* data nodes */
1747 LY_LIST_FOR(sp->data, pnode) {
1748 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), error);
1749 }
1750
1751 /* top-level RPCs and notifications */
Michal Vasko5347e3a2020-11-03 17:14:57 +01001752 COMPILE_OP_ARRAY_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, lys_compile_action, 0, ret, error);
1753 COMPILE_OP_ARRAY_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, lys_compile_notif, 0, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001754
1755 /* extension instances */
1756 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
1757
1758 /* the same for submodules */
1759 LY_ARRAY_FOR(sp->includes, u) {
1760 submod = sp->includes[u].submodule;
1761 ctx.pmod = (struct lysp_module *)submod;
1762
1763 LY_LIST_FOR(submod->data, pnode) {
1764 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
1765 LY_CHECK_GOTO(ret, error);
1766 }
1767
Michal Vasko5347e3a2020-11-03 17:14:57 +01001768 COMPILE_OP_ARRAY_GOTO(&ctx, submod->rpcs, mod_c->rpcs, NULL, lys_compile_action, 0, ret, error);
1769 COMPILE_OP_ARRAY_GOTO(&ctx, submod->notifs, mod_c->notifs, NULL, lys_compile_notif, 0, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001770
1771 COMPILE_EXTS_GOTO(&ctx, submod->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
1772 }
Michal Vasko12606ee2020-11-25 17:05:11 +01001773 ctx.pmod = sp;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001774
1775 /* validate non-instantiated groupings from the parsed schema,
1776 * without it we would accept even the schemas with invalid grouping specification */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001777 ctx.options |= LYS_COMPILE_GROUPING;
1778 LY_ARRAY_FOR(sp->groupings, u) {
1779 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
1780 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, &sp->groupings[u]), error);
1781 }
1782 }
1783 LY_LIST_FOR(sp->data, pnode) {
1784 grps = (struct lysp_grp *)lysp_node_groupings(pnode);
1785 LY_ARRAY_FOR(grps, u) {
1786 if (!(grps[u].flags & LYS_USED_GRP)) {
1787 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &grps[u]), error);
1788 }
1789 }
1790 }
1791 LY_ARRAY_FOR(sp->includes, u) {
1792 submod = sp->includes[u].submodule;
1793 ctx.pmod = (struct lysp_module *)submod;
1794
1795 LY_ARRAY_FOR(submod->groupings, u) {
1796 if (!(submod->groupings[u].flags & LYS_USED_GRP)) {
1797 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, &submod->groupings[u]), error);
1798 }
1799 }
1800 LY_LIST_FOR(submod->data, pnode) {
1801 grps = (struct lysp_grp *)lysp_node_groupings(pnode);
1802 LY_ARRAY_FOR(grps, u) {
1803 if (!(grps[u].flags & LYS_USED_GRP)) {
1804 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &grps[u]), error);
1805 }
1806 }
1807 }
1808 }
1809 ctx.pmod = sp;
1810
Michal Vasko405cc9e2020-12-01 12:01:27 +01001811 /* finish compilation for all unresolved module items in the context */
1812 LY_CHECK_GOTO(ret = lys_compile_unres_mod(&ctx), error);
Michal Vasko12606ee2020-11-25 17:05:11 +01001813
Michal Vasko405cc9e2020-12-01 12:01:27 +01001814 lys_compile_unres_mod_erase(&ctx, 0);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001815 return LY_SUCCESS;
1816
1817error:
1818 lys_precompile_augments_deviations_revert(ctx.ctx, mod);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001819 lys_compile_unres_mod_erase(&ctx, 1);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001820 lysc_module_free(mod_c, NULL);
1821 mod->compiled = NULL;
1822
1823 return ret;
1824}