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 | */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 53 | #define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);} |
| 54 | |
| 55 | #define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);} |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 56 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 57 | #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] | 58 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 59 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 60 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 61 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 62 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 63 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 64 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 65 | } \ |
| 66 | } |
| 67 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 68 | #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] | 69 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 70 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 71 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 72 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 73 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 74 | 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] | 75 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 76 | } \ |
| 77 | } |
| 78 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 79 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \ |
| 80 | if (EXTS_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 81 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \ |
| 82 | 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] | 83 | LY_ARRAY_INCREMENT(EXT_C); \ |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 84 | 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] | 85 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 86 | } \ |
| 87 | } |
| 88 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 89 | #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] | 90 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 91 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 92 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 93 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 94 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 95 | 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] | 96 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 97 | } \ |
| 98 | } |
| 99 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 100 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 101 | if (MEMBER_P) { \ |
| 102 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 103 | 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] | 104 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 105 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 106 | } |
| 107 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 108 | #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] | 109 | if (MEMBER_P) { \ |
| 110 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 111 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 112 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 113 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 114 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 115 | } |
| 116 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 117 | #define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 118 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 119 | 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] | 120 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 121 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 122 | return LY_EVALID; \ |
| 123 | } \ |
| 124 | } \ |
| 125 | } |
| 126 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 127 | #define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 128 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 129 | 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] | 130 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \ |
| 131 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 132 | return LY_EVALID; \ |
| 133 | } \ |
| 134 | } \ |
| 135 | } |
| 136 | |
| 137 | struct lysc_ext * |
| 138 | lysc_ext_dup(struct lysc_ext *orig) |
| 139 | { |
| 140 | ++orig->refcount; |
| 141 | return orig; |
| 142 | } |
| 143 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 144 | static struct lysc_ext_instance * |
| 145 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 146 | { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 147 | /* TODO - extensions, increase refcount */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 148 | (void) ctx; |
| 149 | (void) orig; |
| 150 | return NULL; |
| 151 | } |
| 152 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 153 | static LY_ERR |
| 154 | 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] | 155 | struct lys_module *dflt_mod) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 156 | { |
| 157 | struct lysc_incomplete_dflt *r; |
| 158 | uint32_t i; |
| 159 | |
| 160 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 161 | r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i]; |
| 162 | if (r->leaf == leaf) { |
| 163 | /* just replace the default */ |
| 164 | r->dflt = dflt; |
| 165 | return LY_SUCCESS; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | r = malloc(sizeof *r); |
| 170 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 171 | r->leaf = leaf; |
| 172 | r->dflt = dflt; |
| 173 | r->dflts = NULL; |
| 174 | r->dflt_mod = dflt_mod; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 175 | LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 176 | |
| 177 | return LY_SUCCESS; |
| 178 | } |
| 179 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 180 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 181 | * @brief Add record into the compile context's list of incomplete default values. |
| 182 | * @param[in] ctx Compile context with the incomplete default values list. |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 183 | * @param[in] term Term context node with the default value. |
| 184 | * @param[in] value String default value. |
| 185 | * @param[in] val_len Length of @p value. |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 186 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 187 | * @return LY_EMEM in case of memory allocation failure. |
| 188 | * @return LY_SUCCESS |
| 189 | */ |
| 190 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 191 | 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] | 192 | struct lys_module *dflt_mod) |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 193 | { |
| 194 | struct lysc_incomplete_dflt *r; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 195 | uint32_t i; |
| 196 | |
| 197 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 198 | r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i]; |
| 199 | if (r->llist == llist) { |
| 200 | /* just replace the defaults */ |
| 201 | r->dflts = dflts; |
| 202 | return LY_SUCCESS; |
| 203 | } |
| 204 | } |
| 205 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 206 | r = malloc(sizeof *r); |
| 207 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 208 | r->llist = llist; |
| 209 | r->dflt = NULL; |
| 210 | r->dflts = dflts; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 211 | r->dflt_mod = dflt_mod; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 212 | LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL)); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 213 | |
| 214 | return LY_SUCCESS; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 219 | * @param[in] ctx Compile context with the incomplete default values list. |
| 220 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 221 | */ |
| 222 | static void |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 223 | lysc_incomplete_dflt_remove(struct lysc_ctx *ctx, struct lysc_node *term) |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 224 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 225 | uint32_t u; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 226 | struct lysc_incomplete_dflt *r; |
| 227 | |
| 228 | for (u = 0; u < ctx->dflts.count; ++u) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 229 | r = ctx->dflts.objs[u]; |
| 230 | if (r->leaf == (struct lysc_node_leaf *)term) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 231 | free(ctx->dflts.objs[u]); |
| 232 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 233 | --ctx->dflts.count; |
| 234 | return; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 239 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 240 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 241 | { |
| 242 | int len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 243 | uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 244 | |
| 245 | if (!name) { |
| 246 | /* removing last path segment */ |
| 247 | if (ctx->path[ctx->path_len - 1] == '}') { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 248 | 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] | 249 | if (ctx->path[ctx->path_len] == '=') { |
| 250 | ctx->path[ctx->path_len++] = '}'; |
| 251 | } else { |
| 252 | /* not a top-level special tag, remove also preceiding '/' */ |
| 253 | goto remove_nodelevel; |
| 254 | } |
| 255 | } else { |
| 256 | remove_nodelevel: |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 257 | for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {} |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 258 | if (ctx->path_len == 0) { |
| 259 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 260 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | /* set new terminating NULL-byte */ |
| 264 | ctx->path[ctx->path_len] = '\0'; |
| 265 | } else { |
| 266 | if (ctx->path_len > 1) { |
| 267 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 268 | /* extension of the special tag */ |
| 269 | nextlevel = 2; |
| 270 | --ctx->path_len; |
| 271 | } else { |
| 272 | /* there is already some path, so add next level */ |
| 273 | nextlevel = 1; |
| 274 | } |
| 275 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 276 | |
| 277 | if (nextlevel != 2) { |
| 278 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 279 | /* module not changed, print the name unprefixed */ |
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", nextlevel ? "/" : "", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 281 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 282 | 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] | 283 | } |
| 284 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 285 | 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] | 286 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 287 | if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) { |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 288 | /* output truncated */ |
| 289 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 290 | } else { |
| 291 | ctx->path_len += len; |
| 292 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 297 | * @brief Duplicate the compiled pattern structure. |
| 298 | * |
| 299 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 300 | * |
| 301 | * @param[in] orig The pattern structure to duplicate. |
| 302 | * @return The duplicated structure to use. |
| 303 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 304 | static struct lysc_pattern * |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 305 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 306 | { |
| 307 | ++orig->refcount; |
| 308 | return orig; |
| 309 | } |
| 310 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 311 | /** |
| 312 | * @brief Duplicate the array of compiled patterns. |
| 313 | * |
| 314 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 315 | * |
| 316 | * @param[in] ctx Libyang context for logging. |
| 317 | * @param[in] orig The patterns sized array to duplicate. |
| 318 | * @return New sized array as a copy of @p orig. |
| 319 | * @return NULL in case of memory allocation error. |
| 320 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 321 | static struct lysc_pattern ** |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 322 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 323 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 324 | struct lysc_pattern **dup = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 325 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 326 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 327 | assert(orig); |
| 328 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 329 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 330 | LY_ARRAY_FOR(orig, u) { |
| 331 | dup[u] = lysc_pattern_dup(orig[u]); |
| 332 | LY_ARRAY_INCREMENT(dup); |
| 333 | } |
| 334 | return dup; |
| 335 | } |
| 336 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 337 | /** |
| 338 | * @brief Duplicate compiled range structure. |
| 339 | * |
| 340 | * @param[in] ctx Libyang context for logging. |
| 341 | * @param[in] orig The range structure to be duplicated. |
| 342 | * @return New compiled range structure as a copy of @p orig. |
| 343 | * @return NULL in case of memory allocation error. |
| 344 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 345 | struct lysc_range * |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 346 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 347 | { |
| 348 | struct lysc_range *dup; |
| 349 | LY_ERR ret; |
| 350 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 351 | assert(orig); |
| 352 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 353 | dup = calloc(1, sizeof *dup); |
| 354 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 355 | if (orig->parts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 356 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup); |
| 357 | LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts); |
| 358 | 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] | 359 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 360 | DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup); |
| 361 | DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 362 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 363 | |
| 364 | return dup; |
| 365 | cleanup: |
| 366 | free(dup); |
| 367 | (void) ret; /* set but not used due to the return type */ |
| 368 | return NULL; |
| 369 | } |
| 370 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 371 | /** |
| 372 | * @brief Stack for processing if-feature expressions. |
| 373 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 374 | struct iff_stack { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 375 | size_t size; /**< number of items in the stack */ |
| 376 | size_t index; /**< first empty item */ |
| 377 | 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] | 378 | }; |
| 379 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 380 | /** |
| 381 | * @brief Add @ref ifftokens into the stack. |
| 382 | * @param[in] stack The if-feature stack to use. |
| 383 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 384 | * @return LY_EMEM in case of memory allocation error |
| 385 | * @return LY_ESUCCESS if the value successfully stored. |
| 386 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 387 | static LY_ERR |
| 388 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 389 | { |
| 390 | if (stack->index == stack->size) { |
| 391 | stack->size += 4; |
| 392 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 393 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 394 | } |
| 395 | stack->stack[stack->index++] = value; |
| 396 | return LY_SUCCESS; |
| 397 | } |
| 398 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 399 | /** |
| 400 | * @brief Get (and remove) the last item form the stack. |
| 401 | * @param[in] stack The if-feature stack to use. |
| 402 | * @return The value from the top of the stack. |
| 403 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 404 | static uint8_t |
| 405 | iff_stack_pop(struct iff_stack *stack) |
| 406 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 407 | assert(stack && stack->index); |
| 408 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 409 | stack->index--; |
| 410 | return stack->stack[stack->index]; |
| 411 | } |
| 412 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 413 | /** |
| 414 | * @brief Clean up the stack. |
| 415 | * @param[in] stack The if-feature stack to use. |
| 416 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 417 | static void |
| 418 | iff_stack_clean(struct iff_stack *stack) |
| 419 | { |
| 420 | stack->size = 0; |
| 421 | free(stack->stack); |
| 422 | } |
| 423 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 424 | /** |
| 425 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 426 | * (libyang format of the if-feature expression). |
| 427 | * @param[in,out] list The 2bits array to modify. |
| 428 | * @param[in] op The operand (@ref ifftokens) to store. |
| 429 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 430 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 431 | static void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 432 | iff_setop(uint8_t *list, uint8_t op, size_t pos) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 433 | { |
| 434 | uint8_t *item; |
| 435 | uint8_t mask = 3; |
| 436 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 437 | assert(op <= 3); /* max 2 bits */ |
| 438 | |
| 439 | item = &list[pos / 4]; |
| 440 | mask = mask << 2 * (pos % 4); |
| 441 | *item = (*item) & ~mask; |
| 442 | *item = (*item) | (op << 2 * (pos % 4)); |
| 443 | } |
| 444 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 445 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 446 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 447 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 448 | /** |
| 449 | * @brief Find a feature of the given name and referenced in the given module. |
| 450 | * |
| 451 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 452 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 453 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 454 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 455 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 456 | * assigned till that time will be still valid. |
| 457 | * |
| 458 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 459 | * @param[in] name Name of the feature including possible prefix. |
| 460 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 461 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 462 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 463 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 464 | 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] | 465 | { |
| 466 | size_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 467 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 468 | struct lysc_feature *f; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 469 | |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 470 | assert(mod); |
| 471 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 472 | for (i = 0; i < len; ++i) { |
| 473 | if (name[i] == ':') { |
| 474 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 475 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 476 | LY_CHECK_RET(!mod, NULL); |
| 477 | |
| 478 | name = &name[i + 1]; |
| 479 | len = len - i - 1; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /* we have the correct module, get the feature */ |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 484 | LY_ARRAY_FOR(mod->features, u) { |
| 485 | f = &mod->features[u]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 486 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 487 | return f; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | return NULL; |
| 492 | } |
| 493 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 494 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 495 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 496 | */ |
| 497 | static LY_ERR |
| 498 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 499 | { |
| 500 | LY_ERR ret = LY_SUCCESS; |
| 501 | |
| 502 | if (!ext_p->compiled) { |
| 503 | lysc_update_path(ctx, NULL, "{extension}"); |
| 504 | lysc_update_path(ctx, NULL, ext_p->name); |
| 505 | |
| 506 | /* compile the extension definition */ |
| 507 | ext_p->compiled = calloc(1, sizeof **ext); |
| 508 | ext_p->compiled->refcount = 1; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 509 | DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done); |
| 510 | DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext_p->compiled->argument, ret, done); |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 511 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 512 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 513 | |
| 514 | lysc_update_path(ctx, NULL, NULL); |
| 515 | lysc_update_path(ctx, NULL, NULL); |
| 516 | |
| 517 | /* find extension definition plugin */ |
| 518 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 519 | } |
| 520 | |
| 521 | *ext = lysc_ext_dup(ext_p->compiled); |
| 522 | |
| 523 | done: |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 528 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 529 | * |
| 530 | * @param[in] ctx Compilation context. |
| 531 | * @param[in] ext_p Parsed extension instance. |
| 532 | * @param[in,out] ext Prepared compiled extension instance. |
| 533 | * @param[in] parent Extension instance parent. |
| 534 | * @param[in] parent_type Extension instance parent type. |
| 535 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 536 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 537 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 538 | 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] | 539 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 540 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 541 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 542 | const char *name; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 543 | size_t u; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 544 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 545 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 546 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 547 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret); |
| 548 | LY_CHECK_RET(ret); |
| 549 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 550 | ext->insubstmt = ext_p->insubstmt; |
| 551 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 552 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 553 | ext->parent = parent; |
| 554 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 555 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 556 | 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] | 557 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 558 | /* get module where the extension definition should be placed */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 559 | 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] | 560 | if (ext_p->yin) { |
| 561 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 562 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 563 | * YANG (import) prefix must be done here. */ |
| 564 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 565 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, &ext_p->name[u], 0, &prefixed_name), cleanup); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 566 | u = 0; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 567 | } else { |
| 568 | assert(ctx->mod_def->parsed); |
| 569 | LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) { |
| 570 | 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] | 571 | char *s; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 572 | 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] | 573 | ret = LY_EMEM, cleanup); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 574 | LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->ctx, s, &prefixed_name), cleanup); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 575 | u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 576 | break; |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | if (!prefixed_name) { |
| 581 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 582 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 583 | ret = LY_EVALID; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 584 | goto cleanup; |
| 585 | } |
| 586 | } else { |
| 587 | prefixed_name = ext_p->name; |
| 588 | } |
| 589 | lysc_update_path(ctx, NULL, prefixed_name); |
| 590 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 591 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 592 | 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] | 593 | if (!ext_mod) { |
| 594 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 595 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 596 | ret = LY_EVALID; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 597 | goto cleanup; |
| 598 | } else if (!ext_mod->parsed->extensions) { |
| 599 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 600 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 601 | prefixed_name, ext_mod->name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 602 | ret = LY_EVALID; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 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 | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 612 | LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def), cleanup); |
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); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 619 | ret = LY_EVALID; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 620 | goto cleanup; |
| 621 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 622 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 623 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 624 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 625 | * 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] | 626 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 627 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 628 | struct lysp_stmt *stmt = NULL; |
| 629 | |
| 630 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 631 | /* ... argument was the first XML child element */ |
| 632 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 633 | /* TODO check namespace of the statement */ |
| 634 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 635 | stmt = ext_p->child; |
| 636 | } |
| 637 | } |
| 638 | } else { |
| 639 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 640 | * with LYS_YIN_ATTR flag */ |
| 641 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 642 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 643 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 644 | break; |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | if (!stmt) { |
| 649 | /* missing extension's argument */ |
| 650 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 651 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 652 | ret = LY_EVALID; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 653 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 654 | |
| 655 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 656 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup); |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 657 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 658 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 659 | if (prefixed_name != ext_p->name) { |
| 660 | lydict_remove(ctx->ctx, ext_p->name); |
| 661 | ext_p->name = prefixed_name; |
| 662 | if (!ext_p->argument && ext->argument) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 663 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, ext->argument, 0, &ext_p->argument), cleanup); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 664 | } |
| 665 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 666 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 667 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 668 | if (ext->argument) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 669 | lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 670 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 671 | LY_CHECK_GOTO(ret = ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 672 | if (ext->argument) { |
| 673 | lysc_update_path(ctx, NULL, NULL); |
| 674 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 675 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 676 | ext_p->compiled = ext; |
| 677 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 678 | cleanup: |
| 679 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 680 | lydict_remove(ctx->ctx, prefixed_name); |
| 681 | } |
| 682 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 683 | lysc_update_path(ctx, NULL, NULL); |
| 684 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 685 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 686 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 690 | * @brief Compile information from the if-feature statement |
| 691 | * @param[in] ctx Compile context. |
| 692 | * @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] | 693 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 694 | * @return LY_ERR value. |
| 695 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 696 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 697 | 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] | 698 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 699 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 700 | const char *c = *value; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 701 | int64_t i, j; |
| 702 | int8_t op_len, last_not = 0, checkversion = 0; |
| 703 | LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 704 | uint8_t op; |
| 705 | struct iff_stack stack = {0, 0, NULL}; |
| 706 | struct lysc_feature *f; |
| 707 | |
| 708 | assert(c); |
| 709 | |
| 710 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 711 | for (i = j = 0; c[i]; i++) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 712 | if (c[i] == '(') { |
| 713 | j++; |
| 714 | checkversion = 1; |
| 715 | continue; |
| 716 | } else if (c[i] == ')') { |
| 717 | j--; |
| 718 | continue; |
| 719 | } else if (isspace(c[i])) { |
| 720 | checkversion = 1; |
| 721 | continue; |
| 722 | } |
| 723 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 724 | if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) { |
| 725 | uint64_t spaces; |
| 726 | for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++); |
| 727 | if (c[i + op_len + spaces] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 728 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 729 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 730 | return LY_EVALID; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 731 | } else if (!isspace(c[i + op_len])) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 732 | /* feature name starting with the not/and/or */ |
| 733 | last_not = 0; |
| 734 | f_size++; |
| 735 | } else if (c[i] == 'n') { /* not operation */ |
| 736 | if (last_not) { |
| 737 | /* double not */ |
| 738 | expr_size = expr_size - 2; |
| 739 | last_not = 0; |
| 740 | } else { |
| 741 | last_not = 1; |
| 742 | } |
| 743 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 744 | if (f_exp != f_size) { |
| 745 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 746 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, op_len, &c[i]); |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 747 | return LY_EVALID; |
| 748 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 749 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 750 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 751 | /* not a not operation */ |
| 752 | last_not = 0; |
| 753 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 754 | i += op_len; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 755 | } else { |
| 756 | f_size++; |
| 757 | last_not = 0; |
| 758 | } |
| 759 | expr_size++; |
| 760 | |
| 761 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 762 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 763 | i--; |
| 764 | break; |
| 765 | } |
| 766 | i++; |
| 767 | } |
| 768 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 769 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 770 | /* not matching count of ( and ) */ |
| 771 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 772 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 773 | return LY_EVALID; |
| 774 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 775 | if (f_exp != f_size) { |
| 776 | /* features do not match the needed arguments for the logical operations */ |
| 777 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 778 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 779 | "the required number of operands for the operations.", *value); |
| 780 | return LY_EVALID; |
| 781 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 782 | |
| 783 | if (checkversion || expr_size > 1) { |
| 784 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 785 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 786 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 787 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 788 | return LY_EVALID; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | /* allocate the memory */ |
| 793 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 794 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 795 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 796 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx); rc = LY_EMEM, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 797 | |
| 798 | stack.size = expr_size; |
| 799 | f_size--; expr_size--; /* used as indexes from now */ |
| 800 | |
| 801 | for (i--; i >= 0; i--) { |
| 802 | if (c[i] == ')') { |
| 803 | /* push it on stack */ |
| 804 | iff_stack_push(&stack, LYS_IFF_RP); |
| 805 | continue; |
| 806 | } else if (c[i] == '(') { |
| 807 | /* pop from the stack into result all operators until ) */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 808 | while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 809 | iff_setop(iff->expr, op, expr_size--); |
| 810 | } |
| 811 | continue; |
| 812 | } else if (isspace(c[i])) { |
| 813 | continue; |
| 814 | } |
| 815 | |
| 816 | /* end of operator or operand -> find beginning and get what is it */ |
| 817 | j = i + 1; |
| 818 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 819 | i--; |
| 820 | } |
| 821 | i++; /* go back by one step */ |
| 822 | |
| 823 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 824 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 825 | /* double not */ |
| 826 | iff_stack_pop(&stack); |
| 827 | } else { |
| 828 | /* not has the highest priority, so do not pop from the stack |
| 829 | * as in case of AND and OR */ |
| 830 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 831 | } |
| 832 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 833 | /* as for OR - pop from the stack all operators with the same or higher |
| 834 | * priority and store them to the result, then push the AND to the stack */ |
| 835 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 836 | op = iff_stack_pop(&stack); |
| 837 | iff_setop(iff->expr, op, expr_size--); |
| 838 | } |
| 839 | iff_stack_push(&stack, LYS_IFF_AND); |
| 840 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 841 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 842 | op = iff_stack_pop(&stack); |
| 843 | iff_setop(iff->expr, op, expr_size--); |
| 844 | } |
| 845 | iff_stack_push(&stack, LYS_IFF_OR); |
| 846 | } else { |
| 847 | /* feature name, length is j - i */ |
| 848 | |
| 849 | /* add it to the expression */ |
| 850 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 851 | |
| 852 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 853 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 854 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 855 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 856 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 857 | iff->features[f_size] = f; |
| 858 | LY_ARRAY_INCREMENT(iff->features); |
| 859 | f_size--; |
| 860 | } |
| 861 | } |
| 862 | while (stack.index) { |
| 863 | op = iff_stack_pop(&stack); |
| 864 | iff_setop(iff->expr, op, expr_size--); |
| 865 | } |
| 866 | |
| 867 | if (++expr_size || ++f_size) { |
| 868 | /* not all expected operators and operands found */ |
| 869 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 870 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 871 | rc = LY_EINT; |
| 872 | } else { |
| 873 | rc = LY_SUCCESS; |
| 874 | } |
| 875 | |
| 876 | error: |
| 877 | /* cleanup */ |
| 878 | iff_stack_clean(&stack); |
| 879 | |
| 880 | return rc; |
| 881 | } |
| 882 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 883 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 884 | * @brief Get the XPath context node for the given schema node. |
| 885 | * @param[in] start The schema node where the XPath expression appears. |
| 886 | * @return The context node to evaluate XPath expression in given schema node. |
| 887 | * @return NULL in case the context node is the root node. |
| 888 | */ |
| 889 | static struct lysc_node * |
| 890 | lysc_xpath_context(struct lysc_node *start) |
| 891 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 892 | 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] | 893 | start = start->parent) {} |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 894 | return start; |
| 895 | } |
| 896 | |
| 897 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 898 | * @brief Compile information from the when statement |
| 899 | * @param[in] ctx Compile context. |
| 900 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 901 | * @param[in] flags Flags of the node with the "when" defiition. |
| 902 | * @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] | 903 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 904 | * @return LY_ERR value. |
| 905 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 906 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 907 | 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] | 908 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 909 | LY_ERR ret = LY_SUCCESS; |
| 910 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 911 | *when = calloc(1, sizeof **when); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame^] | 912 | LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 913 | (*when)->refcount = 1; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame^] | 914 | LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond)); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 915 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 916 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 917 | DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done); |
| 918 | DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 919 | 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] | 920 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 921 | |
| 922 | done: |
| 923 | return ret; |
| 924 | } |
| 925 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 926 | /** |
| 927 | * @brief Compile information from the must statement |
| 928 | * @param[in] ctx Compile context. |
| 929 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 930 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 931 | * @return LY_ERR value. |
| 932 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 933 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 934 | 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] | 935 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 936 | LY_ERR ret = LY_SUCCESS; |
| 937 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame^] | 938 | LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1, &must->cond)); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 939 | must->module = ctx->mod_def; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 940 | DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done); |
| 941 | DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done); |
| 942 | DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done); |
| 943 | DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 944 | 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] | 945 | |
| 946 | done: |
| 947 | return ret; |
| 948 | } |
| 949 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 950 | /** |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 951 | * @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] | 952 | * @param[in] ctx Compile context. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 953 | * @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] | 954 | * @return LY_ERR value. |
| 955 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 956 | static LY_ERR |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 957 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 958 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 959 | const struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 960 | LY_ERR ret = LY_SUCCESS; |
| 961 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 962 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 963 | * 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] | 964 | * 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] | 965 | if (!imp_p->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 966 | /* try to use filepath if present */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 967 | if (imp_p->module->filepath) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 968 | struct ly_in *in; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 969 | if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 970 | LOGINT(ctx->ctx); |
| 971 | } else { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 972 | 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] | 973 | ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 974 | if (mod != imp_p->module) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 975 | 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] | 976 | imp_p->module->filepath, imp_p->module->name); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 977 | mod = NULL; |
| 978 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 979 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 980 | ly_in_free(in, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 981 | } |
| 982 | if (!mod) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 983 | 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] | 984 | 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] | 985 | imp_p->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 986 | return LY_ENOTFOUND; |
| 987 | } |
| 988 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 989 | } |
| 990 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 991 | return ret; |
| 992 | } |
| 993 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 994 | LY_ERR |
| 995 | 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] | 996 | struct lysp_ident *identities_p, struct lysc_ident **identities) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 997 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 998 | LY_ARRAY_COUNT_TYPE offset = 0, u, v; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 999 | struct lysc_ctx context = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1000 | LY_ERR ret = LY_SUCCESS; |
| 1001 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1002 | assert(ctx_sc || ctx); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1003 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1004 | if (!ctx_sc) { |
| 1005 | context.ctx = ctx; |
| 1006 | context.mod = module; |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 1007 | context.mod_def = module; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1008 | context.path_len = 1; |
| 1009 | context.path[0] = '/'; |
| 1010 | ctx_sc = &context; |
| 1011 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1012 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1013 | if (!identities_p) { |
| 1014 | return LY_SUCCESS; |
| 1015 | } |
| 1016 | if (*identities) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1017 | offset = LY_ARRAY_COUNT(*identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | lysc_update_path(ctx_sc, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1021 | 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] | 1022 | LY_ARRAY_FOR(identities_p, u) { |
| 1023 | lysc_update_path(ctx_sc, NULL, identities_p[u].name); |
| 1024 | |
| 1025 | LY_ARRAY_INCREMENT(*identities); |
| 1026 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1027 | DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done); |
| 1028 | DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done); |
| 1029 | DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref, ret, done); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1030 | (*identities)[offset + u].module = ctx_sc->mod; |
| 1031 | COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v, |
| 1032 | lys_compile_iffeature, ret, done); |
| 1033 | /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */ |
| 1034 | COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u], |
| 1035 | LYEXT_PAR_IDENT, ret, done); |
| 1036 | (*identities)[offset + u].flags = identities_p[u].flags; |
| 1037 | |
| 1038 | lysc_update_path(ctx_sc, NULL, NULL); |
| 1039 | } |
| 1040 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1041 | done: |
| 1042 | return ret; |
| 1043 | } |
| 1044 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1045 | /** |
| 1046 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 1047 | * |
| 1048 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 1049 | * |
| 1050 | * @param[in] ctx Compile context for logging. |
| 1051 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 1052 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 1053 | * @return LY_SUCCESS if everything is ok. |
| 1054 | * @return LY_EVALID if the identity is derived from itself. |
| 1055 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1056 | static LY_ERR |
| 1057 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 1058 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1059 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1060 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1061 | struct ly_set recursion = {0}; |
| 1062 | struct lysc_ident *drv; |
| 1063 | |
| 1064 | if (!derived) { |
| 1065 | return LY_SUCCESS; |
| 1066 | } |
| 1067 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1068 | for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1069 | if (ident == derived[u]) { |
| 1070 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1071 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1072 | ret = LY_EVALID; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1073 | goto cleanup; |
| 1074 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1075 | ret = ly_set_add(&recursion, derived[u], 0, NULL); |
| 1076 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | for (v = 0; v < recursion.count; ++v) { |
| 1080 | drv = recursion.objs[v]; |
| 1081 | if (!drv->derived) { |
| 1082 | continue; |
| 1083 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1084 | for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1085 | if (ident == drv->derived[u]) { |
| 1086 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1087 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1088 | ret = LY_EVALID; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1089 | goto cleanup; |
| 1090 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1091 | ret = ly_set_add(&recursion, drv->derived[u], 0, NULL); |
| 1092 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1093 | } |
| 1094 | } |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1095 | |
| 1096 | cleanup: |
| 1097 | ly_set_erase(&recursion, NULL); |
| 1098 | return ret; |
| 1099 | } |
| 1100 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1101 | /** |
| 1102 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1103 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1104 | * 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] | 1105 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1106 | * to distinguish these two use cases. |
| 1107 | * |
| 1108 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1109 | * @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] | 1110 | * @param[in] ident Referencing identity to work with, NULL for identityref. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1111 | * @param[in] bases Array of bases of identityref to fill in. |
| 1112 | * @return LY_ERR value. |
| 1113 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1114 | static LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1115 | 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] | 1116 | struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1117 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1118 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1119 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1120 | struct lys_module *mod; |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1121 | struct lysc_ident **idref; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1122 | |
| 1123 | assert(ident || bases); |
| 1124 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1125 | if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1126 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1127 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1128 | return LY_EVALID; |
| 1129 | } |
| 1130 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1131 | LY_ARRAY_FOR(bases_p, u) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1132 | s = strchr(bases_p[u], ':'); |
| 1133 | if (s) { |
| 1134 | /* prefixed identity */ |
| 1135 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1136 | 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] | 1137 | } else { |
| 1138 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1139 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1140 | } |
| 1141 | if (!mod) { |
| 1142 | if (ident) { |
| 1143 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1144 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1145 | } else { |
| 1146 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1147 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1148 | } |
| 1149 | return LY_EVALID; |
| 1150 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1151 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1152 | idref = NULL; |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1153 | LY_ARRAY_FOR(mod->identities, v) { |
| 1154 | if (!strcmp(name, mod->identities[v].name)) { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1155 | if (ident) { |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1156 | if (ident == &mod->identities[v]) { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1157 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1158 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1159 | return LY_EVALID; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1160 | } |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1161 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived)); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1162 | /* we have match! store the backlink */ |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1163 | LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1164 | *idref = ident; |
| 1165 | } else { |
| 1166 | /* we have match! store the found identity */ |
| 1167 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1168 | *idref = &mod->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1169 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1170 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | if (!idref || !(*idref)) { |
| 1174 | if (ident) { |
| 1175 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1176 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1177 | } else { |
| 1178 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1179 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1180 | } |
| 1181 | return LY_EVALID; |
| 1182 | } |
| 1183 | } |
| 1184 | return LY_SUCCESS; |
| 1185 | } |
| 1186 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1187 | /** |
| 1188 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1189 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1190 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1191 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1192 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1193 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1194 | static LY_ERR |
| 1195 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1196 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1197 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1198 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1199 | lysc_update_path(ctx, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1200 | for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1201 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1202 | continue; |
| 1203 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1204 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1205 | 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] | 1206 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1207 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1208 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1209 | return LY_SUCCESS; |
| 1210 | } |
| 1211 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1212 | LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1213 | 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] | 1214 | struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1215 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1216 | LY_ERR ret = LY_SUCCESS; |
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 | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1244 | DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done); |
| 1245 | DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done); |
| 1246 | DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref, ret, done); |
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 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1254 | done: |
| 1255 | return ret; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1256 | } |
| 1257 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1258 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1259 | * @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] | 1260 | * |
| 1261 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1262 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1263 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1264 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1265 | * being currently processed). |
| 1266 | * @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] | 1267 | * @return LY_SUCCESS if everything is ok. |
| 1268 | * @return LY_EVALID if the feature references indirectly itself. |
| 1269 | */ |
| 1270 | static LY_ERR |
| 1271 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1272 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1273 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1274 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1275 | struct ly_set recursion = {0}; |
| 1276 | struct lysc_feature *drv; |
| 1277 | |
| 1278 | if (!depfeatures) { |
| 1279 | return LY_SUCCESS; |
| 1280 | } |
| 1281 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1282 | for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1283 | if (feature == depfeatures[u]) { |
| 1284 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1285 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1286 | ret = LY_EVALID; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1287 | goto cleanup; |
| 1288 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1289 | ret = ly_set_add(&recursion, depfeatures[u], 0, NULL); |
| 1290 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | for (v = 0; v < recursion.count; ++v) { |
| 1294 | drv = recursion.objs[v]; |
| 1295 | if (!drv->depfeatures) { |
| 1296 | continue; |
| 1297 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1298 | for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1299 | if (feature == drv->depfeatures[u]) { |
| 1300 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1301 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1302 | ret = LY_EVALID; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1303 | goto cleanup; |
| 1304 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1305 | ly_set_add(&recursion, drv->depfeatures[u], 0, NULL); |
| 1306 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1307 | } |
| 1308 | } |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1309 | |
| 1310 | cleanup: |
| 1311 | ly_set_erase(&recursion, NULL); |
| 1312 | return ret; |
| 1313 | } |
| 1314 | |
| 1315 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1316 | * @brief Create pre-compiled features array. |
| 1317 | * |
| 1318 | * See lys_feature_precompile() for more details. |
| 1319 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1320 | * @param[in] ctx Compile context. |
| 1321 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1322 | * @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] | 1323 | * @return LY_ERR value. |
| 1324 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1325 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1326 | 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] | 1327 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1328 | LY_ARRAY_COUNT_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1329 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1330 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1331 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1332 | /* find the preprecompiled feature */ |
| 1333 | LY_ARRAY_FOR(features, x) { |
| 1334 | if (strcmp(features[x].name, feature_p->name)) { |
| 1335 | continue; |
| 1336 | } |
| 1337 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1338 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1339 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1340 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1341 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1342 | 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] | 1343 | 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] | 1344 | if (feature->iffeatures) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1345 | for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1346 | if (feature->iffeatures[u].features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1347 | for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1348 | /* check for circular dependency - direct reference first,... */ |
| 1349 | if (feature == feature->iffeatures[u].features[v]) { |
| 1350 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1351 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1352 | return LY_EVALID; |
| 1353 | } |
| 1354 | /* ... and indirect circular reference */ |
| 1355 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1356 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1357 | /* add itself into the dependants list */ |
| 1358 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1359 | *df = feature; |
| 1360 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1361 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1362 | } |
| 1363 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1364 | lysc_update_path(ctx, NULL, NULL); |
| 1365 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1366 | done: |
| 1367 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1368 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1369 | |
| 1370 | LOGINT(ctx->ctx); |
| 1371 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1372 | } |
| 1373 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1374 | /** |
| 1375 | * @brief Revert compiled list of features back to the precompiled state. |
| 1376 | * |
| 1377 | * 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] | 1378 | * 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] | 1379 | * |
| 1380 | * @param[in] ctx Compilation context. |
| 1381 | * @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] | 1382 | * and supposed to hold the dis_features list. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1383 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1384 | static void |
| 1385 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1386 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1387 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1388 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1389 | /* 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] | 1390 | * which may points into the data being freed here */ |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1391 | LY_ARRAY_FOR(mod->features, u) { |
| 1392 | LY_ARRAY_FOR(mod->features[u].iffeatures, v) { |
| 1393 | lysc_iffeature_free(ctx->ctx, &mod->features[u].iffeatures[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1394 | } |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1395 | LY_ARRAY_FREE(mod->features[u].iffeatures); |
| 1396 | mod->features[u].iffeatures = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1397 | |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1398 | LY_ARRAY_FOR(mod->features[u].exts, v) { |
| 1399 | lysc_ext_instance_free(ctx->ctx, &(mod->features[u].exts)[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1400 | } |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1401 | LY_ARRAY_FREE(mod->features[u].exts); |
| 1402 | mod->features[u].exts = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1403 | } |
| 1404 | } |
| 1405 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1406 | /** |
| 1407 | * @brief Validate and normalize numeric value from a range definition. |
| 1408 | * @param[in] ctx Compile context. |
| 1409 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1410 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1411 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1412 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1413 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1414 | * @param[in] value String value of the range boundary. |
| 1415 | * @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. |
| 1416 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1417 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1418 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1419 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1420 | 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] | 1421 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1422 | size_t fraction = 0, size; |
| 1423 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1424 | *len = 0; |
| 1425 | |
| 1426 | assert(value); |
| 1427 | /* parse value */ |
| 1428 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1429 | return LY_EVALID; |
| 1430 | } |
| 1431 | |
| 1432 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1433 | ++(*len); |
| 1434 | } |
| 1435 | |
| 1436 | while (isdigit(value[*len])) { |
| 1437 | ++(*len); |
| 1438 | } |
| 1439 | |
| 1440 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1441 | if (basetype == LY_TYPE_DEC64) { |
| 1442 | goto decimal; |
| 1443 | } else { |
| 1444 | *valcopy = strndup(value, *len); |
| 1445 | return LY_SUCCESS; |
| 1446 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1447 | } |
| 1448 | fraction = *len; |
| 1449 | |
| 1450 | ++(*len); |
| 1451 | while (isdigit(value[*len])) { |
| 1452 | ++(*len); |
| 1453 | } |
| 1454 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1455 | if (basetype == LY_TYPE_DEC64) { |
| 1456 | decimal: |
| 1457 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1458 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1459 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1460 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1461 | *len, value, frdigits); |
| 1462 | return LY_EINVAL; |
| 1463 | } |
| 1464 | if (fraction) { |
| 1465 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1466 | } else { |
| 1467 | size = (*len) + frdigits + 1; |
| 1468 | } |
| 1469 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1470 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1471 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1472 | (*valcopy)[size - 1] = '\0'; |
| 1473 | if (fraction) { |
| 1474 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1475 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1476 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1477 | } else { |
| 1478 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1479 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1480 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1481 | } |
| 1482 | return LY_SUCCESS; |
| 1483 | } |
| 1484 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1485 | /** |
| 1486 | * @brief Check that values in range are in ascendant order. |
| 1487 | * @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] | 1488 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1489 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1490 | * @param[in] value Current value to check. |
| 1491 | * @param[in] prev_value The last seen value. |
| 1492 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1493 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1494 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1495 | range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1496 | { |
| 1497 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1498 | 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] | 1499 | return LY_EEXIST; |
| 1500 | } |
| 1501 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1502 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1503 | return LY_EEXIST; |
| 1504 | } |
| 1505 | } |
| 1506 | return LY_SUCCESS; |
| 1507 | } |
| 1508 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1509 | /** |
| 1510 | * @brief Set min/max value of the range part. |
| 1511 | * @param[in] ctx Compile context. |
| 1512 | * @param[in] part Range part structure to fill. |
| 1513 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1514 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1515 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1516 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1517 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1518 | * @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] | 1519 | * @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] | 1520 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1521 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1522 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1523 | * frdigits value), LY_EMEM. |
| 1524 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1525 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1526 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype, |
| 1527 | ly_bool first, ly_bool 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] | 1528 | { |
| 1529 | LY_ERR ret = LY_SUCCESS; |
| 1530 | char *valcopy = NULL; |
| 1531 | size_t len; |
| 1532 | |
| 1533 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1534 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1535 | LY_CHECK_GOTO(ret, finalize); |
| 1536 | } |
| 1537 | if (!valcopy && base_range) { |
| 1538 | if (max) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1539 | 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] | 1540 | } else { |
| 1541 | part->min_64 = base_range->parts[0].min_64; |
| 1542 | } |
| 1543 | if (!first) { |
| 1544 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1545 | } |
| 1546 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1550 | case LY_TYPE_INT8: /* range */ |
| 1551 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1552 | 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] | 1553 | } else if (max) { |
| 1554 | part->max_64 = INT64_C(127); |
| 1555 | } else { |
| 1556 | part->min_64 = INT64_C(-128); |
| 1557 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1558 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1559 | 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] | 1560 | } |
| 1561 | break; |
| 1562 | case LY_TYPE_INT16: /* range */ |
| 1563 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1564 | 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] | 1565 | } else if (max) { |
| 1566 | part->max_64 = INT64_C(32767); |
| 1567 | } else { |
| 1568 | part->min_64 = INT64_C(-32768); |
| 1569 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1570 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1571 | 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] | 1572 | } |
| 1573 | break; |
| 1574 | case LY_TYPE_INT32: /* range */ |
| 1575 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1576 | 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] | 1577 | } else if (max) { |
| 1578 | part->max_64 = INT64_C(2147483647); |
| 1579 | } else { |
| 1580 | part->min_64 = INT64_C(-2147483648); |
| 1581 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1582 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1583 | 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] | 1584 | } |
| 1585 | break; |
| 1586 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1587 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1588 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1589 | 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] | 1590 | max ? &part->max_64 : &part->min_64); |
| 1591 | } else if (max) { |
| 1592 | part->max_64 = INT64_C(9223372036854775807); |
| 1593 | } else { |
| 1594 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1595 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1596 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1597 | 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] | 1598 | } |
| 1599 | break; |
| 1600 | case LY_TYPE_UINT8: /* range */ |
| 1601 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1602 | 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] | 1603 | } else if (max) { |
| 1604 | part->max_u64 = UINT64_C(255); |
| 1605 | } else { |
| 1606 | part->min_u64 = UINT64_C(0); |
| 1607 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1608 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1609 | 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] | 1610 | } |
| 1611 | break; |
| 1612 | case LY_TYPE_UINT16: /* range */ |
| 1613 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1614 | 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] | 1615 | } else if (max) { |
| 1616 | part->max_u64 = UINT64_C(65535); |
| 1617 | } else { |
| 1618 | part->min_u64 = UINT64_C(0); |
| 1619 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1620 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1621 | 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] | 1622 | } |
| 1623 | break; |
| 1624 | case LY_TYPE_UINT32: /* range */ |
| 1625 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1626 | 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] | 1627 | } else if (max) { |
| 1628 | part->max_u64 = UINT64_C(4294967295); |
| 1629 | } else { |
| 1630 | part->min_u64 = UINT64_C(0); |
| 1631 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1632 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1633 | 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] | 1634 | } |
| 1635 | break; |
| 1636 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1637 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1638 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1639 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1640 | 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] | 1641 | } else if (max) { |
| 1642 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1643 | } else { |
| 1644 | part->min_u64 = UINT64_C(0); |
| 1645 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1646 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1647 | 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] | 1648 | } |
| 1649 | break; |
| 1650 | default: |
| 1651 | LOGINT(ctx->ctx); |
| 1652 | ret = LY_EINT; |
| 1653 | } |
| 1654 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1655 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1656 | if (ret == LY_EDENIED) { |
| 1657 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1658 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1659 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1660 | } else if (ret == LY_EVALID) { |
| 1661 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1662 | "Invalid %s restriction - invalid value \"%s\".", |
| 1663 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1664 | } else if (ret == LY_EEXIST) { |
| 1665 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1666 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1667 | length_restr ? "length" : "range", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1668 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1669 | } else if (!ret && value) { |
| 1670 | *value = *value + len; |
| 1671 | } |
| 1672 | free(valcopy); |
| 1673 | return ret; |
| 1674 | } |
| 1675 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1676 | /** |
| 1677 | * @brief Compile the parsed range restriction. |
| 1678 | * @param[in] ctx Compile context. |
| 1679 | * @param[in] range_p Parsed range structure to compile. |
| 1680 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1681 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1682 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1683 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1684 | * range restriction must be more restrictive than the base_range. |
| 1685 | * @param[in,out] range Pointer to the created current range structure. |
| 1686 | * @return LY_ERR value. |
| 1687 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1688 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1689 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1690 | uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1691 | { |
| 1692 | LY_ERR ret = LY_EVALID; |
| 1693 | const char *expr; |
| 1694 | struct lysc_range_part *parts = NULL, *part; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1695 | ly_bool range_expected = 0, uns; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1696 | LY_ARRAY_COUNT_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1697 | |
| 1698 | assert(range); |
| 1699 | assert(range_p); |
| 1700 | |
| 1701 | expr = range_p->arg; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1702 | while (1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1703 | if (isspace(*expr)) { |
| 1704 | ++expr; |
| 1705 | } else if (*expr == '\0') { |
| 1706 | if (range_expected) { |
| 1707 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1708 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1709 | length_restr ? "length" : "range", range_p->arg); |
| 1710 | goto cleanup; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1711 | } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1712 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1713 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1714 | length_restr ? "length" : "range", range_p->arg); |
| 1715 | goto cleanup; |
| 1716 | } |
| 1717 | parts_done++; |
| 1718 | break; |
| 1719 | } else if (!strncmp(expr, "min", 3)) { |
| 1720 | if (parts) { |
| 1721 | /* min cannot be used elsewhere than in the first part */ |
| 1722 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1723 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1724 | expr - range_p->arg, range_p->arg); |
| 1725 | goto cleanup; |
| 1726 | } |
| 1727 | expr += 3; |
| 1728 | |
| 1729 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1730 | 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] | 1731 | part->max_64 = part->min_64; |
| 1732 | } else if (*expr == '|') { |
| 1733 | if (!parts || range_expected) { |
| 1734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1735 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1736 | goto cleanup; |
| 1737 | } |
| 1738 | expr++; |
| 1739 | parts_done++; |
| 1740 | /* process next part of the expression */ |
| 1741 | } else if (!strncmp(expr, "..", 2)) { |
| 1742 | expr += 2; |
| 1743 | while (isspace(*expr)) { |
| 1744 | expr++; |
| 1745 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1746 | if (!parts || LY_ARRAY_COUNT(parts) == parts_done) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1747 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1748 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1749 | goto cleanup; |
| 1750 | } |
| 1751 | /* continue expecting the upper boundary */ |
| 1752 | range_expected = 1; |
| 1753 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1754 | /* number */ |
| 1755 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1756 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1757 | 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] | 1758 | range_expected = 0; |
| 1759 | } else { |
| 1760 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1761 | 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] | 1762 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1763 | part->max_64 = part->min_64; |
| 1764 | } |
| 1765 | |
| 1766 | /* continue with possible another expression part */ |
| 1767 | } else if (!strncmp(expr, "max", 3)) { |
| 1768 | expr += 3; |
| 1769 | while (isspace(*expr)) { |
| 1770 | expr++; |
| 1771 | } |
| 1772 | if (*expr != '\0') { |
| 1773 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1774 | length_restr ? "length" : "range", expr); |
| 1775 | goto cleanup; |
| 1776 | } |
| 1777 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1778 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1779 | 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] | 1780 | range_expected = 0; |
| 1781 | } else { |
| 1782 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1783 | 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] | 1784 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1785 | part->min_64 = part->max_64; |
| 1786 | } |
| 1787 | } else { |
| 1788 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1789 | length_restr ? "length" : "range", expr); |
| 1790 | goto cleanup; |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | /* check with the previous range/length restriction */ |
| 1795 | if (base_range) { |
| 1796 | switch (basetype) { |
| 1797 | case LY_TYPE_BINARY: |
| 1798 | case LY_TYPE_UINT8: |
| 1799 | case LY_TYPE_UINT16: |
| 1800 | case LY_TYPE_UINT32: |
| 1801 | case LY_TYPE_UINT64: |
| 1802 | case LY_TYPE_STRING: |
| 1803 | uns = 1; |
| 1804 | break; |
| 1805 | case LY_TYPE_DEC64: |
| 1806 | case LY_TYPE_INT8: |
| 1807 | case LY_TYPE_INT16: |
| 1808 | case LY_TYPE_INT32: |
| 1809 | case LY_TYPE_INT64: |
| 1810 | uns = 0; |
| 1811 | break; |
| 1812 | default: |
| 1813 | LOGINT(ctx->ctx); |
| 1814 | ret = LY_EINT; |
| 1815 | goto cleanup; |
| 1816 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1817 | 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] | 1818 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1819 | goto baseerror; |
| 1820 | } |
| 1821 | /* current lower bound is not lower than the base */ |
| 1822 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1823 | /* base has single value */ |
| 1824 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1825 | /* both lower bounds are the same */ |
| 1826 | if (parts[u].min_64 != parts[u].max_64) { |
| 1827 | /* current continues with a range */ |
| 1828 | goto baseerror; |
| 1829 | } else { |
| 1830 | /* equal single values, move both forward */ |
| 1831 | ++v; |
| 1832 | continue; |
| 1833 | } |
| 1834 | } else { |
| 1835 | /* base is single value lower than current range, so the |
| 1836 | * value from base range is removed in the current, |
| 1837 | * move only base and repeat checking */ |
| 1838 | ++v; |
| 1839 | --u; |
| 1840 | continue; |
| 1841 | } |
| 1842 | } else { |
| 1843 | /* base is the range */ |
| 1844 | if (parts[u].min_64 == parts[u].max_64) { |
| 1845 | /* current is a single value */ |
| 1846 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1847 | /* current is behind the base range, so base range is omitted, |
| 1848 | * move the base and keep the current for further check */ |
| 1849 | ++v; |
| 1850 | --u; |
| 1851 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1852 | continue; |
| 1853 | } else { |
| 1854 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1855 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1856 | /* higher bound is higher than the current higher bound */ |
| 1857 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1858 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1859 | * continue with the same current, but move the base */ |
| 1860 | --u; |
| 1861 | ++v; |
| 1862 | continue; |
| 1863 | } |
| 1864 | /* current range starts within the base range but end behind it */ |
| 1865 | goto baseerror; |
| 1866 | } else { |
| 1867 | /* current range is smaller than the base, |
| 1868 | * move current, but stay with the base */ |
| 1869 | continue; |
| 1870 | } |
| 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | if (u != parts_done) { |
| 1875 | baseerror: |
| 1876 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1877 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1878 | length_restr ? "length" : "range", range_p->arg); |
| 1879 | goto cleanup; |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | if (!(*range)) { |
| 1884 | *range = calloc(1, sizeof **range); |
| 1885 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1886 | } |
| 1887 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1888 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1889 | if (range_p->eapptag) { |
| 1890 | lydict_remove(ctx->ctx, (*range)->eapptag); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1891 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1892 | } |
| 1893 | if (range_p->emsg) { |
| 1894 | lydict_remove(ctx->ctx, (*range)->emsg); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1895 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1896 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1897 | if (range_p->dsc) { |
| 1898 | lydict_remove(ctx->ctx, (*range)->dsc); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1899 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1900 | } |
| 1901 | if (range_p->ref) { |
| 1902 | lydict_remove(ctx->ctx, (*range)->ref); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1903 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1904 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1905 | /* extensions are taken only from the last range by the caller */ |
| 1906 | |
| 1907 | (*range)->parts = parts; |
| 1908 | parts = NULL; |
| 1909 | ret = LY_SUCCESS; |
| 1910 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1911 | LY_ARRAY_FREE(parts); |
| 1912 | |
| 1913 | return ret; |
| 1914 | } |
| 1915 | |
| 1916 | /** |
| 1917 | * @brief Checks pattern syntax. |
| 1918 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1919 | * @param[in] ctx Context. |
| 1920 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1921 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1922 | * @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] | 1923 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1924 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1925 | LY_ERR |
| 1926 | 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] | 1927 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1928 | size_t idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1929 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1930 | int err_code; |
| 1931 | const char *orig_ptr; |
| 1932 | PCRE2_SIZE err_offset; |
| 1933 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1934 | #define URANGE_LEN 19 |
| 1935 | char *ublock2urange[][2] = { |
| 1936 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1937 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1938 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1939 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1940 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1941 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1942 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1943 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1944 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1945 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1946 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1947 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1948 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1949 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1950 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1951 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1952 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1953 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1954 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1955 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1956 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1957 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1958 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1959 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1960 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1961 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1962 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1963 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1964 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1965 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1966 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1967 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1968 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1969 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1970 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1971 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1972 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1973 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1974 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1975 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1976 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1977 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1978 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1979 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1980 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1981 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1982 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1983 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1984 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1985 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1986 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1987 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1988 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1989 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1990 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1991 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1992 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1993 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1994 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1995 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1996 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1997 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1998 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1999 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 2000 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 2001 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 2002 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 2003 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 2004 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 2005 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 2006 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 2007 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 2008 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 2009 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 2010 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 2011 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 2012 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 2013 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 2014 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 2015 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 2016 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 2017 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 2018 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 2019 | {NULL, NULL} |
| 2020 | }; |
| 2021 | |
| 2022 | /* adjust the expression to a Perl equivalent |
| 2023 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 2024 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2025 | /* allocate space for the transformed pattern */ |
| 2026 | size = strlen(pattern) + 1; |
| 2027 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2028 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2029 | perl_regex[0] = '\0'; |
| 2030 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2031 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 2032 | brack = 0; |
| 2033 | idx = 0; |
| 2034 | orig_ptr = pattern; |
| 2035 | while (orig_ptr[0]) { |
| 2036 | switch (orig_ptr[0]) { |
| 2037 | case '$': |
| 2038 | case '^': |
| 2039 | if (!brack) { |
| 2040 | /* make space for the extra character */ |
| 2041 | ++size; |
| 2042 | perl_regex = ly_realloc(perl_regex, size); |
| 2043 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2044 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2045 | /* print escape slash */ |
| 2046 | perl_regex[idx] = '\\'; |
| 2047 | ++idx; |
| 2048 | } |
| 2049 | break; |
| 2050 | case '[': |
| 2051 | /* must not be escaped */ |
| 2052 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2053 | ++brack; |
| 2054 | } |
| 2055 | break; |
| 2056 | case ']': |
| 2057 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2058 | /* pattern was checked and compiled already */ |
| 2059 | assert(brack); |
| 2060 | --brack; |
| 2061 | } |
| 2062 | break; |
| 2063 | default: |
| 2064 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2065 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2066 | |
| 2067 | /* copy char */ |
| 2068 | perl_regex[idx] = orig_ptr[0]; |
| 2069 | |
| 2070 | ++idx; |
| 2071 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2072 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2073 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2074 | |
| 2075 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2076 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2077 | start = ptr - perl_regex; |
| 2078 | |
| 2079 | ptr = strchr(ptr, '}'); |
| 2080 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2081 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2082 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 2083 | free(perl_regex); |
| 2084 | return LY_EVALID; |
| 2085 | } |
| 2086 | end = (ptr - perl_regex) + 1; |
| 2087 | |
| 2088 | /* need more space */ |
| 2089 | if (end - start < URANGE_LEN) { |
| 2090 | 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] | 2091 | 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] | 2092 | } |
| 2093 | |
| 2094 | /* find our range */ |
| 2095 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2096 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2097 | break; |
| 2098 | } |
| 2099 | } |
| 2100 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2101 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2102 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2103 | free(perl_regex); |
| 2104 | return LY_EVALID; |
| 2105 | } |
| 2106 | |
| 2107 | /* 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] | 2108 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2109 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2110 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2111 | } |
| 2112 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2113 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2114 | } |
| 2115 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2116 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2117 | /* skip brackets */ |
| 2118 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2119 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2120 | } else { |
| 2121 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2122 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2127 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2128 | 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] | 2129 | &err_code, &err_offset, NULL); |
| 2130 | if (!code_local) { |
| 2131 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2132 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2133 | 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] | 2134 | free(perl_regex); |
| 2135 | return LY_EVALID; |
| 2136 | } |
| 2137 | free(perl_regex); |
| 2138 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2139 | if (code) { |
| 2140 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2141 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2142 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | return LY_SUCCESS; |
| 2146 | |
| 2147 | #undef URANGE_LEN |
| 2148 | } |
| 2149 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2150 | /** |
| 2151 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2152 | * @param[in] ctx Compile context. |
| 2153 | * @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] | 2154 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2155 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2156 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2157 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2158 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2159 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2160 | 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] | 2161 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2162 | { |
| 2163 | struct lysc_pattern **pattern; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2164 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2165 | LY_ERR ret = LY_SUCCESS; |
| 2166 | |
| 2167 | /* first, copy the patterns from the base type */ |
| 2168 | if (base_patterns) { |
| 2169 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2170 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2171 | } |
| 2172 | |
| 2173 | LY_ARRAY_FOR(patterns_p, u) { |
| 2174 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2175 | *pattern = calloc(1, sizeof **pattern); |
| 2176 | ++(*pattern)->refcount; |
| 2177 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2178 | 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] | 2179 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2180 | |
| 2181 | if (patterns_p[u].arg[0] == 0x15) { |
| 2182 | (*pattern)->inverted = 1; |
| 2183 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2184 | DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr, ret, done); |
| 2185 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done); |
| 2186 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done); |
| 2187 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done); |
| 2188 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2189 | 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] | 2190 | } |
| 2191 | done: |
| 2192 | return ret; |
| 2193 | } |
| 2194 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2195 | /** |
| 2196 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2197 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2198 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2199 | 0 /* LY_TYPE_UNKNOWN */, |
| 2200 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2201 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2202 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2203 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2204 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2205 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2206 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2207 | 0 /* LY_TYPE_BOOL */, |
| 2208 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2209 | 0 /* LY_TYPE_EMPTY */, |
| 2210 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2211 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2212 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2213 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2214 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2215 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2216 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2217 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2218 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2219 | }; |
| 2220 | |
| 2221 | /** |
| 2222 | * @brief stringification of the YANG built-in data types |
| 2223 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2224 | 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] | 2225 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2226 | "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] | 2227 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2228 | /** |
| 2229 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2230 | * @param[in] ctx Compile context. |
| 2231 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2232 | * @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] | 2233 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2234 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2235 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2236 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2237 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2238 | 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] | 2239 | 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] | 2240 | { |
| 2241 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2242 | LY_ARRAY_COUNT_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2243 | int32_t value = 0; |
| 2244 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2245 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2246 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2247 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2248 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2249 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2250 | return LY_EVALID; |
| 2251 | } |
| 2252 | |
| 2253 | LY_ARRAY_FOR(enums_p, u) { |
| 2254 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2255 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done); |
| 2256 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->dsc, ret, done); |
| 2257 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2258 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2259 | if (base_enums) { |
| 2260 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2261 | LY_ARRAY_FOR(base_enums, v) { |
| 2262 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2263 | break; |
| 2264 | } |
| 2265 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2266 | if (v == LY_ARRAY_COUNT(base_enums)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2267 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2268 | "Invalid %s - derived type adds new item \"%s\".", |
| 2269 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2270 | return LY_EVALID; |
| 2271 | } |
| 2272 | match = v; |
| 2273 | } |
| 2274 | |
| 2275 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2276 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2277 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2278 | e->value = (int32_t)enums_p[u].value; |
| 2279 | if (!u || e->value >= value) { |
| 2280 | value = e->value + 1; |
| 2281 | } |
| 2282 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2283 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2284 | if (e->value == (*enums)[v].value) { |
| 2285 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2286 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2287 | e->value, e->name, (*enums)[v].name); |
| 2288 | return LY_EVALID; |
| 2289 | } |
| 2290 | } |
| 2291 | } else if (base_enums) { |
| 2292 | /* inherit the assigned value */ |
| 2293 | e->value = base_enums[match].value; |
| 2294 | if (!u || e->value >= value) { |
| 2295 | value = e->value + 1; |
| 2296 | } |
| 2297 | } else { |
| 2298 | /* assign value automatically */ |
| 2299 | if (u && value == INT32_MIN) { |
| 2300 | /* counter overflow */ |
| 2301 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2302 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2303 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2304 | return LY_EVALID; |
| 2305 | } |
| 2306 | e->value = value++; |
| 2307 | } |
| 2308 | } else { /* LY_TYPE_BITS */ |
| 2309 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2310 | e->value = (int32_t)enums_p[u].value; |
| 2311 | if (!u || (uint32_t)e->value >= position) { |
| 2312 | position = (uint32_t)e->value + 1; |
| 2313 | } |
| 2314 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2315 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2316 | if (e->value == (*enums)[v].value) { |
| 2317 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2318 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2319 | (uint32_t)e->value, e->name, (*enums)[v].name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2320 | return LY_EVALID; |
| 2321 | } |
| 2322 | } |
| 2323 | } else if (base_enums) { |
| 2324 | /* inherit the assigned value */ |
| 2325 | e->value = base_enums[match].value; |
| 2326 | if (!u || (uint32_t)e->value >= position) { |
| 2327 | position = (uint32_t)e->value + 1; |
| 2328 | } |
| 2329 | } else { |
| 2330 | /* assign value automatically */ |
| 2331 | if (u && position == 0) { |
| 2332 | /* counter overflow */ |
| 2333 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2334 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2335 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2336 | return LY_EVALID; |
| 2337 | } |
| 2338 | e->value = position++; |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | if (base_enums) { |
| 2343 | /* the assigned values must not change from the derived type */ |
| 2344 | if (e->value != base_enums[match].value) { |
| 2345 | if (basetype == LY_TYPE_ENUM) { |
| 2346 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2347 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2348 | e->name, base_enums[match].value, e->value); |
| 2349 | } else { |
| 2350 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2351 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2352 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2353 | } |
| 2354 | return LY_EVALID; |
| 2355 | } |
| 2356 | } |
| 2357 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2358 | 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] | 2359 | 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] | 2360 | |
| 2361 | if (basetype == LY_TYPE_BITS) { |
| 2362 | /* keep bits ordered by position */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2363 | for (v = u; v && (*enums)[v - 1].value > e->value; --v) {} |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2364 | if (v != u) { |
| 2365 | memcpy(&storage, e, sizeof *e); |
| 2366 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2367 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2368 | } |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | done: |
| 2373 | return ret; |
| 2374 | } |
| 2375 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2376 | /** |
| 2377 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2378 | * |
| 2379 | * path-arg = absolute-path / relative-path |
| 2380 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2381 | * relative-path = 1*(".." "/") descendant-path |
| 2382 | * |
| 2383 | * @param[in,out] path Path to parse. |
| 2384 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2385 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2386 | * @param[out] name Name of the token. |
| 2387 | * @param[out] nam_len Length of the name. |
| 2388 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2389 | * must not be changed between consecutive calls. -1 if the |
| 2390 | * path is absolute. |
| 2391 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2392 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2393 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2394 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2395 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2396 | int32_t *parent_times, ly_bool *has_predicate) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2397 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2398 | int32_t par_times = 0; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2399 | |
| 2400 | assert(path && *path); |
| 2401 | assert(parent_times); |
| 2402 | assert(prefix); |
| 2403 | assert(prefix_len); |
| 2404 | assert(name); |
| 2405 | assert(name_len); |
| 2406 | assert(has_predicate); |
| 2407 | |
| 2408 | *prefix = NULL; |
| 2409 | *prefix_len = 0; |
| 2410 | *name = NULL; |
| 2411 | *name_len = 0; |
| 2412 | *has_predicate = 0; |
| 2413 | |
| 2414 | if (!*parent_times) { |
| 2415 | if (!strncmp(*path, "..", 2)) { |
| 2416 | *path += 2; |
| 2417 | ++par_times; |
| 2418 | while (!strncmp(*path, "/..", 3)) { |
| 2419 | *path += 3; |
| 2420 | ++par_times; |
| 2421 | } |
| 2422 | } |
| 2423 | if (par_times) { |
| 2424 | *parent_times = par_times; |
| 2425 | } else { |
| 2426 | *parent_times = -1; |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | if (**path != '/') { |
| 2431 | return LY_EINVAL; |
| 2432 | } |
| 2433 | /* skip '/' */ |
| 2434 | ++(*path); |
| 2435 | |
| 2436 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2437 | 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] | 2438 | |
| 2439 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2440 | /* path continues by another token or this is the last token */ |
| 2441 | return LY_SUCCESS; |
| 2442 | } else if ((*path)[0] != '[') { |
| 2443 | /* unexpected character */ |
| 2444 | return LY_EINVAL; |
| 2445 | } else { |
| 2446 | /* predicate starting with [ */ |
| 2447 | *has_predicate = 1; |
| 2448 | return LY_SUCCESS; |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2453 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2454 | * |
| 2455 | * The set of features used for target must be a subset of features used for the leafref. |
| 2456 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2457 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2458 | * |
| 2459 | * @param[in] refnode The leafref node. |
| 2460 | * @param[in] target Tha target node of the leafref. |
| 2461 | * @return LY_SUCCESS or LY_EVALID; |
| 2462 | */ |
| 2463 | static LY_ERR |
| 2464 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2465 | { |
| 2466 | LY_ERR ret = LY_EVALID; |
| 2467 | const struct lysc_node *iter; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2468 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2469 | struct ly_set features = {0}; |
| 2470 | |
| 2471 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2472 | if (iter->iffeatures) { |
| 2473 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2474 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2475 | LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0, NULL), cleanup); |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | /* we should have, in features set, a superset of features applicable to the target node. |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2482 | * If the feature is not present, we don;t have a subset of features applicable |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2483 | * to the leafref itself. */ |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2484 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2485 | if (iter->iffeatures) { |
| 2486 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2487 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2488 | if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) { |
| 2489 | /* feature not present */ |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2490 | goto cleanup; |
| 2491 | } |
| 2492 | } |
| 2493 | } |
| 2494 | } |
| 2495 | } |
| 2496 | ret = LY_SUCCESS; |
| 2497 | |
| 2498 | cleanup: |
| 2499 | ly_set_erase(&features, NULL); |
| 2500 | return ret; |
| 2501 | } |
| 2502 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2503 | 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] | 2504 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2505 | 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] | 2506 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2507 | /** |
| 2508 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2509 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2510 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2511 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2512 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2513 | * @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] | 2514 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2515 | * @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] | 2516 | * @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] | 2517 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2518 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2519 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2520 | * @return LY_ERR value. |
| 2521 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2522 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2523 | 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] | 2524 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2525 | struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, |
| 2526 | struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2527 | { |
| 2528 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2529 | struct lysc_type_bin *bin; |
| 2530 | struct lysc_type_num *num; |
| 2531 | struct lysc_type_str *str; |
| 2532 | struct lysc_type_bits *bits; |
| 2533 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2534 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2535 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2536 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2537 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2538 | |
| 2539 | switch (basetype) { |
| 2540 | case LY_TYPE_BINARY: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2541 | bin = (struct lysc_type_bin *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2542 | |
| 2543 | /* 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] | 2544 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2545 | 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] | 2546 | base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2547 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2548 | 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] | 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | if (tpdfname) { |
| 2553 | type_p->compiled = *type; |
| 2554 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2555 | } |
| 2556 | break; |
| 2557 | case LY_TYPE_BITS: |
| 2558 | /* RFC 7950 9.7 - bits */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2559 | bits = (struct lysc_type_bits *)(*type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2560 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2561 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2562 | base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL, |
| 2563 | (struct lysc_type_bitenum_item **)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2564 | } |
| 2565 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2566 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2567 | /* 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] | 2568 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2569 | 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] | 2570 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2571 | 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] | 2572 | free(*type); |
| 2573 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2574 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2575 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | if (tpdfname) { |
| 2579 | type_p->compiled = *type; |
| 2580 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2581 | } |
| 2582 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2583 | case LY_TYPE_DEC64: |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2584 | dec = (struct lysc_type_dec *)(*type); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2585 | |
| 2586 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2587 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2588 | if (!type_p->fraction_digits) { |
| 2589 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2590 | 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] | 2591 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2592 | 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] | 2593 | free(*type); |
| 2594 | *type = NULL; |
| 2595 | } |
| 2596 | return LY_EVALID; |
| 2597 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2598 | dec->fraction_digits = type_p->fraction_digits; |
| 2599 | } else { |
| 2600 | if (type_p->fraction_digits) { |
| 2601 | /* fraction digits is prohibited in types not directly derived from built-in decimal64 */ |
| 2602 | if (tpdfname) { |
| 2603 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2604 | "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.", |
| 2605 | tpdfname); |
| 2606 | } else { |
| 2607 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2608 | "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type."); |
| 2609 | free(*type); |
| 2610 | *type = NULL; |
| 2611 | } |
| 2612 | return LY_EVALID; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2613 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2614 | dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2615 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2616 | |
| 2617 | /* RFC 7950 9.2.4 - range */ |
| 2618 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2619 | 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] | 2620 | base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2621 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2622 | 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] | 2623 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | if (tpdfname) { |
| 2627 | type_p->compiled = *type; |
| 2628 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2629 | } |
| 2630 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2631 | case LY_TYPE_STRING: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2632 | str = (struct lysc_type_str *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2633 | |
| 2634 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2635 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2636 | 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] | 2637 | base ? ((struct lysc_type_str *)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2638 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2639 | 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] | 2640 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2641 | } else if (base && ((struct lysc_type_str *)base)->length) { |
| 2642 | 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] | 2643 | } |
| 2644 | |
| 2645 | /* RFC 7950 9.4.5 - pattern */ |
| 2646 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2647 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2648 | base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns)); |
| 2649 | } else if (base && ((struct lysc_type_str *)base)->patterns) { |
| 2650 | 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] | 2651 | } |
| 2652 | |
| 2653 | if (tpdfname) { |
| 2654 | type_p->compiled = *type; |
| 2655 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2656 | } |
| 2657 | break; |
| 2658 | case LY_TYPE_ENUM: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2659 | enumeration = (struct lysc_type_enum *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2660 | |
| 2661 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2662 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2663 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2664 | base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2665 | } |
| 2666 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2667 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2668 | /* 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] | 2669 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2670 | 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] | 2671 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2672 | 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] | 2673 | free(*type); |
| 2674 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2675 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2676 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | if (tpdfname) { |
| 2680 | type_p->compiled = *type; |
| 2681 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2682 | } |
| 2683 | break; |
| 2684 | case LY_TYPE_INT8: |
| 2685 | case LY_TYPE_UINT8: |
| 2686 | case LY_TYPE_INT16: |
| 2687 | case LY_TYPE_UINT16: |
| 2688 | case LY_TYPE_INT32: |
| 2689 | case LY_TYPE_UINT32: |
| 2690 | case LY_TYPE_INT64: |
| 2691 | case LY_TYPE_UINT64: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2692 | num = (struct lysc_type_num *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2693 | |
| 2694 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2695 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2696 | 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] | 2697 | base ? ((struct lysc_type_num *)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2698 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2699 | 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] | 2700 | } |
| 2701 | } |
| 2702 | |
| 2703 | if (tpdfname) { |
| 2704 | type_p->compiled = *type; |
| 2705 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2706 | } |
| 2707 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2708 | case LY_TYPE_IDENT: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2709 | idref = (struct lysc_type_identityref *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2710 | |
| 2711 | /* RFC 7950 9.10.2 - base */ |
| 2712 | if (type_p->bases) { |
| 2713 | if (base) { |
| 2714 | /* only the directly derived identityrefs can contain base specification */ |
| 2715 | if (tpdfname) { |
| 2716 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2717 | "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] | 2718 | tpdfname); |
| 2719 | } else { |
| 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 not directly derived from identityref built-in type."); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2722 | free(*type); |
| 2723 | *type = NULL; |
| 2724 | } |
| 2725 | return LY_EVALID; |
| 2726 | } |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2727 | 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] | 2728 | } |
| 2729 | |
| 2730 | if (!base && !type_p->flags) { |
| 2731 | /* type derived from identityref built-in type must contain at least one base */ |
| 2732 | if (tpdfname) { |
| 2733 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2734 | } else { |
| 2735 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2736 | free(*type); |
| 2737 | *type = NULL; |
| 2738 | } |
| 2739 | return LY_EVALID; |
| 2740 | } |
| 2741 | |
| 2742 | if (tpdfname) { |
| 2743 | type_p->compiled = *type; |
| 2744 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2745 | } |
| 2746 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2747 | case LY_TYPE_LEAFREF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2748 | lref = (struct lysc_type_leafref *)*type; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2749 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2750 | /* RFC 7950 9.9.3 - require-instance */ |
| 2751 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2752 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2753 | if (tpdfname) { |
| 2754 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2755 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2756 | } else { |
| 2757 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2758 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2759 | free(*type); |
| 2760 | *type = NULL; |
| 2761 | } |
| 2762 | return LY_EVALID; |
| 2763 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2764 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2765 | } else if (base) { |
| 2766 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2767 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2768 | } else { |
| 2769 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2770 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2771 | } |
| 2772 | if (type_p->path) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2773 | lref->path = lyxp_expr_dup(ctx->ctx, type_p->path); |
| 2774 | lref->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2775 | } else if (base) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2776 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path); |
| 2777 | lref->path_context = ((struct lysc_type_leafref *)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2778 | } else if (tpdfname) { |
| 2779 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2780 | return LY_EVALID; |
| 2781 | } else { |
| 2782 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2783 | free(*type); |
| 2784 | *type = NULL; |
| 2785 | return LY_EVALID; |
| 2786 | } |
| 2787 | if (tpdfname) { |
| 2788 | type_p->compiled = *type; |
| 2789 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2790 | } |
| 2791 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2792 | case LY_TYPE_INST: |
| 2793 | /* RFC 7950 9.9.3 - require-instance */ |
| 2794 | if (type_p->flags & LYS_SET_REQINST) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2795 | ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2796 | } else { |
| 2797 | /* default is true */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2798 | ((struct lysc_type_instanceid *)(*type))->require_instance = 1; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | if (tpdfname) { |
| 2802 | type_p->compiled = *type; |
| 2803 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2804 | } |
| 2805 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2806 | case LY_TYPE_UNION: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2807 | un = (struct lysc_type_union *)(*type); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2808 | |
| 2809 | /* RFC 7950 7.4 - type */ |
| 2810 | if (type_p->types) { |
| 2811 | if (base) { |
| 2812 | /* only the directly derived union can contain types specification */ |
| 2813 | if (tpdfname) { |
| 2814 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2815 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2816 | tpdfname); |
| 2817 | } else { |
| 2818 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2819 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2820 | free(*type); |
| 2821 | *type = NULL; |
| 2822 | } |
| 2823 | return LY_EVALID; |
| 2824 | } |
| 2825 | /* compile the type */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2826 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID); |
| 2827 | 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] | 2828 | 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] | 2829 | &type_p->types[u], &un->types[u + additional], NULL, NULL, NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2830 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2831 | /* add space for additional types from the union subtype */ |
| 2832 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2833 | LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1, |
| 2834 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2835 | |
| 2836 | /* copy subtypes of the subtype union */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2837 | 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] | 2838 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2839 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2840 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2841 | 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] | 2842 | lref = (struct lysc_type_leafref *)un->types[u + additional]; |
| 2843 | |
| 2844 | lref->basetype = LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2845 | 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] | 2846 | lref->refcount = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2847 | lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance; |
| 2848 | 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] | 2849 | /* TODO extensions */ |
| 2850 | |
| 2851 | } else { |
| 2852 | un->types[u + additional] = un_aux->types[v]; |
| 2853 | ++un_aux->types[v]->refcount; |
| 2854 | } |
| 2855 | ++additional; |
| 2856 | LY_ARRAY_INCREMENT(un->types); |
| 2857 | } |
| 2858 | /* compensate u increment in main loop */ |
| 2859 | --additional; |
| 2860 | |
| 2861 | /* free the replaced union subtype */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2862 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2863 | } else { |
| 2864 | LY_ARRAY_INCREMENT(un->types); |
| 2865 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2866 | } |
| 2867 | } |
| 2868 | |
| 2869 | if (!base && !type_p->flags) { |
| 2870 | /* type derived from union built-in type must contain at least one type */ |
| 2871 | if (tpdfname) { |
| 2872 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2873 | } else { |
| 2874 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2875 | free(*type); |
| 2876 | *type = NULL; |
| 2877 | } |
| 2878 | return LY_EVALID; |
| 2879 | } |
| 2880 | |
| 2881 | if (tpdfname) { |
| 2882 | type_p->compiled = *type; |
| 2883 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2884 | } |
| 2885 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2886 | case LY_TYPE_BOOL: |
| 2887 | case LY_TYPE_EMPTY: |
| 2888 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2889 | break; |
| 2890 | } |
| 2891 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2892 | done: |
| 2893 | return ret; |
| 2894 | } |
| 2895 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2896 | /** |
| 2897 | * @brief Compile information about the leaf/leaf-list's type. |
| 2898 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2899 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2900 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2901 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2902 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2903 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2904 | * @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] | 2905 | * @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] | 2906 | * @param[out] dflt Default value for the type. |
| 2907 | * @param[out] dflt_mod Local module for the default value. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2908 | * @return LY_ERR value. |
| 2909 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2910 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2911 | 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] | 2912 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2913 | 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] | 2914 | { |
| 2915 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2916 | ly_bool dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2917 | struct type_context { |
| 2918 | const struct lysp_tpdf *tpdf; |
| 2919 | struct lysp_node *node; |
| 2920 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2921 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2922 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2923 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2924 | struct ly_set tpdf_chain = {0}; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2925 | |
| 2926 | assert((dflt && dflt_mod) || (!dflt && !dflt_mod)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2927 | |
| 2928 | (*type) = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2929 | if (dflt) { |
| 2930 | *dflt = NULL; |
| 2931 | *dflt_mod = NULL; |
| 2932 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2933 | |
| 2934 | tctx = calloc(1, sizeof *tctx); |
| 2935 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2936 | 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] | 2937 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2938 | ret == LY_SUCCESS; |
| 2939 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2940 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2941 | if (basetype) { |
| 2942 | break; |
| 2943 | } |
| 2944 | |
| 2945 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2946 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2947 | tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name); |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 2948 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2949 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2950 | if (units && !*units) { |
| 2951 | /* inherit units */ |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 2952 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret); |
| 2953 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2954 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2955 | if (dflt && !*dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2956 | /* inherit default */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2957 | *dflt = tctx->tpdf->dflt; |
| 2958 | *dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2959 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2960 | if (dummyloops && (!units || *units) && dflt && *dflt) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2961 | 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] | 2962 | break; |
| 2963 | } |
| 2964 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2965 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2966 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2967 | * 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] | 2968 | basetype = tctx->tpdf->type.compiled->basetype; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2969 | ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 2970 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2971 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2972 | if ((units && !*units) || (dflt && !*dflt)) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2973 | dummyloops = 1; |
| 2974 | goto preparenext; |
| 2975 | } else { |
| 2976 | tctx = NULL; |
| 2977 | break; |
| 2978 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2979 | } |
| 2980 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2981 | /* circular typedef reference detection */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2982 | for (uint32_t u = 0; u < tpdf_chain.count; u++) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2983 | /* local part */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2984 | tctx_iter = (struct type_context *)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2985 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2986 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2987 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2988 | free(tctx); |
| 2989 | ret = LY_EVALID; |
| 2990 | goto cleanup; |
| 2991 | } |
| 2992 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2993 | for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2994 | /* global part for unions corner case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2995 | tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2996 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2997 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2998 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2999 | free(tctx); |
| 3000 | ret = LY_EVALID; |
| 3001 | goto cleanup; |
| 3002 | } |
| 3003 | } |
| 3004 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3005 | /* store information for the following processing */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3006 | ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 3007 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3008 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3009 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3010 | /* prepare next loop */ |
| 3011 | tctx_prev = tctx; |
| 3012 | tctx = calloc(1, sizeof *tctx); |
| 3013 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3014 | } |
| 3015 | free(tctx); |
| 3016 | |
| 3017 | /* allocate type according to the basetype */ |
| 3018 | switch (basetype) { |
| 3019 | case LY_TYPE_BINARY: |
| 3020 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3021 | break; |
| 3022 | case LY_TYPE_BITS: |
| 3023 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3024 | break; |
| 3025 | case LY_TYPE_BOOL: |
| 3026 | case LY_TYPE_EMPTY: |
| 3027 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3028 | break; |
| 3029 | case LY_TYPE_DEC64: |
| 3030 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3031 | break; |
| 3032 | case LY_TYPE_ENUM: |
| 3033 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3034 | break; |
| 3035 | case LY_TYPE_IDENT: |
| 3036 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3037 | break; |
| 3038 | case LY_TYPE_INST: |
| 3039 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3040 | break; |
| 3041 | case LY_TYPE_LEAFREF: |
| 3042 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3043 | break; |
| 3044 | case LY_TYPE_STRING: |
| 3045 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3046 | break; |
| 3047 | case LY_TYPE_UNION: |
| 3048 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3049 | break; |
| 3050 | case LY_TYPE_INT8: |
| 3051 | case LY_TYPE_UINT8: |
| 3052 | case LY_TYPE_INT16: |
| 3053 | case LY_TYPE_UINT16: |
| 3054 | case LY_TYPE_INT32: |
| 3055 | case LY_TYPE_UINT32: |
| 3056 | case LY_TYPE_INT64: |
| 3057 | case LY_TYPE_UINT64: |
| 3058 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3059 | break; |
| 3060 | case LY_TYPE_UNKNOWN: |
| 3061 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3062 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3063 | ret = LY_EVALID; |
| 3064 | goto cleanup; |
| 3065 | } |
| 3066 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3067 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3068 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3069 | ly_data_type2str[basetype]); |
| 3070 | free(*type); |
| 3071 | (*type) = NULL; |
| 3072 | ret = LY_EVALID; |
| 3073 | goto cleanup; |
| 3074 | } |
| 3075 | |
| 3076 | /* get restrictions from the referred typedefs */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3077 | for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3078 | tctx = (struct type_context *)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3079 | |
| 3080 | /* remember the typedef context for circular check */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3081 | ret = ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
| 3082 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 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. |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3198 | * @param[in] parent Parent of the nodes to check, can be NULL. |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3199 | * @param[in] name Name of the item to find in the given lists. |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3200 | * @param[in] exclude Node that was just added that should be excluded from the name checking. |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3201 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3202 | */ |
| 3203 | static LY_ERR |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3204 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name, |
| 3205 | const struct lysc_node *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3206 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3207 | const struct lysc_node *iter, *iter2; |
| 3208 | const struct lysc_action *actions; |
| 3209 | const struct lysc_notif *notifs; |
| 3210 | uint32_t getnext_flags; |
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 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3213 | #define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name)) |
| 3214 | |
| 3215 | if (exclude->nodetype == LYS_CASE) { |
| 3216 | /* check restricted only to all the cases */ |
| 3217 | assert(parent->nodetype == LYS_CHOICE); |
| 3218 | LY_LIST_FOR(lysc_node_children(parent, 0), iter) { |
| 3219 | if (CHECK_NODE(iter, exclude, name)) { |
| 3220 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case"); |
| 3221 | return LY_EEXIST; |
| 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | return LY_SUCCESS; |
| 3226 | } |
| 3227 | |
| 3228 | /* no reason for our parent to be choice anymore */ |
| 3229 | assert(!parent || (parent->nodetype != LYS_CHOICE)); |
| 3230 | |
| 3231 | if (parent && (parent->nodetype == LYS_CASE)) { |
| 3232 | /* move to the first data definition parent */ |
| 3233 | parent = lysc_data_parent(parent); |
| 3234 | } |
| 3235 | |
| 3236 | getnext_flags = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE; |
| 3237 | if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) { |
| 3238 | getnext_flags |= LYS_GETNEXT_OUTPUT; |
| 3239 | } |
| 3240 | |
| 3241 | iter = NULL; |
| 3242 | while ((iter = lys_getnext(iter, parent, ctx->mod->compiled, getnext_flags))) { |
| 3243 | if (CHECK_NODE(iter, exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3244 | goto error; |
| 3245 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3246 | |
| 3247 | /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */ |
| 3248 | if (iter->nodetype == LYS_CHOICE) { |
| 3249 | iter2 = NULL; |
| 3250 | while ((iter2 = lys_getnext(iter2, iter, NULL, LYS_GETNEXT_NOSTATECHECK))) { |
| 3251 | if (CHECK_NODE(iter2, exclude, name)) { |
| 3252 | goto error; |
| 3253 | } |
| 3254 | } |
| 3255 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3256 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3257 | |
| 3258 | actions = parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3259 | LY_ARRAY_FOR(actions, u) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3260 | if (CHECK_NODE(&actions[u], exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3261 | goto error; |
| 3262 | } |
| 3263 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3264 | |
| 3265 | notifs = parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3266 | LY_ARRAY_FOR(notifs, u) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3267 | if (CHECK_NODE(¬ifs[u], exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3268 | goto error; |
| 3269 | } |
| 3270 | } |
| 3271 | return LY_SUCCESS; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3272 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3273 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3274 | 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] | 3275 | return LY_EEXIST; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3276 | |
| 3277 | #undef CHECK_NODE |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3278 | } |
| 3279 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3280 | 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] | 3281 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3282 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3283 | * @brief Compile parsed RPC/action schema node information. |
| 3284 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3285 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3286 | * @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] | 3287 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3288 | * @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). |
| 3289 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3290 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3291 | */ |
| 3292 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3293 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3294 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3295 | { |
| 3296 | LY_ERR ret = LY_SUCCESS; |
| 3297 | struct lysp_node *child_p; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3298 | LY_ARRAY_COUNT_TYPE u; |
| 3299 | uint32_t opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3300 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3301 | lysc_update_path(ctx, parent, action_p->name); |
| 3302 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3303 | /* member needed for uniqueness check lys_getnext() */ |
| 3304 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
| 3305 | action->module = ctx->mod; |
| 3306 | action->parent = parent; |
| 3307 | |
| 3308 | LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, action_p->name, (struct lysc_node *)action)); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3309 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3310 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3311 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3312 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3313 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3314 | return LY_EVALID; |
| 3315 | } |
| 3316 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3317 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3318 | action->sp = action_p; |
| 3319 | } |
| 3320 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3321 | |
| 3322 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3323 | * 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] | 3324 | 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] | 3325 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3326 | DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup); |
| 3327 | DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup); |
| 3328 | DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3329 | 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] | 3330 | 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] | 3331 | |
| 3332 | /* input */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3333 | lysc_update_path(ctx, (struct lysc_node *)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3334 | 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] | 3335 | 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] | 3336 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3337 | LY_LIST_FOR(action_p->input.data, child_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3338 | 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] | 3339 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3340 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3341 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3342 | |
| 3343 | /* output */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3344 | lysc_update_path(ctx, (struct lysc_node *)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3345 | 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] | 3346 | 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] | 3347 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3348 | LY_LIST_FOR(action_p->output.data, child_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3349 | 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] | 3350 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3351 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3352 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3353 | |
| 3354 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3355 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3356 | ret = ly_set_add(&ctx->xpath, action, 0, NULL); |
| 3357 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3358 | } |
| 3359 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3360 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3361 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3362 | return ret; |
| 3363 | } |
| 3364 | |
| 3365 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3366 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3367 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3368 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3369 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3370 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3371 | * @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] | 3372 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3373 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3374 | */ |
| 3375 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3376 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3377 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3378 | { |
| 3379 | LY_ERR ret = LY_SUCCESS; |
| 3380 | struct lysp_node *child_p; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3381 | LY_ARRAY_COUNT_TYPE u; |
| 3382 | uint32_t opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3383 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3384 | lysc_update_path(ctx, parent, notif_p->name); |
| 3385 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3386 | /* member needed for uniqueness check lys_getnext() */ |
| 3387 | notif->nodetype = LYS_NOTIF; |
| 3388 | notif->module = ctx->mod; |
| 3389 | notif->parent = parent; |
| 3390 | |
| 3391 | LY_CHECK_RET(lys_compile_node_uniqness(ctx, parent, notif_p->name, (struct lysc_node *)notif)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3392 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3393 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3394 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3395 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3396 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3397 | return LY_EVALID; |
| 3398 | } |
| 3399 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3400 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3401 | notif->sp = notif_p; |
| 3402 | } |
| 3403 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3404 | |
| 3405 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3406 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3407 | ret = lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0)); |
| 3408 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3409 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3410 | DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup); |
| 3411 | DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup); |
| 3412 | DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3413 | 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] | 3414 | 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] | 3415 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3416 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3417 | ret = ly_set_add(&ctx->xpath, notif, 0, NULL); |
| 3418 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3419 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3420 | 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] | 3421 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3422 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3423 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3424 | ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status); |
| 3425 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3426 | } |
| 3427 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3428 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3429 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3430 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3431 | return ret; |
| 3432 | } |
| 3433 | |
| 3434 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3435 | * @brief Compile parsed container node information. |
| 3436 | * @param[in] ctx Compile context |
| 3437 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3438 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3439 | * is enriched with the container-specific information. |
| 3440 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3441 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3442 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3443 | 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] | 3444 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3445 | struct lysp_node_container *cont_p = (struct lysp_node_container *)node_p; |
| 3446 | struct lysc_node_container *cont = (struct lysc_node_container *)node; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3447 | struct lysp_node *child_p; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3448 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3449 | LY_ERR ret = LY_SUCCESS; |
| 3450 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3451 | if (cont_p->presence) { |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3452 | /* explicit presence */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3453 | cont->flags |= LYS_PRESENCE; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3454 | } else if (cont_p->musts) { |
| 3455 | /* container with a must condition */ |
Radek Krejci | 175f25b | 2020-08-13 12:02:36 +0200 | [diff] [blame] | 3456 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name); |
| 3457 | cont->flags |= LYS_PRESENCE; |
| 3458 | } else if (cont_p->when) { |
| 3459 | /* container with a when condition */ |
| 3460 | 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] | 3461 | cont->flags |= LYS_PRESENCE; |
| 3462 | } else if (cont_p->parent) { |
| 3463 | if (cont_p->parent->nodetype == LYS_CHOICE) { |
| 3464 | /* 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] | 3465 | 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] | 3466 | cont_p->name, cont_p->parent->name); |
| 3467 | cont->flags |= LYS_PRESENCE; |
| 3468 | } else if ((cont_p->parent->nodetype == LYS_CASE) |
| 3469 | && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) { |
| 3470 | /* 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] | 3471 | 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] | 3472 | cont_p->name, cont_p->parent->name); |
| 3473 | cont->flags |= LYS_PRESENCE; |
| 3474 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3475 | } |
| 3476 | |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3477 | /* more cases when the container has meaning but is kept NP for convenience: |
| 3478 | * - when condition |
| 3479 | * - direct child action/notification |
| 3480 | */ |
| 3481 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3482 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3483 | ret = lys_compile_node(ctx, child_p, node, 0); |
| 3484 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3485 | } |
| 3486 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3487 | 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] | 3488 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3489 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3490 | ret = ly_set_add(&ctx->xpath, cont, 0, NULL); |
| 3491 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3492 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3493 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3494 | 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] | 3495 | |
| 3496 | done: |
| 3497 | return ret; |
| 3498 | } |
| 3499 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3500 | /* |
| 3501 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3502 | * @param[in] ctx Compile context. |
| 3503 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3504 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3505 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3506 | * @return LY_ERR value. |
| 3507 | */ |
| 3508 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3509 | 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] | 3510 | struct lysc_node_leaf *leaf) |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3511 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3512 | const char *dflt; |
| 3513 | struct lys_module *dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3514 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3515 | 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] | 3516 | leaf->units ? NULL : &leaf->units, &dflt, &dflt_mod)); |
| 3517 | |
| 3518 | /* store default value, if any */ |
| 3519 | if (dflt && !(leaf->flags & LYS_SET_DFLT)) { |
| 3520 | 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] | 3521 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3522 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3523 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3524 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3525 | LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3526 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3527 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3528 | LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) { |
| 3529 | 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] | 3530 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3531 | LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3532 | } |
| 3533 | } |
| 3534 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3535 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3536 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3537 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3538 | return LY_EVALID; |
| 3539 | } |
| 3540 | } |
| 3541 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3542 | return LY_SUCCESS; |
| 3543 | } |
| 3544 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3545 | /** |
| 3546 | * @brief Compile parsed leaf node information. |
| 3547 | * @param[in] ctx Compile context |
| 3548 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3549 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3550 | * is enriched with the leaf-specific information. |
| 3551 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3552 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3553 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3554 | 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] | 3555 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3556 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)node_p; |
| 3557 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 3558 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3559 | LY_ERR ret = LY_SUCCESS; |
| 3560 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3561 | 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] | 3562 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3563 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3564 | ret = ly_set_add(&ctx->xpath, leaf, 0, NULL); |
| 3565 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3566 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3567 | if (leaf_p->units) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3568 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3569 | leaf->flags |= LYS_SET_UNITS; |
| 3570 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3571 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3572 | /* compile type */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3573 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
| 3574 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3575 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3576 | /* store/update default value */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3577 | if (leaf_p->dflt) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3578 | 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] | 3579 | leaf->flags |= LYS_SET_DFLT; |
| 3580 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3581 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3582 | done: |
| 3583 | return ret; |
| 3584 | } |
| 3585 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3586 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3587 | * @brief Compile parsed leaf-list node information. |
| 3588 | * @param[in] ctx Compile context |
| 3589 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3590 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3591 | * is enriched with the leaf-list-specific information. |
| 3592 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3593 | */ |
| 3594 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3595 | 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] | 3596 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3597 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)node_p; |
| 3598 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3599 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3600 | LY_ERR ret = LY_SUCCESS; |
| 3601 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3602 | 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] | 3603 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3604 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3605 | ret = ly_set_add(&ctx->xpath, llist, 0, NULL); |
| 3606 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3607 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3608 | if (llist_p->units) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3609 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3610 | llist->flags |= LYS_SET_UNITS; |
| 3611 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3612 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3613 | /* compile type */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3614 | ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf *)llist); |
| 3615 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 6a044b2 | 2020-01-15 12:25:39 +0100 | [diff] [blame] | 3616 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3617 | /* store/update default values */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3618 | if (llist_p->dflts) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3619 | 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] | 3620 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3621 | } |
| 3622 | |
| 3623 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3624 | if (llist->min) { |
| 3625 | llist->flags |= LYS_MAND_TRUE; |
| 3626 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3627 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3628 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3629 | done: |
| 3630 | return ret; |
| 3631 | } |
| 3632 | |
| 3633 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3634 | * @brief Compile information about list's uniques. |
| 3635 | * @param[in] ctx Compile context. |
| 3636 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3637 | * @param[in] uniques Sized array list of unique statements. |
| 3638 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3639 | * @return LY_ERR value. |
| 3640 | */ |
| 3641 | static LY_ERR |
| 3642 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3643 | { |
| 3644 | LY_ERR ret = LY_SUCCESS; |
| 3645 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3646 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3647 | const char *keystr, *delim; |
| 3648 | size_t len; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3649 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3650 | int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3651 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3652 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3653 | for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3654 | config = -1; |
| 3655 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3656 | keystr = uniques[v]; |
| 3657 | while (keystr) { |
| 3658 | delim = strpbrk(keystr, " \t\n"); |
| 3659 | if (delim) { |
| 3660 | len = delim - keystr; |
| 3661 | while (isspace(*delim)) { |
| 3662 | ++delim; |
| 3663 | } |
| 3664 | } else { |
| 3665 | len = strlen(keystr); |
| 3666 | } |
| 3667 | |
| 3668 | /* unique node must be present */ |
| 3669 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3670 | ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, context_module, LYS_LEAF, 0, |
| 3671 | (const struct lysc_node **)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3672 | if (ret != LY_SUCCESS) { |
| 3673 | if (ret == LY_EDENIED) { |
| 3674 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3675 | "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] | 3676 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3677 | } |
| 3678 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3679 | } else if (flags) { |
| 3680 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3681 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3682 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3683 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3684 | } |
| 3685 | |
| 3686 | /* all referenced leafs must be of the same config type */ |
| 3687 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3688 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3689 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3690 | return LY_EVALID; |
| 3691 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3692 | config = 1; |
| 3693 | } else { /* LYS_CONFIG_R */ |
| 3694 | config = 0; |
| 3695 | } |
| 3696 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3697 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3698 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3699 | if (parent->nodetype == LYS_LIST) { |
| 3700 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3701 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3702 | return LY_EVALID; |
| 3703 | } |
| 3704 | } |
| 3705 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3706 | /* check status */ |
| 3707 | 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] | 3708 | (*key)->flags, (*key)->module, (*key)->name)); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3709 | |
| 3710 | /* mark leaf as unique */ |
| 3711 | (*key)->flags |= LYS_UNIQUE; |
| 3712 | |
| 3713 | /* next unique value in line */ |
| 3714 | keystr = delim; |
| 3715 | } |
| 3716 | /* next unique definition */ |
| 3717 | } |
| 3718 | |
| 3719 | return LY_SUCCESS; |
| 3720 | } |
| 3721 | |
| 3722 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3723 | * @brief Compile parsed list node information. |
| 3724 | * @param[in] ctx Compile context |
| 3725 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3726 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3727 | * is enriched with the list-specific information. |
| 3728 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3729 | */ |
| 3730 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3731 | 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] | 3732 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3733 | struct lysp_node_list *list_p = (struct lysp_node_list *)node_p; |
| 3734 | struct lysc_node_list *list = (struct lysc_node_list *)node; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3735 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3736 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3737 | size_t len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3738 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3739 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3740 | LY_ERR ret = LY_SUCCESS; |
| 3741 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3742 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3743 | if (list->min) { |
| 3744 | list->flags |= LYS_MAND_TRUE; |
| 3745 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3746 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3747 | |
| 3748 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3749 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3750 | } |
| 3751 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3752 | 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] | 3753 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3754 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3755 | LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL)); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3756 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3757 | |
| 3758 | /* keys */ |
| 3759 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3760 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3761 | return LY_EVALID; |
| 3762 | } |
| 3763 | |
| 3764 | /* find all the keys (must be direct children) */ |
| 3765 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3766 | if (!keystr) { |
| 3767 | /* keyless list */ |
| 3768 | list->flags |= LYS_KEYLESS; |
| 3769 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3770 | while (keystr) { |
| 3771 | delim = strpbrk(keystr, " \t\n"); |
| 3772 | if (delim) { |
| 3773 | len = delim - keystr; |
| 3774 | while (isspace(*delim)) { |
| 3775 | ++delim; |
| 3776 | } |
| 3777 | } else { |
| 3778 | len = strlen(keystr); |
| 3779 | } |
| 3780 | |
| 3781 | /* key node must be present */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3782 | 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] | 3783 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3784 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3785 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3786 | return LY_EVALID; |
| 3787 | } |
| 3788 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3789 | if (key->flags & LYS_KEY) { |
| 3790 | /* the node was already marked as a key */ |
| 3791 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3792 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3793 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3794 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3795 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3796 | lysc_update_path(ctx, (struct lysc_node *)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3797 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3798 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3799 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3800 | return LY_EVALID; |
| 3801 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3802 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3803 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3804 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3805 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3806 | "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] | 3807 | return LY_EVALID; |
| 3808 | } |
| 3809 | } else { |
| 3810 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3811 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3812 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3813 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3814 | return LY_EVALID; |
| 3815 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3816 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3817 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3818 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3819 | return LY_EVALID; |
| 3820 | } |
| 3821 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3822 | |
| 3823 | /* check status */ |
| 3824 | 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] | 3825 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3826 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3827 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3828 | if (key->dflt) { |
| 3829 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3830 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3831 | free(key->dflt); |
| 3832 | key->dflt = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3833 | } |
| 3834 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3835 | key->flags |= LYS_KEY; |
| 3836 | |
| 3837 | /* move it to the correct position */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3838 | 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] | 3839 | /* fix links in closest previous siblings of the key */ |
| 3840 | if (key->next) { |
| 3841 | key->next->prev = key->prev; |
| 3842 | } else { |
| 3843 | /* last child */ |
| 3844 | list->child->prev = key->prev; |
| 3845 | } |
| 3846 | if (key->prev->next) { |
| 3847 | key->prev->next = key->next; |
| 3848 | } |
| 3849 | /* fix links in the key */ |
| 3850 | if (prev_key) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3851 | key->prev = (struct lysc_node *)prev_key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3852 | key->next = prev_key->next; |
| 3853 | } else { |
| 3854 | key->prev = list->child->prev; |
| 3855 | key->next = list->child; |
| 3856 | } |
| 3857 | /* fix links in closes future siblings of the key */ |
| 3858 | if (prev_key) { |
| 3859 | if (prev_key->next) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3860 | prev_key->next->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3861 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3862 | list->child->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3863 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3864 | prev_key->next = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3865 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3866 | list->child->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3867 | } |
| 3868 | /* fix links in parent */ |
| 3869 | if (!key->prev->next) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3870 | list->child = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3871 | } |
| 3872 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3873 | |
| 3874 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3875 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3876 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3877 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3878 | } |
| 3879 | |
| 3880 | /* uniques */ |
| 3881 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3882 | 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] | 3883 | } |
| 3884 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3885 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3886 | 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] | 3887 | |
| 3888 | done: |
| 3889 | return ret; |
| 3890 | } |
| 3891 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3892 | /** |
| 3893 | * @brief Do some checks and set the default choice's case. |
| 3894 | * |
| 3895 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3896 | * |
| 3897 | * @param[in] ctx Compile context. |
| 3898 | * @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, |
| 3899 | * not the reference to the imported module. |
| 3900 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3901 | * @return LY_ERR value. |
| 3902 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3903 | static LY_ERR |
| 3904 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3905 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3906 | struct lysc_node *iter, *node = (struct lysc_node *)ch; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3907 | const char *prefix = NULL, *name; |
| 3908 | size_t prefix_len = 0; |
| 3909 | |
| 3910 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3911 | name = strchr(dflt, ':'); |
| 3912 | if (name) { |
| 3913 | prefix = dflt; |
| 3914 | prefix_len = name - prefix; |
| 3915 | ++name; |
| 3916 | } else { |
| 3917 | name = dflt; |
| 3918 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 3919 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3920 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3921 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3922 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3923 | prefix_len, prefix); |
| 3924 | return LY_EVALID; |
| 3925 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3926 | 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] | 3927 | if (!ch->dflt) { |
| 3928 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3929 | "Default case \"%s\" not found.", dflt); |
| 3930 | return LY_EVALID; |
| 3931 | } |
| 3932 | /* no mandatory nodes directly under the default case */ |
| 3933 | LY_LIST_FOR(ch->dflt->child, iter) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3934 | if (iter->parent != (struct lysc_node *)ch->dflt) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3935 | break; |
| 3936 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3937 | if (iter->flags & LYS_MAND_TRUE) { |
| 3938 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3939 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3940 | return LY_EVALID; |
| 3941 | } |
| 3942 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3943 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3944 | return LY_SUCCESS; |
| 3945 | } |
| 3946 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3947 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3948 | 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] | 3949 | { |
| 3950 | struct lys_module *mod; |
| 3951 | const char *prefix = NULL, *name; |
| 3952 | size_t prefix_len = 0; |
| 3953 | struct lysc_node_case *cs; |
| 3954 | struct lysc_node *node; |
| 3955 | |
| 3956 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3957 | name = strchr(dflt, ':'); |
| 3958 | if (name) { |
| 3959 | prefix = dflt; |
| 3960 | prefix_len = name - prefix; |
| 3961 | ++name; |
| 3962 | } else { |
| 3963 | name = dflt; |
| 3964 | } |
| 3965 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3966 | if (prefix) { |
| 3967 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3968 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3969 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3970 | "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] | 3971 | return LY_EVALID; |
| 3972 | } |
| 3973 | } else { |
| 3974 | mod = ctx->mod; |
| 3975 | } |
| 3976 | /* get the default case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3977 | 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] | 3978 | if (!cs) { |
| 3979 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3980 | "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] | 3981 | return LY_EVALID; |
| 3982 | } |
| 3983 | |
| 3984 | /* check that there is no mandatory node */ |
| 3985 | LY_LIST_FOR(cs->child, node) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3986 | if (node->parent != (struct lysc_node *)cs) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3987 | break; |
| 3988 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3989 | if (node->flags & LYS_MAND_TRUE) { |
| 3990 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3991 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 3992 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3993 | return LY_EVALID; |
| 3994 | } |
| 3995 | } |
| 3996 | |
| 3997 | /* set the default case in choice */ |
| 3998 | ch->dflt = cs; |
| 3999 | cs->flags |= LYS_SET_DFLT; |
| 4000 | |
| 4001 | return LY_SUCCESS; |
| 4002 | } |
| 4003 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4004 | /** |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4005 | * @brief Compile choice children. |
| 4006 | * |
| 4007 | * @param[in] ctx Compile context |
| 4008 | * @param[in] child_p Parsed choice children nodes. |
| 4009 | * @param[in] node Compiled choice node to compile and add children to. |
| 4010 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4011 | */ |
| 4012 | static LY_ERR |
| 4013 | lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node) |
| 4014 | { |
| 4015 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 8f5fad2 | 2020-09-15 16:50:54 +0200 | [diff] [blame] | 4016 | struct lysp_node *child_p_next = child_p->next; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4017 | struct lysp_node_case *cs_p; |
| 4018 | |
| 4019 | if (child_p->nodetype == LYS_CASE) { |
| 4020 | /* standard case under choice */ |
| 4021 | ret = lys_compile_node(ctx, child_p, node, 0); |
| 4022 | } else { |
| 4023 | /* we need the implicit case first, so create a fake parsed case */ |
| 4024 | cs_p = calloc(1, sizeof *cs_p); |
| 4025 | cs_p->nodetype = LYS_CASE; |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 4026 | DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4027 | cs_p->child = child_p; |
| 4028 | |
| 4029 | /* make the child the only case child */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4030 | child_p->next = NULL; |
| 4031 | |
| 4032 | /* compile it normally */ |
| 4033 | ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0); |
| 4034 | |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 4035 | free_fake_node: |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4036 | /* free the fake parsed node and correct pointers back */ |
| 4037 | cs_p->child = NULL; |
| 4038 | lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p); |
Radek Krejci | 8f5fad2 | 2020-09-15 16:50:54 +0200 | [diff] [blame] | 4039 | child_p->next = child_p_next; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4040 | } |
| 4041 | |
| 4042 | return ret; |
| 4043 | } |
| 4044 | |
| 4045 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4046 | * @brief Compile parsed choice node information. |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4047 | * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4048 | * @param[in] ctx Compile context |
| 4049 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4050 | * @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] | 4051 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4052 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4053 | */ |
| 4054 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4055 | 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] | 4056 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4057 | struct lysp_node_choice *ch_p = (struct lysp_node_choice *)node_p; |
| 4058 | struct lysc_node_choice *ch = (struct lysc_node_choice *)node; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4059 | struct lysp_node *child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4060 | LY_ERR ret = LY_SUCCESS; |
| 4061 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4062 | assert(node->nodetype == LYS_CHOICE); |
| 4063 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4064 | LY_LIST_FOR(ch_p->child, child_p) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4065 | LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4066 | } |
| 4067 | |
| 4068 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4069 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4070 | 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] | 4071 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4072 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4073 | return ret; |
| 4074 | } |
| 4075 | |
| 4076 | /** |
| 4077 | * @brief Compile parsed anydata or anyxml node information. |
| 4078 | * @param[in] ctx Compile context |
| 4079 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4080 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4081 | * is enriched with the any-specific information. |
| 4082 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4083 | */ |
| 4084 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4085 | 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] | 4086 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4087 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)node_p; |
| 4088 | struct lysc_node_anydata *any = (struct lysc_node_anydata *)node; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4089 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4090 | LY_ERR ret = LY_SUCCESS; |
| 4091 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4092 | 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] | 4093 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4094 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4095 | ret = ly_set_add(&ctx->xpath, any, 0, NULL); |
| 4096 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4097 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4098 | |
| 4099 | if (any->flags & LYS_CONFIG_W) { |
Radek Krejci | 5c4ed7b | 2020-08-12 11:29:44 +0200 | [diff] [blame] | 4100 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s", |
| 4101 | 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] | 4102 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4103 | done: |
| 4104 | return ret; |
| 4105 | } |
| 4106 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4107 | /** |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4108 | * @brief Connect the node into the siblings list and check its name uniqueness. Also, |
| 4109 | * keep specific order of augments targetting the same node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4110 | * |
| 4111 | * @param[in] ctx Compile context |
| 4112 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4113 | * the choice itself is expected instead of a specific case node. |
| 4114 | * @param[in] node Schema node to connect into the list. |
| 4115 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4116 | * 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] | 4117 | */ |
| 4118 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4119 | 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] | 4120 | { |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4121 | struct lysc_node **children, *start, *end; |
| 4122 | const struct lys_module *mod; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4123 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4124 | node->parent = parent; |
| 4125 | |
| 4126 | if (parent) { |
| 4127 | if (parent->nodetype == LYS_CHOICE) { |
| 4128 | assert(node->nodetype == LYS_CASE); |
| 4129 | children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases; |
| 4130 | } else { |
| 4131 | children = lysc_node_children_p(parent, ctx->options); |
| 4132 | } |
| 4133 | assert(children); |
| 4134 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4135 | if (!(*children)) { |
| 4136 | /* first child */ |
| 4137 | *children = node; |
| 4138 | } else if (*children != node) { |
| 4139 | /* by the condition in previous branch we cover the choice/case children |
| 4140 | * - the children list is shared by the choice and the the first case, in addition |
| 4141 | * the first child of each case must be referenced from the case node. So the node is |
| 4142 | * actually always already inserted in case it is the first children - so here such |
| 4143 | * a situation actually corresponds to the first branch */ |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4144 | if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module) |
| 4145 | && (strcmp((*children)->prev->module->name, node->module->name) > 0)) { |
| 4146 | /* some augments are already connected and we are connecting new ones, |
| 4147 | * keep module name order and insert the node into the children list */ |
| 4148 | end = (*children); |
| 4149 | do { |
| 4150 | end = end->prev; |
| 4151 | mod = end->module; |
| 4152 | while (end->prev->module == mod) { |
| 4153 | end = end->prev; |
| 4154 | } |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 4155 | } 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] | 4156 | |
| 4157 | /* we have the last existing node after our node, easily get the first before and connect it */ |
| 4158 | start = end->prev; |
| 4159 | start->next = node; |
| 4160 | node->next = end; |
| 4161 | end->prev = node; |
| 4162 | node->prev = start; |
| 4163 | } else { |
| 4164 | /* insert at the end of the parent's children list */ |
| 4165 | (*children)->prev->next = node; |
| 4166 | node->prev = (*children)->prev; |
| 4167 | (*children)->prev = node; |
| 4168 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4169 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4170 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4171 | /* check the name uniqueness (even for an only child, it may be in case) */ |
| 4172 | if (lys_compile_node_uniqness(ctx, parent, node->name, node)) { |
| 4173 | return LY_EEXIST; |
| 4174 | } |
| 4175 | } else { |
| 4176 | /* top-level element */ |
| 4177 | if (!ctx->mod->compiled->data) { |
| 4178 | ctx->mod->compiled->data = node; |
| 4179 | } else { |
| 4180 | /* insert at the end of the module's top-level nodes list */ |
| 4181 | ctx->mod->compiled->data->prev->next = node; |
| 4182 | node->prev = ctx->mod->compiled->data->prev; |
| 4183 | ctx->mod->compiled->data->prev = node; |
| 4184 | } |
| 4185 | |
| 4186 | /* check the name uniqueness on top-level */ |
| 4187 | if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) { |
| 4188 | return LY_EEXIST; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4189 | } |
| 4190 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4191 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4192 | return LY_SUCCESS; |
| 4193 | } |
| 4194 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4195 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4196 | * @brief Prepare the case structure in choice node for the new data node. |
| 4197 | * |
| 4198 | * 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 |
| 4199 | * created in the choice when the first child was processed. |
| 4200 | * |
| 4201 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4202 | * @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] | 4203 | * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4204 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4205 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4206 | * @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, |
| 4207 | * 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] | 4208 | */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4209 | static LY_ERR |
| 4210 | lys_compile_node_case(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] | 4211 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4212 | struct lysp_node *child_p; |
| 4213 | struct lysp_node_case *cs_p = (struct lysp_node_case *)node_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4214 | |
Radek Krejci | 3942480 | 2020-08-12 09:31:00 +0200 | [diff] [blame] | 4215 | if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4216 | /* we have to add an implicit case node into the parent choice */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4217 | } else if (node_p->nodetype == LYS_CASE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4218 | /* explicit parent case */ |
| 4219 | LY_LIST_FOR(cs_p->child, child_p) { |
| 4220 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4221 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4222 | } else { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4223 | LOGINT_RET(ctx->ctx); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4224 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4225 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4226 | return LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4227 | } |
| 4228 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4229 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4230 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4231 | * |
| 4232 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4233 | * @param[in] node Target node where the config is supposed to be changed. |
| 4234 | * @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] | 4235 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4236 | * 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] | 4237 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4238 | * @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] | 4239 | * @return LY_ERR value. |
| 4240 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4241 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4242 | lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag, |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4243 | ly_bool inheriting, ly_bool refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4244 | { |
| 4245 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4246 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4247 | |
| 4248 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4249 | /* nothing to do */ |
| 4250 | return LY_SUCCESS; |
| 4251 | } |
| 4252 | |
| 4253 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4254 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4255 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4256 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4257 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4258 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4259 | return LY_EVALID; |
| 4260 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4261 | node->flags |= LYS_SET_CONFIG; |
| 4262 | } else { |
| 4263 | if (node->flags & LYS_SET_CONFIG) { |
| 4264 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4265 | /* setting config flags, but have node with explicit config true */ |
| 4266 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4267 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4268 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4269 | return LY_EVALID; |
| 4270 | } |
| 4271 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4272 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4273 | return LY_SUCCESS; |
| 4274 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4275 | } |
| 4276 | node->flags &= ~LYS_CONFIG_MASK; |
| 4277 | node->flags |= config; |
| 4278 | |
| 4279 | /* inherit the change into the children */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4280 | LY_LIST_FOR((struct lysc_node *)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4281 | 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] | 4282 | } |
| 4283 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4284 | return LY_SUCCESS; |
| 4285 | } |
| 4286 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4287 | /** |
| 4288 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4289 | * |
| 4290 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4291 | * the flag to such parents from a mandatory children. |
| 4292 | * |
| 4293 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4294 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4295 | * (mandatory children was removed). |
| 4296 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4297 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4298 | lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add) |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4299 | { |
| 4300 | struct lysc_node *iter; |
| 4301 | |
| 4302 | if (add) { /* set flag */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4303 | 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] | 4304 | parent = parent->parent) { |
| 4305 | parent->flags |= LYS_MAND_TRUE; |
| 4306 | } |
| 4307 | } else { /* unset flag */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4308 | 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] | 4309 | 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] | 4310 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4311 | /* there is another mandatory node */ |
| 4312 | return; |
| 4313 | } |
| 4314 | } |
| 4315 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4316 | parent->flags &= ~LYS_MAND_TRUE; |
| 4317 | } |
| 4318 | } |
| 4319 | } |
| 4320 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4321 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4322 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4323 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4324 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4325 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4326 | */ |
| 4327 | static void |
| 4328 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4329 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4330 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4331 | size_t len; |
| 4332 | |
| 4333 | len = strlen(aug_p->nodeid); |
| 4334 | LY_ARRAY_FOR(result, v) { |
| 4335 | if (strlen(result[v]->nodeid) <= len) { |
| 4336 | continue; |
| 4337 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4338 | if (v < LY_ARRAY_COUNT(result)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4339 | /* move the rest of array */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4340 | 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] | 4341 | break; |
| 4342 | } |
| 4343 | } |
| 4344 | result[v] = aug_p; |
| 4345 | LY_ARRAY_INCREMENT(result); |
| 4346 | } |
| 4347 | |
| 4348 | /** |
| 4349 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4350 | * |
| 4351 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4352 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4353 | * |
| 4354 | * @param[in] ctx Compile context. |
| 4355 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4356 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4357 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4358 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4359 | * @return LY_ERR value. |
| 4360 | */ |
| 4361 | LY_ERR |
| 4362 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4363 | { |
| 4364 | struct lysp_augment **result = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4365 | LY_ARRAY_COUNT_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4366 | |
| 4367 | assert(augments); |
| 4368 | |
| 4369 | /* get count of the augments in module and all its submodules */ |
| 4370 | if (aug_p) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4371 | count += LY_ARRAY_COUNT(aug_p); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4372 | } |
| 4373 | LY_ARRAY_FOR(inc_p, u) { |
| 4374 | if (inc_p[u].submodule->augments) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4375 | count += LY_ARRAY_COUNT(inc_p[u].submodule->augments); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4376 | } |
| 4377 | } |
| 4378 | |
| 4379 | if (!count) { |
| 4380 | *augments = NULL; |
| 4381 | return LY_SUCCESS; |
| 4382 | } |
| 4383 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4384 | |
| 4385 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4386 | * together, so there can be even /z/y betwwen them. */ |
| 4387 | LY_ARRAY_FOR(aug_p, u) { |
| 4388 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4389 | } |
| 4390 | LY_ARRAY_FOR(inc_p, u) { |
| 4391 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4392 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4393 | } |
| 4394 | } |
| 4395 | |
| 4396 | *augments = result; |
| 4397 | return LY_SUCCESS; |
| 4398 | } |
| 4399 | |
| 4400 | /** |
| 4401 | * @brief Compile the parsed augment connecting it into its target. |
| 4402 | * |
| 4403 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4404 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4405 | * are already implemented and compiled. |
| 4406 | * |
| 4407 | * @param[in] ctx Compile context. |
| 4408 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4409 | * @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 |
| 4410 | * children in case of the augmenting uses data. |
| 4411 | * @return LY_SUCCESS on success. |
| 4412 | * @return LY_EVALID on failure. |
| 4413 | */ |
| 4414 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4415 | 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] | 4416 | { |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4417 | LY_ERR ret = LY_SUCCESS, rc; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4418 | struct lysp_node *node_p; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4419 | struct lysc_node *target; /* target target of the augment */ |
| 4420 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4421 | struct lysc_when **when, *when_shared; |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4422 | struct lys_module **aug_mod; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4423 | ly_bool allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4424 | uint16_t flags = 0; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4425 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4426 | uint32_t opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4427 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4428 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4429 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4430 | |
Michal Vasko | cb18c7f | 2020-08-24 09:22:28 +0200 | [diff] [blame] | 4431 | 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] | 4432 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4433 | 1, (const struct lysc_node **)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4434 | if (ret != LY_SUCCESS) { |
| 4435 | if (ret == LY_EDENIED) { |
| 4436 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4437 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4438 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4439 | } |
| 4440 | return LY_EVALID; |
| 4441 | } |
| 4442 | |
| 4443 | /* check for mandatory nodes |
| 4444 | * - new cases augmenting some choice can have mandatory nodes |
| 4445 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4446 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4447 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4448 | allow_mandatory = 1; |
| 4449 | } |
| 4450 | |
| 4451 | when_shared = NULL; |
| 4452 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4453 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4454 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4455 | && !((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] | 4456 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4457 | && !(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] | 4458 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4459 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4460 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4461 | return LY_EVALID; |
| 4462 | } |
| 4463 | |
| 4464 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4465 | ctx->options |= flags; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4466 | if (target->nodetype == LYS_CHOICE) { |
| 4467 | LY_CHECK_RET(lys_compile_node_choice_child(ctx, node_p, target)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4468 | } else { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4469 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4470 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4471 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4472 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4473 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4474 | * 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] | 4475 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4476 | /* 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] | 4477 | 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] | 4478 | } else if (target->nodetype == LYS_CHOICE) { |
| 4479 | /* 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] | 4480 | node = ((struct lysc_node_choice *)target)->cases->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4481 | } else { |
| 4482 | /* the compiled node is the last child of the target */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4483 | node = (struct lysc_node *)lysc_node_children(target, flags); |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4484 | if (!node) { |
| 4485 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4486 | break; |
| 4487 | } |
| 4488 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4489 | } |
| 4490 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4491 | 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] | 4492 | node->flags &= ~LYS_MAND_TRUE; |
| 4493 | lys_compile_mandatory_parents(target, 0); |
| 4494 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4495 | "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] | 4496 | return LY_EVALID; |
| 4497 | } |
| 4498 | |
| 4499 | /* pass augment's when to all the children */ |
| 4500 | if (aug_p->when) { |
| 4501 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4502 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4503 | 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] | 4504 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4505 | |
| 4506 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4507 | /* do not check "when" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4508 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 4509 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4510 | } |
| 4511 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4512 | when_shared = *when; |
| 4513 | } else { |
| 4514 | ++when_shared->refcount; |
| 4515 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4516 | |
| 4517 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4518 | /* in this case check "when" again for all children because of dummy node check */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4519 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 4520 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4521 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4522 | } |
| 4523 | } |
| 4524 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4525 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4526 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4527 | switch (target->nodetype) { |
| 4528 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4529 | 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] | 4530 | u, lys_compile_action, 0, ret, error); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4531 | 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] | 4532 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4533 | break; |
| 4534 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4535 | 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] | 4536 | u, lys_compile_action, 0, ret, error); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4537 | 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] | 4538 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4539 | break; |
| 4540 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4541 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4542 | if (aug_p->actions) { |
| 4543 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4544 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4545 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4546 | return LY_EVALID; |
| 4547 | } |
| 4548 | if (aug_p->notifs) { |
| 4549 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4550 | "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] | 4551 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4552 | return LY_EVALID; |
| 4553 | } |
| 4554 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4555 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4556 | /* add this module into the target module augmented_by, if not there already */ |
| 4557 | rc = LY_SUCCESS; |
| 4558 | LY_ARRAY_FOR(target->module->compiled->augmented_by, v) { |
| 4559 | if (target->module->compiled->augmented_by[v] == ctx->mod) { |
| 4560 | rc = LY_EEXIST; |
| 4561 | break; |
| 4562 | } |
| 4563 | } |
| 4564 | if (!rc) { |
| 4565 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error); |
| 4566 | *aug_mod = ctx->mod; |
| 4567 | } |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4568 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4569 | lysc_update_path(ctx, NULL, NULL); |
| 4570 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4571 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4572 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4573 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4574 | return ret; |
| 4575 | } |
| 4576 | |
| 4577 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4578 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4579 | * |
| 4580 | * @param[in] ctx Compile context. |
| 4581 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4582 | * @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] | 4583 | * @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] | 4584 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4585 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4586 | * 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] | 4587 | * @return LY_ERR value. |
| 4588 | */ |
| 4589 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4590 | lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, ly_bool refine_flag) |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4591 | { |
| 4592 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4593 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4594 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4595 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4596 | return LY_EVALID; |
| 4597 | } |
| 4598 | |
| 4599 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4600 | /* check if node has default value */ |
| 4601 | if (node->nodetype & LYS_LEAF) { |
| 4602 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4603 | if (refine_flag) { |
| 4604 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4605 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4606 | return LY_EVALID; |
| 4607 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4608 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4609 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4610 | if (refine_flag) { |
| 4611 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4612 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4613 | return LY_EVALID; |
| 4614 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4615 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4616 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4617 | 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] | 4618 | return LY_EVALID; |
| 4619 | } |
| 4620 | |
| 4621 | node->flags &= ~LYS_MAND_FALSE; |
| 4622 | node->flags |= LYS_MAND_TRUE; |
| 4623 | lys_compile_mandatory_parents(node->parent, 1); |
| 4624 | } else { |
| 4625 | /* make mandatory false */ |
| 4626 | node->flags &= ~LYS_MAND_TRUE; |
| 4627 | node->flags |= LYS_MAND_FALSE; |
| 4628 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4629 | } |
| 4630 | return LY_SUCCESS; |
| 4631 | } |
| 4632 | |
| 4633 | /** |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4634 | * @brief Find grouping for a uses. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4635 | * |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4636 | * @param[in] ctx Compile context. |
| 4637 | * @param[in] uses_p Parsed uses node. |
| 4638 | * @param[out] gpr_p Found grouping on success. |
| 4639 | * @param[out] grp_mod Module of @p grp_p on success. |
| 4640 | * @return LY_ERR value. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4641 | */ |
| 4642 | static LY_ERR |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4643 | lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p, |
| 4644 | struct lys_module **grp_mod) |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4645 | { |
| 4646 | struct lysp_node *node_p; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4647 | struct lysp_grp *grp; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4648 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4649 | ly_bool found = 0; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4650 | const char *id, *name, *prefix; |
| 4651 | size_t prefix_len, name_len; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4652 | struct lys_module *mod; |
| 4653 | |
| 4654 | *grp_p = NULL; |
| 4655 | *grp_mod = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4656 | |
| 4657 | /* search for the grouping definition */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4658 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4659 | 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] | 4660 | if (prefix) { |
| 4661 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4662 | if (!mod) { |
| 4663 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4664 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4665 | return LY_EVALID; |
| 4666 | } |
| 4667 | } else { |
| 4668 | mod = ctx->mod_def; |
| 4669 | } |
| 4670 | if (mod == ctx->mod_def) { |
| 4671 | 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] | 4672 | grp = (struct lysp_grp *)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4673 | LY_ARRAY_FOR(grp, u) { |
| 4674 | if (!strcmp(grp[u].name, name)) { |
| 4675 | grp = &grp[u]; |
| 4676 | found = 1; |
| 4677 | break; |
| 4678 | } |
| 4679 | } |
| 4680 | } |
| 4681 | } |
| 4682 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4683 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4684 | grp = mod->parsed->groupings; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4685 | LY_ARRAY_FOR(grp, u) { |
| 4686 | if (!strcmp(grp[u].name, name)) { |
| 4687 | grp = &grp[u]; |
| 4688 | found = 1; |
| 4689 | break; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4690 | } |
| 4691 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4692 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4693 | /* ... and all the submodules */ |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4694 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4695 | grp = mod->parsed->includes[u].submodule->groupings; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4696 | LY_ARRAY_FOR(grp, v) { |
| 4697 | if (!strcmp(grp[v].name, name)) { |
| 4698 | grp = &grp[v]; |
| 4699 | found = 1; |
| 4700 | break; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4701 | } |
| 4702 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4703 | if (found) { |
| 4704 | break; |
| 4705 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4706 | } |
| 4707 | } |
| 4708 | } |
| 4709 | if (!found) { |
| 4710 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4711 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4712 | return LY_EVALID; |
| 4713 | } |
| 4714 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4715 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4716 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4717 | grp->flags |= LYS_USED_GRP; |
| 4718 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4719 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4720 | *grp_p = grp; |
| 4721 | *grp_mod = mod; |
| 4722 | return LY_SUCCESS; |
| 4723 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4724 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4725 | static LY_ERR |
| 4726 | lys_compile_refines(struct lysc_ctx *ctx, struct lysp_refine *refines, const struct lysc_node *context_node) |
| 4727 | { |
| 4728 | struct lysc_node *node; |
| 4729 | LY_ARRAY_COUNT_TYPE u; |
| 4730 | struct lysp_refine *rfn; |
| 4731 | LY_ERR ret = LY_SUCCESS; |
| 4732 | uint32_t min, max; |
| 4733 | uint16_t flags; |
| 4734 | struct ly_set refined = {0}; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4735 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4736 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4737 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4738 | /* apply refine */ |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4739 | LY_ARRAY_FOR(refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4740 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4741 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4742 | ret = lysc_resolve_schema_nodeid(ctx, rfn->nodeid, 0, context_node, ctx->mod, |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4743 | 0, 0, (const struct lysc_node **)&node, &flags); |
| 4744 | LY_CHECK_GOTO(ret, cleanup); |
| 4745 | ret = ly_set_add(&refined, node, LY_SET_OPT_USEASLIST, NULL); |
| 4746 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4747 | |
| 4748 | /* default value */ |
| 4749 | if (rfn->dflts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4750 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4751 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4752 | "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] | 4753 | lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4754 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4755 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4756 | } |
| 4757 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4758 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4759 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4760 | lys_nodetype2str(node->nodetype)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4761 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4762 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4763 | } |
| 4764 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4765 | /* postpone default compilation when the tree is complete */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4766 | ret = lysc_incomplete_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0], ctx->mod_def); |
| 4767 | LY_CHECK_GOTO(ret, cleanup); |
| 4768 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4769 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4770 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4771 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4772 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4773 | "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules."); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4774 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4775 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4776 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4777 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4778 | /* postpone default compilation when the tree is complete */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4779 | ret = lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts, ctx->mod_def); |
| 4780 | LY_CHECK_GOTO(ret, cleanup); |
| 4781 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4782 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4783 | } else if (node->nodetype == LYS_CHOICE) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4784 | if (((struct lysc_node_choice *)node)->dflt) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4785 | /* unset LYS_SET_DFLT from the current default case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4786 | ((struct lysc_node_choice *)node)->dflt->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4787 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4788 | ret = lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice *)node); |
| 4789 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4790 | } |
| 4791 | } |
| 4792 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4793 | /* description */ |
| 4794 | if (rfn->dsc) { |
| 4795 | FREE_STRING(ctx->ctx, node->dsc); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 4796 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->dsc, 0, &node->dsc), cleanup); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | /* reference */ |
| 4800 | if (rfn->ref) { |
| 4801 | FREE_STRING(ctx->ctx, node->ref); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 4802 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, rfn->ref, 0, &node->ref), cleanup); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4803 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4804 | |
| 4805 | /* config */ |
| 4806 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4807 | if (!flags) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4808 | ret = lys_compile_change_config(ctx, node, rfn->flags, 0, 1); |
| 4809 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4810 | } else { |
| 4811 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4812 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4813 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4814 | } |
| 4815 | |
| 4816 | /* mandatory */ |
| 4817 | if (rfn->flags & LYS_MAND_MASK) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4818 | ret = lys_compile_change_mandatory(ctx, node, rfn->flags, 1); |
| 4819 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4820 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4821 | |
| 4822 | /* presence */ |
| 4823 | if (rfn->presence) { |
| 4824 | if (node->nodetype != LYS_CONTAINER) { |
| 4825 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4826 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4827 | lys_nodetype2str(node->nodetype)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4828 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4829 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4830 | } |
| 4831 | node->flags |= LYS_PRESENCE; |
| 4832 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4833 | |
| 4834 | /* must */ |
| 4835 | if (rfn->musts) { |
| 4836 | switch (node->nodetype) { |
| 4837 | case LYS_LEAF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4838 | 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] | 4839 | break; |
| 4840 | case LYS_LEAFLIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4841 | 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] | 4842 | break; |
| 4843 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4844 | 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] | 4845 | break; |
| 4846 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4847 | 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] | 4848 | break; |
| 4849 | case LYS_ANYXML: |
| 4850 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4851 | 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] | 4852 | break; |
| 4853 | default: |
| 4854 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4855 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 4856 | lys_nodetype2str(node->nodetype)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4857 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4858 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4859 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4860 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 4861 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4862 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4863 | |
| 4864 | /* min/max-elements */ |
| 4865 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4866 | switch (node->nodetype) { |
| 4867 | case LYS_LEAFLIST: |
| 4868 | if (rfn->flags & LYS_SET_MAX) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4869 | ((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] | 4870 | } |
| 4871 | if (rfn->flags & LYS_SET_MIN) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4872 | ((struct lysc_node_leaflist *)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4873 | if (rfn->min) { |
| 4874 | node->flags |= LYS_MAND_TRUE; |
| 4875 | lys_compile_mandatory_parents(node->parent, 1); |
| 4876 | } else { |
| 4877 | node->flags &= ~LYS_MAND_TRUE; |
| 4878 | lys_compile_mandatory_parents(node->parent, 0); |
| 4879 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4880 | } |
| 4881 | break; |
| 4882 | case LYS_LIST: |
| 4883 | if (rfn->flags & LYS_SET_MAX) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4884 | ((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] | 4885 | } |
| 4886 | if (rfn->flags & LYS_SET_MIN) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4887 | ((struct lysc_node_list *)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4888 | if (rfn->min) { |
| 4889 | node->flags |= LYS_MAND_TRUE; |
| 4890 | lys_compile_mandatory_parents(node->parent, 1); |
| 4891 | } else { |
| 4892 | node->flags &= ~LYS_MAND_TRUE; |
| 4893 | lys_compile_mandatory_parents(node->parent, 0); |
| 4894 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4895 | } |
| 4896 | break; |
| 4897 | default: |
| 4898 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4899 | "Invalid refine of %s statement - %s cannot hold this statement.", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4900 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4901 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4902 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4903 | } |
| 4904 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4905 | |
| 4906 | /* if-feature */ |
| 4907 | if (rfn->iffeatures) { |
| 4908 | /* 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] | 4909 | 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] | 4910 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4911 | |
| 4912 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4913 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4914 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4915 | /* 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] | 4916 | for (uint32_t i = 0; i < refined.count; ++i) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4917 | node = (struct lysc_node *)refined.objs[i]; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4918 | rfn = &refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4919 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4920 | |
| 4921 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 4922 | if ((node->flags & LYS_MAND_TRUE) && |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4923 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) || |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4924 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 4925 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4926 | "Invalid refine of default - the node is mandatory."); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4927 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4928 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4929 | } |
| 4930 | |
| 4931 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4932 | if (node->nodetype == LYS_LIST) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4933 | min = ((struct lysc_node_list *)node)->min; |
| 4934 | max = ((struct lysc_node_list *)node)->max; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4935 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4936 | min = ((struct lysc_node_leaflist *)node)->min; |
| 4937 | max = ((struct lysc_node_leaflist *)node)->max; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4938 | } |
| 4939 | if (min > max) { |
| 4940 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4941 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4942 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4943 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4944 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4945 | } |
| 4946 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4947 | |
| 4948 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4949 | } |
| 4950 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4951 | cleanup: |
| 4952 | ly_set_erase(&refined, NULL); |
| 4953 | lysc_update_path(ctx, NULL, NULL); |
| 4954 | return ret; |
| 4955 | } |
| 4956 | |
| 4957 | /** |
| 4958 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4959 | * If present, also apply uses's modificators. |
| 4960 | * |
| 4961 | * @param[in] ctx Compile context |
| 4962 | * @param[in] uses_p Parsed uses schema node. |
| 4963 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4964 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4965 | * the compile context. |
| 4966 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4967 | */ |
| 4968 | static LY_ERR |
| 4969 | lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p) |
| 4970 | { |
| 4971 | struct lysp_node *node_p; |
Radek Krejci | 8f5fad2 | 2020-09-15 16:50:54 +0200 | [diff] [blame] | 4972 | struct lysc_node *child = NULL, *iter; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4973 | /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */ |
| 4974 | struct lysc_node_container context_node_fake = |
| 4975 | {.nodetype = LYS_CONTAINER, |
| 4976 | .module = ctx->mod, |
| 4977 | .flags = parent ? parent->flags : 0, |
| 4978 | .child = NULL, .next = NULL, |
| 4979 | .prev = (struct lysc_node *)&context_node_fake, |
| 4980 | .actions = NULL, .notifs = NULL}; |
| 4981 | struct lysp_grp *grp = NULL; |
| 4982 | LY_ARRAY_COUNT_TYPE u; |
| 4983 | uint32_t grp_stack_count; |
| 4984 | struct lys_module *grp_mod, *mod_old; |
| 4985 | LY_ERR ret = LY_SUCCESS; |
| 4986 | struct lysc_when **when, *when_shared; |
| 4987 | struct lysp_augment **augments = NULL; |
| 4988 | LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0; |
| 4989 | struct lysc_notif **notifs = NULL; |
| 4990 | struct lysc_action **actions = NULL; |
| 4991 | |
| 4992 | /* find the referenced grouping */ |
| 4993 | LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod)); |
| 4994 | |
| 4995 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4996 | grp_stack_count = ctx->groupings.count; |
| 4997 | LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL)); |
| 4998 | if (grp_stack_count == ctx->groupings.count) { |
| 4999 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 5000 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5001 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 5002 | return LY_EVALID; |
| 5003 | } |
| 5004 | |
| 5005 | /* switch context's mod_def */ |
| 5006 | mod_old = ctx->mod_def; |
| 5007 | ctx->mod_def = grp_mod; |
| 5008 | |
| 5009 | /* check status */ |
| 5010 | ret = lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, grp_mod, grp->name); |
| 5011 | LY_CHECK_GOTO(ret, cleanup); |
| 5012 | |
| 5013 | /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly |
| 5014 | * applu refine and augment only to the nodes from the uses */ |
| 5015 | if (parent) { |
| 5016 | child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 5017 | } else if (ctx->mod->compiled->data) { |
| 5018 | child = ctx->mod->compiled->data; |
| 5019 | } else { |
| 5020 | child = NULL; |
| 5021 | } |
| 5022 | /* remember the last child */ |
| 5023 | if (child) { |
| 5024 | child = child->prev; |
| 5025 | } |
| 5026 | |
| 5027 | /* compile data nodes */ |
| 5028 | LY_LIST_FOR(grp->data, node_p) { |
| 5029 | /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */ |
| 5030 | ret = lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3); |
| 5031 | LY_CHECK_GOTO(ret, cleanup); |
| 5032 | } |
| 5033 | |
| 5034 | /* split the children and add the uses's data into the fake context node */ |
| 5035 | if (child) { |
| 5036 | context_node_fake.child = child->next; |
| 5037 | } else if (parent) { |
| 5038 | context_node_fake.child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 5039 | } else if (ctx->mod->compiled->data) { |
| 5040 | context_node_fake.child = ctx->mod->compiled->data; |
| 5041 | } |
| 5042 | if (context_node_fake.child) { |
| 5043 | /* remember child as the last data node added by grouping to fix the list later */ |
| 5044 | child = context_node_fake.child->prev; |
| 5045 | context_node_fake.child->prev = NULL; |
| 5046 | } |
| 5047 | |
| 5048 | when_shared = NULL; |
| 5049 | LY_LIST_FOR(context_node_fake.child, iter) { |
| 5050 | iter->parent = (struct lysc_node *)&context_node_fake; |
| 5051 | |
| 5052 | /* pass uses's when to all the data children, actions and notifications are ignored */ |
| 5053 | if (uses_p->when) { |
| 5054 | LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup); |
| 5055 | if (!when_shared) { |
| 5056 | ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when); |
| 5057 | LY_CHECK_GOTO(ret, cleanup); |
| 5058 | |
| 5059 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5060 | /* do not check "when" semantics in a grouping */ |
| 5061 | ret = ly_set_add(&ctx->xpath, iter, 0, NULL); |
| 5062 | LY_CHECK_GOTO(ret, cleanup); |
| 5063 | } |
| 5064 | |
| 5065 | when_shared = *when; |
| 5066 | } else { |
| 5067 | ++when_shared->refcount; |
| 5068 | (*when) = when_shared; |
| 5069 | |
| 5070 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5071 | /* in this case check "when" again for all children because of dummy node check */ |
| 5072 | ret = ly_set_add(&ctx->xpath, iter, 0, NULL); |
| 5073 | LY_CHECK_GOTO(ret, cleanup); |
| 5074 | } |
| 5075 | } |
| 5076 | } |
| 5077 | } |
| 5078 | |
| 5079 | /* compile actions */ |
| 5080 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 5081 | if (actions) { |
| 5082 | actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0; |
| 5083 | COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup); |
| 5084 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 5085 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 5086 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup); |
| 5087 | LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index; |
| 5088 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions); |
| 5089 | } |
| 5090 | } |
| 5091 | |
| 5092 | /* compile notifications */ |
| 5093 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 5094 | if (notifs) { |
| 5095 | notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0; |
| 5096 | COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup); |
| 5097 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 5098 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 5099 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup); |
| 5100 | LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index; |
| 5101 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs); |
| 5102 | } |
| 5103 | } |
| 5104 | |
| 5105 | /* sort and apply augments */ |
| 5106 | ret = lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments); |
| 5107 | LY_CHECK_GOTO(ret, cleanup); |
| 5108 | LY_ARRAY_FOR(augments, u) { |
| 5109 | ret = lys_compile_augment(ctx, augments[u], (struct lysc_node *)&context_node_fake); |
| 5110 | LY_CHECK_GOTO(ret, cleanup); |
| 5111 | } |
| 5112 | |
| 5113 | /* reload previous context's mod_def */ |
| 5114 | ctx->mod_def = mod_old; |
| 5115 | |
| 5116 | /* apply all refines */ |
| 5117 | ret = lys_compile_refines(ctx, uses_p->refines, (struct lysc_node *)&context_node_fake); |
| 5118 | LY_CHECK_GOTO(ret, cleanup); |
| 5119 | |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5120 | if (first_p) { |
| 5121 | *first_p = context_node_fake.child; |
| 5122 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5123 | |
| 5124 | cleanup: |
| 5125 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5126 | if (context_node_fake.child) { |
| 5127 | context_node_fake.child->prev = child; |
| 5128 | } |
| 5129 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5130 | child->parent = parent; |
| 5131 | } |
| 5132 | |
| 5133 | if (uses_p->augments || uses_p->refines) { |
| 5134 | /* 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] | 5135 | if (context_node_fake.actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5136 | 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] | 5137 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5138 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5139 | if (context_node_fake.notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5140 | 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] | 5141 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5142 | } |
| 5143 | } |
| 5144 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5145 | /* reload previous context's mod_def */ |
| 5146 | ctx->mod_def = mod_old; |
| 5147 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5148 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5149 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5150 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5151 | |
| 5152 | return ret; |
| 5153 | } |
| 5154 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5155 | static int |
| 5156 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5157 | { |
| 5158 | struct lysp_node *iter; |
| 5159 | int len = 0; |
| 5160 | |
| 5161 | *path = NULL; |
| 5162 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5163 | char *s = *path; |
| 5164 | char *id; |
| 5165 | |
| 5166 | switch (iter->nodetype) { |
| 5167 | case LYS_USES: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5168 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5169 | break; |
| 5170 | case LYS_GROUPING: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5171 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5172 | break; |
| 5173 | case LYS_AUGMENT: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5174 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5175 | break; |
| 5176 | default: |
| 5177 | id = strdup(iter->name); |
| 5178 | break; |
| 5179 | } |
| 5180 | |
| 5181 | if (!iter->parent) { |
| 5182 | /* print prefix */ |
| 5183 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5184 | } else { |
| 5185 | /* prefix is the same as in parent */ |
| 5186 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5187 | } |
| 5188 | free(s); |
| 5189 | free(id); |
| 5190 | } |
| 5191 | |
| 5192 | if (len < 0) { |
| 5193 | free(*path); |
| 5194 | *path = NULL; |
| 5195 | } else if (len == 0) { |
| 5196 | *path = strdup("/"); |
| 5197 | len = 1; |
| 5198 | } |
| 5199 | return len; |
| 5200 | } |
| 5201 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5202 | /** |
| 5203 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5204 | * |
| 5205 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5206 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5207 | */ |
| 5208 | static LY_ERR |
| 5209 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5210 | { |
| 5211 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5212 | char *path; |
| 5213 | int len; |
| 5214 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5215 | struct lysp_node_uses fake_uses = { |
| 5216 | .parent = node_p, |
| 5217 | .nodetype = LYS_USES, |
| 5218 | .flags = 0, .next = NULL, |
| 5219 | .name = grp->name, |
| 5220 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5221 | .refines = NULL, .augments = NULL |
| 5222 | }; |
| 5223 | struct lysc_node_container fake_container = { |
| 5224 | .nodetype = LYS_CONTAINER, |
| 5225 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5226 | .module = ctx->mod, |
| 5227 | .sp = NULL, .parent = NULL, .next = NULL, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5228 | .prev = (struct lysc_node *)&fake_container, |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5229 | .name = "fake", |
| 5230 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5231 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5232 | }; |
| 5233 | |
| 5234 | if (grp->parent) { |
| 5235 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5236 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5237 | |
| 5238 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5239 | if (len < 0) { |
| 5240 | LOGMEM(ctx->ctx); |
| 5241 | return LY_EMEM; |
| 5242 | } |
| 5243 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5244 | ctx->path_len = (uint32_t)len; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5245 | free(path); |
| 5246 | |
| 5247 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5248 | lysc_update_path(ctx, NULL, grp->name); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5249 | 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] | 5250 | lysc_update_path(ctx, NULL, NULL); |
| 5251 | lysc_update_path(ctx, NULL, NULL); |
| 5252 | |
| 5253 | ctx->path_len = 1; |
| 5254 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5255 | |
| 5256 | /* cleanup */ |
| 5257 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5258 | |
| 5259 | return ret; |
| 5260 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5261 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5262 | /** |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5263 | * @brief Set config flags for a node. |
| 5264 | * |
| 5265 | * @param[in] ctx Compile context. |
| 5266 | * @param[in] node Compiled node config to set. |
| 5267 | * @param[in] parent Parent of @p node. |
| 5268 | * @return LY_ERR value. |
| 5269 | */ |
| 5270 | static LY_ERR |
| 5271 | lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent) |
| 5272 | { |
| 5273 | if (node->nodetype == LYS_CASE) { |
| 5274 | /* case never has any config */ |
| 5275 | assert(!(node->flags & LYS_CONFIG_MASK)); |
| 5276 | return LY_SUCCESS; |
| 5277 | } |
| 5278 | |
| 5279 | /* adjust parent to always get the ancestor with config */ |
| 5280 | if (parent && (parent->nodetype == LYS_CASE)) { |
| 5281 | parent = parent->parent; |
| 5282 | assert(parent); |
| 5283 | } |
| 5284 | |
| 5285 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
| 5286 | /* ignore config statements inside RPC/action data */ |
| 5287 | node->flags &= ~LYS_CONFIG_MASK; |
| 5288 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5289 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
| 5290 | /* ignore config statements inside Notification data */ |
| 5291 | node->flags &= ~LYS_CONFIG_MASK; |
| 5292 | node->flags |= LYS_CONFIG_R; |
| 5293 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
| 5294 | /* config not explicitely set, inherit it from parent */ |
| 5295 | if (parent) { |
| 5296 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5297 | } else { |
| 5298 | /* default is config true */ |
| 5299 | node->flags |= LYS_CONFIG_W; |
| 5300 | } |
| 5301 | } else { |
| 5302 | /* config set explicitely */ |
| 5303 | node->flags |= LYS_SET_CONFIG; |
| 5304 | } |
| 5305 | |
| 5306 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5307 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5308 | "Configuration node cannot be child of any state data node."); |
| 5309 | return LY_EVALID; |
| 5310 | } |
| 5311 | |
| 5312 | return LY_SUCCESS; |
| 5313 | } |
| 5314 | |
| 5315 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5316 | * @brief Compile parsed schema node information. |
| 5317 | * @param[in] ctx Compile context |
| 5318 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5319 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5320 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5321 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5322 | * @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). |
| 5323 | * 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] | 5324 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5325 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5326 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5327 | 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] | 5328 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5329 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5330 | struct lysc_node *node = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5331 | struct lysc_when **when; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5332 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5333 | 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] | 5334 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5335 | if (node_p->nodetype != LYS_USES) { |
| 5336 | lysc_update_path(ctx, parent, node_p->name); |
| 5337 | } else { |
| 5338 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5339 | lysc_update_path(ctx, NULL, node_p->name); |
| 5340 | } |
| 5341 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5342 | switch (node_p->nodetype) { |
| 5343 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5344 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5345 | node_compile_spec = lys_compile_node_container; |
| 5346 | break; |
| 5347 | case LYS_LEAF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5348 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5349 | node_compile_spec = lys_compile_node_leaf; |
| 5350 | break; |
| 5351 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5352 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5353 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5354 | break; |
| 5355 | case LYS_LEAFLIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5356 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5357 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5358 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5359 | case LYS_CHOICE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5360 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5361 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5362 | break; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5363 | case LYS_CASE: |
| 5364 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case)); |
| 5365 | node_compile_spec = lys_compile_node_case; |
| 5366 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5367 | case LYS_ANYXML: |
| 5368 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5369 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5370 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5371 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5372 | case LYS_USES: |
Michal Vasko | 9e8de2d | 2020-09-01 08:17:10 +0200 | [diff] [blame] | 5373 | ret = lys_compile_uses(ctx, (struct lysp_node_uses *)node_p, parent, &node); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5374 | lysc_update_path(ctx, NULL, NULL); |
| 5375 | lysc_update_path(ctx, NULL, NULL); |
| 5376 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5377 | default: |
| 5378 | LOGINT(ctx->ctx); |
| 5379 | return LY_EINT; |
| 5380 | } |
| 5381 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5382 | node->nodetype = node_p->nodetype; |
| 5383 | node->module = ctx->mod; |
| 5384 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5385 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5386 | |
| 5387 | /* config */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5388 | ret = lys_compile_config(ctx, node, parent); |
| 5389 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5390 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5391 | /* list ordering */ |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5392 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5393 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5394 | 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] | 5395 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5396 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5397 | node->flags &= ~LYS_ORDBY_MASK; |
| 5398 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5399 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5400 | /* default ordering is system */ |
| 5401 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5402 | } |
| 5403 | } |
| 5404 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5405 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5406 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5407 | 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] | 5408 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5409 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5410 | node->sp = node_p; |
| 5411 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 5412 | DUP_STRING_GOTO(ctx->ctx, node_p->name, node->name, ret, error); |
| 5413 | DUP_STRING_GOTO(ctx->ctx, node_p->dsc, node->dsc, ret, error); |
| 5414 | DUP_STRING_GOTO(ctx->ctx, node_p->ref, node->ref, ret, error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5415 | if (node_p->when) { |
| 5416 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5417 | 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] | 5418 | |
| 5419 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5420 | /* do not check "when" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 5421 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 5422 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5423 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5424 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5425 | 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] | 5426 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5427 | /* insert into parent's children/compiled module (we can no longer free the node separately on error) */ |
| 5428 | LY_CHECK_RET(lys_compile_node_connect(ctx, parent, node)); |
| 5429 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5430 | /* nodetype-specific part */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5431 | LY_CHECK_RET(node_compile_spec(ctx, node_p, node)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5432 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5433 | /* final compilation tasks that require the node to be connected */ |
| 5434 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, done); |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5435 | if (node->flags & LYS_MAND_TRUE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5436 | /* inherit LYS_MAND_TRUE in parent containers */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5437 | lys_compile_mandatory_parents(parent, 1); |
| 5438 | } |
| 5439 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5440 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5441 | return LY_SUCCESS; |
| 5442 | |
| 5443 | error: |
| 5444 | lysc_node_free(ctx->ctx, node); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5445 | done: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5446 | return ret; |
| 5447 | } |
| 5448 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5449 | static void |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5450 | lysc_node_unlink(struct lysc_node *node) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5451 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5452 | struct lysc_node *parent, *child; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5453 | struct lysc_module *modc = node->module->compiled; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5454 | |
| 5455 | parent = node->parent; |
| 5456 | |
| 5457 | /* parent's first child */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5458 | if (parent && lysc_node_children(parent, node->flags) == node) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5459 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5460 | } else if (modc->data == node) { |
| 5461 | modc->data = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5462 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5463 | |
| 5464 | /* special choice case unlinking */ |
| 5465 | if (parent && parent->nodetype == LYS_CHOICE) { |
| 5466 | if (((struct lysc_node_choice *)parent)->dflt == (struct lysc_node_case *)node) { |
| 5467 | /* default case removed */ |
| 5468 | ((struct lysc_node_choice *)parent)->dflt = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5469 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5470 | } |
| 5471 | |
| 5472 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5473 | if (node->prev->next) { |
| 5474 | node->prev->next = node->next; |
| 5475 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5476 | if (node->next) { |
| 5477 | node->next->prev = node->prev; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5478 | } else { |
| 5479 | /* last child */ |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5480 | if (parent) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5481 | child = (struct lysc_node *)lysc_node_children(parent, node->flags); |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5482 | } else { |
| 5483 | child = modc->data; |
| 5484 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5485 | if (child) { |
| 5486 | child->prev = node->prev; |
| 5487 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5488 | } |
| 5489 | } |
| 5490 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 5491 | struct lysc_deviation { |
| 5492 | const char *nodeid; |
| 5493 | struct lysc_node *target; /* target node of the deviation */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5494 | 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] | 5495 | uint16_t flags; /* target's flags from lysc_resolve_schema_nodeid() */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5496 | ly_bool not_supported; /* flag if deviates contains not-supported deviate */ |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 5497 | }; |
| 5498 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5499 | /* MACROS for deviates checking */ |
| 5500 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5501 | if (!(target->nodetype & (NODETYPES))) { \ |
| 5502 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\ |
| 5503 | goto cleanup; \ |
| 5504 | } |
| 5505 | |
| 5506 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5507 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
| 5508 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
| 5509 | lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
| 5510 | goto cleanup; \ |
| 5511 | } |
| 5512 | |
| 5513 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5514 | if (!((TYPE)target)->MEMBER || COND) { \ |
| 5515 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
| 5516 | goto cleanup; \ |
| 5517 | } |
| 5518 | |
| 5519 | /** |
| 5520 | * @brief Apply deviate add. |
| 5521 | * |
| 5522 | * @param[in] ctx Compile context. |
| 5523 | * @param[in] target Deviation target. |
| 5524 | * @param[in] dev_flags Internal deviation flags. |
| 5525 | * @param[in] d Deviate add to apply. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5526 | * @return LY_ERR value. |
| 5527 | */ |
| 5528 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5529 | lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysc_node *target, uint32_t dev_flags, struct lysp_deviate_add *d) |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5530 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 5531 | LY_ERR ret = LY_EVALID, rc = LY_SUCCESS; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5532 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 5533 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5534 | LY_ARRAY_COUNT_TYPE x; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5535 | |
| 5536 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5537 | if (((TYPE)target)->MEMBER COND) { \ |
| 5538 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5539 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5540 | PROPERTY, ((TYPE)target)->MEMBER); \ |
| 5541 | goto cleanup; \ |
| 5542 | } |
| 5543 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5544 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5545 | if (((TYPE)target)->MEMBER && (COND)) { \ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5546 | ly_bool dynamic_ = 0; const char *val_; \ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5547 | 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] | 5548 | ctx->mod_def, &dynamic_); \ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5549 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5550 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5551 | if (dynamic_) {free((void*)val_);} \ |
| 5552 | goto cleanup; \ |
| 5553 | } |
| 5554 | |
| 5555 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
| 5556 | if (((TYPE)target)->MEMBER && (COND)) { \ |
| 5557 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5558 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5559 | PROPERTY, ((TYPE)target)->VALUEMEMBER); \ |
| 5560 | goto cleanup; \ |
| 5561 | } |
| 5562 | |
| 5563 | /* [units-stmt] */ |
| 5564 | if (d->units) { |
| 5565 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5566 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units); |
| 5567 | |
| 5568 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 5569 | DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units, rc); |
| 5570 | LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5571 | } |
| 5572 | |
| 5573 | /* *must-stmt */ |
| 5574 | if (d->musts) { |
| 5575 | switch (target->nodetype) { |
| 5576 | case LYS_CONTAINER: |
| 5577 | case LYS_LIST: |
| 5578 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts, |
| 5579 | x, lys_compile_must, ret, cleanup); |
| 5580 | break; |
| 5581 | case LYS_LEAF: |
| 5582 | case LYS_LEAFLIST: |
| 5583 | case LYS_ANYDATA: |
| 5584 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts, |
| 5585 | x, lys_compile_must, ret, cleanup); |
| 5586 | break; |
| 5587 | case LYS_NOTIF: |
| 5588 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts, |
| 5589 | x, lys_compile_must, ret, cleanup); |
| 5590 | break; |
| 5591 | case LYS_RPC: |
| 5592 | case LYS_ACTION: |
| 5593 | if (dev_flags & LYSC_OPT_RPC_INPUT) { |
| 5594 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts, |
| 5595 | x, lys_compile_must, ret, cleanup); |
| 5596 | break; |
| 5597 | } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) { |
| 5598 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts, |
| 5599 | x, lys_compile_must, ret, cleanup); |
| 5600 | break; |
| 5601 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5602 | /* fall through */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5603 | default: |
| 5604 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5605 | lys_nodetype2str(target->nodetype), "add", "must"); |
| 5606 | goto cleanup; |
| 5607 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 5608 | ret = ly_set_add(&ctx->xpath, target, 0, NULL); |
| 5609 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5610 | } |
| 5611 | |
| 5612 | /* *unique-stmt */ |
| 5613 | if (d->uniques) { |
| 5614 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5615 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup); |
| 5616 | } |
| 5617 | |
| 5618 | /* *default-stmt */ |
| 5619 | if (d->dflts) { |
| 5620 | switch (target->nodetype) { |
| 5621 | case LYS_LEAF: |
| 5622 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5623 | 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] | 5624 | if (leaf->dflt) { |
| 5625 | /* first, remove the default value taken from the type */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5626 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5627 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5628 | free(leaf->dflt); |
| 5629 | leaf->dflt = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5630 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5631 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5632 | /* store the default value in unres */ |
Michal Vasko | 31a82d5 | 2020-08-21 08:11:48 +0200 | [diff] [blame] | 5633 | 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] | 5634 | target->flags |= LYS_SET_DFLT; |
| 5635 | break; |
| 5636 | case LYS_LEAFLIST: |
| 5637 | if (llist->dflts && !(target->flags & LYS_SET_DFLT)) { |
| 5638 | /* first, remove the default value taken from the type */ |
| 5639 | LY_ARRAY_FOR(llist->dflts, x) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5640 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5641 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5642 | free(llist->dflts[x]); |
| 5643 | } |
| 5644 | LY_ARRAY_FREE(llist->dflts); |
| 5645 | llist->dflts = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5646 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5647 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5648 | /* store the default values in unres */ |
| 5649 | 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] | 5650 | target->flags |= LYS_SET_DFLT; |
| 5651 | break; |
| 5652 | case LYS_CHOICE: |
| 5653 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
| 5654 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name); |
| 5655 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5656 | * to allow making the default case even the augmented case from the deviating module */ |
| 5657 | if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) { |
| 5658 | goto cleanup; |
| 5659 | } |
| 5660 | break; |
| 5661 | default: |
| 5662 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5663 | lys_nodetype2str(target->nodetype), "add", "default"); |
| 5664 | goto cleanup; |
| 5665 | } |
| 5666 | } |
| 5667 | |
| 5668 | /* [config-stmt] */ |
| 5669 | if (d->flags & LYS_CONFIG_MASK) { |
| 5670 | if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 5671 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5672 | lys_nodetype2str(target->nodetype), "add", "config"); |
| 5673 | goto cleanup; |
| 5674 | } |
| 5675 | if (dev_flags) { |
| 5676 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 5677 | dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
| 5678 | } |
| 5679 | if (target->flags & LYS_SET_CONFIG) { |
| 5680 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5681 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5682 | target->flags & LYS_CONFIG_W ? "true" : "false"); |
| 5683 | goto cleanup; |
| 5684 | } |
| 5685 | LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup); |
| 5686 | } |
| 5687 | |
| 5688 | /* [mandatory-stmt] */ |
| 5689 | if (d->flags & LYS_MAND_MASK) { |
| 5690 | if (target->flags & LYS_MAND_MASK) { |
| 5691 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5692 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5693 | target->flags & LYS_MAND_TRUE ? "true" : "false"); |
| 5694 | goto cleanup; |
| 5695 | } |
| 5696 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup); |
| 5697 | } |
| 5698 | |
| 5699 | /* [min-elements-stmt] */ |
| 5700 | if (d->flags & LYS_SET_MIN) { |
| 5701 | if (target->nodetype == LYS_LEAFLIST) { |
| 5702 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements"); |
| 5703 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5704 | ((struct lysc_node_leaflist *)target)->min = d->min; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5705 | } else if (target->nodetype == LYS_LIST) { |
| 5706 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements"); |
| 5707 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5708 | ((struct lysc_node_list *)target)->min = d->min; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5709 | } else { |
| 5710 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5711 | lys_nodetype2str(target->nodetype), "add", "min-elements"); |
| 5712 | goto cleanup; |
| 5713 | } |
| 5714 | if (d->min) { |
| 5715 | target->flags |= LYS_MAND_TRUE; |
| 5716 | } |
| 5717 | } |
| 5718 | |
| 5719 | /* [max-elements-stmt] */ |
| 5720 | if (d->flags & LYS_SET_MAX) { |
| 5721 | if (target->nodetype == LYS_LEAFLIST) { |
| 5722 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements"); |
| 5723 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5724 | ((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] | 5725 | } else if (target->nodetype == LYS_LIST) { |
| 5726 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements"); |
| 5727 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5728 | ((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] | 5729 | } else { |
| 5730 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5731 | lys_nodetype2str(target->nodetype), "add", "max-elements"); |
| 5732 | goto cleanup; |
| 5733 | } |
| 5734 | } |
| 5735 | |
| 5736 | ret = LY_SUCCESS; |
| 5737 | |
| 5738 | cleanup: |
| 5739 | return ret; |
| 5740 | } |
| 5741 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5742 | static LY_ERR |
| 5743 | lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt) |
| 5744 | { |
| 5745 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5746 | ly_bool dyn = 0; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5747 | const char *orig_dflt; |
| 5748 | uint32_t i; |
| 5749 | |
| 5750 | if (target->module != ctx->mod) { |
| 5751 | /* foreign deviation */ |
| 5752 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt); |
| 5753 | |
| 5754 | /* check that the value matches */ |
| 5755 | orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn); |
| 5756 | if (strcmp(orig_dflt, dflt)) { |
| 5757 | goto error; |
| 5758 | } |
| 5759 | if (dyn) { |
| 5760 | free((char *)orig_dflt); |
| 5761 | } |
| 5762 | |
| 5763 | /* remove the default specification */ |
| 5764 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5765 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5766 | free(leaf->dflt); |
| 5767 | leaf->dflt = NULL; |
| 5768 | } else { |
| 5769 | /* local deviation */ |
| 5770 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt); |
| 5771 | |
| 5772 | /* find the incomplete default */ |
| 5773 | orig_dflt = NULL; |
| 5774 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 5775 | if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) { |
| 5776 | orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt; |
| 5777 | break; |
| 5778 | } |
| 5779 | } |
| 5780 | LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT); |
| 5781 | |
| 5782 | /* check that the value matches */ |
| 5783 | if (strcmp(orig_dflt, dflt)) { |
| 5784 | goto error; |
| 5785 | } |
| 5786 | |
| 5787 | /* update the list of incomplete default values */ |
| 5788 | lysc_incomplete_dflt_remove(ctx, target); |
| 5789 | } |
| 5790 | |
| 5791 | target->flags &= ~LYS_SET_DFLT; |
| 5792 | return LY_SUCCESS; |
| 5793 | |
| 5794 | error: |
| 5795 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5796 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 5797 | dflt, orig_dflt); |
| 5798 | if (dyn) { |
| 5799 | free((char *)orig_dflt); |
| 5800 | } |
| 5801 | cleanup: |
| 5802 | return LY_EVALID; |
| 5803 | } |
| 5804 | |
| 5805 | static LY_ERR |
| 5806 | lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts) |
| 5807 | { |
| 5808 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5809 | ly_bool dyn = 0, found; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5810 | const char *orig_dflt, **orig_dflts; |
| 5811 | uint32_t i; |
| 5812 | LY_ARRAY_COUNT_TYPE x, y; |
| 5813 | |
| 5814 | if (target->module != ctx->mod) { |
| 5815 | /* foreign deviation */ |
| 5816 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]); |
| 5817 | LY_ARRAY_FOR(dflts, x) { |
| 5818 | found = 0; |
| 5819 | LY_ARRAY_FOR(llist->dflts, y) { |
| 5820 | orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn); |
| 5821 | if (!strcmp(orig_dflt, dflts[x])) { |
| 5822 | if (dyn) { |
| 5823 | free((char *)orig_dflt); |
| 5824 | } |
| 5825 | found = 1; |
| 5826 | break; |
| 5827 | } |
| 5828 | if (dyn) { |
| 5829 | free((char *)orig_dflt); |
| 5830 | } |
| 5831 | } |
| 5832 | if (!found) { |
| 5833 | goto error; |
| 5834 | } |
| 5835 | |
| 5836 | /* update compiled default values */ |
| 5837 | LY_ARRAY_DECREMENT(llist->dflts); |
| 5838 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 5839 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 5840 | free(llist->dflts[y]); |
| 5841 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts)); |
| 5842 | } |
| 5843 | if (!LY_ARRAY_COUNT(llist->dflts)) { |
| 5844 | LY_ARRAY_FREE(llist->dflts); |
| 5845 | llist->dflts = NULL; |
| 5846 | llist->flags &= ~LYS_SET_DFLT; |
| 5847 | } |
| 5848 | } else { |
| 5849 | /* local deviation */ |
| 5850 | orig_dflt = NULL; |
| 5851 | orig_dflts = NULL; |
| 5852 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 5853 | if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) { |
| 5854 | orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt; |
| 5855 | orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts; |
| 5856 | break; |
| 5857 | } |
| 5858 | } |
| 5859 | LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT); |
| 5860 | |
| 5861 | if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) { |
| 5862 | /* TODO it is not currently possible to remove just one default value from incomplete defaults array */ |
| 5863 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5864 | "Local deviation deleting leaf-list defaults is not supported."); |
| 5865 | return LY_EVALID; |
| 5866 | } |
| 5867 | |
| 5868 | LY_ARRAY_FOR(dflts, x) { |
| 5869 | found = 0; |
| 5870 | if (orig_dflts) { |
| 5871 | LY_ARRAY_FOR(orig_dflts, y) { |
| 5872 | if (!strcmp(orig_dflts[y], dflts[x])) { |
| 5873 | found = 1; |
| 5874 | break; |
| 5875 | } |
| 5876 | } |
| 5877 | } else if (!strcmp(orig_dflt, dflts[x])) { |
| 5878 | found = 1; |
| 5879 | } |
| 5880 | if (!found) { |
| 5881 | goto error; |
| 5882 | } |
| 5883 | |
| 5884 | /* update the list of incomplete default values */ |
| 5885 | lysc_incomplete_dflt_remove(ctx, target); |
| 5886 | } |
| 5887 | |
| 5888 | llist->flags &= ~LYS_SET_DFLT; |
| 5889 | } |
| 5890 | |
| 5891 | return LY_SUCCESS; |
| 5892 | |
| 5893 | error: |
| 5894 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 5895 | "which does not match any of the target's property values.", dflts[x]); |
| 5896 | cleanup: |
| 5897 | return LY_EVALID; |
| 5898 | } |
| 5899 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5900 | /** |
| 5901 | * @brief Apply deviate delete. |
| 5902 | * |
| 5903 | * @param[in] ctx Compile context. |
| 5904 | * @param[in] target Deviation target. |
| 5905 | * @param[in] dev_flags Internal deviation flags. |
| 5906 | * @param[in] d Deviate delete to apply. |
| 5907 | * @return LY_ERR value. |
| 5908 | */ |
| 5909 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5910 | lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysc_node *target, uint32_t dev_flags, struct lysp_deviate_del *d) |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5911 | { |
| 5912 | LY_ERR ret = LY_EVALID; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5913 | struct lysc_node_list *list = (struct lysc_node_list *)target; |
| 5914 | LY_ARRAY_COUNT_TYPE x, y, z; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5915 | size_t prefix_len, name_len; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5916 | const char *prefix, *name, *nodeid; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5917 | struct lys_module *mod; |
| 5918 | |
| 5919 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5920 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \ |
| 5921 | LY_ARRAY_FOR(d->ARRAY_DEV, x) { \ |
| 5922 | LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \ |
| 5923 | if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \ |
| 5924 | } \ |
| 5925 | if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \ |
| 5926 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5927 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5928 | PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \ |
| 5929 | goto cleanup; \ |
| 5930 | } \ |
| 5931 | LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \ |
| 5932 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \ |
| 5933 | memmove(&((TYPE)target)->ARRAY_TRG[y], \ |
| 5934 | &((TYPE)target)->ARRAY_TRG[y + 1], \ |
| 5935 | (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \ |
| 5936 | } \ |
| 5937 | if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \ |
| 5938 | LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \ |
| 5939 | ((TYPE)target)->ARRAY_TRG = NULL; \ |
| 5940 | } |
| 5941 | |
| 5942 | /* [units-stmt] */ |
| 5943 | if (d->units) { |
| 5944 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5945 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, 0, units, "deleting", "units", d->units); |
| 5946 | if (strcmp(((struct lysc_node_leaf *)target)->units, d->units)) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5947 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5948 | "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] | 5949 | d->units, ((struct lysc_node_leaf *)target)->units); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5950 | goto cleanup; |
| 5951 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5952 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf *)target)->units); |
| 5953 | ((struct lysc_node_leaf *)target)->units = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5954 | } |
| 5955 | |
| 5956 | /* *must-stmt */ |
| 5957 | if (d->musts) { |
| 5958 | switch (target->nodetype) { |
| 5959 | case LYS_CONTAINER: |
| 5960 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5961 | 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] | 5962 | break; |
| 5963 | case LYS_LEAF: |
| 5964 | case LYS_LEAFLIST: |
| 5965 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5966 | 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] | 5967 | break; |
| 5968 | case LYS_NOTIF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5969 | 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] | 5970 | break; |
| 5971 | case LYS_RPC: |
| 5972 | case LYS_ACTION: |
| 5973 | if (dev_flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5974 | 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] | 5975 | break; |
| 5976 | } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5977 | 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] | 5978 | break; |
| 5979 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5980 | /* fall through */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5981 | default: |
| 5982 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5983 | lys_nodetype2str(target->nodetype), "delete", "must"); |
| 5984 | goto cleanup; |
| 5985 | } |
| 5986 | } |
| 5987 | |
| 5988 | /* *unique-stmt */ |
| 5989 | if (d->uniques) { |
| 5990 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 5991 | LY_ARRAY_FOR(d->uniques, x) { |
| 5992 | LY_ARRAY_FOR(list->uniques, z) { |
| 5993 | for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 5994 | nodeid = strpbrk(name, " \t\n"); |
| 5995 | if (nodeid) { |
| 5996 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
| 5997 | break; |
| 5998 | } |
| 5999 | while (isspace(*nodeid)) { |
| 6000 | ++nodeid; |
| 6001 | } |
| 6002 | } else { |
| 6003 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6004 | break; |
| 6005 | } |
| 6006 | } |
| 6007 | } |
| 6008 | if (!name) { |
| 6009 | /* complete match - remove the unique */ |
| 6010 | LY_ARRAY_DECREMENT(list->uniques); |
| 6011 | LY_ARRAY_FREE(list->uniques[z]); |
| 6012 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques)); |
| 6013 | --z; |
| 6014 | break; |
| 6015 | } |
| 6016 | } |
| 6017 | if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) { |
| 6018 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6019 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6020 | d->uniques[x]); |
| 6021 | goto cleanup; |
| 6022 | } |
| 6023 | } |
| 6024 | if (!LY_ARRAY_COUNT(list->uniques)) { |
| 6025 | LY_ARRAY_FREE(list->uniques); |
| 6026 | list->uniques = NULL; |
| 6027 | } |
| 6028 | } |
| 6029 | |
| 6030 | /* *default-stmt */ |
| 6031 | if (d->dflts) { |
| 6032 | switch (target->nodetype) { |
| 6033 | case LYS_LEAF: |
| 6034 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6035 | 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] | 6036 | break; |
| 6037 | case LYS_LEAFLIST: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6038 | 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] | 6039 | break; |
| 6040 | case LYS_CHOICE: |
| 6041 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6042 | 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] | 6043 | nodeid = d->dflts[0]; |
| 6044 | LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup); |
| 6045 | if (prefix) { |
| 6046 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6047 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6048 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6049 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6050 | "The prefix does not match any imported module of the deviation module.", d->dflts[0]); |
| 6051 | goto cleanup; |
| 6052 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6053 | if (mod != ((struct lysc_node_choice *)target)->dflt->module) { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6054 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6055 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6056 | "The prefix does not match the default case's module.", d->dflts[0]); |
| 6057 | goto cleanup; |
| 6058 | } |
| 6059 | } |
| 6060 | /* else { |
| 6061 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6062 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6063 | * the module of the default case, just matching its name */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6064 | if (strcmp(name, ((struct lysc_node_choice *)target)->dflt->name)) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6065 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6066 | "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] | 6067 | d->dflts[0], ((struct lysc_node_choice *)target)->dflt->name); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6068 | goto cleanup; |
| 6069 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6070 | ((struct lysc_node_choice *)target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6071 | ((struct lysc_node_choice *)target)->dflt = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6072 | break; |
| 6073 | default: |
| 6074 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6075 | lys_nodetype2str(target->nodetype), "delete", "default"); |
| 6076 | goto cleanup; |
| 6077 | } |
| 6078 | } |
| 6079 | |
| 6080 | ret = LY_SUCCESS; |
| 6081 | |
| 6082 | cleanup: |
| 6083 | return ret; |
| 6084 | } |
| 6085 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6086 | static LY_ERR |
| 6087 | lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target) |
| 6088 | { |
| 6089 | LY_ERR ret; |
| 6090 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 6091 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 6092 | struct ly_err_item *err = NULL; |
| 6093 | LY_ARRAY_COUNT_TYPE x; |
| 6094 | const char *dflt; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6095 | ly_bool dyn; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6096 | |
| 6097 | if (target->module != ctx->mod) { |
| 6098 | /* foreign deviation */ |
| 6099 | if (target->nodetype == LYS_LEAF) { |
| 6100 | dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn); |
| 6101 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6102 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6103 | |
| 6104 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 6105 | LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err); |
| 6106 | if (dyn) { |
| 6107 | free((char *)dflt); |
| 6108 | } |
| 6109 | if (err) { |
| 6110 | ly_err_print(err); |
| 6111 | ctx->path[0] = '\0'; |
| 6112 | lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6113 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6114 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6115 | ly_err_free(err); |
| 6116 | } |
| 6117 | LY_CHECK_RET(ret); |
| 6118 | |
| 6119 | ++leaf->dflt->realtype->refcount; |
| 6120 | } else { /* LY_LEAFLIST */ |
| 6121 | LY_ARRAY_FOR(llist->dflts, x) { |
| 6122 | dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn); |
| 6123 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6124 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6125 | |
| 6126 | ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 6127 | LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err); |
| 6128 | if (dyn) { |
| 6129 | free((char *)dflt); |
| 6130 | } |
| 6131 | if (err) { |
| 6132 | ly_err_print(err); |
| 6133 | ctx->path[0] = '\0'; |
| 6134 | lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6135 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6136 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6137 | ly_err_free(err); |
| 6138 | } |
| 6139 | LY_CHECK_RET(ret); |
| 6140 | |
| 6141 | ++llist->dflts[x]->realtype->refcount; |
| 6142 | } |
| 6143 | } |
| 6144 | } else { |
| 6145 | /* local deviation */ |
| 6146 | |
| 6147 | /* these default were not compiled yet, so they will use the new type automatically */ |
| 6148 | } |
| 6149 | |
| 6150 | return LY_SUCCESS; |
| 6151 | } |
| 6152 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6153 | /** |
| 6154 | * @brief Apply deviate replace. |
| 6155 | * |
| 6156 | * @param[in] ctx Compile context. |
| 6157 | * @param[in] target Deviation target. |
| 6158 | * @param[in] d Deviate replace to apply. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6159 | * @return LY_ERR value. |
| 6160 | */ |
| 6161 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6162 | 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] | 6163 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 6164 | LY_ERR ret = LY_EVALID, rc = LY_SUCCESS; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6165 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 6166 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 6167 | LY_ARRAY_COUNT_TYPE x; |
| 6168 | |
| 6169 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 6170 | if (!(((TYPE)target)->MEMBER COND)) { \ |
| 6171 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 6172 | "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \ |
| 6173 | goto cleanup; \ |
| 6174 | } |
| 6175 | |
| 6176 | /* [type-stmt] */ |
| 6177 | if (d->type) { |
| 6178 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6179 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6180 | lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type); |
| 6181 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6182 | /* remove only default value inherited from the type */ |
| 6183 | if (!(target->flags & LYS_SET_DFLT)) { |
| 6184 | if (target->module != ctx->mod) { |
| 6185 | /* foreign deviation - the target has default from the previous type, remove it */ |
| 6186 | if (target->nodetype == LYS_LEAF) { |
| 6187 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6188 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6189 | free(leaf->dflt); |
| 6190 | leaf->dflt = NULL; |
| 6191 | } else { /* LYS_LEAFLIST */ |
| 6192 | LY_ARRAY_FOR(llist->dflts, x) { |
| 6193 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6194 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6195 | free(llist->dflts[x]); |
| 6196 | } |
| 6197 | LY_ARRAY_FREE(llist->dflts); |
| 6198 | llist->dflts = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6199 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6200 | } else { |
| 6201 | /* local deviation */ |
| 6202 | lysc_incomplete_dflt_remove(ctx, target); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6203 | } |
| 6204 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6205 | |
| 6206 | LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf)); |
| 6207 | |
| 6208 | if (target->flags & LYS_SET_DFLT) { |
| 6209 | /* the term default value(s) needs to be recompiled */ |
| 6210 | LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target)); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6211 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6212 | } |
| 6213 | |
| 6214 | /* [units-stmt] */ |
| 6215 | if (d->units) { |
| 6216 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6217 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS), |
| 6218 | units, "replacing", "units", d->units); |
| 6219 | |
| 6220 | lydict_remove(ctx->ctx, leaf->units); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 6221 | DUP_STRING(ctx->ctx, d->units, leaf->units, rc); |
| 6222 | LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6223 | } |
| 6224 | |
| 6225 | /* [default-stmt] */ |
| 6226 | if (d->dflt) { |
| 6227 | switch (target->nodetype) { |
| 6228 | case LYS_LEAF: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6229 | if (target->module != ctx->mod) { |
| 6230 | /* foreign deviation */ |
| 6231 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing", |
| 6232 | "default", d->dflt); |
| 6233 | |
| 6234 | /* remove the default specification */ |
| 6235 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6236 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6237 | free(leaf->dflt); |
| 6238 | leaf->dflt = NULL; |
| 6239 | } else { |
| 6240 | /* local deviation */ |
| 6241 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing", |
| 6242 | "default", d->dflt); |
| 6243 | assert(!leaf->dflt); |
| 6244 | } |
| 6245 | |
| 6246 | /* store the new default value */ |
| 6247 | 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] | 6248 | break; |
| 6249 | case LYS_CHOICE: |
| 6250 | DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt); |
| 6251 | if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) { |
| 6252 | goto cleanup; |
| 6253 | } |
| 6254 | break; |
| 6255 | default: |
| 6256 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6257 | lys_nodetype2str(target->nodetype), "replace", "default"); |
| 6258 | goto cleanup; |
| 6259 | } |
| 6260 | } |
| 6261 | |
| 6262 | /* [config-stmt] */ |
| 6263 | if (d->flags & LYS_CONFIG_MASK) { |
| 6264 | if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 6265 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6266 | lys_nodetype2str(target->nodetype), "replace", "config"); |
| 6267 | goto cleanup; |
| 6268 | } |
| 6269 | if (!(target->flags & LYS_SET_CONFIG)) { |
| 6270 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6271 | "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6272 | goto cleanup; |
| 6273 | } |
| 6274 | LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup); |
| 6275 | } |
| 6276 | |
| 6277 | /* [mandatory-stmt] */ |
| 6278 | if (d->flags & LYS_MAND_MASK) { |
| 6279 | if (!(target->flags & LYS_MAND_MASK)) { |
| 6280 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6281 | "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6282 | goto cleanup; |
| 6283 | } |
| 6284 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup); |
| 6285 | } |
| 6286 | |
| 6287 | /* [min-elements-stmt] */ |
| 6288 | if (d->flags & LYS_SET_MIN) { |
| 6289 | if (target->nodetype == LYS_LEAFLIST) { |
| 6290 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements"); |
| 6291 | /* change value */ |
| 6292 | ((struct lysc_node_leaflist *)target)->min = d->min; |
| 6293 | } else if (target->nodetype == LYS_LIST) { |
| 6294 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements"); |
| 6295 | /* change value */ |
| 6296 | ((struct lysc_node_list *)target)->min = d->min; |
| 6297 | } else { |
| 6298 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6299 | lys_nodetype2str(target->nodetype), "replace", "min-elements"); |
| 6300 | goto cleanup; |
| 6301 | } |
| 6302 | if (d->min) { |
| 6303 | target->flags |= LYS_MAND_TRUE; |
| 6304 | } |
| 6305 | } |
| 6306 | |
| 6307 | /* [max-elements-stmt] */ |
| 6308 | if (d->flags & LYS_SET_MAX) { |
| 6309 | if (target->nodetype == LYS_LEAFLIST) { |
| 6310 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements"); |
| 6311 | /* change value */ |
| 6312 | ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1; |
| 6313 | } else if (target->nodetype == LYS_LIST) { |
| 6314 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements"); |
| 6315 | /* change value */ |
| 6316 | ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1; |
| 6317 | } else { |
| 6318 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6319 | lys_nodetype2str(target->nodetype), "replace", "max-elements"); |
| 6320 | goto cleanup; |
| 6321 | } |
| 6322 | } |
| 6323 | |
| 6324 | ret = LY_SUCCESS; |
| 6325 | |
| 6326 | cleanup: |
| 6327 | return ret; |
| 6328 | } |
| 6329 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6330 | /** |
| 6331 | * @brief Apply all deviations of one target node. |
| 6332 | * |
| 6333 | * @param[in] ctx Compile context. |
| 6334 | * @param[in] dev Deviation structure to apply. |
| 6335 | * @return LY_ERR value. |
| 6336 | */ |
| 6337 | static LY_ERR |
| 6338 | lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6339 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6340 | LY_ERR ret = LY_EVALID; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6341 | struct lysc_node *target = dev->target; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6342 | struct lysc_action *rpcs; |
| 6343 | struct lysc_notif *notifs; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6344 | struct lysp_deviate *d; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6345 | LY_ARRAY_COUNT_TYPE v, x; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6346 | uint32_t min, max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6347 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6348 | lysc_update_path(ctx, NULL, dev->nodeid); |
| 6349 | |
| 6350 | /* not-supported */ |
| 6351 | if (dev->not_supported) { |
| 6352 | if (LY_ARRAY_COUNT(dev->deviates) > 1) { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 6353 | 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] | 6354 | LY_ARRAY_COUNT(dev->deviates), dev->nodeid); |
| 6355 | } |
| 6356 | |
| 6357 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 6358 | if (target->parent) { \ |
| 6359 | ARRAY = (TYPE*)GETFUNC(target->parent); \ |
| 6360 | } else { \ |
| 6361 | ARRAY = target->module->compiled->ARRAY; \ |
| 6362 | } \ |
| 6363 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 6364 | if (&ARRAY[x] == (TYPE*)target) { break; } \ |
| 6365 | } \ |
| 6366 | if (x < LY_ARRAY_COUNT(ARRAY)) { \ |
| 6367 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 6368 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 6369 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 6370 | } |
| 6371 | |
| 6372 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 6373 | if (dev->flags & LYSC_OPT_RPC_INPUT) { |
| 6374 | /* remove RPC's/action's input */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6375 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input); |
| 6376 | memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input); |
| 6377 | FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free); |
| 6378 | ((struct lysc_action *)target)->input_exts = NULL; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6379 | } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6380 | /* remove RPC's/action's output */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6381 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output); |
| 6382 | memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output); |
| 6383 | FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free); |
| 6384 | ((struct lysc_action *)target)->output_exts = NULL; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6385 | } else { |
| 6386 | /* remove RPC/action */ |
| 6387 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
| 6388 | } |
| 6389 | } else if (target->nodetype == LYS_NOTIF) { |
| 6390 | /* remove Notification */ |
| 6391 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
| 6392 | } else { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 6393 | if (target->parent && (target->parent->nodetype == LYS_CASE) && (target->prev == target)) { |
| 6394 | /* remove the target node with its parent case node because it is the only node of the case */ |
| 6395 | lysc_node_unlink(target->parent); |
| 6396 | lysc_node_free(ctx->ctx, target->parent); |
| 6397 | } else { |
| 6398 | /* remove the target node */ |
| 6399 | lysc_node_unlink(target); |
| 6400 | lysc_node_free(ctx->ctx, target); |
| 6401 | } |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6402 | } |
| 6403 | |
| 6404 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 6405 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
| 6406 | return LY_SUCCESS; |
| 6407 | } |
| 6408 | |
| 6409 | /* list of deviates (not-supported is not present in the list) */ |
| 6410 | LY_ARRAY_FOR(dev->deviates, v) { |
| 6411 | d = dev->deviates[v]; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6412 | switch (d->mod) { |
| 6413 | case LYS_DEV_ADD: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6414 | 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] | 6415 | break; |
| 6416 | case LYS_DEV_DELETE: |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6417 | 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] | 6418 | break; |
| 6419 | case LYS_DEV_REPLACE: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6420 | 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] | 6421 | break; |
| 6422 | default: |
| 6423 | LOGINT(ctx->ctx); |
| 6424 | goto cleanup; |
| 6425 | } |
| 6426 | } |
| 6427 | |
| 6428 | /* final check when all deviations of a single target node are applied */ |
| 6429 | |
| 6430 | /* check min-max compatibility */ |
| 6431 | if (target->nodetype == LYS_LEAFLIST) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6432 | min = ((struct lysc_node_leaflist *)target)->min; |
| 6433 | max = ((struct lysc_node_leaflist *)target)->max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6434 | } else if (target->nodetype == LYS_LIST) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6435 | min = ((struct lysc_node_list *)target)->min; |
| 6436 | max = ((struct lysc_node_list *)target)->max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6437 | } else { |
| 6438 | min = max = 0; |
| 6439 | } |
| 6440 | if (min > max) { |
| 6441 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements " |
| 6442 | "after deviation: min value %u is bigger than max value %u.", min, max); |
| 6443 | goto cleanup; |
| 6444 | } |
| 6445 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6446 | /* check mandatory - default compatibility */ |
| 6447 | if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT) |
| 6448 | && (target->flags & LYS_MAND_TRUE)) { |
| 6449 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6450 | "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype)); |
| 6451 | goto cleanup; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6452 | } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)target)->dflt |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6453 | && (target->flags & LYS_MAND_TRUE)) { |
| 6454 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice."); |
| 6455 | goto cleanup; |
| 6456 | } |
| 6457 | if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) { |
| 6458 | /* mandatory node under a default case */ |
| 6459 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6460 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6461 | lys_nodetype2str(target->nodetype), target->name, target->parent->name); |
| 6462 | goto cleanup; |
| 6463 | } |
| 6464 | |
| 6465 | /* success */ |
| 6466 | ret = LY_SUCCESS; |
| 6467 | |
| 6468 | cleanup: |
| 6469 | lysc_update_path(ctx, NULL, NULL); |
| 6470 | return ret; |
| 6471 | } |
| 6472 | |
| 6473 | LY_ERR |
| 6474 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
| 6475 | { |
| 6476 | LY_ERR ret = LY_EVALID; |
| 6477 | struct ly_set devs_p = {0}; |
| 6478 | struct ly_set targets = {0}; |
| 6479 | struct lysc_node *target; /* target target of the deviation */ |
| 6480 | struct lysp_deviation *dev; |
| 6481 | struct lysp_deviate *d, **dp_new; |
| 6482 | LY_ARRAY_COUNT_TYPE u, v; |
| 6483 | struct lysc_deviation **devs = NULL; |
| 6484 | struct lys_module *target_mod, **dev_mod; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6485 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6486 | |
| 6487 | /* get all deviations from the module and all its submodules ... */ |
| 6488 | LY_ARRAY_FOR(mod_p->deviations, u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6489 | LY_CHECK_RET(ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST, NULL)); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6490 | } |
| 6491 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 6492 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6493 | LY_CHECK_RET(ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST, NULL)); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6494 | } |
| 6495 | } |
| 6496 | if (!devs_p.count) { |
| 6497 | /* nothing to do */ |
| 6498 | return LY_SUCCESS; |
| 6499 | } |
| 6500 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6501 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 6502 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6503 | /* ... and group them by the target node */ |
| 6504 | devs = calloc(devs_p.count, sizeof *devs); |
| 6505 | for (u = 0; u < devs_p.count; ++u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6506 | uint32_t index; |
| 6507 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6508 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6509 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6510 | |
| 6511 | /* resolve the target */ |
Michal Vasko | cb18c7f | 2020-08-24 09:22:28 +0200 | [diff] [blame] | 6512 | 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] | 6513 | (const struct lysc_node **)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6514 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6515 | /* move the target pointer to input/output to make them different from the action and |
| 6516 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 6517 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 6518 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 6519 | if (flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6520 | target = (struct lysc_node *)&((struct lysc_action *)target)->input; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6521 | flags |= LYSC_OPT_INTERNAL; |
| 6522 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6523 | target = (struct lysc_node *)&((struct lysc_action *)target)->output; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6524 | flags |= LYSC_OPT_INTERNAL; |
| 6525 | } |
| 6526 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6527 | /* insert into the set of targets with duplicity detection */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6528 | ret = ly_set_add(&targets, target, 0, &index); |
| 6529 | LY_CHECK_GOTO(ret, cleanup); |
| 6530 | if (!devs[index]) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6531 | /* new record */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6532 | devs[index] = calloc(1, sizeof **devs); |
| 6533 | devs[index]->target = target; |
| 6534 | devs[index]->nodeid = dev->nodeid; |
| 6535 | devs[index]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6536 | } |
| 6537 | /* add deviates into the deviation's list of deviates */ |
Michal Vasko | 5c38f36 | 2020-08-24 09:22:51 +0200 | [diff] [blame] | 6538 | LY_LIST_FOR(dev->deviates, d) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6539 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[index]->deviates, dp_new, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6540 | *dp_new = d; |
| 6541 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6542 | devs[index]->not_supported = 1; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6543 | } |
| 6544 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6545 | |
| 6546 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6547 | } |
| 6548 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6549 | /* apply deviations */ |
| 6550 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6551 | ly_bool match = 0; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6552 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6553 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 6554 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 6555 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6556 | 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] | 6557 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6558 | 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] | 6559 | } |
| 6560 | } |
| 6561 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6562 | /* remember target module (the target node may be removed) */ |
| 6563 | target_mod = devs[u]->target->module; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6564 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6565 | /* apply the deviation */ |
| 6566 | LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6567 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6568 | /* 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] | 6569 | LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) { |
| 6570 | if (target_mod->compiled->deviated_by[v] == mod_p->mod) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6571 | match = 1; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6572 | break; |
| 6573 | } |
| 6574 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6575 | if (!match) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6576 | 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] | 6577 | *dev_mod = mod_p->mod; |
| 6578 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6579 | } |
| 6580 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6581 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6582 | ret = LY_SUCCESS; |
| 6583 | |
| 6584 | cleanup: |
| 6585 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6586 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6587 | free(devs[u]); |
| 6588 | } |
| 6589 | free(devs); |
| 6590 | ly_set_erase(&targets, NULL); |
| 6591 | ly_set_erase(&devs_p, NULL); |
| 6592 | |
| 6593 | return ret; |
| 6594 | } |
| 6595 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6596 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6597 | * @brief Compile the given YANG submodule into the main module. |
| 6598 | * @param[in] ctx Compile context |
| 6599 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6600 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6601 | */ |
| 6602 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6603 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6604 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6605 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6606 | LY_ERR ret = LY_SUCCESS; |
| 6607 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6608 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6609 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6610 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6611 | |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 6612 | /* features are compiled directly into the compiled module structure, |
| 6613 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6614 | * The features compilation is finished in the main module (lys_compile()). */ |
| 6615 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->mod->features); |
| 6616 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6617 | |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 6618 | ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->mod->identities); |
| 6619 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6620 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6621 | /* data nodes */ |
| 6622 | LY_LIST_FOR(submod->data, node_p) { |
| 6623 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6624 | LY_CHECK_GOTO(ret, error); |
| 6625 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6626 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6627 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6628 | 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] | 6629 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6630 | error: |
| 6631 | return ret; |
| 6632 | } |
| 6633 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6634 | static void * |
| 6635 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6636 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6637 | for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6638 | if (substmts[u].stmt == stmt) { |
| 6639 | return substmts[u].storage; |
| 6640 | } |
| 6641 | } |
| 6642 | return NULL; |
| 6643 | } |
| 6644 | |
| 6645 | LY_ERR |
| 6646 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6647 | { |
| 6648 | LY_ERR ret = LY_EVALID, r; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6649 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6650 | struct lysp_stmt *stmt; |
| 6651 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6652 | |
| 6653 | /* check for invalid substatements */ |
| 6654 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6655 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6656 | continue; |
| 6657 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6658 | for (u = 0; substmts[u].stmt; ++u) { |
| 6659 | if (substmts[u].stmt == stmt->kw) { |
| 6660 | break; |
| 6661 | } |
| 6662 | } |
| 6663 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6664 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6665 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6666 | goto cleanup; |
| 6667 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6668 | } |
| 6669 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6670 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6671 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6672 | /* keep order of the processing the same as the order in the defined substmts, |
| 6673 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6674 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6675 | ly_bool stmt_present = 0; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6676 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6677 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6678 | if (substmts[u].stmt != stmt->kw) { |
| 6679 | continue; |
| 6680 | } |
| 6681 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6682 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6683 | if (substmts[u].storage) { |
| 6684 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6685 | case LY_STMT_STATUS: |
| 6686 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6687 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6688 | break; |
| 6689 | case LY_STMT_UNITS: { |
| 6690 | const char **units; |
| 6691 | |
| 6692 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6693 | /* single item */ |
| 6694 | if (*((const char **)substmts[u].storage)) { |
| 6695 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6696 | goto cleanup; |
| 6697 | } |
| 6698 | units = (const char **)substmts[u].storage; |
| 6699 | } else { |
| 6700 | /* sized array */ |
| 6701 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6702 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6703 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 6704 | r = lydict_insert(ctx->ctx, stmt->arg, 0, units); |
| 6705 | LY_CHECK_ERR_GOTO(r, ret = r, cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6706 | break; |
| 6707 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6708 | case LY_STMT_TYPE: { |
| 6709 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6710 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6711 | |
| 6712 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6713 | /* single item */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6714 | if (*(struct lysc_type **)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6715 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6716 | goto cleanup; |
| 6717 | } |
| 6718 | compiled = substmts[u].storage; |
| 6719 | } else { |
| 6720 | /* sized array */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6721 | struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6722 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6723 | compiled = (void *)type; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6724 | } |
| 6725 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6726 | 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] | 6727 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL, |
| 6728 | 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] | 6729 | 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] | 6730 | lysp_type_free(ctx->ctx, parsed); |
| 6731 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6732 | break; |
| 6733 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6734 | case LY_STMT_IF_FEATURE: { |
| 6735 | struct lysc_iffeature *iff = NULL; |
| 6736 | |
| 6737 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6738 | /* single item */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6739 | if (((struct lysc_iffeature *)substmts[u].storage)->features) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6740 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6741 | goto cleanup; |
| 6742 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6743 | iff = (struct lysc_iffeature *)substmts[u].storage; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6744 | } else { |
| 6745 | /* sized array */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6746 | struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6747 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6748 | } |
| 6749 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6750 | break; |
| 6751 | } |
| 6752 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6753 | * 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] | 6754 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6755 | 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.", |
| 6756 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6757 | goto cleanup; |
| 6758 | } |
| 6759 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6760 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6761 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6762 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6763 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6764 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6765 | goto cleanup; |
| 6766 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6767 | } |
| 6768 | |
| 6769 | ret = LY_SUCCESS; |
| 6770 | |
| 6771 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6772 | return ret; |
| 6773 | } |
| 6774 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6775 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6776 | * @brief Check when for cyclic dependencies. |
| 6777 | * @param[in] set Set with all the referenced nodes. |
| 6778 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6779 | * @return LY_ERR value |
| 6780 | */ |
| 6781 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6782 | 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] | 6783 | { |
| 6784 | struct lyxp_set tmp_set; |
| 6785 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6786 | uint32_t i, j; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6787 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6788 | struct lysc_when *when; |
| 6789 | LY_ERR ret = LY_SUCCESS; |
| 6790 | |
| 6791 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6792 | |
| 6793 | /* prepare in_ctx of the set */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6794 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6795 | xp_scnode = &set->val.scnodes[i]; |
| 6796 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6797 | if (xp_scnode->in_ctx != -1) { |
| 6798 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6799 | xp_scnode->in_ctx = 1; |
| 6800 | } |
| 6801 | } |
| 6802 | |
| 6803 | for (i = 0; i < set->used; ++i) { |
| 6804 | xp_scnode = &set->val.scnodes[i]; |
| 6805 | if (xp_scnode->in_ctx != 1) { |
| 6806 | /* already checked */ |
| 6807 | continue; |
| 6808 | } |
| 6809 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6810 | 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] | 6811 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6812 | /* no when to check */ |
| 6813 | xp_scnode->in_ctx = 0; |
| 6814 | continue; |
| 6815 | } |
| 6816 | |
| 6817 | node = xp_scnode->scnode; |
| 6818 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6819 | LY_ARRAY_FOR(node->when, u) { |
| 6820 | when = node->when[u]; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6821 | ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6822 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 6823 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6824 | 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] | 6825 | goto cleanup; |
| 6826 | } |
| 6827 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6828 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6829 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6830 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6831 | /* try to find this node in our set */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6832 | uint32_t idx; |
| 6833 | if (lyxp_set_scnode_contains(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1, &idx) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6834 | 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] | 6835 | ret = LY_EVALID; |
| 6836 | goto cleanup; |
| 6837 | } |
| 6838 | |
| 6839 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6840 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6841 | } else { |
| 6842 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6843 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6844 | } |
| 6845 | } |
| 6846 | |
| 6847 | /* merge this set into the global when set */ |
| 6848 | lyxp_set_scnode_merge(set, &tmp_set); |
| 6849 | } |
| 6850 | |
| 6851 | /* check when of non-data parents as well */ |
| 6852 | node = node->parent; |
| 6853 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 6854 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6855 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 6856 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6857 | } |
| 6858 | |
| 6859 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6860 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6861 | return ret; |
| 6862 | } |
| 6863 | |
| 6864 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6865 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 6866 | * @param[in] ctx Compile context. |
| 6867 | * @param[in] node Node to check. |
| 6868 | * @return LY_ERR value |
| 6869 | */ |
| 6870 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6871 | 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] | 6872 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6873 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6874 | uint32_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6875 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6876 | uint32_t opts; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6877 | ly_bool input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6878 | struct lysc_when **when = NULL; |
| 6879 | struct lysc_must *musts = NULL; |
| 6880 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6881 | const struct lysc_node *op; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6882 | |
| 6883 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6884 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6885 | if (node->flags & LYS_CONFIG_R) { |
| 6886 | for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent); |
| 6887 | if (op) { |
| 6888 | /* we are actually in output */ |
| 6889 | opts = LYXP_SCNODE_OUTPUT; |
| 6890 | } |
| 6891 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6892 | |
| 6893 | switch (node->nodetype) { |
| 6894 | case LYS_CONTAINER: |
| 6895 | when = ((struct lysc_node_container *)node)->when; |
| 6896 | musts = ((struct lysc_node_container *)node)->musts; |
| 6897 | break; |
| 6898 | case LYS_CHOICE: |
| 6899 | when = ((struct lysc_node_choice *)node)->when; |
| 6900 | break; |
| 6901 | case LYS_LEAF: |
| 6902 | when = ((struct lysc_node_leaf *)node)->when; |
| 6903 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 6904 | break; |
| 6905 | case LYS_LEAFLIST: |
| 6906 | when = ((struct lysc_node_leaflist *)node)->when; |
| 6907 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 6908 | break; |
| 6909 | case LYS_LIST: |
| 6910 | when = ((struct lysc_node_list *)node)->when; |
| 6911 | musts = ((struct lysc_node_list *)node)->musts; |
| 6912 | break; |
| 6913 | case LYS_ANYXML: |
| 6914 | case LYS_ANYDATA: |
| 6915 | when = ((struct lysc_node_anydata *)node)->when; |
| 6916 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 6917 | break; |
| 6918 | case LYS_CASE: |
| 6919 | when = ((struct lysc_node_case *)node)->when; |
| 6920 | break; |
| 6921 | case LYS_NOTIF: |
| 6922 | musts = ((struct lysc_notif *)node)->musts; |
| 6923 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6924 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6925 | case LYS_ACTION: |
| 6926 | /* first process input musts */ |
| 6927 | musts = ((struct lysc_action *)node)->input.musts; |
| 6928 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6929 | default: |
| 6930 | /* nothing to check */ |
| 6931 | break; |
| 6932 | } |
| 6933 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6934 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6935 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6936 | 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] | 6937 | 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] | 6938 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6939 | 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] | 6940 | goto cleanup; |
| 6941 | } |
| 6942 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6943 | ctx->path[0] = '\0'; |
| 6944 | 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] | 6945 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6946 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6947 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 6948 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6949 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6950 | /* 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] | 6951 | 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] | 6952 | schema->name); |
| 6953 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6954 | |
| 6955 | /* check dummy node accessing */ |
| 6956 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6957 | 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] | 6958 | ret = LY_EVALID; |
| 6959 | goto cleanup; |
| 6960 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6961 | } |
| 6962 | } |
| 6963 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6964 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6965 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6966 | LY_CHECK_GOTO(ret, cleanup); |
| 6967 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6968 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6969 | } |
| 6970 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6971 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6972 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6973 | LY_ARRAY_FOR(musts, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6974 | 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] | 6975 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6976 | 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] | 6977 | goto cleanup; |
| 6978 | } |
| 6979 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6980 | ctx->path[0] = '\0'; |
| 6981 | 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] | 6982 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6983 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6984 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6985 | /* 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] | 6986 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 6987 | 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] | 6988 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6989 | } |
| 6990 | } |
| 6991 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6992 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6993 | } |
| 6994 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6995 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6996 | /* now check output musts */ |
| 6997 | input_done = 1; |
| 6998 | musts = ((struct lysc_action *)node)->output.musts; |
| 6999 | opts = LYXP_SCNODE_OUTPUT; |
| 7000 | goto check_musts; |
| 7001 | } |
| 7002 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7003 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7004 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7005 | return ret; |
| 7006 | } |
| 7007 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7008 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7009 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 7010 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7011 | const struct lysc_node *target = NULL, *siter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7012 | struct ly_path *p; |
| 7013 | struct lysc_type *type; |
| 7014 | |
| 7015 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7016 | |
| 7017 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 7018 | LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, |
| 7019 | 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] | 7020 | LY_PREF_SCHEMA, lref->path_context, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7021 | |
| 7022 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7023 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7024 | ly_path_free(node->module->ctx, p); |
| 7025 | |
| 7026 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 7027 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7028 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 7029 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
| 7030 | return LY_EVALID; |
| 7031 | } |
| 7032 | |
| 7033 | /* check status */ |
| 7034 | ctx->path[0] = '\0'; |
| 7035 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7036 | ctx->path_len = strlen(ctx->path); |
| 7037 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 7038 | return LY_EVALID; |
| 7039 | } |
| 7040 | ctx->path_len = 1; |
| 7041 | ctx->path[1] = '\0'; |
| 7042 | |
| 7043 | /* check config */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7044 | if (lref->require_instance) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 7045 | 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] | 7046 | if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7047 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
| 7048 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
| 7049 | return LY_EVALID; |
| 7050 | } |
| 7051 | } |
| 7052 | |
| 7053 | /* store the target's type and check for circular chain of leafrefs */ |
| 7054 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 7055 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 7056 | if (type == (struct lysc_type *)lref) { |
| 7057 | /* circular chain detected */ |
| 7058 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7059 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
| 7060 | return LY_EVALID; |
| 7061 | } |
| 7062 | } |
| 7063 | |
| 7064 | /* check if leafref and its target are under common if-features */ |
| 7065 | if (lys_compile_leafref_features_validate(node, target)) { |
| 7066 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7067 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 7068 | " features applicable to the leafref itself.", lref->path->expr); |
| 7069 | return LY_EVALID; |
| 7070 | } |
| 7071 | |
| 7072 | return LY_SUCCESS; |
| 7073 | } |
| 7074 | |
| 7075 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7076 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7077 | { |
| 7078 | struct lysc_ext_instance *ext; |
| 7079 | struct lysp_ext_instance *ext_p = NULL; |
| 7080 | struct lysp_stmt *stmt; |
| 7081 | const struct lys_module *ext_mod; |
| 7082 | LY_ERR ret = LY_SUCCESS; |
| 7083 | |
| 7084 | /* create the parsed extension instance manually */ |
| 7085 | ext_p = calloc(1, sizeof *ext_p); |
| 7086 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7087 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup); |
| 7088 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "default", 0, &ext_p->argument), cleanup); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7089 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7090 | ext_p->insubstmt_index = 0; |
| 7091 | |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 7092 | ext_p->child = stmt = calloc(1, sizeof *ext_p->child); |
| 7093 | LY_CHECK_ERR_GOTO(!stmt, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7094 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup); |
| 7095 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "boolean", 0, &stmt->arg), cleanup); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7096 | stmt->kw = LY_STMT_TYPE; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7097 | |
| 7098 | /* allocate new extension instance */ |
| 7099 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7100 | |
| 7101 | /* manually get extension definition module */ |
| 7102 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7103 | |
| 7104 | /* compile the extension instance */ |
| 7105 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7106 | |
| 7107 | cleanup: |
| 7108 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7109 | free(ext_p); |
| 7110 | return ret; |
| 7111 | } |
| 7112 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7113 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7114 | 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] | 7115 | const struct lys_module *dflt_mod, struct lyd_value *storage) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7116 | { |
| 7117 | LY_ERR ret; |
| 7118 | struct ly_err_item *err = NULL; |
| 7119 | |
| 7120 | ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 7121 | LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err); |
| 7122 | if (err) { |
| 7123 | ly_err_print(err); |
| 7124 | ctx->path[0] = '\0'; |
| 7125 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7126 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7127 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7128 | ly_err_free(err); |
| 7129 | } |
| 7130 | if (!ret) { |
| 7131 | ++storage->realtype->refcount; |
| 7132 | return LY_SUCCESS; |
| 7133 | } |
| 7134 | return ret; |
| 7135 | } |
| 7136 | |
| 7137 | static LY_ERR |
| 7138 | 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] | 7139 | const struct lys_module *dflt_mod) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7140 | { |
| 7141 | LY_ERR ret; |
| 7142 | |
| 7143 | assert(!leaf->dflt); |
| 7144 | |
| 7145 | if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) { |
| 7146 | /* ignore default values for keys and mandatory leaves */ |
| 7147 | return LY_SUCCESS; |
| 7148 | } |
| 7149 | |
| 7150 | /* allocate the default value */ |
| 7151 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 7152 | LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM); |
| 7153 | |
| 7154 | /* store the default value */ |
| 7155 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt); |
| 7156 | if (ret) { |
| 7157 | free(leaf->dflt); |
| 7158 | leaf->dflt = NULL; |
| 7159 | } |
| 7160 | |
| 7161 | return ret; |
| 7162 | } |
| 7163 | |
| 7164 | static LY_ERR |
| 7165 | 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] | 7166 | const struct lys_module *dflt_mod) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7167 | { |
| 7168 | LY_ERR ret; |
| 7169 | LY_ARRAY_COUNT_TYPE orig_count, u, v; |
| 7170 | |
| 7171 | assert(dflt || dflts); |
| 7172 | |
| 7173 | if (llist->dflts) { |
| 7174 | /* there were already some defaults and we are adding new by deviations */ |
| 7175 | assert(dflts); |
| 7176 | orig_count = LY_ARRAY_COUNT(llist->dflts); |
| 7177 | } else { |
| 7178 | orig_count = 0; |
| 7179 | } |
| 7180 | |
| 7181 | /* allocate new items */ |
| 7182 | if (dflts) { |
| 7183 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM); |
| 7184 | } else { |
| 7185 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM); |
| 7186 | } |
| 7187 | |
| 7188 | /* fill each new default value */ |
| 7189 | if (dflts) { |
| 7190 | LY_ARRAY_FOR(dflts, u) { |
| 7191 | llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts); |
| 7192 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod, |
| 7193 | llist->dflts[orig_count + u]); |
| 7194 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret); |
| 7195 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7196 | } |
| 7197 | } else { |
| 7198 | llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts); |
| 7199 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]); |
| 7200 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret); |
| 7201 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7202 | } |
| 7203 | |
| 7204 | /* check default value uniqueness */ |
| 7205 | if (llist->flags & LYS_CONFIG_W) { |
| 7206 | /* configuration data values must be unique - so check the default values */ |
| 7207 | for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) { |
| 7208 | for (v = 0; v < u; ++v) { |
| 7209 | if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7210 | ly_bool dynamic = 0; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7211 | const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic); |
| 7212 | |
| 7213 | lysc_update_path(ctx, llist->parent, llist->name); |
| 7214 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7215 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 7216 | lysc_update_path(ctx, NULL, NULL); |
| 7217 | if (dynamic) { |
| 7218 | free((char *)val); |
| 7219 | } |
| 7220 | return LY_EVALID; |
| 7221 | } |
| 7222 | } |
| 7223 | } |
| 7224 | } |
| 7225 | |
| 7226 | return LY_SUCCESS; |
| 7227 | } |
| 7228 | |
| 7229 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7230 | lys_compile_unres(struct lysc_ctx *ctx) |
| 7231 | { |
| 7232 | struct lysc_node *node; |
| 7233 | struct lysc_type *type, *typeiter; |
| 7234 | struct lysc_type_leafref *lref; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7235 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7236 | uint32_t i; |
| 7237 | |
| 7238 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7239 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7240 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 7241 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7242 | node = ctx->leafrefs.objs[i]; |
| 7243 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7244 | type = ((struct lysc_node_leaf *)node)->type; |
| 7245 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7246 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 7247 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7248 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7249 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7250 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 7251 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 7252 | } |
| 7253 | } |
| 7254 | } |
| 7255 | } |
| 7256 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7257 | /* store pointer to the real type */ |
| 7258 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 7259 | if (type->basetype == LY_TYPE_LEAFREF) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7260 | for (typeiter = ((struct lysc_type_leafref *)type)->realtype; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7261 | typeiter->basetype == LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7262 | typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {} |
| 7263 | ((struct lysc_type_leafref *)type)->realtype = typeiter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7264 | } else if (type->basetype == LY_TYPE_UNION) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7265 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7266 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7267 | 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] | 7268 | typeiter->basetype == LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7269 | typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {} |
| 7270 | ((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] | 7271 | } |
| 7272 | } |
| 7273 | } |
| 7274 | } |
| 7275 | |
| 7276 | /* check xpath */ |
| 7277 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 7278 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 7279 | } |
| 7280 | |
| 7281 | /* finish incomplete default values compilation */ |
| 7282 | for (i = 0; i < ctx->dflts.count; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7283 | struct lysc_incomplete_dflt *r = ctx->dflts.objs[i]; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7284 | if (r->leaf->nodetype == LYS_LEAF) { |
| 7285 | LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod)); |
| 7286 | } else { |
| 7287 | 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] | 7288 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7289 | } |
| 7290 | |
| 7291 | return LY_SUCCESS; |
| 7292 | } |
| 7293 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7294 | LY_ERR |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7295 | lys_compile(struct lys_module *mod, uint32_t options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7296 | { |
| 7297 | struct lysc_ctx ctx = {0}; |
| 7298 | struct lysc_module *mod_c; |
| 7299 | struct lysp_module *sp; |
| 7300 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7301 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7302 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7303 | struct lys_module *m; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7304 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7305 | uint32_t i; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7306 | uint16_t compile_id; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7307 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7308 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7309 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7310 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7311 | if (!mod->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7312 | /* just imported modules are not compiled */ |
| 7313 | return LY_SUCCESS; |
| 7314 | } |
| 7315 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7316 | compile_id = ++mod->ctx->module_set_id; |
| 7317 | sp = mod->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7318 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7319 | ctx.ctx = mod->ctx; |
| 7320 | ctx.mod = mod; |
| 7321 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7322 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7323 | ctx.path_len = 1; |
| 7324 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7325 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7326 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 7327 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 7328 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7329 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7330 | LY_ARRAY_FOR(sp->imports, u) { |
| 7331 | LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error); |
| 7332 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7333 | |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 7334 | /* features precompilation */ |
| 7335 | if (!mod->features && sp->features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7336 | /* features are compiled directly into the compiled module structure, |
| 7337 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */ |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 7338 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7339 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 7340 | } /* else the features are already precompiled */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7341 | |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 7342 | /* similarly, identities precompilation */ |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 7343 | if (!mod->identities && sp->identities) { |
| 7344 | ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod->identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7345 | LY_CHECK_GOTO(ret, error); |
| 7346 | } |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 7347 | |
| 7348 | /* compile submodules |
| 7349 | * - must be between features/identities precompilation and finishing their compilation to cover features/identities from |
| 7350 | * submodules */ |
| 7351 | LY_ARRAY_FOR(sp->includes, u) { |
| 7352 | LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error); |
| 7353 | } |
| 7354 | |
| 7355 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7356 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7357 | * are present. */ |
| 7358 | LY_ARRAY_FOR(sp->features, u) { |
| 7359 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod->features); |
| 7360 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7361 | } |
| 7362 | lysc_update_path(&ctx, NULL, "{submodule}"); |
| 7363 | LY_ARRAY_FOR(sp->includes, v) { |
| 7364 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
| 7365 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
| 7366 | ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod->features); |
| 7367 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7368 | } |
| 7369 | lysc_update_path(&ctx, NULL, NULL); |
| 7370 | } |
| 7371 | lysc_update_path(&ctx, NULL, NULL); |
| 7372 | |
| 7373 | /* identities, work similarly to features with the precompilation */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7374 | if (sp->identities) { |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 7375 | LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod->identities), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7376 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7377 | lysc_update_path(&ctx, NULL, "{submodule}"); |
| 7378 | LY_ARRAY_FOR(sp->includes, v) { |
| 7379 | if (sp->includes[v].submodule->identities) { |
| 7380 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 7381 | ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod->identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7382 | LY_CHECK_GOTO(ret, error); |
| 7383 | lysc_update_path(&ctx, NULL, NULL); |
| 7384 | } |
| 7385 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7386 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7387 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7388 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7389 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7390 | 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] | 7391 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7392 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7393 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7394 | 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] | 7395 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7396 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7397 | 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] | 7398 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7399 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7400 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7401 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7402 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7403 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7404 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7405 | /* extension instances TODO cover extension instances from submodules */ |
| 7406 | 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] | 7407 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7408 | /* finish compilation for all unresolved items in the context */ |
| 7409 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7410 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7411 | /* validate non-instantiated groupings from the parsed schema, |
| 7412 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7413 | ctx.options |= LYSC_OPT_GROUPING; |
| 7414 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7415 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7416 | 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] | 7417 | } |
| 7418 | } |
| 7419 | LY_LIST_FOR(sp->data, node_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7420 | grps = (struct lysp_grp *)lysp_node_groupings(node_p); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7421 | LY_ARRAY_FOR(grps, u) { |
| 7422 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7423 | 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] | 7424 | } |
| 7425 | } |
| 7426 | } |
| 7427 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7428 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7429 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7430 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7431 | } |
| 7432 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7433 | #if 0 |
| 7434 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7435 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7436 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7437 | * the anotation definitions available in the internal schema structure. */ |
| 7438 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7439 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7440 | lys_free(mod, NULL, 1, 1); |
| 7441 | return NULL; |
| 7442 | } |
| 7443 | } |
| 7444 | #endif |
| 7445 | |
| 7446 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7447 | if (!strcmp(mod->name, "ietf-netconf-with-defaults")) { |
| 7448 | 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] | 7449 | } |
| 7450 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7451 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7452 | ly_set_erase(&ctx.xpath, NULL); |
| 7453 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7454 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7455 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7456 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7457 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7458 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7459 | lysp_module_free(mod->parsed); |
| 7460 | mod->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7461 | } |
| 7462 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7463 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7464 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7465 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7466 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7467 | if (m->implemented > 1) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7468 | m->implemented = 1; |
| 7469 | } |
| 7470 | } |
| 7471 | } |
| 7472 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7473 | return LY_SUCCESS; |
| 7474 | |
| 7475 | error: |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7476 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7477 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7478 | ly_set_erase(&ctx.xpath, NULL); |
| 7479 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7480 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7481 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7482 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7483 | lysc_module_free(mod_c, NULL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7484 | mod->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7485 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7486 | /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency |
| 7487 | * of this module, since this module can be also compiled from dependency, there can be some other modules being |
| 7488 | * 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] | 7489 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7490 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7491 | if ((m->implemented >= compile_id) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7492 | /* revert features list to the precompiled state */ |
| 7493 | lys_feature_precompile_revert(&ctx, m); |
| 7494 | /* mark module as imported-only / not-implemented */ |
| 7495 | m->implemented = 0; |
| 7496 | /* free the compiled version of the module */ |
| 7497 | lysc_module_free(m->compiled, NULL); |
| 7498 | m->compiled = NULL; |
| 7499 | } |
| 7500 | } |
| 7501 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7502 | return ret; |
| 7503 | } |