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