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