Coverage Report

Created: 2025-08-26 06:20

/src/libyang/src/schema_compile.c
Line
Count
Source (jump to first uncovered line)
1
/**
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 <stddef.h>
21
#include <stdint.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include "common.h"
27
#include "compat.h"
28
#include "context.h"
29
#include "dict.h"
30
#include "in.h"
31
#include "log.h"
32
#include "parser_schema.h"
33
#include "path.h"
34
#include "plugins.h"
35
#include "plugins_exts.h"
36
#include "plugins_exts_compile.h"
37
#include "plugins_internal.h"
38
#include "plugins_types.h"
39
#include "schema_compile_amend.h"
40
#include "schema_compile_node.h"
41
#include "schema_features.h"
42
#include "set.h"
43
#include "tree.h"
44
#include "tree_data.h"
45
#include "tree_schema.h"
46
#include "tree_schema_internal.h"
47
#include "xpath.h"
48
49
/**
50
 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
51
 */
52
static LY_ERR
53
lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext)
54
0
{
55
0
    LY_ERR ret = LY_SUCCESS;
56
57
0
    if (ext_p->compiled && (ext_p->compiled->refcount == 1)) {
58
        /* context recompilation - all the extension instances were previously freed (the last link to the compiled extension
59
         * remains from the parsed extension definition) and now we are recompiling them again, to have the up-to-date
60
         * extension definition, we have to recompile it as well now */
61
0
        lysc_extension_free(ctx->ctx, &ext_p->compiled);
62
0
    }
63
64
0
    if (!ext_p->compiled) {
65
0
        lysc_update_path(ctx, NULL, "{extension}");
66
0
        lysc_update_path(ctx, NULL, ext_p->name);
67
68
        /* compile the extension definition */
69
0
        ext_p->compiled = calloc(1, sizeof **ext);
70
0
        ext_p->compiled->refcount = 1;
71
0
        DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done);
72
0
        DUP_STRING_GOTO(ctx->ctx, ext_p->argname, ext_p->compiled->argname, ret, done);
73
0
        ext_p->compiled->module = (struct lys_module *)ext_mod;
74
0
        COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, ret, done);
75
76
0
        lysc_update_path(ctx, NULL, NULL);
77
0
        lysc_update_path(ctx, NULL, NULL);
78
79
        /* find extension definition plugin */
80
0
        ext_p->compiled->plugin = lyplg_find(LYPLG_EXTENSION, ext_p->compiled->module->name,
81
0
                ext_p->compiled->module->revision, ext_p->compiled->name);
82
0
    }
83
84
0
    *ext = lysc_ext_dup(ext_p->compiled);
85
86
0
done:
87
0
    if (ret) {
88
0
        lysc_update_path(ctx, NULL, NULL);
89
0
        lysc_update_path(ctx, NULL, NULL);
90
0
    }
91
0
    return ret;
92
0
}
93
94
LY_ERR
95
lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
96
        const struct lys_module *ext_mod)
97
0
{
98
0
    LY_ERR ret = LY_SUCCESS;
99
0
    struct lysp_ext *ext_def;
100
101
0
    ext->parent_stmt = ext_p->parent_stmt;
102
0
    ext->parent_stmt_index = ext_p->parent_stmt_index;
103
0
    ext->module = ctx->cur_mod;
104
0
    ext->parent = parent;
105
106
0
    lysc_update_path(ctx, LY_STMT_IS_NODE(ext->parent_stmt) ? ((struct lysc_node *)ext->parent)->module : NULL, "{extension}");
107
0
    lysc_update_path(ctx, NULL, ext_p->name);
108
109
0
    LY_CHECK_GOTO(ret = lysp_ext_find_definition(ctx->ctx, ext_p, &ext_mod, &ext_def), cleanup);
110
0
    LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, ext_def, &ext->def), cleanup);
111
112
0
    if (ext_def->argname) {
113
0
        LY_CHECK_GOTO(ret = lysp_ext_instance_resolve_argument(ctx->ctx, ext_p, ext_def), cleanup);
114
0
    }
115
116
0
    DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret);
117
0
    LY_CHECK_RET(ret);
118
119
0
    if (ext->def->plugin && ext->def->plugin->compile) {
120
0
        if (ext->argument) {
121
0
            lysc_update_path(ctx, ext->module, ext->argument);
122
0
        }
123
0
        ret = ext->def->plugin->compile(ctx, ext_p, ext);
124
0
        if (ret == LY_ENOT) {
125
0
            lysc_ext_instance_free(ctx->ctx, ext);
126
0
        }
127
0
        if (ext->argument) {
128
0
            lysc_update_path(ctx, NULL, NULL);
129
0
        }
130
0
        LY_CHECK_GOTO(ret, cleanup);
131
0
    }
132
133
0
cleanup:
134
0
    lysc_update_path(ctx, NULL, NULL);
135
0
    lysc_update_path(ctx, NULL, NULL);
136
137
0
    return ret;
138
0
}
139
140
struct lysc_ext *
141
lysc_ext_dup(struct lysc_ext *orig)
142
0
{
143
0
    ++orig->refcount;
144
0
    return orig;
145
0
}
146
147
API LY_ERR
148
lysc_ext_substmt(const struct lysc_ext_instance *ext, enum ly_stmt substmt, void **instance_p, enum ly_stmt_cardinality *cardinality_p)
149
0
{
150
0
    LY_ARRAY_COUNT_TYPE u;
151
152
0
    LY_ARRAY_FOR(ext->substmts, u) {
153
0
        if (LY_STMT_IS_DATA_NODE(substmt)) {
154
0
            if (!LY_STMT_IS_DATA_NODE(ext->substmts[u].stmt)) {
155
0
                continue;
156
0
            }
157
0
        } else if (LY_STMT_IS_OP(substmt)) {
158
0
            if (!LY_STMT_IS_OP(ext->substmts[u].stmt)) {
159
0
                continue;
160
0
            }
161
0
        } else if (ext->substmts[u].stmt != substmt) {
162
0
            continue;
163
0
        }
164
165
        /* match */
166
0
        if (cardinality_p) {
167
0
            *cardinality_p = ext->substmts[u].cardinality;
168
0
        }
169
0
        if (instance_p) {
170
0
            *instance_p = ext->substmts[u].storage;
171
0
        }
172
0
        return LY_SUCCESS;
173
0
    }
174
175
0
    return LY_ENOT;
176
0
}
177
178
static void
179
lysc_unres_dflt_free(const struct ly_ctx *ctx, struct lysc_unres_dflt *r)
180
0
{
181
0
    assert(!r->dflt || !r->dflts);
182
0
    if (r->dflt) {
183
0
        lysp_qname_free((struct ly_ctx *)ctx, r->dflt);
184
0
        free(r->dflt);
185
0
    } else {
186
0
        FREE_ARRAY((struct ly_ctx *)ctx, r->dflts, lysp_qname_free);
187
0
    }
188
0
    free(r);
189
0
}
190
191
void
192
lysc_update_path(struct lysc_ctx *ctx, struct lys_module *parent_module, const char *name)
193
0
{
194
0
    int len;
195
0
    uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
196
197
0
    if (!name) {
198
        /* removing last path segment */
199
0
        if (ctx->path[ctx->path_len - 1] == '}') {
200
0
            for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
201
0
            if (ctx->path[ctx->path_len] == '=') {
202
0
                ctx->path[ctx->path_len++] = '}';
203
0
            } else {
204
                /* not a top-level special tag, remove also preceiding '/' */
205
0
                goto remove_nodelevel;
206
0
            }
207
0
        } else {
208
0
remove_nodelevel:
209
0
            for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
210
0
            if (ctx->path_len == 0) {
211
                /* top-level (last segment) */
212
0
                ctx->path_len = 1;
213
0
            }
214
0
        }
215
        /* set new terminating NULL-byte */
216
0
        ctx->path[ctx->path_len] = '\0';
217
0
    } else {
218
0
        if (ctx->path_len > 1) {
219
0
            if (!parent_module && (ctx->path[ctx->path_len - 1] == '}') && (ctx->path[ctx->path_len - 2] != '\'')) {
220
                /* extension of the special tag */
221
0
                nextlevel = 2;
222
0
                --ctx->path_len;
223
0
            } else {
224
                /* there is already some path, so add next level */
225
0
                nextlevel = 1;
226
0
            }
227
0
        } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
228
229
0
        if (nextlevel != 2) {
230
0
            if ((parent_module && (parent_module == ctx->cur_mod)) || (!parent_module && (ctx->path_len > 1) && (name[0] == '{'))) {
231
                /* module not changed, print the name unprefixed */
232
0
                len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
233
0
            } else {
234
0
                len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->cur_mod->name, name);
235
0
            }
236
0
        } else {
237
0
            len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
238
0
        }
239
0
        if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
240
            /* output truncated */
241
0
            ctx->path_len = LYSC_CTX_BUFSIZE - 1;
242
0
        } else {
243
0
            ctx->path_len += len;
244
0
        }
245
0
    }
246
247
0
    LOG_LOCBACK(0, 0, 1, 0);
248
0
    LOG_LOCSET(NULL, NULL, ctx->path, NULL);
249
0
}
250
251
LY_ERR
252
lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
253
        struct lysp_ident *identities_p, struct lysc_ident **identities)
254
0
{
255
0
    LY_ARRAY_COUNT_TYPE u;
256
0
    struct lysc_ctx context = {0};
257
0
    struct lysc_ident *ident;
258
0
    LY_ERR ret = LY_SUCCESS;
259
0
    ly_bool enabled;
260
261
0
    assert(ctx_sc || ctx);
262
263
0
    if (!ctx_sc) {
264
0
        context.ctx = ctx;
265
0
        context.cur_mod = parsed_mod ? parsed_mod->mod : NULL;
266
0
        context.pmod = parsed_mod;
267
0
        context.path_len = 1;
268
0
        context.path[0] = '/';
269
0
        ctx_sc = &context;
270
0
    }
271
272
0
    if (!identities_p) {
273
0
        return LY_SUCCESS;
274
0
    }
275
276
0
    lysc_update_path(ctx_sc, NULL, "{identity}");
277
0
    LY_ARRAY_FOR(identities_p, u) {
278
        /* evaluate if-features */
279
0
        LY_CHECK_RET(lys_eval_iffeatures(ctx, identities_p[u].iffeatures, &enabled));
280
0
        if (!enabled) {
281
0
            continue;
282
0
        }
283
284
0
        lysc_update_path(ctx_sc, NULL, identities_p[u].name);
285
286
        /* add new compiled identity */
287
0
        LY_ARRAY_NEW_RET(ctx_sc->ctx, *identities, ident, LY_EMEM);
288
289
0
        DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, ident->name, ret, done);
290
0
        DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, ident->dsc, ret, done);
291
0
        DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, ident->ref, ret, done);
292
0
        ident->module = ctx_sc->cur_mod;
293
        /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
294
0
        COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, ident->exts, ident, ret, done);
295
0
        ident->flags = identities_p[u].flags;
296
297
0
        lysc_update_path(ctx_sc, NULL, NULL);
298
0
    }
299
0
    lysc_update_path(ctx_sc, NULL, NULL);
300
0
done:
301
0
    return ret;
302
0
}
303
304
/**
305
 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
306
 *
307
 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
308
 *
309
 * @param[in] ctx Compile context for logging.
310
 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
311
 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
312
 * @return LY_SUCCESS if everything is ok.
313
 * @return LY_EVALID if the identity is derived from itself.
314
 */
315
static LY_ERR
316
lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
317
0
{
318
0
    LY_ERR ret = LY_SUCCESS;
319
0
    LY_ARRAY_COUNT_TYPE u, v;
320
0
    struct ly_set recursion = {0};
321
0
    struct lysc_ident *drv;
322
323
0
    if (!derived) {
324
0
        return LY_SUCCESS;
325
0
    }
326
327
0
    for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
328
0
        if (ident == derived[u]) {
329
0
            LOGVAL(ctx->ctx, LYVE_REFERENCE,
330
0
                    "Identity \"%s\" is indirectly derived from itself.", ident->name);
331
0
            ret = LY_EVALID;
332
0
            goto cleanup;
333
0
        }
334
0
        ret = ly_set_add(&recursion, derived[u], 0, NULL);
335
0
        LY_CHECK_GOTO(ret, cleanup);
336
0
    }
337
338
0
    for (v = 0; v < recursion.count; ++v) {
339
0
        drv = recursion.objs[v];
340
0
        for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
341
0
            if (ident == drv->derived[u]) {
342
0
                LOGVAL(ctx->ctx, LYVE_REFERENCE,
343
0
                        "Identity \"%s\" is indirectly derived from itself.", ident->name);
344
0
                ret = LY_EVALID;
345
0
                goto cleanup;
346
0
            }
347
0
            ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
348
0
            LY_CHECK_GOTO(ret, cleanup);
349
0
        }
350
0
    }
351
352
0
cleanup:
353
0
    ly_set_erase(&recursion, NULL);
354
0
    return ret;
355
0
}
356
357
LY_ERR
358
lys_compile_identity_bases(struct lysc_ctx *ctx, const struct lysp_module *base_pmod, const char **bases_p,
359
        struct lysc_ident *ident, struct lysc_ident ***bases, ly_bool *enabled)
360
0
{
361
0
    LY_ARRAY_COUNT_TYPE u, v;
362
0
    const char *s, *name;
363
0
    const struct lys_module *mod;
364
0
    struct lysc_ident **idref;
365
366
0
    assert((ident && enabled) || bases);
367
368
0
    if ((LY_ARRAY_COUNT(bases_p) > 1) && (ctx->pmod->version < LYS_VERSION_1_1)) {
369
0
        LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
370
0
                "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
371
0
        return LY_EVALID;
372
0
    }
373
374
0
    LY_ARRAY_FOR(bases_p, u) {
375
0
        s = strchr(bases_p[u], ':');
376
0
        if (s) {
377
            /* prefixed identity */
378
0
            name = &s[1];
379
0
            mod = ly_resolve_prefix(ctx->ctx, bases_p[u], s - bases_p[u], LY_VALUE_SCHEMA, (void *)base_pmod);
380
0
        } else {
381
0
            name = bases_p[u];
382
0
            mod = base_pmod->mod;
383
0
        }
384
0
        if (!mod) {
385
0
            if (ident) {
386
0
                LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
387
0
                        "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
388
0
            } else {
389
0
                LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
390
0
                        "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
391
0
            }
392
0
            return LY_EVALID;
393
0
        }
394
395
0
        idref = NULL;
396
0
        LY_ARRAY_FOR(mod->identities, v) {
397
0
            if (!strcmp(name, mod->identities[v].name)) {
398
0
                if (ident) {
399
0
                    if (ident == &mod->identities[v]) {
400
0
                        LOGVAL(ctx->ctx, LYVE_REFERENCE,
401
0
                                "Identity \"%s\" is derived from itself.", ident->name);
402
0
                        return LY_EVALID;
403
0
                    }
404
0
                    LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived));
405
                    /* we have match! store the backlink */
406
0
                    LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
407
0
                    *idref = ident;
408
0
                } else {
409
                    /* we have match! store the found identity */
410
0
                    LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
411
0
                    *idref = &mod->identities[v];
412
0
                }
413
0
                break;
414
0
            }
415
0
        }
416
0
        if (!idref || !(*idref)) {
417
0
            if (ident || (ctx->options & LYS_COMPILE_DISABLED)) {
418
                /* look into the parsed module to check whether the identity is not merely disabled */
419
0
                LY_ARRAY_FOR(mod->parsed->identities, v) {
420
0
                    if (!strcmp(mod->parsed->identities[v].name, name)) {
421
0
                        if (ident) {
422
0
                            *enabled = 0;
423
0
                        }
424
0
                        return LY_SUCCESS;
425
0
                    }
426
0
                }
427
0
            }
428
0
            if (ident) {
429
0
                LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
430
0
                        "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
431
0
            } else {
432
0
                LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
433
0
                        "Unable to find base (%s) of identityref.", bases_p[u]);
434
0
            }
435
0
            return LY_EVALID;
436
0
        }
437
0
    }
438
439
0
    if (ident) {
440
0
        *enabled = 1;
441
0
    }
442
0
    return LY_SUCCESS;
443
0
}
444
445
/**
446
 * @brief For the given array of identities, set the backlinks from all their base identities.
447
 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
448
 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
449
 * @param[in,out] idents Array of referencing identities to which the backlinks are supposed to be set. Any
450
 * identities with disabled bases are removed.
451
 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
452
 */
453
static LY_ERR
454
lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident **idents)
455
0
{
456
0
    LY_ARRAY_COUNT_TYPE u, v;
457
0
    ly_bool enabled;
458
459
0
    lysc_update_path(ctx, NULL, "{identity}");
460
461
0
restart:
462
0
    for (u = 0, v = 0; u < LY_ARRAY_COUNT(*idents); ++u) {
463
        /* find matching parsed identity, the disabled ones are missing in the compiled array */
464
0
        while (v < LY_ARRAY_COUNT(idents_p)) {
465
0
            if (idents_p[v].name == (*idents)[u].name) {
466
0
                break;
467
0
            }
468
0
            ++v;
469
0
        }
470
0
        assert(v < LY_ARRAY_COUNT(idents_p));
471
472
0
        if (!idents_p[v].bases) {
473
0
            continue;
474
0
        }
475
0
        lysc_update_path(ctx, NULL, (*idents)[u].name);
476
0
        LY_CHECK_RET(lys_compile_identity_bases(ctx, (*idents)[u].module->parsed, idents_p[v].bases, &(*idents)[u], NULL,
477
0
                &enabled));
478
0
        lysc_update_path(ctx, NULL, NULL);
479
480
0
        if (!enabled) {
481
            /* remove the identity */
482
0
            lysc_ident_free(ctx->ctx, &(*idents)[u]);
483
0
            LY_ARRAY_DECREMENT(*idents);
484
0
            if (u < LY_ARRAY_COUNT(*idents)) {
485
0
                memmove(&(*idents)[u], &(*idents)[u + 1], (LY_ARRAY_COUNT(*idents) - u) * sizeof **idents);
486
0
            }
487
488
            /* revert compilation of all the previous identities */
489
0
            for (v = 0; v < u; ++v) {
490
0
                LY_ARRAY_FREE((*idents)[v].derived);
491
0
                (*idents)[v].derived = NULL;
492
0
            }
493
494
            /* free the whole array if there are no identites left */
495
0
            if (!LY_ARRAY_COUNT(*idents)) {
496
0
                LY_ARRAY_FREE(*idents);
497
0
                *idents = NULL;
498
0
            }
499
500
            /* restart the whole process without this identity */
501
0
            goto restart;
502
0
        }
503
0
    }
504
505
0
    lysc_update_path(ctx, NULL, NULL);
506
0
    return LY_SUCCESS;
507
0
}
508
509
static void *
510
lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
511
0
{
512
0
    for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) {
513
0
        if (substmts[u].stmt == stmt) {
514
0
            return substmts[u].storage;
515
0
        }
516
0
    }
517
0
    return NULL;
518
0
}
519
520
LY_ERR
521
lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext)
522
0
{
523
0
    LY_ERR ret = LY_SUCCESS, r;
524
0
    LY_ARRAY_COUNT_TYPE u;
525
0
    struct lysp_stmt *stmt;
526
0
    void *parsed = NULL, **compiled = NULL;
527
528
    /* check for invalid substatements */
529
0
    for (stmt = ext_p->child; stmt; stmt = stmt->next) {
530
0
        if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
531
0
            continue;
532
0
        }
533
0
        LY_ARRAY_FOR(ext->substmts, u) {
534
0
            if (ext->substmts[u].stmt == stmt->kw) {
535
0
                break;
536
0
            }
537
0
        }
538
0
        if (u == LY_ARRAY_COUNT(ext->substmts)) {
539
0
            LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
540
0
                    stmt->stmt, ext_p->name, ext_p->argument ? " " : "", ext_p->argument ? ext_p->argument : "");
541
0
            ret = LY_EVALID;
542
0
            goto cleanup;
543
0
        }
544
0
    }
545
546
    /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
547
548
    /* note into the compile context that we are processing extension now */
549
0
    ctx->ext = ext;
550
551
    /* keep order of the processing the same as the order in the defined substmts,
552
     * the order is important for some of the statements depending on others (e.g. type needs status and units) */
553
554
0
    LY_ARRAY_FOR(ext->substmts, u) {
555
0
        uint64_t stmt_counter = 0;
556
557
0
        for (stmt = ext_p->child; stmt; stmt = stmt->next) {
558
0
            if (ext->substmts[u].stmt != stmt->kw) {
559
0
                continue;
560
0
            }
561
562
0
            parsed = NULL;
563
0
            stmt_counter++;
564
0
            if (ext->substmts[u].storage) {
565
0
                switch (stmt->kw) {
566
0
                case LY_STMT_ACTION:
567
0
                case LY_STMT_ANYDATA:
568
0
                case LY_STMT_ANYXML:
569
0
                case LY_STMT_CONTAINER:
570
0
                case LY_STMT_CHOICE:
571
0
                case LY_STMT_LEAF:
572
0
                case LY_STMT_LEAF_LIST:
573
0
                case LY_STMT_LIST:
574
0
                case LY_STMT_NOTIFICATION:
575
0
                case LY_STMT_RPC:
576
0
                case LY_STMT_USES:
577
0
                    if (!ext_p->parsed) {
578
0
                        struct lysp_ext_instance *unconst_ext_p;
579
0
                        r = lysp_stmt_parse(ctx, stmt, &parsed, NULL);
580
0
                        LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
581
0
                        unconst_ext_p = (struct lysp_ext_instance *)ext_p;
582
0
                        unconst_ext_p->parsed = parsed;
583
0
                    } else {
584
0
                        struct lysp_node *node, *last_node;
585
                        /* get last parsed node */
586
0
                        LY_LIST_FOR(ext_p->parsed, node) {
587
0
                            last_node = node;
588
0
                        }
589
                        /* create and link sibling */
590
0
                        r = lysp_stmt_parse(ctx, stmt, &parsed, NULL);
591
0
                        LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
592
0
                        last_node->next = parsed;
593
0
                    }
594
595
                    /* set storage as an alternative document root in the compile context */
596
0
                    r = lys_compile_node(ctx, parsed, NULL, 0, NULL);
597
0
                    LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
598
0
                    break;
599
0
                case LY_STMT_DESCRIPTION:
600
0
                case LY_STMT_REFERENCE:
601
0
                case LY_STMT_UNITS: {
602
0
                    const char **str_p;
603
604
0
                    if (ext->substmts[u].cardinality < LY_STMT_CARD_SOME) {
605
                        /* single item */
606
0
                        if (*((const char **)ext->substmts[u].storage)) {
607
0
                            LOGVAL(ctx->ctx, LY_VCODE_DUPSTMT, stmt->stmt);
608
0
                            ret = LY_EVALID;
609
0
                            goto cleanup;
610
0
                        }
611
0
                        str_p = (const char **)ext->substmts[u].storage;
612
0
                    } else {
613
                        /* sized array */
614
0
                        const char ***strings_array = (const char ***)ext->substmts[u].storage;
615
0
                        LY_ARRAY_NEW_GOTO(ctx->ctx, *strings_array, str_p, ret, cleanup);
616
0
                    }
617
0
                    r = lydict_insert(ctx->ctx, stmt->arg, 0, str_p);
618
0
                    LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
619
0
                    break;
620
0
                }
621
0
                case LY_STMT_IF_FEATURE: {
622
0
                    ly_bool enabled;
623
624
0
                    r = lysp_stmt_parse(ctx, stmt, &parsed, NULL);
625
0
                    LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
626
627
0
                    r = lys_eval_iffeatures(ctx->ctx, parsed, &enabled);
628
0
                    FREE_ARRAY(ctx->ctx, (struct lysp_qname *)parsed, lysp_qname_free);
629
0
                    LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
630
0
                    if (!enabled) {
631
                        /* it is disabled, remove the whole extension instance */
632
0
                        return LY_ENOT;
633
0
                    }
634
0
                    break;
635
0
                }
636
0
                case LY_STMT_STATUS:
637
0
                    assert(ext->substmts[u].cardinality < LY_STMT_CARD_SOME);
638
0
                    LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, &ext->substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
639
0
                    break;
640
0
                case LY_STMT_TYPE: {
641
0
                    uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, ext->substmts);
642
0
                    const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, ext->substmts);
643
644
0
                    if (ext->substmts[u].cardinality < LY_STMT_CARD_SOME) {
645
                        /* single item */
646
0
                        if (*(struct lysc_type **)ext->substmts[u].storage) {
647
0
                            LOGVAL(ctx->ctx, LY_VCODE_DUPSTMT, stmt->stmt);
648
0
                            ret = LY_EVALID;
649
0
                            goto cleanup;
650
0
                        }
651
0
                        compiled = ext->substmts[u].storage;
652
0
                    } else {
653
                        /* sized array */
654
0
                        struct lysc_type ***types = (struct lysc_type ***)ext->substmts[u].storage, **type = NULL;
655
0
                        LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
656
0
                        compiled = (void *)type;
657
0
                    }
658
659
0
                    r = lysp_stmt_parse(ctx, stmt, &parsed, NULL);
660
0
                    LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
661
0
                    r = lys_compile_type(ctx, NULL, flags ? *flags : 0, ext_p->name, parsed, (struct lysc_type **)compiled,
662
0
                            units && !*units ? units : NULL, NULL);
663
0
                    lysp_type_free(ctx->ctx, parsed);
664
0
                    free(parsed);
665
0
                    LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
666
0
                    break;
667
0
                }
668
                /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
669
                 * also note that in many statements their extensions are not taken into account  */
670
0
                default:
671
0
                    LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s%s%s\") substatement.",
672
0
                            stmt->stmt, ext_p->name, ext_p->argument ? " " : "", ext_p->argument ? ext_p->argument : "");
673
0
                    ret = LY_EVALID;
674
0
                    goto cleanup;
675
0
                }
676
0
            }
677
0
        }
678
679
0
        if (((ext->substmts[u].cardinality == LY_STMT_CARD_MAND) || (ext->substmts[u].cardinality == LY_STMT_CARD_SOME)) && !stmt_counter) {
680
0
            LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
681
0
                    ly_stmt2str(ext->substmts[u].stmt), ext_p->name, ext_p->argument ? " " : "", ext_p->argument ? ext_p->argument : "");
682
0
            ret = LY_EVALID;
683
0
            goto cleanup;
684
0
        }
685
0
    }
686
687
0
cleanup:
688
0
    ctx->ext = NULL;
689
0
    return ret;
690
0
}
691
692
/**
693
 * @brief Check when for cyclic dependencies.
694
 *
695
 * @param[in] set Set with all the referenced nodes.
696
 * @param[in] node Node whose "when" referenced nodes are in @p set.
697
 * @return LY_ERR value
698
 */
699
static LY_ERR
700
lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
701
0
{
702
0
    struct lyxp_set tmp_set;
703
0
    struct lyxp_set_scnode *xp_scnode;
704
0
    uint32_t i, j;
705
0
    LY_ARRAY_COUNT_TYPE u;
706
0
    LY_ERR ret = LY_SUCCESS;
707
708
0
    memset(&tmp_set, 0, sizeof tmp_set);
709
710
    /* prepare in_ctx of the set */
711
0
    for (i = 0; i < set->used; ++i) {
712
0
        xp_scnode = &set->val.scnodes[i];
713
714
0
        if (xp_scnode->in_ctx != LYXP_SET_SCNODE_START_USED) {
715
            /* check node when, skip the context node (it was just checked) */
716
0
            xp_scnode->in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
717
0
        }
718
0
    }
719
720
0
    for (i = 0; i < set->used; ++i) {
721
0
        xp_scnode = &set->val.scnodes[i];
722
0
        if (xp_scnode->in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
723
            /* already checked */
724
0
            continue;
725
0
        }
726
727
0
        if ((xp_scnode->type != LYXP_NODE_ELEM) || !lysc_node_when(xp_scnode->scnode)) {
728
            /* no when to check */
729
0
            xp_scnode->in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
730
0
            continue;
731
0
        }
732
733
0
        node = xp_scnode->scnode;
734
0
        do {
735
0
            struct lysc_when **when_list, *when;
736
737
0
            LOG_LOCSET(node, NULL, NULL, NULL);
738
0
            when_list = lysc_node_when(node);
739
0
            LY_ARRAY_FOR(when_list, u) {
740
0
                when = when_list[u];
741
0
                ret = lyxp_atomize(set->ctx, when->cond, node->module, LY_VALUE_SCHEMA_RESOLVED, when->prefixes,
742
0
                        when->context, &tmp_set, LYXP_SCNODE_SCHEMA);
743
0
                if (ret != LY_SUCCESS) {
744
0
                    LOGVAL(set->ctx, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
745
0
                    goto cleanup;
746
0
                }
747
748
0
                for (j = 0; j < tmp_set.used; ++j) {
749
                    /* skip roots'n'stuff */
750
0
                    if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
751
                        /* try to find this node in our set */
752
0
                        uint32_t idx;
753
0
                        if (lyxp_set_scnode_contains(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1, &idx) &&
754
0
                                (set->val.scnodes[idx].in_ctx == LYXP_SET_SCNODE_START_USED)) {
755
0
                            LOGVAL(set->ctx, LYVE_SEMANTICS, "When condition includes a self-reference.");
756
0
                            ret = LY_EVALID;
757
0
                            goto cleanup;
758
0
                        }
759
760
                        /* needs to be checked, if in both sets, will be ignored */
761
0
                        tmp_set.val.scnodes[j].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
762
0
                    } else {
763
                        /* no when, nothing to check */
764
0
                        tmp_set.val.scnodes[j].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
765
0
                    }
766
0
                }
767
768
                /* merge this set into the global when set */
769
0
                lyxp_set_scnode_merge(set, &tmp_set);
770
0
            }
771
772
            /* check when of non-data parents as well */
773
0
            node = node->parent;
774
775
0
            LOG_LOCBACK(1, 0, 0, 0);
776
0
        } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
777
778
        /* this node when was checked (xp_scnode could have been reallocd) */
779
0
        set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
780
0
    }
781
782
0
cleanup:
783
0
    lyxp_set_free_content(&tmp_set);
784
0
    return ret;
785
0
}
786
787
LY_ERR
788
lysc_check_status(struct lysc_ctx *ctx, uint16_t flags1, void *mod1, const char *name1, uint16_t flags2, void *mod2,
789
        const char *name2)
790
0
{
791
0
    uint16_t flg1, flg2;
792
793
0
    flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
794
0
    flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
795
796
0
    if ((flg1 < flg2) && (mod1 == mod2)) {
797
0
        if (ctx) {
798
0
            LOGVAL(ctx->ctx, LYVE_REFERENCE,
799
0
                    "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".",
800
0
                    flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1,
801
0
                    flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2);
802
0
        }
803
0
        return LY_EVALID;
804
0
    }
805
806
0
    return LY_SUCCESS;
807
0
}
808
809
LY_ERR
810
lys_compile_expr_implement(const struct ly_ctx *ctx, const struct lyxp_expr *expr, LY_VALUE_FORMAT format,
811
        void *prefix_data, ly_bool implement, struct lys_glob_unres *unres, const struct lys_module **mod_p)
812
0
{
813
0
    uint32_t i;
814
0
    const char *ptr, *start;
815
0
    const struct lys_module *mod;
816
817
0
    assert(implement || mod_p);
818
819
0
    for (i = 0; i < expr->used; ++i) {
820
0
        if ((expr->tokens[i] != LYXP_TOKEN_NAMETEST) && (expr->tokens[i] != LYXP_TOKEN_LITERAL)) {
821
            /* token cannot have a prefix */
822
0
            continue;
823
0
        }
824
825
0
        start = expr->expr + expr->tok_pos[i];
826
0
        if (!(ptr = ly_strnchr(start, ':', expr->tok_len[i]))) {
827
            /* token without a prefix */
828
0
            continue;
829
0
        }
830
831
0
        if (!(mod = ly_resolve_prefix(ctx, start, ptr - start, format, prefix_data))) {
832
            /* unknown prefix, do not care right now */
833
0
            continue;
834
0
        }
835
836
0
        if (!mod->implemented) {
837
            /* unimplemented module found */
838
0
            if (implement) {
839
0
                LY_CHECK_RET(lys_set_implemented_r((struct lys_module *)mod, NULL, unres));
840
0
                if (unres->recompile) {
841
0
                    return LY_ERECOMPILE;
842
0
                }
843
0
            } else {
844
0
                *mod_p = mod;
845
0
                break;
846
0
            }
847
0
        }
848
0
    }
849
850
0
    return LY_SUCCESS;
851
0
}
852
853
/**
854
 * @brief Check when/must expressions of a node on a complete compiled schema tree.
855
 *
856
 * @param[in] ctx Compile context.
857
 * @param[in] node Node to check.
858
 * @param[in,out] unres Global unres structure.
859
 * @return LY_ERECOMPILE
860
 * @return LY_ERR value
861
 */
862
static LY_ERR
863
lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node, struct lys_glob_unres *unres)
864
0
{
865
0
    struct lyxp_set tmp_set;
866
0
    uint32_t i, opts;
867
0
    LY_ARRAY_COUNT_TYPE u;
868
0
    ly_bool input_done = 0;
869
0
    struct lysc_when **whens = NULL;
870
0
    struct lysc_must *musts = NULL;
871
0
    LY_ERR ret = LY_SUCCESS;
872
0
    const struct lys_module *mod;
873
874
0
    LOG_LOCSET(node, NULL, NULL, NULL);
875
876
0
    memset(&tmp_set, 0, sizeof tmp_set);
877
0
    opts = LYXP_SCNODE_SCHEMA | ((node->flags & LYS_IS_OUTPUT) ? LYXP_SCNODE_OUTPUT : 0);
878
879
0
    whens = lysc_node_when(node);
880
0
    musts = lysc_node_musts(node);
881
882
0
    LY_ARRAY_FOR(whens, u) {
883
        /* first check whether all the referenced modules are implemented */
884
0
        mod = NULL;
885
0
        ret = lys_compile_expr_implement(ctx->ctx, whens[u]->cond, LY_VALUE_SCHEMA_RESOLVED, whens[u]->prefixes,
886
0
                ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, unres, &mod);
887
0
        if (ret) {
888
0
            goto cleanup;
889
0
        } else if (mod) {
890
0
            LOGWRN(ctx->ctx, "When condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
891
0
                    whens[u]->cond->expr, mod->name);
892
0
            continue;
893
0
        }
894
895
        /* check "when" */
896
0
        ret = lyxp_atomize(ctx->ctx, whens[u]->cond, node->module, LY_VALUE_SCHEMA_RESOLVED, whens[u]->prefixes,
897
0
                whens[u]->context, &tmp_set, opts);
898
0
        if (ret) {
899
0
            LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid when condition \"%s\".", whens[u]->cond->expr);
900
0
            goto cleanup;
901
0
        }
902
903
0
        ctx->path[0] = '\0';
904
0
        lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
905
0
        for (i = 0; i < tmp_set.used; ++i) {
906
            /* skip roots'n'stuff */
907
0
            if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START_USED)) {
908
0
                struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
909
910
                /* XPath expression cannot reference "lower" status than the node that has the definition */
911
0
                ret = lysc_check_status(ctx, whens[u]->flags, node->module, node->name, schema->flags, schema->module,
912
0
                        schema->name);
913
0
                LY_CHECK_GOTO(ret, cleanup);
914
915
                /* check dummy node children/value accessing */
916
0
                if (lysc_data_parent(schema) == node) {
917
0
                    LOGVAL(ctx->ctx, LYVE_SEMANTICS, "When condition is accessing its own conditional node children.");
918
0
                    ret = LY_EVALID;
919
0
                    goto cleanup;
920
0
                } else if ((schema == node) && (tmp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
921
0
                    LOGVAL(ctx->ctx, LYVE_SEMANTICS, "When condition is accessing its own conditional node value.");
922
0
                    ret = LY_EVALID;
923
0
                    goto cleanup;
924
0
                }
925
0
            }
926
0
        }
927
928
        /* check cyclic dependencies */
929
0
        ret = lys_compile_unres_when_cyclic(&tmp_set, node);
930
0
        LY_CHECK_GOTO(ret, cleanup);
931
932
0
        lyxp_set_free_content(&tmp_set);
933
0
    }
934
935
0
check_musts:
936
0
    LY_ARRAY_FOR(musts, u) {
937
        /* first check whether all the referenced modules are implemented */
938
0
        mod = NULL;
939
0
        ret = lys_compile_expr_implement(ctx->ctx, musts[u].cond, LY_VALUE_SCHEMA_RESOLVED, musts[u].prefixes,
940
0
                ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED, unres, &mod);
941
0
        if (ret) {
942
0
            goto cleanup;
943
0
        } else if (mod) {
944
0
            LOGWRN(ctx->ctx, "Must condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
945
0
                    musts[u].cond->expr, mod->name);
946
0
            continue;
947
0
        }
948
949
        /* check "must" */
950
0
        ret = lyxp_atomize(ctx->ctx, musts[u].cond, node->module, LY_VALUE_SCHEMA_RESOLVED, musts[u].prefixes, node,
951
0
                &tmp_set, opts);
952
0
        if (ret) {
953
0
            LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr);
954
0
            goto cleanup;
955
0
        }
956
957
0
        ctx->path[0] = '\0';
958
0
        lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
959
0
        for (i = 0; i < tmp_set.used; ++i) {
960
            /* skip roots'n'stuff */
961
0
            if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
962
                /* XPath expression cannot reference "lower" status than the node that has the definition */
963
0
                ret = lysc_check_status(ctx, node->flags, node->module, node->name, tmp_set.val.scnodes[i].scnode->flags,
964
0
                        tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name);
965
0
                LY_CHECK_GOTO(ret, cleanup);
966
0
            }
967
0
        }
968
969
0
        lyxp_set_free_content(&tmp_set);
970
0
    }
971
972
0
    if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) {
973
        /* now check output musts */
974
0
        input_done = 1;
975
0
        whens = NULL;
976
0
        musts = ((struct lysc_node_action *)node)->output.musts;
977
0
        opts = LYXP_SCNODE_OUTPUT;
978
0
        goto check_musts;
979
0
    }
980
981
0
cleanup:
982
0
    lyxp_set_free_content(&tmp_set);
983
0
    LOG_LOCBACK(1, 0, 0, 0);
984
0
    return ret;
985
0
}
986
987
/**
988
 * @brief Check leafref for its target existence on a complete compiled schema tree.
989
 *
990
 * @param[in] ctx Compile context.
991
 * @param[in] node Context node for the leafref.
992
 * @param[in] lref Leafref to check/resolve.
993
 * @param[in,out] unres Global unres structure.
994
 * @return LY_ERECOMPILE if context recompilation is needed,
995
 * @return LY_ERR value.
996
 */
997
static LY_ERR
998
lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref,
999
        struct lys_glob_unres *unres)
1000
0
{
1001
0
    const struct lysc_node *target = NULL;
1002
0
    struct ly_path *p;
1003
0
    struct lysc_type *type;
1004
1005
0
    assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
1006
1007
    /* try to find the target */
1008
0
    LY_CHECK_RET(ly_path_compile(ctx->ctx, lref->cur_mod, node, NULL, lref->path, LY_PATH_LREF_TRUE,
1009
0
            (node->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
1010
0
            LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, unres, &p));
1011
1012
    /* get the target node */
1013
0
    target = p[LY_ARRAY_COUNT(p) - 1].node;
1014
0
    ly_path_free(node->module->ctx, p);
1015
1016
0
    if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1017
0
        LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
1018
0
                lref->path->expr, lys_nodetype2str(target->nodetype));
1019
0
        return LY_EVALID;
1020
0
    }
1021
1022
    /* check status */
1023
0
    ctx->path[0] = '\0';
1024
0
    lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
1025
0
    ctx->path_len = strlen(ctx->path);
1026
0
    if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) {
1027
0
        return LY_EVALID;
1028
0
    }
1029
0
    ctx->path_len = 1;
1030
0
    ctx->path[1] = '\0';
1031
1032
    /* check config */
1033
0
    if (lref->require_instance) {
1034
0
        if ((node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
1035
0
            LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
1036
0
                    " to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
1037
0
            return LY_EVALID;
1038
0
        }
1039
0
    }
1040
1041
    /* store the target's type and check for circular chain of leafrefs */
1042
0
    lref->realtype = ((struct lysc_node_leaf *)target)->type;
1043
0
    for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) {
1044
0
        if (type == (struct lysc_type *)lref) {
1045
            /* circular chain detected */
1046
0
            LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid leafref path \"%s\" - circular chain of leafrefs detected.",
1047
0
                    lref->path->expr);
1048
0
            return LY_EVALID;
1049
0
        }
1050
0
    }
1051
1052
    /* TODO check if leafref and its target are under common if-features */
1053
1054
0
    return LY_SUCCESS;
1055
0
}
1056
1057
/**
1058
 * @brief Compile default value(s) for leaf or leaf-list expecting a complete compiled schema tree.
1059
 *
1060
 * @param[in] ctx Compile context.
1061
 * @param[in] node Leaf or leaf-list to compile the default value(s) for.
1062
 * @param[in] type Type of the default value.
1063
 * @param[in] dflt Default value.
1064
 * @param[in] dflt_pmod Parsed module of the @p dflt to resolve possible prefixes.
1065
 * @param[in,out] storage Storage for the compiled default value.
1066
 * @param[in,out] unres Global unres structure for newly implemented modules.
1067
 * @return LY_ERR value.
1068
 */
1069
static LY_ERR
1070
lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_type *type, const char *dflt,
1071
        const struct lysp_module *dflt_pmod, struct lyd_value *storage, struct lys_glob_unres *unres)
1072
0
{
1073
0
    LY_ERR ret;
1074
0
    uint32_t options;
1075
0
    struct ly_err_item *err = NULL;
1076
1077
0
    options = (ctx->ctx->flags & LY_CTX_REF_IMPLEMENTED) ? LYPLG_TYPE_STORE_IMPLEMENT : 0;
1078
0
    ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), options, LY_VALUE_SCHEMA, (void *)dflt_pmod,
1079
0
            LYD_HINT_SCHEMA, node, storage, unres, &err);
1080
0
    if (ret == LY_ERECOMPILE) {
1081
        /* fine, but we need to recompile */
1082
0
        return LY_ERECOMPILE;
1083
0
    } else if (ret == LY_EINCOMPLETE) {
1084
        /* we have no data so we will not be resolving it */
1085
0
        ret = LY_SUCCESS;
1086
0
    }
1087
1088
0
    if (ret) {
1089
0
        LOG_LOCSET(node, NULL, NULL, NULL);
1090
0
        if (err) {
1091
0
            LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid default - value does not fit the type (%s).", err->msg);
1092
0
            ly_err_free(err);
1093
0
        } else {
1094
0
            LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid default - value does not fit the type.");
1095
0
        }
1096
0
        LOG_LOCBACK(1, 0, 0, 0);
1097
0
        return ret;
1098
0
    }
1099
1100
0
    ++((struct lysc_type *)storage->realtype)->refcount;
1101
0
    return LY_SUCCESS;
1102
0
}
1103
1104
/**
1105
 * @brief Compile default value of a leaf expecting a complete compiled schema tree.
1106
 *
1107
 * @param[in] ctx Compile context.
1108
 * @param[in] leaf Leaf that the default value is for.
1109
 * @param[in] dflt Default value to compile.
1110
 * @param[in,out] unres Global unres structure for newly implemented modules.
1111
 * @return LY_ERR value.
1112
 */
1113
static LY_ERR
1114
lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt,
1115
        struct lys_glob_unres *unres)
1116
0
{
1117
0
    LY_ERR ret;
1118
1119
0
    assert(!leaf->dflt);
1120
1121
0
    if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
1122
        /* ignore default values for keys and mandatory leaves */
1123
0
        return LY_SUCCESS;
1124
0
    }
1125
1126
    /* allocate the default value */
1127
0
    leaf->dflt = calloc(1, sizeof *leaf->dflt);
1128
0
    LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM);
1129
1130
    /* store the default value */
1131
0
    ret = lys_compile_unres_dflt(ctx, &leaf->node, leaf->type, dflt->str, dflt->mod, leaf->dflt, unres);
1132
0
    if (ret) {
1133
0
        free(leaf->dflt);
1134
0
        leaf->dflt = NULL;
1135
0
    }
1136
1137
0
    return ret;
1138
0
}
1139
1140
/**
1141
 * @brief Compile default values of a leaf-list expecting a complete compiled schema tree.
1142
 *
1143
 * @param[in] ctx Compile context.
1144
 * @param[in] llist Leaf-list that the default value(s) are for.
1145
 * @param[in] dflt Default value to compile, in case of a single value.
1146
 * @param[in] dflts Sized array of default values, in case of more values.
1147
 * @param[in,out] unres Global unres structure for newly implemented modules.
1148
 * @return LY_ERR value.
1149
 */
1150
static LY_ERR
1151
lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflt,
1152
        struct lysp_qname *dflts, struct lys_glob_unres *unres)
1153
0
{
1154
0
    LY_ERR ret;
1155
0
    LY_ARRAY_COUNT_TYPE orig_count, u, v;
1156
1157
0
    assert(dflt || dflts);
1158
1159
    /* in case there were already some defaults and we are adding new by deviations */
1160
0
    orig_count = LY_ARRAY_COUNT(llist->dflts);
1161
1162
    /* allocate new items */
1163
0
    LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + (dflts ? LY_ARRAY_COUNT(dflts) : 1), LY_EMEM);
1164
1165
    /* fill each new default value */
1166
0
    if (dflts) {
1167
0
        LY_ARRAY_FOR(dflts, u) {
1168
0
            llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts);
1169
0
            ret = lys_compile_unres_dflt(ctx, &llist->node, llist->type, dflts[u].str, dflts[u].mod,
1170
0
                    llist->dflts[orig_count + u], unres);
1171
0
            LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret);
1172
0
            LY_ARRAY_INCREMENT(llist->dflts);
1173
0
        }
1174
0
    } else {
1175
0
        llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts);
1176
0
        ret = lys_compile_unres_dflt(ctx, &llist->node, llist->type, dflt->str, dflt->mod,
1177
0
                llist->dflts[orig_count], unres);
1178
0
        LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret);
1179
0
        LY_ARRAY_INCREMENT(llist->dflts);
1180
0
    }
1181
1182
    /* check default value uniqueness */
1183
0
    if (llist->flags & LYS_CONFIG_W) {
1184
        /* configuration data values must be unique - so check the default values */
1185
0
        for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
1186
0
            for (v = 0; v < u; ++v) {
1187
0
                if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) {
1188
0
                    lysc_update_path(ctx, llist->parent ? llist->parent->module : NULL, llist->name);
1189
0
                    LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration leaf-list has multiple defaults of the same value \"%s\".",
1190
0
                            llist->dflts[u]->realtype->plugin->print(ctx->ctx, llist->dflts[u], LY_VALUE_CANON, NULL, NULL, NULL));
1191
0
                    lysc_update_path(ctx, NULL, NULL);
1192
0
                    return LY_EVALID;
1193
0
                }
1194
0
            }
1195
0
        }
1196
0
    }
1197
1198
0
    return LY_SUCCESS;
1199
0
}
1200
1201
LY_ERR
1202
lys_compile_unres_glob(struct ly_ctx *ctx, struct lys_glob_unres *unres)
1203
0
{
1204
0
    LY_ERR ret;
1205
0
    struct lysc_node *node;
1206
0
    struct lysc_type *type, *typeiter;
1207
0
    struct lysc_type_leafref *lref;
1208
0
    struct lysc_ctx cctx = {0};
1209
0
    LY_ARRAY_COUNT_TYPE v;
1210
0
    uint32_t i;
1211
1212
0
    if (unres->recompile) {
1213
0
recompilation:
1214
0
        assert(unres->recompile);
1215
        /* recompile all the modules and resolve the new unres instead (during recompilation) */
1216
0
        unres->recompile = 0;
1217
0
        return lys_recompile(ctx, 1);
1218
0
    }
1219
1220
    /* fake compile context */
1221
0
    cctx.ctx = ctx;
1222
0
    cctx.path_len = 1;
1223
0
    cctx.path[0] = '/';
1224
1225
    /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
1226
     * can be also leafref, in case it is already resolved, go through the chain and check that it does not
1227
     * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
1228
0
    for (i = 0; i < unres->leafrefs.count; ++i) {
1229
0
        LY_ERR ret = LY_SUCCESS;
1230
0
        node = unres->leafrefs.objs[i];
1231
0
        cctx.cur_mod = node->module;
1232
0
        cctx.pmod = node->module->parsed;
1233
1234
0
        LOG_LOCSET(node, NULL, NULL, NULL);
1235
1236
0
        assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
1237
0
        type = ((struct lysc_node_leaf *)node)->type;
1238
0
        if (type->basetype == LY_TYPE_LEAFREF) {
1239
0
            ret = lys_compile_unres_leafref(&cctx, node, (struct lysc_type_leafref *)type, unres);
1240
0
        } else if (type->basetype == LY_TYPE_UNION) {
1241
0
            LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
1242
0
                if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
1243
0
                    lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v];
1244
0
                    ret = lys_compile_unres_leafref(&cctx, node, lref, unres);
1245
0
                    if (ret) {
1246
0
                        break;
1247
0
                    }
1248
0
                }
1249
0
            }
1250
0
        }
1251
1252
0
        LOG_LOCBACK(1, 0, 0, 0);
1253
0
        if (ret == LY_ERECOMPILE) {
1254
            /* leafref caused a new module to be implemented, following leafrefs referencing the module would fail */
1255
0
            goto recompilation;
1256
0
        } else if (ret) {
1257
0
            return ret;
1258
0
        }
1259
0
    }
1260
0
    while (unres->leafrefs.count) {
1261
0
        node = unres->leafrefs.objs[unres->leafrefs.count - 1];
1262
0
        cctx.cur_mod = node->module;
1263
0
        cctx.pmod = node->module->parsed;
1264
1265
0
        LOG_LOCSET(node, NULL, NULL, NULL);
1266
1267
        /* store pointer to the real type */
1268
0
        type = ((struct lysc_node_leaf *)node)->type;
1269
0
        if (type->basetype == LY_TYPE_LEAFREF) {
1270
0
            for (typeiter = ((struct lysc_type_leafref *)type)->realtype;
1271
0
                    typeiter->basetype == LY_TYPE_LEAFREF;
1272
0
                    typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
1273
0
            ((struct lysc_type_leafref *)type)->realtype = typeiter;
1274
0
        } else if (type->basetype == LY_TYPE_UNION) {
1275
0
            LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) {
1276
0
                if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
1277
0
                    for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype;
1278
0
                            typeiter->basetype == LY_TYPE_LEAFREF;
1279
0
                            typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
1280
0
                    ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter;
1281
0
                }
1282
0
            }
1283
0
        }
1284
0
        LOG_LOCBACK(1, 0, 0, 0);
1285
1286
0
        ly_set_rm_index(&unres->leafrefs, unres->leafrefs.count - 1, NULL);
1287
0
    }
1288
1289
    /* check xpath */
1290
0
    while (unres->xpath.count) {
1291
0
        node = unres->xpath.objs[unres->xpath.count - 1];
1292
0
        cctx.cur_mod = node->module;
1293
0
        cctx.pmod = node->module->parsed;
1294
1295
0
        LOG_LOCSET(node, NULL, NULL, NULL);
1296
1297
0
        ret = lys_compile_unres_xpath(&cctx, node, unres);
1298
0
        LOG_LOCBACK(1, 0, 0, 0);
1299
0
        if (ret == LY_ERECOMPILE) {
1300
0
            goto recompilation;
1301
0
        } else if (ret) {
1302
0
            return ret;
1303
0
        }
1304
1305
0
        ly_set_rm_index(&unres->xpath, unres->xpath.count - 1, NULL);
1306
0
    }
1307
1308
    /* finish incomplete default values compilation */
1309
0
    while (unres->dflts.count) {
1310
0
        struct lysc_unres_dflt *r = unres->dflts.objs[unres->dflts.count - 1];
1311
0
        cctx.cur_mod = r->leaf->module;
1312
0
        cctx.pmod = r->leaf->module->parsed;
1313
1314
0
        LOG_LOCSET(&r->leaf->node, NULL, NULL, NULL);
1315
1316
0
        if (r->leaf->nodetype == LYS_LEAF) {
1317
0
            ret = lys_compile_unres_leaf_dlft(&cctx, r->leaf, r->dflt, unres);
1318
0
        } else {
1319
0
            ret = lys_compile_unres_llist_dflts(&cctx, r->llist, r->dflt, r->dflts, unres);
1320
0
        }
1321
0
        LOG_LOCBACK(1, 0, 0, 0);
1322
0
        if (ret == LY_ERECOMPILE) {
1323
0
            goto recompilation;
1324
0
        } else if (ret) {
1325
0
            return ret;
1326
0
        }
1327
1328
0
        lysc_unres_dflt_free(ctx, r);
1329
0
        ly_set_rm_index(&unres->dflts, unres->dflts.count - 1, NULL);
1330
0
    }
1331
1332
    /* some unres items may have been added */
1333
0
    if (unres->leafrefs.count || unres->xpath.count || unres->dflts.count) {
1334
0
        return lys_compile_unres_glob(ctx, unres);
1335
0
    }
1336
1337
    /* finally, remove all disabled nodes */
1338
0
    for (i = 0; i < unres->disabled.count; ++i) {
1339
0
        node = unres->disabled.snodes[i];
1340
0
        if (node->flags & LYS_KEY) {
1341
0
            LOG_LOCSET(node, NULL, NULL, NULL);
1342
0
            LOGVAL(ctx, LYVE_REFERENCE, "Key \"%s\" is disabled by its if-features.", node->name);
1343
0
            LOG_LOCBACK(1, 0, 0, 0);
1344
0
            return LY_EVALID;
1345
0
        }
1346
1347
0
        lysc_node_free(ctx, node, 1);
1348
0
    }
1349
1350
0
    return LY_SUCCESS;
1351
0
}
1352
1353
void
1354
lys_compile_unres_glob_revert(struct ly_ctx *ctx, struct lys_glob_unres *unres)
1355
0
{
1356
0
    uint32_t i;
1357
0
    struct lys_module *m;
1358
1359
0
    for (i = 0; i < unres->implementing.count; ++i) {
1360
0
        m = unres->implementing.objs[i];
1361
0
        assert(m->implemented);
1362
1363
        /* make the module correctly non-implemented again */
1364
0
        m->implemented = 0;
1365
0
        lys_precompile_augments_deviations_revert(ctx, m);
1366
0
    }
1367
1368
0
    for (i = 0; i < unres->creating.count; ++i) {
1369
0
        m = unres->creating.objs[i];
1370
1371
        /* remove the module from the context and free it */
1372
0
        ly_set_rm(&ctx->list, m, NULL);
1373
0
        lys_module_free(m);
1374
0
    }
1375
1376
0
    if (unres->implementing.count) {
1377
        /* recompile because some implemented modules are no longer implemented */
1378
0
        lys_recompile(ctx, 0);
1379
0
    }
1380
0
}
1381
1382
void
1383
lys_compile_unres_glob_erase(const struct ly_ctx *ctx, struct lys_glob_unres *unres)
1384
0
{
1385
0
    uint32_t i;
1386
1387
0
    ly_set_erase(&unres->implementing, NULL);
1388
0
    ly_set_erase(&unres->creating, NULL);
1389
0
    for (i = 0; i < unres->dflts.count; ++i) {
1390
0
        lysc_unres_dflt_free(ctx, unres->dflts.objs[i]);
1391
0
    }
1392
0
    ly_set_erase(&unres->dflts, NULL);
1393
0
    ly_set_erase(&unres->xpath, NULL);
1394
0
    ly_set_erase(&unres->leafrefs, NULL);
1395
0
    ly_set_erase(&unres->disabled, NULL);
1396
0
}
1397
1398
/**
1399
 * @brief Finish compilation of all the module unres sets in a compile context.
1400
 *
1401
 * @param[in] ctx Compile context with unres sets.
1402
 * @return LY_ERR value.
1403
 */
1404
static LY_ERR
1405
lys_compile_unres_mod(struct lysc_ctx *ctx)
1406
0
{
1407
0
    struct lysc_augment *aug;
1408
0
    struct lysc_deviation *dev;
1409
0
    struct lys_module *orig_mod = ctx->cur_mod;
1410
0
    uint32_t i;
1411
1412
    /* check that all augments were applied */
1413
0
    for (i = 0; i < ctx->augs.count; ++i) {
1414
0
        aug = ctx->augs.objs[i];
1415
0
        ctx->cur_mod = aug->aug_pmod->mod;
1416
0
        lysc_update_path(ctx, NULL, "{augment}");
1417
0
        lysc_update_path(ctx, NULL, aug->nodeid->expr);
1418
0
        LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" from module \"%s\" was not found.",
1419
0
                aug->nodeid->expr, LYSP_MODULE_NAME(aug->aug_pmod));
1420
0
        ctx->cur_mod = orig_mod;
1421
0
        lysc_update_path(ctx, NULL, NULL);
1422
0
        lysc_update_path(ctx, NULL, NULL);
1423
0
    }
1424
0
    if (ctx->augs.count) {
1425
0
        return LY_ENOTFOUND;
1426
0
    }
1427
1428
    /* check that all deviations were applied */
1429
0
    for (i = 0; i < ctx->devs.count; ++i) {
1430
0
        dev = ctx->devs.objs[i];
1431
0
        lysc_update_path(ctx, NULL, "{deviation}");
1432
0
        lysc_update_path(ctx, NULL, dev->nodeid->expr);
1433
0
        LOGVAL(ctx->ctx, LYVE_REFERENCE, "Deviation(s) target node \"%s\" from module \"%s\" was not found.",
1434
0
                dev->nodeid->expr, LYSP_MODULE_NAME(dev->dev_pmods[0]));
1435
0
        lysc_update_path(ctx, NULL, NULL);
1436
0
        lysc_update_path(ctx, NULL, NULL);
1437
0
    }
1438
0
    if (ctx->devs.count) {
1439
0
        return LY_ENOTFOUND;
1440
0
    }
1441
1442
0
    return LY_SUCCESS;
1443
0
}
1444
1445
/**
1446
 * @brief Erase all the module unres sets in a compile context.
1447
 *
1448
 * @param[in] ctx Compile context with unres sets.
1449
 * @param[in] error Whether the compilation finished with an error or not.
1450
 */
1451
static void
1452
lys_compile_unres_mod_erase(struct lysc_ctx *ctx, ly_bool error)
1453
0
{
1454
0
    uint32_t i;
1455
1456
0
    ly_set_erase(&ctx->groupings, NULL);
1457
0
    ly_set_erase(&ctx->tpdf_chain, NULL);
1458
1459
0
    if (!error) {
1460
        /* there can be no leftover deviations or augments */
1461
0
        LY_CHECK_ERR_RET(ctx->augs.count, LOGINT(ctx->ctx), );
1462
0
        LY_CHECK_ERR_RET(ctx->devs.count, LOGINT(ctx->ctx), );
1463
1464
0
        ly_set_erase(&ctx->augs, NULL);
1465
0
        ly_set_erase(&ctx->devs, NULL);
1466
0
        ly_set_erase(&ctx->uses_augs, NULL);
1467
0
        ly_set_erase(&ctx->uses_rfns, NULL);
1468
0
    } else {
1469
0
        for (i = 0; i < ctx->augs.count; ++i) {
1470
0
            lysc_augment_free(ctx->ctx, ctx->augs.objs[i]);
1471
0
        }
1472
0
        ly_set_erase(&ctx->augs, NULL);
1473
0
        for (i = 0; i < ctx->devs.count; ++i) {
1474
0
            lysc_deviation_free(ctx->ctx, ctx->devs.objs[i]);
1475
0
        }
1476
0
        ly_set_erase(&ctx->devs, NULL);
1477
0
        for (i = 0; i < ctx->uses_augs.count; ++i) {
1478
0
            lysc_augment_free(ctx->ctx, ctx->uses_augs.objs[i]);
1479
0
        }
1480
0
        ly_set_erase(&ctx->uses_augs, NULL);
1481
0
        for (i = 0; i < ctx->uses_rfns.count; ++i) {
1482
0
            lysc_refine_free(ctx->ctx, ctx->uses_rfns.objs[i]);
1483
0
        }
1484
0
        ly_set_erase(&ctx->uses_rfns, NULL);
1485
0
    }
1486
0
}
1487
1488
/**
1489
 * @brief Compile identites in the current module and all its submodules.
1490
 *
1491
 * @param[in] ctx Compile context.
1492
 * @return LY_ERR value.
1493
 */
1494
static LY_ERR
1495
lys_compile_identities(struct lysc_ctx *ctx)
1496
0
{
1497
0
    struct lysp_submodule *submod;
1498
0
    LY_ARRAY_COUNT_TYPE u;
1499
1500
0
    if (ctx->cur_mod->parsed->identities) {
1501
0
        LY_CHECK_RET(lys_compile_identities_derived(ctx, ctx->cur_mod->parsed->identities, &ctx->cur_mod->identities));
1502
0
    }
1503
0
    lysc_update_path(ctx, NULL, "{submodule}");
1504
0
    LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, u) {
1505
0
        submod = ctx->cur_mod->parsed->includes[u].submodule;
1506
0
        if (submod->identities) {
1507
0
            lysc_update_path(ctx, NULL, submod->name);
1508
0
            LY_CHECK_RET(lys_compile_identities_derived(ctx, submod->identities, &ctx->cur_mod->identities));
1509
0
            lysc_update_path(ctx, NULL, NULL);
1510
0
        }
1511
0
    }
1512
0
    lysc_update_path(ctx, NULL, NULL);
1513
1514
0
    return LY_SUCCESS;
1515
0
}
1516
1517
LY_ERR
1518
lys_recompile(struct ly_ctx *ctx, ly_bool log)
1519
0
{
1520
0
    uint32_t idx;
1521
0
    struct lys_module *mod;
1522
0
    struct lys_glob_unres unres = {0};
1523
0
    LY_ERR ret = LY_SUCCESS;
1524
0
    uint32_t prev_lo = 0;
1525
1526
    /* we are always recompiling all the modules */
1527
0
    unres.full_compilation = 1;
1528
1529
0
    if (!log) {
1530
        /* recompile, must succeed because the modules were already compiled; hide messages because any
1531
         * warnings were already printed, are not really relevant, and would hide the real error */
1532
0
        prev_lo = ly_log_options(0);
1533
0
    }
1534
1535
    /* free all the modules */
1536
0
    for (idx = 0; idx < ctx->list.count; ++idx) {
1537
0
        mod = ctx->list.objs[idx];
1538
0
        if (mod->compiled) {
1539
            /* free the module */
1540
0
            lysc_module_free(mod->compiled);
1541
0
            mod->compiled = NULL;
1542
0
        }
1543
1544
        /* free precompiled iffeatures */
1545
0
        lys_free_feature_iffeatures(mod->parsed);
1546
0
    }
1547
1548
    /* recompile all the modules */
1549
0
    for (idx = 0; idx < ctx->list.count; ++idx) {
1550
0
        mod = ctx->list.objs[idx];
1551
0
        if (!mod->implemented || mod->compiled) {
1552
            /* nothing to do */
1553
0
            continue;
1554
0
        }
1555
1556
        /* recompile */
1557
0
        ret = lys_compile(mod, 0, &unres);
1558
0
        if (ret) {
1559
0
            if (!log) {
1560
0
                LOGERR(mod->ctx, ret, "Recompilation of module \"%s\" failed.", mod->name);
1561
0
            }
1562
0
            goto cleanup;
1563
0
        }
1564
1565
0
        if (unres.recompile) {
1566
            /* we need to recompile again (newly compiled module caused another new implemented module) */
1567
0
            ret = lys_recompile(ctx, log);
1568
0
            goto cleanup;
1569
0
        }
1570
0
    }
1571
1572
    /* resolve global unres */
1573
0
    LY_CHECK_GOTO(ret = lys_compile_unres_glob(ctx, &unres), cleanup);
1574
1575
0
cleanup:
1576
0
    if (!log) {
1577
0
        ly_log_options(prev_lo);
1578
0
    }
1579
0
    lys_compile_unres_glob_erase(ctx, &unres);
1580
0
    return ret;
1581
0
}
1582
1583
/**
1584
 * @brief Check whether a module does not have any (recursive) compiled import.
1585
 *
1586
 * @param[in] mod Module to examine.
1587
 * @return Whether the module has a compiled imported module.
1588
 */
1589
static ly_bool
1590
lys_has_compiled_import_r(struct lys_module *mod)
1591
0
{
1592
0
    LY_ARRAY_COUNT_TYPE u;
1593
1594
0
    LY_ARRAY_FOR(mod->parsed->imports, u) {
1595
0
        if (!mod->parsed->imports[u].module->implemented) {
1596
0
            continue;
1597
0
        }
1598
1599
0
        if (mod->parsed->imports[u].module->compiled) {
1600
0
            return 1;
1601
0
        }
1602
1603
        /* recursive */
1604
0
        if (lys_has_compiled_import_r(mod->parsed->imports[u].module)) {
1605
0
            return 1;
1606
0
        }
1607
0
    }
1608
1609
0
    return 0;
1610
0
}
1611
1612
LY_ERR
1613
lys_compile(struct lys_module *mod, uint32_t options, struct lys_glob_unres *unres)
1614
0
{
1615
0
    struct lysc_ctx ctx = {0};
1616
0
    struct lysc_module *mod_c = NULL;
1617
0
    struct lysp_module *sp;
1618
0
    struct lysp_submodule *submod;
1619
0
    struct lysp_node *pnode;
1620
0
    struct lysp_node_grp *grp;
1621
0
    LY_ARRAY_COUNT_TYPE u;
1622
0
    LY_ERR ret = LY_SUCCESS;
1623
1624
0
    LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
1625
1626
    /* if some previous module was not fully compiled, it is forbidden to compile another one (even though it
1627
     * may be okay in some cases) */
1628
0
    assert(!unres->recompile);
1629
1630
0
    if (!mod->implemented) {
1631
        /* just imported modules are not compiled */
1632
0
        return LY_SUCCESS;
1633
0
    }
1634
1635
0
    sp = mod->parsed;
1636
1637
0
    ctx.ctx = mod->ctx;
1638
0
    ctx.cur_mod = mod;
1639
0
    ctx.pmod = sp;
1640
0
    ctx.options = options;
1641
0
    ctx.path_len = 1;
1642
0
    ctx.path[0] = '/';
1643
0
    ctx.unres = unres;
1644
1645
    /* augments and deviations */
1646
0
    LY_CHECK_GOTO(ret = lys_precompile_augments_deviations(&ctx), cleanup);
1647
1648
0
    if (!unres->recompile && !unres->full_compilation) {
1649
        /* check whether this module may reference any already-compiled modules */
1650
0
        if (lys_has_compiled_import_r(mod)) {
1651
            /* it may and we need even disabled nodes in those modules, recompile them */
1652
0
            unres->recompile = 1;
1653
0
        }
1654
0
    }
1655
1656
0
    if (unres->recompile) {
1657
        /* we need the context recompiled */
1658
0
        goto cleanup;
1659
0
    }
1660
1661
    /* now the actual compilation will take place */
1662
0
    ++mod->ctx->change_count;
1663
0
    mod->compiled = mod_c = calloc(1, sizeof *mod_c);
1664
0
    LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
1665
0
    mod_c->mod = mod;
1666
1667
    /* identities */
1668
0
    LY_CHECK_GOTO(ret = lys_compile_identities(&ctx), cleanup);
1669
1670
    /* compile augments and deviations of our module from other modules so they can be applied during compilation */
1671
0
    LY_CHECK_GOTO(ret = lys_precompile_own_augments(&ctx), cleanup);
1672
0
    LY_CHECK_GOTO(ret = lys_precompile_own_deviations(&ctx), cleanup);
1673
1674
    /* data nodes */
1675
0
    LY_LIST_FOR(sp->data, pnode) {
1676
0
        LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
1677
0
    }
1678
1679
    /* top-level RPCs */
1680
0
    LY_LIST_FOR((struct lysp_node *)sp->rpcs, pnode) {
1681
0
        LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
1682
0
    }
1683
1684
    /* top-level notifications */
1685
0
    LY_LIST_FOR((struct lysp_node *)sp->notifs, pnode) {
1686
0
        LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
1687
0
    }
1688
1689
    /* extension instances */
1690
0
    COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, ret, cleanup);
1691
1692
    /* the same for submodules */
1693
0
    LY_ARRAY_FOR(sp->includes, u) {
1694
0
        submod = sp->includes[u].submodule;
1695
0
        ctx.pmod = (struct lysp_module *)submod;
1696
1697
0
        LY_LIST_FOR(submod->data, pnode) {
1698
0
            ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
1699
0
            LY_CHECK_GOTO(ret, cleanup);
1700
0
        }
1701
1702
0
        LY_LIST_FOR((struct lysp_node *)submod->rpcs, pnode) {
1703
0
            ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
1704
0
            LY_CHECK_GOTO(ret, cleanup);
1705
0
        }
1706
1707
0
        LY_LIST_FOR((struct lysp_node *)submod->notifs, pnode) {
1708
0
            ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
1709
0
            LY_CHECK_GOTO(ret, cleanup);
1710
0
        }
1711
1712
0
        COMPILE_EXTS_GOTO(&ctx, submod->exts, mod_c->exts, mod_c, ret, cleanup);
1713
0
    }
1714
0
    ctx.pmod = sp;
1715
1716
    /* validate non-instantiated groupings from the parsed schema,
1717
     * without it we would accept even the schemas with invalid grouping specification */
1718
0
    ctx.options |= LYS_COMPILE_GROUPING;
1719
0
    LY_LIST_FOR(sp->groupings, grp) {
1720
0
        if (!(grp->flags & LYS_USED_GRP)) {
1721
0
            LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, grp), cleanup);
1722
0
        }
1723
0
    }
1724
0
    LY_LIST_FOR(sp->data, pnode) {
1725
0
        LY_LIST_FOR((struct lysp_node_grp *)lysp_node_groupings(pnode), grp) {
1726
0
            if (!(grp->flags & LYS_USED_GRP)) {
1727
0
                LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, grp), cleanup);
1728
0
            }
1729
0
        }
1730
0
    }
1731
0
    LY_ARRAY_FOR(sp->includes, u) {
1732
0
        submod = sp->includes[u].submodule;
1733
0
        ctx.pmod = (struct lysp_module *)submod;
1734
1735
0
        LY_LIST_FOR(submod->groupings, grp) {
1736
0
            if (!(grp->flags & LYS_USED_GRP)) {
1737
0
                LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, grp), cleanup);
1738
0
            }
1739
0
        }
1740
0
        LY_LIST_FOR(submod->data, pnode) {
1741
0
            LY_LIST_FOR((struct lysp_node_grp *)lysp_node_groupings(pnode), grp) {
1742
0
                if (!(grp->flags & LYS_USED_GRP)) {
1743
0
                    LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, grp), cleanup);
1744
0
                }
1745
0
            }
1746
0
        }
1747
0
    }
1748
0
    ctx.pmod = sp;
1749
1750
0
    LOG_LOCBACK(0, 0, 1, 0);
1751
1752
    /* finish compilation for all unresolved module items in the context */
1753
0
    LY_CHECK_GOTO(ret = lys_compile_unres_mod(&ctx), cleanup);
1754
1755
0
cleanup:
1756
0
    LOG_LOCBACK(0, 0, 1, 0);
1757
0
    lys_compile_unres_mod_erase(&ctx, ret);
1758
0
    if (ret) {
1759
0
        lys_precompile_augments_deviations_revert(ctx.ctx, mod);
1760
0
        lysc_module_free(mod_c);
1761
0
        mod->compiled = NULL;
1762
0
    }
1763
0
    return ret;
1764
0
}