blob: 3dcc1755e32da73299021bd0fa374f8d0d724010 [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>
20#include <ctype.h>
21#include <stddef.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include "common.h"
28#include "compat.h"
29#include "context.h"
30#include "dict.h"
31#include "log.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020032#include "in.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020033#include "parser_schema.h"
34#include "path.h"
35#include "plugins_exts.h"
36#include "plugins_exts_internal.h"
37#include "plugins_types.h"
38#include "schema_compile_amend.h"
39#include "schema_compile_node.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010040#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020041#include "set.h"
42#include "tree.h"
43#include "tree_data.h"
44#include "tree_data_internal.h"
45#include "tree_schema.h"
46#include "tree_schema_internal.h"
47#include "xpath.h"
48
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020049/**
50 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
51 */
52static LY_ERR
53lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
54{
55 LY_ERR ret = LY_SUCCESS;
56
57 if (!ext_p->compiled) {
58 lysc_update_path(ctx, NULL, "{extension}");
59 lysc_update_path(ctx, NULL, ext_p->name);
60
61 /* compile the extension definition */
62 ext_p->compiled = calloc(1, sizeof **ext);
63 ext_p->compiled->refcount = 1;
64 DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done);
65 DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext_p->compiled->argument, ret, done);
66 ext_p->compiled->module = (struct lys_module *)ext_mod;
67 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
68
69 lysc_update_path(ctx, NULL, NULL);
70 lysc_update_path(ctx, NULL, NULL);
71
72 /* find extension definition plugin */
73 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
74 }
75
76 *ext = lysc_ext_dup(ext_p->compiled);
77
78done:
79 return ret;
80}
81
82LY_ERR
83lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
84 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
85{
86 LY_ERR ret = LY_SUCCESS;
87 const char *name;
88 size_t u;
89 LY_ARRAY_COUNT_TYPE v;
90 const char *prefixed_name = NULL;
91
92 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret);
93 LY_CHECK_RET(ret);
94
95 ext->insubstmt = ext_p->insubstmt;
96 ext->insubstmt_index = ext_p->insubstmt_index;
97 ext->module = ctx->cur_mod;
98 ext->parent = parent;
99 ext->parent_type = parent_type;
100
101 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node *)ext->parent : NULL, "{extension}");
102
103 /* get module where the extension definition should be placed */
104 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {}
105 if (ext_p->yin) {
106 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
107 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
108 * YANG (import) prefix must be done here. */
109 if (!ly_strncmp(ctx->pmod->mod->ns, ext_p->name, u - 1)) {
110 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, &ext_p->name[u], 0, &prefixed_name), cleanup);
111 u = 0;
112 } else {
113 LY_ARRAY_FOR(ctx->pmod->imports, v) {
114 if (!ly_strncmp(ctx->pmod->imports[v].module->ns, ext_p->name, u - 1)) {
115 char *s;
116 LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->pmod->imports[v].prefix, &ext_p->name[u]) == -1,
117 ret = LY_EMEM, cleanup);
118 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->ctx, s, &prefixed_name), cleanup);
119 u = strlen(ctx->pmod->imports[v].prefix) + 1; /* add semicolon */
120 break;
121 }
122 }
123 }
124 if (!prefixed_name) {
125 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
126 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
127 ret = LY_EVALID;
128 goto cleanup;
129 }
130 } else {
131 prefixed_name = ext_p->name;
132 }
133 lysc_update_path(ctx, NULL, prefixed_name);
134
135 if (!ext_mod) {
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100136 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 +0200137 if (!ext_mod) {
138 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
139 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
140 ret = LY_EVALID;
141 goto cleanup;
142 } else if (!ext_mod->parsed->extensions) {
143 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
144 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
145 prefixed_name, ext_mod->name);
146 ret = LY_EVALID;
147 goto cleanup;
148 }
149 }
150 name = &prefixed_name[u];
151
152 /* find the parsed extension definition there */
153 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
154 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
155 /* compile extension definition and assign it */
156 LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def), cleanup);
157 break;
158 }
159 }
160 if (!ext->def) {
161 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
162 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
163 ret = LY_EVALID;
164 goto cleanup;
165 }
166
167 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
168 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
169 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
170 if (ext_p->yin && ext->def->argument && !ext->argument) {
171 /* Schema was parsed from YIN and an argument is expected, ... */
172 struct lysp_stmt *stmt = NULL;
173
174 if (ext->def->flags & LYS_YINELEM_TRUE) {
175 /* ... argument was the first XML child element */
176 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
177 /* TODO check namespace of the statement */
178 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
179 stmt = ext_p->child;
180 }
181 }
182 } else {
183 /* ... argument was one of the XML attributes which are represented as child stmt
184 * with LYS_YIN_ATTR flag */
185 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
186 if (!strcmp(stmt->stmt, ext->def->argument)) {
187 /* this is the extension's argument */
188 break;
189 }
190 }
191 }
192 if (!stmt) {
193 /* missing extension's argument */
194 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
195 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
196 ret = LY_EVALID;
197 goto cleanup;
198
199 }
200 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup);
201 stmt->flags |= LYS_YIN_ARGUMENT;
202 }
203 if (prefixed_name != ext_p->name) {
204 lydict_remove(ctx->ctx, ext_p->name);
205 ext_p->name = prefixed_name;
206 if (!ext_p->argument && ext->argument) {
207 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, ext->argument, 0, &ext_p->argument), cleanup);
208 }
209 }
210
211 if (ext->def->plugin && ext->def->plugin->compile) {
212 if (ext->argument) {
213 lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument);
214 }
215 LY_CHECK_GOTO(ret = ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
216 if (ext->argument) {
217 lysc_update_path(ctx, NULL, NULL);
218 }
219 }
220 ext_p->compiled = ext;
221
222cleanup:
223 if (prefixed_name && (prefixed_name != ext_p->name)) {
224 lydict_remove(ctx->ctx, prefixed_name);
225 }
226
227 lysc_update_path(ctx, NULL, NULL);
228 lysc_update_path(ctx, NULL, NULL);
229
230 return ret;
231}
232
233struct lysc_ext *
234lysc_ext_dup(struct lysc_ext *orig)
235{
236 ++orig->refcount;
237 return orig;
238}
239
240static void
241lysc_unres_dflt_free(const struct ly_ctx *ctx, struct lysc_unres_dflt *r)
242{
243 assert(!r->dflt || !r->dflts);
244 if (r->dflt) {
245 lysp_qname_free((struct ly_ctx *)ctx, r->dflt);
246 free(r->dflt);
247 } else {
248 FREE_ARRAY((struct ly_ctx *)ctx, r->dflts, lysp_qname_free);
249 }
250 free(r);
251}
252
253void
254lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
255{
256 int len;
257 uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
258
259 if (!name) {
260 /* removing last path segment */
261 if (ctx->path[ctx->path_len - 1] == '}') {
262 for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
263 if (ctx->path[ctx->path_len] == '=') {
264 ctx->path[ctx->path_len++] = '}';
265 } else {
266 /* not a top-level special tag, remove also preceiding '/' */
267 goto remove_nodelevel;
268 }
269 } else {
270remove_nodelevel:
271 for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
272 if (ctx->path_len == 0) {
273 /* top-level (last segment) */
274 ctx->path_len = 1;
275 }
276 }
277 /* set new terminating NULL-byte */
278 ctx->path[ctx->path_len] = '\0';
279 } else {
280 if (ctx->path_len > 1) {
281 if (!parent && (ctx->path[ctx->path_len - 1] == '}') && (ctx->path[ctx->path_len - 2] != '\'')) {
282 /* extension of the special tag */
283 nextlevel = 2;
284 --ctx->path_len;
285 } else {
286 /* there is already some path, so add next level */
287 nextlevel = 1;
288 }
289 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
290
291 if (nextlevel != 2) {
292 if ((parent && (parent->module == ctx->cur_mod)) || (!parent && (ctx->path_len > 1) && (name[0] == '{'))) {
293 /* module not changed, print the name unprefixed */
294 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
295 } else {
296 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->cur_mod->name, name);
297 }
298 } else {
299 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
300 }
301 if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
302 /* output truncated */
303 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
304 } else {
305 ctx->path_len += len;
306 }
307 }
308}
309
310/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200311 * @brief Compile information in the import statement - make sure there is the target module
312 * @param[in] ctx Compile context.
313 * @param[in] imp_p The parsed import statement structure to fill the module to.
314 * @return LY_ERR value.
315 */
316static LY_ERR
317lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
318{
319 const struct lys_module *mod = NULL;
320 LY_ERR ret = LY_SUCCESS;
321
322 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
323 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
324 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
325 if (!imp_p->module->parsed) {
326 /* try to use filepath if present */
327 if (imp_p->module->filepath) {
328 struct ly_in *in;
329 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
330 LOGINT(ctx->ctx);
331 } else {
332 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 +0100333 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, NULL, &mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200334 if (mod != imp_p->module) {
335 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
336 imp_p->module->filepath, imp_p->module->name);
337 mod = NULL;
338 }
339 }
340 ly_in_free(in, 1);
341 }
342 if (!mod) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100343 if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, NULL, (struct lys_module **)&mod)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200344 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
345 imp_p->module->name, ctx->cur_mod->name);
346 return LY_ENOTFOUND;
347 }
348 }
349 }
350
351 return ret;
352}
353
354LY_ERR
355lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
356 struct lysp_ident *identities_p, struct lysc_ident **identities)
357{
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100358 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200359 struct lysc_ctx context = {0};
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100360 struct lysc_ident *ident;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200361 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100362 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200363
364 assert(ctx_sc || ctx);
365
366 if (!ctx_sc) {
367 context.ctx = ctx;
368 context.cur_mod = parsed_mod->mod;
369 context.pmod = parsed_mod;
370 context.path_len = 1;
371 context.path[0] = '/';
372 ctx_sc = &context;
373 }
374
375 if (!identities_p) {
376 return LY_SUCCESS;
377 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200378
379 lysc_update_path(ctx_sc, NULL, "{identity}");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200380 LY_ARRAY_FOR(identities_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100381 /* evaluate if-features */
382 LY_CHECK_RET(lys_eval_iffeatures(ctx, identities_p[u].iffeatures, &enabled));
383 if (!enabled) {
384 continue;
385 }
386
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200387 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
388
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100389 /* add new compiled identity */
390 LY_ARRAY_NEW_RET(ctx_sc->ctx, *identities, ident, LY_EMEM);
391
392 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, ident->name, ret, done);
393 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, ident->dsc, ret, done);
394 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, ident->ref, ret, done);
395 ident->module = ctx_sc->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200396 /* 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 +0100397 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
398 ident->flags = identities_p[u].flags;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200399
400 lysc_update_path(ctx_sc, NULL, NULL);
401 }
402 lysc_update_path(ctx_sc, NULL, NULL);
403done:
404 return ret;
405}
406
407/**
408 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
409 *
410 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
411 *
412 * @param[in] ctx Compile context for logging.
413 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
414 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
415 * @return LY_SUCCESS if everything is ok.
416 * @return LY_EVALID if the identity is derived from itself.
417 */
418static LY_ERR
419lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
420{
421 LY_ERR ret = LY_SUCCESS;
422 LY_ARRAY_COUNT_TYPE u, v;
423 struct ly_set recursion = {0};
424 struct lysc_ident *drv;
425
426 if (!derived) {
427 return LY_SUCCESS;
428 }
429
430 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
431 if (ident == derived[u]) {
432 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
433 "Identity \"%s\" is indirectly derived from itself.", ident->name);
434 ret = LY_EVALID;
435 goto cleanup;
436 }
437 ret = ly_set_add(&recursion, derived[u], 0, NULL);
438 LY_CHECK_GOTO(ret, cleanup);
439 }
440
441 for (v = 0; v < recursion.count; ++v) {
442 drv = recursion.objs[v];
443 if (!drv->derived) {
444 continue;
445 }
446 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
447 if (ident == drv->derived[u]) {
448 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
449 "Identity \"%s\" is indirectly derived from itself.", ident->name);
450 ret = LY_EVALID;
451 goto cleanup;
452 }
453 ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
454 LY_CHECK_GOTO(ret, cleanup);
455 }
456 }
457
458cleanup:
459 ly_set_erase(&recursion, NULL);
460 return ret;
461}
462
463LY_ERR
464lys_compile_identity_bases(struct lysc_ctx *ctx, const struct lysp_module *base_pmod, const char **bases_p,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100465 struct lysc_ident *ident, struct lysc_ident ***bases, ly_bool *enabled)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200466{
467 LY_ARRAY_COUNT_TYPE u, v;
468 const char *s, *name;
469 const struct lys_module *mod;
470 struct lysc_ident **idref;
471
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100472 assert((ident && enabled) || bases);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200473
474 if ((LY_ARRAY_COUNT(bases_p) > 1) && (ctx->pmod->version < LYS_VERSION_1_1)) {
475 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
476 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
477 return LY_EVALID;
478 }
479
480 LY_ARRAY_FOR(bases_p, u) {
481 s = strchr(bases_p[u], ':');
482 if (s) {
483 /* prefixed identity */
484 name = &s[1];
Michal Vaskob2d55bf2020-11-02 15:42:43 +0100485 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 +0200486 } else {
487 name = bases_p[u];
488 mod = base_pmod->mod;
489 }
490 if (!mod) {
491 if (ident) {
492 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
493 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
494 } else {
495 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
496 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
497 }
498 return LY_EVALID;
499 }
500
501 idref = NULL;
502 LY_ARRAY_FOR(mod->identities, v) {
503 if (!strcmp(name, mod->identities[v].name)) {
504 if (ident) {
505 if (ident == &mod->identities[v]) {
506 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
507 "Identity \"%s\" is derived from itself.", ident->name);
508 return LY_EVALID;
509 }
510 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived));
511 /* we have match! store the backlink */
512 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
513 *idref = ident;
514 } else {
515 /* we have match! store the found identity */
516 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
517 *idref = &mod->identities[v];
518 }
519 break;
520 }
521 }
522 if (!idref || !(*idref)) {
523 if (ident) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100524 /* look into the parsed module to check whether the identity is not merely disabled */
525 LY_ARRAY_FOR(mod->parsed->identities, v) {
526 if (!strcmp(mod->parsed->identities[v].name, name)) {
527 *enabled = 0;
528 return LY_SUCCESS;
529 }
530 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
532 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
533 } else {
534 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
535 "Unable to find base (%s) of identityref.", bases_p[u]);
536 }
537 return LY_EVALID;
538 }
539 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100540
541 if (ident) {
542 *enabled = 1;
543 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200544 return LY_SUCCESS;
545}
546
547/**
548 * @brief For the given array of identities, set the backlinks from all their base identities.
549 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
550 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100551 * @param[in,out] idents Array of referencing identities to which the backlinks are supposed to be set. Any
552 * identities with disabled bases are removed.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200553 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
554 */
555static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100556lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident **idents)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200557{
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100558 LY_ARRAY_COUNT_TYPE u, v;
559 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200560
561 lysc_update_path(ctx, NULL, "{identity}");
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100562
563restart:
564 for (u = 0, v = 0; *idents && (u < LY_ARRAY_COUNT(*idents)); ++u) {
565 /* find matching parsed identity, the disabled ones are missing in the compiled array */
566 while (v < LY_ARRAY_COUNT(idents_p)) {
567 if (idents_p[v].name == (*idents)[u].name) {
568 break;
569 }
570 ++v;
571 }
572 assert(v < LY_ARRAY_COUNT(idents_p));
573
574 if (!idents_p[v].bases) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200575 continue;
576 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100577 lysc_update_path(ctx, NULL, (*idents)[u].name);
578 LY_CHECK_RET(lys_compile_identity_bases(ctx, (*idents)[u].module->parsed, idents_p[v].bases, &(*idents)[u], NULL,
579 &enabled));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200580 lysc_update_path(ctx, NULL, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100581
582 if (!enabled) {
583 /* remove the identity */
584 lysc_ident_free(ctx->ctx, &(*idents)[u]);
585 LY_ARRAY_DECREMENT(*idents);
586 if (u < LY_ARRAY_COUNT(*idents)) {
587 memmove(&(*idents)[u], &(*idents)[u + 1], (LY_ARRAY_COUNT(*idents) - u) * sizeof **idents);
588 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100589
590 /* revert compilation of all the previous identities */
591 for (v = 0; v < u; ++v) {
592 LY_ARRAY_FREE((*idents)[v].derived);
593 }
594
Michal Vasko74c82ae2020-11-03 17:36:53 +0100595 /* free the whole array if there are no identites left */
596 if (!LY_ARRAY_COUNT(*idents)) {
597 LY_ARRAY_FREE(*idents);
598 *idents = NULL;
599 }
600
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100601 /* restart the whole process without this identity */
602 goto restart;
603 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200604 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100605
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200606 lysc_update_path(ctx, NULL, NULL);
607 return LY_SUCCESS;
608}
609
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200610static void *
611lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
612{
613 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
614 if (substmts[u].stmt == stmt) {
615 return substmts[u].storage;
616 }
617 }
618 return NULL;
619}
620
621LY_ERR
622lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
623{
624 LY_ERR ret = LY_EVALID, r;
625 LY_ARRAY_COUNT_TYPE u;
626 struct lysp_stmt *stmt;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100627 struct lysp_qname *iffeatures, *iffeat;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200628 void *parsed = NULL, **compiled = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100629 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200630
631 /* check for invalid substatements */
632 for (stmt = ext->child; stmt; stmt = stmt->next) {
633 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
634 continue;
635 }
636 for (u = 0; substmts[u].stmt; ++u) {
637 if (substmts[u].stmt == stmt->kw) {
638 break;
639 }
640 }
641 if (!substmts[u].stmt) {
642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
643 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
644 goto cleanup;
645 }
646 }
647
648 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
649
650 /* 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) */
652 for (u = 0; substmts[u].stmt; ++u) {
653 ly_bool stmt_present = 0;
654
655 for (stmt = ext->child; stmt; stmt = stmt->next) {
656 if (substmts[u].stmt != stmt->kw) {
657 continue;
658 }
659
660 stmt_present = 1;
661 if (substmts[u].storage) {
662 switch (stmt->kw) {
663 case LY_STMT_STATUS:
664 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
665 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
666 break;
667 case LY_STMT_UNITS: {
668 const char **units;
669
670 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
671 /* single item */
672 if (*((const char **)substmts[u].storage)) {
673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
674 goto cleanup;
675 }
676 units = (const char **)substmts[u].storage;
677 } else {
678 /* sized array */
679 const char ***units_array = (const char ***)substmts[u].storage;
680 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
681 }
682 r = lydict_insert(ctx->ctx, stmt->arg, 0, units);
683 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
684 break;
685 }
686 case LY_STMT_TYPE: {
687 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
688 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
689
690 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
691 /* single item */
692 if (*(struct lysc_type **)substmts[u].storage) {
693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
694 goto cleanup;
695 }
696 compiled = substmts[u].storage;
697 } else {
698 /* sized array */
699 struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL;
700 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
701 compiled = (void *)type;
702 }
703
704 r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL);
705 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Michal Vaskobd6de2b2020-10-22 14:45:03 +0200706 r = lys_compile_type(ctx, NULL, flags ? *flags : 0, ctx->pmod, ext->name, parsed,
707 (struct lysc_type **)compiled, units && !*units ? units : NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200708 lysp_type_free(ctx->ctx, parsed);
709 free(parsed);
710 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
711 break;
712 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100713 case LY_STMT_IF_FEATURE:
714 iffeatures = NULL;
715 LY_ARRAY_NEW_GOTO(ctx->ctx, iffeatures, iffeat, ret, cleanup);
716 iffeat->str = stmt->arg;
717 iffeat->mod = ctx->pmod;
718 r = lys_eval_iffeatures(ctx->ctx, iffeatures, &enabled);
719 LY_ARRAY_FREE(iffeatures);
720 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
721 if (!enabled) {
722 /* it is disabled, remove the whole extension instance */
723 return LY_ENOT;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200724 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200725 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200726 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
727 * also note that in many statements their extensions are not taken into account */
728 default:
729 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.",
730 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
731 goto cleanup;
732 }
733 }
734 }
735
736 if (((substmts[u].cardinality == LY_STMT_CARD_MAND) || (substmts[u].cardinality == LY_STMT_CARD_SOME)) && !stmt_present) {
737 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
738 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
739 goto cleanup;
740 }
741 }
742
743 ret = LY_SUCCESS;
744
745cleanup:
746 return ret;
747}
748
749/**
750 * @brief Check when for cyclic dependencies.
751 *
752 * @param[in] set Set with all the referenced nodes.
753 * @param[in] node Node whose "when" referenced nodes are in @p set.
754 * @return LY_ERR value
755 */
756static LY_ERR
757lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
758{
759 struct lyxp_set tmp_set;
760 struct lyxp_set_scnode *xp_scnode;
761 uint32_t i, j;
762 LY_ARRAY_COUNT_TYPE u;
763 struct lysc_when *when;
764 LY_ERR ret = LY_SUCCESS;
765
766 memset(&tmp_set, 0, sizeof tmp_set);
767
768 /* prepare in_ctx of the set */
769 for (i = 0; i < set->used; ++i) {
770 xp_scnode = &set->val.scnodes[i];
771
772 if (xp_scnode->in_ctx != -1) {
773 /* check node when, skip the context node (it was just checked) */
774 xp_scnode->in_ctx = 1;
775 }
776 }
777
778 for (i = 0; i < set->used; ++i) {
779 xp_scnode = &set->val.scnodes[i];
780 if (xp_scnode->in_ctx != 1) {
781 /* already checked */
782 continue;
783 }
784
785 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) ||
786 !xp_scnode->scnode->when) {
787 /* no when to check */
788 xp_scnode->in_ctx = 0;
789 continue;
790 }
791
792 node = xp_scnode->scnode;
793 do {
794 LY_ARRAY_FOR(node->when, u) {
795 when = node->when[u];
796 ret = lyxp_atomize(when->cond, node->module, LY_PREF_SCHEMA_RESOLVED, when->prefixes, when->context,
797 &tmp_set, LYXP_SCNODE_SCHEMA);
798 if (ret != LY_SUCCESS) {
799 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
800 goto cleanup;
801 }
802
803 for (j = 0; j < tmp_set.used; ++j) {
804 /* skip roots'n'stuff */
805 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
806 /* try to find this node in our set */
807 uint32_t idx;
808 if (lyxp_set_scnode_contains(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1, &idx) && (set->val.scnodes[idx].in_ctx == -1)) {
809 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 */
815 tmp_set.val.scnodes[j].in_ctx = 1;
816 } else {
817 /* no when, nothing to check */
818 tmp_set.val.scnodes[j].in_ctx = 0;
819 }
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) */
831 set->val.scnodes[i].in_ctx = -1;
832 }
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,
863 void *prefix_data, ly_bool implement, 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 Vasko7b1ad1a2020-11-02 15:41:27 +0100891 LY_CHECK_RET(lys_set_implemented((struct lys_module *)mod, NULL));
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.
907 * @return LY_ERR value
908 */
909static LY_ERR
910lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
911{
912 struct lyxp_set tmp_set;
913 uint32_t i, opts;
914 LY_ARRAY_COUNT_TYPE u;
915 ly_bool input_done = 0;
916 struct lysc_when **when = NULL;
917 struct lysc_must *musts = NULL;
918 LY_ERR ret = LY_SUCCESS;
919 const struct lysc_node *op;
Michal Vaskocfaff232020-10-20 09:35:14 +0200920 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200921
922 memset(&tmp_set, 0, sizeof tmp_set);
923 opts = LYXP_SCNODE_SCHEMA;
924 if (node->flags & LYS_CONFIG_R) {
925 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent) {}
926 if (op) {
927 /* we are actually in output */
928 opts = LYXP_SCNODE_OUTPUT;
929 }
930 }
931
932 switch (node->nodetype) {
933 case LYS_CONTAINER:
934 when = ((struct lysc_node_container *)node)->when;
935 musts = ((struct lysc_node_container *)node)->musts;
936 break;
937 case LYS_CHOICE:
938 when = ((struct lysc_node_choice *)node)->when;
939 break;
940 case LYS_LEAF:
941 when = ((struct lysc_node_leaf *)node)->when;
942 musts = ((struct lysc_node_leaf *)node)->musts;
943 break;
944 case LYS_LEAFLIST:
945 when = ((struct lysc_node_leaflist *)node)->when;
946 musts = ((struct lysc_node_leaflist *)node)->musts;
947 break;
948 case LYS_LIST:
949 when = ((struct lysc_node_list *)node)->when;
950 musts = ((struct lysc_node_list *)node)->musts;
951 break;
952 case LYS_ANYXML:
953 case LYS_ANYDATA:
954 when = ((struct lysc_node_anydata *)node)->when;
955 musts = ((struct lysc_node_anydata *)node)->musts;
956 break;
957 case LYS_CASE:
958 when = ((struct lysc_node_case *)node)->when;
959 break;
960 case LYS_NOTIF:
961 when = ((struct lysc_notif *)node)->when;
962 musts = ((struct lysc_notif *)node)->musts;
963 break;
964 case LYS_RPC:
965 case LYS_ACTION:
966 /* first process when and input musts */
967 when = ((struct lysc_action *)node)->when;
968 musts = ((struct lysc_action *)node)->input.musts;
969 break;
970 default:
971 /* nothing to check */
972 break;
973 }
974
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200975 LY_ARRAY_FOR(when, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +0200976 /* first check whether all the referenced modules are implemented */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200977 mod = NULL;
978 ret = lys_compile_expr_implement(ctx->ctx, when[u]->cond, LY_PREF_SCHEMA_RESOLVED, when[u]->prefixes,
979 ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, &mod);
980 if (ret) {
981 goto cleanup;
982 } else if (mod) {
Michal Vaskocfaff232020-10-20 09:35:14 +0200983 LOGWRN(ctx->ctx, "When condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
984 when[u]->cond->expr, mod->name);
985 continue;
986 }
987
988 /* check "when" */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200989 ret = lyxp_atomize(when[u]->cond, node->module, LY_PREF_SCHEMA_RESOLVED, when[u]->prefixes, when[u]->context,
990 &tmp_set, opts);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200991 if (ret) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200992 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
993 goto cleanup;
994 }
995
996 ctx->path[0] = '\0';
997 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
998 for (i = 0; i < tmp_set.used; ++i) {
999 /* skip roots'n'stuff */
1000 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
1001 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
1002
1003 /* XPath expression cannot reference "lower" status than the node that has the definition */
1004 ret = lysc_check_status(ctx, when[u]->flags, node->module, node->name, schema->flags, schema->module,
1005 schema->name);
1006 LY_CHECK_GOTO(ret, cleanup);
1007
1008 /* check dummy node accessing */
1009 if (schema == node) {
1010 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
1011 ret = LY_EVALID;
1012 goto cleanup;
1013 }
1014 }
1015 }
1016
1017 /* check cyclic dependencies */
1018 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
1019 LY_CHECK_GOTO(ret, cleanup);
1020
1021 lyxp_set_free_content(&tmp_set);
1022 }
1023
1024check_musts:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001025 LY_ARRAY_FOR(musts, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +02001026 /* first check whether all the referenced modules are implemented */
Michal Vasko25d6ad02020-10-22 12:20:22 +02001027 mod = NULL;
1028 ret = lys_compile_expr_implement(ctx->ctx, musts[u].cond, LY_PREF_SCHEMA_RESOLVED, musts[u].prefixes,
1029 ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, &mod);
1030 if (ret) {
1031 goto cleanup;
1032 } else if (mod) {
Michal Vaskocfaff232020-10-20 09:35:14 +02001033 LOGWRN(ctx->ctx, "Must condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
1034 musts[u].cond->expr, mod->name);
1035 continue;
1036 }
1037
1038 /* check "must" */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001039 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 +02001040 if (ret) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001041 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
1042 goto cleanup;
1043 }
1044
1045 ctx->path[0] = '\0';
1046 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1047 for (i = 0; i < tmp_set.used; ++i) {
1048 /* skip roots'n'stuff */
1049 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
1050 /* XPath expression cannot reference "lower" status than the node that has the definition */
1051 ret = lysc_check_status(ctx, node->flags, node->module, node->name, tmp_set.val.scnodes[i].scnode->flags,
1052 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
1053 LY_CHECK_GOTO(ret, cleanup);
1054 }
1055 }
1056
1057 lyxp_set_free_content(&tmp_set);
1058 }
1059
1060 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
1061 /* now check output musts */
1062 input_done = 1;
1063 when = NULL;
1064 musts = ((struct lysc_action *)node)->output.musts;
1065 opts = LYXP_SCNODE_OUTPUT;
1066 goto check_musts;
1067 }
1068
1069cleanup:
1070 lyxp_set_free_content(&tmp_set);
1071 return ret;
1072}
1073
1074/**
1075 * @brief Check leafref for its target existence on a complete compiled schema tree.
1076 *
1077 * @param[in] ctx Compile context.
1078 * @param[in] node Context node for the leafref.
1079 * @param[in] lref Leafref to check/resolve.
1080 * @return LY_ERR value.
1081 */
1082static LY_ERR
1083lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
1084{
1085 const struct lysc_node *target = NULL, *siter;
1086 struct ly_path *p;
1087 struct lysc_type *type;
1088
1089 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
1090
1091 /* try to find the target */
1092 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, lysc_is_output(node) ?
1093 LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, &p));
1094
1095 /* get the target node */
1096 target = p[LY_ARRAY_COUNT(p) - 1].node;
1097 ly_path_free(node->module->ctx, p);
1098
1099 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1100 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
1101 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
1102 lref->path->expr, lys_nodetype2str(target->nodetype));
1103 return LY_EVALID;
1104 }
1105
1106 /* check status */
1107 ctx->path[0] = '\0';
1108 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1109 ctx->path_len = strlen(ctx->path);
1110 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
1111 return LY_EVALID;
1112 }
1113 ctx->path_len = 1;
1114 ctx->path[1] = '\0';
1115
1116 /* check config */
1117 if (lref->require_instance) {
1118 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {}
1119 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
1120 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
1121 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
1122 return LY_EVALID;
1123 }
1124 }
1125
1126 /* store the target's type and check for circular chain of leafrefs */
1127 lref->realtype = ((struct lysc_node_leaf *)target)->type;
1128 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
1129 if (type == (struct lysc_type *)lref) {
1130 /* circular chain detected */
1131 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
1132 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
1133 return LY_EVALID;
1134 }
1135 }
1136
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001137 /* TODO check if leafref and its target are under common if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001138
1139 return LY_SUCCESS;
1140}
1141
1142static LY_ERR
1143lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
1144{
1145 struct lysc_ext_instance *ext;
1146 struct lysp_ext_instance *ext_p = NULL;
1147 struct lysp_stmt *stmt;
1148 const struct lys_module *ext_mod;
1149 LY_ERR ret = LY_SUCCESS;
1150
1151 /* create the parsed extension instance manually */
1152 ext_p = calloc(1, sizeof *ext_p);
1153 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1154 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup);
1155 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "default", 0, &ext_p->argument), cleanup);
1156 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
1157 ext_p->insubstmt_index = 0;
1158
1159 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1160 LY_CHECK_ERR_GOTO(!stmt, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1161 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup);
1162 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "boolean", 0, &stmt->arg), cleanup);
1163 stmt->kw = LY_STMT_TYPE;
1164
1165 /* allocate new extension instance */
1166 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
1167
1168 /* manually get extension definition module */
1169 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
1170
1171 /* compile the extension instance */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001172 ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod);
1173 if (ret == LY_ENOT) {
1174 /* free the extension */
1175 lysc_ext_instance_free(ctx->ctx, ext);
1176 LY_ARRAY_DECREMENT_FREE(mod->compiled->exts);
1177
1178 ret = LY_SUCCESS;
1179 goto cleanup;
1180 } else if (ret) {
1181 goto cleanup;
1182 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001183
1184cleanup:
1185 lysp_ext_instance_free(ctx->ctx, ext_p);
1186 free(ext_p);
1187 return ret;
1188}
1189
1190/**
1191 * @brief Compile default value(s) for leaf or leaf-list expecting a complete compiled schema tree.
1192 *
1193 * @param[in] ctx Compile context.
1194 * @param[in] node Leaf or leaf-list to compile the default value(s) for.
1195 * @param[in] type Type of the default value.
1196 * @param[in] dflt Default value.
1197 * @param[in] dflt_pmod Parsed module of the @p dflt to resolve possible prefixes.
1198 * @param[in,out] storage Storage for the compiled default value.
1199 * @return LY_ERR value.
1200 */
1201static LY_ERR
1202lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_type *type, const char *dflt,
1203 const struct lysp_module *dflt_pmod, struct lyd_value *storage)
1204{
1205 LY_ERR ret;
Michal Vasko25d6ad02020-10-22 12:20:22 +02001206 uint32_t options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001207 struct ly_err_item *err = NULL;
1208
Michal Vasko25d6ad02020-10-22 12:20:22 +02001209 options = (ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED) ? LY_TYPE_STORE_IMPLEMENT : 0;
1210 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), options, LY_PREF_SCHEMA, (void *)dflt_pmod,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001211 LYD_HINT_SCHEMA, node, storage, &err);
1212 if (ret == LY_EINCOMPLETE) {
1213 /* we have no data so we will not be resolving it */
1214 ret = LY_SUCCESS;
1215 }
1216
1217 if (ret) {
1218 ctx->path[0] = '\0';
1219 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1220 if (err) {
1221 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1222 "Invalid default - value does not fit the type (%s).", err->msg);
1223 ly_err_free(err);
1224 } else {
Michal Vasko25d6ad02020-10-22 12:20:22 +02001225 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 +02001226 }
1227 return ret;
1228 }
1229
1230 ++((struct lysc_type *)storage->realtype)->refcount;
1231 return LY_SUCCESS;
1232}
1233
1234/**
1235 * @brief Compile default value of a leaf expecting a complete compiled schema tree.
1236 *
1237 * @param[in] ctx Compile context.
1238 * @param[in] leaf Leaf that the default value is for.
1239 * @param[in] dflt Default value to compile.
1240 * @return LY_ERR value.
1241 */
1242static LY_ERR
1243lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
1244{
1245 LY_ERR ret;
1246
1247 assert(!leaf->dflt);
1248
1249 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
1250 /* ignore default values for keys and mandatory leaves */
1251 return LY_SUCCESS;
1252 }
1253
1254 /* allocate the default value */
1255 leaf->dflt = calloc(1, sizeof *leaf->dflt);
1256 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
1257
1258 /* store the default value */
1259 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt->str, dflt->mod, leaf->dflt);
1260 if (ret) {
1261 free(leaf->dflt);
1262 leaf->dflt = NULL;
1263 }
1264
1265 return ret;
1266}
1267
1268/**
1269 * @brief Compile default values of a leaf-list expecting a complete compiled schema tree.
1270 *
1271 * @param[in] ctx Compile context.
1272 * @param[in] llist Leaf-list that the default value(s) are for.
1273 * @param[in] dflt Default value to compile, in case of a single value.
1274 * @param[in] dflts Sized array of default values, in case of more values.
1275 * @return LY_ERR value.
1276 */
1277static LY_ERR
1278lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflt,
1279 struct lysp_qname *dflts)
1280{
1281 LY_ERR ret;
1282 LY_ARRAY_COUNT_TYPE orig_count, u, v;
1283
1284 assert(dflt || dflts);
1285
1286 if (llist->dflts) {
1287 /* there were already some defaults and we are adding new by deviations */
1288 assert(dflts);
1289 orig_count = LY_ARRAY_COUNT(llist->dflts);
1290 } else {
1291 orig_count = 0;
1292 }
1293
1294 /* allocate new items */
1295 if (dflts) {
1296 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
1297 } else {
1298 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
1299 }
1300
1301 /* fill each new default value */
1302 if (dflts) {
1303 LY_ARRAY_FOR(dflts, u) {
1304 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
1305 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u].str, dflts[u].mod,
1306 llist->dflts[orig_count + u]);
1307 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
1308 LY_ARRAY_INCREMENT(llist->dflts);
1309 }
1310 } else {
1311 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
1312 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt->str, dflt->mod,
1313 llist->dflts[orig_count]);
1314 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
1315 LY_ARRAY_INCREMENT(llist->dflts);
1316 }
1317
1318 /* check default value uniqueness */
1319 if (llist->flags & LYS_CONFIG_W) {
1320 /* configuration data values must be unique - so check the default values */
1321 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
1322 for (v = 0; v < u; ++v) {
1323 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
1324 lysc_update_path(ctx, llist->parent, llist->name);
1325 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1326 "Configuration leaf-list has multiple defaults of the same value \"%s\".",
1327 llist->dflts[u]->canonical);
1328 lysc_update_path(ctx, NULL, NULL);
1329 return LY_EVALID;
1330 }
1331 }
1332 }
1333 }
1334
1335 return LY_SUCCESS;
1336}
1337
1338/**
1339 * @brief Finish compilation of all the unres sets of a compile context.
1340 *
1341 * @param[in] ctx Compile context with unres sets.
1342 * @return LY_ERR value.
1343 */
1344static LY_ERR
1345lys_compile_unres(struct lysc_ctx *ctx)
1346{
1347 struct lysc_node *node;
1348 struct lysc_type *type, *typeiter;
1349 struct lysc_type_leafref *lref;
1350 struct lysc_augment *aug;
1351 struct lysc_deviation *dev;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001352 struct ly_set disabled_op = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001353 LY_ARRAY_COUNT_TYPE v;
1354 uint32_t i;
1355
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001356#define ARRAY_DEL_ITEM(array, item) \
1357 { \
1358 LY_ARRAY_COUNT_TYPE u__; \
1359 LY_ARRAY_FOR(array, u__) { \
1360 if ((array) + u__ == item) { \
1361 LY_ARRAY_DECREMENT(array); \
1362 if (u__ < LY_ARRAY_COUNT(array)) { \
1363 memmove((array) + u__, (array) + u__ + 1, (LY_ARRAY_COUNT(array) - u__) * sizeof *(array)); \
1364 } \
1365 if (!LY_ARRAY_COUNT(array)) { \
1366 LY_ARRAY_FREE(array); \
1367 (array) = NULL; \
1368 } \
1369 break; \
1370 } \
1371 } \
1372 }
1373
1374 /* remove all disabled nodes */
1375 for (i = 0; i < ctx->disabled.count; ++i) {
1376 node = ctx->disabled.snodes[i];
1377 if (node->flags & LYS_KEY) {
1378 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Key \"%s\" is disabled by its if-features.",
1379 node->name);
1380 return LY_EVALID;
1381 }
1382
1383 if (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1384 if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1385 lysc_action_free(ctx->ctx, (struct lysc_action *)node);
1386 } else {
1387 lysc_notif_free(ctx->ctx, (struct lysc_notif *)node);
1388 }
1389 /* remember all freed RPCs/actions/notifs */
1390 ly_set_add(&disabled_op, node, 1, NULL);
1391 } else {
1392 lysc_node_free(ctx->ctx, node, 1);
1393 }
1394 }
1395
1396 /* remove ops also from their arrays, from end so as not to move other items and change these pointer targets */
1397 i = disabled_op.count;
1398 while (i) {
1399 --i;
1400 node = disabled_op.snodes[i];
1401 if (node->nodetype == LYS_RPC) {
1402 ARRAY_DEL_ITEM(node->module->compiled->rpcs, (struct lysc_action *)node);
1403 } else if (node->nodetype == LYS_ACTION) {
1404 ARRAY_DEL_ITEM(*lysc_node_actions_p(node->parent), (struct lysc_action *)node);
1405 } else if (node->parent) {
1406 ARRAY_DEL_ITEM(*lysc_node_notifs_p(node->parent), (struct lysc_notif *)node);
1407 } else {
1408 ARRAY_DEL_ITEM(node->module->compiled->notifs, (struct lysc_notif *)node);
1409 }
1410 }
1411 ly_set_erase(&disabled_op, NULL);
1412
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001413 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
1414 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
1415 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
1416 for (i = 0; i < ctx->leafrefs.count; ++i) {
1417 node = ctx->leafrefs.objs[i];
1418 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
1419 type = ((struct lysc_node_leaf *)node)->type;
1420 if (type->basetype == LY_TYPE_LEAFREF) {
1421 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
1422 } else if (type->basetype == LY_TYPE_UNION) {
1423 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
1424 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
1425 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
1426 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
1427 }
1428 }
1429 }
1430 }
1431 for (i = 0; i < ctx->leafrefs.count; ++i) {
1432 /* store pointer to the real type */
1433 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
1434 if (type->basetype == LY_TYPE_LEAFREF) {
1435 for (typeiter = ((struct lysc_type_leafref *)type)->realtype;
1436 typeiter->basetype == LY_TYPE_LEAFREF;
1437 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
1438 ((struct lysc_type_leafref *)type)->realtype = typeiter;
1439 } else if (type->basetype == LY_TYPE_UNION) {
1440 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
1441 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
1442 for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype;
1443 typeiter->basetype == LY_TYPE_LEAFREF;
1444 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
1445 ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter;
1446 }
1447 }
1448 }
1449 }
1450
1451 /* check xpath */
1452 for (i = 0; i < ctx->xpath.count; ++i) {
1453 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
1454 }
1455
1456 /* finish incomplete default values compilation */
1457 for (i = 0; i < ctx->dflts.count; ++i) {
1458 struct lysc_unres_dflt *r = ctx->dflts.objs[i];
1459 if (r->leaf->nodetype == LYS_LEAF) {
1460 LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt));
1461 } else {
1462 LY_CHECK_RET(lys_compile_unres_llist_dflts(ctx, r->llist, r->dflt, r->dflts));
1463 }
1464 }
1465
1466 /* check that all augments were applied */
1467 for (i = 0; i < ctx->augs.count; ++i) {
1468 aug = ctx->augs.objs[i];
1469 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
1470 "Augment target node \"%s\" from module \"%s\" was not found.", aug->nodeid->expr,
1471 LYSP_MODULE_NAME(aug->nodeid_pmod));
1472 }
1473 if (ctx->augs.count) {
1474 return LY_ENOTFOUND;
1475 }
1476
1477 /* check that all deviations were applied */
1478 for (i = 0; i < ctx->devs.count; ++i) {
1479 dev = ctx->devs.objs[i];
1480 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
1481 "Deviation(s) target node \"%s\" from module \"%s\" was not found.", dev->nodeid->expr,
1482 LYSP_MODULE_NAME(dev->dev_pmods[0]));
1483 }
1484 if (ctx->devs.count) {
1485 return LY_ENOTFOUND;
1486 }
1487
1488 return LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001489#undef ARRAY_DEL_ITEM
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001490}
1491
1492/**
1493 * @brief Compile identites in the current module and all its submodules.
1494 *
1495 * @param[in] ctx Compile context.
1496 * @return LY_ERR value.
1497 */
1498static LY_ERR
1499lys_compile_identities(struct lysc_ctx *ctx)
1500{
1501 struct lysp_submodule *submod;
1502 LY_ARRAY_COUNT_TYPE u;
1503
1504 if (!ctx->cur_mod->identities) {
1505 LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, ctx->cur_mod->parsed->identities, &ctx->cur_mod->identities));
1506 LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, u) {
1507 submod = ctx->cur_mod->parsed->includes[u].submodule;
1508 LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, submod->identities, &ctx->cur_mod->identities));
1509 }
1510 }
1511
1512 if (ctx->cur_mod->parsed->identities) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001513 LY_CHECK_RET(lys_compile_identities_derived(ctx, ctx->cur_mod->parsed->identities, &ctx->cur_mod->identities));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001514 }
1515 lysc_update_path(ctx, NULL, "{submodule}");
1516 LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, u) {
1517
1518 submod = ctx->cur_mod->parsed->includes[u].submodule;
1519 if (submod->identities) {
1520 lysc_update_path(ctx, NULL, submod->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001521 LY_CHECK_RET(lys_compile_identities_derived(ctx, submod->identities, &ctx->cur_mod->identities));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001522 lysc_update_path(ctx, NULL, NULL);
1523 }
1524 }
1525 lysc_update_path(ctx, NULL, NULL);
1526
1527 return LY_SUCCESS;
1528}
1529
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001530static LY_ERR
1531lys_recompile_mod(struct lys_module *mod, ly_bool log)
1532{
1533 LY_ERR ret;
1534 uint32_t prev_lo;
1535 struct lysp_import *imp;
1536
1537 if (!mod->implemented || mod->compiled) {
1538 /* nothing to do */
1539 return LY_SUCCESS;
1540 }
1541
1542 /* we need all implemented imports to be recompiled */
1543 LY_ARRAY_FOR(mod->parsed->imports, struct lysp_import, imp) {
1544 LY_CHECK_RET(lys_recompile_mod(imp->module, log));
1545 }
1546
1547 if (!log) {
1548 /* recompile, must succeed because it was already compiled; hide messages because any
1549 * warnings were already printed, are not really relevant, and would hide the real error */
1550 prev_lo = ly_log_options(0);
1551 }
1552 ret = lys_compile(mod, 0);
1553 if (!log) {
1554 ly_log_options(prev_lo);
1555 }
1556
1557 if (ret && !log) {
1558 LOGERR(mod->ctx, ret, "Recompilation of module \"%s\" failed.", mod->name);
1559 }
1560 return ret;
1561}
1562
1563LY_ERR
1564lys_recompile(struct ly_ctx *ctx, const struct lys_module *skip)
1565{
1566 uint32_t idx;
1567 struct lys_module *mod;
1568 LY_ERR ret = LY_SUCCESS, r;
1569
1570 /* free all the modules */
1571 for (idx = 0; idx < ctx->list.count; ++idx) {
1572 mod = ctx->list.objs[idx];
1573 /* skip this module */
1574 if (skip && (mod == skip)) {
1575 continue;
1576 }
1577
1578 if (mod->compiled) {
1579 /* free the module */
1580 lysc_module_free(mod->compiled, NULL);
1581 mod->compiled = NULL;
1582 }
1583
1584 /* free precompiled iffeatures */
1585 lys_free_feature_iffeatures(mod->parsed);
1586 }
1587
1588 /* recompile all the modules */
1589 for (idx = 0; idx < ctx->list.count; ++idx) {
1590 mod = ctx->list.objs[idx];
1591
1592 /* skip this module */
1593 if (skip && (mod == skip)) {
1594 continue;
1595 }
1596
1597 /* compile again */
1598 r = lys_recompile_mod(mod, skip ? 1 : 0);
1599 if (r) {
1600 if (skip) {
1601 return r;
1602 }
1603 ret = r;
1604 }
1605 }
1606
1607 return ret;
1608}
1609
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001610LY_ERR
1611lys_compile(struct lys_module *mod, uint32_t options)
1612{
1613 struct lysc_ctx ctx = {0};
1614 struct lysc_module *mod_c;
1615 struct lysp_module *sp;
1616 struct lysp_submodule *submod;
1617 struct lysp_node *pnode;
1618 struct lysp_grp *grps;
Michal Vasko5347e3a2020-11-03 17:14:57 +01001619 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001620 uint32_t i;
1621 LY_ERR ret = LY_SUCCESS;
1622
1623 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
1624
1625 if (!mod->implemented) {
1626 /* just imported modules are not compiled */
1627 return LY_SUCCESS;
1628 }
1629
1630 /* context will be changed */
1631 ++mod->ctx->module_set_id;
1632
1633 sp = mod->parsed;
1634
1635 ctx.ctx = mod->ctx;
1636 ctx.cur_mod = mod;
1637 ctx.pmod = sp;
1638 ctx.options = options;
1639 ctx.path_len = 1;
1640 ctx.path[0] = '/';
1641
1642 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
1643 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
1644 mod_c->mod = mod;
1645
1646 /* process imports */
1647 LY_ARRAY_FOR(sp->imports, u) {
1648 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
1649 }
1650
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001651 /* identities */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001652 LY_CHECK_GOTO(ret = lys_compile_identities(&ctx), error);
1653
1654 /* augments and deviations */
1655 LY_CHECK_GOTO(ret = lys_precompile_augments_deviations(&ctx), error);
1656
1657 /* compile augments and deviations of our module from other modules so they can be applied during compilation */
1658 LY_CHECK_GOTO(ret = lys_precompile_own_augments(&ctx), error);
1659 LY_CHECK_GOTO(ret = lys_precompile_own_deviations(&ctx), error);
1660
1661 /* data nodes */
1662 LY_LIST_FOR(sp->data, pnode) {
1663 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), error);
1664 }
1665
1666 /* top-level RPCs and notifications */
Michal Vasko5347e3a2020-11-03 17:14:57 +01001667 COMPILE_OP_ARRAY_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, lys_compile_action, 0, ret, error);
1668 COMPILE_OP_ARRAY_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, lys_compile_notif, 0, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001669
1670 /* extension instances */
1671 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
1672
1673 /* the same for submodules */
1674 LY_ARRAY_FOR(sp->includes, u) {
1675 submod = sp->includes[u].submodule;
1676 ctx.pmod = (struct lysp_module *)submod;
1677
1678 LY_LIST_FOR(submod->data, pnode) {
1679 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
1680 LY_CHECK_GOTO(ret, error);
1681 }
1682
Michal Vasko5347e3a2020-11-03 17:14:57 +01001683 COMPILE_OP_ARRAY_GOTO(&ctx, submod->rpcs, mod_c->rpcs, NULL, lys_compile_action, 0, ret, error);
1684 COMPILE_OP_ARRAY_GOTO(&ctx, submod->notifs, mod_c->notifs, NULL, lys_compile_notif, 0, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001685
1686 COMPILE_EXTS_GOTO(&ctx, submod->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
1687 }
1688
1689 /* finish compilation for all unresolved items in the context */
1690 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
1691
1692 /* validate non-instantiated groupings from the parsed schema,
1693 * without it we would accept even the schemas with invalid grouping specification */
1694 ctx.pmod = sp;
1695 ctx.options |= LYS_COMPILE_GROUPING;
1696 LY_ARRAY_FOR(sp->groupings, u) {
1697 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
1698 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, &sp->groupings[u]), error);
1699 }
1700 }
1701 LY_LIST_FOR(sp->data, pnode) {
1702 grps = (struct lysp_grp *)lysp_node_groupings(pnode);
1703 LY_ARRAY_FOR(grps, u) {
1704 if (!(grps[u].flags & LYS_USED_GRP)) {
1705 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &grps[u]), error);
1706 }
1707 }
1708 }
1709 LY_ARRAY_FOR(sp->includes, u) {
1710 submod = sp->includes[u].submodule;
1711 ctx.pmod = (struct lysp_module *)submod;
1712
1713 LY_ARRAY_FOR(submod->groupings, u) {
1714 if (!(submod->groupings[u].flags & LYS_USED_GRP)) {
1715 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, &submod->groupings[u]), error);
1716 }
1717 }
1718 LY_LIST_FOR(submod->data, pnode) {
1719 grps = (struct lysp_grp *)lysp_node_groupings(pnode);
1720 LY_ARRAY_FOR(grps, u) {
1721 if (!(grps[u].flags & LYS_USED_GRP)) {
1722 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &grps[u]), error);
1723 }
1724 }
1725 }
1726 }
1727 ctx.pmod = sp;
1728
1729#if 0
1730 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
1731 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
1732 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
1733 * the anotation definitions available in the internal schema structure. */
1734 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
1735 if (lyp_add_ietf_netconf_annotations(mod)) {
1736 lys_free(mod, NULL, 1, 1);
1737 return NULL;
1738 }
1739 }
1740#endif
1741
1742 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
1743 if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
1744 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error);
1745 }
1746
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001747 /* there can be no leftover deviations or augments */
1748 LY_CHECK_ERR_GOTO(ctx.augs.count, LOGINT(ctx.ctx); ret = LY_EINT, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001749 LY_CHECK_ERR_GOTO(ctx.devs.count, LOGINT(ctx.ctx); ret = LY_EINT, error);
1750
1751 for (i = 0; i < ctx.dflts.count; ++i) {
1752 lysc_unres_dflt_free(ctx.ctx, ctx.dflts.objs[i]);
1753 }
1754 ly_set_erase(&ctx.dflts, NULL);
1755 ly_set_erase(&ctx.xpath, NULL);
1756 ly_set_erase(&ctx.leafrefs, NULL);
1757 ly_set_erase(&ctx.groupings, NULL);
1758 ly_set_erase(&ctx.tpdf_chain, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001759 ly_set_erase(&ctx.disabled, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001760 ly_set_erase(&ctx.augs, NULL);
1761 ly_set_erase(&ctx.devs, NULL);
1762 ly_set_erase(&ctx.uses_augs, NULL);
1763 ly_set_erase(&ctx.uses_rfns, NULL);
1764
1765 return LY_SUCCESS;
1766
1767error:
1768 lys_precompile_augments_deviations_revert(ctx.ctx, mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001769 for (i = 0; i < ctx.dflts.count; ++i) {
1770 lysc_unres_dflt_free(ctx.ctx, ctx.dflts.objs[i]);
1771 }
1772 ly_set_erase(&ctx.dflts, NULL);
1773 ly_set_erase(&ctx.xpath, NULL);
1774 ly_set_erase(&ctx.leafrefs, NULL);
1775 ly_set_erase(&ctx.groupings, NULL);
1776 ly_set_erase(&ctx.tpdf_chain, NULL);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001777 ly_set_erase(&ctx.disabled, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001778 for (i = 0; i < ctx.augs.count; ++i) {
1779 lysc_augment_free(ctx.ctx, ctx.augs.objs[i]);
1780 }
1781 ly_set_erase(&ctx.augs, NULL);
1782 for (i = 0; i < ctx.devs.count; ++i) {
1783 lysc_deviation_free(ctx.ctx, ctx.devs.objs[i]);
1784 }
1785 ly_set_erase(&ctx.devs, NULL);
1786 for (i = 0; i < ctx.uses_augs.count; ++i) {
1787 lysc_augment_free(ctx.ctx, ctx.uses_augs.objs[i]);
1788 }
1789 ly_set_erase(&ctx.uses_augs, NULL);
1790 for (i = 0; i < ctx.uses_rfns.count; ++i) {
1791 lysc_refine_free(ctx.ctx, ctx.uses_rfns.objs[i]);
1792 }
1793 ly_set_erase(&ctx.uses_rfns, NULL);
1794 lysc_module_free(mod_c, NULL);
1795 mod->compiled = NULL;
1796
1797 return ret;
1798}