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