Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file schema_compile_node.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema compilation of common nodes. |
| 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 | |
Christian Hopps | 32874e1 | 2021-05-01 09:43:54 -0400 | [diff] [blame] | 15 | #define _GNU_SOURCE /* asprintf, strdup */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 16 | |
| 17 | #include "schema_compile_node.h" |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <ctype.h> |
| 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" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 29 | #include "dict.h" |
| 30 | #include "log.h" |
Radek Krejci | 0aa1f70 | 2021-04-01 16:16:19 +0200 | [diff] [blame] | 31 | #include "plugins.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 32 | #include "plugins_exts_compile.h" |
Radek Krejci | 04699f0 | 2021-03-22 21:50:29 +0100 | [diff] [blame] | 33 | #include "plugins_internal.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 34 | #include "plugins_types.h" |
| 35 | #include "schema_compile.h" |
| 36 | #include "schema_compile_amend.h" |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 37 | #include "schema_features.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 38 | #include "set.h" |
| 39 | #include "tree.h" |
| 40 | #include "tree_data.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 41 | #include "tree_edit.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 42 | #include "tree_schema.h" |
| 43 | #include "tree_schema_internal.h" |
| 44 | #include "xpath.h" |
| 45 | |
| 46 | static struct lysc_ext_instance * |
| 47 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 48 | { |
| 49 | /* TODO - extensions, increase refcount */ |
| 50 | (void) ctx; |
| 51 | (void) orig; |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | /** |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 56 | * @brief Add a node with must(s) to unres. |
| 57 | * |
| 58 | * @param[in] ctx Compile context. |
| 59 | * @param[in] node Compiled node with must(s). |
| 60 | * @param[in] pnode Parsed ndoe with must(s). |
| 61 | * @return LY_ERR value. |
| 62 | */ |
| 63 | static LY_ERR |
| 64 | lysc_unres_must_add(struct lysc_ctx *ctx, struct lysc_node *node, struct lysp_node *pnode) |
| 65 | { |
| 66 | struct lysc_unres_must *m = NULL; |
| 67 | LY_ARRAY_COUNT_TYPE u; |
| 68 | struct lysc_must *musts; |
| 69 | struct lysp_restr *pmusts; |
| 70 | LY_ERR ret; |
| 71 | |
| 72 | /* do not check must(s) in a grouping */ |
| 73 | if (ctx->compile_opts & LYS_COMPILE_GROUPING) { |
| 74 | return LY_SUCCESS; |
| 75 | } |
| 76 | |
| 77 | musts = lysc_node_musts(node); |
| 78 | pmusts = lysp_node_musts(pnode); |
| 79 | assert(LY_ARRAY_COUNT(musts) == LY_ARRAY_COUNT(pmusts)); |
| 80 | |
| 81 | if (!musts) { |
| 82 | /* no must */ |
| 83 | return LY_SUCCESS; |
| 84 | } |
| 85 | |
| 86 | /* add new unres must */ |
| 87 | m = calloc(1, sizeof *m); |
| 88 | LY_CHECK_ERR_GOTO(!m, ret = LY_EMEM, error); |
| 89 | m->node = node; |
| 90 | |
| 91 | /* add must local modules */ |
| 92 | LY_ARRAY_CREATE_GOTO(ctx->ctx, m->local_mods, LY_ARRAY_COUNT(pmusts), ret, error); |
| 93 | LY_ARRAY_FOR(pmusts, u) { |
| 94 | m->local_mods[u] = pmusts[u].arg.mod; |
| 95 | LY_ARRAY_INCREMENT(m->local_mods); |
| 96 | } |
| 97 | |
| 98 | LY_CHECK_ERR_GOTO(ly_set_add(&ctx->unres->musts, m, 1, NULL), ret = LY_EMEM, error); |
| 99 | |
| 100 | return LY_SUCCESS; |
| 101 | |
| 102 | error: |
| 103 | if (m) { |
| 104 | LY_ARRAY_FREE(m->local_mods); |
| 105 | free(m); |
| 106 | } |
| 107 | LOGMEM(ctx->ctx); |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | static LY_ERR |
| 112 | lysc_unres_leafref_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const struct lysp_module *local_mod) |
| 113 | { |
| 114 | struct lysc_unres_leafref *l = NULL; |
| 115 | struct ly_set *leafrefs_set; |
| 116 | LY_ARRAY_COUNT_TYPE u; |
| 117 | int is_lref = 0; |
| 118 | |
| 119 | if (ctx->compile_opts & LYS_COMPILE_GROUPING) { |
| 120 | /* do not check leafrefs in groupings */ |
| 121 | return LY_SUCCESS; |
| 122 | } |
| 123 | |
| 124 | /* use special set for disabled leafrefs */ |
| 125 | leafrefs_set = ctx->compile_opts & LYS_COMPILE_DISABLED ? &ctx->unres->disabled_leafrefs : &ctx->unres->leafrefs; |
| 126 | |
| 127 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 128 | /* leafref */ |
| 129 | is_lref = 1; |
| 130 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 131 | /* union with leafrefs */ |
| 132 | LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) { |
| 133 | if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 134 | is_lref = 1; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (is_lref) { |
| 141 | /* add new unresolved leafref node */ |
| 142 | l = calloc(1, sizeof *l); |
| 143 | LY_CHECK_ERR_RET(!l, LOGMEM(ctx->ctx), LY_EMEM); |
| 144 | |
| 145 | l->node = &leaf->node; |
| 146 | l->local_mod = local_mod; |
| 147 | |
| 148 | LY_CHECK_ERR_RET(ly_set_add(leafrefs_set, l, 1, NULL), free(l); LOGMEM(ctx->ctx), LY_EMEM); |
| 149 | } |
| 150 | |
| 151 | return LY_SUCCESS; |
| 152 | } |
| 153 | |
| 154 | /** |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 155 | * @brief Add/replace a leaf default value in unres. |
| 156 | * Can also be used for a single leaf-list default value. |
| 157 | * |
| 158 | * @param[in] ctx Compile context. |
| 159 | * @param[in] leaf Leaf with the default value. |
| 160 | * @param[in] dflt Default value to use. |
| 161 | * @return LY_ERR value. |
| 162 | */ |
| 163 | static LY_ERR |
| 164 | lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt) |
| 165 | { |
| 166 | struct lysc_unres_dflt *r = NULL; |
| 167 | uint32_t i; |
| 168 | |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 169 | if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 170 | return LY_SUCCESS; |
| 171 | } |
| 172 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 173 | for (i = 0; i < ctx->unres->dflts.count; ++i) { |
| 174 | if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 175 | /* just replace the default */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 176 | r = ctx->unres->dflts.objs[i]; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 177 | lysp_qname_free(ctx->ctx, r->dflt); |
| 178 | free(r->dflt); |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | if (!r) { |
| 183 | /* add new unres item */ |
| 184 | r = calloc(1, sizeof *r); |
| 185 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 186 | r->leaf = leaf; |
| 187 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 188 | LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | r->dflt = malloc(sizeof *r->dflt); |
| 192 | LY_CHECK_GOTO(!r->dflt, error); |
| 193 | LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error); |
| 194 | |
| 195 | return LY_SUCCESS; |
| 196 | |
| 197 | error: |
| 198 | free(r->dflt); |
| 199 | LOGMEM(ctx->ctx); |
| 200 | return LY_EMEM; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * @brief Add/replace a leaf-list default value(s) in unres. |
| 205 | * |
| 206 | * @param[in] ctx Compile context. |
| 207 | * @param[in] llist Leaf-list with the default value. |
| 208 | * @param[in] dflts Sized array of the default values. |
| 209 | * @return LY_ERR value. |
| 210 | */ |
| 211 | static LY_ERR |
| 212 | lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts) |
| 213 | { |
| 214 | struct lysc_unres_dflt *r = NULL; |
| 215 | uint32_t i; |
| 216 | |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 217 | if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 218 | return LY_SUCCESS; |
| 219 | } |
| 220 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 221 | for (i = 0; i < ctx->unres->dflts.count; ++i) { |
| 222 | if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 223 | /* just replace the defaults */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 224 | r = ctx->unres->dflts.objs[i]; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 225 | lysp_qname_free(ctx->ctx, r->dflt); |
| 226 | free(r->dflt); |
| 227 | r->dflt = NULL; |
| 228 | FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free); |
| 229 | r->dflts = NULL; |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | if (!r) { |
| 234 | r = calloc(1, sizeof *r); |
| 235 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 236 | r->llist = llist; |
| 237 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 238 | LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup); |
| 242 | |
| 243 | return LY_SUCCESS; |
| 244 | } |
| 245 | |
| 246 | /** |
Michal Vasko | f4fa90d | 2021-11-11 15:05:19 +0100 | [diff] [blame] | 247 | * @brief Add a bits/enumeration type to unres. |
| 248 | * |
| 249 | * @param[in] ctx Compile context. |
| 250 | * @param[in] leaf Leaf of type bits/enumeration whose disabled items to free. |
| 251 | * @return LY_ERR value. |
| 252 | */ |
| 253 | static LY_ERR |
| 254 | lysc_unres_bitenum_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf) |
| 255 | { |
| 256 | if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) { |
| 257 | /* skip groupings and redundant for disabled nodes */ |
| 258 | return LY_SUCCESS; |
| 259 | } |
| 260 | |
| 261 | LY_CHECK_RET(ly_set_add(&ctx->unres->disabled_bitenums, leaf, 1, NULL)); |
| 262 | |
| 263 | return LY_SUCCESS; |
| 264 | } |
| 265 | |
| 266 | /** |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 267 | * @brief Duplicate the compiled pattern structure. |
| 268 | * |
| 269 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 270 | * |
| 271 | * @param[in] orig The pattern structure to duplicate. |
| 272 | * @return The duplicated structure to use. |
| 273 | */ |
| 274 | static struct lysc_pattern * |
| 275 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 276 | { |
| 277 | ++orig->refcount; |
| 278 | return orig; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * @brief Duplicate the array of compiled patterns. |
| 283 | * |
| 284 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 285 | * |
| 286 | * @param[in] ctx Libyang context for logging. |
| 287 | * @param[in] orig The patterns sized array to duplicate. |
| 288 | * @return New sized array as a copy of @p orig. |
| 289 | * @return NULL in case of memory allocation error. |
| 290 | */ |
| 291 | static struct lysc_pattern ** |
| 292 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 293 | { |
| 294 | struct lysc_pattern **dup = NULL; |
| 295 | LY_ARRAY_COUNT_TYPE u; |
| 296 | |
| 297 | assert(orig); |
| 298 | |
| 299 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL); |
| 300 | LY_ARRAY_FOR(orig, u) { |
| 301 | dup[u] = lysc_pattern_dup(orig[u]); |
| 302 | LY_ARRAY_INCREMENT(dup); |
| 303 | } |
| 304 | return dup; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * @brief Duplicate compiled range structure. |
| 309 | * |
| 310 | * @param[in] ctx Libyang context for logging. |
| 311 | * @param[in] orig The range structure to be duplicated. |
| 312 | * @return New compiled range structure as a copy of @p orig. |
| 313 | * @return NULL in case of memory allocation error. |
| 314 | */ |
| 315 | static struct lysc_range * |
| 316 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 317 | { |
| 318 | struct lysc_range *dup; |
| 319 | LY_ERR ret; |
| 320 | |
| 321 | assert(orig); |
| 322 | |
| 323 | dup = calloc(1, sizeof *dup); |
| 324 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 325 | if (orig->parts) { |
| 326 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup); |
Michal Vasko | 79135ae | 2020-12-16 10:08:35 +0100 | [diff] [blame] | 327 | (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 328 | memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts); |
| 329 | } |
| 330 | DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup); |
| 331 | DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup); |
| 332 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 333 | |
| 334 | return dup; |
| 335 | cleanup: |
| 336 | free(dup); |
| 337 | (void) ret; /* set but not used due to the return type */ |
| 338 | return NULL; |
| 339 | } |
| 340 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 341 | /** |
Michal Vasko | dfd254d | 2021-06-24 09:24:59 +0200 | [diff] [blame] | 342 | * @brief Compile status information of the given statement. |
| 343 | * |
| 344 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 345 | * has the status correctly set during the compilation. |
| 346 | * |
| 347 | * @param[in] ctx Compile context |
| 348 | * @param[in,out] stmt_flags Compiled flags to update. If the status was set explicitly, it is already set |
| 349 | * in the flags value and we just check the compatibility with the parent's status value. |
| 350 | * @param[in] stmt_name Statement name, for logging. |
| 351 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 352 | * @param[in] parent_name Name of the parent node, for logging. |
| 353 | * @param[in] inherit_log Whether to print inherit message. |
| 354 | * @return LY_ERR value. |
| 355 | */ |
| 356 | static LY_ERR |
| 357 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *stmt_flags, const char *stmt_name, uint16_t parent_flags, |
| 358 | const char *parent_name, ly_bool inherit_log) |
| 359 | { |
| 360 | /* status - it is not inherited by specification, but it does not make sense to have |
| 361 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 362 | if (!(*stmt_flags & LYS_STATUS_MASK)) { |
| 363 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 364 | if (inherit_log) { |
| 365 | LOGVRB("Missing explicit \"%s\" status specified in parent \"%s\", inheriting for \"%s\".", |
| 366 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete", parent_name, stmt_name); |
| 367 | } |
| 368 | *stmt_flags |= parent_flags & LYS_STATUS_MASK; |
| 369 | } else { |
| 370 | *stmt_flags |= LYS_STATUS_CURR; |
| 371 | } |
| 372 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 373 | /* check status compatibility with the parent */ |
| 374 | if ((parent_flags & LYS_STATUS_MASK) > (*stmt_flags & LYS_STATUS_MASK)) { |
| 375 | if (*stmt_flags & LYS_STATUS_CURR) { |
| 376 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
| 377 | "Status \"current\" of \"%s\" is in conflict with the \"%s\" status of parent \"%s\".", |
| 378 | stmt_name, (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete", parent_name); |
| 379 | } else { /* LYS_STATUS_DEPRC */ |
| 380 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
| 381 | "Status \"deprecated\" of \"%s\" is in conflict with the \"obsolete\" status of parent \"%s\".", |
| 382 | stmt_name, parent_name); |
| 383 | } |
| 384 | return LY_EVALID; |
| 385 | } |
| 386 | } |
| 387 | return LY_SUCCESS; |
| 388 | } |
| 389 | |
| 390 | /** |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 391 | * @brief Compile information from the when statement |
| 392 | * |
| 393 | * @param[in] ctx Compile context. |
| 394 | * @param[in] when_p Parsed when structure. |
Michal Vasko | dfd254d | 2021-06-24 09:24:59 +0200 | [diff] [blame] | 395 | * @param[in] parent_flags Flags of the parsed node with the when statement. |
| 396 | * @param[in] compiled_parent Closest compiled parent of the when statement. |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 397 | * @param[in] ctx_node Context node for the when statement. |
| 398 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 399 | * @return LY_ERR value. |
| 400 | */ |
| 401 | static LY_ERR |
Michal Vasko | dfd254d | 2021-06-24 09:24:59 +0200 | [diff] [blame] | 402 | lys_compile_when_(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t parent_flags, |
| 403 | const struct lysc_node *compiled_parent, const struct lysc_node *ctx_node, struct lysc_when **when) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 404 | { |
| 405 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 406 | LY_VALUE_FORMAT format; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 407 | |
| 408 | *when = calloc(1, sizeof **when); |
| 409 | LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM); |
| 410 | (*when)->refcount = 1; |
| 411 | LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond)); |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 412 | LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond), |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 413 | LY_VALUE_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 414 | (*when)->context = (struct lysc_node *)ctx_node; |
| 415 | DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done); |
| 416 | DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done); |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 417 | COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done); |
Michal Vasko | dfd254d | 2021-06-24 09:24:59 +0200 | [diff] [blame] | 418 | (*when)->flags = (parent_flags & LYS_STATUS_MASK); |
| 419 | if (compiled_parent) { |
| 420 | LY_CHECK_RET(lys_compile_status(ctx, &(*when)->flags, "when", compiled_parent->flags, compiled_parent->name, 0)); |
| 421 | } else { |
| 422 | LY_CHECK_RET(lys_compile_status(ctx, &(*when)->flags, "when", 0, NULL, 0)); |
| 423 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 424 | |
| 425 | done: |
| 426 | return ret; |
| 427 | } |
| 428 | |
| 429 | LY_ERR |
Michal Vasko | dfd254d | 2021-06-24 09:24:59 +0200 | [diff] [blame] | 430 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t parent_flags, const struct lysc_node *compiled_parent, |
| 431 | const struct lysc_node *ctx_node, struct lysc_node *node, struct lysc_when **when_c) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 432 | { |
| 433 | struct lysc_when **new_when, ***node_when; |
| 434 | |
| 435 | assert(when_p); |
| 436 | |
| 437 | /* get the when array */ |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 438 | node_when = lysc_node_when_p(node); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 439 | |
| 440 | /* create new when pointer */ |
| 441 | LY_ARRAY_NEW_RET(ctx->ctx, *node_when, new_when, LY_EMEM); |
| 442 | if (!when_c || !(*when_c)) { |
| 443 | /* compile when */ |
Michal Vasko | dfd254d | 2021-06-24 09:24:59 +0200 | [diff] [blame] | 444 | LY_CHECK_RET(lys_compile_when_(ctx, when_p, parent_flags, compiled_parent, ctx_node, new_when)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 445 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 446 | /* remember the compiled when for sharing */ |
| 447 | if (when_c) { |
| 448 | *when_c = *new_when; |
| 449 | } |
| 450 | } else { |
| 451 | /* use the previously compiled when */ |
| 452 | ++(*when_c)->refcount; |
| 453 | *new_when = *when_c; |
Michal Vasko | f01fd0b | 2020-11-23 16:53:07 +0100 | [diff] [blame] | 454 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 455 | |
Michal Vasko | 6f08e96 | 2021-07-23 08:30:47 +0200 | [diff] [blame] | 456 | if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) { |
Michal Vasko | f01fd0b | 2020-11-23 16:53:07 +0100 | [diff] [blame] | 457 | /* do not check "when" semantics in a grouping, but repeat the check for different node because |
| 458 | * of dummy node check */ |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 459 | LY_CHECK_RET(ly_set_add(&ctx->unres->whens, node, 0, NULL)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | return LY_SUCCESS; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * @brief Compile information from the must statement |
| 467 | * @param[in] ctx Compile context. |
| 468 | * @param[in] must_p The parsed must statement structure. |
| 469 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 470 | * @return LY_ERR value. |
| 471 | */ |
| 472 | static LY_ERR |
| 473 | lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must) |
| 474 | { |
| 475 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 476 | LY_VALUE_FORMAT format; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 477 | |
| 478 | LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond)); |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 479 | LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, must_p->arg.str, strlen(must_p->arg.str), |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 480 | LY_VALUE_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 481 | DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done); |
| 482 | DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done); |
| 483 | DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done); |
| 484 | DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done); |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 485 | COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 486 | |
| 487 | done: |
| 488 | return ret; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * @brief Validate and normalize numeric value from a range definition. |
| 493 | * @param[in] ctx Compile context. |
| 494 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 495 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 496 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 497 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 498 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 499 | * @param[in] value String value of the range boundary. |
| 500 | * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary. |
| 501 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 502 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 503 | */ |
| 504 | static LY_ERR |
| 505 | range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, |
| 506 | size_t *len, char **valcopy) |
| 507 | { |
| 508 | size_t fraction = 0, size; |
| 509 | |
| 510 | *len = 0; |
| 511 | |
| 512 | assert(value); |
| 513 | /* parse value */ |
| 514 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 515 | return LY_EVALID; |
| 516 | } |
| 517 | |
| 518 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 519 | ++(*len); |
| 520 | } |
| 521 | |
| 522 | while (isdigit(value[*len])) { |
| 523 | ++(*len); |
| 524 | } |
| 525 | |
| 526 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
| 527 | if (basetype == LY_TYPE_DEC64) { |
| 528 | goto decimal; |
| 529 | } else { |
| 530 | *valcopy = strndup(value, *len); |
| 531 | return LY_SUCCESS; |
| 532 | } |
| 533 | } |
| 534 | fraction = *len; |
| 535 | |
| 536 | ++(*len); |
| 537 | while (isdigit(value[*len])) { |
| 538 | ++(*len); |
| 539 | } |
| 540 | |
| 541 | if (basetype == LY_TYPE_DEC64) { |
| 542 | decimal: |
| 543 | assert(frdigits); |
| 544 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 545 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 546 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 547 | (int)(*len), value, frdigits); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 548 | return LY_EINVAL; |
| 549 | } |
| 550 | if (fraction) { |
| 551 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 552 | } else { |
| 553 | size = (*len) + frdigits + 1; |
| 554 | } |
| 555 | *valcopy = malloc(size * sizeof **valcopy); |
| 556 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 557 | |
| 558 | (*valcopy)[size - 1] = '\0'; |
| 559 | if (fraction) { |
| 560 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 561 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 562 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 563 | } else { |
| 564 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 565 | memset(&(*valcopy)[*len], '0', frdigits); |
| 566 | } |
| 567 | } |
| 568 | return LY_SUCCESS; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * @brief Check that values in range are in ascendant order. |
| 573 | * @param[in] unsigned_value Flag to note that we are working with unsigned values. |
| 574 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 575 | * max can be also equal. |
| 576 | * @param[in] value Current value to check. |
| 577 | * @param[in] prev_value The last seen value. |
| 578 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 579 | */ |
| 580 | static LY_ERR |
| 581 | range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value) |
| 582 | { |
| 583 | if (unsigned_value) { |
| 584 | if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) { |
| 585 | return LY_EEXIST; |
| 586 | } |
| 587 | } else { |
| 588 | if ((max && (prev_value > value)) || (!max && (prev_value >= value))) { |
| 589 | return LY_EEXIST; |
| 590 | } |
| 591 | } |
| 592 | return LY_SUCCESS; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * @brief Set min/max value of the range part. |
| 597 | * @param[in] ctx Compile context. |
| 598 | * @param[in] part Range part structure to fill. |
| 599 | * @param[in] max Flag to distinguish if storing min or max value. |
| 600 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 601 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 602 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 603 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 604 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 605 | * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values. |
| 606 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 607 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 608 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 609 | * frdigits value), LY_EMEM. |
| 610 | */ |
| 611 | static LY_ERR |
| 612 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype, |
| 613 | ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value) |
| 614 | { |
| 615 | LY_ERR ret = LY_SUCCESS; |
| 616 | char *valcopy = NULL; |
Radek Krejci | 2b18bf1 | 2020-11-06 11:20:20 +0100 | [diff] [blame] | 617 | size_t len = 0; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 618 | |
| 619 | if (value) { |
| 620 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
| 621 | LY_CHECK_GOTO(ret, finalize); |
| 622 | } |
| 623 | if (!valcopy && base_range) { |
| 624 | if (max) { |
| 625 | part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64; |
| 626 | } else { |
| 627 | part->min_64 = base_range->parts[0].min_64; |
| 628 | } |
| 629 | if (!first) { |
| 630 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 631 | } |
| 632 | goto finalize; |
| 633 | } |
| 634 | |
| 635 | switch (basetype) { |
| 636 | case LY_TYPE_INT8: /* range */ |
| 637 | if (valcopy) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 638 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), LY_BASE_DEC, max ? &part->max_64 : &part->min_64); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 639 | } else if (max) { |
| 640 | part->max_64 = INT64_C(127); |
| 641 | } else { |
| 642 | part->min_64 = INT64_C(-128); |
| 643 | } |
| 644 | if (!ret && !first) { |
| 645 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
| 646 | } |
| 647 | break; |
| 648 | case LY_TYPE_INT16: /* range */ |
| 649 | if (valcopy) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 650 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC, |
| 651 | max ? &part->max_64 : &part->min_64); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 652 | } else if (max) { |
| 653 | part->max_64 = INT64_C(32767); |
| 654 | } else { |
| 655 | part->min_64 = INT64_C(-32768); |
| 656 | } |
| 657 | if (!ret && !first) { |
| 658 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
| 659 | } |
| 660 | break; |
| 661 | case LY_TYPE_INT32: /* range */ |
| 662 | if (valcopy) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 663 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC, |
| 664 | max ? &part->max_64 : &part->min_64); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 665 | } else if (max) { |
| 666 | part->max_64 = INT64_C(2147483647); |
| 667 | } else { |
| 668 | part->min_64 = INT64_C(-2147483648); |
| 669 | } |
| 670 | if (!ret && !first) { |
| 671 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
| 672 | } |
| 673 | break; |
| 674 | case LY_TYPE_INT64: /* range */ |
| 675 | case LY_TYPE_DEC64: /* range */ |
| 676 | if (valcopy) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 677 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), |
| 678 | LY_BASE_DEC, max ? &part->max_64 : &part->min_64); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 679 | } else if (max) { |
| 680 | part->max_64 = INT64_C(9223372036854775807); |
| 681 | } else { |
| 682 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 683 | } |
| 684 | if (!ret && !first) { |
| 685 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
| 686 | } |
| 687 | break; |
| 688 | case LY_TYPE_UINT8: /* range */ |
| 689 | if (valcopy) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 690 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 691 | } else if (max) { |
| 692 | part->max_u64 = UINT64_C(255); |
| 693 | } else { |
| 694 | part->min_u64 = UINT64_C(0); |
| 695 | } |
| 696 | if (!ret && !first) { |
| 697 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
| 698 | } |
| 699 | break; |
| 700 | case LY_TYPE_UINT16: /* range */ |
| 701 | if (valcopy) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 702 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 703 | } else if (max) { |
| 704 | part->max_u64 = UINT64_C(65535); |
| 705 | } else { |
| 706 | part->min_u64 = UINT64_C(0); |
| 707 | } |
| 708 | if (!ret && !first) { |
| 709 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
| 710 | } |
| 711 | break; |
| 712 | case LY_TYPE_UINT32: /* range */ |
| 713 | if (valcopy) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 714 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC, |
| 715 | max ? &part->max_u64 : &part->min_u64); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 716 | } else if (max) { |
| 717 | part->max_u64 = UINT64_C(4294967295); |
| 718 | } else { |
| 719 | part->min_u64 = UINT64_C(0); |
| 720 | } |
| 721 | if (!ret && !first) { |
| 722 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
| 723 | } |
| 724 | break; |
| 725 | case LY_TYPE_UINT64: /* range */ |
| 726 | case LY_TYPE_STRING: /* length */ |
| 727 | case LY_TYPE_BINARY: /* length */ |
| 728 | if (valcopy) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 729 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC, |
| 730 | max ? &part->max_u64 : &part->min_u64); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 731 | } else if (max) { |
| 732 | part->max_u64 = UINT64_C(18446744073709551615); |
| 733 | } else { |
| 734 | part->min_u64 = UINT64_C(0); |
| 735 | } |
| 736 | if (!ret && !first) { |
| 737 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
| 738 | } |
| 739 | break; |
| 740 | default: |
| 741 | LOGINT(ctx->ctx); |
| 742 | ret = LY_EINT; |
| 743 | } |
| 744 | |
| 745 | finalize: |
| 746 | if (ret == LY_EDENIED) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 747 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 748 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 749 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 750 | } else if (ret == LY_EVALID) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 751 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 752 | "Invalid %s restriction - invalid value \"%s\".", |
| 753 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 754 | } else if (ret == LY_EEXIST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 755 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 756 | "Invalid %s restriction - values are not in ascending order (%s).", |
| 757 | length_restr ? "length" : "range", |
| 758 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
| 759 | } else if (!ret && value) { |
| 760 | *value = *value + len; |
| 761 | } |
| 762 | free(valcopy); |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * @brief Compile the parsed range restriction. |
| 768 | * @param[in] ctx Compile context. |
| 769 | * @param[in] range_p Parsed range structure to compile. |
| 770 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 771 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 772 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 773 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 774 | * range restriction must be more restrictive than the base_range. |
| 775 | * @param[in,out] range Pointer to the created current range structure. |
| 776 | * @return LY_ERR value. |
| 777 | */ |
| 778 | static LY_ERR |
| 779 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr, |
| 780 | uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range) |
| 781 | { |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 782 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 783 | const char *expr; |
| 784 | struct lysc_range_part *parts = NULL, *part; |
| 785 | ly_bool range_expected = 0, uns; |
| 786 | LY_ARRAY_COUNT_TYPE parts_done = 0, u, v; |
| 787 | |
| 788 | assert(range); |
| 789 | assert(range_p); |
| 790 | |
| 791 | expr = range_p->arg.str; |
| 792 | while (1) { |
| 793 | if (isspace(*expr)) { |
| 794 | ++expr; |
| 795 | } else if (*expr == '\0') { |
| 796 | if (range_expected) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 797 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 798 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
Michal Vasko | 20c0b9f | 2021-08-24 08:16:27 +0200 | [diff] [blame] | 799 | length_restr ? "length" : "range", range_p->arg.str); |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 800 | ret = LY_EVALID; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 801 | goto cleanup; |
| 802 | } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 803 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 804 | "Invalid %s restriction - unexpected end of the expression (%s).", |
Michal Vasko | 20c0b9f | 2021-08-24 08:16:27 +0200 | [diff] [blame] | 805 | length_restr ? "length" : "range", range_p->arg.str); |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 806 | ret = LY_EVALID; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 807 | goto cleanup; |
| 808 | } |
| 809 | parts_done++; |
| 810 | break; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 811 | } else if (!strncmp(expr, "min", ly_strlen_const("min"))) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 812 | if (parts) { |
| 813 | /* min cannot be used elsewhere than in the first part */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 814 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 815 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
Radek Krejci | 5240920 | 2021-03-15 09:30:43 +0100 | [diff] [blame] | 816 | (int)(expr - range_p->arg.str), range_p->arg.str); |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 817 | ret = LY_EVALID; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 818 | goto cleanup; |
| 819 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 820 | expr += ly_strlen_const("min"); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 821 | |
| 822 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 823 | LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 824 | part->max_64 = part->min_64; |
| 825 | } else if (*expr == '|') { |
| 826 | if (!parts || range_expected) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 827 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 828 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 829 | ret = LY_EVALID; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 830 | goto cleanup; |
| 831 | } |
| 832 | expr++; |
| 833 | parts_done++; |
| 834 | /* process next part of the expression */ |
| 835 | } else if (!strncmp(expr, "..", 2)) { |
| 836 | expr += 2; |
| 837 | while (isspace(*expr)) { |
| 838 | expr++; |
| 839 | } |
| 840 | if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 841 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 842 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 843 | ret = LY_EVALID; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 844 | goto cleanup; |
| 845 | } |
| 846 | /* continue expecting the upper boundary */ |
| 847 | range_expected = 1; |
| 848 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 849 | /* number */ |
| 850 | if (range_expected) { |
| 851 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 852 | LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 853 | range_expected = 0; |
| 854 | } else { |
| 855 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 856 | LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 857 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
| 858 | part->max_64 = part->min_64; |
| 859 | } |
| 860 | |
| 861 | /* continue with possible another expression part */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 862 | } else if (!strncmp(expr, "max", ly_strlen_const("max"))) { |
| 863 | expr += ly_strlen_const("max"); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 864 | while (isspace(*expr)) { |
| 865 | expr++; |
| 866 | } |
| 867 | if (*expr != '\0') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 868 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 869 | length_restr ? "length" : "range", expr); |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 870 | ret = LY_EVALID; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 871 | goto cleanup; |
| 872 | } |
| 873 | if (range_expected) { |
| 874 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 875 | LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 876 | range_expected = 0; |
| 877 | } else { |
| 878 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 879 | LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 880 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
| 881 | part->min_64 = part->max_64; |
| 882 | } |
| 883 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 884 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 885 | length_restr ? "length" : "range", expr); |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 886 | ret = LY_EVALID; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 887 | goto cleanup; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | /* check with the previous range/length restriction */ |
| 892 | if (base_range) { |
| 893 | switch (basetype) { |
| 894 | case LY_TYPE_BINARY: |
| 895 | case LY_TYPE_UINT8: |
| 896 | case LY_TYPE_UINT16: |
| 897 | case LY_TYPE_UINT32: |
| 898 | case LY_TYPE_UINT64: |
| 899 | case LY_TYPE_STRING: |
| 900 | uns = 1; |
| 901 | break; |
| 902 | case LY_TYPE_DEC64: |
| 903 | case LY_TYPE_INT8: |
| 904 | case LY_TYPE_INT16: |
| 905 | case LY_TYPE_INT32: |
| 906 | case LY_TYPE_INT64: |
| 907 | uns = 0; |
| 908 | break; |
| 909 | default: |
| 910 | LOGINT(ctx->ctx); |
| 911 | ret = LY_EINT; |
| 912 | goto cleanup; |
| 913 | } |
| 914 | for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) { |
| 915 | if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) { |
| 916 | goto baseerror; |
| 917 | } |
| 918 | /* current lower bound is not lower than the base */ |
| 919 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 920 | /* base has single value */ |
| 921 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 922 | /* both lower bounds are the same */ |
| 923 | if (parts[u].min_64 != parts[u].max_64) { |
| 924 | /* current continues with a range */ |
| 925 | goto baseerror; |
| 926 | } else { |
| 927 | /* equal single values, move both forward */ |
| 928 | ++v; |
| 929 | continue; |
| 930 | } |
| 931 | } else { |
| 932 | /* base is single value lower than current range, so the |
| 933 | * value from base range is removed in the current, |
| 934 | * move only base and repeat checking */ |
| 935 | ++v; |
| 936 | --u; |
| 937 | continue; |
| 938 | } |
| 939 | } else { |
| 940 | /* base is the range */ |
| 941 | if (parts[u].min_64 == parts[u].max_64) { |
| 942 | /* current is a single value */ |
| 943 | if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) { |
| 944 | /* current is behind the base range, so base range is omitted, |
| 945 | * move the base and keep the current for further check */ |
| 946 | ++v; |
| 947 | --u; |
| 948 | } /* else it is within the base range, so move the current, but keep the base */ |
| 949 | continue; |
| 950 | } else { |
| 951 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 952 | if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) { |
| 953 | /* higher bound is higher than the current higher bound */ |
| 954 | if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) { |
| 955 | /* but the current lower bound is also higher, so the base range is omitted, |
| 956 | * continue with the same current, but move the base */ |
| 957 | --u; |
| 958 | ++v; |
| 959 | continue; |
| 960 | } |
| 961 | /* current range starts within the base range but end behind it */ |
| 962 | goto baseerror; |
| 963 | } else { |
| 964 | /* current range is smaller than the base, |
| 965 | * move current, but stay with the base */ |
| 966 | continue; |
| 967 | } |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | if (u != parts_done) { |
| 972 | baseerror: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 973 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 974 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
Michal Vasko | 20c0b9f | 2021-08-24 08:16:27 +0200 | [diff] [blame] | 975 | length_restr ? "length" : "range", range_p->arg.str); |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 976 | ret = LY_EVALID; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 977 | goto cleanup; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | if (!(*range)) { |
| 982 | *range = calloc(1, sizeof **range); |
| 983 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 984 | } |
| 985 | |
| 986 | /* we rewrite the following values as the types chain is being processed */ |
| 987 | if (range_p->eapptag) { |
| 988 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 989 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup); |
| 990 | } |
| 991 | if (range_p->emsg) { |
| 992 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 993 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup); |
| 994 | } |
| 995 | if (range_p->dsc) { |
| 996 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 997 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup); |
| 998 | } |
| 999 | if (range_p->ref) { |
| 1000 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1001 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup); |
| 1002 | } |
| 1003 | /* extensions are taken only from the last range by the caller */ |
| 1004 | |
| 1005 | (*range)->parts = parts; |
| 1006 | parts = NULL; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1007 | cleanup: |
| 1008 | LY_ARRAY_FREE(parts); |
| 1009 | |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
| 1013 | LY_ERR |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1014 | lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1015 | { |
| 1016 | size_t idx, idx2, start, end, size, brack; |
| 1017 | char *perl_regex, *ptr; |
Christian Hopps | dafed22 | 2021-03-22 13:02:42 -0400 | [diff] [blame] | 1018 | int err_code, compile_opts; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1019 | const char *orig_ptr; |
| 1020 | PCRE2_SIZE err_offset; |
| 1021 | pcre2_code *code_local; |
| 1022 | |
| 1023 | #define URANGE_LEN 19 |
| 1024 | char *ublock2urange[][2] = { |
| 1025 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1026 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1027 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1028 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1029 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1030 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1031 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1032 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1033 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1034 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1035 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1036 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1037 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1038 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1039 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1040 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1041 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1042 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1043 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1044 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1045 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1046 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1047 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1048 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1049 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1050 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1051 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1052 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1053 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1054 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1055 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1056 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1057 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1058 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1059 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1060 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1061 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1062 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1063 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1064 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1065 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1066 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1067 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1068 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1069 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1070 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1071 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1072 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1073 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1074 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1075 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1076 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1077 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1078 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1079 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1080 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1081 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1082 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1083 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1084 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1085 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1086 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1087 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1088 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1089 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1090 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1091 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1092 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1093 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1094 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1095 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1096 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1097 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1098 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1099 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1100 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1101 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1102 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1103 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1104 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1105 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1106 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1107 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
Michal Vasko | 2b71ab5 | 2020-12-01 16:44:11 +0100 | [diff] [blame] | 1108 | {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"}, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1109 | {NULL, NULL} |
| 1110 | }; |
| 1111 | |
| 1112 | /* adjust the expression to a Perl equivalent |
| 1113 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1114 | |
| 1115 | /* allocate space for the transformed pattern */ |
| 1116 | size = strlen(pattern) + 1; |
Christian Hopps | dafed22 | 2021-03-22 13:02:42 -0400 | [diff] [blame] | 1117 | compile_opts = PCRE2_UTF | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE; |
| 1118 | #ifdef PCRE2_ENDANCHORED |
| 1119 | compile_opts |= PCRE2_ENDANCHORED; |
| 1120 | #else |
| 1121 | /* add space for trailing $ anchor */ |
| 1122 | size++; |
| 1123 | #endif |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1124 | perl_regex = malloc(size); |
| 1125 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
| 1126 | perl_regex[0] = '\0'; |
| 1127 | |
| 1128 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 1129 | brack = 0; |
| 1130 | idx = 0; |
| 1131 | orig_ptr = pattern; |
| 1132 | while (orig_ptr[0]) { |
| 1133 | switch (orig_ptr[0]) { |
| 1134 | case '$': |
| 1135 | case '^': |
| 1136 | if (!brack) { |
| 1137 | /* make space for the extra character */ |
| 1138 | ++size; |
| 1139 | perl_regex = ly_realloc(perl_regex, size); |
| 1140 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
| 1141 | |
| 1142 | /* print escape slash */ |
| 1143 | perl_regex[idx] = '\\'; |
| 1144 | ++idx; |
| 1145 | } |
| 1146 | break; |
| 1147 | case '[': |
| 1148 | /* must not be escaped */ |
| 1149 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 1150 | ++brack; |
| 1151 | } |
| 1152 | break; |
| 1153 | case ']': |
| 1154 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 1155 | /* pattern was checked and compiled already */ |
| 1156 | assert(brack); |
| 1157 | --brack; |
| 1158 | } |
| 1159 | break; |
| 1160 | default: |
| 1161 | break; |
| 1162 | } |
| 1163 | |
| 1164 | /* copy char */ |
| 1165 | perl_regex[idx] = orig_ptr[0]; |
| 1166 | |
| 1167 | ++idx; |
| 1168 | ++orig_ptr; |
| 1169 | } |
Christian Hopps | dafed22 | 2021-03-22 13:02:42 -0400 | [diff] [blame] | 1170 | #ifndef PCRE2_ENDANCHORED |
| 1171 | /* anchor match to end of subject */ |
| 1172 | perl_regex[idx++] = '$'; |
| 1173 | #endif |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1174 | perl_regex[idx] = '\0'; |
| 1175 | |
| 1176 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1177 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1178 | start = ptr - perl_regex; |
| 1179 | |
| 1180 | ptr = strchr(ptr, '}'); |
| 1181 | if (!ptr) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1182 | LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property"); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1183 | free(perl_regex); |
| 1184 | return LY_EVALID; |
| 1185 | } |
| 1186 | end = (ptr - perl_regex) + 1; |
| 1187 | |
| 1188 | /* need more space */ |
| 1189 | if (end - start < URANGE_LEN) { |
| 1190 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
| 1191 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM); |
| 1192 | } |
| 1193 | |
| 1194 | /* find our range */ |
| 1195 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1196 | if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"), |
| 1197 | ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1198 | break; |
| 1199 | } |
| 1200 | } |
| 1201 | if (!ublock2urange[idx][0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1202 | LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name"); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1203 | free(perl_regex); |
| 1204 | return LY_EVALID; |
| 1205 | } |
| 1206 | |
| 1207 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 1208 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
| 1209 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1210 | ++idx; |
| 1211 | } |
| 1212 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1213 | --idx; |
| 1214 | } |
| 1215 | } |
| 1216 | if (idx) { |
| 1217 | /* skip brackets */ |
| 1218 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 1219 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 1220 | } else { |
| 1221 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 1222 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | /* must return 0, already checked during parsing */ |
Christian Hopps | dafed22 | 2021-03-22 13:02:42 -0400 | [diff] [blame] | 1227 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1228 | &err_code, &err_offset, NULL); |
| 1229 | if (!code_local) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1230 | PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0}; |
| 1231 | pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1232 | LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1233 | free(perl_regex); |
| 1234 | return LY_EVALID; |
| 1235 | } |
| 1236 | free(perl_regex); |
| 1237 | |
| 1238 | if (code) { |
| 1239 | *code = code_local; |
| 1240 | } else { |
| 1241 | free(code_local); |
| 1242 | } |
| 1243 | |
| 1244 | return LY_SUCCESS; |
| 1245 | |
| 1246 | #undef URANGE_LEN |
| 1247 | } |
| 1248 | |
| 1249 | /** |
| 1250 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 1251 | * @param[in] ctx Compile context. |
| 1252 | * @param[in] patterns_p Array of parsed patterns from the current type to compile. |
| 1253 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 1254 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 1255 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 1256 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 1257 | */ |
| 1258 | static LY_ERR |
| 1259 | lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, |
| 1260 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 1261 | { |
| 1262 | struct lysc_pattern **pattern; |
| 1263 | LY_ARRAY_COUNT_TYPE u; |
| 1264 | LY_ERR ret = LY_SUCCESS; |
| 1265 | |
| 1266 | /* first, copy the patterns from the base type */ |
| 1267 | if (base_patterns) { |
| 1268 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 1269 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 1270 | } |
| 1271 | |
| 1272 | LY_ARRAY_FOR(patterns_p, u) { |
| 1273 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 1274 | *pattern = calloc(1, sizeof **pattern); |
| 1275 | ++(*pattern)->refcount; |
| 1276 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1277 | ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1278 | LY_CHECK_RET(ret); |
| 1279 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1280 | if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1281 | (*pattern)->inverted = 1; |
| 1282 | } |
| 1283 | DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done); |
| 1284 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done); |
| 1285 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done); |
| 1286 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done); |
| 1287 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done); |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 1288 | COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1289 | } |
| 1290 | done: |
| 1291 | return ret; |
| 1292 | } |
| 1293 | |
| 1294 | /** |
| 1295 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 1296 | */ |
| 1297 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 1298 | 0 /* LY_TYPE_UNKNOWN */, |
| 1299 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
| 1300 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 1301 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 1302 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 1303 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 1304 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
| 1305 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 1306 | 0 /* LY_TYPE_BOOL */, |
| 1307 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 1308 | 0 /* LY_TYPE_EMPTY */, |
| 1309 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 1310 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 1311 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 1312 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
| 1313 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 1314 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
| 1315 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
| 1316 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
| 1317 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 1318 | }; |
| 1319 | |
| 1320 | /** |
| 1321 | * @brief stringification of the YANG built-in data types |
| 1322 | */ |
| 1323 | const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = { |
Radek Krejci | 04699f0 | 2021-03-22 21:50:29 +0100 | [diff] [blame] | 1324 | LY_TYPE_UNKNOWN_STR, |
| 1325 | LY_TYPE_BINARY_STR, |
| 1326 | LY_TYPE_UINT8_STR, |
| 1327 | LY_TYPE_UINT16_STR, |
| 1328 | LY_TYPE_UINT32_STR, |
| 1329 | LY_TYPE_UINT64_STR, |
| 1330 | LY_TYPE_STRING_STR, |
| 1331 | LY_TYPE_BITS_STR, |
| 1332 | LY_TYPE_BOOL_STR, |
| 1333 | LY_TYPE_DEC64_STR, |
| 1334 | LY_TYPE_EMPTY_STR, |
| 1335 | LY_TYPE_ENUM_STR, |
| 1336 | LY_TYPE_IDENT_STR, |
| 1337 | LY_TYPE_INST_STR, |
| 1338 | LY_TYPE_LEAFREF_STR, |
| 1339 | LY_TYPE_UNION_STR, |
| 1340 | LY_TYPE_INT8_STR, |
| 1341 | LY_TYPE_INT16_STR, |
| 1342 | LY_TYPE_INT32_STR, |
| 1343 | LY_TYPE_INT64_STR |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1344 | }; |
| 1345 | |
| 1346 | /** |
| 1347 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 1348 | * @param[in] ctx Compile context. |
| 1349 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 1350 | * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected. |
| 1351 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
Radek IÅ¡a | 0fe25a9 | 2021-03-18 08:45:18 +0100 | [diff] [blame] | 1352 | * @param[out] bitenums Newly created array of the compiled bitenums information for the current type. |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1353 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1354 | */ |
| 1355 | static LY_ERR |
| 1356 | lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, |
Radek IÅ¡a | 0fe25a9 | 2021-03-18 08:45:18 +0100 | [diff] [blame] | 1357 | struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1358 | { |
| 1359 | LY_ERR ret = LY_SUCCESS; |
| 1360 | LY_ARRAY_COUNT_TYPE u, v, match = 0; |
Radek Krejci | 430a558 | 2020-12-01 13:35:18 +0100 | [diff] [blame] | 1361 | int32_t highest_value = INT32_MIN, cur_val = INT32_MIN; |
| 1362 | uint32_t highest_position = 0, cur_pos = 0; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1363 | struct lysc_type_bitenum_item *e, storage; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1364 | ly_bool enabled; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1365 | |
| 1366 | if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1367 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1368 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 1369 | return LY_EVALID; |
| 1370 | } |
| 1371 | |
| 1372 | LY_ARRAY_FOR(enums_p, u) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1373 | /* perform all checks */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1374 | if (base_enums) { |
| 1375 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 1376 | LY_ARRAY_FOR(base_enums, v) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1377 | if (!strcmp(enums_p[u].name, base_enums[v].name)) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1378 | break; |
| 1379 | } |
| 1380 | } |
| 1381 | if (v == LY_ARRAY_COUNT(base_enums)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1382 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1383 | "Invalid %s - derived type adds new item \"%s\".", |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1384 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1385 | return LY_EVALID; |
| 1386 | } |
| 1387 | match = v; |
| 1388 | } |
| 1389 | |
| 1390 | if (basetype == LY_TYPE_ENUM) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1391 | if (enums_p[u].flags & LYS_SET_VALUE) { |
Radek IÅ¡a | 038b60d | 2020-11-26 11:37:15 +0100 | [diff] [blame] | 1392 | /* value assigned by model */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1393 | cur_val = (int32_t)enums_p[u].value; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1394 | /* check collision with other values */ |
Radek IÅ¡a | 0fe25a9 | 2021-03-18 08:45:18 +0100 | [diff] [blame] | 1395 | LY_ARRAY_FOR(*bitenums, v) { |
| 1396 | if (cur_val == (*bitenums)[v].value) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1397 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1398 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
Radek IÅ¡a | 0fe25a9 | 2021-03-18 08:45:18 +0100 | [diff] [blame] | 1399 | cur_val, enums_p[u].name, (*bitenums)[v].name); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1400 | return LY_EVALID; |
| 1401 | } |
| 1402 | } |
| 1403 | } else if (base_enums) { |
| 1404 | /* inherit the assigned value */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1405 | cur_val = base_enums[match].value; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1406 | } else { |
| 1407 | /* assign value automatically */ |
Radek IÅ¡a | 038b60d | 2020-11-26 11:37:15 +0100 | [diff] [blame] | 1408 | if (u == 0) { |
| 1409 | cur_val = 0; |
| 1410 | } else if (highest_value == INT32_MAX) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1411 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1412 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1413 | "\"%s\" since the highest value is already 2147483647.", enums_p[u].name); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1414 | return LY_EVALID; |
Radek IÅ¡a | 038b60d | 2020-11-26 11:37:15 +0100 | [diff] [blame] | 1415 | } else { |
| 1416 | cur_val = highest_value + 1; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1417 | } |
Radek IÅ¡a | 038b60d | 2020-11-26 11:37:15 +0100 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | /* save highest value for auto assing */ |
| 1421 | if (highest_value < cur_val) { |
| 1422 | highest_value = cur_val; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1423 | } |
| 1424 | } else { /* LY_TYPE_BITS */ |
| 1425 | if (enums_p[u].flags & LYS_SET_VALUE) { |
Radek IÅ¡a | ca013ee | 2020-11-26 17:51:26 +0100 | [diff] [blame] | 1426 | /* value assigned by model */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1427 | cur_pos = (uint32_t)enums_p[u].value; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1428 | /* check collision with other values */ |
Radek IÅ¡a | 0fe25a9 | 2021-03-18 08:45:18 +0100 | [diff] [blame] | 1429 | LY_ARRAY_FOR(*bitenums, v) { |
| 1430 | if (cur_pos == (*bitenums)[v].position) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1431 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1432 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
Radek IÅ¡a | 0fe25a9 | 2021-03-18 08:45:18 +0100 | [diff] [blame] | 1433 | cur_pos, enums_p[u].name, (*bitenums)[v].name); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1434 | return LY_EVALID; |
| 1435 | } |
| 1436 | } |
| 1437 | } else if (base_enums) { |
| 1438 | /* inherit the assigned value */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1439 | cur_pos = base_enums[match].position; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1440 | } else { |
| 1441 | /* assign value automatically */ |
Radek IÅ¡a | ca013ee | 2020-11-26 17:51:26 +0100 | [diff] [blame] | 1442 | if (u == 0) { |
| 1443 | cur_pos = 0; |
| 1444 | } else if (highest_position == UINT32_MAX) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1445 | /* counter overflow */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1446 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1447 | "Invalid bits - it is not possible to auto-assign bit position for " |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1448 | "\"%s\" since the highest value is already 4294967295.", enums_p[u].name); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1449 | return LY_EVALID; |
Radek IÅ¡a | ca013ee | 2020-11-26 17:51:26 +0100 | [diff] [blame] | 1450 | } else { |
| 1451 | cur_pos = highest_position + 1; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1452 | } |
Radek IÅ¡a | ca013ee | 2020-11-26 17:51:26 +0100 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | /* save highest position for auto assing */ |
| 1456 | if (highest_position < cur_pos) { |
| 1457 | highest_position = cur_pos; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1458 | } |
| 1459 | } |
| 1460 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1461 | /* the assigned values must not change from the derived type */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1462 | if (base_enums) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1463 | if (basetype == LY_TYPE_ENUM) { |
| 1464 | if (cur_val != base_enums[match].value) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1465 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1466 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1467 | enums_p[u].name, base_enums[match].value, cur_val); |
| 1468 | return LY_EVALID; |
| 1469 | } |
| 1470 | } else { |
| 1471 | if (cur_pos != base_enums[match].position) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1472 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1473 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1474 | enums_p[u].name, base_enums[match].position, cur_pos); |
| 1475 | return LY_EVALID; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1476 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1480 | /* add new enum/bit */ |
Radek IÅ¡a | 0fe25a9 | 2021-03-18 08:45:18 +0100 | [diff] [blame] | 1481 | LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1482 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done); |
| 1483 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done); |
| 1484 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done); |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 1485 | e->flags = (enums_p[u].flags & LYS_FLAGS_COMPILED_MASK) | (basetype == LY_TYPE_ENUM ? LYS_IS_ENUM : 0); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1486 | if (basetype == LY_TYPE_ENUM) { |
| 1487 | e->value = cur_val; |
| 1488 | } else { |
| 1489 | e->position = cur_pos; |
| 1490 | } |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 1491 | COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1492 | |
Michal Vasko | f4fa90d | 2021-11-11 15:05:19 +0100 | [diff] [blame] | 1493 | /* evaluate if-ffeatures */ |
| 1494 | LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled)); |
| 1495 | if (!enabled) { |
| 1496 | /* set only flag, later resolved and removed */ |
| 1497 | e->flags |= LYS_DISABLED; |
| 1498 | } |
| 1499 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1500 | if (basetype == LY_TYPE_BITS) { |
| 1501 | /* keep bits ordered by position */ |
Radek IÅ¡a | 0fe25a9 | 2021-03-18 08:45:18 +0100 | [diff] [blame] | 1502 | for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {} |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1503 | if (v != u) { |
| 1504 | memcpy(&storage, e, sizeof *e); |
Radek IÅ¡a | 0fe25a9 | 2021-03-18 08:45:18 +0100 | [diff] [blame] | 1505 | memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums); |
| 1506 | memcpy(&(*bitenums)[v], &storage, sizeof storage); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1507 | } |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | done: |
| 1512 | return ret; |
| 1513 | } |
| 1514 | |
Radek Krejci | 6a20569 | 2020-12-09 13:58:22 +0100 | [diff] [blame] | 1515 | static LY_ERR |
| 1516 | lys_compile_type_union(struct lysc_ctx *ctx, struct lysp_type *ptypes, struct lysp_node *context_pnode, uint16_t context_flags, |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 1517 | const char *context_name, struct lysc_type ***utypes_p) |
Radek Krejci | 6a20569 | 2020-12-09 13:58:22 +0100 | [diff] [blame] | 1518 | { |
| 1519 | LY_ERR ret = LY_SUCCESS; |
| 1520 | struct lysc_type **utypes = *utypes_p; |
| 1521 | struct lysc_type_union *un_aux = NULL; |
| 1522 | |
| 1523 | LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error); |
| 1524 | for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) { |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 1525 | ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional], |
| 1526 | NULL, NULL); |
Radek Krejci | 6a20569 | 2020-12-09 13:58:22 +0100 | [diff] [blame] | 1527 | LY_CHECK_GOTO(ret, error); |
| 1528 | if (utypes[u + additional]->basetype == LY_TYPE_UNION) { |
| 1529 | /* add space for additional types from the union subtype */ |
| 1530 | un_aux = (struct lysc_type_union *)utypes[u + additional]; |
| 1531 | LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, |
| 1532 | LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error); |
| 1533 | |
| 1534 | /* copy subtypes of the subtype union */ |
| 1535 | for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) { |
| 1536 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 1537 | struct lysc_type_leafref *lref; |
| 1538 | |
| 1539 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 1540 | utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 1541 | LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error); |
| 1542 | lref = (struct lysc_type_leafref *)utypes[u + additional]; |
| 1543 | |
| 1544 | lref->basetype = LY_TYPE_LEAFREF; |
| 1545 | ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path); |
| 1546 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 080064b | 2021-08-31 15:20:44 +0200 | [diff] [blame] | 1547 | lref->refcount = 1; |
Radek Krejci | 6a20569 | 2020-12-09 13:58:22 +0100 | [diff] [blame] | 1548 | lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1549 | ret = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED, |
Radek Krejci | 2926c1b | 2021-03-16 14:45:45 +0100 | [diff] [blame] | 1550 | ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes); |
Radek Krejci | 6a20569 | 2020-12-09 13:58:22 +0100 | [diff] [blame] | 1551 | LY_CHECK_GOTO(ret, error); |
| 1552 | /* TODO extensions */ |
| 1553 | |
| 1554 | } else { |
| 1555 | utypes[u + additional] = un_aux->types[v]; |
Michal Vasko | 04338d9 | 2021-09-01 07:58:14 +0200 | [diff] [blame] | 1556 | LY_ATOMIC_INC_BARRIER(un_aux->types[v]->refcount); |
Radek Krejci | 6a20569 | 2020-12-09 13:58:22 +0100 | [diff] [blame] | 1557 | } |
| 1558 | ++additional; |
| 1559 | LY_ARRAY_INCREMENT(utypes); |
| 1560 | } |
| 1561 | /* compensate u increment in main loop */ |
| 1562 | --additional; |
| 1563 | |
| 1564 | /* free the replaced union subtype */ |
| 1565 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux); |
| 1566 | un_aux = NULL; |
| 1567 | } else { |
| 1568 | LY_ARRAY_INCREMENT(utypes); |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | *utypes_p = utypes; |
| 1573 | return LY_SUCCESS; |
| 1574 | |
| 1575 | error: |
| 1576 | if (un_aux) { |
| 1577 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux); |
| 1578 | } |
| 1579 | *utypes_p = utypes; |
| 1580 | return ret; |
| 1581 | } |
| 1582 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1583 | /** |
| 1584 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 1585 | * @param[in] ctx Compile context. |
| 1586 | * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types. |
| 1587 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1588 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 1589 | * @param[in] type_p Parsed type to compile. |
| 1590 | * @param[in] basetype Base YANG built-in type of the type to compile. |
| 1591 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 1592 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 1593 | * @param[out] type Newly created type structure with the filled information about the type. |
| 1594 | * @return LY_ERR value. |
| 1595 | */ |
| 1596 | static LY_ERR |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 1597 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name, |
| 1598 | struct lysp_type *type_p, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, struct lysc_type **type) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1599 | { |
| 1600 | LY_ERR ret = LY_SUCCESS; |
| 1601 | struct lysc_type_bin *bin; |
| 1602 | struct lysc_type_num *num; |
| 1603 | struct lysc_type_str *str; |
| 1604 | struct lysc_type_bits *bits; |
| 1605 | struct lysc_type_enum *enumeration; |
| 1606 | struct lysc_type_dec *dec; |
| 1607 | struct lysc_type_identityref *idref; |
| 1608 | struct lysc_type_leafref *lref; |
Radek Krejci | 6a20569 | 2020-12-09 13:58:22 +0100 | [diff] [blame] | 1609 | struct lysc_type_union *un; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1610 | |
| 1611 | switch (basetype) { |
| 1612 | case LY_TYPE_BINARY: |
| 1613 | bin = (struct lysc_type_bin *)(*type); |
| 1614 | |
| 1615 | /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */ |
| 1616 | if (type_p->length) { |
| 1617 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 1618 | base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length)); |
| 1619 | if (!tpdfname) { |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 1620 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1621 | } |
| 1622 | } |
| 1623 | break; |
| 1624 | case LY_TYPE_BITS: |
| 1625 | /* RFC 7950 9.7 - bits */ |
| 1626 | bits = (struct lysc_type_bits *)(*type); |
| 1627 | if (type_p->bits) { |
| 1628 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
| 1629 | base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL, |
| 1630 | (struct lysc_type_bitenum_item **)&bits->bits)); |
| 1631 | } |
| 1632 | |
| 1633 | if (!base && !type_p->flags) { |
| 1634 | /* type derived from bits built-in type must contain at least one bit */ |
| 1635 | if (tpdfname) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1636 | LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1637 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1638 | LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", ""); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1639 | } |
| 1640 | return LY_EVALID; |
| 1641 | } |
| 1642 | break; |
| 1643 | case LY_TYPE_DEC64: |
| 1644 | dec = (struct lysc_type_dec *)(*type); |
| 1645 | |
| 1646 | /* RFC 7950 9.3.4 - fraction-digits */ |
| 1647 | if (!base) { |
| 1648 | if (!type_p->fraction_digits) { |
| 1649 | if (tpdfname) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1650 | LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1651 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1652 | LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", ""); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1653 | } |
| 1654 | return LY_EVALID; |
| 1655 | } |
| 1656 | dec->fraction_digits = type_p->fraction_digits; |
| 1657 | } else { |
| 1658 | if (type_p->fraction_digits) { |
| 1659 | /* fraction digits is prohibited in types not directly derived from built-in decimal64 */ |
| 1660 | if (tpdfname) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1661 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1662 | "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.", |
| 1663 | tpdfname); |
| 1664 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1665 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1666 | "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type."); |
| 1667 | } |
| 1668 | return LY_EVALID; |
| 1669 | } |
| 1670 | dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits; |
| 1671 | } |
| 1672 | |
| 1673 | /* RFC 7950 9.2.4 - range */ |
| 1674 | if (type_p->range) { |
| 1675 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 1676 | base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range)); |
| 1677 | if (!tpdfname) { |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 1678 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | break; |
| 1682 | case LY_TYPE_STRING: |
| 1683 | str = (struct lysc_type_str *)(*type); |
| 1684 | |
| 1685 | /* RFC 7950 9.4.4 - length */ |
| 1686 | if (type_p->length) { |
| 1687 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 1688 | base ? ((struct lysc_type_str *)base)->length : NULL, &str->length)); |
| 1689 | if (!tpdfname) { |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 1690 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1691 | } |
| 1692 | } else if (base && ((struct lysc_type_str *)base)->length) { |
| 1693 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length); |
| 1694 | } |
| 1695 | |
| 1696 | /* RFC 7950 9.4.5 - pattern */ |
| 1697 | if (type_p->patterns) { |
| 1698 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
| 1699 | base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns)); |
| 1700 | } else if (base && ((struct lysc_type_str *)base)->patterns) { |
| 1701 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns); |
| 1702 | } |
| 1703 | break; |
| 1704 | case LY_TYPE_ENUM: |
| 1705 | enumeration = (struct lysc_type_enum *)(*type); |
| 1706 | |
| 1707 | /* RFC 7950 9.6 - enum */ |
| 1708 | if (type_p->enums) { |
| 1709 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
| 1710 | base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums)); |
| 1711 | } |
| 1712 | |
| 1713 | if (!base && !type_p->flags) { |
| 1714 | /* type derived from enumerations built-in type must contain at least one enum */ |
| 1715 | if (tpdfname) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1716 | LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1717 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1718 | LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", ""); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1719 | } |
| 1720 | return LY_EVALID; |
| 1721 | } |
| 1722 | break; |
| 1723 | case LY_TYPE_INT8: |
| 1724 | case LY_TYPE_UINT8: |
| 1725 | case LY_TYPE_INT16: |
| 1726 | case LY_TYPE_UINT16: |
| 1727 | case LY_TYPE_INT32: |
| 1728 | case LY_TYPE_UINT32: |
| 1729 | case LY_TYPE_INT64: |
| 1730 | case LY_TYPE_UINT64: |
| 1731 | num = (struct lysc_type_num *)(*type); |
| 1732 | |
| 1733 | /* RFC 6020 9.2.4 - range */ |
| 1734 | if (type_p->range) { |
| 1735 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 1736 | base ? ((struct lysc_type_num *)base)->range : NULL, &num->range)); |
| 1737 | if (!tpdfname) { |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 1738 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1739 | } |
| 1740 | } |
| 1741 | break; |
| 1742 | case LY_TYPE_IDENT: |
| 1743 | idref = (struct lysc_type_identityref *)(*type); |
| 1744 | |
| 1745 | /* RFC 7950 9.10.2 - base */ |
| 1746 | if (type_p->bases) { |
| 1747 | if (base) { |
| 1748 | /* only the directly derived identityrefs can contain base specification */ |
| 1749 | if (tpdfname) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1750 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1751 | "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.", |
| 1752 | tpdfname); |
| 1753 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1754 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1755 | "Invalid base substatement for the type not directly derived from identityref built-in type."); |
| 1756 | } |
| 1757 | return LY_EVALID; |
| 1758 | } |
aPiecek | f4a0a19 | 2021-08-03 15:14:17 +0200 | [diff] [blame] | 1759 | LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->pmod, type_p->bases, NULL, &idref->bases)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | if (!base && !type_p->flags) { |
| 1763 | /* type derived from identityref built-in type must contain at least one base */ |
| 1764 | if (tpdfname) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1765 | LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1766 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1767 | LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1768 | } |
| 1769 | return LY_EVALID; |
| 1770 | } |
| 1771 | break; |
| 1772 | case LY_TYPE_LEAFREF: |
| 1773 | lref = (struct lysc_type_leafref *)*type; |
| 1774 | |
| 1775 | /* RFC 7950 9.9.3 - require-instance */ |
| 1776 | if (type_p->flags & LYS_SET_REQINST) { |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 1777 | if (type_p->pmod->version < LYS_VERSION_1_1) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1778 | if (tpdfname) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1779 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1780 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 1781 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1782 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1783 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 1784 | } |
| 1785 | return LY_EVALID; |
| 1786 | } |
| 1787 | lref->require_instance = type_p->require_instance; |
| 1788 | } else if (base) { |
| 1789 | /* inherit */ |
| 1790 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
| 1791 | } else { |
| 1792 | /* default is true */ |
| 1793 | lref->require_instance = 1; |
| 1794 | } |
| 1795 | if (type_p->path) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1796 | LY_VALUE_FORMAT format; |
Radek Krejci | 2926c1b | 2021-03-16 14:45:45 +0100 | [diff] [blame] | 1797 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1798 | LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path)); |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 1799 | LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, type_p->path->expr, strlen(type_p->path->expr), |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1800 | LY_VALUE_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1801 | } else if (base) { |
| 1802 | LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path)); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1803 | LY_CHECK_RET(lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED, |
Radek Krejci | 2926c1b | 2021-03-16 14:45:45 +0100 | [diff] [blame] | 1804 | ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1805 | } else if (tpdfname) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1806 | LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1807 | return LY_EVALID; |
| 1808 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1809 | LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1810 | return LY_EVALID; |
| 1811 | } |
| 1812 | break; |
| 1813 | case LY_TYPE_INST: |
| 1814 | /* RFC 7950 9.9.3 - require-instance */ |
| 1815 | if (type_p->flags & LYS_SET_REQINST) { |
| 1816 | ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance; |
| 1817 | } else { |
| 1818 | /* default is true */ |
| 1819 | ((struct lysc_type_instanceid *)(*type))->require_instance = 1; |
| 1820 | } |
| 1821 | break; |
| 1822 | case LY_TYPE_UNION: |
| 1823 | un = (struct lysc_type_union *)(*type); |
| 1824 | |
| 1825 | /* RFC 7950 7.4 - type */ |
| 1826 | if (type_p->types) { |
| 1827 | if (base) { |
| 1828 | /* only the directly derived union can contain types specification */ |
| 1829 | if (tpdfname) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1830 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1831 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 1832 | tpdfname); |
| 1833 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1834 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1835 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 1836 | } |
| 1837 | return LY_EVALID; |
| 1838 | } |
| 1839 | /* compile the type */ |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 1840 | LY_CHECK_RET(lys_compile_type_union(ctx, type_p->types, context_pnode, context_flags, context_name, &un->types)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | if (!base && !type_p->flags) { |
| 1844 | /* type derived from union built-in type must contain at least one type */ |
| 1845 | if (tpdfname) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1846 | LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1847 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1848 | LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1849 | } |
| 1850 | return LY_EVALID; |
| 1851 | } |
| 1852 | break; |
| 1853 | case LY_TYPE_BOOL: |
| 1854 | case LY_TYPE_EMPTY: |
| 1855 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 1856 | break; |
| 1857 | } |
| 1858 | |
| 1859 | if (tpdfname) { |
| 1860 | switch (basetype) { |
| 1861 | case LY_TYPE_BINARY: |
| 1862 | type_p->compiled = *type; |
| 1863 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 1864 | break; |
| 1865 | case LY_TYPE_BITS: |
| 1866 | type_p->compiled = *type; |
| 1867 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 1868 | break; |
| 1869 | case LY_TYPE_DEC64: |
| 1870 | type_p->compiled = *type; |
| 1871 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 1872 | break; |
| 1873 | case LY_TYPE_STRING: |
| 1874 | type_p->compiled = *type; |
| 1875 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 1876 | break; |
| 1877 | case LY_TYPE_ENUM: |
| 1878 | type_p->compiled = *type; |
| 1879 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 1880 | break; |
| 1881 | case LY_TYPE_INT8: |
| 1882 | case LY_TYPE_UINT8: |
| 1883 | case LY_TYPE_INT16: |
| 1884 | case LY_TYPE_UINT16: |
| 1885 | case LY_TYPE_INT32: |
| 1886 | case LY_TYPE_UINT32: |
| 1887 | case LY_TYPE_INT64: |
| 1888 | case LY_TYPE_UINT64: |
| 1889 | type_p->compiled = *type; |
| 1890 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 1891 | break; |
| 1892 | case LY_TYPE_IDENT: |
| 1893 | type_p->compiled = *type; |
| 1894 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 1895 | break; |
| 1896 | case LY_TYPE_LEAFREF: |
| 1897 | type_p->compiled = *type; |
| 1898 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 1899 | break; |
| 1900 | case LY_TYPE_INST: |
| 1901 | type_p->compiled = *type; |
| 1902 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 1903 | break; |
| 1904 | case LY_TYPE_UNION: |
| 1905 | type_p->compiled = *type; |
| 1906 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 1907 | break; |
| 1908 | case LY_TYPE_BOOL: |
| 1909 | case LY_TYPE_EMPTY: |
| 1910 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 1911 | break; |
| 1912 | } |
| 1913 | } |
| 1914 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 1915 | |
| 1916 | cleanup: |
| 1917 | return ret; |
| 1918 | } |
| 1919 | |
Radek Krejci | bf940f9 | 2021-03-24 21:04:13 +0100 | [diff] [blame] | 1920 | /** |
| 1921 | * @brief Find the correct plugin implementing the described type |
| 1922 | * |
| 1923 | * @param[in] mod Module where the type is defined |
| 1924 | * @param[in] name Name of the type (typedef) |
| 1925 | * @param[in] basetype Type's basetype (when the built-in base plugin is supposed to be used) |
| 1926 | * @return Pointer to the plugin implementing the described data type. |
| 1927 | */ |
| 1928 | static struct lyplg_type * |
| 1929 | lys_compile_type_get_plugin(struct lys_module *mod, const char *name, LY_DATA_TYPE basetype) |
| 1930 | { |
| 1931 | struct lyplg_type *p; |
| 1932 | |
| 1933 | /* try to find loaded user type plugins */ |
| 1934 | p = lyplg_find(LYPLG_TYPE, mod->name, mod->revision, name); |
| 1935 | if (!p) { |
| 1936 | /* use the internal built-in type implementation */ |
| 1937 | p = lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]); |
| 1938 | } |
| 1939 | |
| 1940 | return p; |
| 1941 | } |
| 1942 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1943 | LY_ERR |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 1944 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name, |
| 1945 | struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1946 | { |
| 1947 | LY_ERR ret = LY_SUCCESS; |
| 1948 | ly_bool dummyloops = 0; |
| 1949 | struct type_context { |
| 1950 | const struct lysp_tpdf *tpdf; |
| 1951 | struct lysp_node *node; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1952 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
| 1953 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
| 1954 | struct lysc_type *base = NULL, *prev_type; |
| 1955 | struct ly_set tpdf_chain = {0}; |
Michal Vasko | 388a663 | 2021-08-06 11:27:43 +0200 | [diff] [blame] | 1956 | struct lyplg_type *plugin; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1957 | |
| 1958 | (*type) = NULL; |
| 1959 | if (dflt) { |
| 1960 | *dflt = NULL; |
| 1961 | } |
| 1962 | |
| 1963 | tctx = calloc(1, sizeof *tctx); |
| 1964 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 1965 | for (ret = lysp_type_find(type_p->name, context_pnode, type_p->pmod, &basetype, &tctx->tpdf, &tctx->node); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1966 | ret == LY_SUCCESS; |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 1967 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod, |
| 1968 | &basetype, &tctx->tpdf, &tctx->node)) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1969 | if (basetype) { |
| 1970 | break; |
| 1971 | } |
| 1972 | |
| 1973 | /* check status */ |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 1974 | ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags, |
| 1975 | (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1976 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 1977 | |
| 1978 | if (units && !*units) { |
| 1979 | /* inherit units */ |
| 1980 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret); |
| 1981 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 1982 | } |
| 1983 | if (dflt && !*dflt && tctx->tpdf->dflt.str) { |
| 1984 | /* inherit default */ |
| 1985 | *dflt = (struct lysp_qname *)&tctx->tpdf->dflt; |
| 1986 | } |
| 1987 | if (dummyloops && (!units || *units) && dflt && *dflt) { |
| 1988 | basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 1989 | break; |
| 1990 | } |
| 1991 | |
Michal Vasko | 080064b | 2021-08-31 15:20:44 +0200 | [diff] [blame] | 1992 | if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) { |
Radek Krejci | 720d261 | 2021-03-03 19:44:22 +0100 | [diff] [blame] | 1993 | /* context recompilation - everything was freed previously (the only reference is from the parsed type itself) |
| 1994 | * and we need now recompile the type again in the updated context. */ |
| 1995 | lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled); |
| 1996 | ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL; |
| 1997 | } |
| 1998 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1999 | if (tctx->tpdf->type.compiled) { |
| 2000 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2001 | * but we still may need to inherit default and units values, so start dummy loops */ |
| 2002 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2003 | ret = ly_set_add(&tpdf_chain, tctx, 1, NULL); |
| 2004 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2005 | |
| 2006 | if ((units && !*units) || (dflt && !*dflt)) { |
| 2007 | dummyloops = 1; |
| 2008 | goto preparenext; |
| 2009 | } else { |
| 2010 | tctx = NULL; |
| 2011 | break; |
| 2012 | } |
| 2013 | } |
| 2014 | |
| 2015 | /* circular typedef reference detection */ |
| 2016 | for (uint32_t u = 0; u < tpdf_chain.count; u++) { |
| 2017 | /* local part */ |
| 2018 | tctx_iter = (struct type_context *)tpdf_chain.objs[u]; |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 2019 | if (tctx_iter->tpdf == tctx->tpdf) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2020 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2021 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2022 | free(tctx); |
| 2023 | ret = LY_EVALID; |
| 2024 | goto cleanup; |
| 2025 | } |
| 2026 | } |
| 2027 | for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) { |
| 2028 | /* global part for unions corner case */ |
| 2029 | tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u]; |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 2030 | if (tctx_iter->tpdf == tctx->tpdf) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2031 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2032 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2033 | free(tctx); |
| 2034 | ret = LY_EVALID; |
| 2035 | goto cleanup; |
| 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | /* store information for the following processing */ |
| 2040 | ret = ly_set_add(&tpdf_chain, tctx, 1, NULL); |
| 2041 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2042 | |
| 2043 | preparenext: |
| 2044 | /* prepare next loop */ |
| 2045 | tctx_prev = tctx; |
| 2046 | tctx = calloc(1, sizeof *tctx); |
| 2047 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 2048 | } |
| 2049 | free(tctx); |
| 2050 | |
| 2051 | /* allocate type according to the basetype */ |
| 2052 | switch (basetype) { |
| 2053 | case LY_TYPE_BINARY: |
| 2054 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2055 | break; |
| 2056 | case LY_TYPE_BITS: |
| 2057 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2058 | break; |
| 2059 | case LY_TYPE_BOOL: |
| 2060 | case LY_TYPE_EMPTY: |
| 2061 | *type = calloc(1, sizeof(struct lysc_type)); |
| 2062 | break; |
| 2063 | case LY_TYPE_DEC64: |
| 2064 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2065 | break; |
| 2066 | case LY_TYPE_ENUM: |
| 2067 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2068 | break; |
| 2069 | case LY_TYPE_IDENT: |
| 2070 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2071 | break; |
| 2072 | case LY_TYPE_INST: |
| 2073 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2074 | break; |
| 2075 | case LY_TYPE_LEAFREF: |
| 2076 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2077 | break; |
| 2078 | case LY_TYPE_STRING: |
| 2079 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2080 | break; |
| 2081 | case LY_TYPE_UNION: |
| 2082 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2083 | break; |
| 2084 | case LY_TYPE_INT8: |
| 2085 | case LY_TYPE_UINT8: |
| 2086 | case LY_TYPE_INT16: |
| 2087 | case LY_TYPE_UINT16: |
| 2088 | case LY_TYPE_INT32: |
| 2089 | case LY_TYPE_UINT32: |
| 2090 | case LY_TYPE_INT64: |
| 2091 | case LY_TYPE_UINT64: |
| 2092 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2093 | break; |
| 2094 | case LY_TYPE_UNKNOWN: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2095 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2096 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 2097 | ret = LY_EVALID; |
| 2098 | goto cleanup; |
| 2099 | } |
| 2100 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
| 2101 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2102 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2103 | ly_data_type2str[basetype]); |
| 2104 | free(*type); |
| 2105 | (*type) = NULL; |
| 2106 | ret = LY_EVALID; |
| 2107 | goto cleanup; |
| 2108 | } |
| 2109 | |
| 2110 | /* get restrictions from the referred typedefs */ |
| 2111 | for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 2112 | tctx = (struct type_context *)tpdf_chain.objs[u]; |
| 2113 | |
| 2114 | /* remember the typedef context for circular check */ |
| 2115 | ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL); |
| 2116 | LY_CHECK_GOTO(ret, cleanup); |
| 2117 | |
| 2118 | if (tctx->tpdf->type.compiled) { |
Michal Vasko | 388a663 | 2021-08-06 11:27:43 +0200 | [diff] [blame] | 2119 | /* already compiled */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2120 | base = tctx->tpdf->type.compiled; |
| 2121 | continue; |
Michal Vasko | 388a663 | 2021-08-06 11:27:43 +0200 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | /* get plugin for the specific typedef */ |
| 2125 | plugin = lys_compile_type_get_plugin(tctx->tpdf->type.pmod->mod, tctx->tpdf->name, basetype); |
| 2126 | assert(plugin); |
| 2127 | |
| 2128 | if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags) && |
| 2129 | (plugin == base->plugin)) { |
| 2130 | /* no change, reuse the compiled base */ |
| 2131 | ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = base; |
Michal Vasko | 04338d9 | 2021-09-01 07:58:14 +0200 | [diff] [blame] | 2132 | LY_ATOMIC_INC_BARRIER(base->refcount); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2133 | continue; |
| 2134 | } |
| 2135 | |
Michal Vasko | 04338d9 | 2021-09-01 07:58:14 +0200 | [diff] [blame] | 2136 | LY_ATOMIC_INC_BARRIER((*type)->refcount); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2137 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2138 | LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2139 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 2140 | ret = LY_EVALID; |
| 2141 | goto cleanup; |
| 2142 | } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2143 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2144 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 2145 | tctx->tpdf->name, tctx->tpdf->dflt.str); |
| 2146 | ret = LY_EVALID; |
| 2147 | goto cleanup; |
| 2148 | } |
| 2149 | |
| 2150 | (*type)->basetype = basetype; |
Michal Vasko | 388a663 | 2021-08-06 11:27:43 +0200 | [diff] [blame] | 2151 | (*type)->plugin = plugin; |
| 2152 | |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 2153 | /* collect extensions */ |
| 2154 | COMPILE_EXTS_GOTO(ctx, tctx->tpdf->type.exts, (*type)->exts, (*type), ret, cleanup); |
Michal Vasko | 388a663 | 2021-08-06 11:27:43 +0200 | [diff] [blame] | 2155 | |
| 2156 | /* compile the new typedef */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2157 | prev_type = *type; |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 2158 | ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2159 | &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type); |
| 2160 | LY_CHECK_GOTO(ret, cleanup); |
| 2161 | base = prev_type; |
| 2162 | } |
| 2163 | /* remove the processed typedef contexts from the stack for circular check */ |
| 2164 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
| 2165 | |
| 2166 | /* process the type definition in leaf */ |
| 2167 | if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) { |
| 2168 | /* get restrictions from the node itself */ |
| 2169 | (*type)->basetype = basetype; |
Radek Krejci | bf940f9 | 2021-03-24 21:04:13 +0100 | [diff] [blame] | 2170 | (*type)->plugin = base ? base->plugin : lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]); |
Michal Vasko | 04338d9 | 2021-09-01 07:58:14 +0200 | [diff] [blame] | 2171 | LY_ATOMIC_INC_BARRIER((*type)->refcount); |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 2172 | ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, type_p, basetype, NULL, base, type); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2173 | LY_CHECK_GOTO(ret, cleanup); |
| 2174 | } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) { |
| 2175 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 2176 | free(*type); |
| 2177 | (*type) = base; |
Michal Vasko | 04338d9 | 2021-09-01 07:58:14 +0200 | [diff] [blame] | 2178 | LY_ATOMIC_INC_BARRIER((*type)->refcount); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2179 | } |
| 2180 | |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 2181 | COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2182 | |
| 2183 | cleanup: |
| 2184 | ly_set_erase(&tpdf_chain, free); |
| 2185 | return ret; |
| 2186 | } |
| 2187 | |
| 2188 | /** |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2189 | * @brief Check uniqness of the node/action/notification name. |
| 2190 | * |
| 2191 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 2192 | * structures, but they share the namespace so we need to check their name collisions. |
| 2193 | * |
| 2194 | * @param[in] ctx Compile context. |
| 2195 | * @param[in] parent Parent of the nodes to check, can be NULL. |
| 2196 | * @param[in] name Name of the item to find in the given lists. |
| 2197 | * @param[in] exclude Node that was just added that should be excluded from the name checking. |
| 2198 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 2199 | */ |
| 2200 | static LY_ERR |
| 2201 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name, |
| 2202 | const struct lysc_node *exclude) |
| 2203 | { |
| 2204 | const struct lysc_node *iter, *iter2; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2205 | const struct lysc_node_action *actions; |
| 2206 | const struct lysc_node_notif *notifs; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2207 | uint32_t getnext_flags; |
Radek Krejci | 078e434 | 2021-02-12 18:17:26 +0100 | [diff] [blame] | 2208 | struct ly_set parent_choices = {0}; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2209 | |
| 2210 | #define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name)) |
| 2211 | |
| 2212 | if (exclude->nodetype == LYS_CASE) { |
| 2213 | /* check restricted only to all the cases */ |
| 2214 | assert(parent->nodetype == LYS_CHOICE); |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 2215 | LY_LIST_FOR(lysc_node_child(parent), iter) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2216 | if (CHECK_NODE(iter, exclude, name)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2217 | LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case"); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2218 | return LY_EEXIST; |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | return LY_SUCCESS; |
| 2223 | } |
| 2224 | |
| 2225 | /* no reason for our parent to be choice anymore */ |
| 2226 | assert(!parent || (parent->nodetype != LYS_CHOICE)); |
| 2227 | |
| 2228 | if (parent && (parent->nodetype == LYS_CASE)) { |
| 2229 | /* move to the first data definition parent */ |
Radek Krejci | 078e434 | 2021-02-12 18:17:26 +0100 | [diff] [blame] | 2230 | |
| 2231 | /* but remember the choice nodes on the parents path to avoid believe they collide with our node */ |
| 2232 | iter = lysc_data_parent(parent); |
| 2233 | do { |
| 2234 | parent = parent->parent; |
| 2235 | if (parent && (parent->nodetype == LYS_CHOICE)) { |
| 2236 | ly_set_add(&parent_choices, (void *)parent, 1, NULL); |
| 2237 | } |
| 2238 | } while (parent != iter); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2239 | } |
| 2240 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 2241 | getnext_flags = LYS_GETNEXT_WITHCHOICE; |
Michal Vasko | b322b7d | 2021-01-29 17:22:24 +0100 | [diff] [blame] | 2242 | if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 2243 | /* move to the inout to avoid traversing a not-filled-yet (the other) node */ |
| 2244 | if (exclude->flags & LYS_IS_OUTPUT) { |
| 2245 | getnext_flags |= LYS_GETNEXT_OUTPUT; |
| 2246 | parent = lysc_node_child(parent)->next; |
| 2247 | } else { |
| 2248 | parent = lysc_node_child(parent); |
| 2249 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | iter = NULL; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 2253 | if (!parent && ctx->ext) { |
| 2254 | while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) { |
| 2255 | if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) { |
| 2256 | goto error; |
| 2257 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2258 | |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 2259 | /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */ |
| 2260 | if (iter->nodetype == LYS_CHOICE) { |
| 2261 | iter2 = NULL; |
| 2262 | while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) { |
| 2263 | if (CHECK_NODE(iter2, exclude, name)) { |
| 2264 | goto error; |
| 2265 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2266 | } |
| 2267 | } |
| 2268 | } |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 2269 | } else { |
| 2270 | while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) { |
| 2271 | if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) { |
| 2272 | goto error; |
| 2273 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2274 | |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 2275 | /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */ |
| 2276 | if (iter->nodetype == LYS_CHOICE) { |
| 2277 | iter2 = NULL; |
| 2278 | while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) { |
| 2279 | if (CHECK_NODE(iter2, exclude, name)) { |
| 2280 | goto error; |
| 2281 | } |
| 2282 | } |
| 2283 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2284 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2285 | |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 2286 | actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs; |
| 2287 | LY_LIST_FOR((struct lysc_node *)actions, iter) { |
| 2288 | if (CHECK_NODE(iter, exclude, name)) { |
| 2289 | goto error; |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs; |
| 2294 | LY_LIST_FOR((struct lysc_node *)notifs, iter) { |
| 2295 | if (CHECK_NODE(iter, exclude, name)) { |
| 2296 | goto error; |
| 2297 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2298 | } |
| 2299 | } |
Radek Krejci | 078e434 | 2021-02-12 18:17:26 +0100 | [diff] [blame] | 2300 | ly_set_erase(&parent_choices, NULL); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2301 | return LY_SUCCESS; |
| 2302 | |
| 2303 | error: |
Radek Krejci | 078e434 | 2021-02-12 18:17:26 +0100 | [diff] [blame] | 2304 | ly_set_erase(&parent_choices, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2305 | LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification"); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2306 | return LY_EEXIST; |
| 2307 | |
| 2308 | #undef CHECK_NODE |
| 2309 | } |
| 2310 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2311 | /** |
| 2312 | * @brief Connect the node into the siblings list and check its name uniqueness. Also, |
| 2313 | * keep specific order of augments targetting the same node. |
| 2314 | * |
| 2315 | * @param[in] ctx Compile context |
| 2316 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 2317 | * the choice itself is expected instead of a specific case node. |
| 2318 | * @param[in] node Schema node to connect into the list. |
| 2319 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 2320 | * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly. |
| 2321 | */ |
| 2322 | static LY_ERR |
| 2323 | lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2324 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2325 | struct lysc_node **children, *anchor = NULL; |
| 2326 | int insert_after = 0; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2327 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2328 | node->parent = parent; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2329 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2330 | if (parent) { |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 2331 | if (node->nodetype == LYS_INPUT) { |
| 2332 | assert(parent->nodetype & (LYS_ACTION | LYS_RPC)); |
| 2333 | /* input node is part of the action but link it with output */ |
| 2334 | node->next = &((struct lysc_node_action *)parent)->output.node; |
| 2335 | node->prev = node->next; |
| 2336 | return LY_SUCCESS; |
| 2337 | } else if (node->nodetype == LYS_OUTPUT) { |
| 2338 | /* output node is part of the action but link it with input */ |
| 2339 | node->next = NULL; |
| 2340 | node->prev = &((struct lysc_node_action *)parent)->input.node; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2341 | return LY_SUCCESS; |
| 2342 | } else if (node->nodetype == LYS_ACTION) { |
| 2343 | children = (struct lysc_node **)lysc_node_actions_p(parent); |
| 2344 | } else if (node->nodetype == LYS_NOTIF) { |
| 2345 | children = (struct lysc_node **)lysc_node_notifs_p(parent); |
| 2346 | } else { |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 2347 | children = lysc_node_child_p(parent); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2348 | } |
| 2349 | assert(children); |
| 2350 | |
| 2351 | if (!(*children)) { |
| 2352 | /* first child */ |
| 2353 | *children = node; |
| 2354 | } else if (node->flags & LYS_KEY) { |
| 2355 | /* special handling of adding keys */ |
| 2356 | assert(node->module == parent->module); |
| 2357 | anchor = *children; |
| 2358 | if (anchor->flags & LYS_KEY) { |
| 2359 | while ((anchor->flags & LYS_KEY) && anchor->next) { |
| 2360 | anchor = anchor->next; |
| 2361 | } |
| 2362 | /* insert after the last key */ |
| 2363 | insert_after = 1; |
| 2364 | } /* else insert before anchor (at the beginning) */ |
| 2365 | } else if ((*children)->prev->module == node->module) { |
| 2366 | /* last child is from the same module, keep the order and insert at the end */ |
| 2367 | anchor = (*children)->prev; |
| 2368 | insert_after = 1; |
| 2369 | } else if (parent->module == node->module) { |
| 2370 | /* adding module child after some augments were connected */ |
| 2371 | for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {} |
| 2372 | } else { |
| 2373 | /* some augments are already connected and we are connecting new ones, |
| 2374 | * keep module name order and insert the node into the children list */ |
| 2375 | anchor = *children; |
| 2376 | do { |
| 2377 | anchor = anchor->prev; |
| 2378 | |
| 2379 | /* check that we have not found the last augment node from our module or |
| 2380 | * the first augment node from a "smaller" module or |
| 2381 | * the first node from a local module */ |
| 2382 | if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) || |
| 2383 | (anchor->module == parent->module)) { |
| 2384 | /* insert after */ |
| 2385 | insert_after = 1; |
| 2386 | break; |
| 2387 | } |
| 2388 | |
| 2389 | /* we have traversed all the nodes, insert before anchor (as the first node) */ |
| 2390 | } while (anchor->prev->next); |
| 2391 | } |
| 2392 | |
| 2393 | /* insert */ |
| 2394 | if (anchor) { |
| 2395 | if (insert_after) { |
| 2396 | node->next = anchor->next; |
| 2397 | node->prev = anchor; |
| 2398 | anchor->next = node; |
| 2399 | if (node->next) { |
| 2400 | /* middle node */ |
| 2401 | node->next->prev = node; |
| 2402 | } else { |
| 2403 | /* last node */ |
| 2404 | (*children)->prev = node; |
| 2405 | } |
| 2406 | } else { |
| 2407 | node->next = anchor; |
| 2408 | node->prev = anchor->prev; |
| 2409 | anchor->prev = node; |
| 2410 | if (anchor == *children) { |
| 2411 | /* first node */ |
| 2412 | *children = node; |
| 2413 | } else { |
| 2414 | /* middle node */ |
| 2415 | node->prev->next = node; |
| 2416 | } |
| 2417 | } |
| 2418 | } |
| 2419 | |
| 2420 | /* check the name uniqueness (even for an only child, it may be in case) */ |
| 2421 | if (lys_compile_node_uniqness(ctx, parent, node->name, node)) { |
| 2422 | return LY_EEXIST; |
| 2423 | } |
| 2424 | } else { |
| 2425 | /* top-level element */ |
| 2426 | struct lysc_node **list; |
| 2427 | |
Radek Krejci | 6b88a46 | 2021-02-17 12:39:34 +0100 | [diff] [blame] | 2428 | if (ctx->ext) { |
| 2429 | lysc_ext_substmt(ctx->ext, LY_STMT_CONTAINER /* matches all data nodes */, (void **)&list, NULL); |
| 2430 | } else if (node->nodetype == LYS_RPC) { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2431 | list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs; |
| 2432 | } else if (node->nodetype == LYS_NOTIF) { |
| 2433 | list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs; |
| 2434 | } else { |
| 2435 | list = &ctx->cur_mod->compiled->data; |
| 2436 | } |
| 2437 | if (!(*list)) { |
| 2438 | *list = node; |
| 2439 | } else { |
| 2440 | /* insert at the end of the module's top-level nodes list */ |
| 2441 | (*list)->prev->next = node; |
| 2442 | node->prev = (*list)->prev; |
| 2443 | (*list)->prev = node; |
| 2444 | } |
| 2445 | |
| 2446 | /* check the name uniqueness on top-level */ |
| 2447 | if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) { |
| 2448 | return LY_EEXIST; |
| 2449 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2450 | } |
| 2451 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2452 | return LY_SUCCESS; |
| 2453 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2454 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2455 | /** |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 2456 | * @brief Set config and operation flags for a node. |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2457 | * |
| 2458 | * @param[in] ctx Compile context. |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 2459 | * @param[in] node Compiled node flags to set. |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2460 | * @return LY_ERR value. |
| 2461 | */ |
| 2462 | static LY_ERR |
Radek Krejci | 91531e1 | 2021-02-08 19:57:54 +0100 | [diff] [blame] | 2463 | lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node) |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2464 | { |
| 2465 | /* case never has any explicit config */ |
| 2466 | assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2467 | |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 2468 | if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) { |
Radek Krejci | 91531e1 | 2021-02-08 19:57:54 +0100 | [diff] [blame] | 2469 | /* ignore config statements inside Notification/RPC/action/... data */ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2470 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2471 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
| 2472 | /* config not explicitly set, inherit it from parent */ |
Radek Krejci | 91531e1 | 2021-02-08 19:57:54 +0100 | [diff] [blame] | 2473 | if (node->parent) { |
| 2474 | node->flags |= node->parent->flags & LYS_CONFIG_MASK; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2475 | } else { |
| 2476 | /* default is config true */ |
| 2477 | node->flags |= LYS_CONFIG_W; |
| 2478 | } |
| 2479 | } else { |
| 2480 | /* config set explicitly */ |
| 2481 | node->flags |= LYS_SET_CONFIG; |
| 2482 | } |
| 2483 | |
Radek Krejci | 91531e1 | 2021-02-08 19:57:54 +0100 | [diff] [blame] | 2484 | if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 2485 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node."); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2486 | return LY_EVALID; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2487 | } |
| 2488 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2489 | return LY_SUCCESS; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2490 | } |
| 2491 | |
Radek Krejci | 91531e1 | 2021-02-08 19:57:54 +0100 | [diff] [blame] | 2492 | /** |
| 2493 | * @brief Set various flags of the compiled nodes |
| 2494 | * |
| 2495 | * @param[in] ctx Compile context. |
| 2496 | * @param[in] node Compiled node where the flags will be set. |
| 2497 | * @param[in] uses_status If the node is being placed instead of uses, here we have the uses's status value (as node's flags). |
| 2498 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 2499 | */ |
| 2500 | static LY_ERR |
| 2501 | lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t uses_status) |
| 2502 | { |
| 2503 | /* inherit config flags */ |
| 2504 | LY_CHECK_RET(lys_compile_config(ctx, node)); |
| 2505 | |
| 2506 | /* status - it is not inherited by specification, but it does not make sense to have |
| 2507 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
Michal Vasko | dfd254d | 2021-06-24 09:24:59 +0200 | [diff] [blame] | 2508 | LY_CHECK_RET(lys_compile_status(ctx, &node->flags, node->name, uses_status ? uses_status : |
| 2509 | (node->parent ? node->parent->flags : 0), uses_status ? "<uses>" : (node->parent ? node->parent->name : NULL), |
| 2510 | !uses_status)); |
Radek Krejci | 91531e1 | 2021-02-08 19:57:54 +0100 | [diff] [blame] | 2511 | |
| 2512 | /* other flags */ |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 2513 | if ((ctx->compile_opts & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) { |
Radek Krejci | 91531e1 | 2021-02-08 19:57:54 +0100 | [diff] [blame] | 2514 | node->flags |= LYS_IS_INPUT; |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 2515 | } else if ((ctx->compile_opts & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) { |
Radek Krejci | 91531e1 | 2021-02-08 19:57:54 +0100 | [diff] [blame] | 2516 | node->flags |= LYS_IS_OUTPUT; |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 2517 | } else if ((ctx->compile_opts & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) { |
Radek Krejci | 91531e1 | 2021-02-08 19:57:54 +0100 | [diff] [blame] | 2518 | node->flags |= LYS_IS_NOTIF; |
| 2519 | } |
| 2520 | |
| 2521 | return LY_SUCCESS; |
| 2522 | } |
| 2523 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2524 | LY_ERR |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2525 | lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status, |
| 2526 | LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *), |
| 2527 | struct lysc_node *node, struct ly_set *child_set) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2528 | { |
| 2529 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 2530 | ly_bool not_supported, enabled; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2531 | struct lysp_node *dev_pnode = NULL; |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 2532 | struct lysp_when *pwhen = NULL; |
Michal Vasko | e02e740 | 2021-07-23 08:29:28 +0200 | [diff] [blame] | 2533 | uint32_t prev_opts = ctx->compile_opts; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2534 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2535 | node->nodetype = pnode->nodetype; |
| 2536 | node->module = ctx->cur_mod; |
Radek Krejci | 91531e1 | 2021-02-08 19:57:54 +0100 | [diff] [blame] | 2537 | node->parent = parent; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2538 | node->prev = node; |
aPiecek | 9922ea9 | 2021-04-12 07:59:20 +0200 | [diff] [blame] | 2539 | node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2540 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2541 | /* compile any deviations for this node */ |
| 2542 | LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, ¬_supported), error); |
Michal Vasko | e02e740 | 2021-07-23 08:29:28 +0200 | [diff] [blame] | 2543 | if (not_supported && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) { |
| 2544 | /* if not supported, keep it just like disabled nodes by if-feature */ |
| 2545 | ly_set_add(&ctx->unres->disabled, node, 1, NULL); |
| 2546 | ctx->compile_opts |= LYS_COMPILE_DISABLED; |
| 2547 | } |
| 2548 | if (dev_pnode) { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2549 | pnode = dev_pnode; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2550 | } |
| 2551 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2552 | node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK; |
Michal Vasko | dfd254d | 2021-06-24 09:24:59 +0200 | [diff] [blame] | 2553 | DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error); |
| 2554 | DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error); |
| 2555 | DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2556 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 2557 | /* if-features */ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2558 | LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error); |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 2559 | if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) { |
Michal Vasko | 30ab8e7 | 2021-04-19 12:47:52 +0200 | [diff] [blame] | 2560 | ly_set_add(&ctx->unres->disabled, node, 1, NULL); |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 2561 | ctx->compile_opts |= LYS_COMPILE_DISABLED; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 2562 | } |
| 2563 | |
Radek Krejci | 91531e1 | 2021-02-08 19:57:54 +0100 | [diff] [blame] | 2564 | /* config, status and other flags */ |
| 2565 | ret = lys_compile_node_flags(ctx, node, uses_status); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2566 | LY_CHECK_GOTO(ret, error); |
| 2567 | |
| 2568 | /* list ordering */ |
| 2569 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 2570 | if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 2571 | LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).", |
Radek Krejci | 91531e1 | 2021-02-08 19:57:54 +0100 | [diff] [blame] | 2572 | (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" : |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 2573 | (ctx->compile_opts & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2574 | node->flags &= ~LYS_ORDBY_MASK; |
| 2575 | node->flags |= LYS_ORDBY_SYSTEM; |
| 2576 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 2577 | /* default ordering is system */ |
| 2578 | node->flags |= LYS_ORDBY_SYSTEM; |
| 2579 | } |
| 2580 | } |
| 2581 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2582 | /* insert into parent's children/compiled module (we can no longer free the node separately on error) */ |
| 2583 | LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup); |
| 2584 | |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 2585 | if ((pwhen = lysp_node_when(pnode))) { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2586 | /* compile when */ |
Michal Vasko | dfd254d | 2021-06-24 09:24:59 +0200 | [diff] [blame] | 2587 | ret = lys_compile_when(ctx, pwhen, pnode->flags, node, lysc_data_node(node), node, NULL); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2588 | LY_CHECK_GOTO(ret, cleanup); |
| 2589 | } |
| 2590 | |
| 2591 | /* connect any augments */ |
| 2592 | LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup); |
| 2593 | |
| 2594 | /* nodetype-specific part */ |
| 2595 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup); |
| 2596 | |
| 2597 | /* final compilation tasks that require the node to be connected */ |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 2598 | COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2599 | if (node->flags & LYS_MAND_TRUE) { |
| 2600 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 2601 | lys_compile_mandatory_parents(parent, 1); |
| 2602 | } |
| 2603 | |
| 2604 | if (child_set) { |
| 2605 | /* add the new node into set */ |
| 2606 | LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup); |
| 2607 | } |
| 2608 | |
| 2609 | goto cleanup; |
| 2610 | |
| 2611 | error: |
| 2612 | lysc_node_free(ctx->ctx, node, 0); |
| 2613 | cleanup: |
| 2614 | if (ret && dev_pnode) { |
| 2615 | LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed."); |
| 2616 | } |
Michal Vasko | e02e740 | 2021-07-23 08:29:28 +0200 | [diff] [blame] | 2617 | ctx->compile_opts = prev_opts; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2618 | lysp_dev_node_free(ctx->ctx, dev_pnode); |
| 2619 | return ret; |
| 2620 | } |
| 2621 | |
| 2622 | /** |
| 2623 | * @brief Compile parsed action's input/output node information. |
| 2624 | * @param[in] ctx Compile context |
| 2625 | * @param[in] pnode Parsed inout node. |
| 2626 | * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information |
| 2627 | * is enriched with the inout-specific information. |
| 2628 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2629 | */ |
| 2630 | LY_ERR |
| 2631 | lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
| 2632 | { |
| 2633 | LY_ERR ret = LY_SUCCESS; |
| 2634 | struct lysp_node *child_p; |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 2635 | uint32_t prev_options = ctx->compile_opts; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2636 | |
| 2637 | struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode; |
| 2638 | struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node; |
| 2639 | |
| 2640 | COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done); |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 2641 | COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done); |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 2642 | ctx->compile_opts |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2643 | |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 2644 | LY_LIST_FOR(inout_p->child, child_p) { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2645 | LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done); |
| 2646 | } |
| 2647 | |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 2648 | ctx->compile_opts = prev_options; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2649 | |
| 2650 | done: |
| 2651 | return ret; |
| 2652 | } |
| 2653 | |
| 2654 | /** |
| 2655 | * @brief Compile parsed action node information. |
| 2656 | * @param[in] ctx Compile context |
| 2657 | * @param[in] pnode Parsed action node. |
| 2658 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2659 | * is enriched with the action-specific information. |
| 2660 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2661 | */ |
| 2662 | LY_ERR |
| 2663 | lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
| 2664 | { |
| 2665 | LY_ERR ret; |
| 2666 | struct lysp_node_action *action_p = (struct lysp_node_action *)pnode; |
| 2667 | struct lysc_node_action *action = (struct lysc_node_action *)node; |
| 2668 | struct lysp_node_action_inout *input, implicit_input = { |
| 2669 | .nodetype = LYS_INPUT, |
| 2670 | .name = "input", |
| 2671 | .parent = pnode, |
| 2672 | }; |
| 2673 | struct lysp_node_action_inout *output, implicit_output = { |
| 2674 | .nodetype = LYS_OUTPUT, |
| 2675 | .name = "output", |
| 2676 | .parent = pnode, |
| 2677 | }; |
| 2678 | |
Michal Vasko | e17ff5f | 2021-01-29 17:23:03 +0100 | [diff] [blame] | 2679 | /* input */ |
Radek Krejci | a601699 | 2021-03-03 10:13:41 +0100 | [diff] [blame] | 2680 | lysc_update_path(ctx, action->module, "input"); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2681 | if (action_p->input.nodetype == LYS_UNKNOWN) { |
| 2682 | input = &implicit_input; |
| 2683 | } else { |
| 2684 | input = &action_p->input; |
| 2685 | } |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 2686 | ret = lys_compile_node_(ctx, &input->node, &action->node, 0, lys_compile_node_action_inout, &action->input.node, NULL); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2687 | lysc_update_path(ctx, NULL, NULL); |
| 2688 | LY_CHECK_GOTO(ret, done); |
| 2689 | |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 2690 | /* add must(s) to unres */ |
| 2691 | ret = lysc_unres_must_add(ctx, &action->input.node, &input->node); |
| 2692 | LY_CHECK_GOTO(ret, done); |
| 2693 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2694 | /* output */ |
Radek Krejci | a601699 | 2021-03-03 10:13:41 +0100 | [diff] [blame] | 2695 | lysc_update_path(ctx, action->module, "output"); |
Michal Vasko | 9149efd | 2021-01-29 11:16:59 +0100 | [diff] [blame] | 2696 | if (action_p->output.nodetype == LYS_UNKNOWN) { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2697 | output = &implicit_output; |
| 2698 | } else { |
| 2699 | output = &action_p->output; |
| 2700 | } |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 2701 | ret = lys_compile_node_(ctx, &output->node, &action->node, 0, lys_compile_node_action_inout, &action->output.node, NULL); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2702 | lysc_update_path(ctx, NULL, NULL); |
| 2703 | LY_CHECK_GOTO(ret, done); |
| 2704 | |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 2705 | /* add must(s) to unres */ |
| 2706 | ret = lysc_unres_must_add(ctx, &action->output.node, &output->node); |
| 2707 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2708 | |
| 2709 | done: |
| 2710 | return ret; |
| 2711 | } |
| 2712 | |
| 2713 | /** |
| 2714 | * @brief Compile parsed action node information. |
| 2715 | * @param[in] ctx Compile context |
| 2716 | * @param[in] pnode Parsed action node. |
| 2717 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2718 | * is enriched with the action-specific information. |
| 2719 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2720 | */ |
| 2721 | LY_ERR |
| 2722 | lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
| 2723 | { |
| 2724 | LY_ERR ret = LY_SUCCESS; |
| 2725 | struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode; |
| 2726 | struct lysc_node_notif *notif = (struct lysc_node_notif *)node; |
| 2727 | struct lysp_node *child_p; |
| 2728 | |
| 2729 | COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done); |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 2730 | |
| 2731 | /* add must(s) to unres */ |
| 2732 | ret = lysc_unres_must_add(ctx, node, pnode); |
| 2733 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2734 | |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 2735 | LY_LIST_FOR(notif_p->child, child_p) { |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 2736 | ret = lys_compile_node(ctx, child_p, node, 0, NULL); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2737 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2738 | } |
| 2739 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2740 | done: |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2741 | return ret; |
| 2742 | } |
| 2743 | |
| 2744 | /** |
| 2745 | * @brief Compile parsed container node information. |
| 2746 | * @param[in] ctx Compile context |
| 2747 | * @param[in] pnode Parsed container node. |
| 2748 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2749 | * is enriched with the container-specific information. |
| 2750 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2751 | */ |
| 2752 | static LY_ERR |
| 2753 | lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
| 2754 | { |
| 2755 | struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode; |
| 2756 | struct lysc_node_container *cont = (struct lysc_node_container *)node; |
| 2757 | struct lysp_node *child_p; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2758 | LY_ERR ret = LY_SUCCESS; |
| 2759 | |
| 2760 | if (cont_p->presence) { |
Michal Vasko | e16c7b7 | 2021-02-26 10:39:06 +0100 | [diff] [blame] | 2761 | /* presence container */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2762 | cont->flags |= LYS_PRESENCE; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2763 | } |
| 2764 | |
| 2765 | /* more cases when the container has meaning but is kept NP for convenience: |
| 2766 | * - when condition |
| 2767 | * - direct child action/notification |
| 2768 | */ |
| 2769 | |
| 2770 | LY_LIST_FOR(cont_p->child, child_p) { |
| 2771 | ret = lys_compile_node(ctx, child_p, node, 0, NULL); |
| 2772 | LY_CHECK_GOTO(ret, done); |
| 2773 | } |
| 2774 | |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 2775 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done); |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 2776 | |
| 2777 | /* add must(s) to unres */ |
| 2778 | ret = lysc_unres_must_add(ctx, node, pnode); |
| 2779 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 2780 | |
| 2781 | LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) { |
| 2782 | ret = lys_compile_node(ctx, child_p, node, 0, NULL); |
| 2783 | LY_CHECK_GOTO(ret, done); |
| 2784 | } |
| 2785 | LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) { |
| 2786 | ret = lys_compile_node(ctx, child_p, node, 0, NULL); |
| 2787 | LY_CHECK_GOTO(ret, done); |
| 2788 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2789 | |
| 2790 | done: |
| 2791 | return ret; |
| 2792 | } |
| 2793 | |
Michal Vasko | 7224488 | 2021-01-12 15:21:05 +0100 | [diff] [blame] | 2794 | /** |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2795 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 2796 | * @param[in] ctx Compile context. |
| 2797 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 2798 | * @param[in] type_p Parsed type to compile. |
| 2799 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 2800 | * @return LY_ERR value. |
| 2801 | */ |
| 2802 | static LY_ERR |
| 2803 | lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, |
| 2804 | struct lysc_node_leaf *leaf) |
| 2805 | { |
| 2806 | struct lysp_qname *dflt; |
Michal Vasko | f4fa90d | 2021-11-11 15:05:19 +0100 | [diff] [blame] | 2807 | struct lysc_type **t; |
| 2808 | LY_ARRAY_COUNT_TYPE u, count; |
| 2809 | ly_bool in_unres = 0; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2810 | |
Michal Vasko | a99b357 | 2021-02-01 11:54:58 +0100 | [diff] [blame] | 2811 | LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2812 | leaf->units ? NULL : &leaf->units, &dflt)); |
| 2813 | |
| 2814 | /* store default value, if any */ |
| 2815 | if (dflt && !(leaf->flags & LYS_SET_DFLT)) { |
| 2816 | LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt)); |
| 2817 | } |
| 2818 | |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 2819 | /* store leafref(s) to be resolved */ |
| 2820 | LY_CHECK_RET(lysc_unres_leafref_add(ctx, leaf, type_p->pmod)); |
| 2821 | |
Michal Vasko | f4fa90d | 2021-11-11 15:05:19 +0100 | [diff] [blame] | 2822 | /* type-specific checks */ |
| 2823 | if (leaf->type->basetype == LY_TYPE_UNION) { |
| 2824 | t = ((struct lysc_type_union *)leaf->type)->types; |
| 2825 | count = LY_ARRAY_COUNT(t); |
| 2826 | } else { |
| 2827 | t = &leaf->type; |
| 2828 | count = 1; |
| 2829 | } |
| 2830 | for (u = 0; u < count; ++u) { |
| 2831 | if (t[u]->basetype == LY_TYPE_EMPTY) { |
| 2832 | if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) { |
| 2833 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 2834 | return LY_EVALID; |
| 2835 | } |
| 2836 | } else if (!in_unres && ((t[u]->basetype == LY_TYPE_BITS) || (t[u]->basetype == LY_TYPE_ENUM))) { |
| 2837 | /* store in unres for all disabled bits/enums to be removed */ |
| 2838 | LY_CHECK_RET(lysc_unres_bitenum_add(ctx, leaf)); |
| 2839 | in_unres = 1; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | |
| 2843 | return LY_SUCCESS; |
| 2844 | } |
| 2845 | |
| 2846 | /** |
| 2847 | * @brief Compile parsed leaf node information. |
| 2848 | * @param[in] ctx Compile context |
| 2849 | * @param[in] pnode Parsed leaf node. |
| 2850 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2851 | * is enriched with the leaf-specific information. |
| 2852 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2853 | */ |
| 2854 | static LY_ERR |
| 2855 | lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
| 2856 | { |
| 2857 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode; |
| 2858 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2859 | LY_ERR ret = LY_SUCCESS; |
| 2860 | |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 2861 | COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done); |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 2862 | |
| 2863 | /* add must(s) to unres */ |
| 2864 | ret = lysc_unres_must_add(ctx, node, pnode); |
| 2865 | LY_CHECK_GOTO(ret, done); |
| 2866 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2867 | if (leaf_p->units) { |
| 2868 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done); |
| 2869 | leaf->flags |= LYS_SET_UNITS; |
| 2870 | } |
| 2871 | |
| 2872 | /* compile type */ |
| 2873 | ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf); |
| 2874 | LY_CHECK_GOTO(ret, done); |
| 2875 | |
| 2876 | /* store/update default value */ |
| 2877 | if (leaf_p->dflt.str) { |
| 2878 | LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt)); |
| 2879 | leaf->flags |= LYS_SET_DFLT; |
| 2880 | } |
| 2881 | |
| 2882 | /* checks */ |
| 2883 | if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) { |
Michal Vasko | 6b8c510 | 2021-10-19 11:29:32 +0200 | [diff] [blame] | 2884 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory leaf with a default value."); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2885 | return LY_EVALID; |
| 2886 | } |
| 2887 | |
| 2888 | done: |
| 2889 | return ret; |
| 2890 | } |
| 2891 | |
| 2892 | /** |
| 2893 | * @brief Compile parsed leaf-list node information. |
| 2894 | * @param[in] ctx Compile context |
| 2895 | * @param[in] pnode Parsed leaf-list node. |
| 2896 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2897 | * is enriched with the leaf-list-specific information. |
| 2898 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2899 | */ |
| 2900 | static LY_ERR |
| 2901 | lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
| 2902 | { |
| 2903 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode; |
| 2904 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2905 | LY_ERR ret = LY_SUCCESS; |
| 2906 | |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 2907 | COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done); |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 2908 | |
| 2909 | /* add must(s) to unres */ |
| 2910 | ret = lysc_unres_must_add(ctx, node, pnode); |
| 2911 | LY_CHECK_GOTO(ret, done); |
| 2912 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2913 | if (llist_p->units) { |
| 2914 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done); |
| 2915 | llist->flags |= LYS_SET_UNITS; |
| 2916 | } |
| 2917 | |
| 2918 | /* compile type */ |
| 2919 | ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist); |
| 2920 | LY_CHECK_GOTO(ret, done); |
| 2921 | |
| 2922 | /* store/update default values */ |
| 2923 | if (llist_p->dflts) { |
| 2924 | if (ctx->pmod->version < LYS_VERSION_1_1) { |
Michal Vasko | 6b8c510 | 2021-10-19 11:29:32 +0200 | [diff] [blame] | 2925 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list default values are allowed only in YANG 1.1 modules."); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2926 | return LY_EVALID; |
| 2927 | } |
| 2928 | |
| 2929 | LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done); |
| 2930 | llist->flags |= LYS_SET_DFLT; |
| 2931 | } |
| 2932 | |
| 2933 | llist->min = llist_p->min; |
| 2934 | if (llist->min) { |
| 2935 | llist->flags |= LYS_MAND_TRUE; |
| 2936 | } |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2937 | llist->max = llist_p->max ? llist_p->max : UINT32_MAX; |
| 2938 | |
| 2939 | if (llist->flags & LYS_CONFIG_R) { |
| 2940 | /* state leaf-list is always ordered-by user */ |
| 2941 | llist->flags &= ~LYS_ORDBY_SYSTEM; |
| 2942 | llist->flags |= LYS_ORDBY_USER; |
| 2943 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2944 | |
| 2945 | /* checks */ |
| 2946 | if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2947 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "The default statement is present on leaf-list with a nonzero min-elements."); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2948 | return LY_EVALID; |
| 2949 | } |
| 2950 | |
| 2951 | if (llist->min > llist->max) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2952 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.", |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2953 | llist->min, llist->max); |
| 2954 | return LY_EVALID; |
| 2955 | } |
| 2956 | |
| 2957 | done: |
| 2958 | return ret; |
| 2959 | } |
| 2960 | |
Michal Vasko | 9716ca7 | 2021-12-14 10:31:04 +0100 | [diff] [blame] | 2961 | /** |
| 2962 | * @brief Find the node according to the given descendant/absolute schema nodeid. |
| 2963 | * Used in unique, refine and augment statements. |
| 2964 | * |
| 2965 | * @param[in] ctx Compile context |
| 2966 | * @param[in] nodeid Descendant-schema-nodeid (according to the YANG grammar) |
| 2967 | * @param[in] nodeid_len Length of the given nodeid, if it is not NULL-terminated string. |
| 2968 | * @param[in] ctx_node Context node for a relative nodeid. |
Michal Vasko | 9716ca7 | 2021-12-14 10:31:04 +0100 | [diff] [blame] | 2969 | * @param[in] format Format of any prefixes. |
| 2970 | * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix). |
| 2971 | * @param[in] nodetype Optional (can be 0) restriction for target's nodetype. If target exists, but does not match |
| 2972 | * the given nodetype, LY_EDENIED is returned (and target is provided), but no error message is printed. |
| 2973 | * The value can be even an ORed value to allow multiple nodetypes. |
| 2974 | * @param[out] target Found target node if any. |
| 2975 | * @param[out] result_flag Output parameter to announce if the schema nodeid goes through the action's input/output or a Notification. |
| 2976 | * The LYSC_OPT_RPC_INPUT, LYSC_OPT_RPC_OUTPUT and LYSC_OPT_NOTIFICATION are used as flags. |
| 2977 | * @return LY_ERR values - LY_ENOTFOUND, LY_EVALID, LY_EDENIED or LY_SUCCESS. |
| 2978 | */ |
| 2979 | static LY_ERR |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2980 | lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node, |
Michal Vasko | c486d21 | 2021-12-16 15:41:30 +0100 | [diff] [blame] | 2981 | LY_VALUE_FORMAT format, void *prefix_data, uint16_t nodetype, const struct lysc_node **target, uint16_t *result_flag) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2982 | { |
| 2983 | LY_ERR ret = LY_EVALID; |
| 2984 | const char *name, *prefix, *id; |
| 2985 | size_t name_len, prefix_len; |
Radek Krejci | 2b18bf1 | 2020-11-06 11:20:20 +0100 | [diff] [blame] | 2986 | const struct lys_module *mod = NULL; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 2987 | const char *nodeid_type; |
| 2988 | uint32_t getnext_extra_flag = 0; |
| 2989 | uint16_t current_nodetype = 0; |
| 2990 | |
| 2991 | assert(nodeid); |
| 2992 | assert(target); |
| 2993 | assert(result_flag); |
| 2994 | *target = NULL; |
| 2995 | *result_flag = 0; |
| 2996 | |
| 2997 | id = nodeid; |
| 2998 | |
| 2999 | if (ctx_node) { |
| 3000 | /* descendant-schema-nodeid */ |
| 3001 | nodeid_type = "descendant"; |
| 3002 | |
| 3003 | if (*id == '/') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3004 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3005 | "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 3006 | (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3007 | return LY_EVALID; |
| 3008 | } |
| 3009 | } else { |
| 3010 | /* absolute-schema-nodeid */ |
| 3011 | nodeid_type = "absolute"; |
| 3012 | |
| 3013 | if (*id != '/') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3014 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3015 | "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 3016 | (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3017 | return LY_EVALID; |
| 3018 | } |
| 3019 | ++id; |
| 3020 | } |
| 3021 | |
| 3022 | while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) { |
| 3023 | if (prefix) { |
| 3024 | mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data); |
| 3025 | if (!mod) { |
| 3026 | /* module must always be found */ |
| 3027 | assert(prefix); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3028 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3029 | "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 3030 | nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3031 | return LY_ENOTFOUND; |
| 3032 | } |
| 3033 | } else { |
| 3034 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 3035 | case LY_VALUE_SCHEMA: |
| 3036 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3037 | /* use the current module */ |
Michal Vasko | c486d21 | 2021-12-16 15:41:30 +0100 | [diff] [blame] | 3038 | mod = ctx->cur_mod; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3039 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 3040 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 3041 | case LY_VALUE_LYB: |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3042 | if (!ctx_node) { |
| 3043 | LOGINT_RET(ctx->ctx); |
| 3044 | } |
| 3045 | /* inherit the module of the previous context node */ |
| 3046 | mod = ctx_node->module; |
| 3047 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 3048 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 3049 | case LY_VALUE_XML: |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3050 | /* not really defined */ |
| 3051 | LOGINT_RET(ctx->ctx); |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 3056 | /* move through input/output manually */ |
| 3057 | if (mod != ctx_node->module) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 3058 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", |
| 3059 | nodeid_type, (int)(id - nodeid), nodeid); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3060 | return LY_ENOTFOUND; |
| 3061 | } |
| 3062 | if (!ly_strncmp("input", name, name_len)) { |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3063 | ctx_node = &((struct lysc_node_action *)ctx_node)->input.node; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3064 | } else if (!ly_strncmp("output", name, name_len)) { |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3065 | ctx_node = &((struct lysc_node_action *)ctx_node)->output.node; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3066 | getnext_extra_flag = LYS_GETNEXT_OUTPUT; |
| 3067 | } else { |
| 3068 | /* only input or output is valid */ |
| 3069 | ctx_node = NULL; |
| 3070 | } |
| 3071 | } else { |
| 3072 | ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0, |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 3073 | getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3074 | getnext_extra_flag = 0; |
| 3075 | } |
| 3076 | if (!ctx_node) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 3077 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.", |
| 3078 | nodeid_type, (int)(id - nodeid), nodeid); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3079 | return LY_ENOTFOUND; |
| 3080 | } |
| 3081 | current_nodetype = ctx_node->nodetype; |
| 3082 | |
| 3083 | if (current_nodetype == LYS_NOTIF) { |
| 3084 | (*result_flag) |= LYS_COMPILE_NOTIFICATION; |
| 3085 | } else if (current_nodetype == LYS_INPUT) { |
| 3086 | (*result_flag) |= LYS_COMPILE_RPC_INPUT; |
| 3087 | } else if (current_nodetype == LYS_OUTPUT) { |
| 3088 | (*result_flag) |= LYS_COMPILE_RPC_OUTPUT; |
| 3089 | } |
| 3090 | |
| 3091 | if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) { |
| 3092 | break; |
| 3093 | } |
| 3094 | if (*id != '/') { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3095 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3096 | "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 3097 | nodeid_type, (int)(id - nodeid + 1), nodeid); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3098 | return LY_EVALID; |
| 3099 | } |
| 3100 | ++id; |
| 3101 | } |
| 3102 | |
| 3103 | if (ret == LY_SUCCESS) { |
| 3104 | *target = ctx_node; |
| 3105 | if (nodetype && !(current_nodetype & nodetype)) { |
| 3106 | return LY_EDENIED; |
| 3107 | } |
| 3108 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3109 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3110 | "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 3111 | nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3112 | } |
| 3113 | |
| 3114 | return ret; |
| 3115 | } |
| 3116 | |
| 3117 | /** |
| 3118 | * @brief Compile information about list's uniques. |
| 3119 | * @param[in] ctx Compile context. |
| 3120 | * @param[in] uniques Sized array list of unique statements. |
| 3121 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3122 | * @return LY_ERR value. |
| 3123 | */ |
| 3124 | static LY_ERR |
| 3125 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list) |
| 3126 | { |
| 3127 | LY_ERR ret = LY_SUCCESS; |
| 3128 | struct lysc_node_leaf **key, ***unique; |
| 3129 | struct lysc_node *parent; |
| 3130 | const char *keystr, *delim; |
| 3131 | size_t len; |
| 3132 | LY_ARRAY_COUNT_TYPE v; |
| 3133 | int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */ |
| 3134 | uint16_t flags; |
| 3135 | |
| 3136 | LY_ARRAY_FOR(uniques, v) { |
| 3137 | config = -1; |
| 3138 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3139 | keystr = uniques[v].str; |
| 3140 | while (keystr) { |
| 3141 | delim = strpbrk(keystr, " \t\n"); |
| 3142 | if (delim) { |
| 3143 | len = delim - keystr; |
| 3144 | while (isspace(*delim)) { |
| 3145 | ++delim; |
| 3146 | } |
| 3147 | } else { |
| 3148 | len = strlen(keystr); |
| 3149 | } |
| 3150 | |
| 3151 | /* unique node must be present */ |
| 3152 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Michal Vasko | c486d21 | 2021-12-16 15:41:30 +0100 | [diff] [blame] | 3153 | ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, LY_VALUE_SCHEMA, (void *)uniques[v].mod, |
| 3154 | LYS_LEAF, (const struct lysc_node **)key, &flags); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3155 | if (ret != LY_SUCCESS) { |
| 3156 | if (ret == LY_EDENIED) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3157 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3158 | "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 3159 | (int)len, keystr, lys_nodetype2str((*key)->nodetype)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3160 | } |
| 3161 | return LY_EVALID; |
| 3162 | } else if (flags) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3163 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3164 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 3165 | (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action"); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3166 | return LY_EVALID; |
| 3167 | } |
| 3168 | |
| 3169 | /* all referenced leafs must be of the same config type */ |
| 3170 | if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) || |
| 3171 | (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3172 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3173 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str); |
| 3174 | return LY_EVALID; |
| 3175 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3176 | config = 1; |
| 3177 | } else { /* LYS_CONFIG_R */ |
| 3178 | config = 0; |
| 3179 | } |
| 3180 | |
| 3181 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3182 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3183 | if (parent->nodetype == LYS_LIST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3184 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3185 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name); |
| 3186 | return LY_EVALID; |
| 3187 | } |
| 3188 | } |
| 3189 | |
| 3190 | /* check status */ |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 3191 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, uniques[v].mod->mod, list->name, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3192 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3193 | |
| 3194 | /* mark leaf as unique */ |
| 3195 | (*key)->flags |= LYS_UNIQUE; |
| 3196 | |
| 3197 | /* next unique value in line */ |
| 3198 | keystr = delim; |
| 3199 | } |
| 3200 | /* next unique definition */ |
| 3201 | } |
| 3202 | |
| 3203 | return LY_SUCCESS; |
| 3204 | } |
| 3205 | |
| 3206 | /** |
| 3207 | * @brief Compile parsed list node information. |
| 3208 | * @param[in] ctx Compile context |
| 3209 | * @param[in] pnode Parsed list node. |
| 3210 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3211 | * is enriched with the list-specific information. |
| 3212 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3213 | */ |
| 3214 | static LY_ERR |
| 3215 | lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
| 3216 | { |
| 3217 | struct lysp_node_list *list_p = (struct lysp_node_list *)pnode; |
| 3218 | struct lysc_node_list *list = (struct lysc_node_list *)node; |
| 3219 | struct lysp_node *child_p; |
| 3220 | struct lysc_node_leaf *key, *prev_key = NULL; |
| 3221 | size_t len; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3222 | const char *keystr, *delim; |
| 3223 | LY_ERR ret = LY_SUCCESS; |
| 3224 | |
| 3225 | list->min = list_p->min; |
| 3226 | if (list->min) { |
| 3227 | list->flags |= LYS_MAND_TRUE; |
| 3228 | } |
| 3229 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3230 | |
| 3231 | LY_LIST_FOR(list_p->child, child_p) { |
| 3232 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL)); |
| 3233 | } |
| 3234 | |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 3235 | COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done); |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 3236 | |
| 3237 | /* add must(s) to unres */ |
| 3238 | ret = lysc_unres_must_add(ctx, node, pnode); |
| 3239 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3240 | |
| 3241 | /* keys */ |
| 3242 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3243 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3244 | return LY_EVALID; |
| 3245 | } |
| 3246 | |
| 3247 | /* find all the keys (must be direct children) */ |
| 3248 | keystr = list_p->key; |
| 3249 | if (!keystr) { |
| 3250 | /* keyless list */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 3251 | list->flags &= ~LYS_ORDBY_SYSTEM; |
| 3252 | list->flags |= LYS_KEYLESS | LYS_ORDBY_USER; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3253 | } |
| 3254 | while (keystr) { |
| 3255 | delim = strpbrk(keystr, " \t\n"); |
| 3256 | if (delim) { |
| 3257 | len = delim - keystr; |
| 3258 | while (isspace(*delim)) { |
| 3259 | ++delim; |
| 3260 | } |
| 3261 | } else { |
| 3262 | len = strlen(keystr); |
| 3263 | } |
| 3264 | |
| 3265 | /* key node must be present */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 3266 | key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE); |
| 3267 | if (!key) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 3268 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3269 | return LY_EVALID; |
| 3270 | } |
| 3271 | /* keys must be unique */ |
| 3272 | if (key->flags & LYS_KEY) { |
| 3273 | /* the node was already marked as a key */ |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 3274 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3275 | return LY_EVALID; |
| 3276 | } |
| 3277 | |
Radek Krejci | a601699 | 2021-03-03 10:13:41 +0100 | [diff] [blame] | 3278 | lysc_update_path(ctx, list->module, key->name); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3279 | /* key must have the same config flag as the list itself */ |
| 3280 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Michal Vasko | 6b8c510 | 2021-10-19 11:29:32 +0200 | [diff] [blame] | 3281 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of a configuration list must not be a state leaf."); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3282 | return LY_EVALID; |
| 3283 | } |
| 3284 | if (ctx->pmod->version < LYS_VERSION_1_1) { |
| 3285 | /* YANG 1.0 denies key to be of empty type */ |
| 3286 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3287 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3288 | "List's key cannot be of \"empty\" type until it is in YANG 1.1 module."); |
| 3289 | return LY_EVALID; |
| 3290 | } |
| 3291 | } else { |
| 3292 | /* when and if-feature are illegal on list keys */ |
| 3293 | if (key->when) { |
Michal Vasko | 6b8c510 | 2021-10-19 11:29:32 +0200 | [diff] [blame] | 3294 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List's key must not have any \"when\" statement."); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3295 | return LY_EVALID; |
| 3296 | } |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 3297 | /* TODO check key, it cannot have any if-features */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | /* check status */ |
| 3301 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3302 | key->flags, key->module, key->name)); |
| 3303 | |
| 3304 | /* ignore default values of the key */ |
| 3305 | if (key->dflt) { |
| 3306 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3307 | lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype); |
| 3308 | free(key->dflt); |
| 3309 | key->dflt = NULL; |
| 3310 | } |
| 3311 | /* mark leaf as key */ |
| 3312 | key->flags |= LYS_KEY; |
| 3313 | |
| 3314 | /* move it to the correct position */ |
| 3315 | if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) { |
| 3316 | /* fix links in closest previous siblings of the key */ |
| 3317 | if (key->next) { |
| 3318 | key->next->prev = key->prev; |
| 3319 | } else { |
| 3320 | /* last child */ |
| 3321 | list->child->prev = key->prev; |
| 3322 | } |
| 3323 | if (key->prev->next) { |
| 3324 | key->prev->next = key->next; |
| 3325 | } |
| 3326 | /* fix links in the key */ |
| 3327 | if (prev_key) { |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3328 | key->prev = &prev_key->node; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3329 | key->next = prev_key->next; |
| 3330 | } else { |
| 3331 | key->prev = list->child->prev; |
| 3332 | key->next = list->child; |
| 3333 | } |
| 3334 | /* fix links in closes future siblings of the key */ |
| 3335 | if (prev_key) { |
| 3336 | if (prev_key->next) { |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3337 | prev_key->next->prev = &key->node; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3338 | } else { |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3339 | list->child->prev = &key->node; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3340 | } |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3341 | prev_key->next = &key->node; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3342 | } else { |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3343 | list->child->prev = &key->node; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3344 | } |
| 3345 | /* fix links in parent */ |
| 3346 | if (!key->prev->next) { |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3347 | list->child = &key->node; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3348 | } |
| 3349 | } |
| 3350 | |
| 3351 | /* next key value */ |
| 3352 | prev_key = key; |
| 3353 | keystr = delim; |
| 3354 | lysc_update_path(ctx, NULL, NULL); |
| 3355 | } |
| 3356 | |
| 3357 | /* uniques */ |
| 3358 | if (list_p->uniques) { |
| 3359 | LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list)); |
| 3360 | } |
| 3361 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3362 | LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) { |
| 3363 | ret = lys_compile_node(ctx, child_p, node, 0, NULL); |
| 3364 | LY_CHECK_GOTO(ret, done); |
| 3365 | } |
| 3366 | LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) { |
| 3367 | ret = lys_compile_node(ctx, child_p, node, 0, NULL); |
| 3368 | LY_CHECK_GOTO(ret, done); |
| 3369 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3370 | |
| 3371 | /* checks */ |
| 3372 | if (list->min > list->max) { |
Michal Vasko | 6b8c510 | 2021-10-19 11:29:32 +0200 | [diff] [blame] | 3373 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.", list->min, list->max); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3374 | return LY_EVALID; |
| 3375 | } |
| 3376 | |
| 3377 | done: |
| 3378 | return ret; |
| 3379 | } |
| 3380 | |
| 3381 | /** |
| 3382 | * @brief Do some checks and set the default choice's case. |
| 3383 | * |
| 3384 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3385 | * |
| 3386 | * @param[in] ctx Compile context. |
| 3387 | * @param[in] dflt Name of the default branch. Can even contain a prefix. |
| 3388 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3389 | * @return LY_ERR value. |
| 3390 | */ |
| 3391 | static LY_ERR |
| 3392 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch) |
| 3393 | { |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3394 | struct lysc_node *iter; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3395 | const struct lys_module *mod; |
| 3396 | const char *prefix = NULL, *name; |
| 3397 | size_t prefix_len = 0; |
| 3398 | |
| 3399 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3400 | name = strchr(dflt->str, ':'); |
| 3401 | if (name) { |
| 3402 | prefix = dflt->str; |
| 3403 | prefix_len = name - prefix; |
| 3404 | ++name; |
| 3405 | } else { |
| 3406 | name = dflt->str; |
| 3407 | } |
| 3408 | if (prefix) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 3409 | mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3410 | if (!mod) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3411 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found " |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 3412 | "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3413 | return LY_EVALID; |
| 3414 | } |
| 3415 | } else { |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3416 | mod = ch->module; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3417 | } |
| 3418 | |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3419 | ch->dflt = (struct lysc_node_case *)lys_find_child(&ch->node, mod, name, 0, LYS_CASE, LYS_GETNEXT_WITHCASE); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3420 | if (!ch->dflt) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3421 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3422 | "Default case \"%s\" not found.", dflt->str); |
| 3423 | return LY_EVALID; |
| 3424 | } |
| 3425 | |
| 3426 | /* no mandatory nodes directly under the default case */ |
| 3427 | LY_LIST_FOR(ch->dflt->child, iter) { |
| 3428 | if (iter->parent != (struct lysc_node *)ch->dflt) { |
| 3429 | break; |
| 3430 | } |
| 3431 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3432 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3433 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str); |
| 3434 | return LY_EVALID; |
| 3435 | } |
| 3436 | } |
| 3437 | |
| 3438 | if (ch->flags & LYS_MAND_TRUE) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3439 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case."); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3440 | return LY_EVALID; |
| 3441 | } |
| 3442 | |
| 3443 | ch->dflt->flags |= LYS_SET_DFLT; |
| 3444 | return LY_SUCCESS; |
| 3445 | } |
| 3446 | |
| 3447 | LY_ERR |
| 3448 | lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node, |
| 3449 | struct ly_set *child_set) |
| 3450 | { |
| 3451 | LY_ERR ret = LY_SUCCESS; |
| 3452 | struct lysp_node *child_p_next = child_p->next; |
| 3453 | struct lysp_node_case *cs_p; |
aPiecek | aa320c9 | 2021-05-21 07:34:24 +0200 | [diff] [blame] | 3454 | struct lysc_node_case *compiled_case; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3455 | |
| 3456 | if (child_p->nodetype == LYS_CASE) { |
| 3457 | /* standard case under choice */ |
| 3458 | ret = lys_compile_node(ctx, child_p, node, 0, child_set); |
| 3459 | } else { |
Michal Vasko | a6cf80a | 2021-06-25 07:42:19 +0200 | [diff] [blame] | 3460 | /* we need the implicit case first, so create a fake parsed (shorthand) case */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3461 | cs_p = calloc(1, sizeof *cs_p); |
| 3462 | cs_p->nodetype = LYS_CASE; |
Michal Vasko | a6cf80a | 2021-06-25 07:42:19 +0200 | [diff] [blame] | 3463 | DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, revert_sh_case); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3464 | cs_p->child = child_p; |
| 3465 | |
| 3466 | /* make the child the only case child */ |
| 3467 | child_p->next = NULL; |
| 3468 | |
| 3469 | /* compile it normally */ |
Michal Vasko | a6cf80a | 2021-06-25 07:42:19 +0200 | [diff] [blame] | 3470 | LY_CHECK_GOTO(ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set), revert_sh_case); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3471 | |
Michal Vasko | a6cf80a | 2021-06-25 07:42:19 +0200 | [diff] [blame] | 3472 | if (((struct lysc_node_choice *)node)->cases) { |
| 3473 | /* get last case node */ |
| 3474 | compiled_case = (struct lysc_node_case *)((struct lysc_node_choice *)node)->cases->prev; |
aPiecek | 082c7dc | 2021-05-20 08:55:07 +0200 | [diff] [blame] | 3475 | |
Michal Vasko | a6cf80a | 2021-06-25 07:42:19 +0200 | [diff] [blame] | 3476 | if (ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED) { |
| 3477 | /* Compiled case node cannot point to his corresponding parsed node |
aPiecek | 8e35138 | 2021-06-25 14:13:55 +0200 | [diff] [blame] | 3478 | * because it exists temporarily. Therefore, it must be set to NULL. |
| 3479 | */ |
Michal Vasko | a6cf80a | 2021-06-25 07:42:19 +0200 | [diff] [blame] | 3480 | compiled_case->priv = NULL; |
| 3481 | } |
aPiecek | aa320c9 | 2021-05-21 07:34:24 +0200 | [diff] [blame] | 3482 | |
Michal Vasko | a6cf80a | 2021-06-25 07:42:19 +0200 | [diff] [blame] | 3483 | /* The status is copied from his child and not from his parent as usual. */ |
| 3484 | if (compiled_case->child) { |
| 3485 | compiled_case->flags &= ~LYS_STATUS_MASK; |
| 3486 | compiled_case->flags |= LYS_STATUS_MASK & compiled_case->child->flags; |
| 3487 | } |
| 3488 | } /* else it was removed by a deviation */ |
aPiecek | aa320c9 | 2021-05-21 07:34:24 +0200 | [diff] [blame] | 3489 | |
Michal Vasko | a6cf80a | 2021-06-25 07:42:19 +0200 | [diff] [blame] | 3490 | revert_sh_case: |
| 3491 | /* free the parsed shorthand case and correct pointers back */ |
aPiecek | aa320c9 | 2021-05-21 07:34:24 +0200 | [diff] [blame] | 3492 | cs_p->child = NULL; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3493 | lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p); |
| 3494 | child_p->next = child_p_next; |
| 3495 | } |
| 3496 | |
| 3497 | return ret; |
| 3498 | } |
| 3499 | |
| 3500 | /** |
| 3501 | * @brief Compile parsed choice node information. |
| 3502 | * |
| 3503 | * @param[in] ctx Compile context |
| 3504 | * @param[in] pnode Parsed choice node. |
| 3505 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3506 | * is enriched with the choice-specific information. |
| 3507 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3508 | */ |
| 3509 | static LY_ERR |
| 3510 | lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
| 3511 | { |
| 3512 | struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode; |
| 3513 | struct lysc_node_choice *ch = (struct lysc_node_choice *)node; |
| 3514 | struct lysp_node *child_p; |
| 3515 | LY_ERR ret = LY_SUCCESS; |
| 3516 | |
| 3517 | assert(node->nodetype == LYS_CHOICE); |
| 3518 | |
| 3519 | LY_LIST_FOR(ch_p->child, child_p) { |
| 3520 | LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL)); |
| 3521 | } |
| 3522 | |
| 3523 | /* default branch */ |
| 3524 | if (ch_p->dflt.str) { |
| 3525 | LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch)); |
| 3526 | } |
| 3527 | |
| 3528 | return ret; |
| 3529 | } |
| 3530 | |
| 3531 | /** |
| 3532 | * @brief Compile parsed anydata or anyxml node information. |
| 3533 | * @param[in] ctx Compile context |
| 3534 | * @param[in] pnode Parsed anydata or anyxml node. |
| 3535 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3536 | * is enriched with the any-specific information. |
| 3537 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3538 | */ |
| 3539 | static LY_ERR |
| 3540 | lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
| 3541 | { |
| 3542 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode; |
| 3543 | struct lysc_node_anydata *any = (struct lysc_node_anydata *)node; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3544 | LY_ERR ret = LY_SUCCESS; |
| 3545 | |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 3546 | COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done); |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 3547 | |
| 3548 | /* add must(s) to unres */ |
| 3549 | ret = lysc_unres_must_add(ctx, node, pnode); |
| 3550 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3551 | |
Radek Krejci | 91531e1 | 2021-02-08 19:57:54 +0100 | [diff] [blame] | 3552 | if (any->flags & LYS_CONFIG_W) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3553 | LOGVRB("Use of %s to define configuration data is not recommended. %s", |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3554 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path); |
| 3555 | } |
| 3556 | done: |
| 3557 | return ret; |
| 3558 | } |
| 3559 | |
| 3560 | /** |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3561 | * @brief Prepare the case structure in choice node for the new data node. |
| 3562 | * |
| 3563 | * It is able to handle implicit as well as explicit cases and the situation when the case has multiple data nodes and the case was already |
| 3564 | * created in the choice when the first child was processed. |
| 3565 | * |
| 3566 | * @param[in] ctx Compile context. |
| 3567 | * @param[in] pnode Node image from the parsed tree. If the case is explicit, it is the LYS_CASE node, but in case of implicit case, |
| 3568 | * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node. |
| 3569 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 3570 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 3571 | * @return The case structure where the child node belongs to, NULL in case of error. Note that the child is not connected into the siblings list, |
| 3572 | * it is linked from the case structure only in case it is its first child. |
| 3573 | */ |
| 3574 | static LY_ERR |
| 3575 | lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node) |
| 3576 | { |
| 3577 | struct lysp_node *child_p; |
| 3578 | struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode; |
| 3579 | |
| 3580 | if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) { |
| 3581 | /* we have to add an implicit case node into the parent choice */ |
| 3582 | } else if (pnode->nodetype == LYS_CASE) { |
| 3583 | /* explicit parent case */ |
| 3584 | LY_LIST_FOR(cs_p->child, child_p) { |
| 3585 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL)); |
| 3586 | } |
| 3587 | } else { |
| 3588 | LOGINT_RET(ctx->ctx); |
| 3589 | } |
| 3590 | |
| 3591 | return LY_SUCCESS; |
| 3592 | } |
| 3593 | |
| 3594 | void |
| 3595 | lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add) |
| 3596 | { |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3597 | const struct lysc_node *iter; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3598 | |
| 3599 | if (add) { /* set flag */ |
| 3600 | for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 3601 | parent = parent->parent) { |
| 3602 | parent->flags |= LYS_MAND_TRUE; |
| 3603 | } |
| 3604 | } else { /* unset flag */ |
| 3605 | for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) { |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 3606 | for (iter = lysc_node_child(parent); iter; iter = iter->next) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3607 | if (iter->flags & LYS_MAND_TRUE) { |
| 3608 | /* there is another mandatory node */ |
| 3609 | return; |
| 3610 | } |
| 3611 | } |
| 3612 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 3613 | parent->flags &= ~LYS_MAND_TRUE; |
| 3614 | } |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | /** |
Radek Krejci | abd33a4 | 2021-01-20 17:18:59 +0100 | [diff] [blame] | 3619 | * @brief Get the grouping with the specified name from given groupings sized array. |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3620 | * @param[in] grps Linked list of groupings. |
Radek Krejci | abd33a4 | 2021-01-20 17:18:59 +0100 | [diff] [blame] | 3621 | * @param[in] name Name of the grouping to find, |
| 3622 | * @return NULL when there is no grouping with the specified name |
| 3623 | * @return Pointer to the grouping of the specified @p name. |
| 3624 | */ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3625 | static struct lysp_node_grp * |
| 3626 | match_grouping(struct lysp_node_grp *grps, const char *name) |
Radek Krejci | abd33a4 | 2021-01-20 17:18:59 +0100 | [diff] [blame] | 3627 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3628 | struct lysp_node_grp *grp; |
Radek Krejci | abd33a4 | 2021-01-20 17:18:59 +0100 | [diff] [blame] | 3629 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3630 | LY_LIST_FOR(grps, grp) { |
| 3631 | if (!strcmp(grp->name, name)) { |
| 3632 | return grp; |
Radek Krejci | abd33a4 | 2021-01-20 17:18:59 +0100 | [diff] [blame] | 3633 | } |
| 3634 | } |
| 3635 | |
| 3636 | return NULL; |
| 3637 | } |
| 3638 | |
| 3639 | /** |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3640 | * @brief Find grouping for a uses. |
| 3641 | * |
| 3642 | * @param[in] ctx Compile context. |
| 3643 | * @param[in] uses_p Parsed uses node. |
| 3644 | * @param[out] gpr_p Found grouping on success. |
| 3645 | * @param[out] grp_pmod Module of @p grp_p on success. |
| 3646 | * @return LY_ERR value. |
| 3647 | */ |
| 3648 | static LY_ERR |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3649 | lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_node_grp **grp_p, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3650 | struct lysp_module **grp_pmod) |
| 3651 | { |
| 3652 | struct lysp_node *pnode; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3653 | struct lysp_node_grp *grp; |
Radek Krejci | abd33a4 | 2021-01-20 17:18:59 +0100 | [diff] [blame] | 3654 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3655 | const char *id, *name, *prefix, *local_pref; |
| 3656 | size_t prefix_len, name_len; |
| 3657 | struct lysp_module *pmod, *found = NULL; |
Michal Vasko | b2d55bf | 2020-11-02 15:42:43 +0100 | [diff] [blame] | 3658 | const struct lys_module *mod; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3659 | |
| 3660 | *grp_p = NULL; |
| 3661 | *grp_pmod = NULL; |
| 3662 | |
| 3663 | /* search for the grouping definition */ |
| 3664 | id = uses_p->name; |
| 3665 | LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID); |
| 3666 | local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix; |
| 3667 | if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) { |
| 3668 | /* current module, search local groupings first */ |
Radek Krejci | abd33a4 | 2021-01-20 17:18:59 +0100 | [diff] [blame] | 3669 | pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3670 | for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3671 | if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) { |
| 3672 | found = ctx->pmod; |
| 3673 | break; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3674 | } |
| 3675 | } |
| 3676 | } else { |
| 3677 | /* foreign module, find it first */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 3678 | mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3679 | if (!mod) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3680 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3681 | "Invalid prefix used for grouping reference.", uses_p->name); |
| 3682 | return LY_EVALID; |
| 3683 | } |
| 3684 | pmod = mod->parsed; |
| 3685 | } |
| 3686 | |
| 3687 | if (!found) { |
| 3688 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | abd33a4 | 2021-01-20 17:18:59 +0100 | [diff] [blame] | 3689 | if ((grp = match_grouping(pmod->groupings, name))) { |
| 3690 | found = pmod; |
| 3691 | } else { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3692 | /* ... and all the submodules */ |
| 3693 | LY_ARRAY_FOR(pmod->includes, u) { |
Radek Krejci | abd33a4 | 2021-01-20 17:18:59 +0100 | [diff] [blame] | 3694 | if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) { |
| 3695 | found = (struct lysp_module *)pmod->includes[u].submodule; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3696 | break; |
| 3697 | } |
| 3698 | } |
| 3699 | } |
| 3700 | } |
| 3701 | if (!found) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3702 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3703 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 3704 | return LY_EVALID; |
| 3705 | } |
| 3706 | |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 3707 | if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) { |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3708 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 3709 | grp->flags |= LYS_USED_GRP; |
| 3710 | } |
| 3711 | |
| 3712 | *grp_p = grp; |
| 3713 | *grp_pmod = found; |
| 3714 | return LY_SUCCESS; |
| 3715 | } |
| 3716 | |
| 3717 | /** |
Michal Vasko | dfd254d | 2021-06-24 09:24:59 +0200 | [diff] [blame] | 3718 | * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function. |
| 3719 | */ |
| 3720 | #define LYS_STATUS_USES LYS_CONFIG_MASK |
| 3721 | |
| 3722 | /** |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3723 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 3724 | * If present, also apply uses's modificators. |
| 3725 | * |
| 3726 | * @param[in] ctx Compile context |
| 3727 | * @param[in] uses_p Parsed uses schema node. |
| 3728 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 3729 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 3730 | * the compile context. |
| 3731 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3732 | */ |
| 3733 | static LY_ERR |
| 3734 | lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set) |
| 3735 | { |
| 3736 | struct lysp_node *pnode; |
| 3737 | struct lysc_node *child; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3738 | struct lysp_node_grp *grp = NULL; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3739 | uint32_t i, grp_stack_count; |
Michal Vasko | 10b6b1c | 2021-07-20 10:43:12 +0200 | [diff] [blame] | 3740 | uint16_t uses_flags; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3741 | struct lysp_module *grp_mod, *mod_old = ctx->pmod; |
| 3742 | LY_ERR ret = LY_SUCCESS; |
| 3743 | struct lysc_when *when_shared = NULL; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3744 | struct ly_set uses_child_set = {0}; |
| 3745 | |
| 3746 | /* find the referenced grouping */ |
| 3747 | LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod)); |
| 3748 | |
| 3749 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 3750 | grp_stack_count = ctx->groupings.count; |
| 3751 | LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL)); |
| 3752 | if (grp_stack_count == ctx->groupings.count) { |
| 3753 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3754 | LOGVAL(ctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3755 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 3756 | return LY_EVALID; |
| 3757 | } |
| 3758 | |
| 3759 | /* check status */ |
| 3760 | ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name); |
| 3761 | LY_CHECK_GOTO(ret, cleanup); |
| 3762 | |
| 3763 | /* compile any augments and refines so they can be applied during the grouping nodes compilation */ |
| 3764 | ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent); |
| 3765 | LY_CHECK_GOTO(ret, cleanup); |
| 3766 | |
| 3767 | /* switch context's parsed module being processed */ |
| 3768 | ctx->pmod = grp_mod; |
| 3769 | |
Michal Vasko | 10b6b1c | 2021-07-20 10:43:12 +0200 | [diff] [blame] | 3770 | /* compile special uses status flags */ |
| 3771 | uses_flags = uses_p->flags; |
| 3772 | ret = lys_compile_status(ctx, &uses_flags, "<uses>", parent ? parent->flags : 0, parent ? parent->name : NULL, 0); |
| 3773 | LY_CHECK_GOTO(ret, cleanup); |
| 3774 | uses_flags |= LYS_STATUS_USES; |
| 3775 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3776 | /* compile data nodes */ |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 3777 | LY_LIST_FOR(grp->child, pnode) { |
Michal Vasko | 10b6b1c | 2021-07-20 10:43:12 +0200 | [diff] [blame] | 3778 | ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3779 | LY_CHECK_GOTO(ret, cleanup); |
| 3780 | } |
| 3781 | |
| 3782 | if (child_set) { |
| 3783 | /* add these children to our compiled child_set as well since uses is a schema-only node */ |
| 3784 | LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup); |
| 3785 | } |
| 3786 | |
| 3787 | if (uses_p->when) { |
| 3788 | /* pass uses's when to all the data children */ |
| 3789 | for (i = 0; i < uses_child_set.count; ++i) { |
| 3790 | child = uses_child_set.snodes[i]; |
| 3791 | |
Michal Vasko | 10b6b1c | 2021-07-20 10:43:12 +0200 | [diff] [blame] | 3792 | ret = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), child, &when_shared); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3793 | LY_CHECK_GOTO(ret, cleanup); |
| 3794 | } |
| 3795 | } |
| 3796 | |
| 3797 | /* compile actions */ |
| 3798 | if (grp->actions) { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3799 | struct lysc_node_action **actions; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3800 | actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs; |
| 3801 | if (!actions) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3802 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.", |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3803 | grp->actions->name, lys_nodetype2str(grp->actions->nodetype), |
| 3804 | parent->name, lys_nodetype2str(parent->nodetype)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3805 | ret = LY_EVALID; |
| 3806 | goto cleanup; |
| 3807 | } |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3808 | LY_LIST_FOR((struct lysp_node *)grp->actions, pnode) { |
Michal Vasko | 10b6b1c | 2021-07-20 10:43:12 +0200 | [diff] [blame] | 3809 | ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3810 | LY_CHECK_GOTO(ret, cleanup); |
| 3811 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3812 | |
| 3813 | if (uses_p->when) { |
| 3814 | /* inherit when */ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3815 | LY_LIST_FOR((struct lysc_node *)*actions, child) { |
Michal Vasko | 10b6b1c | 2021-07-20 10:43:12 +0200 | [diff] [blame] | 3816 | ret = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), child, &when_shared); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3817 | LY_CHECK_GOTO(ret, cleanup); |
| 3818 | } |
| 3819 | } |
| 3820 | } |
| 3821 | |
| 3822 | /* compile notifications */ |
| 3823 | if (grp->notifs) { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3824 | struct lysc_node_notif **notifs; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3825 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs; |
| 3826 | if (!notifs) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3827 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.", |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3828 | grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype), |
| 3829 | parent->name, lys_nodetype2str(parent->nodetype)); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3830 | ret = LY_EVALID; |
| 3831 | goto cleanup; |
| 3832 | } |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3833 | |
| 3834 | LY_LIST_FOR((struct lysp_node *)grp->notifs, pnode) { |
Michal Vasko | 10b6b1c | 2021-07-20 10:43:12 +0200 | [diff] [blame] | 3835 | ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3836 | LY_CHECK_GOTO(ret, cleanup); |
| 3837 | } |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3838 | |
| 3839 | if (uses_p->when) { |
| 3840 | /* inherit when */ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3841 | LY_LIST_FOR((struct lysc_node *)*notifs, child) { |
Michal Vasko | 10b6b1c | 2021-07-20 10:43:12 +0200 | [diff] [blame] | 3842 | ret = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), child, &when_shared); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3843 | LY_CHECK_GOTO(ret, cleanup); |
| 3844 | } |
| 3845 | } |
| 3846 | } |
| 3847 | |
| 3848 | /* check that all augments were applied */ |
| 3849 | for (i = 0; i < ctx->uses_augs.count; ++i) { |
Michal Vasko | d865572 | 2021-01-12 15:20:36 +0100 | [diff] [blame] | 3850 | if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) { |
| 3851 | /* augment of some parent uses, irrelevant now */ |
| 3852 | continue; |
| 3853 | } |
| 3854 | |
| 3855 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.", |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3856 | ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name); |
| 3857 | ret = LY_ENOTFOUND; |
| 3858 | } |
| 3859 | LY_CHECK_GOTO(ret, cleanup); |
| 3860 | |
| 3861 | /* check that all refines were applied */ |
| 3862 | for (i = 0; i < ctx->uses_rfns.count; ++i) { |
Michal Vasko | d865572 | 2021-01-12 15:20:36 +0100 | [diff] [blame] | 3863 | if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) { |
| 3864 | /* refine of some paretn uses, irrelevant now */ |
| 3865 | continue; |
| 3866 | } |
| 3867 | |
| 3868 | LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.", |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3869 | ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name); |
| 3870 | ret = LY_ENOTFOUND; |
| 3871 | } |
| 3872 | LY_CHECK_GOTO(ret, cleanup); |
| 3873 | |
| 3874 | cleanup: |
| 3875 | /* reload previous context's parsed module being processed */ |
| 3876 | ctx->pmod = mod_old; |
| 3877 | |
| 3878 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 3879 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 3880 | assert(ctx->groupings.count == grp_stack_count); |
| 3881 | |
| 3882 | ly_set_erase(&uses_child_set, NULL); |
| 3883 | return ret; |
| 3884 | } |
| 3885 | |
| 3886 | static int |
| 3887 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 3888 | { |
| 3889 | struct lysp_node *iter; |
| 3890 | int len = 0; |
| 3891 | |
| 3892 | *path = NULL; |
| 3893 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 3894 | char *s = *path; |
| 3895 | char *id; |
| 3896 | |
| 3897 | switch (iter->nodetype) { |
| 3898 | case LYS_USES: |
| 3899 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
| 3900 | break; |
| 3901 | case LYS_GROUPING: |
| 3902 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
| 3903 | break; |
| 3904 | case LYS_AUGMENT: |
| 3905 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
| 3906 | break; |
| 3907 | default: |
| 3908 | id = strdup(iter->name); |
| 3909 | break; |
| 3910 | } |
| 3911 | |
| 3912 | if (!iter->parent) { |
| 3913 | /* print prefix */ |
| 3914 | len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : ""); |
| 3915 | } else { |
| 3916 | /* prefix is the same as in parent */ |
| 3917 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 3918 | } |
| 3919 | free(s); |
| 3920 | free(id); |
| 3921 | } |
| 3922 | |
| 3923 | if (len < 0) { |
| 3924 | free(*path); |
| 3925 | *path = NULL; |
| 3926 | } else if (len == 0) { |
| 3927 | *path = strdup("/"); |
| 3928 | len = 1; |
| 3929 | } |
| 3930 | return len; |
| 3931 | } |
| 3932 | |
| 3933 | LY_ERR |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 3934 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3935 | { |
| 3936 | LY_ERR ret; |
| 3937 | char *path; |
| 3938 | int len; |
| 3939 | |
| 3940 | struct lysp_node_uses fake_uses = { |
| 3941 | .parent = pnode, |
| 3942 | .nodetype = LYS_USES, |
| 3943 | .flags = 0, .next = NULL, |
| 3944 | .name = grp->name, |
| 3945 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 3946 | .refines = NULL, .augments = NULL |
| 3947 | }; |
| 3948 | struct lysc_node_container fake_container = { |
| 3949 | .nodetype = LYS_CONTAINER, |
| 3950 | .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 3951 | .module = ctx->cur_mod, |
Michal Vasko | bd6de2b | 2020-10-22 14:45:03 +0200 | [diff] [blame] | 3952 | .parent = NULL, .next = NULL, |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3953 | .prev = &fake_container.node, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3954 | .name = "fake", |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 3955 | .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL, |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3956 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 3957 | }; |
| 3958 | |
| 3959 | if (grp->parent) { |
| 3960 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 3961 | } |
| 3962 | |
| 3963 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 3964 | if (len < 0) { |
| 3965 | LOGMEM(ctx->ctx); |
| 3966 | return LY_EMEM; |
| 3967 | } |
| 3968 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 3969 | ctx->path_len = (uint32_t)len; |
| 3970 | free(path); |
| 3971 | |
| 3972 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 3973 | lysc_update_path(ctx, NULL, grp->name); |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3974 | ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3975 | lysc_update_path(ctx, NULL, NULL); |
| 3976 | lysc_update_path(ctx, NULL, NULL); |
| 3977 | |
| 3978 | ctx->path_len = 1; |
| 3979 | ctx->path[1] = '\0'; |
| 3980 | |
| 3981 | /* cleanup */ |
| 3982 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 3983 | |
| 3984 | return ret; |
| 3985 | } |
| 3986 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3987 | LY_ERR |
| 3988 | lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status, |
| 3989 | struct ly_set *child_set) |
| 3990 | { |
| 3991 | LY_ERR ret = LY_SUCCESS; |
| 3992 | struct lysc_node *node = NULL; |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 3993 | uint32_t prev_opts = ctx->compile_opts; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3994 | |
| 3995 | LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *); |
| 3996 | |
| 3997 | if (pnode->nodetype != LYS_USES) { |
Radek Krejci | a601699 | 2021-03-03 10:13:41 +0100 | [diff] [blame] | 3998 | lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 3999 | } else { |
| 4000 | lysc_update_path(ctx, NULL, "{uses}"); |
| 4001 | lysc_update_path(ctx, NULL, pnode->name); |
| 4002 | } |
| 4003 | |
| 4004 | switch (pnode->nodetype) { |
| 4005 | case LYS_CONTAINER: |
| 4006 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container)); |
| 4007 | node_compile_spec = lys_compile_node_container; |
| 4008 | break; |
| 4009 | case LYS_LEAF: |
| 4010 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf)); |
| 4011 | node_compile_spec = lys_compile_node_leaf; |
| 4012 | break; |
| 4013 | case LYS_LIST: |
| 4014 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list)); |
| 4015 | node_compile_spec = lys_compile_node_list; |
| 4016 | break; |
| 4017 | case LYS_LEAFLIST: |
| 4018 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist)); |
| 4019 | node_compile_spec = lys_compile_node_leaflist; |
| 4020 | break; |
| 4021 | case LYS_CHOICE: |
| 4022 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice)); |
| 4023 | node_compile_spec = lys_compile_node_choice; |
| 4024 | break; |
| 4025 | case LYS_CASE: |
| 4026 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case)); |
| 4027 | node_compile_spec = lys_compile_node_case; |
| 4028 | break; |
| 4029 | case LYS_ANYXML: |
| 4030 | case LYS_ANYDATA: |
| 4031 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata)); |
| 4032 | node_compile_spec = lys_compile_node_any; |
| 4033 | break; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 4034 | case LYS_RPC: |
| 4035 | case LYS_ACTION: |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 4036 | if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 4037 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
| 4038 | "Action \"%s\" is placed inside %s.", pnode->name, |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 4039 | (ctx->compile_opts & LYS_IS_NOTIF) ? "notification" : "another RPC/action"); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 4040 | return LY_EVALID; |
| 4041 | } |
| 4042 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action)); |
| 4043 | node_compile_spec = lys_compile_node_action; |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 4044 | ctx->compile_opts |= LYS_COMPILE_NO_CONFIG; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 4045 | break; |
| 4046 | case LYS_NOTIF: |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 4047 | if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 4048 | LOGVAL(ctx->ctx, LYVE_SEMANTICS, |
| 4049 | "Notification \"%s\" is placed inside %s.", pnode->name, |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 4050 | (ctx->compile_opts & LYS_IS_NOTIF) ? "another notification" : "RPC/action"); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 4051 | return LY_EVALID; |
| 4052 | } |
| 4053 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif)); |
| 4054 | node_compile_spec = lys_compile_node_notif; |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 4055 | ctx->compile_opts |= LYS_COMPILE_NOTIFICATION; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 4056 | break; |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 4057 | case LYS_USES: |
| 4058 | ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set); |
| 4059 | lysc_update_path(ctx, NULL, NULL); |
| 4060 | lysc_update_path(ctx, NULL, NULL); |
| 4061 | return ret; |
| 4062 | default: |
| 4063 | LOGINT(ctx->ctx); |
| 4064 | return LY_EINT; |
| 4065 | } |
| 4066 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 4067 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 4068 | ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 4069 | |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 4070 | ctx->compile_opts = prev_opts; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 4071 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 4072 | return ret; |
| 4073 | } |