Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema tree implementation |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 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 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 16 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | #include <stdint.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 21 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 24 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 26 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | #include "dict.h" |
| 29 | #include "log.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 30 | #include "path.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 31 | #include "parser.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 32 | #include "parser_schema.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 33 | #include "plugins_exts.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | #include "plugins_types.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 35 | #include "plugins_exts_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "set.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 37 | #include "tree.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 38 | #include "tree_data.h" |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 39 | #include "tree_data_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 40 | #include "tree_schema.h" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 41 | #include "tree_schema_internal.h" |
| 42 | #include "xpath.h" |
| 43 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 44 | static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 45 | void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod); |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 46 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 47 | /** |
| 48 | * @brief Duplicate string into dictionary |
| 49 | * @param[in] CTX libyang context of the dictionary. |
| 50 | * @param[in] ORIG String to duplicate. |
| 51 | * @param[out] DUP Where to store the result. |
| 52 | */ |
| 53 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 54 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 55 | #define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 56 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 57 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 58 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 59 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 60 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 61 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 62 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 63 | } \ |
| 64 | } |
| 65 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 66 | #define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 67 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 68 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 69 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 70 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 71 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 72 | RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 73 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 74 | } \ |
| 75 | } |
| 76 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 77 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \ |
| 78 | if (EXTS_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 79 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \ |
| 80 | for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 81 | LY_ARRAY_INCREMENT(EXT_C); \ |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 82 | RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 83 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 84 | } \ |
| 85 | } |
| 86 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 87 | #define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 88 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 89 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 90 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 91 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 92 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 93 | RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 94 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 95 | } \ |
| 96 | } |
| 97 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 98 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 99 | if (MEMBER_P) { \ |
| 100 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 101 | LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 102 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 103 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 104 | } |
| 105 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 106 | #define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 107 | if (MEMBER_P) { \ |
| 108 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 109 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 110 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 111 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 112 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 113 | } |
| 114 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 115 | #define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 116 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 117 | for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 118 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 119 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 120 | return LY_EVALID; \ |
| 121 | } \ |
| 122 | } \ |
| 123 | } |
| 124 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 125 | #define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 126 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 127 | for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \ |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 128 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \ |
| 129 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 130 | return LY_EVALID; \ |
| 131 | } \ |
| 132 | } \ |
| 133 | } |
| 134 | |
| 135 | struct lysc_ext * |
| 136 | lysc_ext_dup(struct lysc_ext *orig) |
| 137 | { |
| 138 | ++orig->refcount; |
| 139 | return orig; |
| 140 | } |
| 141 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 142 | static struct lysc_ext_instance * |
| 143 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 144 | { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 145 | /* TODO - extensions, increase refcount */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 146 | (void) ctx; |
| 147 | (void) orig; |
| 148 | return NULL; |
| 149 | } |
| 150 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 151 | static LY_ERR |
| 152 | lysc_incomplete_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 153 | struct lys_module *dflt_mod) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 154 | { |
| 155 | struct lysc_incomplete_dflt *r; |
| 156 | uint32_t i; |
| 157 | |
| 158 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 159 | r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i]; |
| 160 | if (r->leaf == leaf) { |
| 161 | /* just replace the default */ |
| 162 | r->dflt = dflt; |
| 163 | return LY_SUCCESS; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | r = malloc(sizeof *r); |
| 168 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 169 | r->leaf = leaf; |
| 170 | r->dflt = dflt; |
| 171 | r->dflts = NULL; |
| 172 | r->dflt_mod = dflt_mod; |
| 173 | ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST); |
| 174 | |
| 175 | return LY_SUCCESS; |
| 176 | } |
| 177 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 178 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 179 | * @brief Add record into the compile context's list of incomplete default values. |
| 180 | * @param[in] ctx Compile context with the incomplete default values list. |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 181 | * @param[in] term Term context node with the default value. |
| 182 | * @param[in] value String default value. |
| 183 | * @param[in] val_len Length of @p value. |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 184 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 185 | * @return LY_EMEM in case of memory allocation failure. |
| 186 | * @return LY_SUCCESS |
| 187 | */ |
| 188 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 189 | lysc_incomplete_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char **dflts, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 190 | struct lys_module *dflt_mod) |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 191 | { |
| 192 | struct lysc_incomplete_dflt *r; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 193 | uint32_t i; |
| 194 | |
| 195 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 196 | r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i]; |
| 197 | if (r->llist == llist) { |
| 198 | /* just replace the defaults */ |
| 199 | r->dflts = dflts; |
| 200 | return LY_SUCCESS; |
| 201 | } |
| 202 | } |
| 203 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 204 | r = malloc(sizeof *r); |
| 205 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 206 | r->llist = llist; |
| 207 | r->dflt = NULL; |
| 208 | r->dflts = dflts; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 209 | r->dflt_mod = dflt_mod; |
| 210 | ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST); |
| 211 | |
| 212 | return LY_SUCCESS; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 217 | * @param[in] ctx Compile context with the incomplete default values list. |
| 218 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 219 | */ |
| 220 | static void |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 221 | lysc_incomplete_dflt_remove(struct lysc_ctx *ctx, struct lysc_node *term) |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 222 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 223 | uint32_t u; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 224 | struct lysc_incomplete_dflt *r; |
| 225 | |
| 226 | for (u = 0; u < ctx->dflts.count; ++u) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 227 | r = ctx->dflts.objs[u]; |
| 228 | if (r->leaf == (struct lysc_node_leaf *)term) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 229 | free(ctx->dflts.objs[u]); |
| 230 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 231 | --ctx->dflts.count; |
| 232 | return; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 237 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 238 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 239 | { |
| 240 | int len; |
| 241 | int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
| 242 | |
| 243 | if (!name) { |
| 244 | /* removing last path segment */ |
| 245 | if (ctx->path[ctx->path_len - 1] == '}') { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 246 | for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {} |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 247 | if (ctx->path[ctx->path_len] == '=') { |
| 248 | ctx->path[ctx->path_len++] = '}'; |
| 249 | } else { |
| 250 | /* not a top-level special tag, remove also preceiding '/' */ |
| 251 | goto remove_nodelevel; |
| 252 | } |
| 253 | } else { |
| 254 | remove_nodelevel: |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 255 | for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {} |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 256 | if (ctx->path_len == 0) { |
| 257 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 258 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | /* set new terminating NULL-byte */ |
| 262 | ctx->path[ctx->path_len] = '\0'; |
| 263 | } else { |
| 264 | if (ctx->path_len > 1) { |
| 265 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 266 | /* extension of the special tag */ |
| 267 | nextlevel = 2; |
| 268 | --ctx->path_len; |
| 269 | } else { |
| 270 | /* there is already some path, so add next level */ |
| 271 | nextlevel = 1; |
| 272 | } |
| 273 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 274 | |
| 275 | if (nextlevel != 2) { |
| 276 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 277 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 278 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 279 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 280 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 281 | } |
| 282 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 283 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 284 | } |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 285 | if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) { |
| 286 | /* output truncated */ |
| 287 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 288 | } else { |
| 289 | ctx->path_len += len; |
| 290 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 295 | * @brief Duplicate the compiled pattern structure. |
| 296 | * |
| 297 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 298 | * |
| 299 | * @param[in] orig The pattern structure to duplicate. |
| 300 | * @return The duplicated structure to use. |
| 301 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 302 | static struct lysc_pattern * |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 303 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 304 | { |
| 305 | ++orig->refcount; |
| 306 | return orig; |
| 307 | } |
| 308 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 309 | /** |
| 310 | * @brief Duplicate the array of compiled patterns. |
| 311 | * |
| 312 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 313 | * |
| 314 | * @param[in] ctx Libyang context for logging. |
| 315 | * @param[in] orig The patterns sized array to duplicate. |
| 316 | * @return New sized array as a copy of @p orig. |
| 317 | * @return NULL in case of memory allocation error. |
| 318 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 319 | static struct lysc_pattern ** |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 320 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 321 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 322 | struct lysc_pattern **dup = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 323 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 324 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 325 | assert(orig); |
| 326 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 327 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 328 | LY_ARRAY_FOR(orig, u) { |
| 329 | dup[u] = lysc_pattern_dup(orig[u]); |
| 330 | LY_ARRAY_INCREMENT(dup); |
| 331 | } |
| 332 | return dup; |
| 333 | } |
| 334 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 335 | /** |
| 336 | * @brief Duplicate compiled range structure. |
| 337 | * |
| 338 | * @param[in] ctx Libyang context for logging. |
| 339 | * @param[in] orig The range structure to be duplicated. |
| 340 | * @return New compiled range structure as a copy of @p orig. |
| 341 | * @return NULL in case of memory allocation error. |
| 342 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 343 | struct lysc_range * |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 344 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 345 | { |
| 346 | struct lysc_range *dup; |
| 347 | LY_ERR ret; |
| 348 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 349 | assert(orig); |
| 350 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 351 | dup = calloc(1, sizeof *dup); |
| 352 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 353 | if (orig->parts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 354 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup); |
| 355 | LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts); |
| 356 | memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 357 | } |
| 358 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 359 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 360 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 361 | |
| 362 | return dup; |
| 363 | cleanup: |
| 364 | free(dup); |
| 365 | (void) ret; /* set but not used due to the return type */ |
| 366 | return NULL; |
| 367 | } |
| 368 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 369 | /** |
| 370 | * @brief Stack for processing if-feature expressions. |
| 371 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 372 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 373 | int size; /**< number of items in the stack */ |
| 374 | int index; /**< first empty item */ |
| 375 | uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 376 | }; |
| 377 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 378 | /** |
| 379 | * @brief Add @ref ifftokens into the stack. |
| 380 | * @param[in] stack The if-feature stack to use. |
| 381 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 382 | * @return LY_EMEM in case of memory allocation error |
| 383 | * @return LY_ESUCCESS if the value successfully stored. |
| 384 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 385 | static LY_ERR |
| 386 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 387 | { |
| 388 | if (stack->index == stack->size) { |
| 389 | stack->size += 4; |
| 390 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 391 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 392 | } |
| 393 | stack->stack[stack->index++] = value; |
| 394 | return LY_SUCCESS; |
| 395 | } |
| 396 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 397 | /** |
| 398 | * @brief Get (and remove) the last item form the stack. |
| 399 | * @param[in] stack The if-feature stack to use. |
| 400 | * @return The value from the top of the stack. |
| 401 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 402 | static uint8_t |
| 403 | iff_stack_pop(struct iff_stack *stack) |
| 404 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 405 | assert(stack && stack->index); |
| 406 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 407 | stack->index--; |
| 408 | return stack->stack[stack->index]; |
| 409 | } |
| 410 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 411 | /** |
| 412 | * @brief Clean up the stack. |
| 413 | * @param[in] stack The if-feature stack to use. |
| 414 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 415 | static void |
| 416 | iff_stack_clean(struct iff_stack *stack) |
| 417 | { |
| 418 | stack->size = 0; |
| 419 | free(stack->stack); |
| 420 | } |
| 421 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 422 | /** |
| 423 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 424 | * (libyang format of the if-feature expression). |
| 425 | * @param[in,out] list The 2bits array to modify. |
| 426 | * @param[in] op The operand (@ref ifftokens) to store. |
| 427 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 428 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 429 | static void |
| 430 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 431 | { |
| 432 | uint8_t *item; |
| 433 | uint8_t mask = 3; |
| 434 | |
| 435 | assert(pos >= 0); |
| 436 | assert(op <= 3); /* max 2 bits */ |
| 437 | |
| 438 | item = &list[pos / 4]; |
| 439 | mask = mask << 2 * (pos % 4); |
| 440 | *item = (*item) & ~mask; |
| 441 | *item = (*item) | (op << 2 * (pos % 4)); |
| 442 | } |
| 443 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 444 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 445 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 446 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 447 | /** |
| 448 | * @brief Find a feature of the given name and referenced in the given module. |
| 449 | * |
| 450 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 451 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 452 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 453 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 454 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 455 | * assigned till that time will be still valid. |
| 456 | * |
| 457 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 458 | * @param[in] name Name of the feature including possible prefix. |
| 459 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 460 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 461 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 462 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 463 | lys_feature_find(struct lys_module *mod, const char *name, size_t len) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 464 | { |
| 465 | size_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 466 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 467 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 468 | |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 469 | assert(mod); |
| 470 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 471 | for (i = 0; i < len; ++i) { |
| 472 | if (name[i] == ':') { |
| 473 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 474 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 475 | LY_CHECK_RET(!mod, NULL); |
| 476 | |
| 477 | name = &name[i + 1]; |
| 478 | len = len - i - 1; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 483 | if (mod->implemented) { |
| 484 | /* module is implemented so there is already the compiled schema */ |
| 485 | flist = mod->compiled->features; |
| 486 | } else { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 487 | flist = mod->dis_features; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 488 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 489 | LY_ARRAY_FOR(flist, u) { |
| 490 | f = &flist[u]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 491 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 492 | return f; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | return NULL; |
| 497 | } |
| 498 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 499 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 500 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 501 | */ |
| 502 | static LY_ERR |
| 503 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 504 | { |
| 505 | LY_ERR ret = LY_SUCCESS; |
| 506 | |
| 507 | if (!ext_p->compiled) { |
| 508 | lysc_update_path(ctx, NULL, "{extension}"); |
| 509 | lysc_update_path(ctx, NULL, ext_p->name); |
| 510 | |
| 511 | /* compile the extension definition */ |
| 512 | ext_p->compiled = calloc(1, sizeof **ext); |
| 513 | ext_p->compiled->refcount = 1; |
| 514 | DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name); |
| 515 | DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument); |
| 516 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 517 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 518 | |
| 519 | lysc_update_path(ctx, NULL, NULL); |
| 520 | lysc_update_path(ctx, NULL, NULL); |
| 521 | |
| 522 | /* find extension definition plugin */ |
| 523 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 524 | } |
| 525 | |
| 526 | *ext = lysc_ext_dup(ext_p->compiled); |
| 527 | |
| 528 | done: |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 533 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 534 | * |
| 535 | * @param[in] ctx Compilation context. |
| 536 | * @param[in] ext_p Parsed extension instance. |
| 537 | * @param[in,out] ext Prepared compiled extension instance. |
| 538 | * @param[in] parent Extension instance parent. |
| 539 | * @param[in] parent_type Extension instance parent type. |
| 540 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 541 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 542 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 543 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 544 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 545 | { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 546 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 547 | const char *name; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 548 | size_t u; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 549 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 550 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 551 | |
| 552 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 553 | ext->insubstmt = ext_p->insubstmt; |
| 554 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 555 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 556 | ext->parent = parent; |
| 557 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 558 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 559 | lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node *)ext->parent : NULL, "{extension}"); |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 560 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 561 | /* get module where the extension definition should be placed */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 562 | for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {} |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 563 | if (ext_p->yin) { |
| 564 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 565 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 566 | * YANG (import) prefix must be done here. */ |
| 567 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
| 568 | prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0); |
| 569 | u = 0; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 570 | } else { |
| 571 | assert(ctx->mod_def->parsed); |
| 572 | LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) { |
| 573 | if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 574 | char *s; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 575 | LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->parsed->imports[v].prefix, &ext_p->name[u]) == -1, |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 576 | ret = LY_EMEM, cleanup); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 577 | prefixed_name = lydict_insert_zc(ctx->ctx, s); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 578 | u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 579 | break; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | if (!prefixed_name) { |
| 584 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 585 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
| 586 | goto cleanup; |
| 587 | } |
| 588 | } else { |
| 589 | prefixed_name = ext_p->name; |
| 590 | } |
| 591 | lysc_update_path(ctx, NULL, prefixed_name); |
| 592 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 593 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 594 | ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 595 | if (!ext_mod) { |
| 596 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 597 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
| 598 | goto cleanup; |
| 599 | } else if (!ext_mod->parsed->extensions) { |
| 600 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 601 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 602 | prefixed_name, ext_mod->name); |
| 603 | goto cleanup; |
| 604 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 605 | } |
| 606 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 607 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 608 | /* find the parsed extension definition there */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 609 | LY_ARRAY_FOR(ext_mod->parsed->extensions, v) { |
| 610 | if (!strcmp(name, ext_mod->parsed->extensions[v].name)) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 611 | /* compile extension definition and assign it */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 612 | LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 613 | break; |
| 614 | } |
| 615 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 616 | if (!ext->def) { |
| 617 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 618 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
| 619 | goto cleanup; |
| 620 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 621 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 622 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 623 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 624 | * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 625 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 626 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 627 | struct lysp_stmt *stmt = NULL; |
| 628 | |
| 629 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 630 | /* ... argument was the first XML child element */ |
| 631 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 632 | /* TODO check namespace of the statement */ |
| 633 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 634 | stmt = ext_p->child; |
| 635 | } |
| 636 | } |
| 637 | } else { |
| 638 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 639 | * with LYS_YIN_ATTR flag */ |
| 640 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 641 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 642 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 643 | break; |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | if (!stmt) { |
| 648 | /* missing extension's argument */ |
| 649 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 650 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
| 651 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 652 | |
| 653 | } |
| 654 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 655 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 656 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 657 | if (prefixed_name != ext_p->name) { |
| 658 | lydict_remove(ctx->ctx, ext_p->name); |
| 659 | ext_p->name = prefixed_name; |
| 660 | if (!ext_p->argument && ext->argument) { |
| 661 | ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0); |
| 662 | } |
| 663 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 664 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 665 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 666 | if (ext->argument) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 667 | lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 668 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 669 | LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 670 | if (ext->argument) { |
| 671 | lysc_update_path(ctx, NULL, NULL); |
| 672 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 673 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 674 | ext_p->compiled = ext; |
| 675 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 676 | ret = LY_SUCCESS; |
| 677 | cleanup: |
| 678 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 679 | lydict_remove(ctx->ctx, prefixed_name); |
| 680 | } |
| 681 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 682 | lysc_update_path(ctx, NULL, NULL); |
| 683 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 684 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 685 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 689 | * @brief Compile information from the if-feature statement |
| 690 | * @param[in] ctx Compile context. |
| 691 | * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 692 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 693 | * @return LY_ERR value. |
| 694 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 695 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 696 | lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 697 | { |
| 698 | const char *c = *value; |
| 699 | int r, rc = EXIT_FAILURE; |
| 700 | int i, j, last_not, checkversion = 0; |
| 701 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 702 | uint8_t op; |
| 703 | struct iff_stack stack = {0, 0, NULL}; |
| 704 | struct lysc_feature *f; |
| 705 | |
| 706 | assert(c); |
| 707 | |
| 708 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 709 | for (i = j = last_not = 0; c[i]; i++) { |
| 710 | if (c[i] == '(') { |
| 711 | j++; |
| 712 | checkversion = 1; |
| 713 | continue; |
| 714 | } else if (c[i] == ')') { |
| 715 | j--; |
| 716 | continue; |
| 717 | } else if (isspace(c[i])) { |
| 718 | checkversion = 1; |
| 719 | continue; |
| 720 | } |
| 721 | |
| 722 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 723 | int sp; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 724 | for (sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 725 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 726 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 727 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 728 | return LY_EVALID; |
| 729 | } else if (!isspace(c[i + r])) { |
| 730 | /* feature name starting with the not/and/or */ |
| 731 | last_not = 0; |
| 732 | f_size++; |
| 733 | } else if (c[i] == 'n') { /* not operation */ |
| 734 | if (last_not) { |
| 735 | /* double not */ |
| 736 | expr_size = expr_size - 2; |
| 737 | last_not = 0; |
| 738 | } else { |
| 739 | last_not = 1; |
| 740 | } |
| 741 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 742 | if (f_exp != f_size) { |
| 743 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 744 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 745 | return LY_EVALID; |
| 746 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 747 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 748 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 749 | /* not a not operation */ |
| 750 | last_not = 0; |
| 751 | } |
| 752 | i += r; |
| 753 | } else { |
| 754 | f_size++; |
| 755 | last_not = 0; |
| 756 | } |
| 757 | expr_size++; |
| 758 | |
| 759 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 760 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 761 | i--; |
| 762 | break; |
| 763 | } |
| 764 | i++; |
| 765 | } |
| 766 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 767 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 768 | /* not matching count of ( and ) */ |
| 769 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 770 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 771 | return LY_EVALID; |
| 772 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 773 | if (f_exp != f_size) { |
| 774 | /* features do not match the needed arguments for the logical operations */ |
| 775 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 776 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 777 | "the required number of operands for the operations.", *value); |
| 778 | return LY_EVALID; |
| 779 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 780 | |
| 781 | if (checkversion || expr_size > 1) { |
| 782 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 783 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 784 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 785 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 786 | return LY_EVALID; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /* allocate the memory */ |
| 791 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 792 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 793 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 794 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 795 | |
| 796 | stack.size = expr_size; |
| 797 | f_size--; expr_size--; /* used as indexes from now */ |
| 798 | |
| 799 | for (i--; i >= 0; i--) { |
| 800 | if (c[i] == ')') { |
| 801 | /* push it on stack */ |
| 802 | iff_stack_push(&stack, LYS_IFF_RP); |
| 803 | continue; |
| 804 | } else if (c[i] == '(') { |
| 805 | /* pop from the stack into result all operators until ) */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 806 | while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 807 | iff_setop(iff->expr, op, expr_size--); |
| 808 | } |
| 809 | continue; |
| 810 | } else if (isspace(c[i])) { |
| 811 | continue; |
| 812 | } |
| 813 | |
| 814 | /* end of operator or operand -> find beginning and get what is it */ |
| 815 | j = i + 1; |
| 816 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 817 | i--; |
| 818 | } |
| 819 | i++; /* go back by one step */ |
| 820 | |
| 821 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 822 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 823 | /* double not */ |
| 824 | iff_stack_pop(&stack); |
| 825 | } else { |
| 826 | /* not has the highest priority, so do not pop from the stack |
| 827 | * as in case of AND and OR */ |
| 828 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 829 | } |
| 830 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 831 | /* as for OR - pop from the stack all operators with the same or higher |
| 832 | * priority and store them to the result, then push the AND to the stack */ |
| 833 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 834 | op = iff_stack_pop(&stack); |
| 835 | iff_setop(iff->expr, op, expr_size--); |
| 836 | } |
| 837 | iff_stack_push(&stack, LYS_IFF_AND); |
| 838 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 839 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 840 | op = iff_stack_pop(&stack); |
| 841 | iff_setop(iff->expr, op, expr_size--); |
| 842 | } |
| 843 | iff_stack_push(&stack, LYS_IFF_OR); |
| 844 | } else { |
| 845 | /* feature name, length is j - i */ |
| 846 | |
| 847 | /* add it to the expression */ |
| 848 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 849 | |
| 850 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 851 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 852 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 853 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 854 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 855 | iff->features[f_size] = f; |
| 856 | LY_ARRAY_INCREMENT(iff->features); |
| 857 | f_size--; |
| 858 | } |
| 859 | } |
| 860 | while (stack.index) { |
| 861 | op = iff_stack_pop(&stack); |
| 862 | iff_setop(iff->expr, op, expr_size--); |
| 863 | } |
| 864 | |
| 865 | if (++expr_size || ++f_size) { |
| 866 | /* not all expected operators and operands found */ |
| 867 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 868 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 869 | rc = LY_EINT; |
| 870 | } else { |
| 871 | rc = LY_SUCCESS; |
| 872 | } |
| 873 | |
| 874 | error: |
| 875 | /* cleanup */ |
| 876 | iff_stack_clean(&stack); |
| 877 | |
| 878 | return rc; |
| 879 | } |
| 880 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 881 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 882 | * @brief Get the XPath context node for the given schema node. |
| 883 | * @param[in] start The schema node where the XPath expression appears. |
| 884 | * @return The context node to evaluate XPath expression in given schema node. |
| 885 | * @return NULL in case the context node is the root node. |
| 886 | */ |
| 887 | static struct lysc_node * |
| 888 | lysc_xpath_context(struct lysc_node *start) |
| 889 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 890 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 891 | start = start->parent) {} |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 892 | return start; |
| 893 | } |
| 894 | |
| 895 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 896 | * @brief Compile information from the when statement |
| 897 | * @param[in] ctx Compile context. |
| 898 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 899 | * @param[in] flags Flags of the node with the "when" defiition. |
| 900 | * @param[in] node Node that inherited the "when" definition, must be connected to parents. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 901 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 902 | * @return LY_ERR value. |
| 903 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 904 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 905 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 906 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 907 | LY_ERR ret = LY_SUCCESS; |
| 908 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 909 | *when = calloc(1, sizeof **when); |
| 910 | (*when)->refcount = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 911 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 912 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 913 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 914 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 915 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 916 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 917 | COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 918 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 919 | |
| 920 | done: |
| 921 | return ret; |
| 922 | } |
| 923 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 924 | /** |
| 925 | * @brief Compile information from the must statement |
| 926 | * @param[in] ctx Compile context. |
| 927 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 928 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 929 | * @return LY_ERR value. |
| 930 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 931 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 932 | lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 933 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 934 | LY_ERR ret = LY_SUCCESS; |
| 935 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 936 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 937 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 938 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 939 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 940 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 941 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 942 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 943 | COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 944 | |
| 945 | done: |
| 946 | return ret; |
| 947 | } |
| 948 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 949 | /** |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 950 | * @brief Compile information in the import statement - make sure there is the target module |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 951 | * @param[in] ctx Compile context. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 952 | * @param[in] imp_p The parsed import statement structure to fill the module to. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 953 | * @return LY_ERR value. |
| 954 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 955 | static LY_ERR |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 956 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 957 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 958 | const struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 959 | LY_ERR ret = LY_SUCCESS; |
| 960 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 961 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 962 | * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added, |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 963 | * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 964 | if (!imp_p->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 965 | /* try to use filepath if present */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 966 | if (imp_p->module->filepath) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 967 | struct ly_in *in; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 968 | if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 969 | LOGINT(ctx->ctx); |
| 970 | } else { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 971 | LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)], |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 972 | ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 973 | if (mod != imp_p->module) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 974 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 975 | imp_p->module->filepath, imp_p->module->name); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 976 | mod = NULL; |
| 977 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 978 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 979 | ly_in_free(in, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 980 | } |
| 981 | if (!mod) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 982 | if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, (struct lys_module **)&mod)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 983 | LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.", |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 984 | imp_p->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 985 | return LY_ENOTFOUND; |
| 986 | } |
| 987 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 988 | } |
| 989 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 990 | return ret; |
| 991 | } |
| 992 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 993 | LY_ERR |
| 994 | lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 995 | struct lysp_ident *identities_p, struct lysc_ident **identities) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 996 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 997 | LY_ARRAY_COUNT_TYPE offset = 0, u, v; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 998 | struct lysc_ctx context = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 999 | LY_ERR ret = LY_SUCCESS; |
| 1000 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1001 | assert(ctx_sc || ctx); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1002 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1003 | if (!ctx_sc) { |
| 1004 | context.ctx = ctx; |
| 1005 | context.mod = module; |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 1006 | context.mod_def = module; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1007 | context.path_len = 1; |
| 1008 | context.path[0] = '/'; |
| 1009 | ctx_sc = &context; |
| 1010 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1011 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1012 | if (!identities_p) { |
| 1013 | return LY_SUCCESS; |
| 1014 | } |
| 1015 | if (*identities) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1016 | offset = LY_ARRAY_COUNT(*identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | lysc_update_path(ctx_sc, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1020 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1021 | LY_ARRAY_FOR(identities_p, u) { |
| 1022 | lysc_update_path(ctx_sc, NULL, identities_p[u].name); |
| 1023 | |
| 1024 | LY_ARRAY_INCREMENT(*identities); |
| 1025 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name); |
| 1026 | DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name); |
| 1027 | DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc); |
| 1028 | DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref); |
| 1029 | (*identities)[offset + u].module = ctx_sc->mod; |
| 1030 | COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v, |
| 1031 | lys_compile_iffeature, ret, done); |
| 1032 | /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */ |
| 1033 | COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u], |
| 1034 | LYEXT_PAR_IDENT, ret, done); |
| 1035 | (*identities)[offset + u].flags = identities_p[u].flags; |
| 1036 | |
| 1037 | lysc_update_path(ctx_sc, NULL, NULL); |
| 1038 | } |
| 1039 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1040 | done: |
| 1041 | return ret; |
| 1042 | } |
| 1043 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1044 | /** |
| 1045 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 1046 | * |
| 1047 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 1048 | * |
| 1049 | * @param[in] ctx Compile context for logging. |
| 1050 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 1051 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 1052 | * @return LY_SUCCESS if everything is ok. |
| 1053 | * @return LY_EVALID if the identity is derived from itself. |
| 1054 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1055 | static LY_ERR |
| 1056 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 1057 | { |
| 1058 | LY_ERR ret = LY_EVALID; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1059 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1060 | struct ly_set recursion = {0}; |
| 1061 | struct lysc_ident *drv; |
| 1062 | |
| 1063 | if (!derived) { |
| 1064 | return LY_SUCCESS; |
| 1065 | } |
| 1066 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1067 | for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1068 | if (ident == derived[u]) { |
| 1069 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1070 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1071 | goto cleanup; |
| 1072 | } |
| 1073 | ly_set_add(&recursion, derived[u], 0); |
| 1074 | } |
| 1075 | |
| 1076 | for (v = 0; v < recursion.count; ++v) { |
| 1077 | drv = recursion.objs[v]; |
| 1078 | if (!drv->derived) { |
| 1079 | continue; |
| 1080 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1081 | for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1082 | if (ident == drv->derived[u]) { |
| 1083 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1084 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1085 | goto cleanup; |
| 1086 | } |
| 1087 | ly_set_add(&recursion, drv->derived[u], 0); |
| 1088 | } |
| 1089 | } |
| 1090 | ret = LY_SUCCESS; |
| 1091 | |
| 1092 | cleanup: |
| 1093 | ly_set_erase(&recursion, NULL); |
| 1094 | return ret; |
| 1095 | } |
| 1096 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1097 | /** |
| 1098 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1099 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1100 | * For bases in identity set backlinks to them from the base identities. For identityref, store |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1101 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1102 | * to distinguish these two use cases. |
| 1103 | * |
| 1104 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1105 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1106 | * @param[in] ident Referencing identity to work with, NULL for identityref. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1107 | * @param[in] bases Array of bases of identityref to fill in. |
| 1108 | * @return LY_ERR value. |
| 1109 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1110 | static LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1111 | lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1112 | struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1113 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1114 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1115 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1116 | struct lys_module *mod; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1117 | struct lysc_ident **idref, *identities; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1118 | |
| 1119 | assert(ident || bases); |
| 1120 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1121 | if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1122 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1123 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1124 | return LY_EVALID; |
| 1125 | } |
| 1126 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1127 | LY_ARRAY_FOR(bases_p, u) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1128 | s = strchr(bases_p[u], ':'); |
| 1129 | if (s) { |
| 1130 | /* prefixed identity */ |
| 1131 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1132 | mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1133 | } else { |
| 1134 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1135 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1136 | } |
| 1137 | if (!mod) { |
| 1138 | if (ident) { |
| 1139 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1140 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1141 | } else { |
| 1142 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1143 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1144 | } |
| 1145 | return LY_EVALID; |
| 1146 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1147 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1148 | idref = NULL; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1149 | if (mod->compiled) { |
| 1150 | identities = mod->compiled->identities; |
| 1151 | } else { |
| 1152 | identities = mod->dis_identities; |
| 1153 | } |
| 1154 | LY_ARRAY_FOR(identities, v) { |
| 1155 | if (!strcmp(name, identities[v].name)) { |
| 1156 | if (ident) { |
| 1157 | if (ident == &identities[v]) { |
| 1158 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1159 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1160 | return LY_EVALID; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1161 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1162 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived)); |
| 1163 | /* we have match! store the backlink */ |
| 1164 | LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM); |
| 1165 | *idref = ident; |
| 1166 | } else { |
| 1167 | /* we have match! store the found identity */ |
| 1168 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
| 1169 | *idref = &identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1170 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1171 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | if (!idref || !(*idref)) { |
| 1175 | if (ident) { |
| 1176 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1177 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1178 | } else { |
| 1179 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1180 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1181 | } |
| 1182 | return LY_EVALID; |
| 1183 | } |
| 1184 | } |
| 1185 | return LY_SUCCESS; |
| 1186 | } |
| 1187 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1188 | /** |
| 1189 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1190 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1191 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1192 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1193 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1194 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1195 | static LY_ERR |
| 1196 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1197 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1198 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1199 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1200 | lysc_update_path(ctx, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1201 | for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1202 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1203 | continue; |
| 1204 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1205 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1206 | LY_CHECK_RET(lys_compile_identity_bases(ctx, idents[u].module, idents_p[u].bases, &idents[u], NULL)); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1207 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1208 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1209 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1210 | return LY_SUCCESS; |
| 1211 | } |
| 1212 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1213 | LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1214 | lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1215 | struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1216 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1217 | LY_ARRAY_COUNT_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1218 | struct lysc_ctx context = {0}; |
| 1219 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1220 | assert(ctx_sc || ctx); |
| 1221 | |
| 1222 | if (!ctx_sc) { |
| 1223 | context.ctx = ctx; |
| 1224 | context.mod = module; |
| 1225 | context.path_len = 1; |
| 1226 | context.path[0] = '/'; |
| 1227 | ctx_sc = &context; |
| 1228 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1229 | |
| 1230 | if (!features_p) { |
| 1231 | return LY_SUCCESS; |
| 1232 | } |
| 1233 | if (*features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1234 | offset = LY_ARRAY_COUNT(*features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1235 | } |
| 1236 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1237 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1238 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1239 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1240 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1241 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1242 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1243 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1244 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1245 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1246 | DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1247 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1248 | (*features)[offset + u].module = ctx_sc->mod; |
| 1249 | |
| 1250 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1251 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1252 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1253 | |
| 1254 | return LY_SUCCESS; |
| 1255 | } |
| 1256 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1257 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1258 | * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement). |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1259 | * |
| 1260 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1261 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1262 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1263 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1264 | * being currently processed). |
| 1265 | * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature) |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1266 | * @return LY_SUCCESS if everything is ok. |
| 1267 | * @return LY_EVALID if the feature references indirectly itself. |
| 1268 | */ |
| 1269 | static LY_ERR |
| 1270 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1271 | { |
| 1272 | LY_ERR ret = LY_EVALID; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1273 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1274 | struct ly_set recursion = {0}; |
| 1275 | struct lysc_feature *drv; |
| 1276 | |
| 1277 | if (!depfeatures) { |
| 1278 | return LY_SUCCESS; |
| 1279 | } |
| 1280 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1281 | for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1282 | if (feature == depfeatures[u]) { |
| 1283 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1284 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1285 | goto cleanup; |
| 1286 | } |
| 1287 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1288 | } |
| 1289 | |
| 1290 | for (v = 0; v < recursion.count; ++v) { |
| 1291 | drv = recursion.objs[v]; |
| 1292 | if (!drv->depfeatures) { |
| 1293 | continue; |
| 1294 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1295 | for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1296 | if (feature == drv->depfeatures[u]) { |
| 1297 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1298 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1299 | goto cleanup; |
| 1300 | } |
| 1301 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1302 | } |
| 1303 | } |
| 1304 | ret = LY_SUCCESS; |
| 1305 | |
| 1306 | cleanup: |
| 1307 | ly_set_erase(&recursion, NULL); |
| 1308 | return ret; |
| 1309 | } |
| 1310 | |
| 1311 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1312 | * @brief Create pre-compiled features array. |
| 1313 | * |
| 1314 | * See lys_feature_precompile() for more details. |
| 1315 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1316 | * @param[in] ctx Compile context. |
| 1317 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1318 | * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1319 | * @return LY_ERR value. |
| 1320 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1321 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1322 | lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1323 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1324 | LY_ARRAY_COUNT_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1325 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1326 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1327 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1328 | /* find the preprecompiled feature */ |
| 1329 | LY_ARRAY_FOR(features, x) { |
| 1330 | if (strcmp(features[x].name, feature_p->name)) { |
| 1331 | continue; |
| 1332 | } |
| 1333 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1334 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1335 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1336 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1337 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1338 | COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1339 | COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1340 | if (feature->iffeatures) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1341 | for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1342 | if (feature->iffeatures[u].features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1343 | for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1344 | /* check for circular dependency - direct reference first,... */ |
| 1345 | if (feature == feature->iffeatures[u].features[v]) { |
| 1346 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1347 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1348 | return LY_EVALID; |
| 1349 | } |
| 1350 | /* ... and indirect circular reference */ |
| 1351 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1352 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1353 | /* add itself into the dependants list */ |
| 1354 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1355 | *df = feature; |
| 1356 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1357 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1358 | } |
| 1359 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1360 | lysc_update_path(ctx, NULL, NULL); |
| 1361 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1362 | done: |
| 1363 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1364 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1365 | |
| 1366 | LOGINT(ctx->ctx); |
| 1367 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1368 | } |
| 1369 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1370 | /** |
| 1371 | * @brief Revert compiled list of features back to the precompiled state. |
| 1372 | * |
| 1373 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1374 | * The features are supposed to be stored again as dis_features in ::lys_module structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1375 | * |
| 1376 | * @param[in] ctx Compilation context. |
| 1377 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1378 | * and supposed to hold the dis_features list. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1379 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1380 | static void |
| 1381 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1382 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1383 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1384 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 1385 | if (mod->compiled) { |
| 1386 | /* keep the dis_features list until the complete lys_module is freed */ |
| 1387 | mod->dis_features = mod->compiled->features; |
| 1388 | mod->compiled->features = NULL; |
| 1389 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1390 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1391 | /* in the dis_features list, remove all the parts (from finished compiling process) |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1392 | * which may points into the data being freed here */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1393 | LY_ARRAY_FOR(mod->dis_features, u) { |
| 1394 | LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) { |
| 1395 | lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1396 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1397 | LY_ARRAY_FREE(mod->dis_features[u].iffeatures); |
| 1398 | mod->dis_features[u].iffeatures = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1399 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1400 | LY_ARRAY_FOR(mod->dis_features[u].exts, v) { |
| 1401 | lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1402 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1403 | LY_ARRAY_FREE(mod->dis_features[u].exts); |
| 1404 | mod->dis_features[u].exts = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1405 | } |
| 1406 | } |
| 1407 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1408 | /** |
| 1409 | * @brief Validate and normalize numeric value from a range definition. |
| 1410 | * @param[in] ctx Compile context. |
| 1411 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1412 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1413 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1414 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1415 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1416 | * @param[in] value String value of the range boundary. |
| 1417 | * @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. |
| 1418 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1419 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1420 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1421 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1422 | range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1423 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1424 | size_t fraction = 0, size; |
| 1425 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1426 | *len = 0; |
| 1427 | |
| 1428 | assert(value); |
| 1429 | /* parse value */ |
| 1430 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1431 | return LY_EVALID; |
| 1432 | } |
| 1433 | |
| 1434 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1435 | ++(*len); |
| 1436 | } |
| 1437 | |
| 1438 | while (isdigit(value[*len])) { |
| 1439 | ++(*len); |
| 1440 | } |
| 1441 | |
| 1442 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1443 | if (basetype == LY_TYPE_DEC64) { |
| 1444 | goto decimal; |
| 1445 | } else { |
| 1446 | *valcopy = strndup(value, *len); |
| 1447 | return LY_SUCCESS; |
| 1448 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1449 | } |
| 1450 | fraction = *len; |
| 1451 | |
| 1452 | ++(*len); |
| 1453 | while (isdigit(value[*len])) { |
| 1454 | ++(*len); |
| 1455 | } |
| 1456 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1457 | if (basetype == LY_TYPE_DEC64) { |
| 1458 | decimal: |
| 1459 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1460 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1461 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1462 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1463 | *len, value, frdigits); |
| 1464 | return LY_EINVAL; |
| 1465 | } |
| 1466 | if (fraction) { |
| 1467 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1468 | } else { |
| 1469 | size = (*len) + frdigits + 1; |
| 1470 | } |
| 1471 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1472 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1473 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1474 | (*valcopy)[size - 1] = '\0'; |
| 1475 | if (fraction) { |
| 1476 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1477 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1478 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1479 | } else { |
| 1480 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1481 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1482 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1483 | } |
| 1484 | return LY_SUCCESS; |
| 1485 | } |
| 1486 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1487 | /** |
| 1488 | * @brief Check that values in range are in ascendant order. |
| 1489 | * @param[in] unsigned_value Flag to note that we are working with unsigned values. |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1490 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1491 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1492 | * @param[in] value Current value to check. |
| 1493 | * @param[in] prev_value The last seen value. |
| 1494 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1495 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1496 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1497 | range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1498 | { |
| 1499 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1500 | if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1501 | return LY_EEXIST; |
| 1502 | } |
| 1503 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1504 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1505 | return LY_EEXIST; |
| 1506 | } |
| 1507 | } |
| 1508 | return LY_SUCCESS; |
| 1509 | } |
| 1510 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1511 | /** |
| 1512 | * @brief Set min/max value of the range part. |
| 1513 | * @param[in] ctx Compile context. |
| 1514 | * @param[in] part Range part structure to fill. |
| 1515 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1516 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1517 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1518 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1519 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1520 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1521 | * @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. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1522 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1523 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1524 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1525 | * frdigits value), LY_EMEM. |
| 1526 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1527 | static LY_ERR |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 1528 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1529 | int first, int length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1530 | { |
| 1531 | LY_ERR ret = LY_SUCCESS; |
| 1532 | char *valcopy = NULL; |
| 1533 | size_t len; |
| 1534 | |
| 1535 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1536 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1537 | LY_CHECK_GOTO(ret, finalize); |
| 1538 | } |
| 1539 | if (!valcopy && base_range) { |
| 1540 | if (max) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1541 | part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1542 | } else { |
| 1543 | part->min_64 = base_range->parts[0].min_64; |
| 1544 | } |
| 1545 | if (!first) { |
| 1546 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1547 | } |
| 1548 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1552 | case LY_TYPE_INT8: /* range */ |
| 1553 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1554 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1555 | } else if (max) { |
| 1556 | part->max_64 = INT64_C(127); |
| 1557 | } else { |
| 1558 | part->min_64 = INT64_C(-128); |
| 1559 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1560 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1561 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1562 | } |
| 1563 | break; |
| 1564 | case LY_TYPE_INT16: /* range */ |
| 1565 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1566 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1567 | } else if (max) { |
| 1568 | part->max_64 = INT64_C(32767); |
| 1569 | } else { |
| 1570 | part->min_64 = INT64_C(-32768); |
| 1571 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1572 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1573 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1574 | } |
| 1575 | break; |
| 1576 | case LY_TYPE_INT32: /* range */ |
| 1577 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1578 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1579 | } else if (max) { |
| 1580 | part->max_64 = INT64_C(2147483647); |
| 1581 | } else { |
| 1582 | part->min_64 = INT64_C(-2147483648); |
| 1583 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1584 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1585 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1586 | } |
| 1587 | break; |
| 1588 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1589 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1590 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1591 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1592 | max ? &part->max_64 : &part->min_64); |
| 1593 | } else if (max) { |
| 1594 | part->max_64 = INT64_C(9223372036854775807); |
| 1595 | } else { |
| 1596 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1597 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1598 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1599 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1600 | } |
| 1601 | break; |
| 1602 | case LY_TYPE_UINT8: /* range */ |
| 1603 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1604 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1605 | } else if (max) { |
| 1606 | part->max_u64 = UINT64_C(255); |
| 1607 | } else { |
| 1608 | part->min_u64 = UINT64_C(0); |
| 1609 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1610 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1611 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1612 | } |
| 1613 | break; |
| 1614 | case LY_TYPE_UINT16: /* range */ |
| 1615 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1616 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1617 | } else if (max) { |
| 1618 | part->max_u64 = UINT64_C(65535); |
| 1619 | } else { |
| 1620 | part->min_u64 = UINT64_C(0); |
| 1621 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1622 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1623 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1624 | } |
| 1625 | break; |
| 1626 | case LY_TYPE_UINT32: /* range */ |
| 1627 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1628 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1629 | } else if (max) { |
| 1630 | part->max_u64 = UINT64_C(4294967295); |
| 1631 | } else { |
| 1632 | part->min_u64 = UINT64_C(0); |
| 1633 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1634 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1635 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1636 | } |
| 1637 | break; |
| 1638 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1639 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1640 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1641 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1642 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1643 | } else if (max) { |
| 1644 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1645 | } else { |
| 1646 | part->min_u64 = UINT64_C(0); |
| 1647 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1648 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1649 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1650 | } |
| 1651 | break; |
| 1652 | default: |
| 1653 | LOGINT(ctx->ctx); |
| 1654 | ret = LY_EINT; |
| 1655 | } |
| 1656 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1657 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1658 | if (ret == LY_EDENIED) { |
| 1659 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1660 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1661 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1662 | } else if (ret == LY_EVALID) { |
| 1663 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1664 | "Invalid %s restriction - invalid value \"%s\".", |
| 1665 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1666 | } else if (ret == LY_EEXIST) { |
| 1667 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1668 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1669 | length_restr ? "length" : "range", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1670 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1671 | } else if (!ret && value) { |
| 1672 | *value = *value + len; |
| 1673 | } |
| 1674 | free(valcopy); |
| 1675 | return ret; |
| 1676 | } |
| 1677 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1678 | /** |
| 1679 | * @brief Compile the parsed range restriction. |
| 1680 | * @param[in] ctx Compile context. |
| 1681 | * @param[in] range_p Parsed range structure to compile. |
| 1682 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1683 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1684 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1685 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1686 | * range restriction must be more restrictive than the base_range. |
| 1687 | * @param[in,out] range Pointer to the created current range structure. |
| 1688 | * @return LY_ERR value. |
| 1689 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1690 | static LY_ERR |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 1691 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1692 | uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1693 | { |
| 1694 | LY_ERR ret = LY_EVALID; |
| 1695 | const char *expr; |
| 1696 | struct lysc_range_part *parts = NULL, *part; |
| 1697 | int range_expected = 0, uns; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1698 | LY_ARRAY_COUNT_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1699 | |
| 1700 | assert(range); |
| 1701 | assert(range_p); |
| 1702 | |
| 1703 | expr = range_p->arg; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1704 | while (1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1705 | if (isspace(*expr)) { |
| 1706 | ++expr; |
| 1707 | } else if (*expr == '\0') { |
| 1708 | if (range_expected) { |
| 1709 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1710 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1711 | length_restr ? "length" : "range", range_p->arg); |
| 1712 | goto cleanup; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1713 | } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1714 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1715 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1716 | length_restr ? "length" : "range", range_p->arg); |
| 1717 | goto cleanup; |
| 1718 | } |
| 1719 | parts_done++; |
| 1720 | break; |
| 1721 | } else if (!strncmp(expr, "min", 3)) { |
| 1722 | if (parts) { |
| 1723 | /* min cannot be used elsewhere than in the first part */ |
| 1724 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1725 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1726 | expr - range_p->arg, range_p->arg); |
| 1727 | goto cleanup; |
| 1728 | } |
| 1729 | expr += 3; |
| 1730 | |
| 1731 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1732 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1733 | part->max_64 = part->min_64; |
| 1734 | } else if (*expr == '|') { |
| 1735 | if (!parts || range_expected) { |
| 1736 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1737 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1738 | goto cleanup; |
| 1739 | } |
| 1740 | expr++; |
| 1741 | parts_done++; |
| 1742 | /* process next part of the expression */ |
| 1743 | } else if (!strncmp(expr, "..", 2)) { |
| 1744 | expr += 2; |
| 1745 | while (isspace(*expr)) { |
| 1746 | expr++; |
| 1747 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1748 | if (!parts || LY_ARRAY_COUNT(parts) == parts_done) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1749 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1750 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1751 | goto cleanup; |
| 1752 | } |
| 1753 | /* continue expecting the upper boundary */ |
| 1754 | range_expected = 1; |
| 1755 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1756 | /* number */ |
| 1757 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1758 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1759 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1760 | range_expected = 0; |
| 1761 | } else { |
| 1762 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1763 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1764 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1765 | part->max_64 = part->min_64; |
| 1766 | } |
| 1767 | |
| 1768 | /* continue with possible another expression part */ |
| 1769 | } else if (!strncmp(expr, "max", 3)) { |
| 1770 | expr += 3; |
| 1771 | while (isspace(*expr)) { |
| 1772 | expr++; |
| 1773 | } |
| 1774 | if (*expr != '\0') { |
| 1775 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1776 | length_restr ? "length" : "range", expr); |
| 1777 | goto cleanup; |
| 1778 | } |
| 1779 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1780 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1781 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1782 | range_expected = 0; |
| 1783 | } else { |
| 1784 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1785 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1786 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1787 | part->min_64 = part->max_64; |
| 1788 | } |
| 1789 | } else { |
| 1790 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1791 | length_restr ? "length" : "range", expr); |
| 1792 | goto cleanup; |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | /* check with the previous range/length restriction */ |
| 1797 | if (base_range) { |
| 1798 | switch (basetype) { |
| 1799 | case LY_TYPE_BINARY: |
| 1800 | case LY_TYPE_UINT8: |
| 1801 | case LY_TYPE_UINT16: |
| 1802 | case LY_TYPE_UINT32: |
| 1803 | case LY_TYPE_UINT64: |
| 1804 | case LY_TYPE_STRING: |
| 1805 | uns = 1; |
| 1806 | break; |
| 1807 | case LY_TYPE_DEC64: |
| 1808 | case LY_TYPE_INT8: |
| 1809 | case LY_TYPE_INT16: |
| 1810 | case LY_TYPE_INT32: |
| 1811 | case LY_TYPE_INT64: |
| 1812 | uns = 0; |
| 1813 | break; |
| 1814 | default: |
| 1815 | LOGINT(ctx->ctx); |
| 1816 | ret = LY_EINT; |
| 1817 | goto cleanup; |
| 1818 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1819 | for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1820 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1821 | goto baseerror; |
| 1822 | } |
| 1823 | /* current lower bound is not lower than the base */ |
| 1824 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1825 | /* base has single value */ |
| 1826 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1827 | /* both lower bounds are the same */ |
| 1828 | if (parts[u].min_64 != parts[u].max_64) { |
| 1829 | /* current continues with a range */ |
| 1830 | goto baseerror; |
| 1831 | } else { |
| 1832 | /* equal single values, move both forward */ |
| 1833 | ++v; |
| 1834 | continue; |
| 1835 | } |
| 1836 | } else { |
| 1837 | /* base is single value lower than current range, so the |
| 1838 | * value from base range is removed in the current, |
| 1839 | * move only base and repeat checking */ |
| 1840 | ++v; |
| 1841 | --u; |
| 1842 | continue; |
| 1843 | } |
| 1844 | } else { |
| 1845 | /* base is the range */ |
| 1846 | if (parts[u].min_64 == parts[u].max_64) { |
| 1847 | /* current is a single value */ |
| 1848 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1849 | /* current is behind the base range, so base range is omitted, |
| 1850 | * move the base and keep the current for further check */ |
| 1851 | ++v; |
| 1852 | --u; |
| 1853 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1854 | continue; |
| 1855 | } else { |
| 1856 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1857 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1858 | /* higher bound is higher than the current higher bound */ |
| 1859 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1860 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1861 | * continue with the same current, but move the base */ |
| 1862 | --u; |
| 1863 | ++v; |
| 1864 | continue; |
| 1865 | } |
| 1866 | /* current range starts within the base range but end behind it */ |
| 1867 | goto baseerror; |
| 1868 | } else { |
| 1869 | /* current range is smaller than the base, |
| 1870 | * move current, but stay with the base */ |
| 1871 | continue; |
| 1872 | } |
| 1873 | } |
| 1874 | } |
| 1875 | } |
| 1876 | if (u != parts_done) { |
| 1877 | baseerror: |
| 1878 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1879 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1880 | length_restr ? "length" : "range", range_p->arg); |
| 1881 | goto cleanup; |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | if (!(*range)) { |
| 1886 | *range = calloc(1, sizeof **range); |
| 1887 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1888 | } |
| 1889 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1890 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1891 | if (range_p->eapptag) { |
| 1892 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1893 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1894 | } |
| 1895 | if (range_p->emsg) { |
| 1896 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1897 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1898 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1899 | if (range_p->dsc) { |
| 1900 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1901 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1902 | } |
| 1903 | if (range_p->ref) { |
| 1904 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1905 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1906 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1907 | /* extensions are taken only from the last range by the caller */ |
| 1908 | |
| 1909 | (*range)->parts = parts; |
| 1910 | parts = NULL; |
| 1911 | ret = LY_SUCCESS; |
| 1912 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1913 | LY_ARRAY_FREE(parts); |
| 1914 | |
| 1915 | return ret; |
| 1916 | } |
| 1917 | |
| 1918 | /** |
| 1919 | * @brief Checks pattern syntax. |
| 1920 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1921 | * @param[in] ctx Context. |
| 1922 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1923 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1924 | * @param[in,out] pcre2_code Compiled PCRE2 pattern. If NULL, the compiled information used to validate pattern are freed. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1925 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1926 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1927 | LY_ERR |
| 1928 | lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1929 | { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1930 | int idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1931 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1932 | int err_code; |
| 1933 | const char *orig_ptr; |
| 1934 | PCRE2_SIZE err_offset; |
| 1935 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1936 | #define URANGE_LEN 19 |
| 1937 | char *ublock2urange[][2] = { |
| 1938 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1939 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1940 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1941 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1942 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1943 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1944 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1945 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1946 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1947 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1948 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1949 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1950 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1951 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1952 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1953 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1954 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1955 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1956 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1957 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1958 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1959 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1960 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1961 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1962 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1963 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1964 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1965 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1966 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1967 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1968 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1969 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1970 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1971 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1972 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1973 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1974 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1975 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1976 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1977 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1978 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1979 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1980 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1981 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1982 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1983 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1984 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1985 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1986 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1987 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1988 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1989 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1990 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1991 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1992 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1993 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1994 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1995 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1996 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1997 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1998 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1999 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 2000 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 2001 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 2002 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 2003 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 2004 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 2005 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 2006 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 2007 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 2008 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 2009 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 2010 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 2011 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 2012 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 2013 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 2014 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 2015 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 2016 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 2017 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 2018 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 2019 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 2020 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 2021 | {NULL, NULL} |
| 2022 | }; |
| 2023 | |
| 2024 | /* adjust the expression to a Perl equivalent |
| 2025 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 2026 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2027 | /* allocate space for the transformed pattern */ |
| 2028 | size = strlen(pattern) + 1; |
| 2029 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2030 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2031 | perl_regex[0] = '\0'; |
| 2032 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2033 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 2034 | brack = 0; |
| 2035 | idx = 0; |
| 2036 | orig_ptr = pattern; |
| 2037 | while (orig_ptr[0]) { |
| 2038 | switch (orig_ptr[0]) { |
| 2039 | case '$': |
| 2040 | case '^': |
| 2041 | if (!brack) { |
| 2042 | /* make space for the extra character */ |
| 2043 | ++size; |
| 2044 | perl_regex = ly_realloc(perl_regex, size); |
| 2045 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2046 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2047 | /* print escape slash */ |
| 2048 | perl_regex[idx] = '\\'; |
| 2049 | ++idx; |
| 2050 | } |
| 2051 | break; |
| 2052 | case '[': |
| 2053 | /* must not be escaped */ |
| 2054 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2055 | ++brack; |
| 2056 | } |
| 2057 | break; |
| 2058 | case ']': |
| 2059 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2060 | /* pattern was checked and compiled already */ |
| 2061 | assert(brack); |
| 2062 | --brack; |
| 2063 | } |
| 2064 | break; |
| 2065 | default: |
| 2066 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2067 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2068 | |
| 2069 | /* copy char */ |
| 2070 | perl_regex[idx] = orig_ptr[0]; |
| 2071 | |
| 2072 | ++idx; |
| 2073 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2074 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2075 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2076 | |
| 2077 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2078 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2079 | start = ptr - perl_regex; |
| 2080 | |
| 2081 | ptr = strchr(ptr, '}'); |
| 2082 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2083 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2084 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 2085 | free(perl_regex); |
| 2086 | return LY_EVALID; |
| 2087 | } |
| 2088 | end = (ptr - perl_regex) + 1; |
| 2089 | |
| 2090 | /* need more space */ |
| 2091 | if (end - start < URANGE_LEN) { |
| 2092 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2093 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | /* find our range */ |
| 2097 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2098 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2099 | break; |
| 2100 | } |
| 2101 | } |
| 2102 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2103 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2104 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2105 | free(perl_regex); |
| 2106 | return LY_EVALID; |
| 2107 | } |
| 2108 | |
| 2109 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2110 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2111 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2112 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2113 | } |
| 2114 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2115 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2116 | } |
| 2117 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2118 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2119 | /* skip brackets */ |
| 2120 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2121 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2122 | } else { |
| 2123 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2124 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2129 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2130 | PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE, |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2131 | &err_code, &err_offset, NULL); |
| 2132 | if (!code_local) { |
| 2133 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2134 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2135 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2136 | free(perl_regex); |
| 2137 | return LY_EVALID; |
| 2138 | } |
| 2139 | free(perl_regex); |
| 2140 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2141 | if (code) { |
| 2142 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2143 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2144 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | return LY_SUCCESS; |
| 2148 | |
| 2149 | #undef URANGE_LEN |
| 2150 | } |
| 2151 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2152 | /** |
| 2153 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2154 | * @param[in] ctx Compile context. |
| 2155 | * @param[in] patterns_p Array of parsed patterns from the current type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2156 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2157 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2158 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2159 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2160 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2161 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2162 | lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2163 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2164 | { |
| 2165 | struct lysc_pattern **pattern; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2166 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2167 | LY_ERR ret = LY_SUCCESS; |
| 2168 | |
| 2169 | /* first, copy the patterns from the base type */ |
| 2170 | if (base_patterns) { |
| 2171 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2172 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2173 | } |
| 2174 | |
| 2175 | LY_ARRAY_FOR(patterns_p, u) { |
| 2176 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2177 | *pattern = calloc(1, sizeof **pattern); |
| 2178 | ++(*pattern)->refcount; |
| 2179 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2180 | ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2181 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2182 | |
| 2183 | if (patterns_p[u].arg[0] == 0x15) { |
| 2184 | (*pattern)->inverted = 1; |
| 2185 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2186 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2187 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2188 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2189 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2190 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2191 | COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2192 | } |
| 2193 | done: |
| 2194 | return ret; |
| 2195 | } |
| 2196 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2197 | /** |
| 2198 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2199 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2200 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2201 | 0 /* LY_TYPE_UNKNOWN */, |
| 2202 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2203 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2204 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2205 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2206 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2207 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2208 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2209 | 0 /* LY_TYPE_BOOL */, |
| 2210 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2211 | 0 /* LY_TYPE_EMPTY */, |
| 2212 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2213 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2214 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2215 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2216 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2217 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2218 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2219 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2220 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2221 | }; |
| 2222 | |
| 2223 | /** |
| 2224 | * @brief stringification of the YANG built-in data types |
| 2225 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2226 | const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2227 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2228 | "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2229 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2230 | /** |
| 2231 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2232 | * @param[in] ctx Compile context. |
| 2233 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2234 | * @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. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2235 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2236 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2237 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2238 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2239 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2240 | lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2241 | struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2242 | { |
| 2243 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2244 | LY_ARRAY_COUNT_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2245 | int32_t value = 0; |
| 2246 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2247 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2248 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2249 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2250 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2251 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2252 | return LY_EVALID; |
| 2253 | } |
| 2254 | |
| 2255 | LY_ARRAY_FOR(enums_p, u) { |
| 2256 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2257 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2258 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2259 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2260 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2261 | if (base_enums) { |
| 2262 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2263 | LY_ARRAY_FOR(base_enums, v) { |
| 2264 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2265 | break; |
| 2266 | } |
| 2267 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2268 | if (v == LY_ARRAY_COUNT(base_enums)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2269 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2270 | "Invalid %s - derived type adds new item \"%s\".", |
| 2271 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2272 | return LY_EVALID; |
| 2273 | } |
| 2274 | match = v; |
| 2275 | } |
| 2276 | |
| 2277 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2278 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2279 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2280 | e->value = (int32_t)enums_p[u].value; |
| 2281 | if (!u || e->value >= value) { |
| 2282 | value = e->value + 1; |
| 2283 | } |
| 2284 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2285 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2286 | if (e->value == (*enums)[v].value) { |
| 2287 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2288 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2289 | e->value, e->name, (*enums)[v].name); |
| 2290 | return LY_EVALID; |
| 2291 | } |
| 2292 | } |
| 2293 | } else if (base_enums) { |
| 2294 | /* inherit the assigned value */ |
| 2295 | e->value = base_enums[match].value; |
| 2296 | if (!u || e->value >= value) { |
| 2297 | value = e->value + 1; |
| 2298 | } |
| 2299 | } else { |
| 2300 | /* assign value automatically */ |
| 2301 | if (u && value == INT32_MIN) { |
| 2302 | /* counter overflow */ |
| 2303 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2304 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2305 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2306 | return LY_EVALID; |
| 2307 | } |
| 2308 | e->value = value++; |
| 2309 | } |
| 2310 | } else { /* LY_TYPE_BITS */ |
| 2311 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2312 | e->value = (int32_t)enums_p[u].value; |
| 2313 | if (!u || (uint32_t)e->value >= position) { |
| 2314 | position = (uint32_t)e->value + 1; |
| 2315 | } |
| 2316 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2317 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2318 | if (e->value == (*enums)[v].value) { |
| 2319 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2320 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2321 | (uint32_t)e->value, e->name, (*enums)[v].name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2322 | return LY_EVALID; |
| 2323 | } |
| 2324 | } |
| 2325 | } else if (base_enums) { |
| 2326 | /* inherit the assigned value */ |
| 2327 | e->value = base_enums[match].value; |
| 2328 | if (!u || (uint32_t)e->value >= position) { |
| 2329 | position = (uint32_t)e->value + 1; |
| 2330 | } |
| 2331 | } else { |
| 2332 | /* assign value automatically */ |
| 2333 | if (u && position == 0) { |
| 2334 | /* counter overflow */ |
| 2335 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2336 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2337 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2338 | return LY_EVALID; |
| 2339 | } |
| 2340 | e->value = position++; |
| 2341 | } |
| 2342 | } |
| 2343 | |
| 2344 | if (base_enums) { |
| 2345 | /* the assigned values must not change from the derived type */ |
| 2346 | if (e->value != base_enums[match].value) { |
| 2347 | if (basetype == LY_TYPE_ENUM) { |
| 2348 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2349 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2350 | e->name, base_enums[match].value, e->value); |
| 2351 | } else { |
| 2352 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2353 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2354 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2355 | } |
| 2356 | return LY_EVALID; |
| 2357 | } |
| 2358 | } |
| 2359 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2360 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2361 | COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM : LYEXT_PAR_TYPE_BIT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2362 | |
| 2363 | if (basetype == LY_TYPE_BITS) { |
| 2364 | /* keep bits ordered by position */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2365 | for (v = u; v && (*enums)[v - 1].value > e->value; --v) {} |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2366 | if (v != u) { |
| 2367 | memcpy(&storage, e, sizeof *e); |
| 2368 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2369 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2370 | } |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | done: |
| 2375 | return ret; |
| 2376 | } |
| 2377 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2378 | /** |
| 2379 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2380 | * |
| 2381 | * path-arg = absolute-path / relative-path |
| 2382 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2383 | * relative-path = 1*(".." "/") descendant-path |
| 2384 | * |
| 2385 | * @param[in,out] path Path to parse. |
| 2386 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2387 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2388 | * @param[out] name Name of the token. |
| 2389 | * @param[out] nam_len Length of the name. |
| 2390 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2391 | * must not be changed between consecutive calls. -1 if the |
| 2392 | * path is absolute. |
| 2393 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2394 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2395 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2396 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2397 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2398 | int *parent_times, int *has_predicate) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2399 | { |
| 2400 | int par_times = 0; |
| 2401 | |
| 2402 | assert(path && *path); |
| 2403 | assert(parent_times); |
| 2404 | assert(prefix); |
| 2405 | assert(prefix_len); |
| 2406 | assert(name); |
| 2407 | assert(name_len); |
| 2408 | assert(has_predicate); |
| 2409 | |
| 2410 | *prefix = NULL; |
| 2411 | *prefix_len = 0; |
| 2412 | *name = NULL; |
| 2413 | *name_len = 0; |
| 2414 | *has_predicate = 0; |
| 2415 | |
| 2416 | if (!*parent_times) { |
| 2417 | if (!strncmp(*path, "..", 2)) { |
| 2418 | *path += 2; |
| 2419 | ++par_times; |
| 2420 | while (!strncmp(*path, "/..", 3)) { |
| 2421 | *path += 3; |
| 2422 | ++par_times; |
| 2423 | } |
| 2424 | } |
| 2425 | if (par_times) { |
| 2426 | *parent_times = par_times; |
| 2427 | } else { |
| 2428 | *parent_times = -1; |
| 2429 | } |
| 2430 | } |
| 2431 | |
| 2432 | if (**path != '/') { |
| 2433 | return LY_EINVAL; |
| 2434 | } |
| 2435 | /* skip '/' */ |
| 2436 | ++(*path); |
| 2437 | |
| 2438 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2439 | LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2440 | |
| 2441 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2442 | /* path continues by another token or this is the last token */ |
| 2443 | return LY_SUCCESS; |
| 2444 | } else if ((*path)[0] != '[') { |
| 2445 | /* unexpected character */ |
| 2446 | return LY_EINVAL; |
| 2447 | } else { |
| 2448 | /* predicate starting with [ */ |
| 2449 | *has_predicate = 1; |
| 2450 | return LY_SUCCESS; |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2455 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2456 | * |
| 2457 | * The set of features used for target must be a subset of features used for the leafref. |
| 2458 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2459 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2460 | * |
| 2461 | * @param[in] refnode The leafref node. |
| 2462 | * @param[in] target Tha target node of the leafref. |
| 2463 | * @return LY_SUCCESS or LY_EVALID; |
| 2464 | */ |
| 2465 | static LY_ERR |
| 2466 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2467 | { |
| 2468 | LY_ERR ret = LY_EVALID; |
| 2469 | const struct lysc_node *iter; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2470 | LY_ARRAY_COUNT_TYPE u, v, count; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2471 | struct ly_set features = {0}; |
| 2472 | |
| 2473 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2474 | if (iter->iffeatures) { |
| 2475 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2476 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2477 | LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup); |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2478 | } |
| 2479 | } |
| 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2484 | * So when adding features applicable to the target into the features set, we should not be |
| 2485 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2486 | * to the leafref itself. */ |
| 2487 | count = features.count; |
| 2488 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2489 | if (iter->iffeatures) { |
| 2490 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2491 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2492 | if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) { |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2493 | /* new feature was added (or LY_EMEM) */ |
| 2494 | goto cleanup; |
| 2495 | } |
| 2496 | } |
| 2497 | } |
| 2498 | } |
| 2499 | } |
| 2500 | ret = LY_SUCCESS; |
| 2501 | |
| 2502 | cleanup: |
| 2503 | ly_set_erase(&features, NULL); |
| 2504 | return ret; |
| 2505 | } |
| 2506 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2507 | static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2508 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2509 | struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2510 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2511 | /** |
| 2512 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2513 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2514 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2515 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2516 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2517 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2518 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2519 | * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2520 | * @param[in] basetype Base YANG built-in type of the type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2521 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2522 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2523 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2524 | * @return LY_ERR value. |
| 2525 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2526 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2527 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2528 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2529 | struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, |
| 2530 | struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2531 | { |
| 2532 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2533 | struct lysc_type_bin *bin; |
| 2534 | struct lysc_type_num *num; |
| 2535 | struct lysc_type_str *str; |
| 2536 | struct lysc_type_bits *bits; |
| 2537 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2538 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2539 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2540 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2541 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2542 | |
| 2543 | switch (basetype) { |
| 2544 | case LY_TYPE_BINARY: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2545 | bin = (struct lysc_type_bin *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2546 | |
| 2547 | /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2548 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2549 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2550 | base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2551 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2552 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | if (tpdfname) { |
| 2557 | type_p->compiled = *type; |
| 2558 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2559 | } |
| 2560 | break; |
| 2561 | case LY_TYPE_BITS: |
| 2562 | /* RFC 7950 9.7 - bits */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2563 | bits = (struct lysc_type_bits *)(*type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2564 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2565 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2566 | base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL, |
| 2567 | (struct lysc_type_bitenum_item **)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2568 | } |
| 2569 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2570 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2571 | /* type derived from bits built-in type must contain at least one bit */ |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2572 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2573 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2574 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2575 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2576 | free(*type); |
| 2577 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2578 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2579 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | if (tpdfname) { |
| 2583 | type_p->compiled = *type; |
| 2584 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2585 | } |
| 2586 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2587 | case LY_TYPE_DEC64: |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2588 | dec = (struct lysc_type_dec *)(*type); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2589 | |
| 2590 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2591 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2592 | if (!type_p->fraction_digits) { |
| 2593 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2594 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2595 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2596 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", ""); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2597 | free(*type); |
| 2598 | *type = NULL; |
| 2599 | } |
| 2600 | return LY_EVALID; |
| 2601 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2602 | dec->fraction_digits = type_p->fraction_digits; |
| 2603 | } else { |
| 2604 | if (type_p->fraction_digits) { |
| 2605 | /* fraction digits is prohibited in types not directly derived from built-in decimal64 */ |
| 2606 | if (tpdfname) { |
| 2607 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2608 | "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.", |
| 2609 | tpdfname); |
| 2610 | } else { |
| 2611 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2612 | "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type."); |
| 2613 | free(*type); |
| 2614 | *type = NULL; |
| 2615 | } |
| 2616 | return LY_EVALID; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2617 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2618 | dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2619 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2620 | |
| 2621 | /* RFC 7950 9.2.4 - range */ |
| 2622 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2623 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2624 | base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2625 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2626 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, done); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2627 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2628 | } |
| 2629 | |
| 2630 | if (tpdfname) { |
| 2631 | type_p->compiled = *type; |
| 2632 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2633 | } |
| 2634 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2635 | case LY_TYPE_STRING: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2636 | str = (struct lysc_type_str *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2637 | |
| 2638 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2639 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2640 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2641 | base ? ((struct lysc_type_str *)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2642 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2643 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2644 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2645 | } else if (base && ((struct lysc_type_str *)base)->length) { |
| 2646 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | /* RFC 7950 9.4.5 - pattern */ |
| 2650 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2651 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2652 | base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns)); |
| 2653 | } else if (base && ((struct lysc_type_str *)base)->patterns) { |
| 2654 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | if (tpdfname) { |
| 2658 | type_p->compiled = *type; |
| 2659 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2660 | } |
| 2661 | break; |
| 2662 | case LY_TYPE_ENUM: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2663 | enumeration = (struct lysc_type_enum *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2664 | |
| 2665 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2666 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2667 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2668 | base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2669 | } |
| 2670 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2671 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2672 | /* type derived from enumerations built-in type must contain at least one enum */ |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2673 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2674 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2675 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2676 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2677 | free(*type); |
| 2678 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2679 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2680 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | if (tpdfname) { |
| 2684 | type_p->compiled = *type; |
| 2685 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2686 | } |
| 2687 | break; |
| 2688 | case LY_TYPE_INT8: |
| 2689 | case LY_TYPE_UINT8: |
| 2690 | case LY_TYPE_INT16: |
| 2691 | case LY_TYPE_UINT16: |
| 2692 | case LY_TYPE_INT32: |
| 2693 | case LY_TYPE_UINT32: |
| 2694 | case LY_TYPE_INT64: |
| 2695 | case LY_TYPE_UINT64: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2696 | num = (struct lysc_type_num *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2697 | |
| 2698 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2699 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2700 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2701 | base ? ((struct lysc_type_num *)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2702 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2703 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | if (tpdfname) { |
| 2708 | type_p->compiled = *type; |
| 2709 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2710 | } |
| 2711 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2712 | case LY_TYPE_IDENT: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2713 | idref = (struct lysc_type_identityref *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2714 | |
| 2715 | /* RFC 7950 9.10.2 - base */ |
| 2716 | if (type_p->bases) { |
| 2717 | if (base) { |
| 2718 | /* only the directly derived identityrefs can contain base specification */ |
| 2719 | if (tpdfname) { |
| 2720 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2721 | "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.", |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2722 | tpdfname); |
| 2723 | } else { |
| 2724 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2725 | "Invalid base substatement for the type not directly derived from identityref built-in type."); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2726 | free(*type); |
| 2727 | *type = NULL; |
| 2728 | } |
| 2729 | return LY_EVALID; |
| 2730 | } |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2731 | LY_CHECK_RET(lys_compile_identity_bases(ctx, module, type_p->bases, NULL, &idref->bases)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2732 | } |
| 2733 | |
| 2734 | if (!base && !type_p->flags) { |
| 2735 | /* type derived from identityref built-in type must contain at least one base */ |
| 2736 | if (tpdfname) { |
| 2737 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2738 | } else { |
| 2739 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2740 | free(*type); |
| 2741 | *type = NULL; |
| 2742 | } |
| 2743 | return LY_EVALID; |
| 2744 | } |
| 2745 | |
| 2746 | if (tpdfname) { |
| 2747 | type_p->compiled = *type; |
| 2748 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2749 | } |
| 2750 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2751 | case LY_TYPE_LEAFREF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2752 | lref = (struct lysc_type_leafref *)*type; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2753 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2754 | /* RFC 7950 9.9.3 - require-instance */ |
| 2755 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2756 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2757 | if (tpdfname) { |
| 2758 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2759 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2760 | } else { |
| 2761 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2762 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2763 | free(*type); |
| 2764 | *type = NULL; |
| 2765 | } |
| 2766 | return LY_EVALID; |
| 2767 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2768 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2769 | } else if (base) { |
| 2770 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2771 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2772 | } else { |
| 2773 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2774 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2775 | } |
| 2776 | if (type_p->path) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2777 | lref->path = lyxp_expr_dup(ctx->ctx, type_p->path); |
| 2778 | lref->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2779 | } else if (base) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2780 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path); |
| 2781 | lref->path_context = ((struct lysc_type_leafref *)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2782 | } else if (tpdfname) { |
| 2783 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2784 | return LY_EVALID; |
| 2785 | } else { |
| 2786 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2787 | free(*type); |
| 2788 | *type = NULL; |
| 2789 | return LY_EVALID; |
| 2790 | } |
| 2791 | if (tpdfname) { |
| 2792 | type_p->compiled = *type; |
| 2793 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2794 | } |
| 2795 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2796 | case LY_TYPE_INST: |
| 2797 | /* RFC 7950 9.9.3 - require-instance */ |
| 2798 | if (type_p->flags & LYS_SET_REQINST) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2799 | ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2800 | } else { |
| 2801 | /* default is true */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2802 | ((struct lysc_type_instanceid *)(*type))->require_instance = 1; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | if (tpdfname) { |
| 2806 | type_p->compiled = *type; |
| 2807 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2808 | } |
| 2809 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2810 | case LY_TYPE_UNION: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2811 | un = (struct lysc_type_union *)(*type); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2812 | |
| 2813 | /* RFC 7950 7.4 - type */ |
| 2814 | if (type_p->types) { |
| 2815 | if (base) { |
| 2816 | /* only the directly derived union can contain types specification */ |
| 2817 | if (tpdfname) { |
| 2818 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2819 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2820 | tpdfname); |
| 2821 | } else { |
| 2822 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2823 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2824 | free(*type); |
| 2825 | *type = NULL; |
| 2826 | } |
| 2827 | return LY_EVALID; |
| 2828 | } |
| 2829 | /* compile the type */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2830 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID); |
| 2831 | for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(type_p->types); ++u) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2832 | LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2833 | &type_p->types[u], &un->types[u + additional], NULL, NULL, NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2834 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2835 | /* add space for additional types from the union subtype */ |
| 2836 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2837 | LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1, |
| 2838 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2839 | |
| 2840 | /* copy subtypes of the subtype union */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2841 | for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2842 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2843 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2844 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2845 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx); lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2846 | lref = (struct lysc_type_leafref *)un->types[u + additional]; |
| 2847 | |
| 2848 | lref->basetype = LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2849 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2850 | lref->refcount = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2851 | lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance; |
| 2852 | lref->path_context = ((struct lysc_type_leafref *)un_aux->types[v])->path_context; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2853 | /* TODO extensions */ |
| 2854 | |
| 2855 | } else { |
| 2856 | un->types[u + additional] = un_aux->types[v]; |
| 2857 | ++un_aux->types[v]->refcount; |
| 2858 | } |
| 2859 | ++additional; |
| 2860 | LY_ARRAY_INCREMENT(un->types); |
| 2861 | } |
| 2862 | /* compensate u increment in main loop */ |
| 2863 | --additional; |
| 2864 | |
| 2865 | /* free the replaced union subtype */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2866 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2867 | } else { |
| 2868 | LY_ARRAY_INCREMENT(un->types); |
| 2869 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2870 | } |
| 2871 | } |
| 2872 | |
| 2873 | if (!base && !type_p->flags) { |
| 2874 | /* type derived from union built-in type must contain at least one type */ |
| 2875 | if (tpdfname) { |
| 2876 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2877 | } else { |
| 2878 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2879 | free(*type); |
| 2880 | *type = NULL; |
| 2881 | } |
| 2882 | return LY_EVALID; |
| 2883 | } |
| 2884 | |
| 2885 | if (tpdfname) { |
| 2886 | type_p->compiled = *type; |
| 2887 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2888 | } |
| 2889 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2890 | case LY_TYPE_BOOL: |
| 2891 | case LY_TYPE_EMPTY: |
| 2892 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2893 | break; |
| 2894 | } |
| 2895 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2896 | done: |
| 2897 | return ret; |
| 2898 | } |
| 2899 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2900 | /** |
| 2901 | * @brief Compile information about the leaf/leaf-list's type. |
| 2902 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2903 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2904 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2905 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2906 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2907 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2908 | * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2909 | * @param[out] units Storage for inheriting units value from the typedefs the current type derives from. |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2910 | * @param[out] dflt Default value for the type. |
| 2911 | * @param[out] dflt_mod Local module for the default value. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2912 | * @return LY_ERR value. |
| 2913 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2914 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2915 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2916 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2917 | struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2918 | { |
| 2919 | LY_ERR ret = LY_SUCCESS; |
| 2920 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2921 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2922 | struct type_context { |
| 2923 | const struct lysp_tpdf *tpdf; |
| 2924 | struct lysp_node *node; |
| 2925 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2926 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2927 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2928 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2929 | struct ly_set tpdf_chain = {0}; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2930 | |
| 2931 | assert((dflt && dflt_mod) || (!dflt && !dflt_mod)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2932 | |
| 2933 | (*type) = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2934 | if (dflt) { |
| 2935 | *dflt = NULL; |
| 2936 | *dflt_mod = NULL; |
| 2937 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2938 | |
| 2939 | tctx = calloc(1, sizeof *tctx); |
| 2940 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2941 | for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2942 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2943 | ret == LY_SUCCESS; |
| 2944 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2945 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2946 | if (basetype) { |
| 2947 | break; |
| 2948 | } |
| 2949 | |
| 2950 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2951 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2952 | tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2953 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2954 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2955 | if (units && !*units) { |
| 2956 | /* inherit units */ |
| 2957 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2958 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2959 | if (dflt && !*dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2960 | /* inherit default */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2961 | *dflt = tctx->tpdf->dflt; |
| 2962 | *dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2963 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2964 | if (dummyloops && (!units || *units) && dflt && *dflt) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2965 | basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2966 | break; |
| 2967 | } |
| 2968 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2969 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2970 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2971 | * but we still may need to inherit default and units values, so start dummy loops */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2972 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2973 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2974 | if ((units && !*units) || (dflt && !*dflt)) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2975 | dummyloops = 1; |
| 2976 | goto preparenext; |
| 2977 | } else { |
| 2978 | tctx = NULL; |
| 2979 | break; |
| 2980 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2981 | } |
| 2982 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2983 | /* circular typedef reference detection */ |
| 2984 | for (u = 0; u < tpdf_chain.count; u++) { |
| 2985 | /* local part */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2986 | tctx_iter = (struct type_context *)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2987 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2988 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2989 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2990 | free(tctx); |
| 2991 | ret = LY_EVALID; |
| 2992 | goto cleanup; |
| 2993 | } |
| 2994 | } |
| 2995 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 2996 | /* global part for unions corner case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 2997 | tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2998 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2999 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3000 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3001 | free(tctx); |
| 3002 | ret = LY_EVALID; |
| 3003 | goto cleanup; |
| 3004 | } |
| 3005 | } |
| 3006 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3007 | /* store information for the following processing */ |
| 3008 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3009 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3010 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3011 | /* prepare next loop */ |
| 3012 | tctx_prev = tctx; |
| 3013 | tctx = calloc(1, sizeof *tctx); |
| 3014 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3015 | } |
| 3016 | free(tctx); |
| 3017 | |
| 3018 | /* allocate type according to the basetype */ |
| 3019 | switch (basetype) { |
| 3020 | case LY_TYPE_BINARY: |
| 3021 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3022 | break; |
| 3023 | case LY_TYPE_BITS: |
| 3024 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3025 | break; |
| 3026 | case LY_TYPE_BOOL: |
| 3027 | case LY_TYPE_EMPTY: |
| 3028 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3029 | break; |
| 3030 | case LY_TYPE_DEC64: |
| 3031 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3032 | break; |
| 3033 | case LY_TYPE_ENUM: |
| 3034 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3035 | break; |
| 3036 | case LY_TYPE_IDENT: |
| 3037 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3038 | break; |
| 3039 | case LY_TYPE_INST: |
| 3040 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3041 | break; |
| 3042 | case LY_TYPE_LEAFREF: |
| 3043 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3044 | break; |
| 3045 | case LY_TYPE_STRING: |
| 3046 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3047 | break; |
| 3048 | case LY_TYPE_UNION: |
| 3049 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3050 | break; |
| 3051 | case LY_TYPE_INT8: |
| 3052 | case LY_TYPE_UINT8: |
| 3053 | case LY_TYPE_INT16: |
| 3054 | case LY_TYPE_UINT16: |
| 3055 | case LY_TYPE_INT32: |
| 3056 | case LY_TYPE_UINT32: |
| 3057 | case LY_TYPE_INT64: |
| 3058 | case LY_TYPE_UINT64: |
| 3059 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3060 | break; |
| 3061 | case LY_TYPE_UNKNOWN: |
| 3062 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3063 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3064 | ret = LY_EVALID; |
| 3065 | goto cleanup; |
| 3066 | } |
| 3067 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3068 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3069 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3070 | ly_data_type2str[basetype]); |
| 3071 | free(*type); |
| 3072 | (*type) = NULL; |
| 3073 | ret = LY_EVALID; |
| 3074 | goto cleanup; |
| 3075 | } |
| 3076 | |
| 3077 | /* get restrictions from the referred typedefs */ |
| 3078 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3079 | tctx = (struct type_context *)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3080 | |
| 3081 | /* remember the typedef context for circular check */ |
| 3082 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3083 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3084 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3085 | base = tctx->tpdf->type.compiled; |
| 3086 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3087 | } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3088 | /* no change, just use the type information from the base */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3089 | base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3090 | ++base->refcount; |
| 3091 | continue; |
| 3092 | } |
| 3093 | |
| 3094 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3095 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3096 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3097 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3098 | ret = LY_EVALID; |
| 3099 | goto cleanup; |
| 3100 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3101 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3102 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3103 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3104 | ret = LY_EVALID; |
| 3105 | goto cleanup; |
| 3106 | } |
| 3107 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3108 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3109 | /* TODO user type plugins */ |
| 3110 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3111 | prev_type = *type; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3112 | ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, &((struct lysp_tpdf *)tctx->tpdf)->type, |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 3113 | basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3114 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3115 | LY_CHECK_GOTO(ret, cleanup); |
| 3116 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3117 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3118 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3119 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3120 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3121 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3122 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3123 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3124 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3125 | /* TODO user type plugins */ |
| 3126 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3127 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3128 | ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, NULL, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3129 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 90b148f | 2020-05-06 17:49:40 +0200 | [diff] [blame] | 3130 | } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3131 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3132 | free(*type); |
| 3133 | (*type) = base; |
| 3134 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3135 | } |
| 3136 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3137 | COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3138 | |
| 3139 | cleanup: |
| 3140 | ly_set_erase(&tpdf_chain, free); |
| 3141 | return ret; |
| 3142 | } |
| 3143 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3144 | /** |
| 3145 | * @brief Compile status information of the given node. |
| 3146 | * |
| 3147 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3148 | * has the status correctly set during the compilation. |
| 3149 | * |
| 3150 | * @param[in] ctx Compile context |
| 3151 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3152 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3153 | * the compatibility with the parent's status value. |
| 3154 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3155 | * @return LY_ERR value. |
| 3156 | */ |
| 3157 | static LY_ERR |
| 3158 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3159 | { |
| 3160 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3161 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3162 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3163 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3164 | if ((parent_flags & 0x3) != 0x3) { |
| 3165 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3166 | * combination of bits (0x3) which marks the uses_status value */ |
| 3167 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3168 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3169 | } |
| 3170 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3171 | } else { |
| 3172 | (*node_flags) |= LYS_STATUS_CURR; |
| 3173 | } |
| 3174 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3175 | /* check status compatibility with the parent */ |
| 3176 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3177 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3178 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3179 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3180 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3181 | } else { /* LYS_STATUS_DEPRC */ |
| 3182 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3183 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3184 | } |
| 3185 | return LY_EVALID; |
| 3186 | } |
| 3187 | } |
| 3188 | return LY_SUCCESS; |
| 3189 | } |
| 3190 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3191 | /** |
| 3192 | * @brief Check uniqness of the node/action/notification name. |
| 3193 | * |
| 3194 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3195 | * structures, but they share the namespace so we need to check their name collisions. |
| 3196 | * |
| 3197 | * @param[in] ctx Compile context. |
| 3198 | * @param[in] children List (linked list) of data nodes to go through. |
| 3199 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3200 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3201 | * @param[in] name Name of the item to find in the given lists. |
| 3202 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3203 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3204 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3205 | */ |
| 3206 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3207 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3208 | const struct lysc_notif *notifs, const char *name, void *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3209 | { |
| 3210 | const struct lysc_node *iter; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3211 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3212 | |
| 3213 | LY_LIST_FOR(children, iter) { |
| 3214 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3215 | goto error; |
| 3216 | } |
| 3217 | } |
| 3218 | LY_ARRAY_FOR(actions, u) { |
| 3219 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3220 | goto error; |
| 3221 | } |
| 3222 | } |
| 3223 | LY_ARRAY_FOR(notifs, u) { |
| 3224 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3225 | goto error; |
| 3226 | } |
| 3227 | } |
| 3228 | return LY_SUCCESS; |
| 3229 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3230 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification"); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3231 | return LY_EEXIST; |
| 3232 | } |
| 3233 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3234 | static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3235 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3236 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3237 | * @brief Compile parsed RPC/action schema node information. |
| 3238 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3239 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3240 | * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action) |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3241 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3242 | * @param[in] uses_status If the RPC/action is being placed instead of uses, here we have the uses's status value (as node's flags). |
| 3243 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3244 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3245 | */ |
| 3246 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3247 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3248 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3249 | { |
| 3250 | LY_ERR ret = LY_SUCCESS; |
| 3251 | struct lysp_node *child_p; |
| 3252 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3253 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3254 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3255 | lysc_update_path(ctx, parent, action_p->name); |
| 3256 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3257 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3258 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3259 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3260 | action_p->name, action)) { |
| 3261 | return LY_EVALID; |
| 3262 | } |
| 3263 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3264 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3265 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3266 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3267 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3268 | return LY_EVALID; |
| 3269 | } |
| 3270 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3271 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3272 | action->module = ctx->mod; |
| 3273 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3274 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3275 | action->sp = action_p; |
| 3276 | } |
| 3277 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3278 | |
| 3279 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3280 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 3281 | LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3282 | |
| 3283 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3284 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3285 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3286 | COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3287 | COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3288 | |
| 3289 | /* input */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3290 | lysc_update_path(ctx, (struct lysc_node *)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3291 | COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3292 | COMPILE_EXTS_GOTO(ctx, action_p->input.exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3293 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3294 | LY_LIST_FOR(action_p->input.data, child_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3295 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3296 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3297 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3298 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3299 | |
| 3300 | /* output */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3301 | lysc_update_path(ctx, (struct lysc_node *)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3302 | COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3303 | COMPILE_EXTS_GOTO(ctx, action_p->output.exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3304 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3305 | LY_LIST_FOR(action_p->output.data, child_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3306 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)action, uses_status)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3307 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3308 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3309 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3310 | |
| 3311 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3312 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3313 | ly_set_add(&ctx->xpath, action, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3314 | } |
| 3315 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3316 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3317 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3318 | return ret; |
| 3319 | } |
| 3320 | |
| 3321 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3322 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3323 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3324 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3325 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3326 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3327 | * @param[in] uses_status If the Notification is being placed instead of uses, here we have the uses's status value (as node's flags). |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3328 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3329 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3330 | */ |
| 3331 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3332 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3333 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3334 | { |
| 3335 | LY_ERR ret = LY_SUCCESS; |
| 3336 | struct lysp_node *child_p; |
| 3337 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3338 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3339 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3340 | lysc_update_path(ctx, parent, notif_p->name); |
| 3341 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3342 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3343 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3344 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3345 | notif_p->name, notif)) { |
| 3346 | return LY_EVALID; |
| 3347 | } |
| 3348 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3349 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3350 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3351 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3352 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3353 | return LY_EVALID; |
| 3354 | } |
| 3355 | |
| 3356 | notif->nodetype = LYS_NOTIF; |
| 3357 | notif->module = ctx->mod; |
| 3358 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3359 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3360 | notif->sp = notif_p; |
| 3361 | } |
| 3362 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3363 | |
| 3364 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3365 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3366 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3367 | |
| 3368 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3369 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3370 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3371 | COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3372 | COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3373 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3374 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3375 | ly_set_add(&ctx->xpath, notif, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3376 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3377 | COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3378 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3379 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3380 | LY_LIST_FOR(notif_p->data, child_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3381 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3382 | } |
| 3383 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3384 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3385 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3386 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3387 | return ret; |
| 3388 | } |
| 3389 | |
| 3390 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3391 | * @brief Compile parsed container node information. |
| 3392 | * @param[in] ctx Compile context |
| 3393 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3394 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3395 | * is enriched with the container-specific information. |
| 3396 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3397 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3398 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3399 | lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3400 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3401 | struct lysp_node_container *cont_p = (struct lysp_node_container *)node_p; |
| 3402 | struct lysc_node_container *cont = (struct lysc_node_container *)node; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3403 | struct lysp_node *child_p; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3404 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3405 | LY_ERR ret = LY_SUCCESS; |
| 3406 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3407 | if (cont_p->presence) { |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3408 | /* explicit presence */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3409 | cont->flags |= LYS_PRESENCE; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3410 | } else if (cont_p->musts) { |
| 3411 | /* container with a must condition */ |
Radek Krejci | 175f25b | 2020-08-13 12:02:36 +0200 | [diff] [blame] | 3412 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name); |
| 3413 | cont->flags |= LYS_PRESENCE; |
| 3414 | } else if (cont_p->when) { |
| 3415 | /* container with a when condition */ |
| 3416 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"when\" condition.", cont_p->name); |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3417 | cont->flags |= LYS_PRESENCE; |
| 3418 | } else if (cont_p->parent) { |
| 3419 | if (cont_p->parent->nodetype == LYS_CHOICE) { |
| 3420 | /* container is an implicit case, so its existence decides the existence of the whole case */ |
Radek Krejci | 175f25b | 2020-08-13 12:02:36 +0200 | [diff] [blame] | 3421 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".", |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3422 | cont_p->name, cont_p->parent->name); |
| 3423 | cont->flags |= LYS_PRESENCE; |
| 3424 | } else if ((cont_p->parent->nodetype == LYS_CASE) |
| 3425 | && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) { |
| 3426 | /* container is the only node in a case, so its existence decides the existence of the whole case */ |
Radek Krejci | 175f25b | 2020-08-13 12:02:36 +0200 | [diff] [blame] | 3427 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning as a case of choice \"%s\".", |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3428 | cont_p->name, cont_p->parent->name); |
| 3429 | cont->flags |= LYS_PRESENCE; |
| 3430 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3431 | } |
| 3432 | |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3433 | /* more cases when the container has meaning but is kept NP for convenience: |
| 3434 | * - when condition |
| 3435 | * - direct child action/notification |
| 3436 | */ |
| 3437 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3438 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3439 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3440 | } |
| 3441 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3442 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3443 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3444 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3445 | ly_set_add(&ctx->xpath, cont, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3446 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3447 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3448 | COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3449 | |
| 3450 | done: |
| 3451 | return ret; |
| 3452 | } |
| 3453 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3454 | /* |
| 3455 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3456 | * @param[in] ctx Compile context. |
| 3457 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3458 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3459 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3460 | * @return LY_ERR value. |
| 3461 | */ |
| 3462 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3463 | lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3464 | struct lysc_node_leaf *leaf) |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3465 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3466 | const char *dflt; |
| 3467 | struct lys_module *dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3468 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3469 | LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, &leaf->type, |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3470 | leaf->units ? NULL : &leaf->units, &dflt, &dflt_mod)); |
| 3471 | |
| 3472 | /* store default value, if any */ |
| 3473 | if (dflt && !(leaf->flags & LYS_SET_DFLT)) { |
| 3474 | LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, dflt, dflt_mod)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3475 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3476 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3477 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3478 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3479 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3480 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3481 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3482 | LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) { |
| 3483 | if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3484 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3485 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3486 | } |
| 3487 | } |
| 3488 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3489 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3490 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3491 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3492 | return LY_EVALID; |
| 3493 | } |
| 3494 | } |
| 3495 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3496 | return LY_SUCCESS; |
| 3497 | } |
| 3498 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3499 | /** |
| 3500 | * @brief Compile parsed leaf node information. |
| 3501 | * @param[in] ctx Compile context |
| 3502 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3503 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3504 | * is enriched with the leaf-specific information. |
| 3505 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3506 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3507 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3508 | lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3509 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3510 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)node_p; |
| 3511 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 3512 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3513 | LY_ERR ret = LY_SUCCESS; |
| 3514 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3515 | COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3516 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3517 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3518 | ly_set_add(&ctx->xpath, leaf, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3519 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3520 | if (leaf_p->units) { |
| 3521 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3522 | leaf->flags |= LYS_SET_UNITS; |
| 3523 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3524 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3525 | /* compile type */ |
| 3526 | LY_CHECK_RET(lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf)); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3527 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3528 | /* store/update default value */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3529 | if (leaf_p->dflt) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3530 | LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, leaf_p->dflt, ctx->mod_def)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3531 | leaf->flags |= LYS_SET_DFLT; |
| 3532 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3533 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3534 | done: |
| 3535 | return ret; |
| 3536 | } |
| 3537 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3538 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3539 | * @brief Compile parsed leaf-list node information. |
| 3540 | * @param[in] ctx Compile context |
| 3541 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3542 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3543 | * is enriched with the leaf-list-specific information. |
| 3544 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3545 | */ |
| 3546 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3547 | lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3548 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3549 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)node_p; |
| 3550 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3551 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3552 | LY_ERR ret = LY_SUCCESS; |
| 3553 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3554 | COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3555 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3556 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3557 | ly_set_add(&ctx->xpath, llist, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3558 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3559 | if (llist_p->units) { |
| 3560 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3561 | llist->flags |= LYS_SET_UNITS; |
| 3562 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3563 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3564 | /* compile type */ |
| 3565 | LY_CHECK_RET(lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf *)llist)); |
Michal Vasko | 6a044b2 | 2020-01-15 12:25:39 +0100 | [diff] [blame] | 3566 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3567 | /* store/update default values */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3568 | if (llist_p->dflts) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3569 | LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, llist_p->dflts, ctx->mod_def), done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3570 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3574 | if (llist->min) { |
| 3575 | llist->flags |= LYS_MAND_TRUE; |
| 3576 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3577 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3578 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3579 | done: |
| 3580 | return ret; |
| 3581 | } |
| 3582 | |
| 3583 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3584 | * @brief Compile information about list's uniques. |
| 3585 | * @param[in] ctx Compile context. |
| 3586 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3587 | * @param[in] uniques Sized array list of unique statements. |
| 3588 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3589 | * @return LY_ERR value. |
| 3590 | */ |
| 3591 | static LY_ERR |
| 3592 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3593 | { |
| 3594 | LY_ERR ret = LY_SUCCESS; |
| 3595 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3596 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3597 | const char *keystr, *delim; |
| 3598 | size_t len; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3599 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3600 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3601 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3602 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3603 | for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3604 | config = -1; |
| 3605 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3606 | keystr = uniques[v]; |
| 3607 | while (keystr) { |
| 3608 | delim = strpbrk(keystr, " \t\n"); |
| 3609 | if (delim) { |
| 3610 | len = delim - keystr; |
| 3611 | while (isspace(*delim)) { |
| 3612 | ++delim; |
| 3613 | } |
| 3614 | } else { |
| 3615 | len = strlen(keystr); |
| 3616 | } |
| 3617 | |
| 3618 | /* unique node must be present */ |
| 3619 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3620 | ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, context_module, LYS_LEAF, 0, |
| 3621 | (const struct lysc_node **)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3622 | if (ret != LY_SUCCESS) { |
| 3623 | if (ret == LY_EDENIED) { |
| 3624 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3625 | "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.", |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3626 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3627 | } |
| 3628 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3629 | } else if (flags) { |
| 3630 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3631 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3632 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3633 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | /* all referenced leafs must be of the same config type */ |
| 3637 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3638 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3639 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3640 | return LY_EVALID; |
| 3641 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3642 | config = 1; |
| 3643 | } else { /* LYS_CONFIG_R */ |
| 3644 | config = 0; |
| 3645 | } |
| 3646 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3647 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3648 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3649 | if (parent->nodetype == LYS_LIST) { |
| 3650 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3651 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3652 | return LY_EVALID; |
| 3653 | } |
| 3654 | } |
| 3655 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3656 | /* check status */ |
| 3657 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3658 | (*key)->flags, (*key)->module, (*key)->name)); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3659 | |
| 3660 | /* mark leaf as unique */ |
| 3661 | (*key)->flags |= LYS_UNIQUE; |
| 3662 | |
| 3663 | /* next unique value in line */ |
| 3664 | keystr = delim; |
| 3665 | } |
| 3666 | /* next unique definition */ |
| 3667 | } |
| 3668 | |
| 3669 | return LY_SUCCESS; |
| 3670 | } |
| 3671 | |
| 3672 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3673 | * @brief Compile parsed list node information. |
| 3674 | * @param[in] ctx Compile context |
| 3675 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3676 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3677 | * is enriched with the list-specific information. |
| 3678 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3679 | */ |
| 3680 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3681 | lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3682 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3683 | struct lysp_node_list *list_p = (struct lysp_node_list *)node_p; |
| 3684 | struct lysc_node_list *list = (struct lysc_node_list *)node; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3685 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3686 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3687 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3688 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3689 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3690 | LY_ERR ret = LY_SUCCESS; |
| 3691 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3692 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3693 | if (list->min) { |
| 3694 | list->flags |= LYS_MAND_TRUE; |
| 3695 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3696 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3697 | |
| 3698 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3699 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3700 | } |
| 3701 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3702 | COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3703 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3704 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3705 | ly_set_add(&ctx->xpath, list, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3706 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3707 | |
| 3708 | /* keys */ |
| 3709 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3710 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3711 | return LY_EVALID; |
| 3712 | } |
| 3713 | |
| 3714 | /* find all the keys (must be direct children) */ |
| 3715 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3716 | if (!keystr) { |
| 3717 | /* keyless list */ |
| 3718 | list->flags |= LYS_KEYLESS; |
| 3719 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3720 | while (keystr) { |
| 3721 | delim = strpbrk(keystr, " \t\n"); |
| 3722 | if (delim) { |
| 3723 | len = delim - keystr; |
| 3724 | while (isspace(*delim)) { |
| 3725 | ++delim; |
| 3726 | } |
| 3727 | } else { |
| 3728 | len = strlen(keystr); |
| 3729 | } |
| 3730 | |
| 3731 | /* key node must be present */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3732 | key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3733 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3735 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3736 | return LY_EVALID; |
| 3737 | } |
| 3738 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3739 | if (key->flags & LYS_KEY) { |
| 3740 | /* the node was already marked as a key */ |
| 3741 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3742 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3743 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3744 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3745 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3746 | lysc_update_path(ctx, (struct lysc_node *)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3747 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3748 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3749 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3750 | return LY_EVALID; |
| 3751 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3752 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3753 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3754 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3755 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3756 | "List's key cannot be of \"empty\" type until it is in YANG 1.1 module."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3757 | return LY_EVALID; |
| 3758 | } |
| 3759 | } else { |
| 3760 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3761 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3762 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3763 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3764 | return LY_EVALID; |
| 3765 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3766 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3767 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3768 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3769 | return LY_EVALID; |
| 3770 | } |
| 3771 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3772 | |
| 3773 | /* check status */ |
| 3774 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3775 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3776 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3777 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3778 | if (key->dflt) { |
| 3779 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3780 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3781 | free(key->dflt); |
| 3782 | key->dflt = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3783 | } |
| 3784 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3785 | key->flags |= LYS_KEY; |
| 3786 | |
| 3787 | /* move it to the correct position */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3788 | if ((prev_key && (struct lysc_node *)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3789 | /* fix links in closest previous siblings of the key */ |
| 3790 | if (key->next) { |
| 3791 | key->next->prev = key->prev; |
| 3792 | } else { |
| 3793 | /* last child */ |
| 3794 | list->child->prev = key->prev; |
| 3795 | } |
| 3796 | if (key->prev->next) { |
| 3797 | key->prev->next = key->next; |
| 3798 | } |
| 3799 | /* fix links in the key */ |
| 3800 | if (prev_key) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3801 | key->prev = (struct lysc_node *)prev_key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3802 | key->next = prev_key->next; |
| 3803 | } else { |
| 3804 | key->prev = list->child->prev; |
| 3805 | key->next = list->child; |
| 3806 | } |
| 3807 | /* fix links in closes future siblings of the key */ |
| 3808 | if (prev_key) { |
| 3809 | if (prev_key->next) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3810 | prev_key->next->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3811 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3812 | list->child->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3813 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3814 | prev_key->next = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3815 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3816 | list->child->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3817 | } |
| 3818 | /* fix links in parent */ |
| 3819 | if (!key->prev->next) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3820 | list->child = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3821 | } |
| 3822 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3823 | |
| 3824 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3825 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3826 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3827 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3828 | } |
| 3829 | |
| 3830 | /* uniques */ |
| 3831 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3832 | LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3833 | } |
| 3834 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3835 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3836 | COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3837 | |
| 3838 | done: |
| 3839 | return ret; |
| 3840 | } |
| 3841 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3842 | /** |
| 3843 | * @brief Do some checks and set the default choice's case. |
| 3844 | * |
| 3845 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3846 | * |
| 3847 | * @param[in] ctx Compile context. |
| 3848 | * @param[in] dflt Name of the default branch. Can contain even the prefix, but it make sense only in case it is the prefix of the module itself, |
| 3849 | * not the reference to the imported module. |
| 3850 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3851 | * @return LY_ERR value. |
| 3852 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3853 | static LY_ERR |
| 3854 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3855 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3856 | struct lysc_node *iter, *node = (struct lysc_node *)ch; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3857 | const char *prefix = NULL, *name; |
| 3858 | size_t prefix_len = 0; |
| 3859 | |
| 3860 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3861 | name = strchr(dflt, ':'); |
| 3862 | if (name) { |
| 3863 | prefix = dflt; |
| 3864 | prefix_len = name - prefix; |
| 3865 | ++name; |
| 3866 | } else { |
| 3867 | name = dflt; |
| 3868 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 3869 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3870 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3871 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3872 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3873 | prefix_len, prefix); |
| 3874 | return LY_EVALID; |
| 3875 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3876 | ch->dflt = (struct lysc_node_case *)lys_find_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3877 | if (!ch->dflt) { |
| 3878 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3879 | "Default case \"%s\" not found.", dflt); |
| 3880 | return LY_EVALID; |
| 3881 | } |
| 3882 | /* no mandatory nodes directly under the default case */ |
| 3883 | LY_LIST_FOR(ch->dflt->child, iter) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3884 | if (iter->parent != (struct lysc_node *)ch->dflt) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3885 | break; |
| 3886 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3887 | if (iter->flags & LYS_MAND_TRUE) { |
| 3888 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3889 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3890 | return LY_EVALID; |
| 3891 | } |
| 3892 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3893 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3894 | return LY_SUCCESS; |
| 3895 | } |
| 3896 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3897 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3898 | lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3899 | { |
| 3900 | struct lys_module *mod; |
| 3901 | const char *prefix = NULL, *name; |
| 3902 | size_t prefix_len = 0; |
| 3903 | struct lysc_node_case *cs; |
| 3904 | struct lysc_node *node; |
| 3905 | |
| 3906 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3907 | name = strchr(dflt, ':'); |
| 3908 | if (name) { |
| 3909 | prefix = dflt; |
| 3910 | prefix_len = name - prefix; |
| 3911 | ++name; |
| 3912 | } else { |
| 3913 | name = dflt; |
| 3914 | } |
| 3915 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3916 | if (prefix) { |
| 3917 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3918 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3919 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3920 | "The prefix does not match any imported module of the deviation module.", dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3921 | return LY_EVALID; |
| 3922 | } |
| 3923 | } else { |
| 3924 | mod = ctx->mod; |
| 3925 | } |
| 3926 | /* get the default case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3927 | cs = (struct lysc_node_case *)lys_find_child((struct lysc_node *)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3928 | if (!cs) { |
| 3929 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3930 | "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3931 | return LY_EVALID; |
| 3932 | } |
| 3933 | |
| 3934 | /* check that there is no mandatory node */ |
| 3935 | LY_LIST_FOR(cs->child, node) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3936 | if (node->parent != (struct lysc_node *)cs) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3937 | break; |
| 3938 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3939 | if (node->flags & LYS_MAND_TRUE) { |
| 3940 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3941 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 3942 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3943 | return LY_EVALID; |
| 3944 | } |
| 3945 | } |
| 3946 | |
| 3947 | /* set the default case in choice */ |
| 3948 | ch->dflt = cs; |
| 3949 | cs->flags |= LYS_SET_DFLT; |
| 3950 | |
| 3951 | return LY_SUCCESS; |
| 3952 | } |
| 3953 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3954 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3955 | * @brief Compile parsed choice node information. |
| 3956 | * @param[in] ctx Compile context |
| 3957 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3958 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3959 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3960 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3961 | */ |
| 3962 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3963 | lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3964 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3965 | struct lysp_node_choice *ch_p = (struct lysp_node_choice *)node_p; |
| 3966 | struct lysc_node_choice *ch = (struct lysc_node_choice *)node; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3967 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3968 | LY_ERR ret = LY_SUCCESS; |
| 3969 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3970 | LY_LIST_FOR(ch_p->child, child_p) { |
| 3971 | if (child_p->nodetype == LYS_CASE) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3972 | LY_LIST_FOR(((struct lysp_node_case *)child_p)->child, case_child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3973 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3974 | } |
| 3975 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3976 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3977 | } |
| 3978 | } |
| 3979 | |
| 3980 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3981 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3982 | LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch)); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3983 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3984 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3985 | return ret; |
| 3986 | } |
| 3987 | |
| 3988 | /** |
| 3989 | * @brief Compile parsed anydata or anyxml node information. |
| 3990 | * @param[in] ctx Compile context |
| 3991 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3992 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3993 | * is enriched with the any-specific information. |
| 3994 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3995 | */ |
| 3996 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3997 | lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3998 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 3999 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)node_p; |
| 4000 | struct lysc_node_anydata *any = (struct lysc_node_anydata *)node; |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4001 | unsigned int u; |
| 4002 | LY_ERR ret = LY_SUCCESS; |
| 4003 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4004 | COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4005 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4006 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4007 | ly_set_add(&ctx->xpath, any, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4008 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4009 | |
| 4010 | if (any->flags & LYS_CONFIG_W) { |
Radek Krejci | 5c4ed7b | 2020-08-12 11:29:44 +0200 | [diff] [blame] | 4011 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s", |
| 4012 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4013 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4014 | done: |
| 4015 | return ret; |
| 4016 | } |
| 4017 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4018 | /** |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4019 | * @brief Connect the node into the siblings list and check its name uniqueness. Also, |
| 4020 | * keep specific order of augments targetting the same node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4021 | * |
| 4022 | * @param[in] ctx Compile context |
| 4023 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4024 | * the choice itself is expected instead of a specific case node. |
| 4025 | * @param[in] node Schema node to connect into the list. |
| 4026 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4027 | * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4028 | */ |
| 4029 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4030 | lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4031 | { |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4032 | struct lysc_node **children, *start, *end; |
| 4033 | const struct lys_module *mod; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4034 | |
| 4035 | if (node->nodetype == LYS_CASE) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4036 | children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4037 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4038 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4039 | } |
| 4040 | if (children) { |
| 4041 | if (!(*children)) { |
| 4042 | /* first child */ |
| 4043 | *children = node; |
| 4044 | } else if (*children != node) { |
| 4045 | /* by the condition in previous branch we cover the choice/case children |
| 4046 | * - the children list is shared by the choice and the the first case, in addition |
| 4047 | * the first child of each case must be referenced from the case node. So the node is |
| 4048 | * actually always already inserted in case it is the first children - so here such |
| 4049 | * a situation actually corresponds to the first branch */ |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4050 | if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module) |
| 4051 | && (strcmp((*children)->prev->module->name, node->module->name) > 0)) { |
| 4052 | /* some augments are already connected and we are connecting new ones, |
| 4053 | * keep module name order and insert the node into the children list */ |
| 4054 | end = (*children); |
| 4055 | do { |
| 4056 | end = end->prev; |
| 4057 | mod = end->module; |
| 4058 | while (end->prev->module == mod) { |
| 4059 | end = end->prev; |
| 4060 | } |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 4061 | } while ((end->prev->module != (*children)->module) && (end->prev->module != node->module) && (strcmp(mod->name, node->module->name) > 0)); |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4062 | |
| 4063 | /* we have the last existing node after our node, easily get the first before and connect it */ |
| 4064 | start = end->prev; |
| 4065 | start->next = node; |
| 4066 | node->next = end; |
| 4067 | end->prev = node; |
| 4068 | node->prev = start; |
| 4069 | } else { |
| 4070 | /* insert at the end of the parent's children list */ |
| 4071 | (*children)->prev->next = node; |
| 4072 | node->prev = (*children)->prev; |
| 4073 | (*children)->prev = node; |
| 4074 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4075 | |
| 4076 | /* check the name uniqueness */ |
| 4077 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4078 | lysc_node_notifs(parent), node->name, node)) { |
| 4079 | return LY_EEXIST; |
| 4080 | } |
| 4081 | } |
| 4082 | } |
| 4083 | return LY_SUCCESS; |
| 4084 | } |
| 4085 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4086 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4087 | * @brief Prepare the case structure in choice node for the new data node. |
| 4088 | * |
| 4089 | * 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 |
| 4090 | * created in the choice when the first child was processed. |
| 4091 | * |
| 4092 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4093 | * @param[in] node_p Node image from the parsed tree. If the case is explicit, it is the LYS_CASE node, but in case of implicit case, |
Radek Krejci | 3942480 | 2020-08-12 09:31:00 +0200 | [diff] [blame] | 4094 | * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4095 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4096 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4097 | * @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, |
| 4098 | * it is linked from the case structure only in case it is its first child. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4099 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4100 | static struct lysc_node_case * |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4101 | lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node_choice *ch, struct lysc_node *child) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4102 | { |
| 4103 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4104 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4105 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4106 | unsigned int u; |
| 4107 | LY_ERR ret; |
| 4108 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4109 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4110 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4111 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4112 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4113 | return NULL; \ |
| 4114 | } \ |
| 4115 | } |
| 4116 | |
Radek Krejci | 3942480 | 2020-08-12 09:31:00 +0200 | [diff] [blame] | 4117 | if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4118 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4119 | |
| 4120 | /* we have to add an implicit case node into the parent choice */ |
| 4121 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4122 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4123 | cs->parent = (struct lysc_node *)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4124 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4125 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4126 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4127 | /* the case is already present since the child is not its first children */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4128 | return (struct lysc_node_case *)ch->cases->prev; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4129 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4130 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4131 | |
| 4132 | /* explicit parent case is not present (this is its first child) */ |
| 4133 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4134 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4135 | cs->parent = (struct lysc_node *)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4136 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4137 | cs->sp = node_p; |
| 4138 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4139 | /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */ |
Radek Krejci | bae745f | 2019-04-09 16:28:00 +0200 | [diff] [blame] | 4140 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4141 | |
| 4142 | if (node_p->when) { |
| 4143 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4144 | ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4145 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4146 | |
| 4147 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4148 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4149 | ly_set_add(&ctx->xpath, cs, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4150 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4151 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4152 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4153 | } else { |
| 4154 | LOGINT(ctx->ctx); |
| 4155 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4156 | } |
| 4157 | cs->module = ctx->mod; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4158 | cs->prev = (struct lysc_node *)cs; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4159 | cs->nodetype = LYS_CASE; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4160 | lys_compile_node_connect(ctx, (struct lysc_node *)ch, (struct lysc_node *)cs); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4161 | cs->child = child; |
| 4162 | |
| 4163 | return cs; |
| 4164 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4165 | if (cs) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4166 | lysc_node_free(ctx->ctx, (struct lysc_node *)cs); |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4167 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4168 | return NULL; |
| 4169 | |
| 4170 | #undef UNIQUE_CHECK |
| 4171 | } |
| 4172 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4173 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4174 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4175 | * |
| 4176 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4177 | * @param[in] node Target node where the config is supposed to be changed. |
| 4178 | * @param[in] config_flag Node's config flag to be applied to the @p node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4179 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4180 | * done only on the node for which the refine was created. The function applies also recursively to apply the config change |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4181 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4182 | * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging). |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4183 | * @return LY_ERR value. |
| 4184 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4185 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4186 | lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4187 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4188 | { |
| 4189 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4190 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4191 | |
| 4192 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4193 | /* nothing to do */ |
| 4194 | return LY_SUCCESS; |
| 4195 | } |
| 4196 | |
| 4197 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4198 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4199 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4200 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4201 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4202 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4203 | return LY_EVALID; |
| 4204 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4205 | node->flags |= LYS_SET_CONFIG; |
| 4206 | } else { |
| 4207 | if (node->flags & LYS_SET_CONFIG) { |
| 4208 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4209 | /* setting config flags, but have node with explicit config true */ |
| 4210 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4211 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4212 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4213 | return LY_EVALID; |
| 4214 | } |
| 4215 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4216 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4217 | return LY_SUCCESS; |
| 4218 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4219 | } |
| 4220 | node->flags &= ~LYS_CONFIG_MASK; |
| 4221 | node->flags |= config; |
| 4222 | |
| 4223 | /* inherit the change into the children */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4224 | LY_LIST_FOR((struct lysc_node *)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4225 | LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4226 | } |
| 4227 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4228 | return LY_SUCCESS; |
| 4229 | } |
| 4230 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4231 | /** |
| 4232 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4233 | * |
| 4234 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4235 | * the flag to such parents from a mandatory children. |
| 4236 | * |
| 4237 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4238 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4239 | * (mandatory children was removed). |
| 4240 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4241 | void |
| 4242 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4243 | { |
| 4244 | struct lysc_node *iter; |
| 4245 | |
| 4246 | if (add) { /* set flag */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4247 | for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4248 | parent = parent->parent) { |
| 4249 | parent->flags |= LYS_MAND_TRUE; |
| 4250 | } |
| 4251 | } else { /* unset flag */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4252 | for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4253 | for (iter = (struct lysc_node *)lysc_node_children(parent, 0); iter; iter = iter->next) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4254 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4255 | /* there is another mandatory node */ |
| 4256 | return; |
| 4257 | } |
| 4258 | } |
| 4259 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4260 | parent->flags &= ~LYS_MAND_TRUE; |
| 4261 | } |
| 4262 | } |
| 4263 | } |
| 4264 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4265 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4266 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4267 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4268 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4269 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4270 | */ |
| 4271 | static void |
| 4272 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4273 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4274 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4275 | size_t len; |
| 4276 | |
| 4277 | len = strlen(aug_p->nodeid); |
| 4278 | LY_ARRAY_FOR(result, v) { |
| 4279 | if (strlen(result[v]->nodeid) <= len) { |
| 4280 | continue; |
| 4281 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4282 | if (v < LY_ARRAY_COUNT(result)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4283 | /* move the rest of array */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4284 | memmove(&result[v + 1], &result[v], (LY_ARRAY_COUNT(result) - v) * sizeof *result); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4285 | break; |
| 4286 | } |
| 4287 | } |
| 4288 | result[v] = aug_p; |
| 4289 | LY_ARRAY_INCREMENT(result); |
| 4290 | } |
| 4291 | |
| 4292 | /** |
| 4293 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4294 | * |
| 4295 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4296 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4297 | * |
| 4298 | * @param[in] ctx Compile context. |
| 4299 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4300 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4301 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4302 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4303 | * @return LY_ERR value. |
| 4304 | */ |
| 4305 | LY_ERR |
| 4306 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4307 | { |
| 4308 | struct lysp_augment **result = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4309 | LY_ARRAY_COUNT_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4310 | |
| 4311 | assert(augments); |
| 4312 | |
| 4313 | /* get count of the augments in module and all its submodules */ |
| 4314 | if (aug_p) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4315 | count += LY_ARRAY_COUNT(aug_p); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4316 | } |
| 4317 | LY_ARRAY_FOR(inc_p, u) { |
| 4318 | if (inc_p[u].submodule->augments) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4319 | count += LY_ARRAY_COUNT(inc_p[u].submodule->augments); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4320 | } |
| 4321 | } |
| 4322 | |
| 4323 | if (!count) { |
| 4324 | *augments = NULL; |
| 4325 | return LY_SUCCESS; |
| 4326 | } |
| 4327 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4328 | |
| 4329 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4330 | * together, so there can be even /z/y betwwen them. */ |
| 4331 | LY_ARRAY_FOR(aug_p, u) { |
| 4332 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4333 | } |
| 4334 | LY_ARRAY_FOR(inc_p, u) { |
| 4335 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4336 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4337 | } |
| 4338 | } |
| 4339 | |
| 4340 | *augments = result; |
| 4341 | return LY_SUCCESS; |
| 4342 | } |
| 4343 | |
| 4344 | /** |
| 4345 | * @brief Compile the parsed augment connecting it into its target. |
| 4346 | * |
| 4347 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4348 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4349 | * are already implemented and compiled. |
| 4350 | * |
| 4351 | * @param[in] ctx Compile context. |
| 4352 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4353 | * @param[in] parent Parent node to provide the augment's context. It is NULL for the top level augments and a node holding uses's |
| 4354 | * children in case of the augmenting uses data. |
| 4355 | * @return LY_SUCCESS on success. |
| 4356 | * @return LY_EVALID on failure. |
| 4357 | */ |
| 4358 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4359 | lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent) |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4360 | { |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4361 | LY_ERR ret = LY_SUCCESS, rc; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4362 | struct lysp_node *node_p, *case_node_p; |
| 4363 | struct lysc_node *target; /* target target of the augment */ |
| 4364 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4365 | struct lysc_when **when, *when_shared; |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4366 | struct lys_module **aug_mod; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4367 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4368 | uint16_t flags = 0; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4369 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4370 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4371 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4372 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4373 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4374 | |
Michal Vasko | cb18c7f | 2020-08-24 09:22:28 +0200 | [diff] [blame] | 4375 | ret = lysc_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def, |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4376 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4377 | 1, (const struct lysc_node **)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4378 | if (ret != LY_SUCCESS) { |
| 4379 | if (ret == LY_EDENIED) { |
| 4380 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4381 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4382 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4383 | } |
| 4384 | return LY_EVALID; |
| 4385 | } |
| 4386 | |
| 4387 | /* check for mandatory nodes |
| 4388 | * - new cases augmenting some choice can have mandatory nodes |
| 4389 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4390 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4391 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4392 | allow_mandatory = 1; |
| 4393 | } |
| 4394 | |
| 4395 | when_shared = NULL; |
| 4396 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4397 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4398 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4399 | && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) |
Radek Krejci | 2d56a89 | 2019-02-19 09:05:26 +0100 | [diff] [blame] | 4400 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4401 | && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4402 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4403 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4404 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4405 | return LY_EVALID; |
| 4406 | } |
| 4407 | |
| 4408 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4409 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4410 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4411 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4412 | } else { |
| 4413 | LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4414 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4415 | } |
| 4416 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4417 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4418 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4419 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4420 | * here we gets the last created node as last children of our parent */ |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4421 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4422 | /* the compiled node is the last child of the target (but it is a case, so we have to be careful and stop) */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4423 | for (node = (struct lysc_node *)lysc_node_children(target, flags); node->next && node->next->parent == node->parent; node = node->next) {} |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4424 | } else if (target->nodetype == LYS_CHOICE) { |
| 4425 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4426 | node = ((struct lysc_node_choice *)target)->cases->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4427 | } else { |
| 4428 | /* the compiled node is the last child of the target */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4429 | node = (struct lysc_node *)lysc_node_children(target, flags); |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4430 | if (!node) { |
| 4431 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4432 | break; |
| 4433 | } |
| 4434 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4435 | } |
| 4436 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4437 | if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4438 | node->flags &= ~LYS_MAND_TRUE; |
| 4439 | lys_compile_mandatory_parents(target, 0); |
| 4440 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4441 | "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4442 | return LY_EVALID; |
| 4443 | } |
| 4444 | |
| 4445 | /* pass augment's when to all the children */ |
| 4446 | if (aug_p->when) { |
| 4447 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4448 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4449 | ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4450 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4451 | |
| 4452 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4453 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4454 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4455 | } |
| 4456 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4457 | when_shared = *when; |
| 4458 | } else { |
| 4459 | ++when_shared->refcount; |
| 4460 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4461 | |
| 4462 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4463 | /* in this case check "when" again for all children because of dummy node check */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4464 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4465 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4466 | } |
| 4467 | } |
| 4468 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4469 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4470 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4471 | switch (target->nodetype) { |
| 4472 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4473 | COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container *)target)->actions, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4474 | u, lys_compile_action, 0, ret, error); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4475 | COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container *)target)->notifs, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4476 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4477 | break; |
| 4478 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4479 | COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list *)target)->actions, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4480 | u, lys_compile_action, 0, ret, error); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4481 | COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list *)target)->notifs, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4482 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4483 | break; |
| 4484 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4485 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4486 | if (aug_p->actions) { |
| 4487 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4488 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4489 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4490 | return LY_EVALID; |
| 4491 | } |
| 4492 | if (aug_p->notifs) { |
| 4493 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4494 | "Invalid augment of %s node which is not allowed to contain notification node \"%s\".", |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4495 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4496 | return LY_EVALID; |
| 4497 | } |
| 4498 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4499 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4500 | /* add this module into the target module augmented_by, if not there already */ |
| 4501 | rc = LY_SUCCESS; |
| 4502 | LY_ARRAY_FOR(target->module->compiled->augmented_by, v) { |
| 4503 | if (target->module->compiled->augmented_by[v] == ctx->mod) { |
| 4504 | rc = LY_EEXIST; |
| 4505 | break; |
| 4506 | } |
| 4507 | } |
| 4508 | if (!rc) { |
| 4509 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error); |
| 4510 | *aug_mod = ctx->mod; |
| 4511 | } |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4512 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4513 | lysc_update_path(ctx, NULL, NULL); |
| 4514 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4515 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4516 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4517 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4518 | return ret; |
| 4519 | } |
| 4520 | |
| 4521 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4522 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4523 | * |
| 4524 | * @param[in] ctx Compile context. |
| 4525 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4526 | * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node. |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4527 | * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging). |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4528 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4529 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4530 | * the tests are skipped here, but they are supposed to be performed after all the deviations are applied. |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4531 | * @return LY_ERR value. |
| 4532 | */ |
| 4533 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4534 | lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, int refine_flag) |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4535 | { |
| 4536 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4537 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4538 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4539 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4540 | return LY_EVALID; |
| 4541 | } |
| 4542 | |
| 4543 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4544 | /* check if node has default value */ |
| 4545 | if (node->nodetype & LYS_LEAF) { |
| 4546 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4547 | if (refine_flag) { |
| 4548 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4549 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4550 | return LY_EVALID; |
| 4551 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4552 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4553 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4554 | if (refine_flag) { |
| 4555 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4556 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4557 | return LY_EVALID; |
| 4558 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4559 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4560 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4561 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid refine of mandatory under the default case."); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4562 | return LY_EVALID; |
| 4563 | } |
| 4564 | |
| 4565 | node->flags &= ~LYS_MAND_FALSE; |
| 4566 | node->flags |= LYS_MAND_TRUE; |
| 4567 | lys_compile_mandatory_parents(node->parent, 1); |
| 4568 | } else { |
| 4569 | /* make mandatory false */ |
| 4570 | node->flags &= ~LYS_MAND_TRUE; |
| 4571 | node->flags |= LYS_MAND_FALSE; |
| 4572 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4573 | } |
| 4574 | return LY_SUCCESS; |
| 4575 | } |
| 4576 | |
| 4577 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4578 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4579 | * If present, also apply uses's modificators. |
| 4580 | * |
| 4581 | * @param[in] ctx Compile context |
| 4582 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4583 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4584 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4585 | * the compile context. |
| 4586 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4587 | */ |
| 4588 | static LY_ERR |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4589 | lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p) |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4590 | { |
| 4591 | struct lysp_node *node_p; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4592 | struct lysc_node *node, *child, *iter; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4593 | /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4594 | struct lysc_node_container context_node_fake = |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4595 | {.nodetype = LYS_CONTAINER, |
| 4596 | .module = ctx->mod, |
| 4597 | .flags = parent ? parent->flags : 0, |
| 4598 | .child = NULL, .next = NULL, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4599 | .prev = (struct lysc_node *)&context_node_fake, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4600 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4601 | struct lysp_grp *grp = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4602 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4603 | uint32_t grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4604 | int found; |
| 4605 | const char *id, *name, *prefix; |
| 4606 | size_t prefix_len, name_len; |
| 4607 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4608 | struct lysp_refine *rfn; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4609 | LY_ERR ret = LY_EVALID; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4610 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4611 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4612 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4613 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4614 | struct lysp_augment **augments = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4615 | LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4616 | struct lysc_notif **notifs = NULL; |
| 4617 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4618 | |
| 4619 | /* search for the grouping definition */ |
| 4620 | found = 0; |
| 4621 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4622 | LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4623 | if (prefix) { |
| 4624 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4625 | if (!mod) { |
| 4626 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4627 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4628 | return LY_EVALID; |
| 4629 | } |
| 4630 | } else { |
| 4631 | mod = ctx->mod_def; |
| 4632 | } |
| 4633 | if (mod == ctx->mod_def) { |
| 4634 | for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4635 | grp = (struct lysp_grp *)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4636 | LY_ARRAY_FOR(grp, u) { |
| 4637 | if (!strcmp(grp[u].name, name)) { |
| 4638 | grp = &grp[u]; |
| 4639 | found = 1; |
| 4640 | break; |
| 4641 | } |
| 4642 | } |
| 4643 | } |
| 4644 | } |
| 4645 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4646 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4647 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4648 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4649 | for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4650 | if (!strcmp(grp[u].name, name)) { |
| 4651 | grp = &grp[u]; |
| 4652 | found = 1; |
| 4653 | } |
| 4654 | } |
| 4655 | } |
| 4656 | if (!found && mod->parsed->includes) { |
| 4657 | /* ... and all the submodules */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4658 | for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4659 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4660 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4661 | for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4662 | if (!strcmp(grp[v].name, name)) { |
| 4663 | grp = &grp[v]; |
| 4664 | found = 1; |
| 4665 | } |
| 4666 | } |
| 4667 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4668 | } |
| 4669 | } |
| 4670 | } |
| 4671 | if (!found) { |
| 4672 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4673 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4674 | return LY_EVALID; |
| 4675 | } |
| 4676 | |
| 4677 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4678 | grp_stack_count = ctx->groupings.count; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4679 | ly_set_add(&ctx->groupings, (void *)grp, 0); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4680 | if (grp_stack_count == ctx->groupings.count) { |
| 4681 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4682 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4683 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4684 | return LY_EVALID; |
| 4685 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4686 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4687 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4688 | grp->flags |= LYS_USED_GRP; |
| 4689 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4690 | |
| 4691 | /* switch context's mod_def */ |
| 4692 | mod_old = ctx->mod_def; |
| 4693 | ctx->mod_def = mod; |
| 4694 | |
| 4695 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4696 | LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), cleanup); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4697 | |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4698 | /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly |
| 4699 | * applu refine and augment only to the nodes from the uses */ |
| 4700 | if (parent) { |
| 4701 | if (parent->nodetype == LYS_CASE) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4702 | child = (struct lysc_node *)lysc_node_children(parent->parent, ctx->options & LYSC_OPT_RPC_MASK); |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4703 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4704 | child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4705 | } |
| 4706 | } else if (ctx->mod->compiled->data) { |
| 4707 | child = ctx->mod->compiled->data; |
| 4708 | } else { |
| 4709 | child = NULL; |
| 4710 | } |
| 4711 | /* remember the last child */ |
| 4712 | if (child) { |
| 4713 | child = child->prev; |
| 4714 | } |
| 4715 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4716 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4717 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4718 | /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4719 | LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4720 | } |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4721 | |
| 4722 | /* split the children and add the uses's data into the fake context node */ |
| 4723 | if (child) { |
| 4724 | context_node_fake.child = child->next; |
| 4725 | } else if (parent) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4726 | context_node_fake.child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4727 | } else if (ctx->mod->compiled->data) { |
| 4728 | context_node_fake.child = ctx->mod->compiled->data; |
| 4729 | } |
| 4730 | if (context_node_fake.child) { |
| 4731 | /* remember child as the last data node added by grouping to fix the list later */ |
| 4732 | child = context_node_fake.child->prev; |
| 4733 | context_node_fake.child->prev = NULL; |
| 4734 | } |
| 4735 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4736 | when_shared = NULL; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4737 | LY_LIST_FOR(context_node_fake.child, iter) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4738 | iter->parent = (struct lysc_node *)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4739 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4740 | /* pass uses's when to all the data children, actions and notifications are ignored */ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4741 | if (uses_p->when) { |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4742 | LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4743 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4744 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 4745 | |
| 4746 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4747 | /* do not check "when" semantics in a grouping */ |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4748 | ly_set_add(&ctx->xpath, iter, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4749 | } |
| 4750 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4751 | when_shared = *when; |
| 4752 | } else { |
| 4753 | ++when_shared->refcount; |
| 4754 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4755 | |
| 4756 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4757 | /* in this case check "when" again for all children because of dummy node check */ |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4758 | ly_set_add(&ctx->xpath, iter, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4759 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4760 | } |
| 4761 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4762 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4763 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4764 | /* compile actions */ |
| 4765 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4766 | if (actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4767 | actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4768 | COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4769 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4770 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4771 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup); |
| 4772 | LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index; |
| 4773 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4774 | } |
| 4775 | } |
| 4776 | |
| 4777 | /* compile notifications */ |
| 4778 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4779 | if (notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4780 | notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4781 | COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4782 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4783 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4784 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup); |
| 4785 | LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index; |
| 4786 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4787 | } |
| 4788 | } |
| 4789 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4790 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4791 | LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4792 | LY_ARRAY_FOR(augments, u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4793 | LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node *)&context_node_fake), cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4794 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4795 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4796 | /* reload previous context's mod_def */ |
| 4797 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4798 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4799 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4800 | /* apply refine */ |
| 4801 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4802 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4803 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4804 | LY_CHECK_GOTO(lysc_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node *)&context_node_fake, ctx->mod, |
| 4805 | 0, 0, (const struct lysc_node **)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4806 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4807 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4808 | |
| 4809 | /* default value */ |
| 4810 | if (rfn->dflts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4811 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4812 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4813 | "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE " default values.", |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4814 | lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4815 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4816 | } |
| 4817 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4818 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4819 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4820 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4821 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4822 | } |
| 4823 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4824 | /* postpone default compilation when the tree is complete */ |
| 4825 | LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0], |
| 4826 | ctx->mod_def), cleanup); |
| 4827 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4828 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4829 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4830 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4831 | "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4832 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4833 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4834 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4835 | /* postpone default compilation when the tree is complete */ |
| 4836 | LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts, |
| 4837 | ctx->mod_def), cleanup); |
| 4838 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4839 | } else if (node->nodetype == LYS_CHOICE) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4840 | if (((struct lysc_node_choice *)node)->dflt) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4841 | /* unset LYS_SET_DFLT from the current default case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4842 | ((struct lysc_node_choice *)node)->dflt->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4843 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4844 | LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice *)node), cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4845 | } |
| 4846 | } |
| 4847 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4848 | /* description */ |
| 4849 | if (rfn->dsc) { |
| 4850 | FREE_STRING(ctx->ctx, node->dsc); |
| 4851 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4852 | } |
| 4853 | |
| 4854 | /* reference */ |
| 4855 | if (rfn->ref) { |
| 4856 | FREE_STRING(ctx->ctx, node->ref); |
| 4857 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4858 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4859 | |
| 4860 | /* config */ |
| 4861 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4862 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4863 | LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4864 | } else { |
| 4865 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4866 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4867 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4868 | } |
| 4869 | |
| 4870 | /* mandatory */ |
| 4871 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4872 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4873 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4874 | |
| 4875 | /* presence */ |
| 4876 | if (rfn->presence) { |
| 4877 | if (node->nodetype != LYS_CONTAINER) { |
| 4878 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4879 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4880 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4881 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4882 | } |
| 4883 | node->flags |= LYS_PRESENCE; |
| 4884 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4885 | |
| 4886 | /* must */ |
| 4887 | if (rfn->musts) { |
| 4888 | switch (node->nodetype) { |
| 4889 | case LYS_LEAF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4890 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf *)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4891 | break; |
| 4892 | case LYS_LEAFLIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4893 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist *)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4894 | break; |
| 4895 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4896 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list *)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4897 | break; |
| 4898 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4899 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container *)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4900 | break; |
| 4901 | case LYS_ANYXML: |
| 4902 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4903 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata *)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4904 | break; |
| 4905 | default: |
| 4906 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4907 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 4908 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4909 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4910 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4911 | ly_set_add(&ctx->xpath, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4912 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4913 | |
| 4914 | /* min/max-elements */ |
| 4915 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4916 | switch (node->nodetype) { |
| 4917 | case LYS_LEAFLIST: |
| 4918 | if (rfn->flags & LYS_SET_MAX) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4919 | ((struct lysc_node_leaflist *)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4920 | } |
| 4921 | if (rfn->flags & LYS_SET_MIN) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4922 | ((struct lysc_node_leaflist *)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4923 | if (rfn->min) { |
| 4924 | node->flags |= LYS_MAND_TRUE; |
| 4925 | lys_compile_mandatory_parents(node->parent, 1); |
| 4926 | } else { |
| 4927 | node->flags &= ~LYS_MAND_TRUE; |
| 4928 | lys_compile_mandatory_parents(node->parent, 0); |
| 4929 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4930 | } |
| 4931 | break; |
| 4932 | case LYS_LIST: |
| 4933 | if (rfn->flags & LYS_SET_MAX) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4934 | ((struct lysc_node_list *)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4935 | } |
| 4936 | if (rfn->flags & LYS_SET_MIN) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4937 | ((struct lysc_node_list *)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4938 | if (rfn->min) { |
| 4939 | node->flags |= LYS_MAND_TRUE; |
| 4940 | lys_compile_mandatory_parents(node->parent, 1); |
| 4941 | } else { |
| 4942 | node->flags &= ~LYS_MAND_TRUE; |
| 4943 | lys_compile_mandatory_parents(node->parent, 0); |
| 4944 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4945 | } |
| 4946 | break; |
| 4947 | default: |
| 4948 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4949 | "Invalid refine of %s statement - %s cannot hold this statement.", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4950 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4951 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4952 | } |
| 4953 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4954 | |
| 4955 | /* if-feature */ |
| 4956 | if (rfn->iffeatures) { |
| 4957 | /* any node in compiled tree can get additional if-feature, so do not check nodetype */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4958 | COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4959 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4960 | |
| 4961 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4962 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4963 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4964 | /* do some additional checks of the changed nodes when all the refines are applied */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4965 | for (uint32_t i = 0; i < refined.count; ++i) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4966 | node = (struct lysc_node *)refined.objs[i]; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4967 | rfn = &uses_p->refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4968 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4969 | |
| 4970 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 4971 | if ((node->flags & LYS_MAND_TRUE) && |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4972 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) || |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4973 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 4974 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4975 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4976 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4977 | } |
| 4978 | |
| 4979 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4980 | if (node->nodetype == LYS_LIST) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4981 | min = ((struct lysc_node_list *)node)->min; |
| 4982 | max = ((struct lysc_node_list *)node)->max; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4983 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 4984 | min = ((struct lysc_node_leaflist *)node)->min; |
| 4985 | max = ((struct lysc_node_leaflist *)node)->max; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4986 | } |
| 4987 | if (min > max) { |
| 4988 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4989 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4990 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4991 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4992 | } |
| 4993 | } |
| 4994 | } |
| 4995 | |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4996 | if (first_p) { |
| 4997 | *first_p = context_node_fake.child; |
| 4998 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4999 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5000 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5001 | |
| 5002 | cleanup: |
| 5003 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5004 | if (context_node_fake.child) { |
| 5005 | context_node_fake.child->prev = child; |
| 5006 | } |
| 5007 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5008 | child->parent = parent; |
| 5009 | } |
| 5010 | |
| 5011 | if (uses_p->augments || uses_p->refines) { |
| 5012 | /* return back actions and notifications in case they were separated for augment/refine processing */ |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5013 | if (context_node_fake.actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5014 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5015 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5016 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5017 | if (context_node_fake.notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5018 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5019 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5020 | } |
| 5021 | } |
| 5022 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5023 | /* reload previous context's mod_def */ |
| 5024 | ctx->mod_def = mod_old; |
| 5025 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5026 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5027 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5028 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5029 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5030 | |
| 5031 | return ret; |
| 5032 | } |
| 5033 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5034 | static int |
| 5035 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5036 | { |
| 5037 | struct lysp_node *iter; |
| 5038 | int len = 0; |
| 5039 | |
| 5040 | *path = NULL; |
| 5041 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5042 | char *s = *path; |
| 5043 | char *id; |
| 5044 | |
| 5045 | switch (iter->nodetype) { |
| 5046 | case LYS_USES: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5047 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5048 | break; |
| 5049 | case LYS_GROUPING: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5050 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5051 | break; |
| 5052 | case LYS_AUGMENT: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5053 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5054 | break; |
| 5055 | default: |
| 5056 | id = strdup(iter->name); |
| 5057 | break; |
| 5058 | } |
| 5059 | |
| 5060 | if (!iter->parent) { |
| 5061 | /* print prefix */ |
| 5062 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5063 | } else { |
| 5064 | /* prefix is the same as in parent */ |
| 5065 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5066 | } |
| 5067 | free(s); |
| 5068 | free(id); |
| 5069 | } |
| 5070 | |
| 5071 | if (len < 0) { |
| 5072 | free(*path); |
| 5073 | *path = NULL; |
| 5074 | } else if (len == 0) { |
| 5075 | *path = strdup("/"); |
| 5076 | len = 1; |
| 5077 | } |
| 5078 | return len; |
| 5079 | } |
| 5080 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5081 | /** |
| 5082 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5083 | * |
| 5084 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5085 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5086 | */ |
| 5087 | static LY_ERR |
| 5088 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5089 | { |
| 5090 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5091 | char *path; |
| 5092 | int len; |
| 5093 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5094 | struct lysp_node_uses fake_uses = { |
| 5095 | .parent = node_p, |
| 5096 | .nodetype = LYS_USES, |
| 5097 | .flags = 0, .next = NULL, |
| 5098 | .name = grp->name, |
| 5099 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5100 | .refines = NULL, .augments = NULL |
| 5101 | }; |
| 5102 | struct lysc_node_container fake_container = { |
| 5103 | .nodetype = LYS_CONTAINER, |
| 5104 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5105 | .module = ctx->mod, |
| 5106 | .sp = NULL, .parent = NULL, .next = NULL, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5107 | .prev = (struct lysc_node *)&fake_container, |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5108 | .name = "fake", |
| 5109 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5110 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5111 | }; |
| 5112 | |
| 5113 | if (grp->parent) { |
| 5114 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5115 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5116 | |
| 5117 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5118 | if (len < 0) { |
| 5119 | LOGMEM(ctx->ctx); |
| 5120 | return LY_EMEM; |
| 5121 | } |
| 5122 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5123 | ctx->path_len = (uint16_t)len; |
| 5124 | free(path); |
| 5125 | |
| 5126 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5127 | lysc_update_path(ctx, NULL, grp->name); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5128 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node *)&fake_container, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5129 | lysc_update_path(ctx, NULL, NULL); |
| 5130 | lysc_update_path(ctx, NULL, NULL); |
| 5131 | |
| 5132 | ctx->path_len = 1; |
| 5133 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5134 | |
| 5135 | /* cleanup */ |
| 5136 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5137 | |
| 5138 | return ret; |
| 5139 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5140 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5141 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5142 | * @brief Compile parsed schema node information. |
| 5143 | * @param[in] ctx Compile context |
| 5144 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5145 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5146 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5147 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5148 | * @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). |
| 5149 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5150 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5151 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5152 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5153 | lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5154 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5155 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5156 | struct lysc_node *node = NULL; |
| 5157 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5158 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5159 | unsigned int u; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5160 | LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5161 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5162 | if (node_p->nodetype != LYS_USES) { |
| 5163 | lysc_update_path(ctx, parent, node_p->name); |
| 5164 | } else { |
| 5165 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5166 | lysc_update_path(ctx, NULL, node_p->name); |
| 5167 | } |
| 5168 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5169 | switch (node_p->nodetype) { |
| 5170 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5171 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5172 | node_compile_spec = lys_compile_node_container; |
| 5173 | break; |
| 5174 | case LYS_LEAF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5175 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5176 | node_compile_spec = lys_compile_node_leaf; |
| 5177 | break; |
| 5178 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5179 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5180 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5181 | break; |
| 5182 | case LYS_LEAFLIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5183 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5184 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5185 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5186 | case LYS_CHOICE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5187 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5188 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5189 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5190 | case LYS_ANYXML: |
| 5191 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5192 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5193 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5194 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5195 | case LYS_USES: |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5196 | if (parent && parent->nodetype == LYS_CHOICE) { |
| 5197 | assert(node_p->parent->nodetype == LYS_CASE); |
| 5198 | lysc_update_path(ctx, parent, node_p->parent->name); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5199 | cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice *)parent, NULL); |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5200 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
| 5201 | } |
| 5202 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5203 | ret = lys_compile_uses(ctx, (struct lysp_node_uses *)node_p, cs ? (struct lysc_node *)cs : parent, &node); |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5204 | if (cs) { |
| 5205 | cs->child = node; |
| 5206 | lysc_update_path(ctx, NULL, NULL); |
| 5207 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5208 | lysc_update_path(ctx, NULL, NULL); |
| 5209 | lysc_update_path(ctx, NULL, NULL); |
| 5210 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5211 | default: |
| 5212 | LOGINT(ctx->ctx); |
| 5213 | return LY_EINT; |
| 5214 | } |
| 5215 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5216 | node->nodetype = node_p->nodetype; |
| 5217 | node->module = ctx->mod; |
| 5218 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5219 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5220 | |
| 5221 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5222 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5223 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5224 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5225 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5226 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5227 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5228 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5229 | node->flags |= LYS_CONFIG_R; |
| 5230 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5231 | /* config not explicitely set, inherit it from parent */ |
| 5232 | if (parent) { |
| 5233 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5234 | } else { |
| 5235 | /* default is config true */ |
| 5236 | node->flags |= LYS_CONFIG_W; |
| 5237 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5238 | } else { |
| 5239 | /* config set explicitely */ |
| 5240 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5241 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5242 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5243 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5244 | "Configuration node cannot be child of any state data node."); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5245 | ret = LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5246 | goto error; |
| 5247 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5248 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5249 | /* *list ordering */ |
| 5250 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5251 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5252 | LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5253 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5254 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5255 | node->flags &= ~LYS_ORDBY_MASK; |
| 5256 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5257 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5258 | /* default ordering is system */ |
| 5259 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5260 | } |
| 5261 | } |
| 5262 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5263 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5264 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5265 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5266 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5267 | * the parent is choice (parent here) or some case (so we have to get its flags to check) */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5268 | LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (parent ? parent->flags : 0)), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5269 | } |
| 5270 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5271 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5272 | node->sp = node_p; |
| 5273 | } |
| 5274 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5275 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5276 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5277 | if (node_p->when) { |
| 5278 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5279 | LY_CHECK_GOTO(ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when), error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5280 | |
| 5281 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5282 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5283 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5284 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5285 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5286 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5287 | |
| 5288 | /* nodetype-specific part */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5289 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5290 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5291 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5292 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5293 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5294 | if (node->flags & LYS_MAND_TRUE) { |
| 5295 | lys_compile_mandatory_parents(parent, 1); |
| 5296 | } |
| 5297 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5298 | lysc_update_path(ctx, NULL, NULL); |
| 5299 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5300 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5301 | if (parent) { |
| 5302 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5303 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5304 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5305 | } else { |
| 5306 | lysc_update_path(ctx, parent, node->name); |
| 5307 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5308 | cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice *)parent, node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5309 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5310 | if (uses_status) {} |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5311 | /* the postponed status check of the node and its real parent - in case of implicit case, |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5312 | * it directly gets the same status flags as the choice; |
| 5313 | * uses_status cannot be applied here since uses cannot be child statement of choice */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5314 | LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5315 | node->parent = (struct lysc_node *)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5316 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5317 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5318 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5319 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5320 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5321 | LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5322 | |
| 5323 | if (parent->nodetype == LYS_CHOICE) { |
| 5324 | lysc_update_path(ctx, NULL, NULL); |
| 5325 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5326 | } else { |
| 5327 | /* top-level element */ |
| 5328 | if (!ctx->mod->compiled->data) { |
| 5329 | ctx->mod->compiled->data = node; |
| 5330 | } else { |
| 5331 | /* insert at the end of the module's top-level nodes list */ |
| 5332 | ctx->mod->compiled->data->prev->next = node; |
| 5333 | node->prev = ctx->mod->compiled->data->prev; |
| 5334 | ctx->mod->compiled->data->prev = node; |
| 5335 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5336 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5337 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5338 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5339 | return LY_EVALID; |
| 5340 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5341 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5342 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5343 | |
| 5344 | return LY_SUCCESS; |
| 5345 | |
| 5346 | error: |
| 5347 | lysc_node_free(ctx->ctx, node); |
| 5348 | return ret; |
| 5349 | } |
| 5350 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5351 | static void |
| 5352 | lysc_disconnect(struct lysc_node *node) |
| 5353 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5354 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5355 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5356 | struct lysc_module *modc = node->module->compiled; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5357 | int remove_cs = 0; |
| 5358 | |
| 5359 | parent = node->parent; |
| 5360 | |
| 5361 | /* parent's first child */ |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5362 | if (parent && parent->nodetype == LYS_CHOICE) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5363 | cs = (struct lysc_node_case *)node; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5364 | } else if (parent && parent->nodetype == LYS_CASE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5365 | /* disconnecting some node in a case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5366 | cs = (struct lysc_node_case *)parent; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5367 | parent = cs->parent; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5368 | for (child = cs->child; child && child->parent == (struct lysc_node *)cs; child = child->next) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5369 | if (child == node) { |
| 5370 | if (cs->child == child) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5371 | if (!child->next || child->next->parent != (struct lysc_node *)cs) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5372 | /* case with a single child -> remove also the case */ |
| 5373 | child->parent = NULL; |
| 5374 | remove_cs = 1; |
| 5375 | } else { |
| 5376 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5377 | } |
| 5378 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5379 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5380 | } |
| 5381 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5382 | if (!remove_cs) { |
| 5383 | cs = NULL; |
| 5384 | } |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5385 | } else if (parent && lysc_node_children(parent, node->flags) == node) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5386 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5387 | } else if (modc->data == node) { |
| 5388 | modc->data = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5389 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5390 | if (cs) { |
| 5391 | if (remove_cs) { |
| 5392 | /* cs has only one child which is being also removed */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5393 | lysc_disconnect((struct lysc_node *)cs); |
| 5394 | lysc_node_free(cs->module->ctx, (struct lysc_node *)cs); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5395 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5396 | if (((struct lysc_node_choice *)parent)->dflt == cs) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5397 | /* default case removed */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5398 | ((struct lysc_node_choice *)parent)->dflt = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5399 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5400 | if (((struct lysc_node_choice *)parent)->cases == cs) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5401 | /* first case removed */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5402 | ((struct lysc_node_choice *)parent)->cases = (struct lysc_node_case *)cs->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5403 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5404 | if (cs->child) { |
| 5405 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5406 | if (cs->child->prev->parent != (struct lysc_node *)cs) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5407 | prev = cs->child->prev; |
| 5408 | } /* else all the children are under a single case */ |
| 5409 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5410 | if (child->parent != (struct lysc_node *)cs) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5411 | break; |
| 5412 | } |
| 5413 | lysc_node_free(node->module->ctx, child); |
| 5414 | } |
| 5415 | if (prev) { |
| 5416 | if (prev->next) { |
| 5417 | prev->next = child; |
| 5418 | } |
| 5419 | if (child) { |
| 5420 | child->prev = prev; |
| 5421 | } else { |
| 5422 | /* link from the first child under the cases */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5423 | ((struct lysc_node_choice *)cs->parent)->cases->child->prev = prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5424 | } |
| 5425 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5426 | } |
| 5427 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5428 | } |
| 5429 | |
| 5430 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5431 | if (node->prev->next) { |
| 5432 | node->prev->next = node->next; |
| 5433 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5434 | if (node->next) { |
| 5435 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5436 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5437 | if (parent) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5438 | child = (struct lysc_node *)lysc_node_children(parent, node->flags); |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5439 | } else { |
| 5440 | child = modc->data; |
| 5441 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5442 | if (child) { |
| 5443 | child->prev = node->prev; |
| 5444 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5445 | } else if (((struct lysc_node_choice *)parent)->cases) { |
| 5446 | ((struct lysc_node_choice *)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5447 | } |
| 5448 | } |
| 5449 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 5450 | struct lysc_deviation { |
| 5451 | const char *nodeid; |
| 5452 | struct lysc_node *target; /* target node of the deviation */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5453 | struct lysp_deviate **deviates;/* sized array of pointers to parsed deviate statements to apply on target */ |
Michal Vasko | cb18c7f | 2020-08-24 09:22:28 +0200 | [diff] [blame] | 5454 | uint16_t flags; /* target's flags from lysc_resolve_schema_nodeid() */ |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 5455 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5456 | }; |
| 5457 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5458 | /* MACROS for deviates checking */ |
| 5459 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5460 | if (!(target->nodetype & (NODETYPES))) { \ |
| 5461 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\ |
| 5462 | goto cleanup; \ |
| 5463 | } |
| 5464 | |
| 5465 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5466 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
| 5467 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
| 5468 | lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
| 5469 | goto cleanup; \ |
| 5470 | } |
| 5471 | |
| 5472 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5473 | if (!((TYPE)target)->MEMBER || COND) { \ |
| 5474 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
| 5475 | goto cleanup; \ |
| 5476 | } |
| 5477 | |
| 5478 | /** |
| 5479 | * @brief Apply deviate add. |
| 5480 | * |
| 5481 | * @param[in] ctx Compile context. |
| 5482 | * @param[in] target Deviation target. |
| 5483 | * @param[in] dev_flags Internal deviation flags. |
| 5484 | * @param[in] d Deviate add to apply. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5485 | * @return LY_ERR value. |
| 5486 | */ |
| 5487 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5488 | lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysc_node *target, int dev_flags, struct lysp_deviate_add *d) |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5489 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5490 | LY_ERR ret = LY_EVALID; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5491 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 5492 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5493 | LY_ARRAY_COUNT_TYPE x; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5494 | |
| 5495 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5496 | if (((TYPE)target)->MEMBER COND) { \ |
| 5497 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5498 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5499 | PROPERTY, ((TYPE)target)->MEMBER); \ |
| 5500 | goto cleanup; \ |
| 5501 | } |
| 5502 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5503 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5504 | if (((TYPE)target)->MEMBER && (COND)) { \ |
| 5505 | int dynamic_ = 0; const char *val_; \ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5506 | val_ = ((TYPE)target)->VALUEMEMBER->realtype->plugin->print(((TYPE)target)->VALUEMEMBER, LY_PREF_SCHEMA, \ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5507 | ctx->mod_def, &dynamic_); \ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5508 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5509 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5510 | if (dynamic_) {free((void*)val_);} \ |
| 5511 | goto cleanup; \ |
| 5512 | } |
| 5513 | |
| 5514 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
| 5515 | if (((TYPE)target)->MEMBER && (COND)) { \ |
| 5516 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5517 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5518 | PROPERTY, ((TYPE)target)->VALUEMEMBER); \ |
| 5519 | goto cleanup; \ |
| 5520 | } |
| 5521 | |
| 5522 | /* [units-stmt] */ |
| 5523 | if (d->units) { |
| 5524 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5525 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units); |
| 5526 | |
| 5527 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units); |
| 5528 | DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units); |
| 5529 | } |
| 5530 | |
| 5531 | /* *must-stmt */ |
| 5532 | if (d->musts) { |
| 5533 | switch (target->nodetype) { |
| 5534 | case LYS_CONTAINER: |
| 5535 | case LYS_LIST: |
| 5536 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts, |
| 5537 | x, lys_compile_must, ret, cleanup); |
| 5538 | break; |
| 5539 | case LYS_LEAF: |
| 5540 | case LYS_LEAFLIST: |
| 5541 | case LYS_ANYDATA: |
| 5542 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts, |
| 5543 | x, lys_compile_must, ret, cleanup); |
| 5544 | break; |
| 5545 | case LYS_NOTIF: |
| 5546 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts, |
| 5547 | x, lys_compile_must, ret, cleanup); |
| 5548 | break; |
| 5549 | case LYS_RPC: |
| 5550 | case LYS_ACTION: |
| 5551 | if (dev_flags & LYSC_OPT_RPC_INPUT) { |
| 5552 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts, |
| 5553 | x, lys_compile_must, ret, cleanup); |
| 5554 | break; |
| 5555 | } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) { |
| 5556 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts, |
| 5557 | x, lys_compile_must, ret, cleanup); |
| 5558 | break; |
| 5559 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5560 | /* fall through */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5561 | default: |
| 5562 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5563 | lys_nodetype2str(target->nodetype), "add", "must"); |
| 5564 | goto cleanup; |
| 5565 | } |
| 5566 | ly_set_add(&ctx->xpath, target, 0); |
| 5567 | } |
| 5568 | |
| 5569 | /* *unique-stmt */ |
| 5570 | if (d->uniques) { |
| 5571 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5572 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup); |
| 5573 | } |
| 5574 | |
| 5575 | /* *default-stmt */ |
| 5576 | if (d->dflts) { |
| 5577 | switch (target->nodetype) { |
| 5578 | case LYS_LEAF: |
| 5579 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5580 | DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf *, (target->flags & LYS_SET_DFLT), dflt, "default", dflt); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5581 | if (leaf->dflt) { |
| 5582 | /* first, remove the default value taken from the type */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5583 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5584 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5585 | free(leaf->dflt); |
| 5586 | leaf->dflt = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5587 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5588 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5589 | /* store the default value in unres */ |
Michal Vasko | 31a82d5 | 2020-08-21 08:11:48 +0200 | [diff] [blame] | 5590 | LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflts[0], ctx->mod_def), cleanup); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5591 | target->flags |= LYS_SET_DFLT; |
| 5592 | break; |
| 5593 | case LYS_LEAFLIST: |
| 5594 | if (llist->dflts && !(target->flags & LYS_SET_DFLT)) { |
| 5595 | /* first, remove the default value taken from the type */ |
| 5596 | LY_ARRAY_FOR(llist->dflts, x) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5597 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5598 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5599 | free(llist->dflts[x]); |
| 5600 | } |
| 5601 | LY_ARRAY_FREE(llist->dflts); |
| 5602 | llist->dflts = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5603 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5604 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5605 | /* store the default values in unres */ |
| 5606 | LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, llist, d->dflts, ctx->mod_def), cleanup); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5607 | target->flags |= LYS_SET_DFLT; |
| 5608 | break; |
| 5609 | case LYS_CHOICE: |
| 5610 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
| 5611 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name); |
| 5612 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5613 | * to allow making the default case even the augmented case from the deviating module */ |
| 5614 | if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) { |
| 5615 | goto cleanup; |
| 5616 | } |
| 5617 | break; |
| 5618 | default: |
| 5619 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5620 | lys_nodetype2str(target->nodetype), "add", "default"); |
| 5621 | goto cleanup; |
| 5622 | } |
| 5623 | } |
| 5624 | |
| 5625 | /* [config-stmt] */ |
| 5626 | if (d->flags & LYS_CONFIG_MASK) { |
| 5627 | if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 5628 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5629 | lys_nodetype2str(target->nodetype), "add", "config"); |
| 5630 | goto cleanup; |
| 5631 | } |
| 5632 | if (dev_flags) { |
| 5633 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 5634 | dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
| 5635 | } |
| 5636 | if (target->flags & LYS_SET_CONFIG) { |
| 5637 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5638 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5639 | target->flags & LYS_CONFIG_W ? "true" : "false"); |
| 5640 | goto cleanup; |
| 5641 | } |
| 5642 | LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup); |
| 5643 | } |
| 5644 | |
| 5645 | /* [mandatory-stmt] */ |
| 5646 | if (d->flags & LYS_MAND_MASK) { |
| 5647 | if (target->flags & LYS_MAND_MASK) { |
| 5648 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5649 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5650 | target->flags & LYS_MAND_TRUE ? "true" : "false"); |
| 5651 | goto cleanup; |
| 5652 | } |
| 5653 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup); |
| 5654 | } |
| 5655 | |
| 5656 | /* [min-elements-stmt] */ |
| 5657 | if (d->flags & LYS_SET_MIN) { |
| 5658 | if (target->nodetype == LYS_LEAFLIST) { |
| 5659 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements"); |
| 5660 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5661 | ((struct lysc_node_leaflist *)target)->min = d->min; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5662 | } else if (target->nodetype == LYS_LIST) { |
| 5663 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements"); |
| 5664 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5665 | ((struct lysc_node_list *)target)->min = d->min; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5666 | } else { |
| 5667 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5668 | lys_nodetype2str(target->nodetype), "add", "min-elements"); |
| 5669 | goto cleanup; |
| 5670 | } |
| 5671 | if (d->min) { |
| 5672 | target->flags |= LYS_MAND_TRUE; |
| 5673 | } |
| 5674 | } |
| 5675 | |
| 5676 | /* [max-elements-stmt] */ |
| 5677 | if (d->flags & LYS_SET_MAX) { |
| 5678 | if (target->nodetype == LYS_LEAFLIST) { |
| 5679 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements"); |
| 5680 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5681 | ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5682 | } else if (target->nodetype == LYS_LIST) { |
| 5683 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements"); |
| 5684 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5685 | ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5686 | } else { |
| 5687 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5688 | lys_nodetype2str(target->nodetype), "add", "max-elements"); |
| 5689 | goto cleanup; |
| 5690 | } |
| 5691 | } |
| 5692 | |
| 5693 | ret = LY_SUCCESS; |
| 5694 | |
| 5695 | cleanup: |
| 5696 | return ret; |
| 5697 | } |
| 5698 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5699 | static LY_ERR |
| 5700 | lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt) |
| 5701 | { |
| 5702 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 5703 | int dyn = 0; |
| 5704 | const char *orig_dflt; |
| 5705 | uint32_t i; |
| 5706 | |
| 5707 | if (target->module != ctx->mod) { |
| 5708 | /* foreign deviation */ |
| 5709 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt); |
| 5710 | |
| 5711 | /* check that the value matches */ |
| 5712 | orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn); |
| 5713 | if (strcmp(orig_dflt, dflt)) { |
| 5714 | goto error; |
| 5715 | } |
| 5716 | if (dyn) { |
| 5717 | free((char *)orig_dflt); |
| 5718 | } |
| 5719 | |
| 5720 | /* remove the default specification */ |
| 5721 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5722 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5723 | free(leaf->dflt); |
| 5724 | leaf->dflt = NULL; |
| 5725 | } else { |
| 5726 | /* local deviation */ |
| 5727 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt); |
| 5728 | |
| 5729 | /* find the incomplete default */ |
| 5730 | orig_dflt = NULL; |
| 5731 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 5732 | if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) { |
| 5733 | orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt; |
| 5734 | break; |
| 5735 | } |
| 5736 | } |
| 5737 | LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT); |
| 5738 | |
| 5739 | /* check that the value matches */ |
| 5740 | if (strcmp(orig_dflt, dflt)) { |
| 5741 | goto error; |
| 5742 | } |
| 5743 | |
| 5744 | /* update the list of incomplete default values */ |
| 5745 | lysc_incomplete_dflt_remove(ctx, target); |
| 5746 | } |
| 5747 | |
| 5748 | target->flags &= ~LYS_SET_DFLT; |
| 5749 | return LY_SUCCESS; |
| 5750 | |
| 5751 | error: |
| 5752 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5753 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 5754 | dflt, orig_dflt); |
| 5755 | if (dyn) { |
| 5756 | free((char *)orig_dflt); |
| 5757 | } |
| 5758 | cleanup: |
| 5759 | return LY_EVALID; |
| 5760 | } |
| 5761 | |
| 5762 | static LY_ERR |
| 5763 | lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts) |
| 5764 | { |
| 5765 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 5766 | int dyn = 0, found; |
| 5767 | const char *orig_dflt, **orig_dflts; |
| 5768 | uint32_t i; |
| 5769 | LY_ARRAY_COUNT_TYPE x, y; |
| 5770 | |
| 5771 | if (target->module != ctx->mod) { |
| 5772 | /* foreign deviation */ |
| 5773 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]); |
| 5774 | LY_ARRAY_FOR(dflts, x) { |
| 5775 | found = 0; |
| 5776 | LY_ARRAY_FOR(llist->dflts, y) { |
| 5777 | orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn); |
| 5778 | if (!strcmp(orig_dflt, dflts[x])) { |
| 5779 | if (dyn) { |
| 5780 | free((char *)orig_dflt); |
| 5781 | } |
| 5782 | found = 1; |
| 5783 | break; |
| 5784 | } |
| 5785 | if (dyn) { |
| 5786 | free((char *)orig_dflt); |
| 5787 | } |
| 5788 | } |
| 5789 | if (!found) { |
| 5790 | goto error; |
| 5791 | } |
| 5792 | |
| 5793 | /* update compiled default values */ |
| 5794 | LY_ARRAY_DECREMENT(llist->dflts); |
| 5795 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 5796 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 5797 | free(llist->dflts[y]); |
| 5798 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts)); |
| 5799 | } |
| 5800 | if (!LY_ARRAY_COUNT(llist->dflts)) { |
| 5801 | LY_ARRAY_FREE(llist->dflts); |
| 5802 | llist->dflts = NULL; |
| 5803 | llist->flags &= ~LYS_SET_DFLT; |
| 5804 | } |
| 5805 | } else { |
| 5806 | /* local deviation */ |
| 5807 | orig_dflt = NULL; |
| 5808 | orig_dflts = NULL; |
| 5809 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 5810 | if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) { |
| 5811 | orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt; |
| 5812 | orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts; |
| 5813 | break; |
| 5814 | } |
| 5815 | } |
| 5816 | LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT); |
| 5817 | |
| 5818 | if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) { |
| 5819 | /* TODO it is not currently possible to remove just one default value from incomplete defaults array */ |
| 5820 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5821 | "Local deviation deleting leaf-list defaults is not supported."); |
| 5822 | return LY_EVALID; |
| 5823 | } |
| 5824 | |
| 5825 | LY_ARRAY_FOR(dflts, x) { |
| 5826 | found = 0; |
| 5827 | if (orig_dflts) { |
| 5828 | LY_ARRAY_FOR(orig_dflts, y) { |
| 5829 | if (!strcmp(orig_dflts[y], dflts[x])) { |
| 5830 | found = 1; |
| 5831 | break; |
| 5832 | } |
| 5833 | } |
| 5834 | } else if (!strcmp(orig_dflt, dflts[x])) { |
| 5835 | found = 1; |
| 5836 | } |
| 5837 | if (!found) { |
| 5838 | goto error; |
| 5839 | } |
| 5840 | |
| 5841 | /* update the list of incomplete default values */ |
| 5842 | lysc_incomplete_dflt_remove(ctx, target); |
| 5843 | } |
| 5844 | |
| 5845 | llist->flags &= ~LYS_SET_DFLT; |
| 5846 | } |
| 5847 | |
| 5848 | return LY_SUCCESS; |
| 5849 | |
| 5850 | error: |
| 5851 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 5852 | "which does not match any of the target's property values.", dflts[x]); |
| 5853 | cleanup: |
| 5854 | return LY_EVALID; |
| 5855 | } |
| 5856 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5857 | /** |
| 5858 | * @brief Apply deviate delete. |
| 5859 | * |
| 5860 | * @param[in] ctx Compile context. |
| 5861 | * @param[in] target Deviation target. |
| 5862 | * @param[in] dev_flags Internal deviation flags. |
| 5863 | * @param[in] d Deviate delete to apply. |
| 5864 | * @return LY_ERR value. |
| 5865 | */ |
| 5866 | static LY_ERR |
| 5867 | lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysc_node *target, int dev_flags, struct lysp_deviate_del *d) |
| 5868 | { |
| 5869 | LY_ERR ret = LY_EVALID; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5870 | struct lysc_node_list *list = (struct lysc_node_list *)target; |
| 5871 | LY_ARRAY_COUNT_TYPE x, y, z; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5872 | size_t prefix_len, name_len; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5873 | const char *prefix, *name, *nodeid; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5874 | struct lys_module *mod; |
| 5875 | |
| 5876 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5877 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \ |
| 5878 | LY_ARRAY_FOR(d->ARRAY_DEV, x) { \ |
| 5879 | LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \ |
| 5880 | if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \ |
| 5881 | } \ |
| 5882 | if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \ |
| 5883 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5884 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5885 | PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \ |
| 5886 | goto cleanup; \ |
| 5887 | } \ |
| 5888 | LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \ |
| 5889 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \ |
| 5890 | memmove(&((TYPE)target)->ARRAY_TRG[y], \ |
| 5891 | &((TYPE)target)->ARRAY_TRG[y + 1], \ |
| 5892 | (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \ |
| 5893 | } \ |
| 5894 | if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \ |
| 5895 | LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \ |
| 5896 | ((TYPE)target)->ARRAY_TRG = NULL; \ |
| 5897 | } |
| 5898 | |
| 5899 | /* [units-stmt] */ |
| 5900 | if (d->units) { |
| 5901 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5902 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, 0, units, "deleting", "units", d->units); |
| 5903 | if (strcmp(((struct lysc_node_leaf *)target)->units, d->units)) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5904 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5905 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5906 | d->units, ((struct lysc_node_leaf *)target)->units); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5907 | goto cleanup; |
| 5908 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5909 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf *)target)->units); |
| 5910 | ((struct lysc_node_leaf *)target)->units = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5911 | } |
| 5912 | |
| 5913 | /* *must-stmt */ |
| 5914 | if (d->musts) { |
| 5915 | switch (target->nodetype) { |
| 5916 | case LYS_CONTAINER: |
| 5917 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5918 | DEV_DEL_ARRAY(struct lysc_node_container *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5919 | break; |
| 5920 | case LYS_LEAF: |
| 5921 | case LYS_LEAFLIST: |
| 5922 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5923 | DEV_DEL_ARRAY(struct lysc_node_leaf *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5924 | break; |
| 5925 | case LYS_NOTIF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5926 | DEV_DEL_ARRAY(struct lysc_notif *, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5927 | break; |
| 5928 | case LYS_RPC: |
| 5929 | case LYS_ACTION: |
| 5930 | if (dev_flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5931 | DEV_DEL_ARRAY(struct lysc_action *, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5932 | break; |
| 5933 | } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5934 | DEV_DEL_ARRAY(struct lysc_action *, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5935 | break; |
| 5936 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5937 | /* fall through */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5938 | default: |
| 5939 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5940 | lys_nodetype2str(target->nodetype), "delete", "must"); |
| 5941 | goto cleanup; |
| 5942 | } |
| 5943 | } |
| 5944 | |
| 5945 | /* *unique-stmt */ |
| 5946 | if (d->uniques) { |
| 5947 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 5948 | LY_ARRAY_FOR(d->uniques, x) { |
| 5949 | LY_ARRAY_FOR(list->uniques, z) { |
| 5950 | for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 5951 | nodeid = strpbrk(name, " \t\n"); |
| 5952 | if (nodeid) { |
| 5953 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
| 5954 | break; |
| 5955 | } |
| 5956 | while (isspace(*nodeid)) { |
| 5957 | ++nodeid; |
| 5958 | } |
| 5959 | } else { |
| 5960 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 5961 | break; |
| 5962 | } |
| 5963 | } |
| 5964 | } |
| 5965 | if (!name) { |
| 5966 | /* complete match - remove the unique */ |
| 5967 | LY_ARRAY_DECREMENT(list->uniques); |
| 5968 | LY_ARRAY_FREE(list->uniques[z]); |
| 5969 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques)); |
| 5970 | --z; |
| 5971 | break; |
| 5972 | } |
| 5973 | } |
| 5974 | if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) { |
| 5975 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5976 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 5977 | d->uniques[x]); |
| 5978 | goto cleanup; |
| 5979 | } |
| 5980 | } |
| 5981 | if (!LY_ARRAY_COUNT(list->uniques)) { |
| 5982 | LY_ARRAY_FREE(list->uniques); |
| 5983 | list->uniques = NULL; |
| 5984 | } |
| 5985 | } |
| 5986 | |
| 5987 | /* *default-stmt */ |
| 5988 | if (d->dflts) { |
| 5989 | switch (target->nodetype) { |
| 5990 | case LYS_LEAF: |
| 5991 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5992 | LY_CHECK_GOTO(lys_apply_deviate_delete_leaf_dflt(ctx, target, d->dflts[0]), cleanup); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5993 | break; |
| 5994 | case LYS_LEAFLIST: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5995 | LY_CHECK_GOTO(lys_apply_deviate_delete_llist_dflts(ctx, target, d->dflts), cleanup); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5996 | break; |
| 5997 | case LYS_CHOICE: |
| 5998 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 5999 | DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "deleting", "default", d->dflts[0]); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6000 | nodeid = d->dflts[0]; |
| 6001 | LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup); |
| 6002 | if (prefix) { |
| 6003 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6004 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6005 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6006 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6007 | "The prefix does not match any imported module of the deviation module.", d->dflts[0]); |
| 6008 | goto cleanup; |
| 6009 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6010 | if (mod != ((struct lysc_node_choice *)target)->dflt->module) { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6011 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6012 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6013 | "The prefix does not match the default case's module.", d->dflts[0]); |
| 6014 | goto cleanup; |
| 6015 | } |
| 6016 | } |
| 6017 | /* else { |
| 6018 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6019 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6020 | * the module of the default case, just matching its name */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6021 | if (strcmp(name, ((struct lysc_node_choice *)target)->dflt->name)) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6022 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6023 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6024 | d->dflts[0], ((struct lysc_node_choice *)target)->dflt->name); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6025 | goto cleanup; |
| 6026 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6027 | ((struct lysc_node_choice *)target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6028 | ((struct lysc_node_choice *)target)->dflt = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6029 | break; |
| 6030 | default: |
| 6031 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6032 | lys_nodetype2str(target->nodetype), "delete", "default"); |
| 6033 | goto cleanup; |
| 6034 | } |
| 6035 | } |
| 6036 | |
| 6037 | ret = LY_SUCCESS; |
| 6038 | |
| 6039 | cleanup: |
| 6040 | return ret; |
| 6041 | } |
| 6042 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6043 | static LY_ERR |
| 6044 | lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target) |
| 6045 | { |
| 6046 | LY_ERR ret; |
| 6047 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 6048 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 6049 | struct ly_err_item *err = NULL; |
| 6050 | LY_ARRAY_COUNT_TYPE x; |
| 6051 | const char *dflt; |
| 6052 | int dyn; |
| 6053 | |
| 6054 | if (target->module != ctx->mod) { |
| 6055 | /* foreign deviation */ |
| 6056 | if (target->nodetype == LYS_LEAF) { |
| 6057 | dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn); |
| 6058 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6059 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6060 | |
| 6061 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 6062 | LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err); |
| 6063 | if (dyn) { |
| 6064 | free((char *)dflt); |
| 6065 | } |
| 6066 | if (err) { |
| 6067 | ly_err_print(err); |
| 6068 | ctx->path[0] = '\0'; |
| 6069 | lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6070 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6071 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6072 | ly_err_free(err); |
| 6073 | } |
| 6074 | LY_CHECK_RET(ret); |
| 6075 | |
| 6076 | ++leaf->dflt->realtype->refcount; |
| 6077 | } else { /* LY_LEAFLIST */ |
| 6078 | LY_ARRAY_FOR(llist->dflts, x) { |
| 6079 | dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn); |
| 6080 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6081 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6082 | |
| 6083 | ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 6084 | LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err); |
| 6085 | if (dyn) { |
| 6086 | free((char *)dflt); |
| 6087 | } |
| 6088 | if (err) { |
| 6089 | ly_err_print(err); |
| 6090 | ctx->path[0] = '\0'; |
| 6091 | lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6092 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6093 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6094 | ly_err_free(err); |
| 6095 | } |
| 6096 | LY_CHECK_RET(ret); |
| 6097 | |
| 6098 | ++llist->dflts[x]->realtype->refcount; |
| 6099 | } |
| 6100 | } |
| 6101 | } else { |
| 6102 | /* local deviation */ |
| 6103 | |
| 6104 | /* these default were not compiled yet, so they will use the new type automatically */ |
| 6105 | } |
| 6106 | |
| 6107 | return LY_SUCCESS; |
| 6108 | } |
| 6109 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6110 | /** |
| 6111 | * @brief Apply deviate replace. |
| 6112 | * |
| 6113 | * @param[in] ctx Compile context. |
| 6114 | * @param[in] target Deviation target. |
| 6115 | * @param[in] d Deviate replace to apply. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6116 | * @return LY_ERR value. |
| 6117 | */ |
| 6118 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6119 | lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysc_node *target, struct lysp_deviate_rpl *d) |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6120 | { |
| 6121 | LY_ERR ret = LY_EVALID; |
| 6122 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 6123 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 6124 | LY_ARRAY_COUNT_TYPE x; |
| 6125 | |
| 6126 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 6127 | if (!(((TYPE)target)->MEMBER COND)) { \ |
| 6128 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 6129 | "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \ |
| 6130 | goto cleanup; \ |
| 6131 | } |
| 6132 | |
| 6133 | /* [type-stmt] */ |
| 6134 | if (d->type) { |
| 6135 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6136 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6137 | lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type); |
| 6138 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6139 | /* remove only default value inherited from the type */ |
| 6140 | if (!(target->flags & LYS_SET_DFLT)) { |
| 6141 | if (target->module != ctx->mod) { |
| 6142 | /* foreign deviation - the target has default from the previous type, remove it */ |
| 6143 | if (target->nodetype == LYS_LEAF) { |
| 6144 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6145 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6146 | free(leaf->dflt); |
| 6147 | leaf->dflt = NULL; |
| 6148 | } else { /* LYS_LEAFLIST */ |
| 6149 | LY_ARRAY_FOR(llist->dflts, x) { |
| 6150 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6151 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6152 | free(llist->dflts[x]); |
| 6153 | } |
| 6154 | LY_ARRAY_FREE(llist->dflts); |
| 6155 | llist->dflts = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6156 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6157 | } else { |
| 6158 | /* local deviation */ |
| 6159 | lysc_incomplete_dflt_remove(ctx, target); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6160 | } |
| 6161 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6162 | |
| 6163 | LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf)); |
| 6164 | |
| 6165 | if (target->flags & LYS_SET_DFLT) { |
| 6166 | /* the term default value(s) needs to be recompiled */ |
| 6167 | LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target)); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6168 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6169 | } |
| 6170 | |
| 6171 | /* [units-stmt] */ |
| 6172 | if (d->units) { |
| 6173 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6174 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS), |
| 6175 | units, "replacing", "units", d->units); |
| 6176 | |
| 6177 | lydict_remove(ctx->ctx, leaf->units); |
| 6178 | DUP_STRING(ctx->ctx, d->units, leaf->units); |
| 6179 | } |
| 6180 | |
| 6181 | /* [default-stmt] */ |
| 6182 | if (d->dflt) { |
| 6183 | switch (target->nodetype) { |
| 6184 | case LYS_LEAF: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6185 | if (target->module != ctx->mod) { |
| 6186 | /* foreign deviation */ |
| 6187 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing", |
| 6188 | "default", d->dflt); |
| 6189 | |
| 6190 | /* remove the default specification */ |
| 6191 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6192 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6193 | free(leaf->dflt); |
| 6194 | leaf->dflt = NULL; |
| 6195 | } else { |
| 6196 | /* local deviation */ |
| 6197 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing", |
| 6198 | "default", d->dflt); |
| 6199 | assert(!leaf->dflt); |
| 6200 | } |
| 6201 | |
| 6202 | /* store the new default value */ |
| 6203 | LY_CHECK_RET(lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflt, ctx->mod_def)); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6204 | break; |
| 6205 | case LYS_CHOICE: |
| 6206 | DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt); |
| 6207 | if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) { |
| 6208 | goto cleanup; |
| 6209 | } |
| 6210 | break; |
| 6211 | default: |
| 6212 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6213 | lys_nodetype2str(target->nodetype), "replace", "default"); |
| 6214 | goto cleanup; |
| 6215 | } |
| 6216 | } |
| 6217 | |
| 6218 | /* [config-stmt] */ |
| 6219 | if (d->flags & LYS_CONFIG_MASK) { |
| 6220 | if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 6221 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6222 | lys_nodetype2str(target->nodetype), "replace", "config"); |
| 6223 | goto cleanup; |
| 6224 | } |
| 6225 | if (!(target->flags & LYS_SET_CONFIG)) { |
| 6226 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6227 | "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6228 | goto cleanup; |
| 6229 | } |
| 6230 | LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup); |
| 6231 | } |
| 6232 | |
| 6233 | /* [mandatory-stmt] */ |
| 6234 | if (d->flags & LYS_MAND_MASK) { |
| 6235 | if (!(target->flags & LYS_MAND_MASK)) { |
| 6236 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6237 | "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6238 | goto cleanup; |
| 6239 | } |
| 6240 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup); |
| 6241 | } |
| 6242 | |
| 6243 | /* [min-elements-stmt] */ |
| 6244 | if (d->flags & LYS_SET_MIN) { |
| 6245 | if (target->nodetype == LYS_LEAFLIST) { |
| 6246 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements"); |
| 6247 | /* change value */ |
| 6248 | ((struct lysc_node_leaflist *)target)->min = d->min; |
| 6249 | } else if (target->nodetype == LYS_LIST) { |
| 6250 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements"); |
| 6251 | /* change value */ |
| 6252 | ((struct lysc_node_list *)target)->min = d->min; |
| 6253 | } else { |
| 6254 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6255 | lys_nodetype2str(target->nodetype), "replace", "min-elements"); |
| 6256 | goto cleanup; |
| 6257 | } |
| 6258 | if (d->min) { |
| 6259 | target->flags |= LYS_MAND_TRUE; |
| 6260 | } |
| 6261 | } |
| 6262 | |
| 6263 | /* [max-elements-stmt] */ |
| 6264 | if (d->flags & LYS_SET_MAX) { |
| 6265 | if (target->nodetype == LYS_LEAFLIST) { |
| 6266 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements"); |
| 6267 | /* change value */ |
| 6268 | ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1; |
| 6269 | } else if (target->nodetype == LYS_LIST) { |
| 6270 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements"); |
| 6271 | /* change value */ |
| 6272 | ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1; |
| 6273 | } else { |
| 6274 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6275 | lys_nodetype2str(target->nodetype), "replace", "max-elements"); |
| 6276 | goto cleanup; |
| 6277 | } |
| 6278 | } |
| 6279 | |
| 6280 | ret = LY_SUCCESS; |
| 6281 | |
| 6282 | cleanup: |
| 6283 | return ret; |
| 6284 | } |
| 6285 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6286 | /** |
| 6287 | * @brief Apply all deviations of one target node. |
| 6288 | * |
| 6289 | * @param[in] ctx Compile context. |
| 6290 | * @param[in] dev Deviation structure to apply. |
| 6291 | * @return LY_ERR value. |
| 6292 | */ |
| 6293 | static LY_ERR |
| 6294 | lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6295 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6296 | LY_ERR ret = LY_EVALID; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6297 | struct lysc_node *target = dev->target; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6298 | struct lysc_action *rpcs; |
| 6299 | struct lysc_notif *notifs; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6300 | struct lysp_deviate *d; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6301 | LY_ARRAY_COUNT_TYPE v, x; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6302 | uint32_t min, max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6303 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6304 | lysc_update_path(ctx, NULL, dev->nodeid); |
| 6305 | |
| 6306 | /* not-supported */ |
| 6307 | if (dev->not_supported) { |
| 6308 | if (LY_ARRAY_COUNT(dev->deviates) > 1) { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 6309 | LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE ") deviates on node \"%s\" since the node is not-supported.", |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6310 | LY_ARRAY_COUNT(dev->deviates), dev->nodeid); |
| 6311 | } |
| 6312 | |
| 6313 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 6314 | if (target->parent) { \ |
| 6315 | ARRAY = (TYPE*)GETFUNC(target->parent); \ |
| 6316 | } else { \ |
| 6317 | ARRAY = target->module->compiled->ARRAY; \ |
| 6318 | } \ |
| 6319 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 6320 | if (&ARRAY[x] == (TYPE*)target) { break; } \ |
| 6321 | } \ |
| 6322 | if (x < LY_ARRAY_COUNT(ARRAY)) { \ |
| 6323 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 6324 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 6325 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 6326 | } |
| 6327 | |
| 6328 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 6329 | if (dev->flags & LYSC_OPT_RPC_INPUT) { |
| 6330 | /* remove RPC's/action's input */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6331 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input); |
| 6332 | memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input); |
| 6333 | FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free); |
| 6334 | ((struct lysc_action *)target)->input_exts = NULL; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6335 | } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6336 | /* remove RPC's/action's output */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6337 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output); |
| 6338 | memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output); |
| 6339 | FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free); |
| 6340 | ((struct lysc_action *)target)->output_exts = NULL; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6341 | } else { |
| 6342 | /* remove RPC/action */ |
| 6343 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
| 6344 | } |
| 6345 | } else if (target->nodetype == LYS_NOTIF) { |
| 6346 | /* remove Notification */ |
| 6347 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
| 6348 | } else { |
| 6349 | /* remove the target node */ |
| 6350 | lysc_disconnect(target); |
| 6351 | lysc_node_free(ctx->ctx, target); |
| 6352 | } |
| 6353 | |
| 6354 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 6355 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
| 6356 | return LY_SUCCESS; |
| 6357 | } |
| 6358 | |
| 6359 | /* list of deviates (not-supported is not present in the list) */ |
| 6360 | LY_ARRAY_FOR(dev->deviates, v) { |
| 6361 | d = dev->deviates[v]; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6362 | switch (d->mod) { |
| 6363 | case LYS_DEV_ADD: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6364 | LY_CHECK_GOTO(lys_apply_deviate_add(ctx, target, dev->flags, (struct lysp_deviate_add *)d), cleanup); |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6365 | break; |
| 6366 | case LYS_DEV_DELETE: |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6367 | LY_CHECK_GOTO(lys_apply_deviate_delete(ctx, target, dev->flags, (struct lysp_deviate_del *)d), cleanup); |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6368 | break; |
| 6369 | case LYS_DEV_REPLACE: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6370 | LY_CHECK_GOTO(lys_apply_deviate_replace(ctx, target, (struct lysp_deviate_rpl *)d), cleanup); |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6371 | break; |
| 6372 | default: |
| 6373 | LOGINT(ctx->ctx); |
| 6374 | goto cleanup; |
| 6375 | } |
| 6376 | } |
| 6377 | |
| 6378 | /* final check when all deviations of a single target node are applied */ |
| 6379 | |
| 6380 | /* check min-max compatibility */ |
| 6381 | if (target->nodetype == LYS_LEAFLIST) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6382 | min = ((struct lysc_node_leaflist *)target)->min; |
| 6383 | max = ((struct lysc_node_leaflist *)target)->max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6384 | } else if (target->nodetype == LYS_LIST) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6385 | min = ((struct lysc_node_list *)target)->min; |
| 6386 | max = ((struct lysc_node_list *)target)->max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6387 | } else { |
| 6388 | min = max = 0; |
| 6389 | } |
| 6390 | if (min > max) { |
| 6391 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements " |
| 6392 | "after deviation: min value %u is bigger than max value %u.", min, max); |
| 6393 | goto cleanup; |
| 6394 | } |
| 6395 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6396 | /* check mandatory - default compatibility */ |
| 6397 | if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT) |
| 6398 | && (target->flags & LYS_MAND_TRUE)) { |
| 6399 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6400 | "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype)); |
| 6401 | goto cleanup; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6402 | } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)target)->dflt |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6403 | && (target->flags & LYS_MAND_TRUE)) { |
| 6404 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice."); |
| 6405 | goto cleanup; |
| 6406 | } |
| 6407 | if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) { |
| 6408 | /* mandatory node under a default case */ |
| 6409 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6410 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6411 | lys_nodetype2str(target->nodetype), target->name, target->parent->name); |
| 6412 | goto cleanup; |
| 6413 | } |
| 6414 | |
| 6415 | /* success */ |
| 6416 | ret = LY_SUCCESS; |
| 6417 | |
| 6418 | cleanup: |
| 6419 | lysc_update_path(ctx, NULL, NULL); |
| 6420 | return ret; |
| 6421 | } |
| 6422 | |
| 6423 | LY_ERR |
| 6424 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
| 6425 | { |
| 6426 | LY_ERR ret = LY_EVALID; |
| 6427 | struct ly_set devs_p = {0}; |
| 6428 | struct ly_set targets = {0}; |
| 6429 | struct lysc_node *target; /* target target of the deviation */ |
| 6430 | struct lysp_deviation *dev; |
| 6431 | struct lysp_deviate *d, **dp_new; |
| 6432 | LY_ARRAY_COUNT_TYPE u, v; |
| 6433 | struct lysc_deviation **devs = NULL; |
| 6434 | struct lys_module *target_mod, **dev_mod; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6435 | uint16_t flags; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6436 | int i; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6437 | |
| 6438 | /* get all deviations from the module and all its submodules ... */ |
| 6439 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 6440 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 6441 | } |
| 6442 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 6443 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 6444 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 6445 | } |
| 6446 | } |
| 6447 | if (!devs_p.count) { |
| 6448 | /* nothing to do */ |
| 6449 | return LY_SUCCESS; |
| 6450 | } |
| 6451 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6452 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 6453 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6454 | /* ... and group them by the target node */ |
| 6455 | devs = calloc(devs_p.count, sizeof *devs); |
| 6456 | for (u = 0; u < devs_p.count; ++u) { |
| 6457 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6458 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6459 | |
| 6460 | /* resolve the target */ |
Michal Vasko | cb18c7f | 2020-08-24 09:22:28 +0200 | [diff] [blame] | 6461 | LY_CHECK_GOTO(lysc_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6462 | (const struct lysc_node **)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6463 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6464 | /* move the target pointer to input/output to make them different from the action and |
| 6465 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 6466 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 6467 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 6468 | if (flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6469 | target = (struct lysc_node *)&((struct lysc_action *)target)->input; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6470 | flags |= LYSC_OPT_INTERNAL; |
| 6471 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6472 | target = (struct lysc_node *)&((struct lysc_action *)target)->output; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6473 | flags |= LYSC_OPT_INTERNAL; |
| 6474 | } |
| 6475 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6476 | /* insert into the set of targets with duplicity detection */ |
| 6477 | i = ly_set_add(&targets, target, 0); |
| 6478 | if (!devs[i]) { |
| 6479 | /* new record */ |
| 6480 | devs[i] = calloc(1, sizeof **devs); |
| 6481 | devs[i]->target = target; |
| 6482 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6483 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6484 | } |
| 6485 | /* add deviates into the deviation's list of deviates */ |
Michal Vasko | 5c38f36 | 2020-08-24 09:22:51 +0200 | [diff] [blame] | 6486 | LY_LIST_FOR(dev->deviates, d) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6487 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 6488 | *dp_new = d; |
| 6489 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 6490 | devs[i]->not_supported = 1; |
| 6491 | } |
| 6492 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6493 | |
| 6494 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6495 | } |
| 6496 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6497 | /* apply deviations */ |
| 6498 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6499 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 6500 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 6501 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6502 | devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, input)); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6503 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6504 | devs[u]->target = (struct lysc_node *)((char *)devs[u]->target - offsetof(struct lysc_action, output)); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6505 | } |
| 6506 | } |
| 6507 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6508 | /* remember target module (the target node may be removed) */ |
| 6509 | target_mod = devs[u]->target->module; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6510 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6511 | /* apply the deviation */ |
| 6512 | LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6513 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6514 | /* add this module into the target module deviated_by, if not there already */ |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6515 | i = 0; |
| 6516 | LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) { |
| 6517 | if (target_mod->compiled->deviated_by[v] == mod_p->mod) { |
| 6518 | i = 1; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6519 | break; |
| 6520 | } |
| 6521 | } |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6522 | if (!i) { |
| 6523 | LY_ARRAY_NEW_GOTO(ctx->ctx, target_mod->compiled->deviated_by, dev_mod, ret, cleanup); |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6524 | *dev_mod = mod_p->mod; |
| 6525 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6526 | } |
| 6527 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6528 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6529 | ret = LY_SUCCESS; |
| 6530 | |
| 6531 | cleanup: |
| 6532 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6533 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6534 | free(devs[u]); |
| 6535 | } |
| 6536 | free(devs); |
| 6537 | ly_set_erase(&targets, NULL); |
| 6538 | ly_set_erase(&devs_p, NULL); |
| 6539 | |
| 6540 | return ret; |
| 6541 | } |
| 6542 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6543 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6544 | * @brief Compile the given YANG submodule into the main module. |
| 6545 | * @param[in] ctx Compile context |
| 6546 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6547 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6548 | */ |
| 6549 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6550 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6551 | { |
| 6552 | unsigned int u; |
| 6553 | LY_ERR ret = LY_SUCCESS; |
| 6554 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6555 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6556 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6557 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6558 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6559 | if (!mainmod->mod->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6560 | /* features are compiled directly into the compiled module structure, |
| 6561 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6562 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6563 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6564 | LY_CHECK_GOTO(ret, error); |
| 6565 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6566 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6567 | if (!mainmod->mod->dis_identities) { |
| 6568 | ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities); |
| 6569 | LY_CHECK_GOTO(ret, error); |
| 6570 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6571 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6572 | /* data nodes */ |
| 6573 | LY_LIST_FOR(submod->data, node_p) { |
| 6574 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6575 | LY_CHECK_GOTO(ret, error); |
| 6576 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6577 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6578 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6579 | COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6580 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6581 | error: |
| 6582 | return ret; |
| 6583 | } |
| 6584 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6585 | static void * |
| 6586 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6587 | { |
| 6588 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6589 | if (substmts[u].stmt == stmt) { |
| 6590 | return substmts[u].storage; |
| 6591 | } |
| 6592 | } |
| 6593 | return NULL; |
| 6594 | } |
| 6595 | |
| 6596 | LY_ERR |
| 6597 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6598 | { |
| 6599 | LY_ERR ret = LY_EVALID, r; |
| 6600 | unsigned int u; |
| 6601 | struct lysp_stmt *stmt; |
| 6602 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6603 | |
| 6604 | /* check for invalid substatements */ |
| 6605 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6606 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6607 | continue; |
| 6608 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6609 | for (u = 0; substmts[u].stmt; ++u) { |
| 6610 | if (substmts[u].stmt == stmt->kw) { |
| 6611 | break; |
| 6612 | } |
| 6613 | } |
| 6614 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6615 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6616 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6617 | goto cleanup; |
| 6618 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6619 | } |
| 6620 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6621 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6622 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6623 | /* keep order of the processing the same as the order in the defined substmts, |
| 6624 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6625 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6626 | int stmt_present = 0; |
| 6627 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6628 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6629 | if (substmts[u].stmt != stmt->kw) { |
| 6630 | continue; |
| 6631 | } |
| 6632 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6633 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6634 | if (substmts[u].storage) { |
| 6635 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6636 | case LY_STMT_STATUS: |
| 6637 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6638 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6639 | break; |
| 6640 | case LY_STMT_UNITS: { |
| 6641 | const char **units; |
| 6642 | |
| 6643 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6644 | /* single item */ |
| 6645 | if (*((const char **)substmts[u].storage)) { |
| 6646 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6647 | goto cleanup; |
| 6648 | } |
| 6649 | units = (const char **)substmts[u].storage; |
| 6650 | } else { |
| 6651 | /* sized array */ |
| 6652 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6653 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6654 | } |
| 6655 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6656 | break; |
| 6657 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6658 | case LY_STMT_TYPE: { |
| 6659 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6660 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6661 | |
| 6662 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6663 | /* single item */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6664 | if (*(struct lysc_type **)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6665 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6666 | goto cleanup; |
| 6667 | } |
| 6668 | compiled = substmts[u].storage; |
| 6669 | } else { |
| 6670 | /* sized array */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6671 | struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6672 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6673 | compiled = (void *)type; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6674 | } |
| 6675 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6676 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6677 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL, |
| 6678 | flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type **)compiled, |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6679 | units && !*units ? units : NULL, NULL, NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 6680 | lysp_type_free(ctx->ctx, parsed); |
| 6681 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6682 | break; |
| 6683 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6684 | case LY_STMT_IF_FEATURE: { |
| 6685 | struct lysc_iffeature *iff = NULL; |
| 6686 | |
| 6687 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6688 | /* single item */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6689 | if (((struct lysc_iffeature *)substmts[u].storage)->features) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6690 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6691 | goto cleanup; |
| 6692 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6693 | iff = (struct lysc_iffeature *)substmts[u].storage; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6694 | } else { |
| 6695 | /* sized array */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 6696 | struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6697 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6698 | } |
| 6699 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6700 | break; |
| 6701 | } |
| 6702 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6703 | * also note that in many statements their extensions are not taken into account */ |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6704 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6705 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s%s%s\") substatement.", |
| 6706 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6707 | goto cleanup; |
| 6708 | } |
| 6709 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6710 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6711 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6712 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6713 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6714 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6715 | goto cleanup; |
| 6716 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6717 | } |
| 6718 | |
| 6719 | ret = LY_SUCCESS; |
| 6720 | |
| 6721 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6722 | return ret; |
| 6723 | } |
| 6724 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6725 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6726 | * @brief Check when for cyclic dependencies. |
| 6727 | * @param[in] set Set with all the referenced nodes. |
| 6728 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6729 | * @return LY_ERR value |
| 6730 | */ |
| 6731 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6732 | lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node) |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6733 | { |
| 6734 | struct lyxp_set tmp_set; |
| 6735 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6736 | uint32_t i, j; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6737 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6738 | int idx; |
| 6739 | struct lysc_when *when; |
| 6740 | LY_ERR ret = LY_SUCCESS; |
| 6741 | |
| 6742 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6743 | |
| 6744 | /* prepare in_ctx of the set */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6745 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6746 | xp_scnode = &set->val.scnodes[i]; |
| 6747 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6748 | if (xp_scnode->in_ctx != -1) { |
| 6749 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6750 | xp_scnode->in_ctx = 1; |
| 6751 | } |
| 6752 | } |
| 6753 | |
| 6754 | for (i = 0; i < set->used; ++i) { |
| 6755 | xp_scnode = &set->val.scnodes[i]; |
| 6756 | if (xp_scnode->in_ctx != 1) { |
| 6757 | /* already checked */ |
| 6758 | continue; |
| 6759 | } |
| 6760 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6761 | if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) |
Michal Vasko | 2ff7efe | 2019-12-10 14:50:59 +0100 | [diff] [blame] | 6762 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6763 | /* no when to check */ |
| 6764 | xp_scnode->in_ctx = 0; |
| 6765 | continue; |
| 6766 | } |
| 6767 | |
| 6768 | node = xp_scnode->scnode; |
| 6769 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6770 | LY_ARRAY_FOR(node->when, u) { |
| 6771 | when = node->when[u]; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6772 | ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6773 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 6774 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6775 | LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6776 | goto cleanup; |
| 6777 | } |
| 6778 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6779 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6780 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6781 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6782 | /* try to find this node in our set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6783 | idx = lyxp_set_scnode_dup_node_check(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6784 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6785 | LOGVAL(set->ctx, LY_VLOG_LYSC, node, LY_VCODE_CIRC_WHEN, node->name, set->val.scnodes[idx].scnode->name); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6786 | ret = LY_EVALID; |
| 6787 | goto cleanup; |
| 6788 | } |
| 6789 | |
| 6790 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6791 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6792 | } else { |
| 6793 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6794 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6795 | } |
| 6796 | } |
| 6797 | |
| 6798 | /* merge this set into the global when set */ |
| 6799 | lyxp_set_scnode_merge(set, &tmp_set); |
| 6800 | } |
| 6801 | |
| 6802 | /* check when of non-data parents as well */ |
| 6803 | node = node->parent; |
| 6804 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 6805 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6806 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 6807 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6808 | } |
| 6809 | |
| 6810 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6811 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6812 | return ret; |
| 6813 | } |
| 6814 | |
| 6815 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6816 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 6817 | * @param[in] ctx Compile context. |
| 6818 | * @param[in] node Node to check. |
| 6819 | * @return LY_ERR value |
| 6820 | */ |
| 6821 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6822 | lys_compile_unres_xpath(struct lysc_ctx *ctx, const struct lysc_node *node) |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6823 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6824 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6825 | uint32_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6826 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6827 | int opts, input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6828 | struct lysc_when **when = NULL; |
| 6829 | struct lysc_must *musts = NULL; |
| 6830 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6831 | const struct lysc_node *op; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6832 | |
| 6833 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6834 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6835 | if (node->flags & LYS_CONFIG_R) { |
| 6836 | for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent); |
| 6837 | if (op) { |
| 6838 | /* we are actually in output */ |
| 6839 | opts = LYXP_SCNODE_OUTPUT; |
| 6840 | } |
| 6841 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6842 | |
| 6843 | switch (node->nodetype) { |
| 6844 | case LYS_CONTAINER: |
| 6845 | when = ((struct lysc_node_container *)node)->when; |
| 6846 | musts = ((struct lysc_node_container *)node)->musts; |
| 6847 | break; |
| 6848 | case LYS_CHOICE: |
| 6849 | when = ((struct lysc_node_choice *)node)->when; |
| 6850 | break; |
| 6851 | case LYS_LEAF: |
| 6852 | when = ((struct lysc_node_leaf *)node)->when; |
| 6853 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 6854 | break; |
| 6855 | case LYS_LEAFLIST: |
| 6856 | when = ((struct lysc_node_leaflist *)node)->when; |
| 6857 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 6858 | break; |
| 6859 | case LYS_LIST: |
| 6860 | when = ((struct lysc_node_list *)node)->when; |
| 6861 | musts = ((struct lysc_node_list *)node)->musts; |
| 6862 | break; |
| 6863 | case LYS_ANYXML: |
| 6864 | case LYS_ANYDATA: |
| 6865 | when = ((struct lysc_node_anydata *)node)->when; |
| 6866 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 6867 | break; |
| 6868 | case LYS_CASE: |
| 6869 | when = ((struct lysc_node_case *)node)->when; |
| 6870 | break; |
| 6871 | case LYS_NOTIF: |
| 6872 | musts = ((struct lysc_notif *)node)->musts; |
| 6873 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6874 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6875 | case LYS_ACTION: |
| 6876 | /* first process input musts */ |
| 6877 | musts = ((struct lysc_action *)node)->input.musts; |
| 6878 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6879 | default: |
| 6880 | /* nothing to check */ |
| 6881 | break; |
| 6882 | } |
| 6883 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6884 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6885 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6886 | ret = lyxp_atomize(when[u]->cond, LY_PREF_SCHEMA, when[u]->module, when[u]->context ? when[u]->context : node, |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6887 | when[u]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6888 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6889 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6890 | goto cleanup; |
| 6891 | } |
| 6892 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6893 | ctx->path[0] = '\0'; |
| 6894 | lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6895 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6896 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6897 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 6898 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6899 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6900 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6901 | ret = lysc_check_status(ctx, when[u]->flags, when[u]->module, node->name, schema->flags, schema->module, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6902 | schema->name); |
| 6903 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6904 | |
| 6905 | /* check dummy node accessing */ |
| 6906 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6907 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6908 | ret = LY_EVALID; |
| 6909 | goto cleanup; |
| 6910 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6911 | } |
| 6912 | } |
| 6913 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6914 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6915 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6916 | LY_CHECK_GOTO(ret, cleanup); |
| 6917 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6918 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6919 | } |
| 6920 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6921 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6922 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6923 | LY_ARRAY_FOR(musts, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6924 | ret = lyxp_atomize(musts[u].cond, LY_PREF_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, &tmp_set, opts); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6925 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6926 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[u].cond->expr); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6927 | goto cleanup; |
| 6928 | } |
| 6929 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6930 | ctx->path[0] = '\0'; |
| 6931 | lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6932 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6933 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6934 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6935 | /* XPath expression cannot reference "lower" status than the node that has the definition */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6936 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 6937 | tmp_set.val.scnodes[i].scnode->module, tmp_set.val.scnodes[i].scnode->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6938 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6939 | } |
| 6940 | } |
| 6941 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6942 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6943 | } |
| 6944 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6945 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6946 | /* now check output musts */ |
| 6947 | input_done = 1; |
| 6948 | musts = ((struct lysc_action *)node)->output.musts; |
| 6949 | opts = LYXP_SCNODE_OUTPUT; |
| 6950 | goto check_musts; |
| 6951 | } |
| 6952 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6953 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6954 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6955 | return ret; |
| 6956 | } |
| 6957 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 6958 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6959 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 6960 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6961 | const struct lysc_node *target = NULL, *siter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6962 | struct ly_path *p; |
| 6963 | struct lysc_type *type; |
| 6964 | |
| 6965 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 6966 | |
| 6967 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 6968 | LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, |
| 6969 | lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6970 | LY_PREF_SCHEMA, lref->path_context, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6971 | |
| 6972 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6973 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6974 | ly_path_free(node->module->ctx, p); |
| 6975 | |
| 6976 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 6977 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6978 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 6979 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
| 6980 | return LY_EVALID; |
| 6981 | } |
| 6982 | |
| 6983 | /* check status */ |
| 6984 | ctx->path[0] = '\0'; |
| 6985 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6986 | ctx->path_len = strlen(ctx->path); |
| 6987 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 6988 | return LY_EVALID; |
| 6989 | } |
| 6990 | ctx->path_len = 1; |
| 6991 | ctx->path[1] = '\0'; |
| 6992 | |
| 6993 | /* check config */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6994 | if (lref->require_instance) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 6995 | for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent) {} |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6996 | if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6997 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
| 6998 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
| 6999 | return LY_EVALID; |
| 7000 | } |
| 7001 | } |
| 7002 | |
| 7003 | /* store the target's type and check for circular chain of leafrefs */ |
| 7004 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 7005 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 7006 | if (type == (struct lysc_type *)lref) { |
| 7007 | /* circular chain detected */ |
| 7008 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7009 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
| 7010 | return LY_EVALID; |
| 7011 | } |
| 7012 | } |
| 7013 | |
| 7014 | /* check if leafref and its target are under common if-features */ |
| 7015 | if (lys_compile_leafref_features_validate(node, target)) { |
| 7016 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7017 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 7018 | " features applicable to the leafref itself.", lref->path->expr); |
| 7019 | return LY_EVALID; |
| 7020 | } |
| 7021 | |
| 7022 | return LY_SUCCESS; |
| 7023 | } |
| 7024 | |
| 7025 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7026 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7027 | { |
| 7028 | struct lysc_ext_instance *ext; |
| 7029 | struct lysp_ext_instance *ext_p = NULL; |
| 7030 | struct lysp_stmt *stmt; |
| 7031 | const struct lys_module *ext_mod; |
| 7032 | LY_ERR ret = LY_SUCCESS; |
| 7033 | |
| 7034 | /* create the parsed extension instance manually */ |
| 7035 | ext_p = calloc(1, sizeof *ext_p); |
| 7036 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 7037 | ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0); |
| 7038 | ext_p->argument = lydict_insert(ctx->ctx, "default", 0); |
| 7039 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7040 | ext_p->insubstmt_index = 0; |
| 7041 | |
| 7042 | stmt = calloc(1, sizeof *ext_p->child); |
| 7043 | stmt->stmt = lydict_insert(ctx->ctx, "type", 0); |
| 7044 | stmt->arg = lydict_insert(ctx->ctx, "boolean", 0); |
| 7045 | stmt->kw = LY_STMT_TYPE; |
| 7046 | ext_p->child = stmt; |
| 7047 | |
| 7048 | /* allocate new extension instance */ |
| 7049 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7050 | |
| 7051 | /* manually get extension definition module */ |
| 7052 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7053 | |
| 7054 | /* compile the extension instance */ |
| 7055 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7056 | |
| 7057 | cleanup: |
| 7058 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7059 | free(ext_p); |
| 7060 | return ret; |
| 7061 | } |
| 7062 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7063 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7064 | lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_type *type, const char *dflt, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 7065 | const struct lys_module *dflt_mod, struct lyd_value *storage) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7066 | { |
| 7067 | LY_ERR ret; |
| 7068 | struct ly_err_item *err = NULL; |
| 7069 | |
| 7070 | ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 7071 | LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err); |
| 7072 | if (err) { |
| 7073 | ly_err_print(err); |
| 7074 | ctx->path[0] = '\0'; |
| 7075 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7076 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7077 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7078 | ly_err_free(err); |
| 7079 | } |
| 7080 | if (!ret) { |
| 7081 | ++storage->realtype->refcount; |
| 7082 | return LY_SUCCESS; |
| 7083 | } |
| 7084 | return ret; |
| 7085 | } |
| 7086 | |
| 7087 | static LY_ERR |
| 7088 | lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 7089 | const struct lys_module *dflt_mod) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7090 | { |
| 7091 | LY_ERR ret; |
| 7092 | |
| 7093 | assert(!leaf->dflt); |
| 7094 | |
| 7095 | if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) { |
| 7096 | /* ignore default values for keys and mandatory leaves */ |
| 7097 | return LY_SUCCESS; |
| 7098 | } |
| 7099 | |
| 7100 | /* allocate the default value */ |
| 7101 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 7102 | LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM); |
| 7103 | |
| 7104 | /* store the default value */ |
| 7105 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt); |
| 7106 | if (ret) { |
| 7107 | free(leaf->dflt); |
| 7108 | leaf->dflt = NULL; |
| 7109 | } |
| 7110 | |
| 7111 | return ret; |
| 7112 | } |
| 7113 | |
| 7114 | static LY_ERR |
| 7115 | lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char *dflt, const char **dflts, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 7116 | const struct lys_module *dflt_mod) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7117 | { |
| 7118 | LY_ERR ret; |
| 7119 | LY_ARRAY_COUNT_TYPE orig_count, u, v; |
| 7120 | |
| 7121 | assert(dflt || dflts); |
| 7122 | |
| 7123 | if (llist->dflts) { |
| 7124 | /* there were already some defaults and we are adding new by deviations */ |
| 7125 | assert(dflts); |
| 7126 | orig_count = LY_ARRAY_COUNT(llist->dflts); |
| 7127 | } else { |
| 7128 | orig_count = 0; |
| 7129 | } |
| 7130 | |
| 7131 | /* allocate new items */ |
| 7132 | if (dflts) { |
| 7133 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM); |
| 7134 | } else { |
| 7135 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM); |
| 7136 | } |
| 7137 | |
| 7138 | /* fill each new default value */ |
| 7139 | if (dflts) { |
| 7140 | LY_ARRAY_FOR(dflts, u) { |
| 7141 | llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts); |
| 7142 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod, |
| 7143 | llist->dflts[orig_count + u]); |
| 7144 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret); |
| 7145 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7146 | } |
| 7147 | } else { |
| 7148 | llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts); |
| 7149 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]); |
| 7150 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret); |
| 7151 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7152 | } |
| 7153 | |
| 7154 | /* check default value uniqueness */ |
| 7155 | if (llist->flags & LYS_CONFIG_W) { |
| 7156 | /* configuration data values must be unique - so check the default values */ |
| 7157 | for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) { |
| 7158 | for (v = 0; v < u; ++v) { |
| 7159 | if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 7160 | int dynamic = 0; |
| 7161 | const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic); |
| 7162 | |
| 7163 | lysc_update_path(ctx, llist->parent, llist->name); |
| 7164 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7165 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 7166 | lysc_update_path(ctx, NULL, NULL); |
| 7167 | if (dynamic) { |
| 7168 | free((char *)val); |
| 7169 | } |
| 7170 | return LY_EVALID; |
| 7171 | } |
| 7172 | } |
| 7173 | } |
| 7174 | } |
| 7175 | |
| 7176 | return LY_SUCCESS; |
| 7177 | } |
| 7178 | |
| 7179 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7180 | lys_compile_unres(struct lysc_ctx *ctx) |
| 7181 | { |
| 7182 | struct lysc_node *node; |
| 7183 | struct lysc_type *type, *typeiter; |
| 7184 | struct lysc_type_leafref *lref; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7185 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7186 | uint32_t i; |
| 7187 | |
| 7188 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7189 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7190 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 7191 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7192 | node = ctx->leafrefs.objs[i]; |
| 7193 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7194 | type = ((struct lysc_node_leaf *)node)->type; |
| 7195 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7196 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 7197 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7198 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7199 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7200 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 7201 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 7202 | } |
| 7203 | } |
| 7204 | } |
| 7205 | } |
| 7206 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7207 | /* store pointer to the real type */ |
| 7208 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 7209 | if (type->basetype == LY_TYPE_LEAFREF) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 7210 | for (typeiter = ((struct lysc_type_leafref *)type)->realtype; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7211 | typeiter->basetype == LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 7212 | typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {} |
| 7213 | ((struct lysc_type_leafref *)type)->realtype = typeiter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7214 | } else if (type->basetype == LY_TYPE_UNION) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 7215 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7216 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7217 | for (typeiter = ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7218 | typeiter->basetype == LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 7219 | typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {} |
| 7220 | ((struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v])->realtype = typeiter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7221 | } |
| 7222 | } |
| 7223 | } |
| 7224 | } |
| 7225 | |
| 7226 | /* check xpath */ |
| 7227 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 7228 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 7229 | } |
| 7230 | |
| 7231 | /* finish incomplete default values compilation */ |
| 7232 | for (i = 0; i < ctx->dflts.count; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7233 | struct lysc_incomplete_dflt *r = ctx->dflts.objs[i]; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7234 | if (r->leaf->nodetype == LYS_LEAF) { |
| 7235 | LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod)); |
| 7236 | } else { |
| 7237 | LY_CHECK_RET(lys_compile_unres_llist_dflts(ctx, r->llist, r->dflt, r->dflts, r->dflt_mod)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7238 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7239 | } |
| 7240 | |
| 7241 | return LY_SUCCESS; |
| 7242 | } |
| 7243 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7244 | LY_ERR |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7245 | lys_compile(struct lys_module **mod, int options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7246 | { |
| 7247 | struct lysc_ctx ctx = {0}; |
| 7248 | struct lysc_module *mod_c; |
| 7249 | struct lysp_module *sp; |
| 7250 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7251 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7252 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7253 | struct lys_module *m; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7254 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7255 | uint32_t i; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7256 | uint16_t compile_id; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7257 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7258 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7259 | LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7260 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7261 | if (!(*mod)->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7262 | /* just imported modules are not compiled */ |
| 7263 | return LY_SUCCESS; |
| 7264 | } |
| 7265 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7266 | compile_id = ++(*mod)->ctx->module_set_id; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7267 | sp = (*mod)->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7268 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7269 | ctx.ctx = (*mod)->ctx; |
| 7270 | ctx.mod = *mod; |
| 7271 | ctx.mod_def = *mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7272 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7273 | ctx.path_len = 1; |
| 7274 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7275 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7276 | (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 7277 | LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM); |
| 7278 | mod_c->mod = *mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7279 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7280 | LY_ARRAY_FOR(sp->imports, u) { |
| 7281 | LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error); |
| 7282 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7283 | LY_ARRAY_FOR(sp->includes, u) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7284 | LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7285 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7286 | |
| 7287 | /* features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7288 | if ((*mod)->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7289 | /* there is already precompiled array of features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7290 | mod_c->features = (*mod)->dis_features; |
| 7291 | (*mod)->dis_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7292 | } else { |
| 7293 | /* features are compiled directly into the compiled module structure, |
| 7294 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7295 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7296 | LY_CHECK_GOTO(ret, error); |
| 7297 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7298 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7299 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7300 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7301 | * are present. */ |
| 7302 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7303 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7304 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7305 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7306 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7307 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7308 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7309 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7310 | ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7311 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7312 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7313 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7314 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7315 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7316 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7317 | /* identities, work similarly to features with the precompilation */ |
| 7318 | if ((*mod)->dis_identities) { |
| 7319 | mod_c->identities = (*mod)->dis_identities; |
| 7320 | (*mod)->dis_identities = NULL; |
| 7321 | } else { |
| 7322 | ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities); |
| 7323 | LY_CHECK_GOTO(ret, error); |
| 7324 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7325 | if (sp->identities) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7326 | LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7327 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7328 | lysc_update_path(&ctx, NULL, "{submodule}"); |
| 7329 | LY_ARRAY_FOR(sp->includes, v) { |
| 7330 | if (sp->includes[v].submodule->identities) { |
| 7331 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
| 7332 | ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities); |
| 7333 | LY_CHECK_GOTO(ret, error); |
| 7334 | lysc_update_path(&ctx, NULL, NULL); |
| 7335 | } |
| 7336 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7337 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7338 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7339 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7340 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7341 | LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7342 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7343 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7344 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7345 | COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7346 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7347 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7348 | LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7349 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7350 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7351 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7352 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7353 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7354 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7355 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7356 | /* extension instances TODO cover extension instances from submodules */ |
| 7357 | COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7358 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7359 | /* finish compilation for all unresolved items in the context */ |
| 7360 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7361 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7362 | /* validate non-instantiated groupings from the parsed schema, |
| 7363 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7364 | ctx.options |= LYSC_OPT_GROUPING; |
| 7365 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7366 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7367 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7368 | } |
| 7369 | } |
| 7370 | LY_LIST_FOR(sp->data, node_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame^] | 7371 | grps = (struct lysp_grp *)lysp_node_groupings(node_p); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7372 | LY_ARRAY_FOR(grps, u) { |
| 7373 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7374 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7375 | } |
| 7376 | } |
| 7377 | } |
| 7378 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7379 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7380 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7381 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7382 | } |
| 7383 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7384 | #if 0 |
| 7385 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7386 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7387 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7388 | * the anotation definitions available in the internal schema structure. */ |
| 7389 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7390 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7391 | lys_free(mod, NULL, 1, 1); |
| 7392 | return NULL; |
| 7393 | } |
| 7394 | } |
| 7395 | #endif |
| 7396 | |
| 7397 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7398 | if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) { |
| 7399 | LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7400 | } |
| 7401 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7402 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7403 | ly_set_erase(&ctx.xpath, NULL); |
| 7404 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7405 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7406 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7407 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7408 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7409 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7410 | lysp_module_free((*mod)->parsed); |
| 7411 | (*mod)->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7412 | } |
| 7413 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7414 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7415 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7416 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7417 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7418 | if (m->implemented > 1) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7419 | m->implemented = 1; |
| 7420 | } |
| 7421 | } |
| 7422 | } |
| 7423 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7424 | return LY_SUCCESS; |
| 7425 | |
| 7426 | error: |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7427 | lys_feature_precompile_revert(&ctx, *mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7428 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7429 | ly_set_erase(&ctx.xpath, NULL); |
| 7430 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7431 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7432 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7433 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7434 | lysc_module_free(mod_c, NULL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7435 | (*mod)->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7436 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7437 | /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency |
| 7438 | * of this module, since this module can be also compiled from dependency, there can be some other modules being |
| 7439 | * processed and we are going to get back to them via stack, so freeing them is not a good idea. */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7440 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7441 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7442 | if ((m->implemented >= compile_id) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7443 | /* revert features list to the precompiled state */ |
| 7444 | lys_feature_precompile_revert(&ctx, m); |
| 7445 | /* mark module as imported-only / not-implemented */ |
| 7446 | m->implemented = 0; |
| 7447 | /* free the compiled version of the module */ |
| 7448 | lysc_module_free(m->compiled, NULL); |
| 7449 | m->compiled = NULL; |
| 7450 | } |
| 7451 | } |
| 7452 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7453 | /* remove the module itself from the context and free it */ |
| 7454 | ly_set_rm(&ctx.ctx->list, *mod, NULL); |
| 7455 | lys_module_free(*mod, NULL); |
| 7456 | *mod = NULL; |
| 7457 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7458 | return ret; |
| 7459 | } |