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