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