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