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