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