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