blob: f6c2552b3143d8c68c149a9ab2dc834edb72e97e [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"
40#include "set.h"
41#include "tree.h"
42#include "tree_data.h"
43#include "tree_data_internal.h"
44#include "tree_schema.h"
45#include "tree_schema_internal.h"
46#include "xpath.h"
47
48#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
49 if (ARRAY) { \
50 for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
51 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
52 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
53 return LY_EVALID; \
54 } \
55 } \
56 }
57
58/**
59 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
60 */
61static LY_ERR
62lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
63{
64 LY_ERR ret = LY_SUCCESS;
65
66 if (!ext_p->compiled) {
67 lysc_update_path(ctx, NULL, "{extension}");
68 lysc_update_path(ctx, NULL, ext_p->name);
69
70 /* compile the extension definition */
71 ext_p->compiled = calloc(1, sizeof **ext);
72 ext_p->compiled->refcount = 1;
73 DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done);
74 DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext_p->compiled->argument, ret, done);
75 ext_p->compiled->module = (struct lys_module *)ext_mod;
76 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done);
77
78 lysc_update_path(ctx, NULL, NULL);
79 lysc_update_path(ctx, NULL, NULL);
80
81 /* find extension definition plugin */
82 ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled);
83 }
84
85 *ext = lysc_ext_dup(ext_p->compiled);
86
87done:
88 return ret;
89}
90
91LY_ERR
92lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
93 LYEXT_PARENT parent_type, const struct lys_module *ext_mod)
94{
95 LY_ERR ret = LY_SUCCESS;
96 const char *name;
97 size_t u;
98 LY_ARRAY_COUNT_TYPE v;
99 const char *prefixed_name = NULL;
100
101 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret);
102 LY_CHECK_RET(ret);
103
104 ext->insubstmt = ext_p->insubstmt;
105 ext->insubstmt_index = ext_p->insubstmt_index;
106 ext->module = ctx->cur_mod;
107 ext->parent = parent;
108 ext->parent_type = parent_type;
109
110 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node *)ext->parent : NULL, "{extension}");
111
112 /* get module where the extension definition should be placed */
113 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {}
114 if (ext_p->yin) {
115 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
116 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
117 * YANG (import) prefix must be done here. */
118 if (!ly_strncmp(ctx->pmod->mod->ns, ext_p->name, u - 1)) {
119 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, &ext_p->name[u], 0, &prefixed_name), cleanup);
120 u = 0;
121 } else {
122 LY_ARRAY_FOR(ctx->pmod->imports, v) {
123 if (!ly_strncmp(ctx->pmod->imports[v].module->ns, ext_p->name, u - 1)) {
124 char *s;
125 LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->pmod->imports[v].prefix, &ext_p->name[u]) == -1,
126 ret = LY_EMEM, cleanup);
127 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->ctx, s, &prefixed_name), cleanup);
128 u = strlen(ctx->pmod->imports[v].prefix) + 1; /* add semicolon */
129 break;
130 }
131 }
132 }
133 if (!prefixed_name) {
134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
135 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
136 ret = LY_EVALID;
137 goto cleanup;
138 }
139 } else {
140 prefixed_name = ext_p->name;
141 }
142 lysc_update_path(ctx, NULL, prefixed_name);
143
144 if (!ext_mod) {
145 ext_mod = u ? lysp_module_find_prefix(ctx->pmod, prefixed_name, u - 1) : ctx->pmod->mod;
146 if (!ext_mod) {
147 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
148 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
149 ret = LY_EVALID;
150 goto cleanup;
151 } else if (!ext_mod->parsed->extensions) {
152 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
153 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
154 prefixed_name, ext_mod->name);
155 ret = LY_EVALID;
156 goto cleanup;
157 }
158 }
159 name = &prefixed_name[u];
160
161 /* find the parsed extension definition there */
162 LY_ARRAY_FOR(ext_mod->parsed->extensions, v) {
163 if (!strcmp(name, ext_mod->parsed->extensions[v].name)) {
164 /* compile extension definition and assign it */
165 LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def), cleanup);
166 break;
167 }
168 }
169 if (!ext->def) {
170 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
171 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
172 ret = LY_EVALID;
173 goto cleanup;
174 }
175
176 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
177 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
178 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
179 if (ext_p->yin && ext->def->argument && !ext->argument) {
180 /* Schema was parsed from YIN and an argument is expected, ... */
181 struct lysp_stmt *stmt = NULL;
182
183 if (ext->def->flags & LYS_YINELEM_TRUE) {
184 /* ... argument was the first XML child element */
185 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
186 /* TODO check namespace of the statement */
187 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
188 stmt = ext_p->child;
189 }
190 }
191 } else {
192 /* ... argument was one of the XML attributes which are represented as child stmt
193 * with LYS_YIN_ATTR flag */
194 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
195 if (!strcmp(stmt->stmt, ext->def->argument)) {
196 /* this is the extension's argument */
197 break;
198 }
199 }
200 }
201 if (!stmt) {
202 /* missing extension's argument */
203 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
204 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
205 ret = LY_EVALID;
206 goto cleanup;
207
208 }
209 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup);
210 stmt->flags |= LYS_YIN_ARGUMENT;
211 }
212 if (prefixed_name != ext_p->name) {
213 lydict_remove(ctx->ctx, ext_p->name);
214 ext_p->name = prefixed_name;
215 if (!ext_p->argument && ext->argument) {
216 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, ext->argument, 0, &ext_p->argument), cleanup);
217 }
218 }
219
220 if (ext->def->plugin && ext->def->plugin->compile) {
221 if (ext->argument) {
222 lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument);
223 }
224 LY_CHECK_GOTO(ret = ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
225 if (ext->argument) {
226 lysc_update_path(ctx, NULL, NULL);
227 }
228 }
229 ext_p->compiled = ext;
230
231cleanup:
232 if (prefixed_name && (prefixed_name != ext_p->name)) {
233 lydict_remove(ctx->ctx, prefixed_name);
234 }
235
236 lysc_update_path(ctx, NULL, NULL);
237 lysc_update_path(ctx, NULL, NULL);
238
239 return ret;
240}
241
242struct lysc_ext *
243lysc_ext_dup(struct lysc_ext *orig)
244{
245 ++orig->refcount;
246 return orig;
247}
248
249static void
250lysc_unres_dflt_free(const struct ly_ctx *ctx, struct lysc_unres_dflt *r)
251{
252 assert(!r->dflt || !r->dflts);
253 if (r->dflt) {
254 lysp_qname_free((struct ly_ctx *)ctx, r->dflt);
255 free(r->dflt);
256 } else {
257 FREE_ARRAY((struct ly_ctx *)ctx, r->dflts, lysp_qname_free);
258 }
259 free(r);
260}
261
262void
263lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
264{
265 int len;
266 uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
267
268 if (!name) {
269 /* removing last path segment */
270 if (ctx->path[ctx->path_len - 1] == '}') {
271 for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
272 if (ctx->path[ctx->path_len] == '=') {
273 ctx->path[ctx->path_len++] = '}';
274 } else {
275 /* not a top-level special tag, remove also preceiding '/' */
276 goto remove_nodelevel;
277 }
278 } else {
279remove_nodelevel:
280 for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
281 if (ctx->path_len == 0) {
282 /* top-level (last segment) */
283 ctx->path_len = 1;
284 }
285 }
286 /* set new terminating NULL-byte */
287 ctx->path[ctx->path_len] = '\0';
288 } else {
289 if (ctx->path_len > 1) {
290 if (!parent && (ctx->path[ctx->path_len - 1] == '}') && (ctx->path[ctx->path_len - 2] != '\'')) {
291 /* extension of the special tag */
292 nextlevel = 2;
293 --ctx->path_len;
294 } else {
295 /* there is already some path, so add next level */
296 nextlevel = 1;
297 }
298 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
299
300 if (nextlevel != 2) {
301 if ((parent && (parent->module == ctx->cur_mod)) || (!parent && (ctx->path_len > 1) && (name[0] == '{'))) {
302 /* module not changed, print the name unprefixed */
303 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
304 } else {
305 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->cur_mod->name, name);
306 }
307 } else {
308 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
309 }
310 if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
311 /* output truncated */
312 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
313 } else {
314 ctx->path_len += len;
315 }
316 }
317}
318
319/**
320 * @brief Stack for processing if-feature expressions.
321 */
322struct iff_stack {
323 size_t size; /**< number of items in the stack */
324 size_t index; /**< first empty item */
325 uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
326};
327
328/**
329 * @brief Add @ref ifftokens into the stack.
330 * @param[in] stack The if-feature stack to use.
331 * @param[in] value One of the @ref ifftokens to store in the stack.
332 * @return LY_EMEM in case of memory allocation error
333 * @return LY_ESUCCESS if the value successfully stored.
334 */
335static LY_ERR
336iff_stack_push(struct iff_stack *stack, uint8_t value)
337{
338 if (stack->index == stack->size) {
339 stack->size += 4;
340 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
341 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
342 }
343 stack->stack[stack->index++] = value;
344 return LY_SUCCESS;
345}
346
347/**
348 * @brief Get (and remove) the last item form the stack.
349 * @param[in] stack The if-feature stack to use.
350 * @return The value from the top of the stack.
351 */
352static uint8_t
353iff_stack_pop(struct iff_stack *stack)
354{
355 assert(stack && stack->index);
356
357 stack->index--;
358 return stack->stack[stack->index];
359}
360
361/**
362 * @brief Clean up the stack.
363 * @param[in] stack The if-feature stack to use.
364 */
365static void
366iff_stack_clean(struct iff_stack *stack)
367{
368 stack->size = 0;
369 free(stack->stack);
370}
371
372/**
373 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
374 * (libyang format of the if-feature expression).
375 * @param[in,out] list The 2bits array to modify.
376 * @param[in] op The operand (@ref ifftokens) to store.
377 * @param[in] pos Position (0-based) where to store the given @p op.
378 */
379static void
380iff_setop(uint8_t *list, uint8_t op, size_t pos)
381{
382 uint8_t *item;
383 uint8_t mask = 3;
384
385 assert(op <= 3); /* max 2 bits */
386
387 item = &list[pos / 4];
388 mask = mask << 2 * (pos % 4);
389 *item = (*item) & ~mask;
390 *item = (*item) | (op << 2 * (pos % 4));
391}
392
393#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
394#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
395
396/**
397 * @brief Find a feature of the given name and referenced in the given module.
398 *
399 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
400 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
401 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
402 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
403 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
404 * assigned till that time will be still valid.
405 *
406 * @param[in] pmod Module where the feature was referenced (used to resolve prefix of the feature).
407 * @param[in] name Name of the feature including possible prefix.
408 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
409 * @return Pointer to the feature structure if found, NULL otherwise.
410 */
411static struct lysc_feature *
412lys_feature_find(const struct lysp_module *pmod, const char *name, size_t len)
413{
414 LY_ARRAY_COUNT_TYPE u;
415 struct lysc_feature *f;
416 const struct lys_module *mod;
417 const char *ptr;
418
419 assert(pmod);
420
421 if ((ptr = ly_strnchr(name, ':', len))) {
422 /* we have a prefixed feature */
423 mod = lysp_module_find_prefix(pmod, name, ptr - name);
424 LY_CHECK_RET(!mod, NULL);
425
426 len = len - (ptr - name) - 1;
427 name = ptr + 1;
428 } else {
429 /* local feature */
430 mod = pmod->mod;
431 }
432
433 /* we have the correct module, get the feature */
434 LY_ARRAY_FOR(mod->features, u) {
435 f = &mod->features[u];
436 if (!ly_strncmp(f->name, name, len)) {
437 return f;
438 }
439 }
440
441 return NULL;
442}
443
444LY_ERR
445lys_compile_iffeature(struct lysc_ctx *ctx, struct lysp_qname *qname, struct lysc_iffeature *iff)
446{
447 LY_ERR rc = LY_SUCCESS;
448 const char *c = qname->str;
449 int64_t i, j;
450 int8_t op_len, last_not = 0, checkversion = 0;
451 LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
452 uint8_t op;
453 struct iff_stack stack = {0, 0, NULL};
454 struct lysc_feature *f;
455
456 assert(c);
457
458 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
459 for (i = j = 0; c[i]; i++) {
460 if (c[i] == '(') {
461 j++;
462 checkversion = 1;
463 continue;
464 } else if (c[i] == ')') {
465 j--;
466 continue;
467 } else if (isspace(c[i])) {
468 checkversion = 1;
469 continue;
470 }
471
472 if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) {
473 uint64_t spaces;
474 for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++) {}
475 if (c[i + op_len + spaces] == '\0') {
476 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
477 "Invalid value \"%s\" of if-feature - unexpected end of expression.", qname->str);
478 return LY_EVALID;
479 } else if (!isspace(c[i + op_len])) {
480 /* feature name starting with the not/and/or */
481 last_not = 0;
482 f_size++;
483 } else if (c[i] == 'n') { /* not operation */
484 if (last_not) {
485 /* double not */
486 expr_size = expr_size - 2;
487 last_not = 0;
488 } else {
489 last_not = 1;
490 }
491 } else { /* and, or */
492 if (f_exp != f_size) {
493 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
494 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.",
495 qname->str, op_len, &c[i]);
496 return LY_EVALID;
497 }
498 f_exp++;
499
500 /* not a not operation */
501 last_not = 0;
502 }
503 i += op_len;
504 } else {
505 f_size++;
506 last_not = 0;
507 }
508 expr_size++;
509
510 while (!isspace(c[i])) {
511 if (!c[i] || (c[i] == ')') || (c[i] == '(')) {
512 i--;
513 break;
514 }
515 i++;
516 }
517 }
518 if (j) {
519 /* not matching count of ( and ) */
520 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
521 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", qname->str);
522 return LY_EVALID;
523 }
524 if (f_exp != f_size) {
525 /* features do not match the needed arguments for the logical operations */
526 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
527 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
528 "the required number of operands for the operations.", qname->str);
529 return LY_EVALID;
530 }
531
532 if (checkversion || (expr_size > 1)) {
533 /* check that we have 1.1 module */
534 if (qname->mod->version != LYS_VERSION_1_1) {
535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
536 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", qname->str);
537 return LY_EVALID;
538 }
539 }
540
541 /* allocate the memory */
542 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
543 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
544 stack.stack = malloc(expr_size * sizeof *stack.stack);
545 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx); rc = LY_EMEM, error);
546
547 stack.size = expr_size;
548 f_size--; expr_size--; /* used as indexes from now */
549
550 for (i--; i >= 0; i--) {
551 if (c[i] == ')') {
552 /* push it on stack */
553 iff_stack_push(&stack, LYS_IFF_RP);
554 continue;
555 } else if (c[i] == '(') {
556 /* pop from the stack into result all operators until ) */
557 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
558 iff_setop(iff->expr, op, expr_size--);
559 }
560 continue;
561 } else if (isspace(c[i])) {
562 continue;
563 }
564
565 /* end of operator or operand -> find beginning and get what is it */
566 j = i + 1;
567 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
568 i--;
569 }
570 i++; /* go back by one step */
571
572 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
573 if (stack.index && (stack.stack[stack.index - 1] == LYS_IFF_NOT)) {
574 /* double not */
575 iff_stack_pop(&stack);
576 } else {
577 /* not has the highest priority, so do not pop from the stack
578 * as in case of AND and OR */
579 iff_stack_push(&stack, LYS_IFF_NOT);
580 }
581 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
582 /* as for OR - pop from the stack all operators with the same or higher
583 * priority and store them to the result, then push the AND to the stack */
584 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
585 op = iff_stack_pop(&stack);
586 iff_setop(iff->expr, op, expr_size--);
587 }
588 iff_stack_push(&stack, LYS_IFF_AND);
589 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
590 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
591 op = iff_stack_pop(&stack);
592 iff_setop(iff->expr, op, expr_size--);
593 }
594 iff_stack_push(&stack, LYS_IFF_OR);
595 } else {
596 /* feature name, length is j - i */
597
598 /* add it to the expression */
599 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
600
601 /* now get the link to the feature definition */
602 f = lys_feature_find(qname->mod, &c[i], j - i);
603 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
604 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", qname->str, j - i, &c[i]);
605 rc = LY_EVALID, error)
606 iff->features[f_size] = f;
607 LY_ARRAY_INCREMENT(iff->features);
608 f_size--;
609 }
610 }
611 while (stack.index) {
612 op = iff_stack_pop(&stack);
613 iff_setop(iff->expr, op, expr_size--);
614 }
615
616 if (++expr_size || ++f_size) {
617 /* not all expected operators and operands found */
618 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
619 "Invalid value \"%s\" of if-feature - processing error.", qname->str);
620 rc = LY_EINT;
621 } else {
622 rc = LY_SUCCESS;
623 }
624
625error:
626 /* cleanup */
627 iff_stack_clean(&stack);
628
629 return rc;
630}
631
632/**
633 * @brief Compile information in the import statement - make sure there is the target module
634 * @param[in] ctx Compile context.
635 * @param[in] imp_p The parsed import statement structure to fill the module to.
636 * @return LY_ERR value.
637 */
638static LY_ERR
639lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p)
640{
641 const struct lys_module *mod = NULL;
642 LY_ERR ret = LY_SUCCESS;
643
644 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
645 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
646 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
647 if (!imp_p->module->parsed) {
648 /* try to use filepath if present */
649 if (imp_p->module->filepath) {
650 struct ly_in *in;
651 if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) {
652 LOGINT(ctx->ctx);
653 } else {
654 LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)],
655 ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod));
656 if (mod != imp_p->module) {
657 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
658 imp_p->module->filepath, imp_p->module->name);
659 mod = NULL;
660 }
661 }
662 ly_in_free(in, 1);
663 }
664 if (!mod) {
665 if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, (struct lys_module **)&mod)) {
666 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
667 imp_p->module->name, ctx->cur_mod->name);
668 return LY_ENOTFOUND;
669 }
670 }
671 }
672
673 return ret;
674}
675
676LY_ERR
677lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
678 struct lysp_ident *identities_p, struct lysc_ident **identities)
679{
680 LY_ARRAY_COUNT_TYPE offset = 0, u, v;
681 struct lysc_ctx context = {0};
682 LY_ERR ret = LY_SUCCESS;
683
684 assert(ctx_sc || ctx);
685
686 if (!ctx_sc) {
687 context.ctx = ctx;
688 context.cur_mod = parsed_mod->mod;
689 context.pmod = parsed_mod;
690 context.path_len = 1;
691 context.path[0] = '/';
692 ctx_sc = &context;
693 }
694
695 if (!identities_p) {
696 return LY_SUCCESS;
697 }
698 if (*identities) {
699 offset = LY_ARRAY_COUNT(*identities);
700 }
701
702 lysc_update_path(ctx_sc, NULL, "{identity}");
703 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
704 LY_ARRAY_FOR(identities_p, u) {
705 lysc_update_path(ctx_sc, NULL, identities_p[u].name);
706
707 LY_ARRAY_INCREMENT(*identities);
708 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
709 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done);
710 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done);
711 DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref, ret, done);
712 (*identities)[offset + u].module = ctx_sc->cur_mod;
713 COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
714 lys_compile_iffeature, ret, done);
715 /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
716 COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
717 LYEXT_PAR_IDENT, ret, done);
718 (*identities)[offset + u].flags = identities_p[u].flags;
719
720 lysc_update_path(ctx_sc, NULL, NULL);
721 }
722 lysc_update_path(ctx_sc, NULL, NULL);
723done:
724 return ret;
725}
726
727/**
728 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
729 *
730 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
731 *
732 * @param[in] ctx Compile context for logging.
733 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
734 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
735 * @return LY_SUCCESS if everything is ok.
736 * @return LY_EVALID if the identity is derived from itself.
737 */
738static LY_ERR
739lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
740{
741 LY_ERR ret = LY_SUCCESS;
742 LY_ARRAY_COUNT_TYPE u, v;
743 struct ly_set recursion = {0};
744 struct lysc_ident *drv;
745
746 if (!derived) {
747 return LY_SUCCESS;
748 }
749
750 for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
751 if (ident == derived[u]) {
752 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
753 "Identity \"%s\" is indirectly derived from itself.", ident->name);
754 ret = LY_EVALID;
755 goto cleanup;
756 }
757 ret = ly_set_add(&recursion, derived[u], 0, NULL);
758 LY_CHECK_GOTO(ret, cleanup);
759 }
760
761 for (v = 0; v < recursion.count; ++v) {
762 drv = recursion.objs[v];
763 if (!drv->derived) {
764 continue;
765 }
766 for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
767 if (ident == drv->derived[u]) {
768 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
769 "Identity \"%s\" is indirectly derived from itself.", ident->name);
770 ret = LY_EVALID;
771 goto cleanup;
772 }
773 ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
774 LY_CHECK_GOTO(ret, cleanup);
775 }
776 }
777
778cleanup:
779 ly_set_erase(&recursion, NULL);
780 return ret;
781}
782
783LY_ERR
784lys_compile_identity_bases(struct lysc_ctx *ctx, const struct lysp_module *base_pmod, const char **bases_p,
785 struct lysc_ident *ident, struct lysc_ident ***bases)
786{
787 LY_ARRAY_COUNT_TYPE u, v;
788 const char *s, *name;
789 const struct lys_module *mod;
790 struct lysc_ident **idref;
791
792 assert(ident || bases);
793
794 if ((LY_ARRAY_COUNT(bases_p) > 1) && (ctx->pmod->version < LYS_VERSION_1_1)) {
795 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
796 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
797 return LY_EVALID;
798 }
799
800 LY_ARRAY_FOR(bases_p, u) {
801 s = strchr(bases_p[u], ':');
802 if (s) {
803 /* prefixed identity */
804 name = &s[1];
805 mod = lysp_module_find_prefix(base_pmod, bases_p[u], s - bases_p[u]);
806 } else {
807 name = bases_p[u];
808 mod = base_pmod->mod;
809 }
810 if (!mod) {
811 if (ident) {
812 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
813 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
814 } else {
815 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
816 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
817 }
818 return LY_EVALID;
819 }
820
821 idref = NULL;
822 LY_ARRAY_FOR(mod->identities, v) {
823 if (!strcmp(name, mod->identities[v].name)) {
824 if (ident) {
825 if (ident == &mod->identities[v]) {
826 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
827 "Identity \"%s\" is derived from itself.", ident->name);
828 return LY_EVALID;
829 }
830 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived));
831 /* we have match! store the backlink */
832 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
833 *idref = ident;
834 } else {
835 /* we have match! store the found identity */
836 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
837 *idref = &mod->identities[v];
838 }
839 break;
840 }
841 }
842 if (!idref || !(*idref)) {
843 if (ident) {
844 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
845 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
846 } else {
847 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
848 "Unable to find base (%s) of identityref.", bases_p[u]);
849 }
850 return LY_EVALID;
851 }
852 }
853 return LY_SUCCESS;
854}
855
856/**
857 * @brief For the given array of identities, set the backlinks from all their base identities.
858 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
859 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
860 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
861 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
862 */
863static LY_ERR
864lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
865{
866 LY_ARRAY_COUNT_TYPE u;
867
868 lysc_update_path(ctx, NULL, "{identity}");
869 for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
870 if (!idents_p[u].bases) {
871 continue;
872 }
873 lysc_update_path(ctx, NULL, idents[u].name);
874 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents[u].module->parsed, idents_p[u].bases, &idents[u], NULL));
875 lysc_update_path(ctx, NULL, NULL);
876 }
877 lysc_update_path(ctx, NULL, NULL);
878 return LY_SUCCESS;
879}
880
881LY_ERR
882lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
883 struct lysp_feature *features_p, struct lysc_feature **features)
884{
885 LY_ERR ret = LY_SUCCESS;
886 LY_ARRAY_COUNT_TYPE offset = 0, u;
887 struct lysc_ctx context = {0};
888
889 assert(ctx_sc || ctx);
890
891 if (!ctx_sc) {
892 context.ctx = ctx;
893 context.cur_mod = parsed_mod->mod;
894 context.pmod = parsed_mod;
895 context.path_len = 1;
896 context.path[0] = '/';
897 ctx_sc = &context;
898 }
899
900 if (!features_p) {
901 return LY_SUCCESS;
902 }
903 if (*features) {
904 offset = LY_ARRAY_COUNT(*features);
905 }
906
907 lysc_update_path(ctx_sc, NULL, "{feature}");
908 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
909 LY_ARRAY_FOR(features_p, u) {
910 lysc_update_path(ctx_sc, NULL, features_p[u].name);
911
912 LY_ARRAY_INCREMENT(*features);
913 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
914 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done);
915 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done);
916 DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref, ret, done);
917 (*features)[offset + u].flags = features_p[u].flags;
918 (*features)[offset + u].module = ctx_sc->cur_mod;
919
920 lysc_update_path(ctx_sc, NULL, NULL);
921 }
922 lysc_update_path(ctx_sc, NULL, NULL);
923
924done:
925 return ret;
926}
927
928/**
929 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
930 *
931 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
932 *
933 * @param[in] ctx Compile context for logging.
934 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
935 * being currently processed).
936 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
937 * @return LY_SUCCESS if everything is ok.
938 * @return LY_EVALID if the feature references indirectly itself.
939 */
940static LY_ERR
941lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
942{
943 LY_ERR ret = LY_SUCCESS;
944 LY_ARRAY_COUNT_TYPE u, v;
945 struct ly_set recursion = {0};
946 struct lysc_feature *drv;
947
948 if (!depfeatures) {
949 return LY_SUCCESS;
950 }
951
952 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
953 if (feature == depfeatures[u]) {
954 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
955 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
956 ret = LY_EVALID;
957 goto cleanup;
958 }
959 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
960 LY_CHECK_GOTO(ret, cleanup);
961 }
962
963 for (v = 0; v < recursion.count; ++v) {
964 drv = recursion.objs[v];
965 if (!drv->depfeatures) {
966 continue;
967 }
968 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
969 if (feature == drv->depfeatures[u]) {
970 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
971 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
972 ret = LY_EVALID;
973 goto cleanup;
974 }
975 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
976 LY_CHECK_GOTO(ret, cleanup);
977 }
978 }
979
980cleanup:
981 ly_set_erase(&recursion, NULL);
982 return ret;
983}
984
985/**
986 * @brief Create pre-compiled features array.
987 *
988 * See lys_feature_precompile() for more details.
989 *
990 * @param[in] ctx Compile context.
991 * @param[in] feature_p Parsed feature definition to compile.
992 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
993 * @return LY_ERR value.
994 */
995static LY_ERR
996lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
997{
998 LY_ARRAY_COUNT_TYPE u, v, x;
999 struct lysc_feature *feature, **df;
1000 LY_ERR ret = LY_SUCCESS;
1001
1002 /* find the preprecompiled feature */
1003 LY_ARRAY_FOR(features, x) {
1004 if (strcmp(features[x].name, feature_p->name)) {
1005 continue;
1006 }
1007 feature = &features[x];
1008 lysc_update_path(ctx, NULL, "{feature}");
1009 lysc_update_path(ctx, NULL, feature_p->name);
1010
1011 /* finish compilation started in lys_feature_precompile() */
1012 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
1013 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
1014 if (feature->iffeatures) {
1015 for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
1016 if (feature->iffeatures[u].features) {
1017 for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
1018 /* check for circular dependency - direct reference first,... */
1019 if (feature == feature->iffeatures[u].features[v]) {
1020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1021 "Feature \"%s\" is referenced from itself.", feature->name);
1022 return LY_EVALID;
1023 }
1024 /* ... and indirect circular reference */
1025 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1026
1027 /* add itself into the dependants list */
1028 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1029 *df = feature;
1030 }
1031 }
1032 }
1033 }
1034 lysc_update_path(ctx, NULL, NULL);
1035 lysc_update_path(ctx, NULL, NULL);
1036done:
1037 return ret;
1038 }
1039
1040 LOGINT(ctx->ctx);
1041 return LY_EINT;
1042}
1043
1044void
1045lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1046{
1047 LY_ARRAY_COUNT_TYPE u, v;
1048
1049 /* in the dis_features list, remove all the parts (from finished compiling process)
1050 * which may points into the data being freed here */
1051 LY_ARRAY_FOR(mod->features, u) {
1052 LY_ARRAY_FOR(mod->features[u].iffeatures, v) {
1053 lysc_iffeature_free(ctx->ctx, &mod->features[u].iffeatures[v]);
1054 }
1055 LY_ARRAY_FREE(mod->features[u].iffeatures);
1056 mod->features[u].iffeatures = NULL;
1057
1058 LY_ARRAY_FOR(mod->features[u].exts, v) {
1059 lysc_ext_instance_free(ctx->ctx, &(mod->features[u].exts)[v]);
1060 }
1061 LY_ARRAY_FREE(mod->features[u].exts);
1062 mod->features[u].exts = NULL;
1063 }
1064}
1065
1066/**
1067 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
1068 *
1069 * The set of features used for target must be a subset of features used for the leafref.
1070 * This is not a perfect, we should compare the truth tables but it could require too much resources
1071 * and RFC 7950 does not require it explicitely, so we simplify that.
1072 *
1073 * @param[in] refnode The leafref node.
1074 * @param[in] target Tha target node of the leafref.
1075 * @return LY_SUCCESS or LY_EVALID;
1076 */
1077static LY_ERR
1078lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
1079{
1080 LY_ERR ret = LY_EVALID;
1081 const struct lysc_node *iter;
1082 LY_ARRAY_COUNT_TYPE u, v;
1083 struct ly_set features = {0};
1084
1085 for (iter = refnode; iter; iter = iter->parent) {
1086 if (iter->iffeatures) {
1087 LY_ARRAY_FOR(iter->iffeatures, u) {
1088 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
1089 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0, NULL), cleanup);
1090 }
1091 }
1092 }
1093 }
1094
1095 /* we should have, in features set, a superset of features applicable to the target node.
1096 * If the feature is not present, we don;t have a subset of features applicable
1097 * to the leafref itself. */
1098 for (iter = target; iter; iter = iter->parent) {
1099 if (iter->iffeatures) {
1100 LY_ARRAY_FOR(iter->iffeatures, u) {
1101 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
1102 if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) {
1103 /* feature not present */
1104 goto cleanup;
1105 }
1106 }
1107 }
1108 }
1109 }
1110 ret = LY_SUCCESS;
1111
1112cleanup:
1113 ly_set_erase(&features, NULL);
1114 return ret;
1115}
1116
1117static void *
1118lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
1119{
1120 for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
1121 if (substmts[u].stmt == stmt) {
1122 return substmts[u].storage;
1123 }
1124 }
1125 return NULL;
1126}
1127
1128LY_ERR
1129lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
1130{
1131 LY_ERR ret = LY_EVALID, r;
1132 LY_ARRAY_COUNT_TYPE u;
1133 struct lysp_stmt *stmt;
1134 struct lysp_qname qname;
1135 void *parsed = NULL, **compiled = NULL;
1136
1137 /* check for invalid substatements */
1138 for (stmt = ext->child; stmt; stmt = stmt->next) {
1139 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
1140 continue;
1141 }
1142 for (u = 0; substmts[u].stmt; ++u) {
1143 if (substmts[u].stmt == stmt->kw) {
1144 break;
1145 }
1146 }
1147 if (!substmts[u].stmt) {
1148 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
1149 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
1150 goto cleanup;
1151 }
1152 }
1153
1154 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
1155
1156 /* keep order of the processing the same as the order in the defined substmts,
1157 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
1158 for (u = 0; substmts[u].stmt; ++u) {
1159 ly_bool stmt_present = 0;
1160
1161 for (stmt = ext->child; stmt; stmt = stmt->next) {
1162 if (substmts[u].stmt != stmt->kw) {
1163 continue;
1164 }
1165
1166 stmt_present = 1;
1167 if (substmts[u].storage) {
1168 switch (stmt->kw) {
1169 case LY_STMT_STATUS:
1170 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
1171 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
1172 break;
1173 case LY_STMT_UNITS: {
1174 const char **units;
1175
1176 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1177 /* single item */
1178 if (*((const char **)substmts[u].storage)) {
1179 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
1180 goto cleanup;
1181 }
1182 units = (const char **)substmts[u].storage;
1183 } else {
1184 /* sized array */
1185 const char ***units_array = (const char ***)substmts[u].storage;
1186 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
1187 }
1188 r = lydict_insert(ctx->ctx, stmt->arg, 0, units);
1189 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1190 break;
1191 }
1192 case LY_STMT_TYPE: {
1193 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
1194 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
1195
1196 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1197 /* single item */
1198 if (*(struct lysc_type **)substmts[u].storage) {
1199 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
1200 goto cleanup;
1201 }
1202 compiled = substmts[u].storage;
1203 } else {
1204 /* sized array */
1205 struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL;
1206 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
1207 compiled = (void *)type;
1208 }
1209
1210 r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL);
1211 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1212 r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL,
1213 flags ? *flags : 0, ctx->pmod, ext->name, parsed, (struct lysc_type **)compiled,
1214 units && !*units ? units : NULL, NULL);
1215 lysp_type_free(ctx->ctx, parsed);
1216 free(parsed);
1217 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1218 break;
1219 }
1220 case LY_STMT_IF_FEATURE: {
1221 struct lysc_iffeature *iff = NULL;
1222
1223 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1224 /* single item */
1225 if (((struct lysc_iffeature *)substmts[u].storage)->features) {
1226 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
1227 goto cleanup;
1228 }
1229 iff = (struct lysc_iffeature *)substmts[u].storage;
1230 } else {
1231 /* sized array */
1232 struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage;
1233 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
1234 }
1235 qname.str = stmt->arg;
1236 qname.mod = ctx->pmod;
1237 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &qname, iff), ret = r, cleanup);
1238 break;
1239 }
1240 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
1241 * also note that in many statements their extensions are not taken into account */
1242 default:
1243 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.",
1244 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
1245 goto cleanup;
1246 }
1247 }
1248 }
1249
1250 if (((substmts[u].cardinality == LY_STMT_CARD_MAND) || (substmts[u].cardinality == LY_STMT_CARD_SOME)) && !stmt_present) {
1251 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
1252 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
1253 goto cleanup;
1254 }
1255 }
1256
1257 ret = LY_SUCCESS;
1258
1259cleanup:
1260 return ret;
1261}
1262
1263/**
1264 * @brief Check when for cyclic dependencies.
1265 *
1266 * @param[in] set Set with all the referenced nodes.
1267 * @param[in] node Node whose "when" referenced nodes are in @p set.
1268 * @return LY_ERR value
1269 */
1270static LY_ERR
1271lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
1272{
1273 struct lyxp_set tmp_set;
1274 struct lyxp_set_scnode *xp_scnode;
1275 uint32_t i, j;
1276 LY_ARRAY_COUNT_TYPE u;
1277 struct lysc_when *when;
1278 LY_ERR ret = LY_SUCCESS;
1279
1280 memset(&tmp_set, 0, sizeof tmp_set);
1281
1282 /* prepare in_ctx of the set */
1283 for (i = 0; i < set->used; ++i) {
1284 xp_scnode = &set->val.scnodes[i];
1285
1286 if (xp_scnode->in_ctx != -1) {
1287 /* check node when, skip the context node (it was just checked) */
1288 xp_scnode->in_ctx = 1;
1289 }
1290 }
1291
1292 for (i = 0; i < set->used; ++i) {
1293 xp_scnode = &set->val.scnodes[i];
1294 if (xp_scnode->in_ctx != 1) {
1295 /* already checked */
1296 continue;
1297 }
1298
1299 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) ||
1300 !xp_scnode->scnode->when) {
1301 /* no when to check */
1302 xp_scnode->in_ctx = 0;
1303 continue;
1304 }
1305
1306 node = xp_scnode->scnode;
1307 do {
1308 LY_ARRAY_FOR(node->when, u) {
1309 when = node->when[u];
1310 ret = lyxp_atomize(when->cond, node->module, LY_PREF_SCHEMA_RESOLVED, when->prefixes, when->context,
1311 &tmp_set, LYXP_SCNODE_SCHEMA);
1312 if (ret != LY_SUCCESS) {
1313 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
1314 goto cleanup;
1315 }
1316
1317 for (j = 0; j < tmp_set.used; ++j) {
1318 /* skip roots'n'stuff */
1319 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
1320 /* try to find this node in our set */
1321 uint32_t idx;
1322 if (lyxp_set_scnode_contains(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1, &idx) && (set->val.scnodes[idx].in_ctx == -1)) {
1323 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LY_VCODE_CIRC_WHEN, node->name, set->val.scnodes[idx].scnode->name);
1324 ret = LY_EVALID;
1325 goto cleanup;
1326 }
1327
1328 /* needs to be checked, if in both sets, will be ignored */
1329 tmp_set.val.scnodes[j].in_ctx = 1;
1330 } else {
1331 /* no when, nothing to check */
1332 tmp_set.val.scnodes[j].in_ctx = 0;
1333 }
1334 }
1335
1336 /* merge this set into the global when set */
1337 lyxp_set_scnode_merge(set, &tmp_set);
1338 }
1339
1340 /* check when of non-data parents as well */
1341 node = node->parent;
1342 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1343
1344 /* this node when was checked (xp_scnode could have been reallocd) */
1345 set->val.scnodes[i].in_ctx = -1;
1346 }
1347
1348cleanup:
1349 lyxp_set_free_content(&tmp_set);
1350 return ret;
1351}
1352
1353LY_ERR
1354lysc_check_status(struct lysc_ctx *ctx, uint16_t flags1, void *mod1, const char *name1, uint16_t flags2, void *mod2,
1355 const char *name2)
1356{
1357 uint16_t flg1, flg2;
1358
1359 flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
1360 flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
1361
1362 if ((flg1 < flg2) && (mod1 == mod2)) {
1363 if (ctx) {
1364 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1365 "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".",
1366 flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1,
1367 flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2);
1368 }
1369 return LY_EVALID;
1370 }
1371
1372 return LY_SUCCESS;
1373}
1374
1375/**
Michal Vaskocfaff232020-10-20 09:35:14 +02001376 * @brief Check parsed expression for any prefixes of unimplemented modules.
1377 *
1378 * @param[in] ctx libyang context.
1379 * @param[in] expr Parsed expression.
1380 * @param[in] format Prefix format.
1381 * @param[in] prefix_data Format-specific data (see ::ly_resolve_prefix()).
1382 * @param[out] mod_p Optional module that is not implemented.
1383 * @return Whether all the found modules are implemented or at least one is not.
1384 */
1385static ly_bool
1386lys_compile_expr_target_is_implemented(const struct ly_ctx *ctx, const struct lyxp_expr *expr, LY_PREFIX_FORMAT format,
1387 void *prefix_data, const struct lys_module **mod_p)
1388{
1389 uint32_t i;
1390 const char *ptr, *start;
1391 const struct lys_module *mod;
1392
1393 for (i = 0; i < expr->used; ++i) {
1394 if ((expr->tokens[i] != LYXP_TOKEN_NAMETEST) && (expr->tokens[i] != LYXP_TOKEN_LITERAL)) {
1395 /* token cannot have a prefix */
1396 continue;
1397 }
1398
1399 start = expr->expr + expr->tok_pos[i];
1400 if (!(ptr = ly_strnchr(start, ':', expr->tok_len[i]))) {
1401 /* token without a prefix */
1402 continue;
1403 }
1404
1405 if (!(mod = ly_resolve_prefix(ctx, start, ptr - start, format, prefix_data))) {
1406 /* unknown prefix, do not care right now */
1407 continue;
1408 }
1409
1410 if (!mod->implemented) {
1411 /* unimplemented module found */
1412 if (mod_p) {
1413 *mod_p = mod;
1414 }
1415 return 0;
1416 }
1417 }
1418
1419 return 1;
1420}
1421
1422/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001423 * @brief Check when/must expressions of a node on a complete compiled schema tree.
1424 *
1425 * @param[in] ctx Compile context.
1426 * @param[in] node Node to check.
1427 * @return LY_ERR value
1428 */
1429static LY_ERR
1430lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
1431{
1432 struct lyxp_set tmp_set;
1433 uint32_t i, opts;
1434 LY_ARRAY_COUNT_TYPE u;
1435 ly_bool input_done = 0;
1436 struct lysc_when **when = NULL;
1437 struct lysc_must *musts = NULL;
1438 LY_ERR ret = LY_SUCCESS;
1439 const struct lysc_node *op;
Michal Vaskocfaff232020-10-20 09:35:14 +02001440 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001441
1442 memset(&tmp_set, 0, sizeof tmp_set);
1443 opts = LYXP_SCNODE_SCHEMA;
1444 if (node->flags & LYS_CONFIG_R) {
1445 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent) {}
1446 if (op) {
1447 /* we are actually in output */
1448 opts = LYXP_SCNODE_OUTPUT;
1449 }
1450 }
1451
1452 switch (node->nodetype) {
1453 case LYS_CONTAINER:
1454 when = ((struct lysc_node_container *)node)->when;
1455 musts = ((struct lysc_node_container *)node)->musts;
1456 break;
1457 case LYS_CHOICE:
1458 when = ((struct lysc_node_choice *)node)->when;
1459 break;
1460 case LYS_LEAF:
1461 when = ((struct lysc_node_leaf *)node)->when;
1462 musts = ((struct lysc_node_leaf *)node)->musts;
1463 break;
1464 case LYS_LEAFLIST:
1465 when = ((struct lysc_node_leaflist *)node)->when;
1466 musts = ((struct lysc_node_leaflist *)node)->musts;
1467 break;
1468 case LYS_LIST:
1469 when = ((struct lysc_node_list *)node)->when;
1470 musts = ((struct lysc_node_list *)node)->musts;
1471 break;
1472 case LYS_ANYXML:
1473 case LYS_ANYDATA:
1474 when = ((struct lysc_node_anydata *)node)->when;
1475 musts = ((struct lysc_node_anydata *)node)->musts;
1476 break;
1477 case LYS_CASE:
1478 when = ((struct lysc_node_case *)node)->when;
1479 break;
1480 case LYS_NOTIF:
1481 when = ((struct lysc_notif *)node)->when;
1482 musts = ((struct lysc_notif *)node)->musts;
1483 break;
1484 case LYS_RPC:
1485 case LYS_ACTION:
1486 /* first process when and input musts */
1487 when = ((struct lysc_action *)node)->when;
1488 musts = ((struct lysc_action *)node)->input.musts;
1489 break;
1490 default:
1491 /* nothing to check */
1492 break;
1493 }
1494
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001495 LY_ARRAY_FOR(when, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +02001496 /* first check whether all the referenced modules are implemented */
1497 if (!lys_compile_expr_target_is_implemented(ctx->ctx, when[u]->cond, LY_PREF_SCHEMA_RESOLVED,
1498 when[u]->prefixes, &mod)) {
1499 LOGWRN(ctx->ctx, "When condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
1500 when[u]->cond->expr, mod->name);
1501 continue;
1502 }
1503
1504 /* check "when" */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001505 ret = lyxp_atomize(when[u]->cond, node->module, LY_PREF_SCHEMA_RESOLVED, when[u]->prefixes, when[u]->context,
1506 &tmp_set, opts);
1507 if (ret != LY_SUCCESS) {
1508 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
1509 goto cleanup;
1510 }
1511
1512 ctx->path[0] = '\0';
1513 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1514 for (i = 0; i < tmp_set.used; ++i) {
1515 /* skip roots'n'stuff */
1516 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
1517 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
1518
1519 /* XPath expression cannot reference "lower" status than the node that has the definition */
1520 ret = lysc_check_status(ctx, when[u]->flags, node->module, node->name, schema->flags, schema->module,
1521 schema->name);
1522 LY_CHECK_GOTO(ret, cleanup);
1523
1524 /* check dummy node accessing */
1525 if (schema == node) {
1526 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
1527 ret = LY_EVALID;
1528 goto cleanup;
1529 }
1530 }
1531 }
1532
1533 /* check cyclic dependencies */
1534 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
1535 LY_CHECK_GOTO(ret, cleanup);
1536
1537 lyxp_set_free_content(&tmp_set);
1538 }
1539
1540check_musts:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001541 LY_ARRAY_FOR(musts, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +02001542 /* first check whether all the referenced modules are implemented */
1543 if (!lys_compile_expr_target_is_implemented(ctx->ctx, musts[u].cond, LY_PREF_SCHEMA_RESOLVED,
1544 musts[u].prefixes, &mod)) {
1545 LOGWRN(ctx->ctx, "Must condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
1546 musts[u].cond->expr, mod->name);
1547 continue;
1548 }
1549
1550 /* check "must" */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001551 ret = lyxp_atomize(musts[u].cond, node->module, LY_PREF_SCHEMA_RESOLVED, musts[u].prefixes, node, &tmp_set, opts);
1552 if (ret != LY_SUCCESS) {
1553 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
1554 goto cleanup;
1555 }
1556
1557 ctx->path[0] = '\0';
1558 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1559 for (i = 0; i < tmp_set.used; ++i) {
1560 /* skip roots'n'stuff */
1561 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
1562 /* XPath expression cannot reference "lower" status than the node that has the definition */
1563 ret = lysc_check_status(ctx, node->flags, node->module, node->name, tmp_set.val.scnodes[i].scnode->flags,
1564 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
1565 LY_CHECK_GOTO(ret, cleanup);
1566 }
1567 }
1568
1569 lyxp_set_free_content(&tmp_set);
1570 }
1571
1572 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
1573 /* now check output musts */
1574 input_done = 1;
1575 when = NULL;
1576 musts = ((struct lysc_action *)node)->output.musts;
1577 opts = LYXP_SCNODE_OUTPUT;
1578 goto check_musts;
1579 }
1580
1581cleanup:
1582 lyxp_set_free_content(&tmp_set);
1583 return ret;
1584}
1585
1586/**
1587 * @brief Check leafref for its target existence on a complete compiled schema tree.
1588 *
1589 * @param[in] ctx Compile context.
1590 * @param[in] node Context node for the leafref.
1591 * @param[in] lref Leafref to check/resolve.
1592 * @return LY_ERR value.
1593 */
1594static LY_ERR
1595lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
1596{
1597 const struct lysc_node *target = NULL, *siter;
1598 struct ly_path *p;
1599 struct lysc_type *type;
1600
1601 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
1602
1603 /* try to find the target */
1604 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, lysc_is_output(node) ?
1605 LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, &p));
1606
1607 /* get the target node */
1608 target = p[LY_ARRAY_COUNT(p) - 1].node;
1609 ly_path_free(node->module->ctx, p);
1610
1611 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1612 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
1613 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
1614 lref->path->expr, lys_nodetype2str(target->nodetype));
1615 return LY_EVALID;
1616 }
1617
1618 /* check status */
1619 ctx->path[0] = '\0';
1620 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1621 ctx->path_len = strlen(ctx->path);
1622 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
1623 return LY_EVALID;
1624 }
1625 ctx->path_len = 1;
1626 ctx->path[1] = '\0';
1627
1628 /* check config */
1629 if (lref->require_instance) {
1630 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {}
1631 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
1632 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
1633 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
1634 return LY_EVALID;
1635 }
1636 }
1637
1638 /* store the target's type and check for circular chain of leafrefs */
1639 lref->realtype = ((struct lysc_node_leaf *)target)->type;
1640 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
1641 if (type == (struct lysc_type *)lref) {
1642 /* circular chain detected */
1643 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
1644 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
1645 return LY_EVALID;
1646 }
1647 }
1648
1649 /* check if leafref and its target are under common if-features */
1650 if (lys_compile_leafref_features_validate(node, target)) {
1651 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
1652 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
1653 " features applicable to the leafref itself.", lref->path->expr);
1654 return LY_EVALID;
1655 }
1656
1657 return LY_SUCCESS;
1658}
1659
1660static LY_ERR
1661lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
1662{
1663 struct lysc_ext_instance *ext;
1664 struct lysp_ext_instance *ext_p = NULL;
1665 struct lysp_stmt *stmt;
1666 const struct lys_module *ext_mod;
1667 LY_ERR ret = LY_SUCCESS;
1668
1669 /* create the parsed extension instance manually */
1670 ext_p = calloc(1, sizeof *ext_p);
1671 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1672 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup);
1673 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "default", 0, &ext_p->argument), cleanup);
1674 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
1675 ext_p->insubstmt_index = 0;
1676
1677 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1678 LY_CHECK_ERR_GOTO(!stmt, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1679 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup);
1680 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "boolean", 0, &stmt->arg), cleanup);
1681 stmt->kw = LY_STMT_TYPE;
1682
1683 /* allocate new extension instance */
1684 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
1685
1686 /* manually get extension definition module */
1687 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
1688
1689 /* compile the extension instance */
1690 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
1691
1692cleanup:
1693 lysp_ext_instance_free(ctx->ctx, ext_p);
1694 free(ext_p);
1695 return ret;
1696}
1697
1698/**
1699 * @brief Compile default value(s) for leaf or leaf-list expecting a complete compiled schema tree.
1700 *
1701 * @param[in] ctx Compile context.
1702 * @param[in] node Leaf or leaf-list to compile the default value(s) for.
1703 * @param[in] type Type of the default value.
1704 * @param[in] dflt Default value.
1705 * @param[in] dflt_pmod Parsed module of the @p dflt to resolve possible prefixes.
1706 * @param[in,out] storage Storage for the compiled default value.
1707 * @return LY_ERR value.
1708 */
1709static LY_ERR
1710lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_type *type, const char *dflt,
1711 const struct lysp_module *dflt_pmod, struct lyd_value *storage)
1712{
1713 LY_ERR ret;
1714 struct ly_err_item *err = NULL;
1715
1716 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), 0, LY_PREF_SCHEMA, (void *)dflt_pmod,
1717 LYD_HINT_SCHEMA, node, storage, &err);
1718 if (ret == LY_EINCOMPLETE) {
1719 /* we have no data so we will not be resolving it */
1720 ret = LY_SUCCESS;
1721 }
1722
1723 if (ret) {
1724 ctx->path[0] = '\0';
1725 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1726 if (err) {
1727 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1728 "Invalid default - value does not fit the type (%s).", err->msg);
1729 ly_err_free(err);
1730 } else {
1731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1732 "Invalid default - value does not fit the type.");
1733 }
1734 return ret;
1735 }
1736
1737 ++((struct lysc_type *)storage->realtype)->refcount;
1738 return LY_SUCCESS;
1739}
1740
1741/**
1742 * @brief Compile default value of a leaf expecting a complete compiled schema tree.
1743 *
1744 * @param[in] ctx Compile context.
1745 * @param[in] leaf Leaf that the default value is for.
1746 * @param[in] dflt Default value to compile.
1747 * @return LY_ERR value.
1748 */
1749static LY_ERR
1750lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
1751{
1752 LY_ERR ret;
1753
1754 assert(!leaf->dflt);
1755
1756 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
1757 /* ignore default values for keys and mandatory leaves */
1758 return LY_SUCCESS;
1759 }
1760
1761 /* allocate the default value */
1762 leaf->dflt = calloc(1, sizeof *leaf->dflt);
1763 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
1764
1765 /* store the default value */
1766 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt->str, dflt->mod, leaf->dflt);
1767 if (ret) {
1768 free(leaf->dflt);
1769 leaf->dflt = NULL;
1770 }
1771
1772 return ret;
1773}
1774
1775/**
1776 * @brief Compile default values of a leaf-list expecting a complete compiled schema tree.
1777 *
1778 * @param[in] ctx Compile context.
1779 * @param[in] llist Leaf-list that the default value(s) are for.
1780 * @param[in] dflt Default value to compile, in case of a single value.
1781 * @param[in] dflts Sized array of default values, in case of more values.
1782 * @return LY_ERR value.
1783 */
1784static LY_ERR
1785lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflt,
1786 struct lysp_qname *dflts)
1787{
1788 LY_ERR ret;
1789 LY_ARRAY_COUNT_TYPE orig_count, u, v;
1790
1791 assert(dflt || dflts);
1792
1793 if (llist->dflts) {
1794 /* there were already some defaults and we are adding new by deviations */
1795 assert(dflts);
1796 orig_count = LY_ARRAY_COUNT(llist->dflts);
1797 } else {
1798 orig_count = 0;
1799 }
1800
1801 /* allocate new items */
1802 if (dflts) {
1803 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
1804 } else {
1805 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
1806 }
1807
1808 /* fill each new default value */
1809 if (dflts) {
1810 LY_ARRAY_FOR(dflts, u) {
1811 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
1812 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u].str, dflts[u].mod,
1813 llist->dflts[orig_count + u]);
1814 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
1815 LY_ARRAY_INCREMENT(llist->dflts);
1816 }
1817 } else {
1818 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
1819 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt->str, dflt->mod,
1820 llist->dflts[orig_count]);
1821 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
1822 LY_ARRAY_INCREMENT(llist->dflts);
1823 }
1824
1825 /* check default value uniqueness */
1826 if (llist->flags & LYS_CONFIG_W) {
1827 /* configuration data values must be unique - so check the default values */
1828 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
1829 for (v = 0; v < u; ++v) {
1830 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
1831 lysc_update_path(ctx, llist->parent, llist->name);
1832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1833 "Configuration leaf-list has multiple defaults of the same value \"%s\".",
1834 llist->dflts[u]->canonical);
1835 lysc_update_path(ctx, NULL, NULL);
1836 return LY_EVALID;
1837 }
1838 }
1839 }
1840 }
1841
1842 return LY_SUCCESS;
1843}
1844
1845/**
1846 * @brief Finish compilation of all the unres sets of a compile context.
1847 *
1848 * @param[in] ctx Compile context with unres sets.
1849 * @return LY_ERR value.
1850 */
1851static LY_ERR
1852lys_compile_unres(struct lysc_ctx *ctx)
1853{
1854 struct lysc_node *node;
1855 struct lysc_type *type, *typeiter;
1856 struct lysc_type_leafref *lref;
1857 struct lysc_augment *aug;
1858 struct lysc_deviation *dev;
1859 LY_ARRAY_COUNT_TYPE v;
1860 uint32_t i;
1861
1862 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
1863 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
1864 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
1865 for (i = 0; i < ctx->leafrefs.count; ++i) {
1866 node = ctx->leafrefs.objs[i];
1867 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
1868 type = ((struct lysc_node_leaf *)node)->type;
1869 if (type->basetype == LY_TYPE_LEAFREF) {
1870 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
1871 } else if (type->basetype == LY_TYPE_UNION) {
1872 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
1873 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
1874 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
1875 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
1876 }
1877 }
1878 }
1879 }
1880 for (i = 0; i < ctx->leafrefs.count; ++i) {
1881 /* store pointer to the real type */
1882 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
1883 if (type->basetype == LY_TYPE_LEAFREF) {
1884 for (typeiter = ((struct lysc_type_leafref *)type)->realtype;
1885 typeiter->basetype == LY_TYPE_LEAFREF;
1886 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
1887 ((struct lysc_type_leafref *)type)->realtype = typeiter;
1888 } else if (type->basetype == LY_TYPE_UNION) {
1889 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
1890 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
1891 for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype;
1892 typeiter->basetype == LY_TYPE_LEAFREF;
1893 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
1894 ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter;
1895 }
1896 }
1897 }
1898 }
1899
1900 /* check xpath */
1901 for (i = 0; i < ctx->xpath.count; ++i) {
1902 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
1903 }
1904
1905 /* finish incomplete default values compilation */
1906 for (i = 0; i < ctx->dflts.count; ++i) {
1907 struct lysc_unres_dflt *r = ctx->dflts.objs[i];
1908 if (r->leaf->nodetype == LYS_LEAF) {
1909 LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt));
1910 } else {
1911 LY_CHECK_RET(lys_compile_unres_llist_dflts(ctx, r->llist, r->dflt, r->dflts));
1912 }
1913 }
1914
1915 /* check that all augments were applied */
1916 for (i = 0; i < ctx->augs.count; ++i) {
1917 aug = ctx->augs.objs[i];
1918 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
1919 "Augment target node \"%s\" from module \"%s\" was not found.", aug->nodeid->expr,
1920 LYSP_MODULE_NAME(aug->nodeid_pmod));
1921 }
1922 if (ctx->augs.count) {
1923 return LY_ENOTFOUND;
1924 }
1925
1926 /* check that all deviations were applied */
1927 for (i = 0; i < ctx->devs.count; ++i) {
1928 dev = ctx->devs.objs[i];
1929 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
1930 "Deviation(s) target node \"%s\" from module \"%s\" was not found.", dev->nodeid->expr,
1931 LYSP_MODULE_NAME(dev->dev_pmods[0]));
1932 }
1933 if (ctx->devs.count) {
1934 return LY_ENOTFOUND;
1935 }
1936
1937 return LY_SUCCESS;
1938}
1939
1940/**
1941 * @brief Compile features in the current module and all its submodules.
1942 *
1943 * @param[in] ctx Compile context.
1944 * @return LY_ERR value.
1945 */
1946static LY_ERR
1947lys_compile_features(struct lysc_ctx *ctx)
1948{
1949 struct lysp_submodule *submod;
1950 LY_ARRAY_COUNT_TYPE u, v;
1951
1952 if (!ctx->cur_mod->features) {
1953 /* features are compiled directly into the module structure,
1954 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
1955 LY_CHECK_RET(lys_feature_precompile(ctx, NULL, NULL, ctx->cur_mod->parsed->features, &ctx->cur_mod->features));
1956 LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, v) {
1957 submod = ctx->cur_mod->parsed->includes[v].submodule;
1958 LY_CHECK_RET(lys_feature_precompile(ctx, NULL, NULL, submod->features, &ctx->cur_mod->features));
1959 }
1960 }
1961
1962 /* finish feature compilation, not only for the main module, but also for the submodules.
1963 * Due to possible forward references, it must be done when all the features (including submodules)
1964 * are present. */
1965 LY_ARRAY_FOR(ctx->cur_mod->parsed->features, u) {
1966 LY_CHECK_RET(lys_feature_precompile_finish(ctx, &ctx->cur_mod->parsed->features[u], ctx->cur_mod->features));
1967 }
1968
1969 lysc_update_path(ctx, NULL, "{submodule}");
1970 LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, v) {
1971 submod = ctx->cur_mod->parsed->includes[v].submodule;
1972
1973 lysc_update_path(ctx, NULL, submod->name);
1974 LY_ARRAY_FOR(submod->features, u) {
1975 LY_CHECK_RET(lys_feature_precompile_finish(ctx, &submod->features[u], ctx->cur_mod->features));
1976 }
1977 lysc_update_path(ctx, NULL, NULL);
1978 }
1979 lysc_update_path(ctx, NULL, NULL);
1980
1981 return LY_SUCCESS;
1982}
1983
1984/**
1985 * @brief Compile identites in the current module and all its submodules.
1986 *
1987 * @param[in] ctx Compile context.
1988 * @return LY_ERR value.
1989 */
1990static LY_ERR
1991lys_compile_identities(struct lysc_ctx *ctx)
1992{
1993 struct lysp_submodule *submod;
1994 LY_ARRAY_COUNT_TYPE u;
1995
1996 if (!ctx->cur_mod->identities) {
1997 LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, ctx->cur_mod->parsed->identities, &ctx->cur_mod->identities));
1998 LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, u) {
1999 submod = ctx->cur_mod->parsed->includes[u].submodule;
2000 LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, submod->identities, &ctx->cur_mod->identities));
2001 }
2002 }
2003
2004 if (ctx->cur_mod->parsed->identities) {
2005 LY_CHECK_RET(lys_compile_identities_derived(ctx, ctx->cur_mod->parsed->identities, ctx->cur_mod->identities));
2006 }
2007 lysc_update_path(ctx, NULL, "{submodule}");
2008 LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, u) {
2009
2010 submod = ctx->cur_mod->parsed->includes[u].submodule;
2011 if (submod->identities) {
2012 lysc_update_path(ctx, NULL, submod->name);
2013 LY_CHECK_RET(lys_compile_identities_derived(ctx, submod->identities, ctx->cur_mod->identities));
2014 lysc_update_path(ctx, NULL, NULL);
2015 }
2016 }
2017 lysc_update_path(ctx, NULL, NULL);
2018
2019 return LY_SUCCESS;
2020}
2021
2022LY_ERR
2023lys_compile(struct lys_module *mod, uint32_t options)
2024{
2025 struct lysc_ctx ctx = {0};
2026 struct lysc_module *mod_c;
2027 struct lysp_module *sp;
2028 struct lysp_submodule *submod;
2029 struct lysp_node *pnode;
2030 struct lysp_grp *grps;
2031 LY_ARRAY_COUNT_TYPE u, v;
2032 uint32_t i;
2033 LY_ERR ret = LY_SUCCESS;
2034
2035 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
2036
2037 if (!mod->implemented) {
2038 /* just imported modules are not compiled */
2039 return LY_SUCCESS;
2040 }
2041
2042 /* context will be changed */
2043 ++mod->ctx->module_set_id;
2044
2045 sp = mod->parsed;
2046
2047 ctx.ctx = mod->ctx;
2048 ctx.cur_mod = mod;
2049 ctx.pmod = sp;
2050 ctx.options = options;
2051 ctx.path_len = 1;
2052 ctx.path[0] = '/';
2053
2054 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
2055 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
2056 mod_c->mod = mod;
2057
2058 /* process imports */
2059 LY_ARRAY_FOR(sp->imports, u) {
2060 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
2061 }
2062
2063 /* features */
2064 LY_CHECK_GOTO(ret = lys_compile_features(&ctx), error);
2065
2066 /* identities, work similarly to features with the precompilation */
2067 LY_CHECK_GOTO(ret = lys_compile_identities(&ctx), error);
2068
2069 /* augments and deviations */
2070 LY_CHECK_GOTO(ret = lys_precompile_augments_deviations(&ctx), error);
2071
2072 /* compile augments and deviations of our module from other modules so they can be applied during compilation */
2073 LY_CHECK_GOTO(ret = lys_precompile_own_augments(&ctx), error);
2074 LY_CHECK_GOTO(ret = lys_precompile_own_deviations(&ctx), error);
2075
2076 /* data nodes */
2077 LY_LIST_FOR(sp->data, pnode) {
2078 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), error);
2079 }
2080
2081 /* top-level RPCs and notifications */
2082 COMPILE_OP_ARRAY_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
2083 COMPILE_OP_ARRAY_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
2084
2085 /* extension instances */
2086 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
2087
2088 /* the same for submodules */
2089 LY_ARRAY_FOR(sp->includes, u) {
2090 submod = sp->includes[u].submodule;
2091 ctx.pmod = (struct lysp_module *)submod;
2092
2093 LY_LIST_FOR(submod->data, pnode) {
2094 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
2095 LY_CHECK_GOTO(ret, error);
2096 }
2097
2098 COMPILE_OP_ARRAY_GOTO(&ctx, submod->rpcs, mod_c->rpcs, NULL, v, lys_compile_action, 0, ret, error);
2099 COMPILE_OP_ARRAY_GOTO(&ctx, submod->notifs, mod_c->notifs, NULL, v, lys_compile_notif, 0, ret, error);
2100
2101 COMPILE_EXTS_GOTO(&ctx, submod->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
2102 }
2103
2104 /* finish compilation for all unresolved items in the context */
2105 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
2106
2107 /* validate non-instantiated groupings from the parsed schema,
2108 * without it we would accept even the schemas with invalid grouping specification */
2109 ctx.pmod = sp;
2110 ctx.options |= LYS_COMPILE_GROUPING;
2111 LY_ARRAY_FOR(sp->groupings, u) {
2112 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
2113 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, &sp->groupings[u]), error);
2114 }
2115 }
2116 LY_LIST_FOR(sp->data, pnode) {
2117 grps = (struct lysp_grp *)lysp_node_groupings(pnode);
2118 LY_ARRAY_FOR(grps, u) {
2119 if (!(grps[u].flags & LYS_USED_GRP)) {
2120 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &grps[u]), error);
2121 }
2122 }
2123 }
2124 LY_ARRAY_FOR(sp->includes, u) {
2125 submod = sp->includes[u].submodule;
2126 ctx.pmod = (struct lysp_module *)submod;
2127
2128 LY_ARRAY_FOR(submod->groupings, u) {
2129 if (!(submod->groupings[u].flags & LYS_USED_GRP)) {
2130 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, &submod->groupings[u]), error);
2131 }
2132 }
2133 LY_LIST_FOR(submod->data, pnode) {
2134 grps = (struct lysp_grp *)lysp_node_groupings(pnode);
2135 LY_ARRAY_FOR(grps, u) {
2136 if (!(grps[u].flags & LYS_USED_GRP)) {
2137 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &grps[u]), error);
2138 }
2139 }
2140 }
2141 }
2142 ctx.pmod = sp;
2143
2144#if 0
2145 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
2146 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
2147 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
2148 * the anotation definitions available in the internal schema structure. */
2149 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
2150 if (lyp_add_ietf_netconf_annotations(mod)) {
2151 lys_free(mod, NULL, 1, 1);
2152 return NULL;
2153 }
2154 }
2155#endif
2156
2157 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
2158 if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
2159 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error);
2160 }
2161
2162 /* there can be no leftover deviations */
2163 LY_CHECK_ERR_GOTO(ctx.devs.count, LOGINT(ctx.ctx); ret = LY_EINT, error);
2164
2165 for (i = 0; i < ctx.dflts.count; ++i) {
2166 lysc_unres_dflt_free(ctx.ctx, ctx.dflts.objs[i]);
2167 }
2168 ly_set_erase(&ctx.dflts, NULL);
2169 ly_set_erase(&ctx.xpath, NULL);
2170 ly_set_erase(&ctx.leafrefs, NULL);
2171 ly_set_erase(&ctx.groupings, NULL);
2172 ly_set_erase(&ctx.tpdf_chain, NULL);
2173 ly_set_erase(&ctx.augs, NULL);
2174 ly_set_erase(&ctx.devs, NULL);
2175 ly_set_erase(&ctx.uses_augs, NULL);
2176 ly_set_erase(&ctx.uses_rfns, NULL);
2177
2178 return LY_SUCCESS;
2179
2180error:
2181 lys_precompile_augments_deviations_revert(ctx.ctx, mod);
2182 lys_feature_precompile_revert(&ctx, mod);
2183 for (i = 0; i < ctx.dflts.count; ++i) {
2184 lysc_unres_dflt_free(ctx.ctx, ctx.dflts.objs[i]);
2185 }
2186 ly_set_erase(&ctx.dflts, NULL);
2187 ly_set_erase(&ctx.xpath, NULL);
2188 ly_set_erase(&ctx.leafrefs, NULL);
2189 ly_set_erase(&ctx.groupings, NULL);
2190 ly_set_erase(&ctx.tpdf_chain, NULL);
2191 for (i = 0; i < ctx.augs.count; ++i) {
2192 lysc_augment_free(ctx.ctx, ctx.augs.objs[i]);
2193 }
2194 ly_set_erase(&ctx.augs, NULL);
2195 for (i = 0; i < ctx.devs.count; ++i) {
2196 lysc_deviation_free(ctx.ctx, ctx.devs.objs[i]);
2197 }
2198 ly_set_erase(&ctx.devs, NULL);
2199 for (i = 0; i < ctx.uses_augs.count; ++i) {
2200 lysc_augment_free(ctx.ctx, ctx.uses_augs.objs[i]);
2201 }
2202 ly_set_erase(&ctx.uses_augs, NULL);
2203 for (i = 0; i < ctx.uses_rfns.count; ++i) {
2204 lysc_refine_free(ctx.ctx, ctx.uses_rfns.objs[i]);
2205 }
2206 ly_set_erase(&ctx.uses_rfns, NULL);
2207 lysc_module_free(mod_c, NULL);
2208 mod->compiled = NULL;
2209
2210 return ret;
2211}