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