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