blob: 6156baa2e4f90b8cc0a33d32ff72a036b7dce1e2 [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
Michal Vasko25d6ad02020-10-22 12:20:22 +02001375LY_ERR
1376lys_compile_expr_implement(const struct ly_ctx *ctx, const struct lyxp_expr *expr, LY_PREFIX_FORMAT format,
1377 void *prefix_data, ly_bool implement, const struct lys_module **mod_p)
Michal Vaskocfaff232020-10-20 09:35:14 +02001378{
1379 uint32_t i;
1380 const char *ptr, *start;
1381 const struct lys_module *mod;
1382
Michal Vasko25d6ad02020-10-22 12:20:22 +02001383 assert(implement || mod_p);
1384
Michal Vaskocfaff232020-10-20 09:35:14 +02001385 for (i = 0; i < expr->used; ++i) {
1386 if ((expr->tokens[i] != LYXP_TOKEN_NAMETEST) && (expr->tokens[i] != LYXP_TOKEN_LITERAL)) {
1387 /* token cannot have a prefix */
1388 continue;
1389 }
1390
1391 start = expr->expr + expr->tok_pos[i];
1392 if (!(ptr = ly_strnchr(start, ':', expr->tok_len[i]))) {
1393 /* token without a prefix */
1394 continue;
1395 }
1396
1397 if (!(mod = ly_resolve_prefix(ctx, start, ptr - start, format, prefix_data))) {
1398 /* unknown prefix, do not care right now */
1399 continue;
1400 }
1401
1402 if (!mod->implemented) {
1403 /* unimplemented module found */
Michal Vasko25d6ad02020-10-22 12:20:22 +02001404 if (implement) {
1405 LY_CHECK_RET(lys_set_implemented((struct lys_module *)mod));
1406 } else {
Michal Vaskocfaff232020-10-20 09:35:14 +02001407 *mod_p = mod;
Michal Vasko25d6ad02020-10-22 12:20:22 +02001408 break;
Michal Vaskocfaff232020-10-20 09:35:14 +02001409 }
Michal Vaskocfaff232020-10-20 09:35:14 +02001410 }
1411 }
1412
Michal Vasko25d6ad02020-10-22 12:20:22 +02001413 return LY_SUCCESS;
Michal Vaskocfaff232020-10-20 09:35:14 +02001414}
1415
1416/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001417 * @brief Check when/must expressions of a node on a complete compiled schema tree.
1418 *
1419 * @param[in] ctx Compile context.
1420 * @param[in] node Node to check.
1421 * @return LY_ERR value
1422 */
1423static LY_ERR
1424lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
1425{
1426 struct lyxp_set tmp_set;
1427 uint32_t i, opts;
1428 LY_ARRAY_COUNT_TYPE u;
1429 ly_bool input_done = 0;
1430 struct lysc_when **when = NULL;
1431 struct lysc_must *musts = NULL;
1432 LY_ERR ret = LY_SUCCESS;
1433 const struct lysc_node *op;
Michal Vaskocfaff232020-10-20 09:35:14 +02001434 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001435
1436 memset(&tmp_set, 0, sizeof tmp_set);
1437 opts = LYXP_SCNODE_SCHEMA;
1438 if (node->flags & LYS_CONFIG_R) {
1439 for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent) {}
1440 if (op) {
1441 /* we are actually in output */
1442 opts = LYXP_SCNODE_OUTPUT;
1443 }
1444 }
1445
1446 switch (node->nodetype) {
1447 case LYS_CONTAINER:
1448 when = ((struct lysc_node_container *)node)->when;
1449 musts = ((struct lysc_node_container *)node)->musts;
1450 break;
1451 case LYS_CHOICE:
1452 when = ((struct lysc_node_choice *)node)->when;
1453 break;
1454 case LYS_LEAF:
1455 when = ((struct lysc_node_leaf *)node)->when;
1456 musts = ((struct lysc_node_leaf *)node)->musts;
1457 break;
1458 case LYS_LEAFLIST:
1459 when = ((struct lysc_node_leaflist *)node)->when;
1460 musts = ((struct lysc_node_leaflist *)node)->musts;
1461 break;
1462 case LYS_LIST:
1463 when = ((struct lysc_node_list *)node)->when;
1464 musts = ((struct lysc_node_list *)node)->musts;
1465 break;
1466 case LYS_ANYXML:
1467 case LYS_ANYDATA:
1468 when = ((struct lysc_node_anydata *)node)->when;
1469 musts = ((struct lysc_node_anydata *)node)->musts;
1470 break;
1471 case LYS_CASE:
1472 when = ((struct lysc_node_case *)node)->when;
1473 break;
1474 case LYS_NOTIF:
1475 when = ((struct lysc_notif *)node)->when;
1476 musts = ((struct lysc_notif *)node)->musts;
1477 break;
1478 case LYS_RPC:
1479 case LYS_ACTION:
1480 /* first process when and input musts */
1481 when = ((struct lysc_action *)node)->when;
1482 musts = ((struct lysc_action *)node)->input.musts;
1483 break;
1484 default:
1485 /* nothing to check */
1486 break;
1487 }
1488
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001489 LY_ARRAY_FOR(when, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +02001490 /* first check whether all the referenced modules are implemented */
Michal Vasko25d6ad02020-10-22 12:20:22 +02001491 mod = NULL;
1492 ret = lys_compile_expr_implement(ctx->ctx, when[u]->cond, LY_PREF_SCHEMA_RESOLVED, when[u]->prefixes,
1493 ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, &mod);
1494 if (ret) {
1495 goto cleanup;
1496 } else if (mod) {
Michal Vaskocfaff232020-10-20 09:35:14 +02001497 LOGWRN(ctx->ctx, "When condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
1498 when[u]->cond->expr, mod->name);
1499 continue;
1500 }
1501
1502 /* check "when" */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001503 ret = lyxp_atomize(when[u]->cond, node->module, LY_PREF_SCHEMA_RESOLVED, when[u]->prefixes, when[u]->context,
1504 &tmp_set, opts);
Michal Vasko25d6ad02020-10-22 12:20:22 +02001505 if (ret) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001506 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr);
1507 goto cleanup;
1508 }
1509
1510 ctx->path[0] = '\0';
1511 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1512 for (i = 0; i < tmp_set.used; ++i) {
1513 /* skip roots'n'stuff */
1514 if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) {
1515 struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
1516
1517 /* XPath expression cannot reference "lower" status than the node that has the definition */
1518 ret = lysc_check_status(ctx, when[u]->flags, node->module, node->name, schema->flags, schema->module,
1519 schema->name);
1520 LY_CHECK_GOTO(ret, cleanup);
1521
1522 /* check dummy node accessing */
1523 if (schema == node) {
1524 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
1525 ret = LY_EVALID;
1526 goto cleanup;
1527 }
1528 }
1529 }
1530
1531 /* check cyclic dependencies */
1532 ret = lys_compile_unres_when_cyclic(&tmp_set, node);
1533 LY_CHECK_GOTO(ret, cleanup);
1534
1535 lyxp_set_free_content(&tmp_set);
1536 }
1537
1538check_musts:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001539 LY_ARRAY_FOR(musts, u) {
Michal Vaskocfaff232020-10-20 09:35:14 +02001540 /* first check whether all the referenced modules are implemented */
Michal Vasko25d6ad02020-10-22 12:20:22 +02001541 mod = NULL;
1542 ret = lys_compile_expr_implement(ctx->ctx, musts[u].cond, LY_PREF_SCHEMA_RESOLVED, musts[u].prefixes,
1543 ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, &mod);
1544 if (ret) {
1545 goto cleanup;
1546 } else if (mod) {
Michal Vaskocfaff232020-10-20 09:35:14 +02001547 LOGWRN(ctx->ctx, "Must condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
1548 musts[u].cond->expr, mod->name);
1549 continue;
1550 }
1551
1552 /* check "must" */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001553 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 +02001554 if (ret) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001555 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
1556 goto cleanup;
1557 }
1558
1559 ctx->path[0] = '\0';
1560 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1561 for (i = 0; i < tmp_set.used; ++i) {
1562 /* skip roots'n'stuff */
1563 if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
1564 /* XPath expression cannot reference "lower" status than the node that has the definition */
1565 ret = lysc_check_status(ctx, node->flags, node->module, node->name, tmp_set.val.scnodes[i].scnode->flags,
1566 tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
1567 LY_CHECK_GOTO(ret, cleanup);
1568 }
1569 }
1570
1571 lyxp_set_free_content(&tmp_set);
1572 }
1573
1574 if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
1575 /* now check output musts */
1576 input_done = 1;
1577 when = NULL;
1578 musts = ((struct lysc_action *)node)->output.musts;
1579 opts = LYXP_SCNODE_OUTPUT;
1580 goto check_musts;
1581 }
1582
1583cleanup:
1584 lyxp_set_free_content(&tmp_set);
1585 return ret;
1586}
1587
1588/**
1589 * @brief Check leafref for its target existence on a complete compiled schema tree.
1590 *
1591 * @param[in] ctx Compile context.
1592 * @param[in] node Context node for the leafref.
1593 * @param[in] lref Leafref to check/resolve.
1594 * @return LY_ERR value.
1595 */
1596static LY_ERR
1597lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref)
1598{
1599 const struct lysc_node *target = NULL, *siter;
1600 struct ly_path *p;
1601 struct lysc_type *type;
1602
1603 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
1604
1605 /* try to find the target */
1606 LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, lysc_is_output(node) ?
1607 LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, &p));
1608
1609 /* get the target node */
1610 target = p[LY_ARRAY_COUNT(p) - 1].node;
1611 ly_path_free(node->module->ctx, p);
1612
1613 if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1614 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
1615 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
1616 lref->path->expr, lys_nodetype2str(target->nodetype));
1617 return LY_EVALID;
1618 }
1619
1620 /* check status */
1621 ctx->path[0] = '\0';
1622 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1623 ctx->path_len = strlen(ctx->path);
1624 if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
1625 return LY_EVALID;
1626 }
1627 ctx->path_len = 1;
1628 ctx->path[1] = '\0';
1629
1630 /* check config */
1631 if (lref->require_instance) {
1632 for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {}
1633 if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
1634 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
1635 " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
1636 return LY_EVALID;
1637 }
1638 }
1639
1640 /* store the target's type and check for circular chain of leafrefs */
1641 lref->realtype = ((struct lysc_node_leaf *)target)->type;
1642 for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
1643 if (type == (struct lysc_type *)lref) {
1644 /* circular chain detected */
1645 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
1646 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr);
1647 return LY_EVALID;
1648 }
1649 }
1650
1651 /* check if leafref and its target are under common if-features */
1652 if (lys_compile_leafref_features_validate(node, target)) {
1653 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
1654 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
1655 " features applicable to the leafref itself.", lref->path->expr);
1656 return LY_EVALID;
1657 }
1658
1659 return LY_SUCCESS;
1660}
1661
1662static LY_ERR
1663lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod)
1664{
1665 struct lysc_ext_instance *ext;
1666 struct lysp_ext_instance *ext_p = NULL;
1667 struct lysp_stmt *stmt;
1668 const struct lys_module *ext_mod;
1669 LY_ERR ret = LY_SUCCESS;
1670
1671 /* create the parsed extension instance manually */
1672 ext_p = calloc(1, sizeof *ext_p);
1673 LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1674 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup);
1675 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "default", 0, &ext_p->argument), cleanup);
1676 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
1677 ext_p->insubstmt_index = 0;
1678
1679 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1680 LY_CHECK_ERR_GOTO(!stmt, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1681 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup);
1682 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "boolean", 0, &stmt->arg), cleanup);
1683 stmt->kw = LY_STMT_TYPE;
1684
1685 /* allocate new extension instance */
1686 LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup);
1687
1688 /* manually get extension definition module */
1689 ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
1690
1691 /* compile the extension instance */
1692 LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
1693
1694cleanup:
1695 lysp_ext_instance_free(ctx->ctx, ext_p);
1696 free(ext_p);
1697 return ret;
1698}
1699
1700/**
1701 * @brief Compile default value(s) for leaf or leaf-list expecting a complete compiled schema tree.
1702 *
1703 * @param[in] ctx Compile context.
1704 * @param[in] node Leaf or leaf-list to compile the default value(s) for.
1705 * @param[in] type Type of the default value.
1706 * @param[in] dflt Default value.
1707 * @param[in] dflt_pmod Parsed module of the @p dflt to resolve possible prefixes.
1708 * @param[in,out] storage Storage for the compiled default value.
1709 * @return LY_ERR value.
1710 */
1711static LY_ERR
1712lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_type *type, const char *dflt,
1713 const struct lysp_module *dflt_pmod, struct lyd_value *storage)
1714{
1715 LY_ERR ret;
Michal Vasko25d6ad02020-10-22 12:20:22 +02001716 uint32_t options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001717 struct ly_err_item *err = NULL;
1718
Michal Vasko25d6ad02020-10-22 12:20:22 +02001719 options = (ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED) ? LY_TYPE_STORE_IMPLEMENT : 0;
1720 ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), options, LY_PREF_SCHEMA, (void *)dflt_pmod,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001721 LYD_HINT_SCHEMA, node, storage, &err);
1722 if (ret == LY_EINCOMPLETE) {
1723 /* we have no data so we will not be resolving it */
1724 ret = LY_SUCCESS;
1725 }
1726
1727 if (ret) {
1728 ctx->path[0] = '\0';
1729 lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1730 if (err) {
1731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1732 "Invalid default - value does not fit the type (%s).", err->msg);
1733 ly_err_free(err);
1734 } else {
Michal Vasko25d6ad02020-10-22 12:20:22 +02001735 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 +02001736 }
1737 return ret;
1738 }
1739
1740 ++((struct lysc_type *)storage->realtype)->refcount;
1741 return LY_SUCCESS;
1742}
1743
1744/**
1745 * @brief Compile default value of a leaf expecting a complete compiled schema tree.
1746 *
1747 * @param[in] ctx Compile context.
1748 * @param[in] leaf Leaf that the default value is for.
1749 * @param[in] dflt Default value to compile.
1750 * @return LY_ERR value.
1751 */
1752static LY_ERR
1753lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
1754{
1755 LY_ERR ret;
1756
1757 assert(!leaf->dflt);
1758
1759 if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
1760 /* ignore default values for keys and mandatory leaves */
1761 return LY_SUCCESS;
1762 }
1763
1764 /* allocate the default value */
1765 leaf->dflt = calloc(1, sizeof *leaf->dflt);
1766 LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
1767
1768 /* store the default value */
1769 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt->str, dflt->mod, leaf->dflt);
1770 if (ret) {
1771 free(leaf->dflt);
1772 leaf->dflt = NULL;
1773 }
1774
1775 return ret;
1776}
1777
1778/**
1779 * @brief Compile default values of a leaf-list expecting a complete compiled schema tree.
1780 *
1781 * @param[in] ctx Compile context.
1782 * @param[in] llist Leaf-list that the default value(s) are for.
1783 * @param[in] dflt Default value to compile, in case of a single value.
1784 * @param[in] dflts Sized array of default values, in case of more values.
1785 * @return LY_ERR value.
1786 */
1787static LY_ERR
1788lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflt,
1789 struct lysp_qname *dflts)
1790{
1791 LY_ERR ret;
1792 LY_ARRAY_COUNT_TYPE orig_count, u, v;
1793
1794 assert(dflt || dflts);
1795
1796 if (llist->dflts) {
1797 /* there were already some defaults and we are adding new by deviations */
1798 assert(dflts);
1799 orig_count = LY_ARRAY_COUNT(llist->dflts);
1800 } else {
1801 orig_count = 0;
1802 }
1803
1804 /* allocate new items */
1805 if (dflts) {
1806 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
1807 } else {
1808 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
1809 }
1810
1811 /* fill each new default value */
1812 if (dflts) {
1813 LY_ARRAY_FOR(dflts, u) {
1814 llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
1815 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u].str, dflts[u].mod,
1816 llist->dflts[orig_count + u]);
1817 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
1818 LY_ARRAY_INCREMENT(llist->dflts);
1819 }
1820 } else {
1821 llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
1822 ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt->str, dflt->mod,
1823 llist->dflts[orig_count]);
1824 LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
1825 LY_ARRAY_INCREMENT(llist->dflts);
1826 }
1827
1828 /* check default value uniqueness */
1829 if (llist->flags & LYS_CONFIG_W) {
1830 /* configuration data values must be unique - so check the default values */
1831 for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
1832 for (v = 0; v < u; ++v) {
1833 if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
1834 lysc_update_path(ctx, llist->parent, llist->name);
1835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1836 "Configuration leaf-list has multiple defaults of the same value \"%s\".",
1837 llist->dflts[u]->canonical);
1838 lysc_update_path(ctx, NULL, NULL);
1839 return LY_EVALID;
1840 }
1841 }
1842 }
1843 }
1844
1845 return LY_SUCCESS;
1846}
1847
1848/**
1849 * @brief Finish compilation of all the unres sets of a compile context.
1850 *
1851 * @param[in] ctx Compile context with unres sets.
1852 * @return LY_ERR value.
1853 */
1854static LY_ERR
1855lys_compile_unres(struct lysc_ctx *ctx)
1856{
1857 struct lysc_node *node;
1858 struct lysc_type *type, *typeiter;
1859 struct lysc_type_leafref *lref;
1860 struct lysc_augment *aug;
1861 struct lysc_deviation *dev;
1862 LY_ARRAY_COUNT_TYPE v;
1863 uint32_t i;
1864
1865 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
1866 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
1867 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
1868 for (i = 0; i < ctx->leafrefs.count; ++i) {
1869 node = ctx->leafrefs.objs[i];
1870 assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
1871 type = ((struct lysc_node_leaf *)node)->type;
1872 if (type->basetype == LY_TYPE_LEAFREF) {
1873 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type));
1874 } else if (type->basetype == LY_TYPE_UNION) {
1875 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
1876 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
1877 lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
1878 LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref));
1879 }
1880 }
1881 }
1882 }
1883 for (i = 0; i < ctx->leafrefs.count; ++i) {
1884 /* store pointer to the real type */
1885 type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type;
1886 if (type->basetype == LY_TYPE_LEAFREF) {
1887 for (typeiter = ((struct lysc_type_leafref *)type)->realtype;
1888 typeiter->basetype == LY_TYPE_LEAFREF;
1889 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
1890 ((struct lysc_type_leafref *)type)->realtype = typeiter;
1891 } else if (type->basetype == LY_TYPE_UNION) {
1892 LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
1893 if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
1894 for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype;
1895 typeiter->basetype == LY_TYPE_LEAFREF;
1896 typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
1897 ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter;
1898 }
1899 }
1900 }
1901 }
1902
1903 /* check xpath */
1904 for (i = 0; i < ctx->xpath.count; ++i) {
1905 LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i]));
1906 }
1907
1908 /* finish incomplete default values compilation */
1909 for (i = 0; i < ctx->dflts.count; ++i) {
1910 struct lysc_unres_dflt *r = ctx->dflts.objs[i];
1911 if (r->leaf->nodetype == LYS_LEAF) {
1912 LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt));
1913 } else {
1914 LY_CHECK_RET(lys_compile_unres_llist_dflts(ctx, r->llist, r->dflt, r->dflts));
1915 }
1916 }
1917
1918 /* check that all augments were applied */
1919 for (i = 0; i < ctx->augs.count; ++i) {
1920 aug = ctx->augs.objs[i];
1921 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
1922 "Augment target node \"%s\" from module \"%s\" was not found.", aug->nodeid->expr,
1923 LYSP_MODULE_NAME(aug->nodeid_pmod));
1924 }
1925 if (ctx->augs.count) {
1926 return LY_ENOTFOUND;
1927 }
1928
1929 /* check that all deviations were applied */
1930 for (i = 0; i < ctx->devs.count; ++i) {
1931 dev = ctx->devs.objs[i];
1932 LOGVAL(ctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
1933 "Deviation(s) target node \"%s\" from module \"%s\" was not found.", dev->nodeid->expr,
1934 LYSP_MODULE_NAME(dev->dev_pmods[0]));
1935 }
1936 if (ctx->devs.count) {
1937 return LY_ENOTFOUND;
1938 }
1939
1940 return LY_SUCCESS;
1941}
1942
1943/**
1944 * @brief Compile features in the current module and all its submodules.
1945 *
1946 * @param[in] ctx Compile context.
1947 * @return LY_ERR value.
1948 */
1949static LY_ERR
1950lys_compile_features(struct lysc_ctx *ctx)
1951{
1952 struct lysp_submodule *submod;
1953 LY_ARRAY_COUNT_TYPE u, v;
1954
1955 if (!ctx->cur_mod->features) {
1956 /* features are compiled directly into the module structure,
1957 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
1958 LY_CHECK_RET(lys_feature_precompile(ctx, NULL, NULL, ctx->cur_mod->parsed->features, &ctx->cur_mod->features));
1959 LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, v) {
1960 submod = ctx->cur_mod->parsed->includes[v].submodule;
1961 LY_CHECK_RET(lys_feature_precompile(ctx, NULL, NULL, submod->features, &ctx->cur_mod->features));
1962 }
1963 }
1964
1965 /* finish feature compilation, not only for the main module, but also for the submodules.
1966 * Due to possible forward references, it must be done when all the features (including submodules)
1967 * are present. */
1968 LY_ARRAY_FOR(ctx->cur_mod->parsed->features, u) {
1969 LY_CHECK_RET(lys_feature_precompile_finish(ctx, &ctx->cur_mod->parsed->features[u], ctx->cur_mod->features));
1970 }
1971
1972 lysc_update_path(ctx, NULL, "{submodule}");
1973 LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, v) {
1974 submod = ctx->cur_mod->parsed->includes[v].submodule;
1975
1976 lysc_update_path(ctx, NULL, submod->name);
1977 LY_ARRAY_FOR(submod->features, u) {
1978 LY_CHECK_RET(lys_feature_precompile_finish(ctx, &submod->features[u], ctx->cur_mod->features));
1979 }
1980 lysc_update_path(ctx, NULL, NULL);
1981 }
1982 lysc_update_path(ctx, NULL, NULL);
1983
1984 return LY_SUCCESS;
1985}
1986
1987/**
1988 * @brief Compile identites in the current module and all its submodules.
1989 *
1990 * @param[in] ctx Compile context.
1991 * @return LY_ERR value.
1992 */
1993static LY_ERR
1994lys_compile_identities(struct lysc_ctx *ctx)
1995{
1996 struct lysp_submodule *submod;
1997 LY_ARRAY_COUNT_TYPE u;
1998
1999 if (!ctx->cur_mod->identities) {
2000 LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, ctx->cur_mod->parsed->identities, &ctx->cur_mod->identities));
2001 LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, u) {
2002 submod = ctx->cur_mod->parsed->includes[u].submodule;
2003 LY_CHECK_RET(lys_identity_precompile(ctx, NULL, NULL, submod->identities, &ctx->cur_mod->identities));
2004 }
2005 }
2006
2007 if (ctx->cur_mod->parsed->identities) {
2008 LY_CHECK_RET(lys_compile_identities_derived(ctx, ctx->cur_mod->parsed->identities, ctx->cur_mod->identities));
2009 }
2010 lysc_update_path(ctx, NULL, "{submodule}");
2011 LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, u) {
2012
2013 submod = ctx->cur_mod->parsed->includes[u].submodule;
2014 if (submod->identities) {
2015 lysc_update_path(ctx, NULL, submod->name);
2016 LY_CHECK_RET(lys_compile_identities_derived(ctx, submod->identities, ctx->cur_mod->identities));
2017 lysc_update_path(ctx, NULL, NULL);
2018 }
2019 }
2020 lysc_update_path(ctx, NULL, NULL);
2021
2022 return LY_SUCCESS;
2023}
2024
2025LY_ERR
2026lys_compile(struct lys_module *mod, uint32_t options)
2027{
2028 struct lysc_ctx ctx = {0};
2029 struct lysc_module *mod_c;
2030 struct lysp_module *sp;
2031 struct lysp_submodule *submod;
2032 struct lysp_node *pnode;
2033 struct lysp_grp *grps;
2034 LY_ARRAY_COUNT_TYPE u, v;
2035 uint32_t i;
2036 LY_ERR ret = LY_SUCCESS;
2037
2038 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
2039
2040 if (!mod->implemented) {
2041 /* just imported modules are not compiled */
2042 return LY_SUCCESS;
2043 }
2044
2045 /* context will be changed */
2046 ++mod->ctx->module_set_id;
2047
2048 sp = mod->parsed;
2049
2050 ctx.ctx = mod->ctx;
2051 ctx.cur_mod = mod;
2052 ctx.pmod = sp;
2053 ctx.options = options;
2054 ctx.path_len = 1;
2055 ctx.path[0] = '/';
2056
2057 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
2058 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
2059 mod_c->mod = mod;
2060
2061 /* process imports */
2062 LY_ARRAY_FOR(sp->imports, u) {
2063 LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
2064 }
2065
2066 /* features */
2067 LY_CHECK_GOTO(ret = lys_compile_features(&ctx), error);
2068
2069 /* identities, work similarly to features with the precompilation */
2070 LY_CHECK_GOTO(ret = lys_compile_identities(&ctx), error);
2071
2072 /* augments and deviations */
2073 LY_CHECK_GOTO(ret = lys_precompile_augments_deviations(&ctx), error);
2074
2075 /* compile augments and deviations of our module from other modules so they can be applied during compilation */
2076 LY_CHECK_GOTO(ret = lys_precompile_own_augments(&ctx), error);
2077 LY_CHECK_GOTO(ret = lys_precompile_own_deviations(&ctx), error);
2078
2079 /* data nodes */
2080 LY_LIST_FOR(sp->data, pnode) {
2081 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), error);
2082 }
2083
2084 /* top-level RPCs and notifications */
2085 COMPILE_OP_ARRAY_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
2086 COMPILE_OP_ARRAY_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
2087
2088 /* extension instances */
2089 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
2090
2091 /* the same for submodules */
2092 LY_ARRAY_FOR(sp->includes, u) {
2093 submod = sp->includes[u].submodule;
2094 ctx.pmod = (struct lysp_module *)submod;
2095
2096 LY_LIST_FOR(submod->data, pnode) {
2097 ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
2098 LY_CHECK_GOTO(ret, error);
2099 }
2100
2101 COMPILE_OP_ARRAY_GOTO(&ctx, submod->rpcs, mod_c->rpcs, NULL, v, lys_compile_action, 0, ret, error);
2102 COMPILE_OP_ARRAY_GOTO(&ctx, submod->notifs, mod_c->notifs, NULL, v, lys_compile_notif, 0, ret, error);
2103
2104 COMPILE_EXTS_GOTO(&ctx, submod->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
2105 }
2106
2107 /* finish compilation for all unresolved items in the context */
2108 LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error);
2109
2110 /* validate non-instantiated groupings from the parsed schema,
2111 * without it we would accept even the schemas with invalid grouping specification */
2112 ctx.pmod = sp;
2113 ctx.options |= LYS_COMPILE_GROUPING;
2114 LY_ARRAY_FOR(sp->groupings, u) {
2115 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
2116 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, &sp->groupings[u]), error);
2117 }
2118 }
2119 LY_LIST_FOR(sp->data, pnode) {
2120 grps = (struct lysp_grp *)lysp_node_groupings(pnode);
2121 LY_ARRAY_FOR(grps, u) {
2122 if (!(grps[u].flags & LYS_USED_GRP)) {
2123 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &grps[u]), error);
2124 }
2125 }
2126 }
2127 LY_ARRAY_FOR(sp->includes, u) {
2128 submod = sp->includes[u].submodule;
2129 ctx.pmod = (struct lysp_module *)submod;
2130
2131 LY_ARRAY_FOR(submod->groupings, u) {
2132 if (!(submod->groupings[u].flags & LYS_USED_GRP)) {
2133 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, &submod->groupings[u]), error);
2134 }
2135 }
2136 LY_LIST_FOR(submod->data, pnode) {
2137 grps = (struct lysp_grp *)lysp_node_groupings(pnode);
2138 LY_ARRAY_FOR(grps, u) {
2139 if (!(grps[u].flags & LYS_USED_GRP)) {
2140 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, &grps[u]), error);
2141 }
2142 }
2143 }
2144 }
2145 ctx.pmod = sp;
2146
2147#if 0
2148 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
2149 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
2150 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
2151 * the anotation definitions available in the internal schema structure. */
2152 if (ly_strequal(mod->name, "ietf-netconf", 0)) {
2153 if (lyp_add_ietf_netconf_annotations(mod)) {
2154 lys_free(mod, NULL, 1, 1);
2155 return NULL;
2156 }
2157 }
2158#endif
2159
2160 /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
2161 if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
2162 LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error);
2163 }
2164
2165 /* there can be no leftover deviations */
2166 LY_CHECK_ERR_GOTO(ctx.devs.count, LOGINT(ctx.ctx); ret = LY_EINT, error);
2167
2168 for (i = 0; i < ctx.dflts.count; ++i) {
2169 lysc_unres_dflt_free(ctx.ctx, ctx.dflts.objs[i]);
2170 }
2171 ly_set_erase(&ctx.dflts, NULL);
2172 ly_set_erase(&ctx.xpath, NULL);
2173 ly_set_erase(&ctx.leafrefs, NULL);
2174 ly_set_erase(&ctx.groupings, NULL);
2175 ly_set_erase(&ctx.tpdf_chain, NULL);
2176 ly_set_erase(&ctx.augs, NULL);
2177 ly_set_erase(&ctx.devs, NULL);
2178 ly_set_erase(&ctx.uses_augs, NULL);
2179 ly_set_erase(&ctx.uses_rfns, NULL);
2180
2181 return LY_SUCCESS;
2182
2183error:
2184 lys_precompile_augments_deviations_revert(ctx.ctx, mod);
2185 lys_feature_precompile_revert(&ctx, mod);
2186 for (i = 0; i < ctx.dflts.count; ++i) {
2187 lysc_unres_dflt_free(ctx.ctx, ctx.dflts.objs[i]);
2188 }
2189 ly_set_erase(&ctx.dflts, NULL);
2190 ly_set_erase(&ctx.xpath, NULL);
2191 ly_set_erase(&ctx.leafrefs, NULL);
2192 ly_set_erase(&ctx.groupings, NULL);
2193 ly_set_erase(&ctx.tpdf_chain, NULL);
2194 for (i = 0; i < ctx.augs.count; ++i) {
2195 lysc_augment_free(ctx.ctx, ctx.augs.objs[i]);
2196 }
2197 ly_set_erase(&ctx.augs, NULL);
2198 for (i = 0; i < ctx.devs.count; ++i) {
2199 lysc_deviation_free(ctx.ctx, ctx.devs.objs[i]);
2200 }
2201 ly_set_erase(&ctx.devs, NULL);
2202 for (i = 0; i < ctx.uses_augs.count; ++i) {
2203 lysc_augment_free(ctx.ctx, ctx.uses_augs.objs[i]);
2204 }
2205 ly_set_erase(&ctx.uses_augs, NULL);
2206 for (i = 0; i < ctx.uses_rfns.count; ++i) {
2207 lysc_refine_free(ctx.ctx, ctx.uses_rfns.objs[i]);
2208 }
2209 ly_set_erase(&ctx.uses_rfns, NULL);
2210 lysc_module_free(mod_c, NULL);
2211 mod->compiled = NULL;
2212
2213 return ret;
2214}