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 | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 468 | struct lysc_feature *f, *flist; |
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 | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 484 | if (mod->implemented) { |
| 485 | /* module is implemented so there is already the compiled schema */ |
| 486 | flist = mod->compiled->features; |
| 487 | } else { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 488 | flist = mod->dis_features; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 489 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 490 | LY_ARRAY_FOR(flist, u) { |
| 491 | f = &flist[u]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 492 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 493 | return f; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | return NULL; |
| 498 | } |
| 499 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 500 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 501 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 502 | */ |
| 503 | static LY_ERR |
| 504 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 505 | { |
| 506 | LY_ERR ret = LY_SUCCESS; |
| 507 | |
| 508 | if (!ext_p->compiled) { |
| 509 | lysc_update_path(ctx, NULL, "{extension}"); |
| 510 | lysc_update_path(ctx, NULL, ext_p->name); |
| 511 | |
| 512 | /* compile the extension definition */ |
| 513 | ext_p->compiled = calloc(1, sizeof **ext); |
| 514 | ext_p->compiled->refcount = 1; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 515 | DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done); |
| 516 | 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] | 517 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 518 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 519 | |
| 520 | lysc_update_path(ctx, NULL, NULL); |
| 521 | lysc_update_path(ctx, NULL, NULL); |
| 522 | |
| 523 | /* find extension definition plugin */ |
| 524 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 525 | } |
| 526 | |
| 527 | *ext = lysc_ext_dup(ext_p->compiled); |
| 528 | |
| 529 | done: |
| 530 | return ret; |
| 531 | } |
| 532 | |
| 533 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 534 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 535 | * |
| 536 | * @param[in] ctx Compilation context. |
| 537 | * @param[in] ext_p Parsed extension instance. |
| 538 | * @param[in,out] ext Prepared compiled extension instance. |
| 539 | * @param[in] parent Extension instance parent. |
| 540 | * @param[in] parent_type Extension instance parent type. |
| 541 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 542 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 543 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 544 | 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] | 545 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
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 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 548 | const char *name; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 549 | size_t u; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 550 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 551 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 552 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 553 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret); |
| 554 | LY_CHECK_RET(ret); |
| 555 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 556 | ext->insubstmt = ext_p->insubstmt; |
| 557 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 558 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 559 | ext->parent = parent; |
| 560 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 561 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 562 | 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] | 563 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 564 | /* get module where the extension definition should be placed */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 565 | 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] | 566 | if (ext_p->yin) { |
| 567 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 568 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 569 | * YANG (import) prefix must be done here. */ |
| 570 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 571 | 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] | 572 | u = 0; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 573 | } else { |
| 574 | assert(ctx->mod_def->parsed); |
| 575 | LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) { |
| 576 | 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] | 577 | char *s; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 578 | 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] | 579 | ret = LY_EMEM, cleanup); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 580 | 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] | 581 | u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 582 | break; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | if (!prefixed_name) { |
| 587 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 588 | "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^] | 589 | ret = LY_EVALID; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 590 | goto cleanup; |
| 591 | } |
| 592 | } else { |
| 593 | prefixed_name = ext_p->name; |
| 594 | } |
| 595 | lysc_update_path(ctx, NULL, prefixed_name); |
| 596 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 597 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 598 | 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] | 599 | if (!ext_mod) { |
| 600 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 601 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_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 | } else if (!ext_mod->parsed->extensions) { |
| 605 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 606 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 607 | prefixed_name, ext_mod->name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 608 | ret = LY_EVALID; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 609 | goto cleanup; |
| 610 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 611 | } |
| 612 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 613 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 614 | /* find the parsed extension definition there */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 615 | LY_ARRAY_FOR(ext_mod->parsed->extensions, v) { |
| 616 | if (!strcmp(name, ext_mod->parsed->extensions[v].name)) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 617 | /* compile extension definition and assign it */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 618 | 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] | 619 | break; |
| 620 | } |
| 621 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 622 | if (!ext->def) { |
| 623 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 624 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 625 | ret = LY_EVALID; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 626 | goto cleanup; |
| 627 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 628 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 629 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 630 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 631 | * 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] | 632 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 633 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 634 | struct lysp_stmt *stmt = NULL; |
| 635 | |
| 636 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 637 | /* ... argument was the first XML child element */ |
| 638 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 639 | /* TODO check namespace of the statement */ |
| 640 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 641 | stmt = ext_p->child; |
| 642 | } |
| 643 | } |
| 644 | } else { |
| 645 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 646 | * with LYS_YIN_ATTR flag */ |
| 647 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 648 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 649 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 650 | break; |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | if (!stmt) { |
| 655 | /* missing extension's argument */ |
| 656 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 657 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 658 | ret = LY_EVALID; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 659 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 660 | |
| 661 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 662 | 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] | 663 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 664 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 665 | if (prefixed_name != ext_p->name) { |
| 666 | lydict_remove(ctx->ctx, ext_p->name); |
| 667 | ext_p->name = prefixed_name; |
| 668 | if (!ext_p->argument && ext->argument) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 669 | 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] | 670 | } |
| 671 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 672 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 673 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 674 | if (ext->argument) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 675 | lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 676 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 677 | 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] | 678 | if (ext->argument) { |
| 679 | lysc_update_path(ctx, NULL, NULL); |
| 680 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 681 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 682 | ext_p->compiled = ext; |
| 683 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 684 | cleanup: |
| 685 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 686 | lydict_remove(ctx->ctx, prefixed_name); |
| 687 | } |
| 688 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 689 | lysc_update_path(ctx, NULL, NULL); |
| 690 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 691 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 692 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 696 | * @brief Compile information from the if-feature statement |
| 697 | * @param[in] ctx Compile context. |
| 698 | * @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] | 699 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 700 | * @return LY_ERR value. |
| 701 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 702 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 703 | 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] | 704 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 705 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 706 | const char *c = *value; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 707 | int64_t i, j; |
| 708 | int8_t op_len, last_not = 0, checkversion = 0; |
| 709 | 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] | 710 | uint8_t op; |
| 711 | struct iff_stack stack = {0, 0, NULL}; |
| 712 | struct lysc_feature *f; |
| 713 | |
| 714 | assert(c); |
| 715 | |
| 716 | /* 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] | 717 | for (i = j = 0; c[i]; i++) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 718 | if (c[i] == '(') { |
| 719 | j++; |
| 720 | checkversion = 1; |
| 721 | continue; |
| 722 | } else if (c[i] == ')') { |
| 723 | j--; |
| 724 | continue; |
| 725 | } else if (isspace(c[i])) { |
| 726 | checkversion = 1; |
| 727 | continue; |
| 728 | } |
| 729 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 730 | if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) { |
| 731 | uint64_t spaces; |
| 732 | for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++); |
| 733 | if (c[i + op_len + spaces] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 735 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 736 | return LY_EVALID; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 737 | } else if (!isspace(c[i + op_len])) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 738 | /* feature name starting with the not/and/or */ |
| 739 | last_not = 0; |
| 740 | f_size++; |
| 741 | } else if (c[i] == 'n') { /* not operation */ |
| 742 | if (last_not) { |
| 743 | /* double not */ |
| 744 | expr_size = expr_size - 2; |
| 745 | last_not = 0; |
| 746 | } else { |
| 747 | last_not = 1; |
| 748 | } |
| 749 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 750 | if (f_exp != f_size) { |
| 751 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 752 | "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] | 753 | return LY_EVALID; |
| 754 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 755 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 756 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 757 | /* not a not operation */ |
| 758 | last_not = 0; |
| 759 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 760 | i += op_len; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 761 | } else { |
| 762 | f_size++; |
| 763 | last_not = 0; |
| 764 | } |
| 765 | expr_size++; |
| 766 | |
| 767 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 768 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 769 | i--; |
| 770 | break; |
| 771 | } |
| 772 | i++; |
| 773 | } |
| 774 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 775 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 776 | /* not matching count of ( and ) */ |
| 777 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 778 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 779 | return LY_EVALID; |
| 780 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 781 | if (f_exp != f_size) { |
| 782 | /* features do not match the needed arguments for the logical operations */ |
| 783 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 784 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 785 | "the required number of operands for the operations.", *value); |
| 786 | return LY_EVALID; |
| 787 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 788 | |
| 789 | if (checkversion || expr_size > 1) { |
| 790 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 791 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 792 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 793 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 794 | return LY_EVALID; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /* allocate the memory */ |
| 799 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 800 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 801 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 802 | 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] | 803 | |
| 804 | stack.size = expr_size; |
| 805 | f_size--; expr_size--; /* used as indexes from now */ |
| 806 | |
| 807 | for (i--; i >= 0; i--) { |
| 808 | if (c[i] == ')') { |
| 809 | /* push it on stack */ |
| 810 | iff_stack_push(&stack, LYS_IFF_RP); |
| 811 | continue; |
| 812 | } else if (c[i] == '(') { |
| 813 | /* pop from the stack into result all operators until ) */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 814 | while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 815 | iff_setop(iff->expr, op, expr_size--); |
| 816 | } |
| 817 | continue; |
| 818 | } else if (isspace(c[i])) { |
| 819 | continue; |
| 820 | } |
| 821 | |
| 822 | /* end of operator or operand -> find beginning and get what is it */ |
| 823 | j = i + 1; |
| 824 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 825 | i--; |
| 826 | } |
| 827 | i++; /* go back by one step */ |
| 828 | |
| 829 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 830 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 831 | /* double not */ |
| 832 | iff_stack_pop(&stack); |
| 833 | } else { |
| 834 | /* not has the highest priority, so do not pop from the stack |
| 835 | * as in case of AND and OR */ |
| 836 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 837 | } |
| 838 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 839 | /* as for OR - pop from the stack all operators with the same or higher |
| 840 | * priority and store them to the result, then push the AND to the stack */ |
| 841 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 842 | op = iff_stack_pop(&stack); |
| 843 | iff_setop(iff->expr, op, expr_size--); |
| 844 | } |
| 845 | iff_stack_push(&stack, LYS_IFF_AND); |
| 846 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 847 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 848 | op = iff_stack_pop(&stack); |
| 849 | iff_setop(iff->expr, op, expr_size--); |
| 850 | } |
| 851 | iff_stack_push(&stack, LYS_IFF_OR); |
| 852 | } else { |
| 853 | /* feature name, length is j - i */ |
| 854 | |
| 855 | /* add it to the expression */ |
| 856 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 857 | |
| 858 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 859 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 860 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 861 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 862 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 863 | iff->features[f_size] = f; |
| 864 | LY_ARRAY_INCREMENT(iff->features); |
| 865 | f_size--; |
| 866 | } |
| 867 | } |
| 868 | while (stack.index) { |
| 869 | op = iff_stack_pop(&stack); |
| 870 | iff_setop(iff->expr, op, expr_size--); |
| 871 | } |
| 872 | |
| 873 | if (++expr_size || ++f_size) { |
| 874 | /* not all expected operators and operands found */ |
| 875 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 876 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 877 | rc = LY_EINT; |
| 878 | } else { |
| 879 | rc = LY_SUCCESS; |
| 880 | } |
| 881 | |
| 882 | error: |
| 883 | /* cleanup */ |
| 884 | iff_stack_clean(&stack); |
| 885 | |
| 886 | return rc; |
| 887 | } |
| 888 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 889 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 890 | * @brief Get the XPath context node for the given schema node. |
| 891 | * @param[in] start The schema node where the XPath expression appears. |
| 892 | * @return The context node to evaluate XPath expression in given schema node. |
| 893 | * @return NULL in case the context node is the root node. |
| 894 | */ |
| 895 | static struct lysc_node * |
| 896 | lysc_xpath_context(struct lysc_node *start) |
| 897 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 898 | 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] | 899 | start = start->parent) {} |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 900 | return start; |
| 901 | } |
| 902 | |
| 903 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 904 | * @brief Compile information from the when statement |
| 905 | * @param[in] ctx Compile context. |
| 906 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 907 | * @param[in] flags Flags of the node with the "when" defiition. |
| 908 | * @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] | 909 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 910 | * @return LY_ERR value. |
| 911 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 912 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 913 | 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] | 914 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 915 | LY_ERR ret = LY_SUCCESS; |
| 916 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 917 | *when = calloc(1, sizeof **when); |
| 918 | (*when)->refcount = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 919 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 920 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 921 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 922 | DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done); |
| 923 | DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 924 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 925 | 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] | 926 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 927 | |
| 928 | done: |
| 929 | return ret; |
| 930 | } |
| 931 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 932 | /** |
| 933 | * @brief Compile information from the must statement |
| 934 | * @param[in] ctx Compile context. |
| 935 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 936 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 937 | * @return LY_ERR value. |
| 938 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 939 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 940 | 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] | 941 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 942 | LY_ERR ret = LY_SUCCESS; |
| 943 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 944 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 945 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 946 | must->module = ctx->mod_def; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 947 | DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done); |
| 948 | DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done); |
| 949 | DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done); |
| 950 | DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 951 | 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] | 952 | |
| 953 | done: |
| 954 | return ret; |
| 955 | } |
| 956 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 957 | /** |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 958 | * @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] | 959 | * @param[in] ctx Compile context. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 960 | * @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] | 961 | * @return LY_ERR value. |
| 962 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 963 | static LY_ERR |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 964 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 965 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 966 | const struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 967 | LY_ERR ret = LY_SUCCESS; |
| 968 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 969 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 970 | * 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] | 971 | * 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] | 972 | if (!imp_p->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 973 | /* try to use filepath if present */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 974 | if (imp_p->module->filepath) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 975 | struct ly_in *in; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 976 | if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 977 | LOGINT(ctx->ctx); |
| 978 | } else { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 979 | 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] | 980 | ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 981 | if (mod != imp_p->module) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 982 | 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] | 983 | imp_p->module->filepath, imp_p->module->name); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 984 | mod = NULL; |
| 985 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 986 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 987 | ly_in_free(in, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 988 | } |
| 989 | if (!mod) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 990 | 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] | 991 | 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] | 992 | imp_p->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 993 | return LY_ENOTFOUND; |
| 994 | } |
| 995 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 996 | } |
| 997 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 998 | return ret; |
| 999 | } |
| 1000 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1001 | LY_ERR |
| 1002 | 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] | 1003 | struct lysp_ident *identities_p, struct lysc_ident **identities) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1004 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1005 | LY_ARRAY_COUNT_TYPE offset = 0, u, v; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1006 | struct lysc_ctx context = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1007 | LY_ERR ret = LY_SUCCESS; |
| 1008 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1009 | assert(ctx_sc || ctx); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1010 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1011 | if (!ctx_sc) { |
| 1012 | context.ctx = ctx; |
| 1013 | context.mod = module; |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 1014 | context.mod_def = module; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1015 | context.path_len = 1; |
| 1016 | context.path[0] = '/'; |
| 1017 | ctx_sc = &context; |
| 1018 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1019 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1020 | if (!identities_p) { |
| 1021 | return LY_SUCCESS; |
| 1022 | } |
| 1023 | if (*identities) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1024 | offset = LY_ARRAY_COUNT(*identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | lysc_update_path(ctx_sc, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1028 | 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] | 1029 | LY_ARRAY_FOR(identities_p, u) { |
| 1030 | lysc_update_path(ctx_sc, NULL, identities_p[u].name); |
| 1031 | |
| 1032 | LY_ARRAY_INCREMENT(*identities); |
| 1033 | 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^] | 1034 | DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done); |
| 1035 | DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done); |
| 1036 | 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] | 1037 | (*identities)[offset + u].module = ctx_sc->mod; |
| 1038 | COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v, |
| 1039 | lys_compile_iffeature, ret, done); |
| 1040 | /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */ |
| 1041 | COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u], |
| 1042 | LYEXT_PAR_IDENT, ret, done); |
| 1043 | (*identities)[offset + u].flags = identities_p[u].flags; |
| 1044 | |
| 1045 | lysc_update_path(ctx_sc, NULL, NULL); |
| 1046 | } |
| 1047 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1048 | done: |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1052 | /** |
| 1053 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 1054 | * |
| 1055 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 1056 | * |
| 1057 | * @param[in] ctx Compile context for logging. |
| 1058 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 1059 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 1060 | * @return LY_SUCCESS if everything is ok. |
| 1061 | * @return LY_EVALID if the identity is derived from itself. |
| 1062 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1063 | static LY_ERR |
| 1064 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 1065 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1066 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1067 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1068 | struct ly_set recursion = {0}; |
| 1069 | struct lysc_ident *drv; |
| 1070 | |
| 1071 | if (!derived) { |
| 1072 | return LY_SUCCESS; |
| 1073 | } |
| 1074 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1075 | for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1076 | if (ident == derived[u]) { |
| 1077 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1078 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1079 | ret = LY_EVALID; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1080 | goto cleanup; |
| 1081 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1082 | ret = ly_set_add(&recursion, derived[u], 0, NULL); |
| 1083 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | for (v = 0; v < recursion.count; ++v) { |
| 1087 | drv = recursion.objs[v]; |
| 1088 | if (!drv->derived) { |
| 1089 | continue; |
| 1090 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1091 | for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1092 | if (ident == drv->derived[u]) { |
| 1093 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1094 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1095 | ret = LY_EVALID; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1096 | goto cleanup; |
| 1097 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1098 | ret = ly_set_add(&recursion, drv->derived[u], 0, NULL); |
| 1099 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1100 | } |
| 1101 | } |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1102 | |
| 1103 | cleanup: |
| 1104 | ly_set_erase(&recursion, NULL); |
| 1105 | return ret; |
| 1106 | } |
| 1107 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1108 | /** |
| 1109 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1110 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1111 | * 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] | 1112 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1113 | * to distinguish these two use cases. |
| 1114 | * |
| 1115 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1116 | * @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] | 1117 | * @param[in] ident Referencing identity to work with, NULL for identityref. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1118 | * @param[in] bases Array of bases of identityref to fill in. |
| 1119 | * @return LY_ERR value. |
| 1120 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1121 | static LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1122 | 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] | 1123 | struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1124 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1125 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1126 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1127 | struct lys_module *mod; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1128 | struct lysc_ident **idref, *identities; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1129 | |
| 1130 | assert(ident || bases); |
| 1131 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1132 | if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1133 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1134 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1135 | return LY_EVALID; |
| 1136 | } |
| 1137 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1138 | LY_ARRAY_FOR(bases_p, u) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1139 | s = strchr(bases_p[u], ':'); |
| 1140 | if (s) { |
| 1141 | /* prefixed identity */ |
| 1142 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1143 | 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] | 1144 | } else { |
| 1145 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1146 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1147 | } |
| 1148 | if (!mod) { |
| 1149 | if (ident) { |
| 1150 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1151 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1152 | } else { |
| 1153 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1154 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1155 | } |
| 1156 | return LY_EVALID; |
| 1157 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1158 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1159 | idref = NULL; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1160 | if (mod->compiled) { |
| 1161 | identities = mod->compiled->identities; |
| 1162 | } else { |
| 1163 | identities = mod->dis_identities; |
| 1164 | } |
| 1165 | LY_ARRAY_FOR(identities, v) { |
| 1166 | if (!strcmp(name, identities[v].name)) { |
| 1167 | if (ident) { |
| 1168 | if (ident == &identities[v]) { |
| 1169 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1170 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1171 | return LY_EVALID; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1172 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1173 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived)); |
| 1174 | /* we have match! store the backlink */ |
| 1175 | LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM); |
| 1176 | *idref = ident; |
| 1177 | } else { |
| 1178 | /* we have match! store the found identity */ |
| 1179 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
| 1180 | *idref = &identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1181 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1182 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1183 | } |
| 1184 | } |
| 1185 | if (!idref || !(*idref)) { |
| 1186 | if (ident) { |
| 1187 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1188 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1189 | } else { |
| 1190 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1191 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1192 | } |
| 1193 | return LY_EVALID; |
| 1194 | } |
| 1195 | } |
| 1196 | return LY_SUCCESS; |
| 1197 | } |
| 1198 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1199 | /** |
| 1200 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1201 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1202 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1203 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1204 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1205 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1206 | static LY_ERR |
| 1207 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1208 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1209 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1210 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1211 | lysc_update_path(ctx, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1212 | for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1213 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1214 | continue; |
| 1215 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1216 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1217 | 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] | 1218 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1219 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1220 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1221 | return LY_SUCCESS; |
| 1222 | } |
| 1223 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1224 | LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1225 | 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] | 1226 | struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1227 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 1228 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1229 | LY_ARRAY_COUNT_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1230 | struct lysc_ctx context = {0}; |
| 1231 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1232 | assert(ctx_sc || ctx); |
| 1233 | |
| 1234 | if (!ctx_sc) { |
| 1235 | context.ctx = ctx; |
| 1236 | context.mod = module; |
| 1237 | context.path_len = 1; |
| 1238 | context.path[0] = '/'; |
| 1239 | ctx_sc = &context; |
| 1240 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1241 | |
| 1242 | if (!features_p) { |
| 1243 | return LY_SUCCESS; |
| 1244 | } |
| 1245 | if (*features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1246 | offset = LY_ARRAY_COUNT(*features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1247 | } |
| 1248 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1249 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1250 | 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] | 1251 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1252 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1253 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1254 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1255 | 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^] | 1256 | DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done); |
| 1257 | DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done); |
| 1258 | 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] | 1259 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1260 | (*features)[offset + u].module = ctx_sc->mod; |
| 1261 | |
| 1262 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1263 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1264 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1265 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 1266 | done: |
| 1267 | return ret; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1268 | } |
| 1269 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1270 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1271 | * @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] | 1272 | * |
| 1273 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1274 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1275 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1276 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1277 | * being currently processed). |
| 1278 | * @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] | 1279 | * @return LY_SUCCESS if everything is ok. |
| 1280 | * @return LY_EVALID if the feature references indirectly itself. |
| 1281 | */ |
| 1282 | static LY_ERR |
| 1283 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1284 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1285 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1286 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1287 | struct ly_set recursion = {0}; |
| 1288 | struct lysc_feature *drv; |
| 1289 | |
| 1290 | if (!depfeatures) { |
| 1291 | return LY_SUCCESS; |
| 1292 | } |
| 1293 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1294 | for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1295 | if (feature == depfeatures[u]) { |
| 1296 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1297 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1298 | ret = LY_EVALID; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1299 | goto cleanup; |
| 1300 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1301 | ret = ly_set_add(&recursion, depfeatures[u], 0, NULL); |
| 1302 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | for (v = 0; v < recursion.count; ++v) { |
| 1306 | drv = recursion.objs[v]; |
| 1307 | if (!drv->depfeatures) { |
| 1308 | continue; |
| 1309 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1310 | for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1311 | if (feature == drv->depfeatures[u]) { |
| 1312 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1313 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1314 | ret = LY_EVALID; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1315 | goto cleanup; |
| 1316 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1317 | ly_set_add(&recursion, drv->depfeatures[u], 0, NULL); |
| 1318 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1319 | } |
| 1320 | } |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1321 | |
| 1322 | cleanup: |
| 1323 | ly_set_erase(&recursion, NULL); |
| 1324 | return ret; |
| 1325 | } |
| 1326 | |
| 1327 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1328 | * @brief Create pre-compiled features array. |
| 1329 | * |
| 1330 | * See lys_feature_precompile() for more details. |
| 1331 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1332 | * @param[in] ctx Compile context. |
| 1333 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1334 | * @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] | 1335 | * @return LY_ERR value. |
| 1336 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1337 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1338 | 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] | 1339 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1340 | LY_ARRAY_COUNT_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1341 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1342 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1343 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1344 | /* find the preprecompiled feature */ |
| 1345 | LY_ARRAY_FOR(features, x) { |
| 1346 | if (strcmp(features[x].name, feature_p->name)) { |
| 1347 | continue; |
| 1348 | } |
| 1349 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1350 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1351 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1352 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1353 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1354 | 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] | 1355 | 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] | 1356 | if (feature->iffeatures) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1357 | for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1358 | if (feature->iffeatures[u].features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1359 | for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1360 | /* check for circular dependency - direct reference first,... */ |
| 1361 | if (feature == feature->iffeatures[u].features[v]) { |
| 1362 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1363 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1364 | return LY_EVALID; |
| 1365 | } |
| 1366 | /* ... and indirect circular reference */ |
| 1367 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1368 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1369 | /* add itself into the dependants list */ |
| 1370 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1371 | *df = feature; |
| 1372 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1373 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1374 | } |
| 1375 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1376 | lysc_update_path(ctx, NULL, NULL); |
| 1377 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1378 | done: |
| 1379 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1380 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1381 | |
| 1382 | LOGINT(ctx->ctx); |
| 1383 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1384 | } |
| 1385 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1386 | /** |
| 1387 | * @brief Revert compiled list of features back to the precompiled state. |
| 1388 | * |
| 1389 | * 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] | 1390 | * 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] | 1391 | * |
| 1392 | * @param[in] ctx Compilation context. |
| 1393 | * @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] | 1394 | * and supposed to hold the dis_features list. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1395 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1396 | static void |
| 1397 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1398 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1399 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1400 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 1401 | if (mod->compiled) { |
| 1402 | /* keep the dis_features list until the complete lys_module is freed */ |
| 1403 | mod->dis_features = mod->compiled->features; |
| 1404 | mod->compiled->features = NULL; |
| 1405 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1406 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1407 | /* 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] | 1408 | * which may points into the data being freed here */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1409 | LY_ARRAY_FOR(mod->dis_features, u) { |
| 1410 | LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) { |
| 1411 | lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1412 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1413 | LY_ARRAY_FREE(mod->dis_features[u].iffeatures); |
| 1414 | mod->dis_features[u].iffeatures = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1415 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1416 | LY_ARRAY_FOR(mod->dis_features[u].exts, v) { |
| 1417 | lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1418 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1419 | LY_ARRAY_FREE(mod->dis_features[u].exts); |
| 1420 | mod->dis_features[u].exts = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1424 | /** |
| 1425 | * @brief Validate and normalize numeric value from a range definition. |
| 1426 | * @param[in] ctx Compile context. |
| 1427 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1428 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1429 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1430 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1431 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1432 | * @param[in] value String value of the range boundary. |
| 1433 | * @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. |
| 1434 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1435 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1436 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1437 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1438 | 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] | 1439 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1440 | size_t fraction = 0, size; |
| 1441 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1442 | *len = 0; |
| 1443 | |
| 1444 | assert(value); |
| 1445 | /* parse value */ |
| 1446 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1447 | return LY_EVALID; |
| 1448 | } |
| 1449 | |
| 1450 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1451 | ++(*len); |
| 1452 | } |
| 1453 | |
| 1454 | while (isdigit(value[*len])) { |
| 1455 | ++(*len); |
| 1456 | } |
| 1457 | |
| 1458 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1459 | if (basetype == LY_TYPE_DEC64) { |
| 1460 | goto decimal; |
| 1461 | } else { |
| 1462 | *valcopy = strndup(value, *len); |
| 1463 | return LY_SUCCESS; |
| 1464 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1465 | } |
| 1466 | fraction = *len; |
| 1467 | |
| 1468 | ++(*len); |
| 1469 | while (isdigit(value[*len])) { |
| 1470 | ++(*len); |
| 1471 | } |
| 1472 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1473 | if (basetype == LY_TYPE_DEC64) { |
| 1474 | decimal: |
| 1475 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1476 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1477 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1478 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1479 | *len, value, frdigits); |
| 1480 | return LY_EINVAL; |
| 1481 | } |
| 1482 | if (fraction) { |
| 1483 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1484 | } else { |
| 1485 | size = (*len) + frdigits + 1; |
| 1486 | } |
| 1487 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1488 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1489 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1490 | (*valcopy)[size - 1] = '\0'; |
| 1491 | if (fraction) { |
| 1492 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1493 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1494 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1495 | } else { |
| 1496 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1497 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1498 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1499 | } |
| 1500 | return LY_SUCCESS; |
| 1501 | } |
| 1502 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1503 | /** |
| 1504 | * @brief Check that values in range are in ascendant order. |
| 1505 | * @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] | 1506 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1507 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1508 | * @param[in] value Current value to check. |
| 1509 | * @param[in] prev_value The last seen value. |
| 1510 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1511 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1512 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1513 | 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] | 1514 | { |
| 1515 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1516 | 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] | 1517 | return LY_EEXIST; |
| 1518 | } |
| 1519 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1520 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1521 | return LY_EEXIST; |
| 1522 | } |
| 1523 | } |
| 1524 | return LY_SUCCESS; |
| 1525 | } |
| 1526 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1527 | /** |
| 1528 | * @brief Set min/max value of the range part. |
| 1529 | * @param[in] ctx Compile context. |
| 1530 | * @param[in] part Range part structure to fill. |
| 1531 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1532 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1533 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1534 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1535 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1536 | * @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] | 1537 | * @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] | 1538 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1539 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1540 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1541 | * frdigits value), LY_EMEM. |
| 1542 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1543 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1544 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype, |
| 1545 | 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] | 1546 | { |
| 1547 | LY_ERR ret = LY_SUCCESS; |
| 1548 | char *valcopy = NULL; |
| 1549 | size_t len; |
| 1550 | |
| 1551 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1552 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1553 | LY_CHECK_GOTO(ret, finalize); |
| 1554 | } |
| 1555 | if (!valcopy && base_range) { |
| 1556 | if (max) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1557 | 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] | 1558 | } else { |
| 1559 | part->min_64 = base_range->parts[0].min_64; |
| 1560 | } |
| 1561 | if (!first) { |
| 1562 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1563 | } |
| 1564 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1568 | case LY_TYPE_INT8: /* range */ |
| 1569 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1570 | 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] | 1571 | } else if (max) { |
| 1572 | part->max_64 = INT64_C(127); |
| 1573 | } else { |
| 1574 | part->min_64 = INT64_C(-128); |
| 1575 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1576 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1577 | 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] | 1578 | } |
| 1579 | break; |
| 1580 | case LY_TYPE_INT16: /* range */ |
| 1581 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1582 | 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] | 1583 | } else if (max) { |
| 1584 | part->max_64 = INT64_C(32767); |
| 1585 | } else { |
| 1586 | part->min_64 = INT64_C(-32768); |
| 1587 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1588 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1589 | 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] | 1590 | } |
| 1591 | break; |
| 1592 | case LY_TYPE_INT32: /* range */ |
| 1593 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1594 | 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] | 1595 | } else if (max) { |
| 1596 | part->max_64 = INT64_C(2147483647); |
| 1597 | } else { |
| 1598 | part->min_64 = INT64_C(-2147483648); |
| 1599 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1600 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1601 | 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] | 1602 | } |
| 1603 | break; |
| 1604 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1605 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1606 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1607 | 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] | 1608 | max ? &part->max_64 : &part->min_64); |
| 1609 | } else if (max) { |
| 1610 | part->max_64 = INT64_C(9223372036854775807); |
| 1611 | } else { |
| 1612 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1613 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1614 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1615 | 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] | 1616 | } |
| 1617 | break; |
| 1618 | case LY_TYPE_UINT8: /* range */ |
| 1619 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1620 | 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] | 1621 | } else if (max) { |
| 1622 | part->max_u64 = UINT64_C(255); |
| 1623 | } else { |
| 1624 | part->min_u64 = UINT64_C(0); |
| 1625 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1626 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1627 | 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] | 1628 | } |
| 1629 | break; |
| 1630 | case LY_TYPE_UINT16: /* range */ |
| 1631 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1632 | 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] | 1633 | } else if (max) { |
| 1634 | part->max_u64 = UINT64_C(65535); |
| 1635 | } else { |
| 1636 | part->min_u64 = UINT64_C(0); |
| 1637 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1638 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1639 | 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] | 1640 | } |
| 1641 | break; |
| 1642 | case LY_TYPE_UINT32: /* range */ |
| 1643 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1644 | 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] | 1645 | } else if (max) { |
| 1646 | part->max_u64 = UINT64_C(4294967295); |
| 1647 | } else { |
| 1648 | part->min_u64 = UINT64_C(0); |
| 1649 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1650 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1651 | 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] | 1652 | } |
| 1653 | break; |
| 1654 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1655 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1656 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1657 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1658 | 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] | 1659 | } else if (max) { |
| 1660 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1661 | } else { |
| 1662 | part->min_u64 = UINT64_C(0); |
| 1663 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1664 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1665 | 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] | 1666 | } |
| 1667 | break; |
| 1668 | default: |
| 1669 | LOGINT(ctx->ctx); |
| 1670 | ret = LY_EINT; |
| 1671 | } |
| 1672 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1673 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1674 | if (ret == LY_EDENIED) { |
| 1675 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1676 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1677 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1678 | } else if (ret == LY_EVALID) { |
| 1679 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1680 | "Invalid %s restriction - invalid value \"%s\".", |
| 1681 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1682 | } else if (ret == LY_EEXIST) { |
| 1683 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1684 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1685 | length_restr ? "length" : "range", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1686 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1687 | } else if (!ret && value) { |
| 1688 | *value = *value + len; |
| 1689 | } |
| 1690 | free(valcopy); |
| 1691 | return ret; |
| 1692 | } |
| 1693 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1694 | /** |
| 1695 | * @brief Compile the parsed range restriction. |
| 1696 | * @param[in] ctx Compile context. |
| 1697 | * @param[in] range_p Parsed range structure to compile. |
| 1698 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1699 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1700 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1701 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1702 | * range restriction must be more restrictive than the base_range. |
| 1703 | * @param[in,out] range Pointer to the created current range structure. |
| 1704 | * @return LY_ERR value. |
| 1705 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1706 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1707 | 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] | 1708 | uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1709 | { |
| 1710 | LY_ERR ret = LY_EVALID; |
| 1711 | const char *expr; |
| 1712 | struct lysc_range_part *parts = NULL, *part; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1713 | ly_bool range_expected = 0, uns; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1714 | LY_ARRAY_COUNT_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1715 | |
| 1716 | assert(range); |
| 1717 | assert(range_p); |
| 1718 | |
| 1719 | expr = range_p->arg; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1720 | while (1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1721 | if (isspace(*expr)) { |
| 1722 | ++expr; |
| 1723 | } else if (*expr == '\0') { |
| 1724 | if (range_expected) { |
| 1725 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1726 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1727 | length_restr ? "length" : "range", range_p->arg); |
| 1728 | goto cleanup; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1729 | } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1730 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1731 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1732 | length_restr ? "length" : "range", range_p->arg); |
| 1733 | goto cleanup; |
| 1734 | } |
| 1735 | parts_done++; |
| 1736 | break; |
| 1737 | } else if (!strncmp(expr, "min", 3)) { |
| 1738 | if (parts) { |
| 1739 | /* min cannot be used elsewhere than in the first part */ |
| 1740 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1741 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1742 | expr - range_p->arg, range_p->arg); |
| 1743 | goto cleanup; |
| 1744 | } |
| 1745 | expr += 3; |
| 1746 | |
| 1747 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1748 | 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] | 1749 | part->max_64 = part->min_64; |
| 1750 | } else if (*expr == '|') { |
| 1751 | if (!parts || range_expected) { |
| 1752 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1753 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1754 | goto cleanup; |
| 1755 | } |
| 1756 | expr++; |
| 1757 | parts_done++; |
| 1758 | /* process next part of the expression */ |
| 1759 | } else if (!strncmp(expr, "..", 2)) { |
| 1760 | expr += 2; |
| 1761 | while (isspace(*expr)) { |
| 1762 | expr++; |
| 1763 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1764 | if (!parts || LY_ARRAY_COUNT(parts) == parts_done) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1765 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1766 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1767 | goto cleanup; |
| 1768 | } |
| 1769 | /* continue expecting the upper boundary */ |
| 1770 | range_expected = 1; |
| 1771 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1772 | /* number */ |
| 1773 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1774 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1775 | 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] | 1776 | range_expected = 0; |
| 1777 | } else { |
| 1778 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1779 | 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] | 1780 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1781 | part->max_64 = part->min_64; |
| 1782 | } |
| 1783 | |
| 1784 | /* continue with possible another expression part */ |
| 1785 | } else if (!strncmp(expr, "max", 3)) { |
| 1786 | expr += 3; |
| 1787 | while (isspace(*expr)) { |
| 1788 | expr++; |
| 1789 | } |
| 1790 | if (*expr != '\0') { |
| 1791 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1792 | length_restr ? "length" : "range", expr); |
| 1793 | goto cleanup; |
| 1794 | } |
| 1795 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1796 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1797 | 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] | 1798 | range_expected = 0; |
| 1799 | } else { |
| 1800 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1801 | 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] | 1802 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1803 | part->min_64 = part->max_64; |
| 1804 | } |
| 1805 | } else { |
| 1806 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1807 | length_restr ? "length" : "range", expr); |
| 1808 | goto cleanup; |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | /* check with the previous range/length restriction */ |
| 1813 | if (base_range) { |
| 1814 | switch (basetype) { |
| 1815 | case LY_TYPE_BINARY: |
| 1816 | case LY_TYPE_UINT8: |
| 1817 | case LY_TYPE_UINT16: |
| 1818 | case LY_TYPE_UINT32: |
| 1819 | case LY_TYPE_UINT64: |
| 1820 | case LY_TYPE_STRING: |
| 1821 | uns = 1; |
| 1822 | break; |
| 1823 | case LY_TYPE_DEC64: |
| 1824 | case LY_TYPE_INT8: |
| 1825 | case LY_TYPE_INT16: |
| 1826 | case LY_TYPE_INT32: |
| 1827 | case LY_TYPE_INT64: |
| 1828 | uns = 0; |
| 1829 | break; |
| 1830 | default: |
| 1831 | LOGINT(ctx->ctx); |
| 1832 | ret = LY_EINT; |
| 1833 | goto cleanup; |
| 1834 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1835 | 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] | 1836 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1837 | goto baseerror; |
| 1838 | } |
| 1839 | /* current lower bound is not lower than the base */ |
| 1840 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1841 | /* base has single value */ |
| 1842 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1843 | /* both lower bounds are the same */ |
| 1844 | if (parts[u].min_64 != parts[u].max_64) { |
| 1845 | /* current continues with a range */ |
| 1846 | goto baseerror; |
| 1847 | } else { |
| 1848 | /* equal single values, move both forward */ |
| 1849 | ++v; |
| 1850 | continue; |
| 1851 | } |
| 1852 | } else { |
| 1853 | /* base is single value lower than current range, so the |
| 1854 | * value from base range is removed in the current, |
| 1855 | * move only base and repeat checking */ |
| 1856 | ++v; |
| 1857 | --u; |
| 1858 | continue; |
| 1859 | } |
| 1860 | } else { |
| 1861 | /* base is the range */ |
| 1862 | if (parts[u].min_64 == parts[u].max_64) { |
| 1863 | /* current is a single value */ |
| 1864 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1865 | /* current is behind the base range, so base range is omitted, |
| 1866 | * move the base and keep the current for further check */ |
| 1867 | ++v; |
| 1868 | --u; |
| 1869 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1870 | continue; |
| 1871 | } else { |
| 1872 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1873 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1874 | /* higher bound is higher than the current higher bound */ |
| 1875 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1876 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1877 | * continue with the same current, but move the base */ |
| 1878 | --u; |
| 1879 | ++v; |
| 1880 | continue; |
| 1881 | } |
| 1882 | /* current range starts within the base range but end behind it */ |
| 1883 | goto baseerror; |
| 1884 | } else { |
| 1885 | /* current range is smaller than the base, |
| 1886 | * move current, but stay with the base */ |
| 1887 | continue; |
| 1888 | } |
| 1889 | } |
| 1890 | } |
| 1891 | } |
| 1892 | if (u != parts_done) { |
| 1893 | baseerror: |
| 1894 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1895 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1896 | length_restr ? "length" : "range", range_p->arg); |
| 1897 | goto cleanup; |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | if (!(*range)) { |
| 1902 | *range = calloc(1, sizeof **range); |
| 1903 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1904 | } |
| 1905 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1906 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1907 | if (range_p->eapptag) { |
| 1908 | lydict_remove(ctx->ctx, (*range)->eapptag); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 1909 | 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] | 1910 | } |
| 1911 | if (range_p->emsg) { |
| 1912 | lydict_remove(ctx->ctx, (*range)->emsg); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 1913 | 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] | 1914 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1915 | if (range_p->dsc) { |
| 1916 | lydict_remove(ctx->ctx, (*range)->dsc); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 1917 | 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] | 1918 | } |
| 1919 | if (range_p->ref) { |
| 1920 | lydict_remove(ctx->ctx, (*range)->ref); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 1921 | 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] | 1922 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1923 | /* extensions are taken only from the last range by the caller */ |
| 1924 | |
| 1925 | (*range)->parts = parts; |
| 1926 | parts = NULL; |
| 1927 | ret = LY_SUCCESS; |
| 1928 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1929 | LY_ARRAY_FREE(parts); |
| 1930 | |
| 1931 | return ret; |
| 1932 | } |
| 1933 | |
| 1934 | /** |
| 1935 | * @brief Checks pattern syntax. |
| 1936 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1937 | * @param[in] ctx Context. |
| 1938 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1939 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1940 | * @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] | 1941 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1942 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1943 | LY_ERR |
| 1944 | 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] | 1945 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1946 | size_t idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1947 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1948 | int err_code; |
| 1949 | const char *orig_ptr; |
| 1950 | PCRE2_SIZE err_offset; |
| 1951 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1952 | #define URANGE_LEN 19 |
| 1953 | char *ublock2urange[][2] = { |
| 1954 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1955 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1956 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1957 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1958 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1959 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1960 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1961 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1962 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1963 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1964 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1965 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1966 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1967 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1968 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1969 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1970 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1971 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1972 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1973 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1974 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1975 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1976 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1977 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1978 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1979 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1980 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1981 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1982 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1983 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1984 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1985 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1986 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1987 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1988 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1989 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1990 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1991 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1992 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1993 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1994 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1995 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1996 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1997 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1998 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1999 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 2000 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 2001 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 2002 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 2003 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 2004 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 2005 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 2006 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 2007 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 2008 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 2009 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 2010 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 2011 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 2012 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 2013 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 2014 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 2015 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 2016 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 2017 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 2018 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 2019 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 2020 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 2021 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 2022 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 2023 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 2024 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 2025 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 2026 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 2027 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 2028 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 2029 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 2030 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 2031 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 2032 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 2033 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 2034 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 2035 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 2036 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 2037 | {NULL, NULL} |
| 2038 | }; |
| 2039 | |
| 2040 | /* adjust the expression to a Perl equivalent |
| 2041 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 2042 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2043 | /* allocate space for the transformed pattern */ |
| 2044 | size = strlen(pattern) + 1; |
| 2045 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2046 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2047 | perl_regex[0] = '\0'; |
| 2048 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2049 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 2050 | brack = 0; |
| 2051 | idx = 0; |
| 2052 | orig_ptr = pattern; |
| 2053 | while (orig_ptr[0]) { |
| 2054 | switch (orig_ptr[0]) { |
| 2055 | case '$': |
| 2056 | case '^': |
| 2057 | if (!brack) { |
| 2058 | /* make space for the extra character */ |
| 2059 | ++size; |
| 2060 | perl_regex = ly_realloc(perl_regex, size); |
| 2061 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2062 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2063 | /* print escape slash */ |
| 2064 | perl_regex[idx] = '\\'; |
| 2065 | ++idx; |
| 2066 | } |
| 2067 | break; |
| 2068 | case '[': |
| 2069 | /* must not be escaped */ |
| 2070 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2071 | ++brack; |
| 2072 | } |
| 2073 | break; |
| 2074 | case ']': |
| 2075 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2076 | /* pattern was checked and compiled already */ |
| 2077 | assert(brack); |
| 2078 | --brack; |
| 2079 | } |
| 2080 | break; |
| 2081 | default: |
| 2082 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2083 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2084 | |
| 2085 | /* copy char */ |
| 2086 | perl_regex[idx] = orig_ptr[0]; |
| 2087 | |
| 2088 | ++idx; |
| 2089 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2090 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2091 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2092 | |
| 2093 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2094 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2095 | start = ptr - perl_regex; |
| 2096 | |
| 2097 | ptr = strchr(ptr, '}'); |
| 2098 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2099 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2100 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 2101 | free(perl_regex); |
| 2102 | return LY_EVALID; |
| 2103 | } |
| 2104 | end = (ptr - perl_regex) + 1; |
| 2105 | |
| 2106 | /* need more space */ |
| 2107 | if (end - start < URANGE_LEN) { |
| 2108 | 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] | 2109 | 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] | 2110 | } |
| 2111 | |
| 2112 | /* find our range */ |
| 2113 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2114 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2115 | break; |
| 2116 | } |
| 2117 | } |
| 2118 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2119 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2120 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2121 | free(perl_regex); |
| 2122 | return LY_EVALID; |
| 2123 | } |
| 2124 | |
| 2125 | /* 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] | 2126 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2127 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2128 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2129 | } |
| 2130 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2131 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2132 | } |
| 2133 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2134 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2135 | /* skip brackets */ |
| 2136 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2137 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2138 | } else { |
| 2139 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2140 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2145 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2146 | 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] | 2147 | &err_code, &err_offset, NULL); |
| 2148 | if (!code_local) { |
| 2149 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2150 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2151 | 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] | 2152 | free(perl_regex); |
| 2153 | return LY_EVALID; |
| 2154 | } |
| 2155 | free(perl_regex); |
| 2156 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2157 | if (code) { |
| 2158 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2159 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2160 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | return LY_SUCCESS; |
| 2164 | |
| 2165 | #undef URANGE_LEN |
| 2166 | } |
| 2167 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2168 | /** |
| 2169 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2170 | * @param[in] ctx Compile context. |
| 2171 | * @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] | 2172 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2173 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2174 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2175 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2176 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2177 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2178 | 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] | 2179 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2180 | { |
| 2181 | struct lysc_pattern **pattern; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2182 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2183 | LY_ERR ret = LY_SUCCESS; |
| 2184 | |
| 2185 | /* first, copy the patterns from the base type */ |
| 2186 | if (base_patterns) { |
| 2187 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2188 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2189 | } |
| 2190 | |
| 2191 | LY_ARRAY_FOR(patterns_p, u) { |
| 2192 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2193 | *pattern = calloc(1, sizeof **pattern); |
| 2194 | ++(*pattern)->refcount; |
| 2195 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2196 | 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] | 2197 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2198 | |
| 2199 | if (patterns_p[u].arg[0] == 0x15) { |
| 2200 | (*pattern)->inverted = 1; |
| 2201 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 2202 | DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr, ret, done); |
| 2203 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done); |
| 2204 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done); |
| 2205 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done); |
| 2206 | 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] | 2207 | 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] | 2208 | } |
| 2209 | done: |
| 2210 | return ret; |
| 2211 | } |
| 2212 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2213 | /** |
| 2214 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2215 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2216 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2217 | 0 /* LY_TYPE_UNKNOWN */, |
| 2218 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2219 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2220 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2221 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2222 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2223 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2224 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2225 | 0 /* LY_TYPE_BOOL */, |
| 2226 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2227 | 0 /* LY_TYPE_EMPTY */, |
| 2228 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2229 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2230 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2231 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2232 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2233 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2234 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2235 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2236 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2237 | }; |
| 2238 | |
| 2239 | /** |
| 2240 | * @brief stringification of the YANG built-in data types |
| 2241 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2242 | 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] | 2243 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2244 | "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] | 2245 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2246 | /** |
| 2247 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2248 | * @param[in] ctx Compile context. |
| 2249 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2250 | * @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] | 2251 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2252 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2253 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2254 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2255 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2256 | 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] | 2257 | 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] | 2258 | { |
| 2259 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2260 | LY_ARRAY_COUNT_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2261 | int32_t value = 0; |
| 2262 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2263 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2264 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2265 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2266 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2267 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2268 | return LY_EVALID; |
| 2269 | } |
| 2270 | |
| 2271 | LY_ARRAY_FOR(enums_p, u) { |
| 2272 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 2273 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done); |
| 2274 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->dsc, ret, done); |
| 2275 | 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] | 2276 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2277 | if (base_enums) { |
| 2278 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2279 | LY_ARRAY_FOR(base_enums, v) { |
| 2280 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2281 | break; |
| 2282 | } |
| 2283 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2284 | if (v == LY_ARRAY_COUNT(base_enums)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2285 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2286 | "Invalid %s - derived type adds new item \"%s\".", |
| 2287 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2288 | return LY_EVALID; |
| 2289 | } |
| 2290 | match = v; |
| 2291 | } |
| 2292 | |
| 2293 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2294 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2295 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2296 | e->value = (int32_t)enums_p[u].value; |
| 2297 | if (!u || e->value >= value) { |
| 2298 | value = e->value + 1; |
| 2299 | } |
| 2300 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2301 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2302 | if (e->value == (*enums)[v].value) { |
| 2303 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2304 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2305 | e->value, e->name, (*enums)[v].name); |
| 2306 | return LY_EVALID; |
| 2307 | } |
| 2308 | } |
| 2309 | } else if (base_enums) { |
| 2310 | /* inherit the assigned value */ |
| 2311 | e->value = base_enums[match].value; |
| 2312 | if (!u || e->value >= value) { |
| 2313 | value = e->value + 1; |
| 2314 | } |
| 2315 | } else { |
| 2316 | /* assign value automatically */ |
| 2317 | if (u && value == INT32_MIN) { |
| 2318 | /* counter overflow */ |
| 2319 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2320 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2321 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2322 | return LY_EVALID; |
| 2323 | } |
| 2324 | e->value = value++; |
| 2325 | } |
| 2326 | } else { /* LY_TYPE_BITS */ |
| 2327 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2328 | e->value = (int32_t)enums_p[u].value; |
| 2329 | if (!u || (uint32_t)e->value >= position) { |
| 2330 | position = (uint32_t)e->value + 1; |
| 2331 | } |
| 2332 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2333 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2334 | if (e->value == (*enums)[v].value) { |
| 2335 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2336 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2337 | (uint32_t)e->value, e->name, (*enums)[v].name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2338 | return LY_EVALID; |
| 2339 | } |
| 2340 | } |
| 2341 | } else if (base_enums) { |
| 2342 | /* inherit the assigned value */ |
| 2343 | e->value = base_enums[match].value; |
| 2344 | if (!u || (uint32_t)e->value >= position) { |
| 2345 | position = (uint32_t)e->value + 1; |
| 2346 | } |
| 2347 | } else { |
| 2348 | /* assign value automatically */ |
| 2349 | if (u && position == 0) { |
| 2350 | /* counter overflow */ |
| 2351 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2352 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2353 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2354 | return LY_EVALID; |
| 2355 | } |
| 2356 | e->value = position++; |
| 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | if (base_enums) { |
| 2361 | /* the assigned values must not change from the derived type */ |
| 2362 | if (e->value != base_enums[match].value) { |
| 2363 | if (basetype == LY_TYPE_ENUM) { |
| 2364 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2365 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2366 | e->name, base_enums[match].value, e->value); |
| 2367 | } else { |
| 2368 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2369 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2370 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2371 | } |
| 2372 | return LY_EVALID; |
| 2373 | } |
| 2374 | } |
| 2375 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2376 | 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] | 2377 | 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] | 2378 | |
| 2379 | if (basetype == LY_TYPE_BITS) { |
| 2380 | /* keep bits ordered by position */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2381 | for (v = u; v && (*enums)[v - 1].value > e->value; --v) {} |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2382 | if (v != u) { |
| 2383 | memcpy(&storage, e, sizeof *e); |
| 2384 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2385 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2386 | } |
| 2387 | } |
| 2388 | } |
| 2389 | |
| 2390 | done: |
| 2391 | return ret; |
| 2392 | } |
| 2393 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2394 | /** |
| 2395 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2396 | * |
| 2397 | * path-arg = absolute-path / relative-path |
| 2398 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2399 | * relative-path = 1*(".." "/") descendant-path |
| 2400 | * |
| 2401 | * @param[in,out] path Path to parse. |
| 2402 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2403 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2404 | * @param[out] name Name of the token. |
| 2405 | * @param[out] nam_len Length of the name. |
| 2406 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2407 | * must not be changed between consecutive calls. -1 if the |
| 2408 | * path is absolute. |
| 2409 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2410 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2411 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2412 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2413 | 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] | 2414 | int32_t *parent_times, ly_bool *has_predicate) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2415 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2416 | int32_t par_times = 0; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2417 | |
| 2418 | assert(path && *path); |
| 2419 | assert(parent_times); |
| 2420 | assert(prefix); |
| 2421 | assert(prefix_len); |
| 2422 | assert(name); |
| 2423 | assert(name_len); |
| 2424 | assert(has_predicate); |
| 2425 | |
| 2426 | *prefix = NULL; |
| 2427 | *prefix_len = 0; |
| 2428 | *name = NULL; |
| 2429 | *name_len = 0; |
| 2430 | *has_predicate = 0; |
| 2431 | |
| 2432 | if (!*parent_times) { |
| 2433 | if (!strncmp(*path, "..", 2)) { |
| 2434 | *path += 2; |
| 2435 | ++par_times; |
| 2436 | while (!strncmp(*path, "/..", 3)) { |
| 2437 | *path += 3; |
| 2438 | ++par_times; |
| 2439 | } |
| 2440 | } |
| 2441 | if (par_times) { |
| 2442 | *parent_times = par_times; |
| 2443 | } else { |
| 2444 | *parent_times = -1; |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | if (**path != '/') { |
| 2449 | return LY_EINVAL; |
| 2450 | } |
| 2451 | /* skip '/' */ |
| 2452 | ++(*path); |
| 2453 | |
| 2454 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2455 | 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] | 2456 | |
| 2457 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2458 | /* path continues by another token or this is the last token */ |
| 2459 | return LY_SUCCESS; |
| 2460 | } else if ((*path)[0] != '[') { |
| 2461 | /* unexpected character */ |
| 2462 | return LY_EINVAL; |
| 2463 | } else { |
| 2464 | /* predicate starting with [ */ |
| 2465 | *has_predicate = 1; |
| 2466 | return LY_SUCCESS; |
| 2467 | } |
| 2468 | } |
| 2469 | |
| 2470 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2471 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2472 | * |
| 2473 | * The set of features used for target must be a subset of features used for the leafref. |
| 2474 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2475 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2476 | * |
| 2477 | * @param[in] refnode The leafref node. |
| 2478 | * @param[in] target Tha target node of the leafref. |
| 2479 | * @return LY_SUCCESS or LY_EVALID; |
| 2480 | */ |
| 2481 | static LY_ERR |
| 2482 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2483 | { |
| 2484 | LY_ERR ret = LY_EVALID; |
| 2485 | const struct lysc_node *iter; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2486 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2487 | struct ly_set features = {0}; |
| 2488 | |
| 2489 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2490 | if (iter->iffeatures) { |
| 2491 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2492 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2493 | 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] | 2494 | } |
| 2495 | } |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | /* 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] | 2500 | * 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] | 2501 | * to the leafref itself. */ |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2502 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2503 | if (iter->iffeatures) { |
| 2504 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2505 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2506 | if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) { |
| 2507 | /* feature not present */ |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2508 | goto cleanup; |
| 2509 | } |
| 2510 | } |
| 2511 | } |
| 2512 | } |
| 2513 | } |
| 2514 | ret = LY_SUCCESS; |
| 2515 | |
| 2516 | cleanup: |
| 2517 | ly_set_erase(&features, NULL); |
| 2518 | return ret; |
| 2519 | } |
| 2520 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2521 | 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] | 2522 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2523 | 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] | 2524 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2525 | /** |
| 2526 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2527 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2528 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2529 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2530 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2531 | * @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] | 2532 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2533 | * @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] | 2534 | * @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] | 2535 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2536 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2537 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2538 | * @return LY_ERR value. |
| 2539 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2540 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2541 | 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] | 2542 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2543 | struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, |
| 2544 | struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2545 | { |
| 2546 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2547 | struct lysc_type_bin *bin; |
| 2548 | struct lysc_type_num *num; |
| 2549 | struct lysc_type_str *str; |
| 2550 | struct lysc_type_bits *bits; |
| 2551 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2552 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2553 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2554 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2555 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2556 | |
| 2557 | switch (basetype) { |
| 2558 | case LY_TYPE_BINARY: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2559 | bin = (struct lysc_type_bin *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2560 | |
| 2561 | /* 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] | 2562 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2563 | 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] | 2564 | base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2565 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2566 | 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] | 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | if (tpdfname) { |
| 2571 | type_p->compiled = *type; |
| 2572 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2573 | } |
| 2574 | break; |
| 2575 | case LY_TYPE_BITS: |
| 2576 | /* RFC 7950 9.7 - bits */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2577 | bits = (struct lysc_type_bits *)(*type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2578 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2579 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2580 | base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL, |
| 2581 | (struct lysc_type_bitenum_item **)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2582 | } |
| 2583 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2584 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2585 | /* 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] | 2586 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2587 | 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] | 2588 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2589 | 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] | 2590 | free(*type); |
| 2591 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2592 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2593 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | if (tpdfname) { |
| 2597 | type_p->compiled = *type; |
| 2598 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2599 | } |
| 2600 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2601 | case LY_TYPE_DEC64: |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2602 | dec = (struct lysc_type_dec *)(*type); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2603 | |
| 2604 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2605 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2606 | if (!type_p->fraction_digits) { |
| 2607 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2608 | 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] | 2609 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2610 | 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] | 2611 | free(*type); |
| 2612 | *type = NULL; |
| 2613 | } |
| 2614 | return LY_EVALID; |
| 2615 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2616 | dec->fraction_digits = type_p->fraction_digits; |
| 2617 | } else { |
| 2618 | if (type_p->fraction_digits) { |
| 2619 | /* fraction digits is prohibited in types not directly derived from built-in decimal64 */ |
| 2620 | if (tpdfname) { |
| 2621 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2622 | "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.", |
| 2623 | tpdfname); |
| 2624 | } else { |
| 2625 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2626 | "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type."); |
| 2627 | free(*type); |
| 2628 | *type = NULL; |
| 2629 | } |
| 2630 | return LY_EVALID; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2631 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2632 | dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2633 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2634 | |
| 2635 | /* RFC 7950 9.2.4 - range */ |
| 2636 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2637 | 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] | 2638 | base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2639 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2640 | 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] | 2641 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | if (tpdfname) { |
| 2645 | type_p->compiled = *type; |
| 2646 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2647 | } |
| 2648 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2649 | case LY_TYPE_STRING: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2650 | str = (struct lysc_type_str *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2651 | |
| 2652 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2653 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2654 | 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] | 2655 | base ? ((struct lysc_type_str *)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2656 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2657 | 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] | 2658 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2659 | } else if (base && ((struct lysc_type_str *)base)->length) { |
| 2660 | 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] | 2661 | } |
| 2662 | |
| 2663 | /* RFC 7950 9.4.5 - pattern */ |
| 2664 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2665 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2666 | base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns)); |
| 2667 | } else if (base && ((struct lysc_type_str *)base)->patterns) { |
| 2668 | 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] | 2669 | } |
| 2670 | |
| 2671 | if (tpdfname) { |
| 2672 | type_p->compiled = *type; |
| 2673 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2674 | } |
| 2675 | break; |
| 2676 | case LY_TYPE_ENUM: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2677 | enumeration = (struct lysc_type_enum *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2678 | |
| 2679 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2680 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2681 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2682 | base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2683 | } |
| 2684 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2685 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2686 | /* 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] | 2687 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2688 | 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] | 2689 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2690 | 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] | 2691 | free(*type); |
| 2692 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2693 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2694 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | if (tpdfname) { |
| 2698 | type_p->compiled = *type; |
| 2699 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2700 | } |
| 2701 | break; |
| 2702 | case LY_TYPE_INT8: |
| 2703 | case LY_TYPE_UINT8: |
| 2704 | case LY_TYPE_INT16: |
| 2705 | case LY_TYPE_UINT16: |
| 2706 | case LY_TYPE_INT32: |
| 2707 | case LY_TYPE_UINT32: |
| 2708 | case LY_TYPE_INT64: |
| 2709 | case LY_TYPE_UINT64: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2710 | num = (struct lysc_type_num *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2711 | |
| 2712 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2713 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2714 | 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] | 2715 | base ? ((struct lysc_type_num *)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2716 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2717 | 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] | 2718 | } |
| 2719 | } |
| 2720 | |
| 2721 | if (tpdfname) { |
| 2722 | type_p->compiled = *type; |
| 2723 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2724 | } |
| 2725 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2726 | case LY_TYPE_IDENT: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2727 | idref = (struct lysc_type_identityref *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2728 | |
| 2729 | /* RFC 7950 9.10.2 - base */ |
| 2730 | if (type_p->bases) { |
| 2731 | if (base) { |
| 2732 | /* only the directly derived identityrefs can contain base specification */ |
| 2733 | if (tpdfname) { |
| 2734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2735 | "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] | 2736 | tpdfname); |
| 2737 | } else { |
| 2738 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2739 | "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] | 2740 | free(*type); |
| 2741 | *type = NULL; |
| 2742 | } |
| 2743 | return LY_EVALID; |
| 2744 | } |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2745 | 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] | 2746 | } |
| 2747 | |
| 2748 | if (!base && !type_p->flags) { |
| 2749 | /* type derived from identityref built-in type must contain at least one base */ |
| 2750 | if (tpdfname) { |
| 2751 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2752 | } else { |
| 2753 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2754 | free(*type); |
| 2755 | *type = NULL; |
| 2756 | } |
| 2757 | return LY_EVALID; |
| 2758 | } |
| 2759 | |
| 2760 | if (tpdfname) { |
| 2761 | type_p->compiled = *type; |
| 2762 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2763 | } |
| 2764 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2765 | case LY_TYPE_LEAFREF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2766 | lref = (struct lysc_type_leafref *)*type; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2767 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2768 | /* RFC 7950 9.9.3 - require-instance */ |
| 2769 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2770 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2771 | if (tpdfname) { |
| 2772 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2773 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2774 | } else { |
| 2775 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2776 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2777 | free(*type); |
| 2778 | *type = NULL; |
| 2779 | } |
| 2780 | return LY_EVALID; |
| 2781 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2782 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2783 | } else if (base) { |
| 2784 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2785 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2786 | } else { |
| 2787 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2788 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2789 | } |
| 2790 | if (type_p->path) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2791 | lref->path = lyxp_expr_dup(ctx->ctx, type_p->path); |
| 2792 | lref->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2793 | } else if (base) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2794 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path); |
| 2795 | lref->path_context = ((struct lysc_type_leafref *)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2796 | } else if (tpdfname) { |
| 2797 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2798 | return LY_EVALID; |
| 2799 | } else { |
| 2800 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2801 | free(*type); |
| 2802 | *type = NULL; |
| 2803 | return LY_EVALID; |
| 2804 | } |
| 2805 | if (tpdfname) { |
| 2806 | type_p->compiled = *type; |
| 2807 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2808 | } |
| 2809 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2810 | case LY_TYPE_INST: |
| 2811 | /* RFC 7950 9.9.3 - require-instance */ |
| 2812 | if (type_p->flags & LYS_SET_REQINST) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2813 | ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2814 | } else { |
| 2815 | /* default is true */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2816 | ((struct lysc_type_instanceid *)(*type))->require_instance = 1; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | if (tpdfname) { |
| 2820 | type_p->compiled = *type; |
| 2821 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2822 | } |
| 2823 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2824 | case LY_TYPE_UNION: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2825 | un = (struct lysc_type_union *)(*type); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2826 | |
| 2827 | /* RFC 7950 7.4 - type */ |
| 2828 | if (type_p->types) { |
| 2829 | if (base) { |
| 2830 | /* only the directly derived union can contain types specification */ |
| 2831 | if (tpdfname) { |
| 2832 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2833 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2834 | tpdfname); |
| 2835 | } else { |
| 2836 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2837 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2838 | free(*type); |
| 2839 | *type = NULL; |
| 2840 | } |
| 2841 | return LY_EVALID; |
| 2842 | } |
| 2843 | /* compile the type */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2844 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID); |
| 2845 | 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] | 2846 | 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] | 2847 | &type_p->types[u], &un->types[u + additional], NULL, NULL, NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2848 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2849 | /* add space for additional types from the union subtype */ |
| 2850 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2851 | LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1, |
| 2852 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2853 | |
| 2854 | /* copy subtypes of the subtype union */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2855 | 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] | 2856 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2857 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2858 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2859 | 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] | 2860 | lref = (struct lysc_type_leafref *)un->types[u + additional]; |
| 2861 | |
| 2862 | lref->basetype = LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2863 | 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] | 2864 | lref->refcount = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2865 | lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance; |
| 2866 | 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] | 2867 | /* TODO extensions */ |
| 2868 | |
| 2869 | } else { |
| 2870 | un->types[u + additional] = un_aux->types[v]; |
| 2871 | ++un_aux->types[v]->refcount; |
| 2872 | } |
| 2873 | ++additional; |
| 2874 | LY_ARRAY_INCREMENT(un->types); |
| 2875 | } |
| 2876 | /* compensate u increment in main loop */ |
| 2877 | --additional; |
| 2878 | |
| 2879 | /* free the replaced union subtype */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2880 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2881 | } else { |
| 2882 | LY_ARRAY_INCREMENT(un->types); |
| 2883 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2884 | } |
| 2885 | } |
| 2886 | |
| 2887 | if (!base && !type_p->flags) { |
| 2888 | /* type derived from union built-in type must contain at least one type */ |
| 2889 | if (tpdfname) { |
| 2890 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2891 | } else { |
| 2892 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2893 | free(*type); |
| 2894 | *type = NULL; |
| 2895 | } |
| 2896 | return LY_EVALID; |
| 2897 | } |
| 2898 | |
| 2899 | if (tpdfname) { |
| 2900 | type_p->compiled = *type; |
| 2901 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2902 | } |
| 2903 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2904 | case LY_TYPE_BOOL: |
| 2905 | case LY_TYPE_EMPTY: |
| 2906 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2907 | break; |
| 2908 | } |
| 2909 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2910 | done: |
| 2911 | return ret; |
| 2912 | } |
| 2913 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2914 | /** |
| 2915 | * @brief Compile information about the leaf/leaf-list's type. |
| 2916 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2917 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2918 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2919 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2920 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2921 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2922 | * @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] | 2923 | * @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] | 2924 | * @param[out] dflt Default value for the type. |
| 2925 | * @param[out] dflt_mod Local module for the default value. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2926 | * @return LY_ERR value. |
| 2927 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2928 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2929 | 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] | 2930 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2931 | 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] | 2932 | { |
| 2933 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2934 | ly_bool dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2935 | struct type_context { |
| 2936 | const struct lysp_tpdf *tpdf; |
| 2937 | struct lysp_node *node; |
| 2938 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2939 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2940 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2941 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2942 | struct ly_set tpdf_chain = {0}; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2943 | |
| 2944 | assert((dflt && dflt_mod) || (!dflt && !dflt_mod)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2945 | |
| 2946 | (*type) = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2947 | if (dflt) { |
| 2948 | *dflt = NULL; |
| 2949 | *dflt_mod = NULL; |
| 2950 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2951 | |
| 2952 | tctx = calloc(1, sizeof *tctx); |
| 2953 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2954 | 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] | 2955 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2956 | ret == LY_SUCCESS; |
| 2957 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2958 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2959 | if (basetype) { |
| 2960 | break; |
| 2961 | } |
| 2962 | |
| 2963 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2964 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2965 | tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2966 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2967 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2968 | if (units && !*units) { |
| 2969 | /* inherit units */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 2970 | DUP_STRING_GOTO(ctx->ctx, tctx->tpdf->units, *units, ret, cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2971 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2972 | if (dflt && !*dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2973 | /* inherit default */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2974 | *dflt = tctx->tpdf->dflt; |
| 2975 | *dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2976 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2977 | if (dummyloops && (!units || *units) && dflt && *dflt) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2978 | 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] | 2979 | break; |
| 2980 | } |
| 2981 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2982 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2983 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2984 | * 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] | 2985 | basetype = tctx->tpdf->type.compiled->basetype; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2986 | ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
| 2987 | LY_CHECK_GOTO(ret, cleanup); |
| 2988 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2989 | if ((units && !*units) || (dflt && !*dflt)) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2990 | dummyloops = 1; |
| 2991 | goto preparenext; |
| 2992 | } else { |
| 2993 | tctx = NULL; |
| 2994 | break; |
| 2995 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2996 | } |
| 2997 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2998 | /* circular typedef reference detection */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2999 | for (uint32_t u = 0; u < tpdf_chain.count; u++) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3000 | /* local part */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3001 | tctx_iter = (struct type_context *)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3002 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3003 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3004 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3005 | free(tctx); |
| 3006 | ret = LY_EVALID; |
| 3007 | goto cleanup; |
| 3008 | } |
| 3009 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3010 | for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3011 | /* global part for unions corner case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3012 | tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3013 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3014 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3015 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3016 | free(tctx); |
| 3017 | ret = LY_EVALID; |
| 3018 | goto cleanup; |
| 3019 | } |
| 3020 | } |
| 3021 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3022 | /* store information for the following processing */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3023 | ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
| 3024 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3025 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3026 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3027 | /* prepare next loop */ |
| 3028 | tctx_prev = tctx; |
| 3029 | tctx = calloc(1, sizeof *tctx); |
| 3030 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3031 | } |
| 3032 | free(tctx); |
| 3033 | |
| 3034 | /* allocate type according to the basetype */ |
| 3035 | switch (basetype) { |
| 3036 | case LY_TYPE_BINARY: |
| 3037 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3038 | break; |
| 3039 | case LY_TYPE_BITS: |
| 3040 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3041 | break; |
| 3042 | case LY_TYPE_BOOL: |
| 3043 | case LY_TYPE_EMPTY: |
| 3044 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3045 | break; |
| 3046 | case LY_TYPE_DEC64: |
| 3047 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3048 | break; |
| 3049 | case LY_TYPE_ENUM: |
| 3050 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3051 | break; |
| 3052 | case LY_TYPE_IDENT: |
| 3053 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3054 | break; |
| 3055 | case LY_TYPE_INST: |
| 3056 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3057 | break; |
| 3058 | case LY_TYPE_LEAFREF: |
| 3059 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3060 | break; |
| 3061 | case LY_TYPE_STRING: |
| 3062 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3063 | break; |
| 3064 | case LY_TYPE_UNION: |
| 3065 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3066 | break; |
| 3067 | case LY_TYPE_INT8: |
| 3068 | case LY_TYPE_UINT8: |
| 3069 | case LY_TYPE_INT16: |
| 3070 | case LY_TYPE_UINT16: |
| 3071 | case LY_TYPE_INT32: |
| 3072 | case LY_TYPE_UINT32: |
| 3073 | case LY_TYPE_INT64: |
| 3074 | case LY_TYPE_UINT64: |
| 3075 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3076 | break; |
| 3077 | case LY_TYPE_UNKNOWN: |
| 3078 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3079 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3080 | ret = LY_EVALID; |
| 3081 | goto cleanup; |
| 3082 | } |
| 3083 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3084 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3085 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3086 | ly_data_type2str[basetype]); |
| 3087 | free(*type); |
| 3088 | (*type) = NULL; |
| 3089 | ret = LY_EVALID; |
| 3090 | goto cleanup; |
| 3091 | } |
| 3092 | |
| 3093 | /* get restrictions from the referred typedefs */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3094 | for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3095 | tctx = (struct type_context *)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3096 | |
| 3097 | /* remember the typedef context for circular check */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3098 | ret = ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
| 3099 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3100 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3101 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3102 | base = tctx->tpdf->type.compiled; |
| 3103 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3104 | } 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] | 3105 | /* no change, just use the type information from the base */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3106 | 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] | 3107 | ++base->refcount; |
| 3108 | continue; |
| 3109 | } |
| 3110 | |
| 3111 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3112 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3113 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3114 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3115 | ret = LY_EVALID; |
| 3116 | goto cleanup; |
| 3117 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3118 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3119 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3120 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3121 | ret = LY_EVALID; |
| 3122 | goto cleanup; |
| 3123 | } |
| 3124 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3125 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3126 | /* TODO user type plugins */ |
| 3127 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3128 | prev_type = *type; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3129 | 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] | 3130 | 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] | 3131 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3132 | LY_CHECK_GOTO(ret, cleanup); |
| 3133 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3134 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3135 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3136 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3137 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3138 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3139 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3140 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3141 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3142 | /* TODO user type plugins */ |
| 3143 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3144 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3145 | 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] | 3146 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 90b148f | 2020-05-06 17:49:40 +0200 | [diff] [blame] | 3147 | } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3148 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3149 | free(*type); |
| 3150 | (*type) = base; |
| 3151 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3152 | } |
| 3153 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3154 | 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] | 3155 | |
| 3156 | cleanup: |
| 3157 | ly_set_erase(&tpdf_chain, free); |
| 3158 | return ret; |
| 3159 | } |
| 3160 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3161 | /** |
| 3162 | * @brief Compile status information of the given node. |
| 3163 | * |
| 3164 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3165 | * has the status correctly set during the compilation. |
| 3166 | * |
| 3167 | * @param[in] ctx Compile context |
| 3168 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3169 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3170 | * the compatibility with the parent's status value. |
| 3171 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3172 | * @return LY_ERR value. |
| 3173 | */ |
| 3174 | static LY_ERR |
| 3175 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3176 | { |
| 3177 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3178 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3179 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3180 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3181 | if ((parent_flags & 0x3) != 0x3) { |
| 3182 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3183 | * combination of bits (0x3) which marks the uses_status value */ |
| 3184 | 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] | 3185 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3186 | } |
| 3187 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3188 | } else { |
| 3189 | (*node_flags) |= LYS_STATUS_CURR; |
| 3190 | } |
| 3191 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3192 | /* check status compatibility with the parent */ |
| 3193 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3194 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3195 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3196 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3197 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3198 | } else { /* LYS_STATUS_DEPRC */ |
| 3199 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3200 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3201 | } |
| 3202 | return LY_EVALID; |
| 3203 | } |
| 3204 | } |
| 3205 | return LY_SUCCESS; |
| 3206 | } |
| 3207 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3208 | /** |
| 3209 | * @brief Check uniqness of the node/action/notification name. |
| 3210 | * |
| 3211 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3212 | * structures, but they share the namespace so we need to check their name collisions. |
| 3213 | * |
| 3214 | * @param[in] ctx Compile context. |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3215 | * @param[in] parent Parent of the nodes to check, can be NULL. |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3216 | * @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] | 3217 | * @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] | 3218 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3219 | */ |
| 3220 | static LY_ERR |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3221 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name, |
| 3222 | const struct lysc_node *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3223 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3224 | const struct lysc_node *iter, *iter2; |
| 3225 | const struct lysc_action *actions; |
| 3226 | const struct lysc_notif *notifs; |
| 3227 | uint32_t getnext_flags; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3228 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3229 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3230 | #define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name)) |
| 3231 | |
| 3232 | if (exclude->nodetype == LYS_CASE) { |
| 3233 | /* check restricted only to all the cases */ |
| 3234 | assert(parent->nodetype == LYS_CHOICE); |
| 3235 | LY_LIST_FOR(lysc_node_children(parent, 0), iter) { |
| 3236 | if (CHECK_NODE(iter, exclude, name)) { |
| 3237 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case"); |
| 3238 | return LY_EEXIST; |
| 3239 | } |
| 3240 | } |
| 3241 | |
| 3242 | return LY_SUCCESS; |
| 3243 | } |
| 3244 | |
| 3245 | /* no reason for our parent to be choice anymore */ |
| 3246 | assert(!parent || (parent->nodetype != LYS_CHOICE)); |
| 3247 | |
| 3248 | if (parent && (parent->nodetype == LYS_CASE)) { |
| 3249 | /* move to the first data definition parent */ |
| 3250 | parent = lysc_data_parent(parent); |
| 3251 | } |
| 3252 | |
| 3253 | getnext_flags = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE; |
| 3254 | if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) { |
| 3255 | getnext_flags |= LYS_GETNEXT_OUTPUT; |
| 3256 | } |
| 3257 | |
| 3258 | iter = NULL; |
| 3259 | while ((iter = lys_getnext(iter, parent, ctx->mod->compiled, getnext_flags))) { |
| 3260 | if (CHECK_NODE(iter, exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3261 | goto error; |
| 3262 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3263 | |
| 3264 | /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */ |
| 3265 | if (iter->nodetype == LYS_CHOICE) { |
| 3266 | iter2 = NULL; |
| 3267 | while ((iter2 = lys_getnext(iter2, iter, NULL, LYS_GETNEXT_NOSTATECHECK))) { |
| 3268 | if (CHECK_NODE(iter2, exclude, name)) { |
| 3269 | goto error; |
| 3270 | } |
| 3271 | } |
| 3272 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3273 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3274 | |
| 3275 | actions = parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3276 | LY_ARRAY_FOR(actions, u) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3277 | if (CHECK_NODE(&actions[u], exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3278 | goto error; |
| 3279 | } |
| 3280 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3281 | |
| 3282 | notifs = parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3283 | LY_ARRAY_FOR(notifs, u) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3284 | if (CHECK_NODE(¬ifs[u], exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3285 | goto error; |
| 3286 | } |
| 3287 | } |
| 3288 | return LY_SUCCESS; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3289 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3290 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3291 | 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] | 3292 | return LY_EEXIST; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3293 | |
| 3294 | #undef CHECK_NODE |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3295 | } |
| 3296 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3297 | 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] | 3298 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3299 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3300 | * @brief Compile parsed RPC/action schema node information. |
| 3301 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3302 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3303 | * @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] | 3304 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3305 | * @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). |
| 3306 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3307 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3308 | */ |
| 3309 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3310 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3311 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3312 | { |
| 3313 | LY_ERR ret = LY_SUCCESS; |
| 3314 | struct lysp_node *child_p; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3315 | LY_ARRAY_COUNT_TYPE u; |
| 3316 | uint32_t opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3317 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3318 | lysc_update_path(ctx, parent, action_p->name); |
| 3319 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3320 | /* member needed for uniqueness check lys_getnext() */ |
| 3321 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
| 3322 | action->module = ctx->mod; |
| 3323 | action->parent = parent; |
| 3324 | |
| 3325 | 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] | 3326 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3327 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3328 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3329 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3330 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3331 | return LY_EVALID; |
| 3332 | } |
| 3333 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3334 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3335 | action->sp = action_p; |
| 3336 | } |
| 3337 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3338 | |
| 3339 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3340 | * 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] | 3341 | 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] | 3342 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 3343 | DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup); |
| 3344 | DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup); |
| 3345 | DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3346 | 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] | 3347 | 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] | 3348 | |
| 3349 | /* input */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3350 | lysc_update_path(ctx, (struct lysc_node *)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3351 | 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] | 3352 | 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] | 3353 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3354 | LY_LIST_FOR(action_p->input.data, child_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3355 | 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] | 3356 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3357 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3358 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3359 | |
| 3360 | /* output */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3361 | lysc_update_path(ctx, (struct lysc_node *)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3362 | 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] | 3363 | 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] | 3364 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3365 | LY_LIST_FOR(action_p->output.data, child_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3366 | 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] | 3367 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3368 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3369 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3370 | |
| 3371 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3372 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3373 | ret = ly_set_add(&ctx->xpath, action, 0, NULL); |
| 3374 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3375 | } |
| 3376 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3377 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3378 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3379 | return ret; |
| 3380 | } |
| 3381 | |
| 3382 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3383 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3384 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3385 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3386 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3387 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3388 | * @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] | 3389 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3390 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3391 | */ |
| 3392 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3393 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3394 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3395 | { |
| 3396 | LY_ERR ret = LY_SUCCESS; |
| 3397 | struct lysp_node *child_p; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3398 | LY_ARRAY_COUNT_TYPE u; |
| 3399 | uint32_t opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3400 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3401 | lysc_update_path(ctx, parent, notif_p->name); |
| 3402 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3403 | /* member needed for uniqueness check lys_getnext() */ |
| 3404 | notif->nodetype = LYS_NOTIF; |
| 3405 | notif->module = ctx->mod; |
| 3406 | notif->parent = parent; |
| 3407 | |
| 3408 | 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] | 3409 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3410 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3411 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3412 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3413 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3414 | return LY_EVALID; |
| 3415 | } |
| 3416 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3417 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3418 | notif->sp = notif_p; |
| 3419 | } |
| 3420 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3421 | |
| 3422 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3423 | * 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] | 3424 | ret = lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0)); |
| 3425 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3426 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 3427 | DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup); |
| 3428 | DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup); |
| 3429 | DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3430 | 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] | 3431 | 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] | 3432 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3433 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3434 | ret = ly_set_add(&ctx->xpath, notif, 0, NULL); |
| 3435 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3436 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3437 | 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] | 3438 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3439 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3440 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3441 | ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status); |
| 3442 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3443 | } |
| 3444 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3445 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3446 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3447 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3448 | return ret; |
| 3449 | } |
| 3450 | |
| 3451 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3452 | * @brief Compile parsed container node information. |
| 3453 | * @param[in] ctx Compile context |
| 3454 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3455 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3456 | * is enriched with the container-specific information. |
| 3457 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3458 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3459 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3460 | 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] | 3461 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3462 | struct lysp_node_container *cont_p = (struct lysp_node_container *)node_p; |
| 3463 | struct lysc_node_container *cont = (struct lysc_node_container *)node; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3464 | struct lysp_node *child_p; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3465 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3466 | LY_ERR ret = LY_SUCCESS; |
| 3467 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3468 | if (cont_p->presence) { |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3469 | /* explicit presence */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3470 | cont->flags |= LYS_PRESENCE; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3471 | } else if (cont_p->musts) { |
| 3472 | /* container with a must condition */ |
Radek Krejci | 175f25b | 2020-08-13 12:02:36 +0200 | [diff] [blame] | 3473 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name); |
| 3474 | cont->flags |= LYS_PRESENCE; |
| 3475 | } else if (cont_p->when) { |
| 3476 | /* container with a when condition */ |
| 3477 | 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] | 3478 | cont->flags |= LYS_PRESENCE; |
| 3479 | } else if (cont_p->parent) { |
| 3480 | if (cont_p->parent->nodetype == LYS_CHOICE) { |
| 3481 | /* 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] | 3482 | 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] | 3483 | cont_p->name, cont_p->parent->name); |
| 3484 | cont->flags |= LYS_PRESENCE; |
| 3485 | } else if ((cont_p->parent->nodetype == LYS_CASE) |
| 3486 | && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) { |
| 3487 | /* 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] | 3488 | 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] | 3489 | cont_p->name, cont_p->parent->name); |
| 3490 | cont->flags |= LYS_PRESENCE; |
| 3491 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3492 | } |
| 3493 | |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3494 | /* more cases when the container has meaning but is kept NP for convenience: |
| 3495 | * - when condition |
| 3496 | * - direct child action/notification |
| 3497 | */ |
| 3498 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3499 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3500 | ret = lys_compile_node(ctx, child_p, node, 0); |
| 3501 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3502 | } |
| 3503 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3504 | 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] | 3505 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3506 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3507 | ret = ly_set_add(&ctx->xpath, cont, 0, NULL); |
| 3508 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3509 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3510 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3511 | 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] | 3512 | |
| 3513 | done: |
| 3514 | return ret; |
| 3515 | } |
| 3516 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3517 | /* |
| 3518 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3519 | * @param[in] ctx Compile context. |
| 3520 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3521 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3522 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3523 | * @return LY_ERR value. |
| 3524 | */ |
| 3525 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3526 | 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] | 3527 | struct lysc_node_leaf *leaf) |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3528 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3529 | const char *dflt; |
| 3530 | struct lys_module *dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3531 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3532 | 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] | 3533 | leaf->units ? NULL : &leaf->units, &dflt, &dflt_mod)); |
| 3534 | |
| 3535 | /* store default value, if any */ |
| 3536 | if (dflt && !(leaf->flags & LYS_SET_DFLT)) { |
| 3537 | 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] | 3538 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3539 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3540 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3541 | /* 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] | 3542 | LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3543 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3544 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3545 | LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) { |
| 3546 | 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] | 3547 | /* 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] | 3548 | LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3549 | } |
| 3550 | } |
| 3551 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3552 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3553 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3554 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3555 | return LY_EVALID; |
| 3556 | } |
| 3557 | } |
| 3558 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3559 | return LY_SUCCESS; |
| 3560 | } |
| 3561 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3562 | /** |
| 3563 | * @brief Compile parsed leaf node information. |
| 3564 | * @param[in] ctx Compile context |
| 3565 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3566 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3567 | * is enriched with the leaf-specific information. |
| 3568 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3569 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3570 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3571 | 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] | 3572 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3573 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)node_p; |
| 3574 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 3575 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3576 | LY_ERR ret = LY_SUCCESS; |
| 3577 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3578 | 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] | 3579 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3580 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3581 | ret = ly_set_add(&ctx->xpath, leaf, 0, NULL); |
| 3582 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3583 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3584 | if (leaf_p->units) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 3585 | 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] | 3586 | leaf->flags |= LYS_SET_UNITS; |
| 3587 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3588 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3589 | /* compile type */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3590 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
| 3591 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3592 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3593 | /* store/update default value */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3594 | if (leaf_p->dflt) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3595 | 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] | 3596 | leaf->flags |= LYS_SET_DFLT; |
| 3597 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3598 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3599 | done: |
| 3600 | return ret; |
| 3601 | } |
| 3602 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3603 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3604 | * @brief Compile parsed leaf-list node information. |
| 3605 | * @param[in] ctx Compile context |
| 3606 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3607 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3608 | * is enriched with the leaf-list-specific information. |
| 3609 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3610 | */ |
| 3611 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3612 | 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] | 3613 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3614 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)node_p; |
| 3615 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3616 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3617 | LY_ERR ret = LY_SUCCESS; |
| 3618 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3619 | 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] | 3620 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3621 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3622 | ret = ly_set_add(&ctx->xpath, llist, 0, NULL); |
| 3623 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3624 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3625 | if (llist_p->units) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 3626 | 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] | 3627 | llist->flags |= LYS_SET_UNITS; |
| 3628 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3629 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3630 | /* compile type */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3631 | ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf *)llist); |
| 3632 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 6a044b2 | 2020-01-15 12:25:39 +0100 | [diff] [blame] | 3633 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3634 | /* store/update default values */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3635 | if (llist_p->dflts) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3636 | 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] | 3637 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3638 | } |
| 3639 | |
| 3640 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3641 | if (llist->min) { |
| 3642 | llist->flags |= LYS_MAND_TRUE; |
| 3643 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3644 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3645 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3646 | done: |
| 3647 | return ret; |
| 3648 | } |
| 3649 | |
| 3650 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3651 | * @brief Compile information about list's uniques. |
| 3652 | * @param[in] ctx Compile context. |
| 3653 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3654 | * @param[in] uniques Sized array list of unique statements. |
| 3655 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3656 | * @return LY_ERR value. |
| 3657 | */ |
| 3658 | static LY_ERR |
| 3659 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3660 | { |
| 3661 | LY_ERR ret = LY_SUCCESS; |
| 3662 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3663 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3664 | const char *keystr, *delim; |
| 3665 | size_t len; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3666 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3667 | 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] | 3668 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3669 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3670 | for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3671 | config = -1; |
| 3672 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3673 | keystr = uniques[v]; |
| 3674 | while (keystr) { |
| 3675 | delim = strpbrk(keystr, " \t\n"); |
| 3676 | if (delim) { |
| 3677 | len = delim - keystr; |
| 3678 | while (isspace(*delim)) { |
| 3679 | ++delim; |
| 3680 | } |
| 3681 | } else { |
| 3682 | len = strlen(keystr); |
| 3683 | } |
| 3684 | |
| 3685 | /* unique node must be present */ |
| 3686 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3687 | ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, context_module, LYS_LEAF, 0, |
| 3688 | (const struct lysc_node **)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3689 | if (ret != LY_SUCCESS) { |
| 3690 | if (ret == LY_EDENIED) { |
| 3691 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3692 | "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] | 3693 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3694 | } |
| 3695 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3696 | } else if (flags) { |
| 3697 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3698 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3699 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3700 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3701 | } |
| 3702 | |
| 3703 | /* all referenced leafs must be of the same config type */ |
| 3704 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3705 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3706 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3707 | return LY_EVALID; |
| 3708 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3709 | config = 1; |
| 3710 | } else { /* LYS_CONFIG_R */ |
| 3711 | config = 0; |
| 3712 | } |
| 3713 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3714 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3715 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3716 | if (parent->nodetype == LYS_LIST) { |
| 3717 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3718 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3719 | return LY_EVALID; |
| 3720 | } |
| 3721 | } |
| 3722 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3723 | /* check status */ |
| 3724 | 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] | 3725 | (*key)->flags, (*key)->module, (*key)->name)); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3726 | |
| 3727 | /* mark leaf as unique */ |
| 3728 | (*key)->flags |= LYS_UNIQUE; |
| 3729 | |
| 3730 | /* next unique value in line */ |
| 3731 | keystr = delim; |
| 3732 | } |
| 3733 | /* next unique definition */ |
| 3734 | } |
| 3735 | |
| 3736 | return LY_SUCCESS; |
| 3737 | } |
| 3738 | |
| 3739 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3740 | * @brief Compile parsed list node information. |
| 3741 | * @param[in] ctx Compile context |
| 3742 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3743 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3744 | * is enriched with the list-specific information. |
| 3745 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3746 | */ |
| 3747 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3748 | 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] | 3749 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3750 | struct lysp_node_list *list_p = (struct lysp_node_list *)node_p; |
| 3751 | struct lysc_node_list *list = (struct lysc_node_list *)node; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3752 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3753 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3754 | size_t len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3755 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3756 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3757 | LY_ERR ret = LY_SUCCESS; |
| 3758 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3759 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3760 | if (list->min) { |
| 3761 | list->flags |= LYS_MAND_TRUE; |
| 3762 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3763 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3764 | |
| 3765 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3766 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3767 | } |
| 3768 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3769 | 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] | 3770 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3771 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3772 | LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL)); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3773 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3774 | |
| 3775 | /* keys */ |
| 3776 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3777 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3778 | return LY_EVALID; |
| 3779 | } |
| 3780 | |
| 3781 | /* find all the keys (must be direct children) */ |
| 3782 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3783 | if (!keystr) { |
| 3784 | /* keyless list */ |
| 3785 | list->flags |= LYS_KEYLESS; |
| 3786 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3787 | while (keystr) { |
| 3788 | delim = strpbrk(keystr, " \t\n"); |
| 3789 | if (delim) { |
| 3790 | len = delim - keystr; |
| 3791 | while (isspace(*delim)) { |
| 3792 | ++delim; |
| 3793 | } |
| 3794 | } else { |
| 3795 | len = strlen(keystr); |
| 3796 | } |
| 3797 | |
| 3798 | /* key node must be present */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3799 | 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] | 3800 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3801 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3802 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3803 | return LY_EVALID; |
| 3804 | } |
| 3805 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3806 | if (key->flags & LYS_KEY) { |
| 3807 | /* the node was already marked as a key */ |
| 3808 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3809 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3810 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3811 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3812 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3813 | lysc_update_path(ctx, (struct lysc_node *)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3814 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3815 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3816 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3817 | return LY_EVALID; |
| 3818 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3819 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3820 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3821 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3822 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3823 | "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] | 3824 | return LY_EVALID; |
| 3825 | } |
| 3826 | } else { |
| 3827 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3828 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3829 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3830 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3831 | return LY_EVALID; |
| 3832 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3833 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3834 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3835 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3836 | return LY_EVALID; |
| 3837 | } |
| 3838 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3839 | |
| 3840 | /* check status */ |
| 3841 | 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] | 3842 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3843 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3844 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3845 | if (key->dflt) { |
| 3846 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3847 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3848 | free(key->dflt); |
| 3849 | key->dflt = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3850 | } |
| 3851 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3852 | key->flags |= LYS_KEY; |
| 3853 | |
| 3854 | /* move it to the correct position */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3855 | 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] | 3856 | /* fix links in closest previous siblings of the key */ |
| 3857 | if (key->next) { |
| 3858 | key->next->prev = key->prev; |
| 3859 | } else { |
| 3860 | /* last child */ |
| 3861 | list->child->prev = key->prev; |
| 3862 | } |
| 3863 | if (key->prev->next) { |
| 3864 | key->prev->next = key->next; |
| 3865 | } |
| 3866 | /* fix links in the key */ |
| 3867 | if (prev_key) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3868 | key->prev = (struct lysc_node *)prev_key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3869 | key->next = prev_key->next; |
| 3870 | } else { |
| 3871 | key->prev = list->child->prev; |
| 3872 | key->next = list->child; |
| 3873 | } |
| 3874 | /* fix links in closes future siblings of the key */ |
| 3875 | if (prev_key) { |
| 3876 | if (prev_key->next) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3877 | prev_key->next->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3878 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3879 | list->child->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3880 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3881 | prev_key->next = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3882 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3883 | list->child->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3884 | } |
| 3885 | /* fix links in parent */ |
| 3886 | if (!key->prev->next) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3887 | list->child = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3888 | } |
| 3889 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3890 | |
| 3891 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3892 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3893 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3894 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3895 | } |
| 3896 | |
| 3897 | /* uniques */ |
| 3898 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3899 | 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] | 3900 | } |
| 3901 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3902 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3903 | 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] | 3904 | |
| 3905 | done: |
| 3906 | return ret; |
| 3907 | } |
| 3908 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3909 | /** |
| 3910 | * @brief Do some checks and set the default choice's case. |
| 3911 | * |
| 3912 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3913 | * |
| 3914 | * @param[in] ctx Compile context. |
| 3915 | * @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, |
| 3916 | * not the reference to the imported module. |
| 3917 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3918 | * @return LY_ERR value. |
| 3919 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3920 | static LY_ERR |
| 3921 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3922 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3923 | struct lysc_node *iter, *node = (struct lysc_node *)ch; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3924 | const char *prefix = NULL, *name; |
| 3925 | size_t prefix_len = 0; |
| 3926 | |
| 3927 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3928 | name = strchr(dflt, ':'); |
| 3929 | if (name) { |
| 3930 | prefix = dflt; |
| 3931 | prefix_len = name - prefix; |
| 3932 | ++name; |
| 3933 | } else { |
| 3934 | name = dflt; |
| 3935 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 3936 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3937 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3938 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3939 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3940 | prefix_len, prefix); |
| 3941 | return LY_EVALID; |
| 3942 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3943 | 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] | 3944 | if (!ch->dflt) { |
| 3945 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3946 | "Default case \"%s\" not found.", dflt); |
| 3947 | return LY_EVALID; |
| 3948 | } |
| 3949 | /* no mandatory nodes directly under the default case */ |
| 3950 | LY_LIST_FOR(ch->dflt->child, iter) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3951 | if (iter->parent != (struct lysc_node *)ch->dflt) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3952 | break; |
| 3953 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3954 | if (iter->flags & LYS_MAND_TRUE) { |
| 3955 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3956 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3957 | return LY_EVALID; |
| 3958 | } |
| 3959 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3960 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3961 | return LY_SUCCESS; |
| 3962 | } |
| 3963 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3964 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3965 | 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] | 3966 | { |
| 3967 | struct lys_module *mod; |
| 3968 | const char *prefix = NULL, *name; |
| 3969 | size_t prefix_len = 0; |
| 3970 | struct lysc_node_case *cs; |
| 3971 | struct lysc_node *node; |
| 3972 | |
| 3973 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3974 | name = strchr(dflt, ':'); |
| 3975 | if (name) { |
| 3976 | prefix = dflt; |
| 3977 | prefix_len = name - prefix; |
| 3978 | ++name; |
| 3979 | } else { |
| 3980 | name = dflt; |
| 3981 | } |
| 3982 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3983 | if (prefix) { |
| 3984 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3985 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3986 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3987 | "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] | 3988 | return LY_EVALID; |
| 3989 | } |
| 3990 | } else { |
| 3991 | mod = ctx->mod; |
| 3992 | } |
| 3993 | /* get the default case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3994 | 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] | 3995 | if (!cs) { |
| 3996 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3997 | "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] | 3998 | return LY_EVALID; |
| 3999 | } |
| 4000 | |
| 4001 | /* check that there is no mandatory node */ |
| 4002 | LY_LIST_FOR(cs->child, node) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4003 | if (node->parent != (struct lysc_node *)cs) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4004 | break; |
| 4005 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4006 | if (node->flags & LYS_MAND_TRUE) { |
| 4007 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4008 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4009 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4010 | return LY_EVALID; |
| 4011 | } |
| 4012 | } |
| 4013 | |
| 4014 | /* set the default case in choice */ |
| 4015 | ch->dflt = cs; |
| 4016 | cs->flags |= LYS_SET_DFLT; |
| 4017 | |
| 4018 | return LY_SUCCESS; |
| 4019 | } |
| 4020 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4021 | /** |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4022 | * @brief Compile choice children. |
| 4023 | * |
| 4024 | * @param[in] ctx Compile context |
| 4025 | * @param[in] child_p Parsed choice children nodes. |
| 4026 | * @param[in] node Compiled choice node to compile and add children to. |
| 4027 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4028 | */ |
| 4029 | static LY_ERR |
| 4030 | lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node) |
| 4031 | { |
| 4032 | LY_ERR ret = LY_SUCCESS; |
| 4033 | struct lysp_node *child_next_p; |
| 4034 | struct lysp_node_case *cs_p; |
| 4035 | |
| 4036 | if (child_p->nodetype == LYS_CASE) { |
| 4037 | /* standard case under choice */ |
| 4038 | ret = lys_compile_node(ctx, child_p, node, 0); |
| 4039 | } else { |
| 4040 | /* we need the implicit case first, so create a fake parsed case */ |
| 4041 | cs_p = calloc(1, sizeof *cs_p); |
| 4042 | cs_p->nodetype = LYS_CASE; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 4043 | DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, done); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4044 | cs_p->child = child_p; |
| 4045 | |
| 4046 | /* make the child the only case child */ |
| 4047 | child_next_p = child_p->next; |
| 4048 | child_p->next = NULL; |
| 4049 | |
| 4050 | /* compile it normally */ |
| 4051 | ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0); |
| 4052 | |
| 4053 | /* free the fake parsed node and correct pointers back */ |
| 4054 | cs_p->child = NULL; |
| 4055 | lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p); |
| 4056 | child_p->next = child_next_p; |
| 4057 | } |
| 4058 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 4059 | done: |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4060 | return ret; |
| 4061 | } |
| 4062 | |
| 4063 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4064 | * @brief Compile parsed choice node information. |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4065 | * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4066 | * @param[in] ctx Compile context |
| 4067 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4068 | * @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] | 4069 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4070 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4071 | */ |
| 4072 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4073 | 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] | 4074 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4075 | struct lysp_node_choice *ch_p = (struct lysp_node_choice *)node_p; |
| 4076 | struct lysc_node_choice *ch = (struct lysc_node_choice *)node; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4077 | struct lysp_node *child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4078 | LY_ERR ret = LY_SUCCESS; |
| 4079 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4080 | assert(node->nodetype == LYS_CHOICE); |
| 4081 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4082 | LY_LIST_FOR(ch_p->child, child_p) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4083 | LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4087 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4088 | 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] | 4089 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4090 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4091 | return ret; |
| 4092 | } |
| 4093 | |
| 4094 | /** |
| 4095 | * @brief Compile parsed anydata or anyxml node information. |
| 4096 | * @param[in] ctx Compile context |
| 4097 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4098 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4099 | * is enriched with the any-specific information. |
| 4100 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4101 | */ |
| 4102 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4103 | 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] | 4104 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4105 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)node_p; |
| 4106 | struct lysc_node_anydata *any = (struct lysc_node_anydata *)node; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4107 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4108 | LY_ERR ret = LY_SUCCESS; |
| 4109 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4110 | 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] | 4111 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4112 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4113 | ret = ly_set_add(&ctx->xpath, any, 0, NULL); |
| 4114 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4115 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4116 | |
| 4117 | if (any->flags & LYS_CONFIG_W) { |
Radek Krejci | 5c4ed7b | 2020-08-12 11:29:44 +0200 | [diff] [blame] | 4118 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s", |
| 4119 | 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] | 4120 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4121 | done: |
| 4122 | return ret; |
| 4123 | } |
| 4124 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4125 | /** |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4126 | * @brief Connect the node into the siblings list and check its name uniqueness. Also, |
| 4127 | * keep specific order of augments targetting the same node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4128 | * |
| 4129 | * @param[in] ctx Compile context |
| 4130 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4131 | * the choice itself is expected instead of a specific case node. |
| 4132 | * @param[in] node Schema node to connect into the list. |
| 4133 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4134 | * 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] | 4135 | */ |
| 4136 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4137 | 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] | 4138 | { |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4139 | struct lysc_node **children, *start, *end; |
| 4140 | const struct lys_module *mod; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4141 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4142 | node->parent = parent; |
| 4143 | |
| 4144 | if (parent) { |
| 4145 | if (parent->nodetype == LYS_CHOICE) { |
| 4146 | assert(node->nodetype == LYS_CASE); |
| 4147 | children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases; |
| 4148 | } else { |
| 4149 | children = lysc_node_children_p(parent, ctx->options); |
| 4150 | } |
| 4151 | assert(children); |
| 4152 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4153 | if (!(*children)) { |
| 4154 | /* first child */ |
| 4155 | *children = node; |
| 4156 | } else if (*children != node) { |
| 4157 | /* by the condition in previous branch we cover the choice/case children |
| 4158 | * - the children list is shared by the choice and the the first case, in addition |
| 4159 | * the first child of each case must be referenced from the case node. So the node is |
| 4160 | * actually always already inserted in case it is the first children - so here such |
| 4161 | * a situation actually corresponds to the first branch */ |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4162 | if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module) |
| 4163 | && (strcmp((*children)->prev->module->name, node->module->name) > 0)) { |
| 4164 | /* some augments are already connected and we are connecting new ones, |
| 4165 | * keep module name order and insert the node into the children list */ |
| 4166 | end = (*children); |
| 4167 | do { |
| 4168 | end = end->prev; |
| 4169 | mod = end->module; |
| 4170 | while (end->prev->module == mod) { |
| 4171 | end = end->prev; |
| 4172 | } |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 4173 | } 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] | 4174 | |
| 4175 | /* we have the last existing node after our node, easily get the first before and connect it */ |
| 4176 | start = end->prev; |
| 4177 | start->next = node; |
| 4178 | node->next = end; |
| 4179 | end->prev = node; |
| 4180 | node->prev = start; |
| 4181 | } else { |
| 4182 | /* insert at the end of the parent's children list */ |
| 4183 | (*children)->prev->next = node; |
| 4184 | node->prev = (*children)->prev; |
| 4185 | (*children)->prev = node; |
| 4186 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4187 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4188 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4189 | /* check the name uniqueness (even for an only child, it may be in case) */ |
| 4190 | if (lys_compile_node_uniqness(ctx, parent, node->name, node)) { |
| 4191 | return LY_EEXIST; |
| 4192 | } |
| 4193 | } else { |
| 4194 | /* top-level element */ |
| 4195 | if (!ctx->mod->compiled->data) { |
| 4196 | ctx->mod->compiled->data = node; |
| 4197 | } else { |
| 4198 | /* insert at the end of the module's top-level nodes list */ |
| 4199 | ctx->mod->compiled->data->prev->next = node; |
| 4200 | node->prev = ctx->mod->compiled->data->prev; |
| 4201 | ctx->mod->compiled->data->prev = node; |
| 4202 | } |
| 4203 | |
| 4204 | /* check the name uniqueness on top-level */ |
| 4205 | if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) { |
| 4206 | return LY_EEXIST; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4207 | } |
| 4208 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4209 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4210 | return LY_SUCCESS; |
| 4211 | } |
| 4212 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4213 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4214 | * @brief Prepare the case structure in choice node for the new data node. |
| 4215 | * |
| 4216 | * 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 |
| 4217 | * created in the choice when the first child was processed. |
| 4218 | * |
| 4219 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4220 | * @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] | 4221 | * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4222 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4223 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4224 | * @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, |
| 4225 | * 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] | 4226 | */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4227 | static LY_ERR |
| 4228 | 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] | 4229 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4230 | struct lysp_node *child_p; |
| 4231 | struct lysp_node_case *cs_p = (struct lysp_node_case *)node_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4232 | |
Radek Krejci | 3942480 | 2020-08-12 09:31:00 +0200 | [diff] [blame] | 4233 | if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4234 | /* we have to add an implicit case node into the parent choice */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4235 | } else if (node_p->nodetype == LYS_CASE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4236 | /* explicit parent case */ |
| 4237 | LY_LIST_FOR(cs_p->child, child_p) { |
| 4238 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4239 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4240 | } else { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4241 | LOGINT_RET(ctx->ctx); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4242 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4243 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4244 | return LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4245 | } |
| 4246 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4247 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4248 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4249 | * |
| 4250 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4251 | * @param[in] node Target node where the config is supposed to be changed. |
| 4252 | * @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] | 4253 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4254 | * 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] | 4255 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4256 | * @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] | 4257 | * @return LY_ERR value. |
| 4258 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4259 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4260 | 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] | 4261 | ly_bool inheriting, ly_bool refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4262 | { |
| 4263 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4264 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4265 | |
| 4266 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4267 | /* nothing to do */ |
| 4268 | return LY_SUCCESS; |
| 4269 | } |
| 4270 | |
| 4271 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4272 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4273 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4274 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4275 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4276 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4277 | return LY_EVALID; |
| 4278 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4279 | node->flags |= LYS_SET_CONFIG; |
| 4280 | } else { |
| 4281 | if (node->flags & LYS_SET_CONFIG) { |
| 4282 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4283 | /* setting config flags, but have node with explicit config true */ |
| 4284 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4285 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4286 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4287 | return LY_EVALID; |
| 4288 | } |
| 4289 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4290 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4291 | return LY_SUCCESS; |
| 4292 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4293 | } |
| 4294 | node->flags &= ~LYS_CONFIG_MASK; |
| 4295 | node->flags |= config; |
| 4296 | |
| 4297 | /* inherit the change into the children */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4298 | LY_LIST_FOR((struct lysc_node *)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4299 | 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] | 4300 | } |
| 4301 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4302 | return LY_SUCCESS; |
| 4303 | } |
| 4304 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4305 | /** |
| 4306 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4307 | * |
| 4308 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4309 | * the flag to such parents from a mandatory children. |
| 4310 | * |
| 4311 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4312 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4313 | * (mandatory children was removed). |
| 4314 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4315 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4316 | lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add) |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4317 | { |
| 4318 | struct lysc_node *iter; |
| 4319 | |
| 4320 | if (add) { /* set flag */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4321 | 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] | 4322 | parent = parent->parent) { |
| 4323 | parent->flags |= LYS_MAND_TRUE; |
| 4324 | } |
| 4325 | } else { /* unset flag */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4326 | 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] | 4327 | 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] | 4328 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4329 | /* there is another mandatory node */ |
| 4330 | return; |
| 4331 | } |
| 4332 | } |
| 4333 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4334 | parent->flags &= ~LYS_MAND_TRUE; |
| 4335 | } |
| 4336 | } |
| 4337 | } |
| 4338 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4339 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4340 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4341 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4342 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4343 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4344 | */ |
| 4345 | static void |
| 4346 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4347 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4348 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4349 | size_t len; |
| 4350 | |
| 4351 | len = strlen(aug_p->nodeid); |
| 4352 | LY_ARRAY_FOR(result, v) { |
| 4353 | if (strlen(result[v]->nodeid) <= len) { |
| 4354 | continue; |
| 4355 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4356 | if (v < LY_ARRAY_COUNT(result)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4357 | /* move the rest of array */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4358 | 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] | 4359 | break; |
| 4360 | } |
| 4361 | } |
| 4362 | result[v] = aug_p; |
| 4363 | LY_ARRAY_INCREMENT(result); |
| 4364 | } |
| 4365 | |
| 4366 | /** |
| 4367 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4368 | * |
| 4369 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4370 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4371 | * |
| 4372 | * @param[in] ctx Compile context. |
| 4373 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4374 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4375 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4376 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4377 | * @return LY_ERR value. |
| 4378 | */ |
| 4379 | LY_ERR |
| 4380 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4381 | { |
| 4382 | struct lysp_augment **result = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4383 | LY_ARRAY_COUNT_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4384 | |
| 4385 | assert(augments); |
| 4386 | |
| 4387 | /* get count of the augments in module and all its submodules */ |
| 4388 | if (aug_p) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4389 | count += LY_ARRAY_COUNT(aug_p); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4390 | } |
| 4391 | LY_ARRAY_FOR(inc_p, u) { |
| 4392 | if (inc_p[u].submodule->augments) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4393 | count += LY_ARRAY_COUNT(inc_p[u].submodule->augments); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4394 | } |
| 4395 | } |
| 4396 | |
| 4397 | if (!count) { |
| 4398 | *augments = NULL; |
| 4399 | return LY_SUCCESS; |
| 4400 | } |
| 4401 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4402 | |
| 4403 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4404 | * together, so there can be even /z/y betwwen them. */ |
| 4405 | LY_ARRAY_FOR(aug_p, u) { |
| 4406 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4407 | } |
| 4408 | LY_ARRAY_FOR(inc_p, u) { |
| 4409 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4410 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4411 | } |
| 4412 | } |
| 4413 | |
| 4414 | *augments = result; |
| 4415 | return LY_SUCCESS; |
| 4416 | } |
| 4417 | |
| 4418 | /** |
| 4419 | * @brief Compile the parsed augment connecting it into its target. |
| 4420 | * |
| 4421 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4422 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4423 | * are already implemented and compiled. |
| 4424 | * |
| 4425 | * @param[in] ctx Compile context. |
| 4426 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4427 | * @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 |
| 4428 | * children in case of the augmenting uses data. |
| 4429 | * @return LY_SUCCESS on success. |
| 4430 | * @return LY_EVALID on failure. |
| 4431 | */ |
| 4432 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4433 | 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] | 4434 | { |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4435 | LY_ERR ret = LY_SUCCESS, rc; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4436 | struct lysp_node *node_p; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4437 | struct lysc_node *target; /* target target of the augment */ |
| 4438 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4439 | struct lysc_when **when, *when_shared; |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4440 | struct lys_module **aug_mod; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4441 | ly_bool allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4442 | uint16_t flags = 0; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4443 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4444 | uint32_t opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4445 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4446 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4447 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4448 | |
Michal Vasko | cb18c7f | 2020-08-24 09:22:28 +0200 | [diff] [blame] | 4449 | 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] | 4450 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4451 | 1, (const struct lysc_node **)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4452 | if (ret != LY_SUCCESS) { |
| 4453 | if (ret == LY_EDENIED) { |
| 4454 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4455 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4456 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4457 | } |
| 4458 | return LY_EVALID; |
| 4459 | } |
| 4460 | |
| 4461 | /* check for mandatory nodes |
| 4462 | * - new cases augmenting some choice can have mandatory nodes |
| 4463 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4464 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4465 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4466 | allow_mandatory = 1; |
| 4467 | } |
| 4468 | |
| 4469 | when_shared = NULL; |
| 4470 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4471 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4472 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4473 | && !((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] | 4474 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4475 | && !(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] | 4476 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4477 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4478 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4479 | return LY_EVALID; |
| 4480 | } |
| 4481 | |
| 4482 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4483 | ctx->options |= flags; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4484 | if (target->nodetype == LYS_CHOICE) { |
| 4485 | LY_CHECK_RET(lys_compile_node_choice_child(ctx, node_p, target)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4486 | } else { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4487 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4488 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4489 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4490 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4491 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4492 | * 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] | 4493 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4494 | /* 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] | 4495 | 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] | 4496 | } else if (target->nodetype == LYS_CHOICE) { |
| 4497 | /* 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] | 4498 | node = ((struct lysc_node_choice *)target)->cases->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4499 | } else { |
| 4500 | /* the compiled node is the last child of the target */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4501 | node = (struct lysc_node *)lysc_node_children(target, flags); |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4502 | if (!node) { |
| 4503 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4504 | break; |
| 4505 | } |
| 4506 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4507 | } |
| 4508 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4509 | 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] | 4510 | node->flags &= ~LYS_MAND_TRUE; |
| 4511 | lys_compile_mandatory_parents(target, 0); |
| 4512 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4513 | "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] | 4514 | return LY_EVALID; |
| 4515 | } |
| 4516 | |
| 4517 | /* pass augment's when to all the children */ |
| 4518 | if (aug_p->when) { |
| 4519 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4520 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4521 | 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] | 4522 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4523 | |
| 4524 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4525 | /* do not check "when" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4526 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 4527 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4528 | } |
| 4529 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4530 | when_shared = *when; |
| 4531 | } else { |
| 4532 | ++when_shared->refcount; |
| 4533 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4534 | |
| 4535 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4536 | /* 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] | 4537 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 4538 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4539 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4540 | } |
| 4541 | } |
| 4542 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4543 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4544 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4545 | switch (target->nodetype) { |
| 4546 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4547 | 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] | 4548 | u, lys_compile_action, 0, ret, error); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4549 | 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] | 4550 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4551 | break; |
| 4552 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4553 | 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] | 4554 | u, lys_compile_action, 0, ret, error); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4555 | 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] | 4556 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4557 | break; |
| 4558 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4559 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4560 | if (aug_p->actions) { |
| 4561 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4562 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4563 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4564 | return LY_EVALID; |
| 4565 | } |
| 4566 | if (aug_p->notifs) { |
| 4567 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4568 | "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] | 4569 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4570 | return LY_EVALID; |
| 4571 | } |
| 4572 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4573 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4574 | /* add this module into the target module augmented_by, if not there already */ |
| 4575 | rc = LY_SUCCESS; |
| 4576 | LY_ARRAY_FOR(target->module->compiled->augmented_by, v) { |
| 4577 | if (target->module->compiled->augmented_by[v] == ctx->mod) { |
| 4578 | rc = LY_EEXIST; |
| 4579 | break; |
| 4580 | } |
| 4581 | } |
| 4582 | if (!rc) { |
| 4583 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error); |
| 4584 | *aug_mod = ctx->mod; |
| 4585 | } |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4586 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4587 | lysc_update_path(ctx, NULL, NULL); |
| 4588 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4589 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4590 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4591 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4592 | return ret; |
| 4593 | } |
| 4594 | |
| 4595 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4596 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4597 | * |
| 4598 | * @param[in] ctx Compile context. |
| 4599 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4600 | * @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] | 4601 | * @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] | 4602 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4603 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4604 | * 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] | 4605 | * @return LY_ERR value. |
| 4606 | */ |
| 4607 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4608 | 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] | 4609 | { |
| 4610 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 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 %s of mandatory - %s cannot hold mandatory statement.", |
| 4613 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4614 | return LY_EVALID; |
| 4615 | } |
| 4616 | |
| 4617 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4618 | /* check if node has default value */ |
| 4619 | if (node->nodetype & LYS_LEAF) { |
| 4620 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4621 | if (refine_flag) { |
| 4622 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4623 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4624 | return LY_EVALID; |
| 4625 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4626 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4627 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4628 | if (refine_flag) { |
| 4629 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4630 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4631 | return LY_EVALID; |
| 4632 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4633 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4634 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4635 | 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] | 4636 | return LY_EVALID; |
| 4637 | } |
| 4638 | |
| 4639 | node->flags &= ~LYS_MAND_FALSE; |
| 4640 | node->flags |= LYS_MAND_TRUE; |
| 4641 | lys_compile_mandatory_parents(node->parent, 1); |
| 4642 | } else { |
| 4643 | /* make mandatory false */ |
| 4644 | node->flags &= ~LYS_MAND_TRUE; |
| 4645 | node->flags |= LYS_MAND_FALSE; |
| 4646 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4647 | } |
| 4648 | return LY_SUCCESS; |
| 4649 | } |
| 4650 | |
| 4651 | /** |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4652 | * @brief Find grouping for a uses. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4653 | * |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4654 | * @param[in] ctx Compile context. |
| 4655 | * @param[in] uses_p Parsed uses node. |
| 4656 | * @param[out] gpr_p Found grouping on success. |
| 4657 | * @param[out] grp_mod Module of @p grp_p on success. |
| 4658 | * @return LY_ERR value. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4659 | */ |
| 4660 | static LY_ERR |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4661 | lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p, |
| 4662 | struct lys_module **grp_mod) |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4663 | { |
| 4664 | struct lysp_node *node_p; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4665 | struct lysp_grp *grp; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4666 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4667 | ly_bool found = 0; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4668 | const char *id, *name, *prefix; |
| 4669 | size_t prefix_len, name_len; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4670 | struct lys_module *mod; |
| 4671 | |
| 4672 | *grp_p = NULL; |
| 4673 | *grp_mod = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4674 | |
| 4675 | /* search for the grouping definition */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4676 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4677 | 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] | 4678 | if (prefix) { |
| 4679 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4680 | if (!mod) { |
| 4681 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4682 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4683 | return LY_EVALID; |
| 4684 | } |
| 4685 | } else { |
| 4686 | mod = ctx->mod_def; |
| 4687 | } |
| 4688 | if (mod == ctx->mod_def) { |
| 4689 | 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] | 4690 | grp = (struct lysp_grp *)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4691 | LY_ARRAY_FOR(grp, u) { |
| 4692 | if (!strcmp(grp[u].name, name)) { |
| 4693 | grp = &grp[u]; |
| 4694 | found = 1; |
| 4695 | break; |
| 4696 | } |
| 4697 | } |
| 4698 | } |
| 4699 | } |
| 4700 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4701 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4702 | grp = mod->parsed->groupings; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4703 | LY_ARRAY_FOR(grp, u) { |
| 4704 | if (!strcmp(grp[u].name, name)) { |
| 4705 | grp = &grp[u]; |
| 4706 | found = 1; |
| 4707 | break; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4708 | } |
| 4709 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4710 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4711 | /* ... and all the submodules */ |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4712 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4713 | grp = mod->parsed->includes[u].submodule->groupings; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4714 | LY_ARRAY_FOR(grp, v) { |
| 4715 | if (!strcmp(grp[v].name, name)) { |
| 4716 | grp = &grp[v]; |
| 4717 | found = 1; |
| 4718 | break; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4719 | } |
| 4720 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4721 | if (found) { |
| 4722 | break; |
| 4723 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4724 | } |
| 4725 | } |
| 4726 | } |
| 4727 | if (!found) { |
| 4728 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4729 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4730 | return LY_EVALID; |
| 4731 | } |
| 4732 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4733 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4734 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4735 | grp->flags |= LYS_USED_GRP; |
| 4736 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4737 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4738 | *grp_p = grp; |
| 4739 | *grp_mod = mod; |
| 4740 | return LY_SUCCESS; |
| 4741 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4742 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4743 | static LY_ERR |
| 4744 | lys_compile_refines(struct lysc_ctx *ctx, struct lysp_refine *refines, const struct lysc_node *context_node) |
| 4745 | { |
| 4746 | struct lysc_node *node; |
| 4747 | LY_ARRAY_COUNT_TYPE u; |
| 4748 | struct lysp_refine *rfn; |
| 4749 | LY_ERR ret = LY_SUCCESS; |
| 4750 | uint32_t min, max; |
| 4751 | uint16_t flags; |
| 4752 | struct ly_set refined = {0}; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4753 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4754 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4755 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4756 | /* apply refine */ |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4757 | LY_ARRAY_FOR(refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4758 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4759 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4760 | 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] | 4761 | 0, 0, (const struct lysc_node **)&node, &flags); |
| 4762 | LY_CHECK_GOTO(ret, cleanup); |
| 4763 | ret = ly_set_add(&refined, node, LY_SET_OPT_USEASLIST, NULL); |
| 4764 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4765 | |
| 4766 | /* default value */ |
| 4767 | if (rfn->dflts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4768 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4769 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4770 | "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] | 4771 | lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4772 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4773 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4774 | } |
| 4775 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4776 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4777 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4778 | lys_nodetype2str(node->nodetype)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4779 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4780 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4781 | } |
| 4782 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4783 | /* postpone default compilation when the tree is complete */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4784 | ret = lysc_incomplete_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0], ctx->mod_def); |
| 4785 | LY_CHECK_GOTO(ret, cleanup); |
| 4786 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4787 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4788 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4789 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4790 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4791 | "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] | 4792 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4793 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4794 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4795 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4796 | /* postpone default compilation when the tree is complete */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4797 | ret = lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts, ctx->mod_def); |
| 4798 | LY_CHECK_GOTO(ret, cleanup); |
| 4799 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4800 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4801 | } else if (node->nodetype == LYS_CHOICE) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4802 | if (((struct lysc_node_choice *)node)->dflt) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4803 | /* unset LYS_SET_DFLT from the current default case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4804 | ((struct lysc_node_choice *)node)->dflt->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4805 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4806 | ret = lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice *)node); |
| 4807 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4808 | } |
| 4809 | } |
| 4810 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4811 | /* description */ |
| 4812 | if (rfn->dsc) { |
| 4813 | FREE_STRING(ctx->ctx, node->dsc); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 4814 | 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] | 4815 | } |
| 4816 | |
| 4817 | /* reference */ |
| 4818 | if (rfn->ref) { |
| 4819 | FREE_STRING(ctx->ctx, node->ref); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 4820 | 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] | 4821 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4822 | |
| 4823 | /* config */ |
| 4824 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4825 | if (!flags) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4826 | ret = lys_compile_change_config(ctx, node, rfn->flags, 0, 1); |
| 4827 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4828 | } else { |
| 4829 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4830 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4831 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4832 | } |
| 4833 | |
| 4834 | /* mandatory */ |
| 4835 | if (rfn->flags & LYS_MAND_MASK) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4836 | ret = lys_compile_change_mandatory(ctx, node, rfn->flags, 1); |
| 4837 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4838 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4839 | |
| 4840 | /* presence */ |
| 4841 | if (rfn->presence) { |
| 4842 | if (node->nodetype != LYS_CONTAINER) { |
| 4843 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4844 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4845 | lys_nodetype2str(node->nodetype)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4846 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4847 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4848 | } |
| 4849 | node->flags |= LYS_PRESENCE; |
| 4850 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4851 | |
| 4852 | /* must */ |
| 4853 | if (rfn->musts) { |
| 4854 | switch (node->nodetype) { |
| 4855 | case LYS_LEAF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4856 | 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] | 4857 | break; |
| 4858 | case LYS_LEAFLIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4859 | 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] | 4860 | break; |
| 4861 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4862 | 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] | 4863 | break; |
| 4864 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4865 | 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] | 4866 | break; |
| 4867 | case LYS_ANYXML: |
| 4868 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4869 | 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] | 4870 | break; |
| 4871 | default: |
| 4872 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4873 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 4874 | lys_nodetype2str(node->nodetype)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4875 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4876 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4877 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4878 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 4879 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4880 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4881 | |
| 4882 | /* min/max-elements */ |
| 4883 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4884 | switch (node->nodetype) { |
| 4885 | case LYS_LEAFLIST: |
| 4886 | if (rfn->flags & LYS_SET_MAX) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4887 | ((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] | 4888 | } |
| 4889 | if (rfn->flags & LYS_SET_MIN) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4890 | ((struct lysc_node_leaflist *)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4891 | if (rfn->min) { |
| 4892 | node->flags |= LYS_MAND_TRUE; |
| 4893 | lys_compile_mandatory_parents(node->parent, 1); |
| 4894 | } else { |
| 4895 | node->flags &= ~LYS_MAND_TRUE; |
| 4896 | lys_compile_mandatory_parents(node->parent, 0); |
| 4897 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4898 | } |
| 4899 | break; |
| 4900 | case LYS_LIST: |
| 4901 | if (rfn->flags & LYS_SET_MAX) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4902 | ((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] | 4903 | } |
| 4904 | if (rfn->flags & LYS_SET_MIN) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4905 | ((struct lysc_node_list *)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4906 | if (rfn->min) { |
| 4907 | node->flags |= LYS_MAND_TRUE; |
| 4908 | lys_compile_mandatory_parents(node->parent, 1); |
| 4909 | } else { |
| 4910 | node->flags &= ~LYS_MAND_TRUE; |
| 4911 | lys_compile_mandatory_parents(node->parent, 0); |
| 4912 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4913 | } |
| 4914 | break; |
| 4915 | default: |
| 4916 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4917 | "Invalid refine of %s statement - %s cannot hold this statement.", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4918 | (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] | 4919 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4920 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4921 | } |
| 4922 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4923 | |
| 4924 | /* if-feature */ |
| 4925 | if (rfn->iffeatures) { |
| 4926 | /* 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] | 4927 | 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] | 4928 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4929 | |
| 4930 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4931 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4932 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4933 | /* 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] | 4934 | for (uint32_t i = 0; i < refined.count; ++i) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4935 | node = (struct lysc_node *)refined.objs[i]; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4936 | rfn = &refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4937 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4938 | |
| 4939 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 4940 | if ((node->flags & LYS_MAND_TRUE) && |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4941 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) || |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4942 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 4943 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4944 | "Invalid refine of default - the node is mandatory."); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4945 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4946 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4947 | } |
| 4948 | |
| 4949 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4950 | if (node->nodetype == LYS_LIST) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4951 | min = ((struct lysc_node_list *)node)->min; |
| 4952 | max = ((struct lysc_node_list *)node)->max; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4953 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4954 | min = ((struct lysc_node_leaflist *)node)->min; |
| 4955 | max = ((struct lysc_node_leaflist *)node)->max; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4956 | } |
| 4957 | if (min > max) { |
| 4958 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4959 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4960 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4961 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4962 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4963 | } |
| 4964 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4965 | |
| 4966 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4967 | } |
| 4968 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4969 | cleanup: |
| 4970 | ly_set_erase(&refined, NULL); |
| 4971 | lysc_update_path(ctx, NULL, NULL); |
| 4972 | return ret; |
| 4973 | } |
| 4974 | |
| 4975 | /** |
| 4976 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4977 | * If present, also apply uses's modificators. |
| 4978 | * |
| 4979 | * @param[in] ctx Compile context |
| 4980 | * @param[in] uses_p Parsed uses schema node. |
| 4981 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4982 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4983 | * the compile context. |
| 4984 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4985 | */ |
| 4986 | static LY_ERR |
| 4987 | lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p) |
| 4988 | { |
| 4989 | struct lysp_node *node_p; |
| 4990 | struct lysc_node *child, *iter; |
| 4991 | /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */ |
| 4992 | struct lysc_node_container context_node_fake = |
| 4993 | {.nodetype = LYS_CONTAINER, |
| 4994 | .module = ctx->mod, |
| 4995 | .flags = parent ? parent->flags : 0, |
| 4996 | .child = NULL, .next = NULL, |
| 4997 | .prev = (struct lysc_node *)&context_node_fake, |
| 4998 | .actions = NULL, .notifs = NULL}; |
| 4999 | struct lysp_grp *grp = NULL; |
| 5000 | LY_ARRAY_COUNT_TYPE u; |
| 5001 | uint32_t grp_stack_count; |
| 5002 | struct lys_module *grp_mod, *mod_old; |
| 5003 | LY_ERR ret = LY_SUCCESS; |
| 5004 | struct lysc_when **when, *when_shared; |
| 5005 | struct lysp_augment **augments = NULL; |
| 5006 | LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0; |
| 5007 | struct lysc_notif **notifs = NULL; |
| 5008 | struct lysc_action **actions = NULL; |
| 5009 | |
| 5010 | /* find the referenced grouping */ |
| 5011 | LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod)); |
| 5012 | |
| 5013 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 5014 | grp_stack_count = ctx->groupings.count; |
| 5015 | LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL)); |
| 5016 | if (grp_stack_count == ctx->groupings.count) { |
| 5017 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 5018 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5019 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 5020 | return LY_EVALID; |
| 5021 | } |
| 5022 | |
| 5023 | /* switch context's mod_def */ |
| 5024 | mod_old = ctx->mod_def; |
| 5025 | ctx->mod_def = grp_mod; |
| 5026 | |
| 5027 | /* check status */ |
| 5028 | ret = lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, grp_mod, grp->name); |
| 5029 | LY_CHECK_GOTO(ret, cleanup); |
| 5030 | |
| 5031 | /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly |
| 5032 | * applu refine and augment only to the nodes from the uses */ |
| 5033 | if (parent) { |
| 5034 | child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 5035 | } else if (ctx->mod->compiled->data) { |
| 5036 | child = ctx->mod->compiled->data; |
| 5037 | } else { |
| 5038 | child = NULL; |
| 5039 | } |
| 5040 | /* remember the last child */ |
| 5041 | if (child) { |
| 5042 | child = child->prev; |
| 5043 | } |
| 5044 | |
| 5045 | /* compile data nodes */ |
| 5046 | LY_LIST_FOR(grp->data, node_p) { |
| 5047 | /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */ |
| 5048 | ret = lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3); |
| 5049 | LY_CHECK_GOTO(ret, cleanup); |
| 5050 | } |
| 5051 | |
| 5052 | /* split the children and add the uses's data into the fake context node */ |
| 5053 | if (child) { |
| 5054 | context_node_fake.child = child->next; |
| 5055 | } else if (parent) { |
| 5056 | context_node_fake.child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 5057 | } else if (ctx->mod->compiled->data) { |
| 5058 | context_node_fake.child = ctx->mod->compiled->data; |
| 5059 | } |
| 5060 | if (context_node_fake.child) { |
| 5061 | /* remember child as the last data node added by grouping to fix the list later */ |
| 5062 | child = context_node_fake.child->prev; |
| 5063 | context_node_fake.child->prev = NULL; |
| 5064 | } |
| 5065 | |
| 5066 | when_shared = NULL; |
| 5067 | LY_LIST_FOR(context_node_fake.child, iter) { |
| 5068 | iter->parent = (struct lysc_node *)&context_node_fake; |
| 5069 | |
| 5070 | /* pass uses's when to all the data children, actions and notifications are ignored */ |
| 5071 | if (uses_p->when) { |
| 5072 | LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup); |
| 5073 | if (!when_shared) { |
| 5074 | ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when); |
| 5075 | LY_CHECK_GOTO(ret, cleanup); |
| 5076 | |
| 5077 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5078 | /* do not check "when" semantics in a grouping */ |
| 5079 | ret = ly_set_add(&ctx->xpath, iter, 0, NULL); |
| 5080 | LY_CHECK_GOTO(ret, cleanup); |
| 5081 | } |
| 5082 | |
| 5083 | when_shared = *when; |
| 5084 | } else { |
| 5085 | ++when_shared->refcount; |
| 5086 | (*when) = when_shared; |
| 5087 | |
| 5088 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5089 | /* in this case check "when" again for all children because of dummy node check */ |
| 5090 | ret = ly_set_add(&ctx->xpath, iter, 0, NULL); |
| 5091 | LY_CHECK_GOTO(ret, cleanup); |
| 5092 | } |
| 5093 | } |
| 5094 | } |
| 5095 | } |
| 5096 | |
| 5097 | /* compile actions */ |
| 5098 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 5099 | if (actions) { |
| 5100 | actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0; |
| 5101 | COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup); |
| 5102 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 5103 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 5104 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup); |
| 5105 | LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index; |
| 5106 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions); |
| 5107 | } |
| 5108 | } |
| 5109 | |
| 5110 | /* compile notifications */ |
| 5111 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 5112 | if (notifs) { |
| 5113 | notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0; |
| 5114 | COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup); |
| 5115 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 5116 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 5117 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup); |
| 5118 | LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index; |
| 5119 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs); |
| 5120 | } |
| 5121 | } |
| 5122 | |
| 5123 | /* sort and apply augments */ |
| 5124 | ret = lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments); |
| 5125 | LY_CHECK_GOTO(ret, cleanup); |
| 5126 | LY_ARRAY_FOR(augments, u) { |
| 5127 | ret = lys_compile_augment(ctx, augments[u], (struct lysc_node *)&context_node_fake); |
| 5128 | LY_CHECK_GOTO(ret, cleanup); |
| 5129 | } |
| 5130 | |
| 5131 | /* reload previous context's mod_def */ |
| 5132 | ctx->mod_def = mod_old; |
| 5133 | |
| 5134 | /* apply all refines */ |
| 5135 | ret = lys_compile_refines(ctx, uses_p->refines, (struct lysc_node *)&context_node_fake); |
| 5136 | LY_CHECK_GOTO(ret, cleanup); |
| 5137 | |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5138 | if (first_p) { |
| 5139 | *first_p = context_node_fake.child; |
| 5140 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5141 | |
| 5142 | cleanup: |
| 5143 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5144 | if (context_node_fake.child) { |
| 5145 | context_node_fake.child->prev = child; |
| 5146 | } |
| 5147 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5148 | child->parent = parent; |
| 5149 | } |
| 5150 | |
| 5151 | if (uses_p->augments || uses_p->refines) { |
| 5152 | /* 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] | 5153 | if (context_node_fake.actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5154 | 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] | 5155 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5156 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5157 | if (context_node_fake.notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5158 | 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] | 5159 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5160 | } |
| 5161 | } |
| 5162 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5163 | /* reload previous context's mod_def */ |
| 5164 | ctx->mod_def = mod_old; |
| 5165 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5166 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5167 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5168 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5169 | |
| 5170 | return ret; |
| 5171 | } |
| 5172 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5173 | static int |
| 5174 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5175 | { |
| 5176 | struct lysp_node *iter; |
| 5177 | int len = 0; |
| 5178 | |
| 5179 | *path = NULL; |
| 5180 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5181 | char *s = *path; |
| 5182 | char *id; |
| 5183 | |
| 5184 | switch (iter->nodetype) { |
| 5185 | case LYS_USES: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5186 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5187 | break; |
| 5188 | case LYS_GROUPING: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5189 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5190 | break; |
| 5191 | case LYS_AUGMENT: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5192 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5193 | break; |
| 5194 | default: |
| 5195 | id = strdup(iter->name); |
| 5196 | break; |
| 5197 | } |
| 5198 | |
| 5199 | if (!iter->parent) { |
| 5200 | /* print prefix */ |
| 5201 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5202 | } else { |
| 5203 | /* prefix is the same as in parent */ |
| 5204 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5205 | } |
| 5206 | free(s); |
| 5207 | free(id); |
| 5208 | } |
| 5209 | |
| 5210 | if (len < 0) { |
| 5211 | free(*path); |
| 5212 | *path = NULL; |
| 5213 | } else if (len == 0) { |
| 5214 | *path = strdup("/"); |
| 5215 | len = 1; |
| 5216 | } |
| 5217 | return len; |
| 5218 | } |
| 5219 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5220 | /** |
| 5221 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5222 | * |
| 5223 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5224 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5225 | */ |
| 5226 | static LY_ERR |
| 5227 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5228 | { |
| 5229 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5230 | char *path; |
| 5231 | int len; |
| 5232 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5233 | struct lysp_node_uses fake_uses = { |
| 5234 | .parent = node_p, |
| 5235 | .nodetype = LYS_USES, |
| 5236 | .flags = 0, .next = NULL, |
| 5237 | .name = grp->name, |
| 5238 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5239 | .refines = NULL, .augments = NULL |
| 5240 | }; |
| 5241 | struct lysc_node_container fake_container = { |
| 5242 | .nodetype = LYS_CONTAINER, |
| 5243 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5244 | .module = ctx->mod, |
| 5245 | .sp = NULL, .parent = NULL, .next = NULL, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5246 | .prev = (struct lysc_node *)&fake_container, |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5247 | .name = "fake", |
| 5248 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5249 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5250 | }; |
| 5251 | |
| 5252 | if (grp->parent) { |
| 5253 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5254 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5255 | |
| 5256 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5257 | if (len < 0) { |
| 5258 | LOGMEM(ctx->ctx); |
| 5259 | return LY_EMEM; |
| 5260 | } |
| 5261 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5262 | ctx->path_len = (uint32_t)len; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5263 | free(path); |
| 5264 | |
| 5265 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5266 | lysc_update_path(ctx, NULL, grp->name); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5267 | 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] | 5268 | lysc_update_path(ctx, NULL, NULL); |
| 5269 | lysc_update_path(ctx, NULL, NULL); |
| 5270 | |
| 5271 | ctx->path_len = 1; |
| 5272 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5273 | |
| 5274 | /* cleanup */ |
| 5275 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5276 | |
| 5277 | return ret; |
| 5278 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5279 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5280 | /** |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5281 | * @brief Set config flags for a node. |
| 5282 | * |
| 5283 | * @param[in] ctx Compile context. |
| 5284 | * @param[in] node Compiled node config to set. |
| 5285 | * @param[in] parent Parent of @p node. |
| 5286 | * @return LY_ERR value. |
| 5287 | */ |
| 5288 | static LY_ERR |
| 5289 | lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent) |
| 5290 | { |
| 5291 | if (node->nodetype == LYS_CASE) { |
| 5292 | /* case never has any config */ |
| 5293 | assert(!(node->flags & LYS_CONFIG_MASK)); |
| 5294 | return LY_SUCCESS; |
| 5295 | } |
| 5296 | |
| 5297 | /* adjust parent to always get the ancestor with config */ |
| 5298 | if (parent && (parent->nodetype == LYS_CASE)) { |
| 5299 | parent = parent->parent; |
| 5300 | assert(parent); |
| 5301 | } |
| 5302 | |
| 5303 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
| 5304 | /* ignore config statements inside RPC/action data */ |
| 5305 | node->flags &= ~LYS_CONFIG_MASK; |
| 5306 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5307 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
| 5308 | /* ignore config statements inside Notification data */ |
| 5309 | node->flags &= ~LYS_CONFIG_MASK; |
| 5310 | node->flags |= LYS_CONFIG_R; |
| 5311 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
| 5312 | /* config not explicitely set, inherit it from parent */ |
| 5313 | if (parent) { |
| 5314 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5315 | } else { |
| 5316 | /* default is config true */ |
| 5317 | node->flags |= LYS_CONFIG_W; |
| 5318 | } |
| 5319 | } else { |
| 5320 | /* config set explicitely */ |
| 5321 | node->flags |= LYS_SET_CONFIG; |
| 5322 | } |
| 5323 | |
| 5324 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5325 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5326 | "Configuration node cannot be child of any state data node."); |
| 5327 | return LY_EVALID; |
| 5328 | } |
| 5329 | |
| 5330 | return LY_SUCCESS; |
| 5331 | } |
| 5332 | |
| 5333 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5334 | * @brief Compile parsed schema node information. |
| 5335 | * @param[in] ctx Compile context |
| 5336 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5337 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5338 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5339 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5340 | * @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). |
| 5341 | * 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] | 5342 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5343 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5344 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5345 | 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] | 5346 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5347 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5348 | struct lysc_node *node = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5349 | struct lysc_when **when; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5350 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5351 | 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] | 5352 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5353 | if (node_p->nodetype != LYS_USES) { |
| 5354 | lysc_update_path(ctx, parent, node_p->name); |
| 5355 | } else { |
| 5356 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5357 | lysc_update_path(ctx, NULL, node_p->name); |
| 5358 | } |
| 5359 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5360 | switch (node_p->nodetype) { |
| 5361 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5362 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5363 | node_compile_spec = lys_compile_node_container; |
| 5364 | break; |
| 5365 | case LYS_LEAF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5366 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5367 | node_compile_spec = lys_compile_node_leaf; |
| 5368 | break; |
| 5369 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5370 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5371 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5372 | break; |
| 5373 | case LYS_LEAFLIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5374 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5375 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5376 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5377 | case LYS_CHOICE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5378 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5379 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5380 | break; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5381 | case LYS_CASE: |
| 5382 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case)); |
| 5383 | node_compile_spec = lys_compile_node_case; |
| 5384 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5385 | case LYS_ANYXML: |
| 5386 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5387 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5388 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5389 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5390 | case LYS_USES: |
Michal Vasko | 9e8de2d | 2020-09-01 08:17:10 +0200 | [diff] [blame] | 5391 | 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] | 5392 | lysc_update_path(ctx, NULL, NULL); |
| 5393 | lysc_update_path(ctx, NULL, NULL); |
| 5394 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5395 | default: |
| 5396 | LOGINT(ctx->ctx); |
| 5397 | return LY_EINT; |
| 5398 | } |
| 5399 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5400 | node->nodetype = node_p->nodetype; |
| 5401 | node->module = ctx->mod; |
| 5402 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5403 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5404 | |
| 5405 | /* config */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5406 | ret = lys_compile_config(ctx, node, parent); |
| 5407 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5408 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5409 | /* list ordering */ |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5410 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5411 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5412 | 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] | 5413 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5414 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5415 | node->flags &= ~LYS_ORDBY_MASK; |
| 5416 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5417 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5418 | /* default ordering is system */ |
| 5419 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5420 | } |
| 5421 | } |
| 5422 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5423 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5424 | * 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] | 5425 | 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] | 5426 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5427 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5428 | node->sp = node_p; |
| 5429 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 5430 | DUP_STRING_GOTO(ctx->ctx, node_p->name, node->name, ret, error); |
| 5431 | DUP_STRING_GOTO(ctx->ctx, node_p->dsc, node->dsc, ret, error); |
| 5432 | DUP_STRING_GOTO(ctx->ctx, node_p->ref, node->ref, ret, error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5433 | if (node_p->when) { |
| 5434 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5435 | 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] | 5436 | |
| 5437 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5438 | /* do not check "when" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 5439 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 5440 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5441 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5442 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5443 | 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] | 5444 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5445 | /* insert into parent's children/compiled module (we can no longer free the node separately on error) */ |
| 5446 | LY_CHECK_RET(lys_compile_node_connect(ctx, parent, node)); |
| 5447 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5448 | /* nodetype-specific part */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5449 | LY_CHECK_RET(node_compile_spec(ctx, node_p, node)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5450 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5451 | /* final compilation tasks that require the node to be connected */ |
| 5452 | 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] | 5453 | if (node->flags & LYS_MAND_TRUE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5454 | /* inherit LYS_MAND_TRUE in parent containers */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5455 | lys_compile_mandatory_parents(parent, 1); |
| 5456 | } |
| 5457 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5458 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5459 | return LY_SUCCESS; |
| 5460 | |
| 5461 | error: |
| 5462 | lysc_node_free(ctx->ctx, node); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5463 | done: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5464 | return ret; |
| 5465 | } |
| 5466 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5467 | static void |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5468 | lysc_node_unlink(struct lysc_node *node) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5469 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5470 | struct lysc_node *parent, *child; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5471 | struct lysc_module *modc = node->module->compiled; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5472 | |
| 5473 | parent = node->parent; |
| 5474 | |
| 5475 | /* parent's first child */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5476 | if (parent && lysc_node_children(parent, node->flags) == node) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5477 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5478 | } else if (modc->data == node) { |
| 5479 | modc->data = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5480 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5481 | |
| 5482 | /* special choice case unlinking */ |
| 5483 | if (parent && parent->nodetype == LYS_CHOICE) { |
| 5484 | if (((struct lysc_node_choice *)parent)->dflt == (struct lysc_node_case *)node) { |
| 5485 | /* default case removed */ |
| 5486 | ((struct lysc_node_choice *)parent)->dflt = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5487 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5488 | } |
| 5489 | |
| 5490 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5491 | if (node->prev->next) { |
| 5492 | node->prev->next = node->next; |
| 5493 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5494 | if (node->next) { |
| 5495 | node->next->prev = node->prev; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5496 | } else { |
| 5497 | /* last child */ |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5498 | if (parent) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5499 | child = (struct lysc_node *)lysc_node_children(parent, node->flags); |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5500 | } else { |
| 5501 | child = modc->data; |
| 5502 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5503 | if (child) { |
| 5504 | child->prev = node->prev; |
| 5505 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5506 | } |
| 5507 | } |
| 5508 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 5509 | struct lysc_deviation { |
| 5510 | const char *nodeid; |
| 5511 | struct lysc_node *target; /* target node of the deviation */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5512 | 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] | 5513 | uint16_t flags; /* target's flags from lysc_resolve_schema_nodeid() */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5514 | ly_bool not_supported; /* flag if deviates contains not-supported deviate */ |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 5515 | }; |
| 5516 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5517 | /* MACROS for deviates checking */ |
| 5518 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5519 | if (!(target->nodetype & (NODETYPES))) { \ |
| 5520 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\ |
| 5521 | goto cleanup; \ |
| 5522 | } |
| 5523 | |
| 5524 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5525 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
| 5526 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
| 5527 | lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
| 5528 | goto cleanup; \ |
| 5529 | } |
| 5530 | |
| 5531 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5532 | if (!((TYPE)target)->MEMBER || COND) { \ |
| 5533 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
| 5534 | goto cleanup; \ |
| 5535 | } |
| 5536 | |
| 5537 | /** |
| 5538 | * @brief Apply deviate add. |
| 5539 | * |
| 5540 | * @param[in] ctx Compile context. |
| 5541 | * @param[in] target Deviation target. |
| 5542 | * @param[in] dev_flags Internal deviation flags. |
| 5543 | * @param[in] d Deviate add to apply. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5544 | * @return LY_ERR value. |
| 5545 | */ |
| 5546 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5547 | 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] | 5548 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 5549 | LY_ERR ret = LY_EVALID, rc = LY_SUCCESS; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5550 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 5551 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5552 | LY_ARRAY_COUNT_TYPE x; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5553 | |
| 5554 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5555 | if (((TYPE)target)->MEMBER COND) { \ |
| 5556 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5557 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5558 | PROPERTY, ((TYPE)target)->MEMBER); \ |
| 5559 | goto cleanup; \ |
| 5560 | } |
| 5561 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5562 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5563 | if (((TYPE)target)->MEMBER && (COND)) { \ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5564 | ly_bool dynamic_ = 0; const char *val_; \ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5565 | 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] | 5566 | ctx->mod_def, &dynamic_); \ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5567 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5568 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5569 | if (dynamic_) {free((void*)val_);} \ |
| 5570 | goto cleanup; \ |
| 5571 | } |
| 5572 | |
| 5573 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
| 5574 | if (((TYPE)target)->MEMBER && (COND)) { \ |
| 5575 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5576 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5577 | PROPERTY, ((TYPE)target)->VALUEMEMBER); \ |
| 5578 | goto cleanup; \ |
| 5579 | } |
| 5580 | |
| 5581 | /* [units-stmt] */ |
| 5582 | if (d->units) { |
| 5583 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5584 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units); |
| 5585 | |
| 5586 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 5587 | DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units, rc); |
| 5588 | LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5589 | } |
| 5590 | |
| 5591 | /* *must-stmt */ |
| 5592 | if (d->musts) { |
| 5593 | switch (target->nodetype) { |
| 5594 | case LYS_CONTAINER: |
| 5595 | case LYS_LIST: |
| 5596 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts, |
| 5597 | x, lys_compile_must, ret, cleanup); |
| 5598 | break; |
| 5599 | case LYS_LEAF: |
| 5600 | case LYS_LEAFLIST: |
| 5601 | case LYS_ANYDATA: |
| 5602 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts, |
| 5603 | x, lys_compile_must, ret, cleanup); |
| 5604 | break; |
| 5605 | case LYS_NOTIF: |
| 5606 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts, |
| 5607 | x, lys_compile_must, ret, cleanup); |
| 5608 | break; |
| 5609 | case LYS_RPC: |
| 5610 | case LYS_ACTION: |
| 5611 | if (dev_flags & LYSC_OPT_RPC_INPUT) { |
| 5612 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts, |
| 5613 | x, lys_compile_must, ret, cleanup); |
| 5614 | break; |
| 5615 | } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) { |
| 5616 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts, |
| 5617 | x, lys_compile_must, ret, cleanup); |
| 5618 | break; |
| 5619 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5620 | /* fall through */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5621 | default: |
| 5622 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5623 | lys_nodetype2str(target->nodetype), "add", "must"); |
| 5624 | goto cleanup; |
| 5625 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 5626 | ret = ly_set_add(&ctx->xpath, target, 0, NULL); |
| 5627 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5628 | } |
| 5629 | |
| 5630 | /* *unique-stmt */ |
| 5631 | if (d->uniques) { |
| 5632 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5633 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup); |
| 5634 | } |
| 5635 | |
| 5636 | /* *default-stmt */ |
| 5637 | if (d->dflts) { |
| 5638 | switch (target->nodetype) { |
| 5639 | case LYS_LEAF: |
| 5640 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5641 | 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] | 5642 | if (leaf->dflt) { |
| 5643 | /* first, remove the default value taken from the type */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5644 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5645 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5646 | free(leaf->dflt); |
| 5647 | leaf->dflt = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5648 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5649 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5650 | /* store the default value in unres */ |
Michal Vasko | 31a82d5 | 2020-08-21 08:11:48 +0200 | [diff] [blame] | 5651 | 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] | 5652 | target->flags |= LYS_SET_DFLT; |
| 5653 | break; |
| 5654 | case LYS_LEAFLIST: |
| 5655 | if (llist->dflts && !(target->flags & LYS_SET_DFLT)) { |
| 5656 | /* first, remove the default value taken from the type */ |
| 5657 | LY_ARRAY_FOR(llist->dflts, x) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5658 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5659 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5660 | free(llist->dflts[x]); |
| 5661 | } |
| 5662 | LY_ARRAY_FREE(llist->dflts); |
| 5663 | llist->dflts = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5664 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5665 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5666 | /* store the default values in unres */ |
| 5667 | 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] | 5668 | target->flags |= LYS_SET_DFLT; |
| 5669 | break; |
| 5670 | case LYS_CHOICE: |
| 5671 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
| 5672 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name); |
| 5673 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5674 | * to allow making the default case even the augmented case from the deviating module */ |
| 5675 | if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) { |
| 5676 | goto cleanup; |
| 5677 | } |
| 5678 | break; |
| 5679 | default: |
| 5680 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5681 | lys_nodetype2str(target->nodetype), "add", "default"); |
| 5682 | goto cleanup; |
| 5683 | } |
| 5684 | } |
| 5685 | |
| 5686 | /* [config-stmt] */ |
| 5687 | if (d->flags & LYS_CONFIG_MASK) { |
| 5688 | if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 5689 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5690 | lys_nodetype2str(target->nodetype), "add", "config"); |
| 5691 | goto cleanup; |
| 5692 | } |
| 5693 | if (dev_flags) { |
| 5694 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 5695 | dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
| 5696 | } |
| 5697 | if (target->flags & LYS_SET_CONFIG) { |
| 5698 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5699 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5700 | target->flags & LYS_CONFIG_W ? "true" : "false"); |
| 5701 | goto cleanup; |
| 5702 | } |
| 5703 | LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup); |
| 5704 | } |
| 5705 | |
| 5706 | /* [mandatory-stmt] */ |
| 5707 | if (d->flags & LYS_MAND_MASK) { |
| 5708 | if (target->flags & LYS_MAND_MASK) { |
| 5709 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5710 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5711 | target->flags & LYS_MAND_TRUE ? "true" : "false"); |
| 5712 | goto cleanup; |
| 5713 | } |
| 5714 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup); |
| 5715 | } |
| 5716 | |
| 5717 | /* [min-elements-stmt] */ |
| 5718 | if (d->flags & LYS_SET_MIN) { |
| 5719 | if (target->nodetype == LYS_LEAFLIST) { |
| 5720 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements"); |
| 5721 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5722 | ((struct lysc_node_leaflist *)target)->min = d->min; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5723 | } else if (target->nodetype == LYS_LIST) { |
| 5724 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements"); |
| 5725 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5726 | ((struct lysc_node_list *)target)->min = d->min; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5727 | } else { |
| 5728 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5729 | lys_nodetype2str(target->nodetype), "add", "min-elements"); |
| 5730 | goto cleanup; |
| 5731 | } |
| 5732 | if (d->min) { |
| 5733 | target->flags |= LYS_MAND_TRUE; |
| 5734 | } |
| 5735 | } |
| 5736 | |
| 5737 | /* [max-elements-stmt] */ |
| 5738 | if (d->flags & LYS_SET_MAX) { |
| 5739 | if (target->nodetype == LYS_LEAFLIST) { |
| 5740 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements"); |
| 5741 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5742 | ((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] | 5743 | } else if (target->nodetype == LYS_LIST) { |
| 5744 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements"); |
| 5745 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5746 | ((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] | 5747 | } else { |
| 5748 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5749 | lys_nodetype2str(target->nodetype), "add", "max-elements"); |
| 5750 | goto cleanup; |
| 5751 | } |
| 5752 | } |
| 5753 | |
| 5754 | ret = LY_SUCCESS; |
| 5755 | |
| 5756 | cleanup: |
| 5757 | return ret; |
| 5758 | } |
| 5759 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5760 | static LY_ERR |
| 5761 | lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt) |
| 5762 | { |
| 5763 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5764 | ly_bool dyn = 0; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5765 | const char *orig_dflt; |
| 5766 | uint32_t i; |
| 5767 | |
| 5768 | if (target->module != ctx->mod) { |
| 5769 | /* foreign deviation */ |
| 5770 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt); |
| 5771 | |
| 5772 | /* check that the value matches */ |
| 5773 | orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn); |
| 5774 | if (strcmp(orig_dflt, dflt)) { |
| 5775 | goto error; |
| 5776 | } |
| 5777 | if (dyn) { |
| 5778 | free((char *)orig_dflt); |
| 5779 | } |
| 5780 | |
| 5781 | /* remove the default specification */ |
| 5782 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5783 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5784 | free(leaf->dflt); |
| 5785 | leaf->dflt = NULL; |
| 5786 | } else { |
| 5787 | /* local deviation */ |
| 5788 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt); |
| 5789 | |
| 5790 | /* find the incomplete default */ |
| 5791 | orig_dflt = NULL; |
| 5792 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 5793 | if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) { |
| 5794 | orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt; |
| 5795 | break; |
| 5796 | } |
| 5797 | } |
| 5798 | LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT); |
| 5799 | |
| 5800 | /* check that the value matches */ |
| 5801 | if (strcmp(orig_dflt, dflt)) { |
| 5802 | goto error; |
| 5803 | } |
| 5804 | |
| 5805 | /* update the list of incomplete default values */ |
| 5806 | lysc_incomplete_dflt_remove(ctx, target); |
| 5807 | } |
| 5808 | |
| 5809 | target->flags &= ~LYS_SET_DFLT; |
| 5810 | return LY_SUCCESS; |
| 5811 | |
| 5812 | error: |
| 5813 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5814 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 5815 | dflt, orig_dflt); |
| 5816 | if (dyn) { |
| 5817 | free((char *)orig_dflt); |
| 5818 | } |
| 5819 | cleanup: |
| 5820 | return LY_EVALID; |
| 5821 | } |
| 5822 | |
| 5823 | static LY_ERR |
| 5824 | lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts) |
| 5825 | { |
| 5826 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5827 | ly_bool dyn = 0, found; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5828 | const char *orig_dflt, **orig_dflts; |
| 5829 | uint32_t i; |
| 5830 | LY_ARRAY_COUNT_TYPE x, y; |
| 5831 | |
| 5832 | if (target->module != ctx->mod) { |
| 5833 | /* foreign deviation */ |
| 5834 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]); |
| 5835 | LY_ARRAY_FOR(dflts, x) { |
| 5836 | found = 0; |
| 5837 | LY_ARRAY_FOR(llist->dflts, y) { |
| 5838 | orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn); |
| 5839 | if (!strcmp(orig_dflt, dflts[x])) { |
| 5840 | if (dyn) { |
| 5841 | free((char *)orig_dflt); |
| 5842 | } |
| 5843 | found = 1; |
| 5844 | break; |
| 5845 | } |
| 5846 | if (dyn) { |
| 5847 | free((char *)orig_dflt); |
| 5848 | } |
| 5849 | } |
| 5850 | if (!found) { |
| 5851 | goto error; |
| 5852 | } |
| 5853 | |
| 5854 | /* update compiled default values */ |
| 5855 | LY_ARRAY_DECREMENT(llist->dflts); |
| 5856 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 5857 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 5858 | free(llist->dflts[y]); |
| 5859 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts)); |
| 5860 | } |
| 5861 | if (!LY_ARRAY_COUNT(llist->dflts)) { |
| 5862 | LY_ARRAY_FREE(llist->dflts); |
| 5863 | llist->dflts = NULL; |
| 5864 | llist->flags &= ~LYS_SET_DFLT; |
| 5865 | } |
| 5866 | } else { |
| 5867 | /* local deviation */ |
| 5868 | orig_dflt = NULL; |
| 5869 | orig_dflts = NULL; |
| 5870 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 5871 | if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) { |
| 5872 | orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt; |
| 5873 | orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts; |
| 5874 | break; |
| 5875 | } |
| 5876 | } |
| 5877 | LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT); |
| 5878 | |
| 5879 | if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) { |
| 5880 | /* TODO it is not currently possible to remove just one default value from incomplete defaults array */ |
| 5881 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5882 | "Local deviation deleting leaf-list defaults is not supported."); |
| 5883 | return LY_EVALID; |
| 5884 | } |
| 5885 | |
| 5886 | LY_ARRAY_FOR(dflts, x) { |
| 5887 | found = 0; |
| 5888 | if (orig_dflts) { |
| 5889 | LY_ARRAY_FOR(orig_dflts, y) { |
| 5890 | if (!strcmp(orig_dflts[y], dflts[x])) { |
| 5891 | found = 1; |
| 5892 | break; |
| 5893 | } |
| 5894 | } |
| 5895 | } else if (!strcmp(orig_dflt, dflts[x])) { |
| 5896 | found = 1; |
| 5897 | } |
| 5898 | if (!found) { |
| 5899 | goto error; |
| 5900 | } |
| 5901 | |
| 5902 | /* update the list of incomplete default values */ |
| 5903 | lysc_incomplete_dflt_remove(ctx, target); |
| 5904 | } |
| 5905 | |
| 5906 | llist->flags &= ~LYS_SET_DFLT; |
| 5907 | } |
| 5908 | |
| 5909 | return LY_SUCCESS; |
| 5910 | |
| 5911 | error: |
| 5912 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 5913 | "which does not match any of the target's property values.", dflts[x]); |
| 5914 | cleanup: |
| 5915 | return LY_EVALID; |
| 5916 | } |
| 5917 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5918 | /** |
| 5919 | * @brief Apply deviate delete. |
| 5920 | * |
| 5921 | * @param[in] ctx Compile context. |
| 5922 | * @param[in] target Deviation target. |
| 5923 | * @param[in] dev_flags Internal deviation flags. |
| 5924 | * @param[in] d Deviate delete to apply. |
| 5925 | * @return LY_ERR value. |
| 5926 | */ |
| 5927 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5928 | 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] | 5929 | { |
| 5930 | LY_ERR ret = LY_EVALID; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5931 | struct lysc_node_list *list = (struct lysc_node_list *)target; |
| 5932 | LY_ARRAY_COUNT_TYPE x, y, z; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5933 | size_t prefix_len, name_len; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5934 | const char *prefix, *name, *nodeid; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5935 | struct lys_module *mod; |
| 5936 | |
| 5937 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5938 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \ |
| 5939 | LY_ARRAY_FOR(d->ARRAY_DEV, x) { \ |
| 5940 | LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \ |
| 5941 | if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \ |
| 5942 | } \ |
| 5943 | if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \ |
| 5944 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5945 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5946 | PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \ |
| 5947 | goto cleanup; \ |
| 5948 | } \ |
| 5949 | LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \ |
| 5950 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \ |
| 5951 | memmove(&((TYPE)target)->ARRAY_TRG[y], \ |
| 5952 | &((TYPE)target)->ARRAY_TRG[y + 1], \ |
| 5953 | (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \ |
| 5954 | } \ |
| 5955 | if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \ |
| 5956 | LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \ |
| 5957 | ((TYPE)target)->ARRAY_TRG = NULL; \ |
| 5958 | } |
| 5959 | |
| 5960 | /* [units-stmt] */ |
| 5961 | if (d->units) { |
| 5962 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5963 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, 0, units, "deleting", "units", d->units); |
| 5964 | if (strcmp(((struct lysc_node_leaf *)target)->units, d->units)) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5965 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5966 | "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] | 5967 | d->units, ((struct lysc_node_leaf *)target)->units); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5968 | goto cleanup; |
| 5969 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5970 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf *)target)->units); |
| 5971 | ((struct lysc_node_leaf *)target)->units = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5972 | } |
| 5973 | |
| 5974 | /* *must-stmt */ |
| 5975 | if (d->musts) { |
| 5976 | switch (target->nodetype) { |
| 5977 | case LYS_CONTAINER: |
| 5978 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5979 | 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] | 5980 | break; |
| 5981 | case LYS_LEAF: |
| 5982 | case LYS_LEAFLIST: |
| 5983 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5984 | 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] | 5985 | break; |
| 5986 | case LYS_NOTIF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5987 | 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] | 5988 | break; |
| 5989 | case LYS_RPC: |
| 5990 | case LYS_ACTION: |
| 5991 | if (dev_flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5992 | 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] | 5993 | break; |
| 5994 | } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5995 | 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] | 5996 | break; |
| 5997 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5998 | /* fall through */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5999 | default: |
| 6000 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6001 | lys_nodetype2str(target->nodetype), "delete", "must"); |
| 6002 | goto cleanup; |
| 6003 | } |
| 6004 | } |
| 6005 | |
| 6006 | /* *unique-stmt */ |
| 6007 | if (d->uniques) { |
| 6008 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6009 | LY_ARRAY_FOR(d->uniques, x) { |
| 6010 | LY_ARRAY_FOR(list->uniques, z) { |
| 6011 | for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6012 | nodeid = strpbrk(name, " \t\n"); |
| 6013 | if (nodeid) { |
| 6014 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
| 6015 | break; |
| 6016 | } |
| 6017 | while (isspace(*nodeid)) { |
| 6018 | ++nodeid; |
| 6019 | } |
| 6020 | } else { |
| 6021 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6022 | break; |
| 6023 | } |
| 6024 | } |
| 6025 | } |
| 6026 | if (!name) { |
| 6027 | /* complete match - remove the unique */ |
| 6028 | LY_ARRAY_DECREMENT(list->uniques); |
| 6029 | LY_ARRAY_FREE(list->uniques[z]); |
| 6030 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques)); |
| 6031 | --z; |
| 6032 | break; |
| 6033 | } |
| 6034 | } |
| 6035 | if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) { |
| 6036 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6037 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6038 | d->uniques[x]); |
| 6039 | goto cleanup; |
| 6040 | } |
| 6041 | } |
| 6042 | if (!LY_ARRAY_COUNT(list->uniques)) { |
| 6043 | LY_ARRAY_FREE(list->uniques); |
| 6044 | list->uniques = NULL; |
| 6045 | } |
| 6046 | } |
| 6047 | |
| 6048 | /* *default-stmt */ |
| 6049 | if (d->dflts) { |
| 6050 | switch (target->nodetype) { |
| 6051 | case LYS_LEAF: |
| 6052 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6053 | 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] | 6054 | break; |
| 6055 | case LYS_LEAFLIST: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6056 | 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] | 6057 | break; |
| 6058 | case LYS_CHOICE: |
| 6059 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6060 | 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] | 6061 | nodeid = d->dflts[0]; |
| 6062 | LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup); |
| 6063 | if (prefix) { |
| 6064 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6065 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6066 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6067 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6068 | "The prefix does not match any imported module of the deviation module.", d->dflts[0]); |
| 6069 | goto cleanup; |
| 6070 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6071 | if (mod != ((struct lysc_node_choice *)target)->dflt->module) { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6072 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6073 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6074 | "The prefix does not match the default case's module.", d->dflts[0]); |
| 6075 | goto cleanup; |
| 6076 | } |
| 6077 | } |
| 6078 | /* else { |
| 6079 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6080 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6081 | * the module of the default case, just matching its name */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6082 | if (strcmp(name, ((struct lysc_node_choice *)target)->dflt->name)) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6083 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6084 | "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] | 6085 | d->dflts[0], ((struct lysc_node_choice *)target)->dflt->name); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6086 | goto cleanup; |
| 6087 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6088 | ((struct lysc_node_choice *)target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6089 | ((struct lysc_node_choice *)target)->dflt = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6090 | break; |
| 6091 | default: |
| 6092 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6093 | lys_nodetype2str(target->nodetype), "delete", "default"); |
| 6094 | goto cleanup; |
| 6095 | } |
| 6096 | } |
| 6097 | |
| 6098 | ret = LY_SUCCESS; |
| 6099 | |
| 6100 | cleanup: |
| 6101 | return ret; |
| 6102 | } |
| 6103 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6104 | static LY_ERR |
| 6105 | lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target) |
| 6106 | { |
| 6107 | LY_ERR ret; |
| 6108 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 6109 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 6110 | struct ly_err_item *err = NULL; |
| 6111 | LY_ARRAY_COUNT_TYPE x; |
| 6112 | const char *dflt; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6113 | ly_bool dyn; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6114 | |
| 6115 | if (target->module != ctx->mod) { |
| 6116 | /* foreign deviation */ |
| 6117 | if (target->nodetype == LYS_LEAF) { |
| 6118 | dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn); |
| 6119 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6120 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6121 | |
| 6122 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 6123 | LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err); |
| 6124 | if (dyn) { |
| 6125 | free((char *)dflt); |
| 6126 | } |
| 6127 | if (err) { |
| 6128 | ly_err_print(err); |
| 6129 | ctx->path[0] = '\0'; |
| 6130 | lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6131 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6132 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6133 | ly_err_free(err); |
| 6134 | } |
| 6135 | LY_CHECK_RET(ret); |
| 6136 | |
| 6137 | ++leaf->dflt->realtype->refcount; |
| 6138 | } else { /* LY_LEAFLIST */ |
| 6139 | LY_ARRAY_FOR(llist->dflts, x) { |
| 6140 | dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn); |
| 6141 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6142 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6143 | |
| 6144 | ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 6145 | LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err); |
| 6146 | if (dyn) { |
| 6147 | free((char *)dflt); |
| 6148 | } |
| 6149 | if (err) { |
| 6150 | ly_err_print(err); |
| 6151 | ctx->path[0] = '\0'; |
| 6152 | lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6153 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6154 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6155 | ly_err_free(err); |
| 6156 | } |
| 6157 | LY_CHECK_RET(ret); |
| 6158 | |
| 6159 | ++llist->dflts[x]->realtype->refcount; |
| 6160 | } |
| 6161 | } |
| 6162 | } else { |
| 6163 | /* local deviation */ |
| 6164 | |
| 6165 | /* these default were not compiled yet, so they will use the new type automatically */ |
| 6166 | } |
| 6167 | |
| 6168 | return LY_SUCCESS; |
| 6169 | } |
| 6170 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6171 | /** |
| 6172 | * @brief Apply deviate replace. |
| 6173 | * |
| 6174 | * @param[in] ctx Compile context. |
| 6175 | * @param[in] target Deviation target. |
| 6176 | * @param[in] d Deviate replace to apply. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6177 | * @return LY_ERR value. |
| 6178 | */ |
| 6179 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6180 | 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] | 6181 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 6182 | LY_ERR ret = LY_EVALID, rc = LY_SUCCESS; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6183 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 6184 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 6185 | LY_ARRAY_COUNT_TYPE x; |
| 6186 | |
| 6187 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 6188 | if (!(((TYPE)target)->MEMBER COND)) { \ |
| 6189 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 6190 | "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \ |
| 6191 | goto cleanup; \ |
| 6192 | } |
| 6193 | |
| 6194 | /* [type-stmt] */ |
| 6195 | if (d->type) { |
| 6196 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6197 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6198 | lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type); |
| 6199 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6200 | /* remove only default value inherited from the type */ |
| 6201 | if (!(target->flags & LYS_SET_DFLT)) { |
| 6202 | if (target->module != ctx->mod) { |
| 6203 | /* foreign deviation - the target has default from the previous type, remove it */ |
| 6204 | if (target->nodetype == LYS_LEAF) { |
| 6205 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6206 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6207 | free(leaf->dflt); |
| 6208 | leaf->dflt = NULL; |
| 6209 | } else { /* LYS_LEAFLIST */ |
| 6210 | LY_ARRAY_FOR(llist->dflts, x) { |
| 6211 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6212 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6213 | free(llist->dflts[x]); |
| 6214 | } |
| 6215 | LY_ARRAY_FREE(llist->dflts); |
| 6216 | llist->dflts = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6217 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6218 | } else { |
| 6219 | /* local deviation */ |
| 6220 | lysc_incomplete_dflt_remove(ctx, target); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6221 | } |
| 6222 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6223 | |
| 6224 | LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf)); |
| 6225 | |
| 6226 | if (target->flags & LYS_SET_DFLT) { |
| 6227 | /* the term default value(s) needs to be recompiled */ |
| 6228 | LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target)); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6229 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6230 | } |
| 6231 | |
| 6232 | /* [units-stmt] */ |
| 6233 | if (d->units) { |
| 6234 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6235 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS), |
| 6236 | units, "replacing", "units", d->units); |
| 6237 | |
| 6238 | lydict_remove(ctx->ctx, leaf->units); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 6239 | DUP_STRING(ctx->ctx, d->units, leaf->units, rc); |
| 6240 | LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6241 | } |
| 6242 | |
| 6243 | /* [default-stmt] */ |
| 6244 | if (d->dflt) { |
| 6245 | switch (target->nodetype) { |
| 6246 | case LYS_LEAF: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6247 | if (target->module != ctx->mod) { |
| 6248 | /* foreign deviation */ |
| 6249 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing", |
| 6250 | "default", d->dflt); |
| 6251 | |
| 6252 | /* remove the default specification */ |
| 6253 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6254 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6255 | free(leaf->dflt); |
| 6256 | leaf->dflt = NULL; |
| 6257 | } else { |
| 6258 | /* local deviation */ |
| 6259 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing", |
| 6260 | "default", d->dflt); |
| 6261 | assert(!leaf->dflt); |
| 6262 | } |
| 6263 | |
| 6264 | /* store the new default value */ |
| 6265 | 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] | 6266 | break; |
| 6267 | case LYS_CHOICE: |
| 6268 | DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt); |
| 6269 | if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) { |
| 6270 | goto cleanup; |
| 6271 | } |
| 6272 | break; |
| 6273 | default: |
| 6274 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6275 | lys_nodetype2str(target->nodetype), "replace", "default"); |
| 6276 | goto cleanup; |
| 6277 | } |
| 6278 | } |
| 6279 | |
| 6280 | /* [config-stmt] */ |
| 6281 | if (d->flags & LYS_CONFIG_MASK) { |
| 6282 | if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 6283 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6284 | lys_nodetype2str(target->nodetype), "replace", "config"); |
| 6285 | goto cleanup; |
| 6286 | } |
| 6287 | if (!(target->flags & LYS_SET_CONFIG)) { |
| 6288 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6289 | "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6290 | goto cleanup; |
| 6291 | } |
| 6292 | LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup); |
| 6293 | } |
| 6294 | |
| 6295 | /* [mandatory-stmt] */ |
| 6296 | if (d->flags & LYS_MAND_MASK) { |
| 6297 | if (!(target->flags & LYS_MAND_MASK)) { |
| 6298 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6299 | "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6300 | goto cleanup; |
| 6301 | } |
| 6302 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup); |
| 6303 | } |
| 6304 | |
| 6305 | /* [min-elements-stmt] */ |
| 6306 | if (d->flags & LYS_SET_MIN) { |
| 6307 | if (target->nodetype == LYS_LEAFLIST) { |
| 6308 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements"); |
| 6309 | /* change value */ |
| 6310 | ((struct lysc_node_leaflist *)target)->min = d->min; |
| 6311 | } else if (target->nodetype == LYS_LIST) { |
| 6312 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements"); |
| 6313 | /* change value */ |
| 6314 | ((struct lysc_node_list *)target)->min = d->min; |
| 6315 | } else { |
| 6316 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6317 | lys_nodetype2str(target->nodetype), "replace", "min-elements"); |
| 6318 | goto cleanup; |
| 6319 | } |
| 6320 | if (d->min) { |
| 6321 | target->flags |= LYS_MAND_TRUE; |
| 6322 | } |
| 6323 | } |
| 6324 | |
| 6325 | /* [max-elements-stmt] */ |
| 6326 | if (d->flags & LYS_SET_MAX) { |
| 6327 | if (target->nodetype == LYS_LEAFLIST) { |
| 6328 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements"); |
| 6329 | /* change value */ |
| 6330 | ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1; |
| 6331 | } else if (target->nodetype == LYS_LIST) { |
| 6332 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements"); |
| 6333 | /* change value */ |
| 6334 | ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1; |
| 6335 | } else { |
| 6336 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6337 | lys_nodetype2str(target->nodetype), "replace", "max-elements"); |
| 6338 | goto cleanup; |
| 6339 | } |
| 6340 | } |
| 6341 | |
| 6342 | ret = LY_SUCCESS; |
| 6343 | |
| 6344 | cleanup: |
| 6345 | return ret; |
| 6346 | } |
| 6347 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6348 | /** |
| 6349 | * @brief Apply all deviations of one target node. |
| 6350 | * |
| 6351 | * @param[in] ctx Compile context. |
| 6352 | * @param[in] dev Deviation structure to apply. |
| 6353 | * @return LY_ERR value. |
| 6354 | */ |
| 6355 | static LY_ERR |
| 6356 | lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6357 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6358 | LY_ERR ret = LY_EVALID; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6359 | struct lysc_node *target = dev->target; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6360 | struct lysc_action *rpcs; |
| 6361 | struct lysc_notif *notifs; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6362 | struct lysp_deviate *d; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6363 | LY_ARRAY_COUNT_TYPE v, x; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6364 | uint32_t min, max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6365 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6366 | lysc_update_path(ctx, NULL, dev->nodeid); |
| 6367 | |
| 6368 | /* not-supported */ |
| 6369 | if (dev->not_supported) { |
| 6370 | if (LY_ARRAY_COUNT(dev->deviates) > 1) { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 6371 | 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] | 6372 | LY_ARRAY_COUNT(dev->deviates), dev->nodeid); |
| 6373 | } |
| 6374 | |
| 6375 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 6376 | if (target->parent) { \ |
| 6377 | ARRAY = (TYPE*)GETFUNC(target->parent); \ |
| 6378 | } else { \ |
| 6379 | ARRAY = target->module->compiled->ARRAY; \ |
| 6380 | } \ |
| 6381 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 6382 | if (&ARRAY[x] == (TYPE*)target) { break; } \ |
| 6383 | } \ |
| 6384 | if (x < LY_ARRAY_COUNT(ARRAY)) { \ |
| 6385 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 6386 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 6387 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 6388 | } |
| 6389 | |
| 6390 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 6391 | if (dev->flags & LYSC_OPT_RPC_INPUT) { |
| 6392 | /* remove RPC's/action's input */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6393 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input); |
| 6394 | memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input); |
| 6395 | FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free); |
| 6396 | ((struct lysc_action *)target)->input_exts = NULL; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6397 | } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6398 | /* remove RPC's/action's output */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6399 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output); |
| 6400 | memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output); |
| 6401 | FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free); |
| 6402 | ((struct lysc_action *)target)->output_exts = NULL; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6403 | } else { |
| 6404 | /* remove RPC/action */ |
| 6405 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
| 6406 | } |
| 6407 | } else if (target->nodetype == LYS_NOTIF) { |
| 6408 | /* remove Notification */ |
| 6409 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
| 6410 | } else { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 6411 | if (target->parent && (target->parent->nodetype == LYS_CASE) && (target->prev == target)) { |
| 6412 | /* remove the target node with its parent case node because it is the only node of the case */ |
| 6413 | lysc_node_unlink(target->parent); |
| 6414 | lysc_node_free(ctx->ctx, target->parent); |
| 6415 | } else { |
| 6416 | /* remove the target node */ |
| 6417 | lysc_node_unlink(target); |
| 6418 | lysc_node_free(ctx->ctx, target); |
| 6419 | } |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6420 | } |
| 6421 | |
| 6422 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 6423 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
| 6424 | return LY_SUCCESS; |
| 6425 | } |
| 6426 | |
| 6427 | /* list of deviates (not-supported is not present in the list) */ |
| 6428 | LY_ARRAY_FOR(dev->deviates, v) { |
| 6429 | d = dev->deviates[v]; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6430 | switch (d->mod) { |
| 6431 | case LYS_DEV_ADD: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6432 | 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] | 6433 | break; |
| 6434 | case LYS_DEV_DELETE: |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6435 | 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] | 6436 | break; |
| 6437 | case LYS_DEV_REPLACE: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6438 | 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] | 6439 | break; |
| 6440 | default: |
| 6441 | LOGINT(ctx->ctx); |
| 6442 | goto cleanup; |
| 6443 | } |
| 6444 | } |
| 6445 | |
| 6446 | /* final check when all deviations of a single target node are applied */ |
| 6447 | |
| 6448 | /* check min-max compatibility */ |
| 6449 | if (target->nodetype == LYS_LEAFLIST) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6450 | min = ((struct lysc_node_leaflist *)target)->min; |
| 6451 | max = ((struct lysc_node_leaflist *)target)->max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6452 | } else if (target->nodetype == LYS_LIST) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6453 | min = ((struct lysc_node_list *)target)->min; |
| 6454 | max = ((struct lysc_node_list *)target)->max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6455 | } else { |
| 6456 | min = max = 0; |
| 6457 | } |
| 6458 | if (min > max) { |
| 6459 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements " |
| 6460 | "after deviation: min value %u is bigger than max value %u.", min, max); |
| 6461 | goto cleanup; |
| 6462 | } |
| 6463 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6464 | /* check mandatory - default compatibility */ |
| 6465 | if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT) |
| 6466 | && (target->flags & LYS_MAND_TRUE)) { |
| 6467 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6468 | "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype)); |
| 6469 | goto cleanup; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6470 | } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)target)->dflt |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6471 | && (target->flags & LYS_MAND_TRUE)) { |
| 6472 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice."); |
| 6473 | goto cleanup; |
| 6474 | } |
| 6475 | if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) { |
| 6476 | /* mandatory node under a default case */ |
| 6477 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6478 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6479 | lys_nodetype2str(target->nodetype), target->name, target->parent->name); |
| 6480 | goto cleanup; |
| 6481 | } |
| 6482 | |
| 6483 | /* success */ |
| 6484 | ret = LY_SUCCESS; |
| 6485 | |
| 6486 | cleanup: |
| 6487 | lysc_update_path(ctx, NULL, NULL); |
| 6488 | return ret; |
| 6489 | } |
| 6490 | |
| 6491 | LY_ERR |
| 6492 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
| 6493 | { |
| 6494 | LY_ERR ret = LY_EVALID; |
| 6495 | struct ly_set devs_p = {0}; |
| 6496 | struct ly_set targets = {0}; |
| 6497 | struct lysc_node *target; /* target target of the deviation */ |
| 6498 | struct lysp_deviation *dev; |
| 6499 | struct lysp_deviate *d, **dp_new; |
| 6500 | LY_ARRAY_COUNT_TYPE u, v; |
| 6501 | struct lysc_deviation **devs = NULL; |
| 6502 | struct lys_module *target_mod, **dev_mod; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6503 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6504 | |
| 6505 | /* get all deviations from the module and all its submodules ... */ |
| 6506 | LY_ARRAY_FOR(mod_p->deviations, u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6507 | 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] | 6508 | } |
| 6509 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 6510 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6511 | 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] | 6512 | } |
| 6513 | } |
| 6514 | if (!devs_p.count) { |
| 6515 | /* nothing to do */ |
| 6516 | return LY_SUCCESS; |
| 6517 | } |
| 6518 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6519 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 6520 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6521 | /* ... and group them by the target node */ |
| 6522 | devs = calloc(devs_p.count, sizeof *devs); |
| 6523 | for (u = 0; u < devs_p.count; ++u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6524 | uint32_t index; |
| 6525 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6526 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6527 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6528 | |
| 6529 | /* resolve the target */ |
Michal Vasko | cb18c7f | 2020-08-24 09:22:28 +0200 | [diff] [blame] | 6530 | 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] | 6531 | (const struct lysc_node **)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6532 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6533 | /* move the target pointer to input/output to make them different from the action and |
| 6534 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 6535 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 6536 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 6537 | if (flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6538 | target = (struct lysc_node *)&((struct lysc_action *)target)->input; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6539 | flags |= LYSC_OPT_INTERNAL; |
| 6540 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6541 | target = (struct lysc_node *)&((struct lysc_action *)target)->output; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6542 | flags |= LYSC_OPT_INTERNAL; |
| 6543 | } |
| 6544 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6545 | /* insert into the set of targets with duplicity detection */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6546 | ret = ly_set_add(&targets, target, 0, &index); |
| 6547 | LY_CHECK_GOTO(ret, cleanup); |
| 6548 | if (!devs[index]) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6549 | /* new record */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6550 | devs[index] = calloc(1, sizeof **devs); |
| 6551 | devs[index]->target = target; |
| 6552 | devs[index]->nodeid = dev->nodeid; |
| 6553 | devs[index]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6554 | } |
| 6555 | /* add deviates into the deviation's list of deviates */ |
Michal Vasko | 5c38f36 | 2020-08-24 09:22:51 +0200 | [diff] [blame] | 6556 | LY_LIST_FOR(dev->deviates, d) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6557 | 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] | 6558 | *dp_new = d; |
| 6559 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6560 | devs[index]->not_supported = 1; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6561 | } |
| 6562 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6563 | |
| 6564 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6565 | } |
| 6566 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6567 | /* apply deviations */ |
| 6568 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6569 | ly_bool match = 0; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6570 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6571 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 6572 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 6573 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6574 | 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] | 6575 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6576 | 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] | 6577 | } |
| 6578 | } |
| 6579 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6580 | /* remember target module (the target node may be removed) */ |
| 6581 | target_mod = devs[u]->target->module; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6582 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6583 | /* apply the deviation */ |
| 6584 | LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6585 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6586 | /* 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] | 6587 | LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) { |
| 6588 | if (target_mod->compiled->deviated_by[v] == mod_p->mod) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6589 | match = 1; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6590 | break; |
| 6591 | } |
| 6592 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6593 | if (!match) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6594 | 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] | 6595 | *dev_mod = mod_p->mod; |
| 6596 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6597 | } |
| 6598 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6599 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6600 | ret = LY_SUCCESS; |
| 6601 | |
| 6602 | cleanup: |
| 6603 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6604 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6605 | free(devs[u]); |
| 6606 | } |
| 6607 | free(devs); |
| 6608 | ly_set_erase(&targets, NULL); |
| 6609 | ly_set_erase(&devs_p, NULL); |
| 6610 | |
| 6611 | return ret; |
| 6612 | } |
| 6613 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6614 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6615 | * @brief Compile the given YANG submodule into the main module. |
| 6616 | * @param[in] ctx Compile context |
| 6617 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6618 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6619 | */ |
| 6620 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6621 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6622 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6623 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6624 | LY_ERR ret = LY_SUCCESS; |
| 6625 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6626 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6627 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6628 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6629 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6630 | if (!mainmod->mod->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6631 | /* features are compiled directly into the compiled module structure, |
| 6632 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6633 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6634 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6635 | LY_CHECK_GOTO(ret, error); |
| 6636 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6637 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6638 | if (!mainmod->mod->dis_identities) { |
| 6639 | ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities); |
| 6640 | LY_CHECK_GOTO(ret, error); |
| 6641 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6642 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6643 | /* data nodes */ |
| 6644 | LY_LIST_FOR(submod->data, node_p) { |
| 6645 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6646 | LY_CHECK_GOTO(ret, error); |
| 6647 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6648 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6649 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6650 | 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] | 6651 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6652 | error: |
| 6653 | return ret; |
| 6654 | } |
| 6655 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6656 | static void * |
| 6657 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6658 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6659 | for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6660 | if (substmts[u].stmt == stmt) { |
| 6661 | return substmts[u].storage; |
| 6662 | } |
| 6663 | } |
| 6664 | return NULL; |
| 6665 | } |
| 6666 | |
| 6667 | LY_ERR |
| 6668 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6669 | { |
| 6670 | LY_ERR ret = LY_EVALID, r; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6671 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6672 | struct lysp_stmt *stmt; |
| 6673 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6674 | |
| 6675 | /* check for invalid substatements */ |
| 6676 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6677 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6678 | continue; |
| 6679 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6680 | for (u = 0; substmts[u].stmt; ++u) { |
| 6681 | if (substmts[u].stmt == stmt->kw) { |
| 6682 | break; |
| 6683 | } |
| 6684 | } |
| 6685 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6686 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6687 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6688 | goto cleanup; |
| 6689 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6690 | } |
| 6691 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6692 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6693 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6694 | /* keep order of the processing the same as the order in the defined substmts, |
| 6695 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6696 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6697 | ly_bool stmt_present = 0; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6698 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6699 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6700 | if (substmts[u].stmt != stmt->kw) { |
| 6701 | continue; |
| 6702 | } |
| 6703 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6704 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6705 | if (substmts[u].storage) { |
| 6706 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6707 | case LY_STMT_STATUS: |
| 6708 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6709 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6710 | break; |
| 6711 | case LY_STMT_UNITS: { |
| 6712 | const char **units; |
| 6713 | |
| 6714 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6715 | /* single item */ |
| 6716 | if (*((const char **)substmts[u].storage)) { |
| 6717 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6718 | goto cleanup; |
| 6719 | } |
| 6720 | units = (const char **)substmts[u].storage; |
| 6721 | } else { |
| 6722 | /* sized array */ |
| 6723 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6724 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6725 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 6726 | r = lydict_insert(ctx->ctx, stmt->arg, 0, units); |
| 6727 | LY_CHECK_ERR_GOTO(r, ret = r, cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6728 | break; |
| 6729 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6730 | case LY_STMT_TYPE: { |
| 6731 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6732 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6733 | |
| 6734 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6735 | /* single item */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6736 | if (*(struct lysc_type **)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6737 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6738 | goto cleanup; |
| 6739 | } |
| 6740 | compiled = substmts[u].storage; |
| 6741 | } else { |
| 6742 | /* sized array */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6743 | struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6744 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6745 | compiled = (void *)type; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6746 | } |
| 6747 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6748 | 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] | 6749 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL, |
| 6750 | 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] | 6751 | 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] | 6752 | lysp_type_free(ctx->ctx, parsed); |
| 6753 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6754 | break; |
| 6755 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6756 | case LY_STMT_IF_FEATURE: { |
| 6757 | struct lysc_iffeature *iff = NULL; |
| 6758 | |
| 6759 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6760 | /* single item */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6761 | if (((struct lysc_iffeature *)substmts[u].storage)->features) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6762 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6763 | goto cleanup; |
| 6764 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6765 | iff = (struct lysc_iffeature *)substmts[u].storage; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6766 | } else { |
| 6767 | /* sized array */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6768 | struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6769 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6770 | } |
| 6771 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6772 | break; |
| 6773 | } |
| 6774 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6775 | * 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] | 6776 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6777 | 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.", |
| 6778 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6779 | goto cleanup; |
| 6780 | } |
| 6781 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6782 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6783 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6784 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6785 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6786 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6787 | goto cleanup; |
| 6788 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6789 | } |
| 6790 | |
| 6791 | ret = LY_SUCCESS; |
| 6792 | |
| 6793 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6794 | return ret; |
| 6795 | } |
| 6796 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6797 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6798 | * @brief Check when for cyclic dependencies. |
| 6799 | * @param[in] set Set with all the referenced nodes. |
| 6800 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6801 | * @return LY_ERR value |
| 6802 | */ |
| 6803 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6804 | 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] | 6805 | { |
| 6806 | struct lyxp_set tmp_set; |
| 6807 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6808 | uint32_t i, j; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6809 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6810 | struct lysc_when *when; |
| 6811 | LY_ERR ret = LY_SUCCESS; |
| 6812 | |
| 6813 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6814 | |
| 6815 | /* prepare in_ctx of the set */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6816 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6817 | xp_scnode = &set->val.scnodes[i]; |
| 6818 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6819 | if (xp_scnode->in_ctx != -1) { |
| 6820 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6821 | xp_scnode->in_ctx = 1; |
| 6822 | } |
| 6823 | } |
| 6824 | |
| 6825 | for (i = 0; i < set->used; ++i) { |
| 6826 | xp_scnode = &set->val.scnodes[i]; |
| 6827 | if (xp_scnode->in_ctx != 1) { |
| 6828 | /* already checked */ |
| 6829 | continue; |
| 6830 | } |
| 6831 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6832 | 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] | 6833 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6834 | /* no when to check */ |
| 6835 | xp_scnode->in_ctx = 0; |
| 6836 | continue; |
| 6837 | } |
| 6838 | |
| 6839 | node = xp_scnode->scnode; |
| 6840 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6841 | LY_ARRAY_FOR(node->when, u) { |
| 6842 | when = node->when[u]; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6843 | ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6844 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 6845 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6846 | 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] | 6847 | goto cleanup; |
| 6848 | } |
| 6849 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6850 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6851 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6852 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6853 | /* try to find this node in our set */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6854 | uint32_t idx; |
| 6855 | 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] | 6856 | 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] | 6857 | ret = LY_EVALID; |
| 6858 | goto cleanup; |
| 6859 | } |
| 6860 | |
| 6861 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6862 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6863 | } else { |
| 6864 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6865 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6866 | } |
| 6867 | } |
| 6868 | |
| 6869 | /* merge this set into the global when set */ |
| 6870 | lyxp_set_scnode_merge(set, &tmp_set); |
| 6871 | } |
| 6872 | |
| 6873 | /* check when of non-data parents as well */ |
| 6874 | node = node->parent; |
| 6875 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 6876 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6877 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 6878 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6879 | } |
| 6880 | |
| 6881 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6882 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6883 | return ret; |
| 6884 | } |
| 6885 | |
| 6886 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6887 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 6888 | * @param[in] ctx Compile context. |
| 6889 | * @param[in] node Node to check. |
| 6890 | * @return LY_ERR value |
| 6891 | */ |
| 6892 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6893 | 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] | 6894 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6895 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6896 | uint32_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6897 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6898 | uint32_t opts; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6899 | ly_bool input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6900 | struct lysc_when **when = NULL; |
| 6901 | struct lysc_must *musts = NULL; |
| 6902 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6903 | const struct lysc_node *op; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6904 | |
| 6905 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6906 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6907 | if (node->flags & LYS_CONFIG_R) { |
| 6908 | for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent); |
| 6909 | if (op) { |
| 6910 | /* we are actually in output */ |
| 6911 | opts = LYXP_SCNODE_OUTPUT; |
| 6912 | } |
| 6913 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6914 | |
| 6915 | switch (node->nodetype) { |
| 6916 | case LYS_CONTAINER: |
| 6917 | when = ((struct lysc_node_container *)node)->when; |
| 6918 | musts = ((struct lysc_node_container *)node)->musts; |
| 6919 | break; |
| 6920 | case LYS_CHOICE: |
| 6921 | when = ((struct lysc_node_choice *)node)->when; |
| 6922 | break; |
| 6923 | case LYS_LEAF: |
| 6924 | when = ((struct lysc_node_leaf *)node)->when; |
| 6925 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 6926 | break; |
| 6927 | case LYS_LEAFLIST: |
| 6928 | when = ((struct lysc_node_leaflist *)node)->when; |
| 6929 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 6930 | break; |
| 6931 | case LYS_LIST: |
| 6932 | when = ((struct lysc_node_list *)node)->when; |
| 6933 | musts = ((struct lysc_node_list *)node)->musts; |
| 6934 | break; |
| 6935 | case LYS_ANYXML: |
| 6936 | case LYS_ANYDATA: |
| 6937 | when = ((struct lysc_node_anydata *)node)->when; |
| 6938 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 6939 | break; |
| 6940 | case LYS_CASE: |
| 6941 | when = ((struct lysc_node_case *)node)->when; |
| 6942 | break; |
| 6943 | case LYS_NOTIF: |
| 6944 | musts = ((struct lysc_notif *)node)->musts; |
| 6945 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6946 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6947 | case LYS_ACTION: |
| 6948 | /* first process input musts */ |
| 6949 | musts = ((struct lysc_action *)node)->input.musts; |
| 6950 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6951 | default: |
| 6952 | /* nothing to check */ |
| 6953 | break; |
| 6954 | } |
| 6955 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6956 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6957 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6958 | 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] | 6959 | 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] | 6960 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6961 | 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] | 6962 | goto cleanup; |
| 6963 | } |
| 6964 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6965 | ctx->path[0] = '\0'; |
| 6966 | 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] | 6967 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6968 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6969 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 6970 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6971 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6972 | /* 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] | 6973 | 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] | 6974 | schema->name); |
| 6975 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6976 | |
| 6977 | /* check dummy node accessing */ |
| 6978 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6979 | 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] | 6980 | ret = LY_EVALID; |
| 6981 | goto cleanup; |
| 6982 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6983 | } |
| 6984 | } |
| 6985 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6986 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6987 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6988 | LY_CHECK_GOTO(ret, cleanup); |
| 6989 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6990 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6991 | } |
| 6992 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6993 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6994 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6995 | LY_ARRAY_FOR(musts, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6996 | 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] | 6997 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6998 | 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] | 6999 | goto cleanup; |
| 7000 | } |
| 7001 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 7002 | ctx->path[0] = '\0'; |
| 7003 | 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] | 7004 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7005 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7006 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7007 | /* 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] | 7008 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 7009 | 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] | 7010 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7011 | } |
| 7012 | } |
| 7013 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7014 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7015 | } |
| 7016 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 7017 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 7018 | /* now check output musts */ |
| 7019 | input_done = 1; |
| 7020 | musts = ((struct lysc_action *)node)->output.musts; |
| 7021 | opts = LYXP_SCNODE_OUTPUT; |
| 7022 | goto check_musts; |
| 7023 | } |
| 7024 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7025 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7026 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 7027 | return ret; |
| 7028 | } |
| 7029 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7030 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7031 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 7032 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7033 | const struct lysc_node *target = NULL, *siter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7034 | struct ly_path *p; |
| 7035 | struct lysc_type *type; |
| 7036 | |
| 7037 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7038 | |
| 7039 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 7040 | LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, |
| 7041 | 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] | 7042 | LY_PREF_SCHEMA, lref->path_context, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7043 | |
| 7044 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7045 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7046 | ly_path_free(node->module->ctx, p); |
| 7047 | |
| 7048 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 7049 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7050 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 7051 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
| 7052 | return LY_EVALID; |
| 7053 | } |
| 7054 | |
| 7055 | /* check status */ |
| 7056 | ctx->path[0] = '\0'; |
| 7057 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7058 | ctx->path_len = strlen(ctx->path); |
| 7059 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 7060 | return LY_EVALID; |
| 7061 | } |
| 7062 | ctx->path_len = 1; |
| 7063 | ctx->path[1] = '\0'; |
| 7064 | |
| 7065 | /* check config */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7066 | if (lref->require_instance) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 7067 | 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] | 7068 | if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7069 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
| 7070 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
| 7071 | return LY_EVALID; |
| 7072 | } |
| 7073 | } |
| 7074 | |
| 7075 | /* store the target's type and check for circular chain of leafrefs */ |
| 7076 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 7077 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 7078 | if (type == (struct lysc_type *)lref) { |
| 7079 | /* circular chain detected */ |
| 7080 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7081 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
| 7082 | return LY_EVALID; |
| 7083 | } |
| 7084 | } |
| 7085 | |
| 7086 | /* check if leafref and its target are under common if-features */ |
| 7087 | if (lys_compile_leafref_features_validate(node, target)) { |
| 7088 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7089 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 7090 | " features applicable to the leafref itself.", lref->path->expr); |
| 7091 | return LY_EVALID; |
| 7092 | } |
| 7093 | |
| 7094 | return LY_SUCCESS; |
| 7095 | } |
| 7096 | |
| 7097 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7098 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7099 | { |
| 7100 | struct lysc_ext_instance *ext; |
| 7101 | struct lysp_ext_instance *ext_p = NULL; |
| 7102 | struct lysp_stmt *stmt; |
| 7103 | const struct lys_module *ext_mod; |
| 7104 | LY_ERR ret = LY_SUCCESS; |
| 7105 | |
| 7106 | /* create the parsed extension instance manually */ |
| 7107 | ext_p = calloc(1, sizeof *ext_p); |
| 7108 | 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^] | 7109 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup); |
| 7110 | 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] | 7111 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7112 | ext_p->insubstmt_index = 0; |
| 7113 | |
| 7114 | stmt = calloc(1, sizeof *ext_p->child); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 7115 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup); |
| 7116 | 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] | 7117 | stmt->kw = LY_STMT_TYPE; |
| 7118 | ext_p->child = stmt; |
| 7119 | |
| 7120 | /* allocate new extension instance */ |
| 7121 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7122 | |
| 7123 | /* manually get extension definition module */ |
| 7124 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7125 | |
| 7126 | /* compile the extension instance */ |
| 7127 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7128 | |
| 7129 | cleanup: |
| 7130 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7131 | free(ext_p); |
| 7132 | return ret; |
| 7133 | } |
| 7134 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7135 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7136 | 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] | 7137 | const struct lys_module *dflt_mod, struct lyd_value *storage) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7138 | { |
| 7139 | LY_ERR ret; |
| 7140 | struct ly_err_item *err = NULL; |
| 7141 | |
| 7142 | ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 7143 | LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err); |
| 7144 | if (err) { |
| 7145 | ly_err_print(err); |
| 7146 | ctx->path[0] = '\0'; |
| 7147 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7148 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7149 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7150 | ly_err_free(err); |
| 7151 | } |
| 7152 | if (!ret) { |
| 7153 | ++storage->realtype->refcount; |
| 7154 | return LY_SUCCESS; |
| 7155 | } |
| 7156 | return ret; |
| 7157 | } |
| 7158 | |
| 7159 | static LY_ERR |
| 7160 | 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] | 7161 | const struct lys_module *dflt_mod) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7162 | { |
| 7163 | LY_ERR ret; |
| 7164 | |
| 7165 | assert(!leaf->dflt); |
| 7166 | |
| 7167 | if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) { |
| 7168 | /* ignore default values for keys and mandatory leaves */ |
| 7169 | return LY_SUCCESS; |
| 7170 | } |
| 7171 | |
| 7172 | /* allocate the default value */ |
| 7173 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 7174 | LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM); |
| 7175 | |
| 7176 | /* store the default value */ |
| 7177 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt); |
| 7178 | if (ret) { |
| 7179 | free(leaf->dflt); |
| 7180 | leaf->dflt = NULL; |
| 7181 | } |
| 7182 | |
| 7183 | return ret; |
| 7184 | } |
| 7185 | |
| 7186 | static LY_ERR |
| 7187 | 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] | 7188 | const struct lys_module *dflt_mod) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7189 | { |
| 7190 | LY_ERR ret; |
| 7191 | LY_ARRAY_COUNT_TYPE orig_count, u, v; |
| 7192 | |
| 7193 | assert(dflt || dflts); |
| 7194 | |
| 7195 | if (llist->dflts) { |
| 7196 | /* there were already some defaults and we are adding new by deviations */ |
| 7197 | assert(dflts); |
| 7198 | orig_count = LY_ARRAY_COUNT(llist->dflts); |
| 7199 | } else { |
| 7200 | orig_count = 0; |
| 7201 | } |
| 7202 | |
| 7203 | /* allocate new items */ |
| 7204 | if (dflts) { |
| 7205 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM); |
| 7206 | } else { |
| 7207 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM); |
| 7208 | } |
| 7209 | |
| 7210 | /* fill each new default value */ |
| 7211 | if (dflts) { |
| 7212 | LY_ARRAY_FOR(dflts, u) { |
| 7213 | llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts); |
| 7214 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod, |
| 7215 | llist->dflts[orig_count + u]); |
| 7216 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret); |
| 7217 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7218 | } |
| 7219 | } else { |
| 7220 | llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts); |
| 7221 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]); |
| 7222 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret); |
| 7223 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7224 | } |
| 7225 | |
| 7226 | /* check default value uniqueness */ |
| 7227 | if (llist->flags & LYS_CONFIG_W) { |
| 7228 | /* configuration data values must be unique - so check the default values */ |
| 7229 | for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) { |
| 7230 | for (v = 0; v < u; ++v) { |
| 7231 | 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] | 7232 | ly_bool dynamic = 0; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7233 | const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic); |
| 7234 | |
| 7235 | lysc_update_path(ctx, llist->parent, llist->name); |
| 7236 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7237 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 7238 | lysc_update_path(ctx, NULL, NULL); |
| 7239 | if (dynamic) { |
| 7240 | free((char *)val); |
| 7241 | } |
| 7242 | return LY_EVALID; |
| 7243 | } |
| 7244 | } |
| 7245 | } |
| 7246 | } |
| 7247 | |
| 7248 | return LY_SUCCESS; |
| 7249 | } |
| 7250 | |
| 7251 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7252 | lys_compile_unres(struct lysc_ctx *ctx) |
| 7253 | { |
| 7254 | struct lysc_node *node; |
| 7255 | struct lysc_type *type, *typeiter; |
| 7256 | struct lysc_type_leafref *lref; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7257 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7258 | uint32_t i; |
| 7259 | |
| 7260 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7261 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7262 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 7263 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7264 | node = ctx->leafrefs.objs[i]; |
| 7265 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7266 | type = ((struct lysc_node_leaf *)node)->type; |
| 7267 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7268 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 7269 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7270 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7271 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7272 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 7273 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 7274 | } |
| 7275 | } |
| 7276 | } |
| 7277 | } |
| 7278 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7279 | /* store pointer to the real type */ |
| 7280 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 7281 | if (type->basetype == LY_TYPE_LEAFREF) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7282 | for (typeiter = ((struct lysc_type_leafref *)type)->realtype; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7283 | typeiter->basetype == LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7284 | typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {} |
| 7285 | ((struct lysc_type_leafref *)type)->realtype = typeiter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7286 | } else if (type->basetype == LY_TYPE_UNION) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7287 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7288 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7289 | 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] | 7290 | typeiter->basetype == LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7291 | typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {} |
| 7292 | ((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] | 7293 | } |
| 7294 | } |
| 7295 | } |
| 7296 | } |
| 7297 | |
| 7298 | /* check xpath */ |
| 7299 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 7300 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 7301 | } |
| 7302 | |
| 7303 | /* finish incomplete default values compilation */ |
| 7304 | for (i = 0; i < ctx->dflts.count; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7305 | struct lysc_incomplete_dflt *r = ctx->dflts.objs[i]; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7306 | if (r->leaf->nodetype == LYS_LEAF) { |
| 7307 | LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod)); |
| 7308 | } else { |
| 7309 | 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] | 7310 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7311 | } |
| 7312 | |
| 7313 | return LY_SUCCESS; |
| 7314 | } |
| 7315 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7316 | LY_ERR |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7317 | lys_compile(struct lys_module *mod, uint32_t options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7318 | { |
| 7319 | struct lysc_ctx ctx = {0}; |
| 7320 | struct lysc_module *mod_c; |
| 7321 | struct lysp_module *sp; |
| 7322 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7323 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7324 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7325 | struct lys_module *m; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7326 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7327 | uint32_t i; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7328 | uint16_t compile_id; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7329 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7330 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7331 | 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] | 7332 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7333 | if (!mod->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7334 | /* just imported modules are not compiled */ |
| 7335 | return LY_SUCCESS; |
| 7336 | } |
| 7337 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7338 | compile_id = ++mod->ctx->module_set_id; |
| 7339 | sp = mod->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7340 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7341 | ctx.ctx = mod->ctx; |
| 7342 | ctx.mod = mod; |
| 7343 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7344 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7345 | ctx.path_len = 1; |
| 7346 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7347 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7348 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 7349 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 7350 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7351 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7352 | LY_ARRAY_FOR(sp->imports, u) { |
| 7353 | LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error); |
| 7354 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7355 | LY_ARRAY_FOR(sp->includes, u) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7356 | LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7357 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7358 | |
| 7359 | /* features */ |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7360 | if (mod->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7361 | /* there is already precompiled array of features */ |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7362 | mod_c->features = mod->dis_features; |
| 7363 | mod->dis_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7364 | } else { |
| 7365 | /* features are compiled directly into the compiled module structure, |
| 7366 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7367 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7368 | LY_CHECK_GOTO(ret, error); |
| 7369 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7370 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7371 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7372 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7373 | * are present. */ |
| 7374 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7375 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7376 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7377 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7378 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7379 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7380 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7381 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7382 | ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7383 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7384 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7385 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7386 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7387 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7388 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7389 | /* identities, work similarly to features with the precompilation */ |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7390 | if (mod->dis_identities) { |
| 7391 | mod_c->identities = mod->dis_identities; |
| 7392 | mod->dis_identities = NULL; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7393 | } else { |
| 7394 | ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities); |
| 7395 | LY_CHECK_GOTO(ret, error); |
| 7396 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7397 | if (sp->identities) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7398 | LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7399 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7400 | lysc_update_path(&ctx, NULL, "{submodule}"); |
| 7401 | LY_ARRAY_FOR(sp->includes, v) { |
| 7402 | if (sp->includes[v].submodule->identities) { |
| 7403 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
| 7404 | ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities); |
| 7405 | LY_CHECK_GOTO(ret, error); |
| 7406 | lysc_update_path(&ctx, NULL, NULL); |
| 7407 | } |
| 7408 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7409 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7410 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7411 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7412 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7413 | 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] | 7414 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7415 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7416 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7417 | 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] | 7418 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7419 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7420 | 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] | 7421 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7422 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7423 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7424 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7425 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7426 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7427 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7428 | /* extension instances TODO cover extension instances from submodules */ |
| 7429 | 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] | 7430 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7431 | /* finish compilation for all unresolved items in the context */ |
| 7432 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7433 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7434 | /* validate non-instantiated groupings from the parsed schema, |
| 7435 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7436 | ctx.options |= LYSC_OPT_GROUPING; |
| 7437 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7438 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7439 | 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] | 7440 | } |
| 7441 | } |
| 7442 | LY_LIST_FOR(sp->data, node_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7443 | grps = (struct lysp_grp *)lysp_node_groupings(node_p); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7444 | LY_ARRAY_FOR(grps, u) { |
| 7445 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7446 | 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] | 7447 | } |
| 7448 | } |
| 7449 | } |
| 7450 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7451 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7452 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7453 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7454 | } |
| 7455 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7456 | #if 0 |
| 7457 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7458 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7459 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7460 | * the anotation definitions available in the internal schema structure. */ |
| 7461 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7462 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7463 | lys_free(mod, NULL, 1, 1); |
| 7464 | return NULL; |
| 7465 | } |
| 7466 | } |
| 7467 | #endif |
| 7468 | |
| 7469 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7470 | if (!strcmp(mod->name, "ietf-netconf-with-defaults")) { |
| 7471 | 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] | 7472 | } |
| 7473 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7474 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7475 | ly_set_erase(&ctx.xpath, NULL); |
| 7476 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7477 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7478 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7479 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7480 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7481 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7482 | lysp_module_free(mod->parsed); |
| 7483 | mod->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7484 | } |
| 7485 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7486 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7487 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7488 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7489 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7490 | if (m->implemented > 1) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7491 | m->implemented = 1; |
| 7492 | } |
| 7493 | } |
| 7494 | } |
| 7495 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7496 | return LY_SUCCESS; |
| 7497 | |
| 7498 | error: |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7499 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7500 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7501 | ly_set_erase(&ctx.xpath, NULL); |
| 7502 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7503 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7504 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7505 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7506 | lysc_module_free(mod_c, NULL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7507 | mod->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7508 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7509 | /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency |
| 7510 | * of this module, since this module can be also compiled from dependency, there can be some other modules being |
| 7511 | * 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] | 7512 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7513 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7514 | if ((m->implemented >= compile_id) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7515 | /* revert features list to the precompiled state */ |
| 7516 | lys_feature_precompile_revert(&ctx, m); |
| 7517 | /* mark module as imported-only / not-implemented */ |
| 7518 | m->implemented = 0; |
| 7519 | /* free the compiled version of the module */ |
| 7520 | lysc_module_free(m->compiled, NULL); |
| 7521 | m->compiled = NULL; |
| 7522 | } |
| 7523 | } |
| 7524 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7525 | return ret; |
| 7526 | } |