Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema tree implementation |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 16 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | #include <stdint.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 21 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 24 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 26 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | #include "dict.h" |
| 29 | #include "log.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 30 | #include "path.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 31 | #include "parser.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 32 | #include "parser_schema.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 33 | #include "plugins_exts.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | #include "plugins_types.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 35 | #include "plugins_exts_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "set.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 37 | #include "tree.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 38 | #include "tree_data.h" |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 39 | #include "tree_data_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 40 | #include "tree_schema.h" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 41 | #include "tree_schema_internal.h" |
| 42 | #include "xpath.h" |
| 43 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 44 | static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 45 | void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod); |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 46 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 47 | /** |
| 48 | * @brief Duplicate string into dictionary |
| 49 | * @param[in] CTX libyang context of the dictionary. |
| 50 | * @param[in] ORIG String to duplicate. |
| 51 | * @param[out] DUP Where to store the result. |
| 52 | */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 53 | #define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);} |
| 54 | |
| 55 | #define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);} |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 56 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 57 | #define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 58 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 59 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 60 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 61 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 62 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 63 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 64 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 65 | } \ |
| 66 | } |
| 67 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 68 | #define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 69 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 70 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 71 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 72 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 73 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 74 | RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 75 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 76 | } \ |
| 77 | } |
| 78 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 79 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \ |
| 80 | if (EXTS_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 81 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \ |
| 82 | for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 83 | LY_ARRAY_INCREMENT(EXT_C); \ |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 84 | RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 85 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 86 | } \ |
| 87 | } |
| 88 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 89 | #define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 90 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 91 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 92 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 93 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 94 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 95 | RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 96 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 97 | } \ |
| 98 | } |
| 99 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 100 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 101 | if (MEMBER_P) { \ |
| 102 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 103 | LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 104 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 105 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 106 | } |
| 107 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 108 | #define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 109 | if (MEMBER_P) { \ |
| 110 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 111 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 112 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 113 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 114 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 115 | } |
| 116 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 117 | #define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 118 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 119 | for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 120 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 121 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 122 | return LY_EVALID; \ |
| 123 | } \ |
| 124 | } \ |
| 125 | } |
| 126 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 127 | #define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 128 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 129 | for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \ |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 130 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \ |
| 131 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 132 | return LY_EVALID; \ |
| 133 | } \ |
| 134 | } \ |
| 135 | } |
| 136 | |
| 137 | struct lysc_ext * |
| 138 | lysc_ext_dup(struct lysc_ext *orig) |
| 139 | { |
| 140 | ++orig->refcount; |
| 141 | return orig; |
| 142 | } |
| 143 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 144 | static struct lysc_ext_instance * |
| 145 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 146 | { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 147 | /* TODO - extensions, increase refcount */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 148 | (void) ctx; |
| 149 | (void) orig; |
| 150 | return NULL; |
| 151 | } |
| 152 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 153 | static LY_ERR |
| 154 | lysc_incomplete_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 155 | struct lys_module *dflt_mod) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 156 | { |
| 157 | struct lysc_incomplete_dflt *r; |
| 158 | uint32_t i; |
| 159 | |
| 160 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 161 | r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i]; |
| 162 | if (r->leaf == leaf) { |
| 163 | /* just replace the default */ |
| 164 | r->dflt = dflt; |
| 165 | return LY_SUCCESS; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | r = malloc(sizeof *r); |
| 170 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 171 | r->leaf = leaf; |
| 172 | r->dflt = dflt; |
| 173 | r->dflts = NULL; |
| 174 | r->dflt_mod = dflt_mod; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 175 | LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 176 | |
| 177 | return LY_SUCCESS; |
| 178 | } |
| 179 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 180 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 181 | * @brief Add record into the compile context's list of incomplete default values. |
| 182 | * @param[in] ctx Compile context with the incomplete default values list. |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 183 | * @param[in] term Term context node with the default value. |
| 184 | * @param[in] value String default value. |
| 185 | * @param[in] val_len Length of @p value. |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 186 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 187 | * @return LY_EMEM in case of memory allocation failure. |
| 188 | * @return LY_SUCCESS |
| 189 | */ |
| 190 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 191 | lysc_incomplete_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char **dflts, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 192 | struct lys_module *dflt_mod) |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 193 | { |
| 194 | struct lysc_incomplete_dflt *r; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 195 | uint32_t i; |
| 196 | |
| 197 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 198 | r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i]; |
| 199 | if (r->llist == llist) { |
| 200 | /* just replace the defaults */ |
| 201 | r->dflts = dflts; |
| 202 | return LY_SUCCESS; |
| 203 | } |
| 204 | } |
| 205 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 206 | r = malloc(sizeof *r); |
| 207 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 208 | r->llist = llist; |
| 209 | r->dflt = NULL; |
| 210 | r->dflts = dflts; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 211 | r->dflt_mod = dflt_mod; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 212 | LY_CHECK_RET(ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST, NULL)); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 213 | |
| 214 | return LY_SUCCESS; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 219 | * @param[in] ctx Compile context with the incomplete default values list. |
| 220 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 221 | */ |
| 222 | static void |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 223 | lysc_incomplete_dflt_remove(struct lysc_ctx *ctx, struct lysc_node *term) |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 224 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 225 | uint32_t u; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 226 | struct lysc_incomplete_dflt *r; |
| 227 | |
| 228 | for (u = 0; u < ctx->dflts.count; ++u) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 229 | r = ctx->dflts.objs[u]; |
| 230 | if (r->leaf == (struct lysc_node_leaf *)term) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 231 | free(ctx->dflts.objs[u]); |
| 232 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 233 | --ctx->dflts.count; |
| 234 | return; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 239 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 240 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 241 | { |
| 242 | int len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 243 | uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 244 | |
| 245 | if (!name) { |
| 246 | /* removing last path segment */ |
| 247 | if (ctx->path[ctx->path_len - 1] == '}') { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 248 | for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {} |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 249 | if (ctx->path[ctx->path_len] == '=') { |
| 250 | ctx->path[ctx->path_len++] = '}'; |
| 251 | } else { |
| 252 | /* not a top-level special tag, remove also preceiding '/' */ |
| 253 | goto remove_nodelevel; |
| 254 | } |
| 255 | } else { |
| 256 | remove_nodelevel: |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 257 | for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {} |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 258 | if (ctx->path_len == 0) { |
| 259 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 260 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | /* set new terminating NULL-byte */ |
| 264 | ctx->path[ctx->path_len] = '\0'; |
| 265 | } else { |
| 266 | if (ctx->path_len > 1) { |
| 267 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 268 | /* extension of the special tag */ |
| 269 | nextlevel = 2; |
| 270 | --ctx->path_len; |
| 271 | } else { |
| 272 | /* there is already some path, so add next level */ |
| 273 | nextlevel = 1; |
| 274 | } |
| 275 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 276 | |
| 277 | if (nextlevel != 2) { |
| 278 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 279 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 280 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 281 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 282 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 283 | } |
| 284 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 285 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 286 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 287 | if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) { |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 288 | /* output truncated */ |
| 289 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 290 | } else { |
| 291 | ctx->path_len += len; |
| 292 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 297 | * @brief Duplicate the compiled pattern structure. |
| 298 | * |
| 299 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 300 | * |
| 301 | * @param[in] orig The pattern structure to duplicate. |
| 302 | * @return The duplicated structure to use. |
| 303 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 304 | static struct lysc_pattern * |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 305 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 306 | { |
| 307 | ++orig->refcount; |
| 308 | return orig; |
| 309 | } |
| 310 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 311 | /** |
| 312 | * @brief Duplicate the array of compiled patterns. |
| 313 | * |
| 314 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 315 | * |
| 316 | * @param[in] ctx Libyang context for logging. |
| 317 | * @param[in] orig The patterns sized array to duplicate. |
| 318 | * @return New sized array as a copy of @p orig. |
| 319 | * @return NULL in case of memory allocation error. |
| 320 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 321 | static struct lysc_pattern ** |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 322 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 323 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 324 | struct lysc_pattern **dup = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 325 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 326 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 327 | assert(orig); |
| 328 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 329 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 330 | LY_ARRAY_FOR(orig, u) { |
| 331 | dup[u] = lysc_pattern_dup(orig[u]); |
| 332 | LY_ARRAY_INCREMENT(dup); |
| 333 | } |
| 334 | return dup; |
| 335 | } |
| 336 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 337 | /** |
| 338 | * @brief Duplicate compiled range structure. |
| 339 | * |
| 340 | * @param[in] ctx Libyang context for logging. |
| 341 | * @param[in] orig The range structure to be duplicated. |
| 342 | * @return New compiled range structure as a copy of @p orig. |
| 343 | * @return NULL in case of memory allocation error. |
| 344 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 345 | struct lysc_range * |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 346 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 347 | { |
| 348 | struct lysc_range *dup; |
| 349 | LY_ERR ret; |
| 350 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 351 | assert(orig); |
| 352 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 353 | dup = calloc(1, sizeof *dup); |
| 354 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 355 | if (orig->parts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 356 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup); |
| 357 | LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts); |
| 358 | memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 359 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 360 | DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup); |
| 361 | DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 362 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 363 | |
| 364 | return dup; |
| 365 | cleanup: |
| 366 | free(dup); |
| 367 | (void) ret; /* set but not used due to the return type */ |
| 368 | return NULL; |
| 369 | } |
| 370 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 371 | /** |
| 372 | * @brief Stack for processing if-feature expressions. |
| 373 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 374 | struct iff_stack { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 375 | size_t size; /**< number of items in the stack */ |
| 376 | size_t index; /**< first empty item */ |
| 377 | uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 378 | }; |
| 379 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 380 | /** |
| 381 | * @brief Add @ref ifftokens into the stack. |
| 382 | * @param[in] stack The if-feature stack to use. |
| 383 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 384 | * @return LY_EMEM in case of memory allocation error |
| 385 | * @return LY_ESUCCESS if the value successfully stored. |
| 386 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 387 | static LY_ERR |
| 388 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 389 | { |
| 390 | if (stack->index == stack->size) { |
| 391 | stack->size += 4; |
| 392 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 393 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 394 | } |
| 395 | stack->stack[stack->index++] = value; |
| 396 | return LY_SUCCESS; |
| 397 | } |
| 398 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 399 | /** |
| 400 | * @brief Get (and remove) the last item form the stack. |
| 401 | * @param[in] stack The if-feature stack to use. |
| 402 | * @return The value from the top of the stack. |
| 403 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 404 | static uint8_t |
| 405 | iff_stack_pop(struct iff_stack *stack) |
| 406 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 407 | assert(stack && stack->index); |
| 408 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 409 | stack->index--; |
| 410 | return stack->stack[stack->index]; |
| 411 | } |
| 412 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 413 | /** |
| 414 | * @brief Clean up the stack. |
| 415 | * @param[in] stack The if-feature stack to use. |
| 416 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 417 | static void |
| 418 | iff_stack_clean(struct iff_stack *stack) |
| 419 | { |
| 420 | stack->size = 0; |
| 421 | free(stack->stack); |
| 422 | } |
| 423 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 424 | /** |
| 425 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 426 | * (libyang format of the if-feature expression). |
| 427 | * @param[in,out] list The 2bits array to modify. |
| 428 | * @param[in] op The operand (@ref ifftokens) to store. |
| 429 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 430 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 431 | static void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 432 | iff_setop(uint8_t *list, uint8_t op, size_t pos) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 433 | { |
| 434 | uint8_t *item; |
| 435 | uint8_t mask = 3; |
| 436 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 437 | assert(op <= 3); /* max 2 bits */ |
| 438 | |
| 439 | item = &list[pos / 4]; |
| 440 | mask = mask << 2 * (pos % 4); |
| 441 | *item = (*item) & ~mask; |
| 442 | *item = (*item) | (op << 2 * (pos % 4)); |
| 443 | } |
| 444 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 445 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 446 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 447 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 448 | /** |
| 449 | * @brief Find a feature of the given name and referenced in the given module. |
| 450 | * |
| 451 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 452 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 453 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 454 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 455 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 456 | * assigned till that time will be still valid. |
| 457 | * |
| 458 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 459 | * @param[in] name Name of the feature including possible prefix. |
| 460 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 461 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 462 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 463 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 464 | lys_feature_find(struct lys_module *mod, const char *name, size_t len) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 465 | { |
| 466 | size_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 467 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 468 | struct lysc_feature *f; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 469 | |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 470 | assert(mod); |
| 471 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 472 | for (i = 0; i < len; ++i) { |
| 473 | if (name[i] == ':') { |
| 474 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 475 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 476 | LY_CHECK_RET(!mod, NULL); |
| 477 | |
| 478 | name = &name[i + 1]; |
| 479 | len = len - i - 1; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /* we have the correct module, get the feature */ |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 484 | LY_ARRAY_FOR(mod->features, u) { |
| 485 | f = &mod->features[u]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 486 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 487 | return f; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | return NULL; |
| 492 | } |
| 493 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 494 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 495 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 496 | */ |
| 497 | static LY_ERR |
| 498 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 499 | { |
| 500 | LY_ERR ret = LY_SUCCESS; |
| 501 | |
| 502 | if (!ext_p->compiled) { |
| 503 | lysc_update_path(ctx, NULL, "{extension}"); |
| 504 | lysc_update_path(ctx, NULL, ext_p->name); |
| 505 | |
| 506 | /* compile the extension definition */ |
| 507 | ext_p->compiled = calloc(1, sizeof **ext); |
| 508 | ext_p->compiled->refcount = 1; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 509 | DUP_STRING_GOTO(ctx->ctx, ext_p->name, ext_p->compiled->name, ret, done); |
| 510 | DUP_STRING_GOTO(ctx->ctx, ext_p->argument, ext_p->compiled->argument, ret, done); |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 511 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 512 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 513 | |
| 514 | lysc_update_path(ctx, NULL, NULL); |
| 515 | lysc_update_path(ctx, NULL, NULL); |
| 516 | |
| 517 | /* find extension definition plugin */ |
| 518 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 519 | } |
| 520 | |
| 521 | *ext = lysc_ext_dup(ext_p->compiled); |
| 522 | |
| 523 | done: |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 528 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 529 | * |
| 530 | * @param[in] ctx Compilation context. |
| 531 | * @param[in] ext_p Parsed extension instance. |
| 532 | * @param[in,out] ext Prepared compiled extension instance. |
| 533 | * @param[in] parent Extension instance parent. |
| 534 | * @param[in] parent_type Extension instance parent type. |
| 535 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 536 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 537 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 538 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 539 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 540 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 541 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 542 | const char *name; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 543 | size_t u; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 544 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 545 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 546 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 547 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument, ret); |
| 548 | LY_CHECK_RET(ret); |
| 549 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 550 | ext->insubstmt = ext_p->insubstmt; |
| 551 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 552 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 553 | ext->parent = parent; |
| 554 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 555 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 556 | lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node *)ext->parent : NULL, "{extension}"); |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 557 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 558 | /* get module where the extension definition should be placed */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 559 | for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u) {} |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 560 | if (ext_p->yin) { |
| 561 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 562 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 563 | * YANG (import) prefix must be done here. */ |
| 564 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 565 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, &ext_p->name[u], 0, &prefixed_name), cleanup); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 566 | u = 0; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 567 | } else { |
| 568 | assert(ctx->mod_def->parsed); |
| 569 | LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) { |
| 570 | if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 571 | char *s; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 572 | LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->parsed->imports[v].prefix, &ext_p->name[u]) == -1, |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 573 | ret = LY_EMEM, cleanup); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 574 | LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->ctx, s, &prefixed_name), cleanup); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 575 | u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 576 | break; |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | if (!prefixed_name) { |
| 581 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 582 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 583 | ret = LY_EVALID; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 584 | goto cleanup; |
| 585 | } |
| 586 | } else { |
| 587 | prefixed_name = ext_p->name; |
| 588 | } |
| 589 | lysc_update_path(ctx, NULL, prefixed_name); |
| 590 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 591 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 592 | ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 593 | if (!ext_mod) { |
| 594 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 595 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 596 | ret = LY_EVALID; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 597 | goto cleanup; |
| 598 | } else if (!ext_mod->parsed->extensions) { |
| 599 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 600 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 601 | prefixed_name, ext_mod->name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 602 | ret = LY_EVALID; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 603 | goto cleanup; |
| 604 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 605 | } |
| 606 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 607 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 608 | /* find the parsed extension definition there */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 609 | LY_ARRAY_FOR(ext_mod->parsed->extensions, v) { |
| 610 | if (!strcmp(name, ext_mod->parsed->extensions[v].name)) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 611 | /* compile extension definition and assign it */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 612 | LY_CHECK_GOTO(ret = lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 613 | break; |
| 614 | } |
| 615 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 616 | if (!ext->def) { |
| 617 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 618 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 619 | ret = LY_EVALID; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 620 | goto cleanup; |
| 621 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 622 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 623 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 624 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 625 | * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 626 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 627 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 628 | struct lysp_stmt *stmt = NULL; |
| 629 | |
| 630 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 631 | /* ... argument was the first XML child element */ |
| 632 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 633 | /* TODO check namespace of the statement */ |
| 634 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 635 | stmt = ext_p->child; |
| 636 | } |
| 637 | } |
| 638 | } else { |
| 639 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 640 | * with LYS_YIN_ATTR flag */ |
| 641 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 642 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 643 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 644 | break; |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | if (!stmt) { |
| 649 | /* missing extension's argument */ |
| 650 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 651 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 652 | ret = LY_EVALID; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 653 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 654 | |
| 655 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 656 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, stmt->arg, 0, &ext->argument), cleanup); |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 657 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 658 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 659 | if (prefixed_name != ext_p->name) { |
| 660 | lydict_remove(ctx->ctx, ext_p->name); |
| 661 | ext_p->name = prefixed_name; |
| 662 | if (!ext_p->argument && ext->argument) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 663 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, ext->argument, 0, &ext_p->argument), cleanup); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 664 | } |
| 665 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 666 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 667 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 668 | if (ext->argument) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 669 | lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 670 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 671 | LY_CHECK_GOTO(ret = ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 672 | if (ext->argument) { |
| 673 | lysc_update_path(ctx, NULL, NULL); |
| 674 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 675 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 676 | ext_p->compiled = ext; |
| 677 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 678 | cleanup: |
| 679 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 680 | lydict_remove(ctx->ctx, prefixed_name); |
| 681 | } |
| 682 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 683 | lysc_update_path(ctx, NULL, NULL); |
| 684 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 685 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 686 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 690 | * @brief Compile information from the if-feature statement |
| 691 | * @param[in] ctx Compile context. |
| 692 | * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 693 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 694 | * @return LY_ERR value. |
| 695 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 696 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 697 | lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 698 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 699 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 700 | const char *c = *value; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 701 | int64_t i, j; |
| 702 | int8_t op_len, last_not = 0, checkversion = 0; |
| 703 | LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 704 | uint8_t op; |
| 705 | struct iff_stack stack = {0, 0, NULL}; |
| 706 | struct lysc_feature *f; |
| 707 | |
| 708 | assert(c); |
| 709 | |
| 710 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 711 | for (i = j = 0; c[i]; i++) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 712 | if (c[i] == '(') { |
| 713 | j++; |
| 714 | checkversion = 1; |
| 715 | continue; |
| 716 | } else if (c[i] == ')') { |
| 717 | j--; |
| 718 | continue; |
| 719 | } else if (isspace(c[i])) { |
| 720 | checkversion = 1; |
| 721 | continue; |
| 722 | } |
| 723 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 724 | if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) { |
| 725 | uint64_t spaces; |
| 726 | for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++); |
| 727 | if (c[i + op_len + spaces] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 728 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 729 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 730 | return LY_EVALID; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 731 | } else if (!isspace(c[i + op_len])) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 732 | /* feature name starting with the not/and/or */ |
| 733 | last_not = 0; |
| 734 | f_size++; |
| 735 | } else if (c[i] == 'n') { /* not operation */ |
| 736 | if (last_not) { |
| 737 | /* double not */ |
| 738 | expr_size = expr_size - 2; |
| 739 | last_not = 0; |
| 740 | } else { |
| 741 | last_not = 1; |
| 742 | } |
| 743 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 744 | if (f_exp != f_size) { |
| 745 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 746 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, op_len, &c[i]); |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 747 | return LY_EVALID; |
| 748 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 749 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 750 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 751 | /* not a not operation */ |
| 752 | last_not = 0; |
| 753 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 754 | i += op_len; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 755 | } else { |
| 756 | f_size++; |
| 757 | last_not = 0; |
| 758 | } |
| 759 | expr_size++; |
| 760 | |
| 761 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 762 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 763 | i--; |
| 764 | break; |
| 765 | } |
| 766 | i++; |
| 767 | } |
| 768 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 769 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 770 | /* not matching count of ( and ) */ |
| 771 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 772 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 773 | return LY_EVALID; |
| 774 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 775 | if (f_exp != f_size) { |
| 776 | /* features do not match the needed arguments for the logical operations */ |
| 777 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 778 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 779 | "the required number of operands for the operations.", *value); |
| 780 | return LY_EVALID; |
| 781 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 782 | |
| 783 | if (checkversion || expr_size > 1) { |
| 784 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 785 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 786 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 787 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 788 | return LY_EVALID; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | /* allocate the memory */ |
| 793 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 794 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 795 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 796 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx); rc = LY_EMEM, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 797 | |
| 798 | stack.size = expr_size; |
| 799 | f_size--; expr_size--; /* used as indexes from now */ |
| 800 | |
| 801 | for (i--; i >= 0; i--) { |
| 802 | if (c[i] == ')') { |
| 803 | /* push it on stack */ |
| 804 | iff_stack_push(&stack, LYS_IFF_RP); |
| 805 | continue; |
| 806 | } else if (c[i] == '(') { |
| 807 | /* pop from the stack into result all operators until ) */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 808 | while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 809 | iff_setop(iff->expr, op, expr_size--); |
| 810 | } |
| 811 | continue; |
| 812 | } else if (isspace(c[i])) { |
| 813 | continue; |
| 814 | } |
| 815 | |
| 816 | /* end of operator or operand -> find beginning and get what is it */ |
| 817 | j = i + 1; |
| 818 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 819 | i--; |
| 820 | } |
| 821 | i++; /* go back by one step */ |
| 822 | |
| 823 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 824 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 825 | /* double not */ |
| 826 | iff_stack_pop(&stack); |
| 827 | } else { |
| 828 | /* not has the highest priority, so do not pop from the stack |
| 829 | * as in case of AND and OR */ |
| 830 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 831 | } |
| 832 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 833 | /* as for OR - pop from the stack all operators with the same or higher |
| 834 | * priority and store them to the result, then push the AND to the stack */ |
| 835 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 836 | op = iff_stack_pop(&stack); |
| 837 | iff_setop(iff->expr, op, expr_size--); |
| 838 | } |
| 839 | iff_stack_push(&stack, LYS_IFF_AND); |
| 840 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 841 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 842 | op = iff_stack_pop(&stack); |
| 843 | iff_setop(iff->expr, op, expr_size--); |
| 844 | } |
| 845 | iff_stack_push(&stack, LYS_IFF_OR); |
| 846 | } else { |
| 847 | /* feature name, length is j - i */ |
| 848 | |
| 849 | /* add it to the expression */ |
| 850 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 851 | |
| 852 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 853 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 854 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 855 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 856 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 857 | iff->features[f_size] = f; |
| 858 | LY_ARRAY_INCREMENT(iff->features); |
| 859 | f_size--; |
| 860 | } |
| 861 | } |
| 862 | while (stack.index) { |
| 863 | op = iff_stack_pop(&stack); |
| 864 | iff_setop(iff->expr, op, expr_size--); |
| 865 | } |
| 866 | |
| 867 | if (++expr_size || ++f_size) { |
| 868 | /* not all expected operators and operands found */ |
| 869 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 870 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 871 | rc = LY_EINT; |
| 872 | } else { |
| 873 | rc = LY_SUCCESS; |
| 874 | } |
| 875 | |
| 876 | error: |
| 877 | /* cleanup */ |
| 878 | iff_stack_clean(&stack); |
| 879 | |
| 880 | return rc; |
| 881 | } |
| 882 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 883 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 884 | * @brief Get the XPath context node for the given schema node. |
| 885 | * @param[in] start The schema node where the XPath expression appears. |
| 886 | * @return The context node to evaluate XPath expression in given schema node. |
| 887 | * @return NULL in case the context node is the root node. |
| 888 | */ |
| 889 | static struct lysc_node * |
| 890 | lysc_xpath_context(struct lysc_node *start) |
| 891 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 892 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 893 | start = start->parent) {} |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 894 | return start; |
| 895 | } |
| 896 | |
| 897 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 898 | * @brief Compile information from the when statement |
| 899 | * @param[in] ctx Compile context. |
| 900 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 901 | * @param[in] flags Flags of the node with the "when" defiition. |
| 902 | * @param[in] node Node that inherited the "when" definition, must be connected to parents. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 903 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 904 | * @return LY_ERR value. |
| 905 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 906 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 907 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 908 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 909 | LY_ERR ret = LY_SUCCESS; |
| 910 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 911 | *when = calloc(1, sizeof **when); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 912 | LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 913 | (*when)->refcount = 1; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 914 | LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond)); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 915 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 916 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 917 | DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done); |
| 918 | DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 919 | COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 920 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 921 | |
| 922 | done: |
| 923 | return ret; |
| 924 | } |
| 925 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 926 | /** |
| 927 | * @brief Compile information from the must statement |
| 928 | * @param[in] ctx Compile context. |
| 929 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 930 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 931 | * @return LY_ERR value. |
| 932 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 933 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 934 | lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 935 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 936 | LY_ERR ret = LY_SUCCESS; |
| 937 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 938 | LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1, &must->cond)); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 939 | must->module = ctx->mod_def; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 940 | DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done); |
| 941 | DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done); |
| 942 | DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done); |
| 943 | DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 944 | COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 945 | |
| 946 | done: |
| 947 | return ret; |
| 948 | } |
| 949 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 950 | /** |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 951 | * @brief Compile information in the import statement - make sure there is the target module |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 952 | * @param[in] ctx Compile context. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 953 | * @param[in] imp_p The parsed import statement structure to fill the module to. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 954 | * @return LY_ERR value. |
| 955 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 956 | static LY_ERR |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 957 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 958 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 959 | const struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 960 | LY_ERR ret = LY_SUCCESS; |
| 961 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 962 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 963 | * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added, |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 964 | * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 965 | if (!imp_p->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 966 | /* try to use filepath if present */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 967 | if (imp_p->module->filepath) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 968 | struct ly_in *in; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 969 | if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 970 | LOGINT(ctx->ctx); |
| 971 | } else { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 972 | LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)], |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 973 | ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 974 | if (mod != imp_p->module) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 975 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 976 | imp_p->module->filepath, imp_p->module->name); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 977 | mod = NULL; |
| 978 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 979 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 980 | ly_in_free(in, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 981 | } |
| 982 | if (!mod) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 983 | if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, (struct lys_module **)&mod)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 984 | LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.", |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 985 | imp_p->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 986 | return LY_ENOTFOUND; |
| 987 | } |
| 988 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 989 | } |
| 990 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 991 | return ret; |
| 992 | } |
| 993 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 994 | LY_ERR |
| 995 | lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 996 | struct lysp_ident *identities_p, struct lysc_ident **identities) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 997 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 998 | LY_ARRAY_COUNT_TYPE offset = 0, u, v; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 999 | struct lysc_ctx context = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1000 | LY_ERR ret = LY_SUCCESS; |
| 1001 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1002 | assert(ctx_sc || ctx); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1003 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1004 | if (!ctx_sc) { |
| 1005 | context.ctx = ctx; |
| 1006 | context.mod = module; |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 1007 | context.mod_def = module; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1008 | context.path_len = 1; |
| 1009 | context.path[0] = '/'; |
| 1010 | ctx_sc = &context; |
| 1011 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1012 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1013 | if (!identities_p) { |
| 1014 | return LY_SUCCESS; |
| 1015 | } |
| 1016 | if (*identities) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1017 | offset = LY_ARRAY_COUNT(*identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | lysc_update_path(ctx_sc, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1021 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1022 | LY_ARRAY_FOR(identities_p, u) { |
| 1023 | lysc_update_path(ctx_sc, NULL, identities_p[u].name); |
| 1024 | |
| 1025 | LY_ARRAY_INCREMENT(*identities); |
| 1026 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1027 | DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done); |
| 1028 | DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done); |
| 1029 | DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref, ret, done); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1030 | (*identities)[offset + u].module = ctx_sc->mod; |
| 1031 | COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v, |
| 1032 | lys_compile_iffeature, ret, done); |
| 1033 | /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */ |
| 1034 | COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u], |
| 1035 | LYEXT_PAR_IDENT, ret, done); |
| 1036 | (*identities)[offset + u].flags = identities_p[u].flags; |
| 1037 | |
| 1038 | lysc_update_path(ctx_sc, NULL, NULL); |
| 1039 | } |
| 1040 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1041 | done: |
| 1042 | return ret; |
| 1043 | } |
| 1044 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1045 | /** |
| 1046 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 1047 | * |
| 1048 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 1049 | * |
| 1050 | * @param[in] ctx Compile context for logging. |
| 1051 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 1052 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 1053 | * @return LY_SUCCESS if everything is ok. |
| 1054 | * @return LY_EVALID if the identity is derived from itself. |
| 1055 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1056 | static LY_ERR |
| 1057 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 1058 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1059 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1060 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1061 | struct ly_set recursion = {0}; |
| 1062 | struct lysc_ident *drv; |
| 1063 | |
| 1064 | if (!derived) { |
| 1065 | return LY_SUCCESS; |
| 1066 | } |
| 1067 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1068 | for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1069 | if (ident == derived[u]) { |
| 1070 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1071 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1072 | ret = LY_EVALID; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1073 | goto cleanup; |
| 1074 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1075 | ret = ly_set_add(&recursion, derived[u], 0, NULL); |
| 1076 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | for (v = 0; v < recursion.count; ++v) { |
| 1080 | drv = recursion.objs[v]; |
| 1081 | if (!drv->derived) { |
| 1082 | continue; |
| 1083 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1084 | for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1085 | if (ident == drv->derived[u]) { |
| 1086 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1087 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1088 | ret = LY_EVALID; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1089 | goto cleanup; |
| 1090 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1091 | ret = ly_set_add(&recursion, drv->derived[u], 0, NULL); |
| 1092 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1093 | } |
| 1094 | } |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1095 | |
| 1096 | cleanup: |
| 1097 | ly_set_erase(&recursion, NULL); |
| 1098 | return ret; |
| 1099 | } |
| 1100 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1101 | /** |
| 1102 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1103 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1104 | * For bases in identity set backlinks to them from the base identities. For identityref, store |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1105 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1106 | * to distinguish these two use cases. |
| 1107 | * |
| 1108 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1109 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1110 | * @param[in] ident Referencing identity to work with, NULL for identityref. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1111 | * @param[in] bases Array of bases of identityref to fill in. |
| 1112 | * @return LY_ERR value. |
| 1113 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1114 | static LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1115 | lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1116 | struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1117 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1118 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1119 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1120 | struct lys_module *mod; |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1121 | struct lysc_ident **idref; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1122 | |
| 1123 | assert(ident || bases); |
| 1124 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1125 | if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1126 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1127 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1128 | return LY_EVALID; |
| 1129 | } |
| 1130 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1131 | LY_ARRAY_FOR(bases_p, u) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1132 | s = strchr(bases_p[u], ':'); |
| 1133 | if (s) { |
| 1134 | /* prefixed identity */ |
| 1135 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1136 | mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1137 | } else { |
| 1138 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1139 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1140 | } |
| 1141 | if (!mod) { |
| 1142 | if (ident) { |
| 1143 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1144 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1145 | } else { |
| 1146 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1147 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1148 | } |
| 1149 | return LY_EVALID; |
| 1150 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1151 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1152 | idref = NULL; |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1153 | LY_ARRAY_FOR(mod->identities, v) { |
| 1154 | if (!strcmp(name, mod->identities[v].name)) { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1155 | if (ident) { |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1156 | if (ident == &mod->identities[v]) { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1157 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1158 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1159 | return LY_EVALID; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1160 | } |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1161 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived)); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1162 | /* we have match! store the backlink */ |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1163 | LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1164 | *idref = ident; |
| 1165 | } else { |
| 1166 | /* we have match! store the found identity */ |
| 1167 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 1168 | *idref = &mod->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1169 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1170 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | if (!idref || !(*idref)) { |
| 1174 | if (ident) { |
| 1175 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1176 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1177 | } else { |
| 1178 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1179 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1180 | } |
| 1181 | return LY_EVALID; |
| 1182 | } |
| 1183 | } |
| 1184 | return LY_SUCCESS; |
| 1185 | } |
| 1186 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1187 | /** |
| 1188 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1189 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1190 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1191 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1192 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1193 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1194 | static LY_ERR |
| 1195 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1196 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1197 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1198 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1199 | lysc_update_path(ctx, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1200 | for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1201 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1202 | continue; |
| 1203 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1204 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1205 | LY_CHECK_RET(lys_compile_identity_bases(ctx, idents[u].module, idents_p[u].bases, &idents[u], NULL)); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1206 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1207 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1208 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1209 | return LY_SUCCESS; |
| 1210 | } |
| 1211 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1212 | LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1213 | lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1214 | struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1215 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1216 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1217 | LY_ARRAY_COUNT_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1218 | struct lysc_ctx context = {0}; |
| 1219 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1220 | assert(ctx_sc || ctx); |
| 1221 | |
| 1222 | if (!ctx_sc) { |
| 1223 | context.ctx = ctx; |
| 1224 | context.mod = module; |
| 1225 | context.path_len = 1; |
| 1226 | context.path[0] = '/'; |
| 1227 | ctx_sc = &context; |
| 1228 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1229 | |
| 1230 | if (!features_p) { |
| 1231 | return LY_SUCCESS; |
| 1232 | } |
| 1233 | if (*features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1234 | offset = LY_ARRAY_COUNT(*features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1235 | } |
| 1236 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1237 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1238 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1239 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1240 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1241 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1242 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1243 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1244 | DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done); |
| 1245 | DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done); |
| 1246 | DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref, ret, done); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1247 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1248 | (*features)[offset + u].module = ctx_sc->mod; |
| 1249 | |
| 1250 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1251 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1252 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1253 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1254 | done: |
| 1255 | return ret; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1256 | } |
| 1257 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1258 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1259 | * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement). |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1260 | * |
| 1261 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1262 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1263 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1264 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1265 | * being currently processed). |
| 1266 | * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature) |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1267 | * @return LY_SUCCESS if everything is ok. |
| 1268 | * @return LY_EVALID if the feature references indirectly itself. |
| 1269 | */ |
| 1270 | static LY_ERR |
| 1271 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1272 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1273 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1274 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1275 | struct ly_set recursion = {0}; |
| 1276 | struct lysc_feature *drv; |
| 1277 | |
| 1278 | if (!depfeatures) { |
| 1279 | return LY_SUCCESS; |
| 1280 | } |
| 1281 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1282 | for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1283 | if (feature == depfeatures[u]) { |
| 1284 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1285 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1286 | ret = LY_EVALID; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1287 | goto cleanup; |
| 1288 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1289 | ret = ly_set_add(&recursion, depfeatures[u], 0, NULL); |
| 1290 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | for (v = 0; v < recursion.count; ++v) { |
| 1294 | drv = recursion.objs[v]; |
| 1295 | if (!drv->depfeatures) { |
| 1296 | continue; |
| 1297 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1298 | for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1299 | if (feature == drv->depfeatures[u]) { |
| 1300 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1301 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1302 | ret = LY_EVALID; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1303 | goto cleanup; |
| 1304 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1305 | ly_set_add(&recursion, drv->depfeatures[u], 0, NULL); |
| 1306 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1307 | } |
| 1308 | } |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1309 | |
| 1310 | cleanup: |
| 1311 | ly_set_erase(&recursion, NULL); |
| 1312 | return ret; |
| 1313 | } |
| 1314 | |
| 1315 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1316 | * @brief Create pre-compiled features array. |
| 1317 | * |
| 1318 | * See lys_feature_precompile() for more details. |
| 1319 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1320 | * @param[in] ctx Compile context. |
| 1321 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1322 | * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1323 | * @return LY_ERR value. |
| 1324 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1325 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1326 | lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1327 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1328 | LY_ARRAY_COUNT_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1329 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1330 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1331 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1332 | /* find the preprecompiled feature */ |
| 1333 | LY_ARRAY_FOR(features, x) { |
| 1334 | if (strcmp(features[x].name, feature_p->name)) { |
| 1335 | continue; |
| 1336 | } |
| 1337 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1338 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1339 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1340 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1341 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1342 | COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1343 | COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1344 | if (feature->iffeatures) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1345 | for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1346 | if (feature->iffeatures[u].features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1347 | for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1348 | /* check for circular dependency - direct reference first,... */ |
| 1349 | if (feature == feature->iffeatures[u].features[v]) { |
| 1350 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1351 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1352 | return LY_EVALID; |
| 1353 | } |
| 1354 | /* ... and indirect circular reference */ |
| 1355 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1356 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1357 | /* add itself into the dependants list */ |
| 1358 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1359 | *df = feature; |
| 1360 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1361 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1362 | } |
| 1363 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1364 | lysc_update_path(ctx, NULL, NULL); |
| 1365 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1366 | done: |
| 1367 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1368 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1369 | |
| 1370 | LOGINT(ctx->ctx); |
| 1371 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1372 | } |
| 1373 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1374 | /** |
| 1375 | * @brief Revert compiled list of features back to the precompiled state. |
| 1376 | * |
| 1377 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1378 | * The features are supposed to be stored again as dis_features in ::lys_module structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1379 | * |
| 1380 | * @param[in] ctx Compilation context. |
| 1381 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1382 | * and supposed to hold the dis_features list. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1383 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1384 | static void |
| 1385 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1386 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1387 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1388 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1389 | /* in the dis_features list, remove all the parts (from finished compiling process) |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1390 | * which may points into the data being freed here */ |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1391 | LY_ARRAY_FOR(mod->features, u) { |
| 1392 | LY_ARRAY_FOR(mod->features[u].iffeatures, v) { |
| 1393 | lysc_iffeature_free(ctx->ctx, &mod->features[u].iffeatures[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1394 | } |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1395 | LY_ARRAY_FREE(mod->features[u].iffeatures); |
| 1396 | mod->features[u].iffeatures = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1397 | |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1398 | LY_ARRAY_FOR(mod->features[u].exts, v) { |
| 1399 | lysc_ext_instance_free(ctx->ctx, &(mod->features[u].exts)[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1400 | } |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 1401 | LY_ARRAY_FREE(mod->features[u].exts); |
| 1402 | mod->features[u].exts = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1403 | } |
| 1404 | } |
| 1405 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1406 | /** |
| 1407 | * @brief Validate and normalize numeric value from a range definition. |
| 1408 | * @param[in] ctx Compile context. |
| 1409 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1410 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1411 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1412 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1413 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1414 | * @param[in] value String value of the range boundary. |
| 1415 | * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary. |
| 1416 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1417 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1418 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1419 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1420 | range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1421 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1422 | size_t fraction = 0, size; |
| 1423 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1424 | *len = 0; |
| 1425 | |
| 1426 | assert(value); |
| 1427 | /* parse value */ |
| 1428 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1429 | return LY_EVALID; |
| 1430 | } |
| 1431 | |
| 1432 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1433 | ++(*len); |
| 1434 | } |
| 1435 | |
| 1436 | while (isdigit(value[*len])) { |
| 1437 | ++(*len); |
| 1438 | } |
| 1439 | |
| 1440 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1441 | if (basetype == LY_TYPE_DEC64) { |
| 1442 | goto decimal; |
| 1443 | } else { |
| 1444 | *valcopy = strndup(value, *len); |
| 1445 | return LY_SUCCESS; |
| 1446 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1447 | } |
| 1448 | fraction = *len; |
| 1449 | |
| 1450 | ++(*len); |
| 1451 | while (isdigit(value[*len])) { |
| 1452 | ++(*len); |
| 1453 | } |
| 1454 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1455 | if (basetype == LY_TYPE_DEC64) { |
| 1456 | decimal: |
| 1457 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1458 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1459 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1460 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1461 | *len, value, frdigits); |
| 1462 | return LY_EINVAL; |
| 1463 | } |
| 1464 | if (fraction) { |
| 1465 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1466 | } else { |
| 1467 | size = (*len) + frdigits + 1; |
| 1468 | } |
| 1469 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1470 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1471 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1472 | (*valcopy)[size - 1] = '\0'; |
| 1473 | if (fraction) { |
| 1474 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1475 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1476 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1477 | } else { |
| 1478 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1479 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1480 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1481 | } |
| 1482 | return LY_SUCCESS; |
| 1483 | } |
| 1484 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1485 | /** |
| 1486 | * @brief Check that values in range are in ascendant order. |
| 1487 | * @param[in] unsigned_value Flag to note that we are working with unsigned values. |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1488 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1489 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1490 | * @param[in] value Current value to check. |
| 1491 | * @param[in] prev_value The last seen value. |
| 1492 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1493 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1494 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1495 | range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1496 | { |
| 1497 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1498 | if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1499 | return LY_EEXIST; |
| 1500 | } |
| 1501 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1502 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1503 | return LY_EEXIST; |
| 1504 | } |
| 1505 | } |
| 1506 | return LY_SUCCESS; |
| 1507 | } |
| 1508 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1509 | /** |
| 1510 | * @brief Set min/max value of the range part. |
| 1511 | * @param[in] ctx Compile context. |
| 1512 | * @param[in] part Range part structure to fill. |
| 1513 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1514 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1515 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1516 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1517 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1518 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1519 | * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1520 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1521 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1522 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1523 | * frdigits value), LY_EMEM. |
| 1524 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1525 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1526 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype, |
| 1527 | ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1528 | { |
| 1529 | LY_ERR ret = LY_SUCCESS; |
| 1530 | char *valcopy = NULL; |
| 1531 | size_t len; |
| 1532 | |
| 1533 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1534 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1535 | LY_CHECK_GOTO(ret, finalize); |
| 1536 | } |
| 1537 | if (!valcopy && base_range) { |
| 1538 | if (max) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1539 | part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1540 | } else { |
| 1541 | part->min_64 = base_range->parts[0].min_64; |
| 1542 | } |
| 1543 | if (!first) { |
| 1544 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1545 | } |
| 1546 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1550 | case LY_TYPE_INT8: /* range */ |
| 1551 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1552 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1553 | } else if (max) { |
| 1554 | part->max_64 = INT64_C(127); |
| 1555 | } else { |
| 1556 | part->min_64 = INT64_C(-128); |
| 1557 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1558 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1559 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1560 | } |
| 1561 | break; |
| 1562 | case LY_TYPE_INT16: /* range */ |
| 1563 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1564 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1565 | } else if (max) { |
| 1566 | part->max_64 = INT64_C(32767); |
| 1567 | } else { |
| 1568 | part->min_64 = INT64_C(-32768); |
| 1569 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1570 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1571 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1572 | } |
| 1573 | break; |
| 1574 | case LY_TYPE_INT32: /* range */ |
| 1575 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1576 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1577 | } else if (max) { |
| 1578 | part->max_64 = INT64_C(2147483647); |
| 1579 | } else { |
| 1580 | part->min_64 = INT64_C(-2147483648); |
| 1581 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1582 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1583 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1584 | } |
| 1585 | break; |
| 1586 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1587 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1588 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1589 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1590 | max ? &part->max_64 : &part->min_64); |
| 1591 | } else if (max) { |
| 1592 | part->max_64 = INT64_C(9223372036854775807); |
| 1593 | } else { |
| 1594 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1595 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1596 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1597 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1598 | } |
| 1599 | break; |
| 1600 | case LY_TYPE_UINT8: /* range */ |
| 1601 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1602 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1603 | } else if (max) { |
| 1604 | part->max_u64 = UINT64_C(255); |
| 1605 | } else { |
| 1606 | part->min_u64 = UINT64_C(0); |
| 1607 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1608 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1609 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1610 | } |
| 1611 | break; |
| 1612 | case LY_TYPE_UINT16: /* range */ |
| 1613 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1614 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1615 | } else if (max) { |
| 1616 | part->max_u64 = UINT64_C(65535); |
| 1617 | } else { |
| 1618 | part->min_u64 = UINT64_C(0); |
| 1619 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1620 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1621 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1622 | } |
| 1623 | break; |
| 1624 | case LY_TYPE_UINT32: /* range */ |
| 1625 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1626 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1627 | } else if (max) { |
| 1628 | part->max_u64 = UINT64_C(4294967295); |
| 1629 | } else { |
| 1630 | part->min_u64 = UINT64_C(0); |
| 1631 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1632 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1633 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1634 | } |
| 1635 | break; |
| 1636 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1637 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1638 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1639 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1640 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1641 | } else if (max) { |
| 1642 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1643 | } else { |
| 1644 | part->min_u64 = UINT64_C(0); |
| 1645 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1646 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1647 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1648 | } |
| 1649 | break; |
| 1650 | default: |
| 1651 | LOGINT(ctx->ctx); |
| 1652 | ret = LY_EINT; |
| 1653 | } |
| 1654 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1655 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1656 | if (ret == LY_EDENIED) { |
| 1657 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1658 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1659 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1660 | } else if (ret == LY_EVALID) { |
| 1661 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1662 | "Invalid %s restriction - invalid value \"%s\".", |
| 1663 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1664 | } else if (ret == LY_EEXIST) { |
| 1665 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1666 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1667 | length_restr ? "length" : "range", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1668 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1669 | } else if (!ret && value) { |
| 1670 | *value = *value + len; |
| 1671 | } |
| 1672 | free(valcopy); |
| 1673 | return ret; |
| 1674 | } |
| 1675 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1676 | /** |
| 1677 | * @brief Compile the parsed range restriction. |
| 1678 | * @param[in] ctx Compile context. |
| 1679 | * @param[in] range_p Parsed range structure to compile. |
| 1680 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1681 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1682 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1683 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1684 | * range restriction must be more restrictive than the base_range. |
| 1685 | * @param[in,out] range Pointer to the created current range structure. |
| 1686 | * @return LY_ERR value. |
| 1687 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1688 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1689 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1690 | uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1691 | { |
| 1692 | LY_ERR ret = LY_EVALID; |
| 1693 | const char *expr; |
| 1694 | struct lysc_range_part *parts = NULL, *part; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1695 | ly_bool range_expected = 0, uns; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1696 | LY_ARRAY_COUNT_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1697 | |
| 1698 | assert(range); |
| 1699 | assert(range_p); |
| 1700 | |
| 1701 | expr = range_p->arg; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1702 | while (1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1703 | if (isspace(*expr)) { |
| 1704 | ++expr; |
| 1705 | } else if (*expr == '\0') { |
| 1706 | if (range_expected) { |
| 1707 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1708 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1709 | length_restr ? "length" : "range", range_p->arg); |
| 1710 | goto cleanup; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1711 | } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1712 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1713 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1714 | length_restr ? "length" : "range", range_p->arg); |
| 1715 | goto cleanup; |
| 1716 | } |
| 1717 | parts_done++; |
| 1718 | break; |
| 1719 | } else if (!strncmp(expr, "min", 3)) { |
| 1720 | if (parts) { |
| 1721 | /* min cannot be used elsewhere than in the first part */ |
| 1722 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1723 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1724 | expr - range_p->arg, range_p->arg); |
| 1725 | goto cleanup; |
| 1726 | } |
| 1727 | expr += 3; |
| 1728 | |
| 1729 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1730 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1731 | part->max_64 = part->min_64; |
| 1732 | } else if (*expr == '|') { |
| 1733 | if (!parts || range_expected) { |
| 1734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1735 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1736 | goto cleanup; |
| 1737 | } |
| 1738 | expr++; |
| 1739 | parts_done++; |
| 1740 | /* process next part of the expression */ |
| 1741 | } else if (!strncmp(expr, "..", 2)) { |
| 1742 | expr += 2; |
| 1743 | while (isspace(*expr)) { |
| 1744 | expr++; |
| 1745 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1746 | if (!parts || LY_ARRAY_COUNT(parts) == parts_done) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1747 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1748 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1749 | goto cleanup; |
| 1750 | } |
| 1751 | /* continue expecting the upper boundary */ |
| 1752 | range_expected = 1; |
| 1753 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1754 | /* number */ |
| 1755 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1756 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1757 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1758 | range_expected = 0; |
| 1759 | } else { |
| 1760 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1761 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1762 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1763 | part->max_64 = part->min_64; |
| 1764 | } |
| 1765 | |
| 1766 | /* continue with possible another expression part */ |
| 1767 | } else if (!strncmp(expr, "max", 3)) { |
| 1768 | expr += 3; |
| 1769 | while (isspace(*expr)) { |
| 1770 | expr++; |
| 1771 | } |
| 1772 | if (*expr != '\0') { |
| 1773 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1774 | length_restr ? "length" : "range", expr); |
| 1775 | goto cleanup; |
| 1776 | } |
| 1777 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1778 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1779 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1780 | range_expected = 0; |
| 1781 | } else { |
| 1782 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1783 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1784 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1785 | part->min_64 = part->max_64; |
| 1786 | } |
| 1787 | } else { |
| 1788 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1789 | length_restr ? "length" : "range", expr); |
| 1790 | goto cleanup; |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | /* check with the previous range/length restriction */ |
| 1795 | if (base_range) { |
| 1796 | switch (basetype) { |
| 1797 | case LY_TYPE_BINARY: |
| 1798 | case LY_TYPE_UINT8: |
| 1799 | case LY_TYPE_UINT16: |
| 1800 | case LY_TYPE_UINT32: |
| 1801 | case LY_TYPE_UINT64: |
| 1802 | case LY_TYPE_STRING: |
| 1803 | uns = 1; |
| 1804 | break; |
| 1805 | case LY_TYPE_DEC64: |
| 1806 | case LY_TYPE_INT8: |
| 1807 | case LY_TYPE_INT16: |
| 1808 | case LY_TYPE_INT32: |
| 1809 | case LY_TYPE_INT64: |
| 1810 | uns = 0; |
| 1811 | break; |
| 1812 | default: |
| 1813 | LOGINT(ctx->ctx); |
| 1814 | ret = LY_EINT; |
| 1815 | goto cleanup; |
| 1816 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1817 | for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1818 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1819 | goto baseerror; |
| 1820 | } |
| 1821 | /* current lower bound is not lower than the base */ |
| 1822 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1823 | /* base has single value */ |
| 1824 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1825 | /* both lower bounds are the same */ |
| 1826 | if (parts[u].min_64 != parts[u].max_64) { |
| 1827 | /* current continues with a range */ |
| 1828 | goto baseerror; |
| 1829 | } else { |
| 1830 | /* equal single values, move both forward */ |
| 1831 | ++v; |
| 1832 | continue; |
| 1833 | } |
| 1834 | } else { |
| 1835 | /* base is single value lower than current range, so the |
| 1836 | * value from base range is removed in the current, |
| 1837 | * move only base and repeat checking */ |
| 1838 | ++v; |
| 1839 | --u; |
| 1840 | continue; |
| 1841 | } |
| 1842 | } else { |
| 1843 | /* base is the range */ |
| 1844 | if (parts[u].min_64 == parts[u].max_64) { |
| 1845 | /* current is a single value */ |
| 1846 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1847 | /* current is behind the base range, so base range is omitted, |
| 1848 | * move the base and keep the current for further check */ |
| 1849 | ++v; |
| 1850 | --u; |
| 1851 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1852 | continue; |
| 1853 | } else { |
| 1854 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1855 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1856 | /* higher bound is higher than the current higher bound */ |
| 1857 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1858 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1859 | * continue with the same current, but move the base */ |
| 1860 | --u; |
| 1861 | ++v; |
| 1862 | continue; |
| 1863 | } |
| 1864 | /* current range starts within the base range but end behind it */ |
| 1865 | goto baseerror; |
| 1866 | } else { |
| 1867 | /* current range is smaller than the base, |
| 1868 | * move current, but stay with the base */ |
| 1869 | continue; |
| 1870 | } |
| 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | if (u != parts_done) { |
| 1875 | baseerror: |
| 1876 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1877 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1878 | length_restr ? "length" : "range", range_p->arg); |
| 1879 | goto cleanup; |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | if (!(*range)) { |
| 1884 | *range = calloc(1, sizeof **range); |
| 1885 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1886 | } |
| 1887 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1888 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1889 | if (range_p->eapptag) { |
| 1890 | lydict_remove(ctx->ctx, (*range)->eapptag); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1891 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1892 | } |
| 1893 | if (range_p->emsg) { |
| 1894 | lydict_remove(ctx->ctx, (*range)->emsg); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1895 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1896 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1897 | if (range_p->dsc) { |
| 1898 | lydict_remove(ctx->ctx, (*range)->dsc); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1899 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1900 | } |
| 1901 | if (range_p->ref) { |
| 1902 | lydict_remove(ctx->ctx, (*range)->ref); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1903 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1904 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1905 | /* extensions are taken only from the last range by the caller */ |
| 1906 | |
| 1907 | (*range)->parts = parts; |
| 1908 | parts = NULL; |
| 1909 | ret = LY_SUCCESS; |
| 1910 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1911 | LY_ARRAY_FREE(parts); |
| 1912 | |
| 1913 | return ret; |
| 1914 | } |
| 1915 | |
| 1916 | /** |
| 1917 | * @brief Checks pattern syntax. |
| 1918 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1919 | * @param[in] ctx Context. |
| 1920 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1921 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1922 | * @param[in,out] pcre2_code Compiled PCRE2 pattern. If NULL, the compiled information used to validate pattern are freed. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1923 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1924 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1925 | LY_ERR |
| 1926 | lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1927 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1928 | size_t idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1929 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1930 | int err_code; |
| 1931 | const char *orig_ptr; |
| 1932 | PCRE2_SIZE err_offset; |
| 1933 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1934 | #define URANGE_LEN 19 |
| 1935 | char *ublock2urange[][2] = { |
| 1936 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1937 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1938 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1939 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1940 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1941 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1942 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1943 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1944 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1945 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1946 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1947 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1948 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1949 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1950 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1951 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1952 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1953 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1954 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1955 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1956 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1957 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1958 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1959 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1960 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1961 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1962 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1963 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1964 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1965 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1966 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1967 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1968 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1969 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1970 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1971 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1972 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1973 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1974 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1975 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1976 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1977 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1978 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1979 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1980 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1981 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1982 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1983 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1984 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1985 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1986 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1987 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1988 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1989 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1990 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1991 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1992 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1993 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1994 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1995 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1996 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1997 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1998 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1999 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 2000 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 2001 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 2002 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 2003 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 2004 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 2005 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 2006 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 2007 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 2008 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 2009 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 2010 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 2011 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 2012 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 2013 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 2014 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 2015 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 2016 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 2017 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 2018 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 2019 | {NULL, NULL} |
| 2020 | }; |
| 2021 | |
| 2022 | /* adjust the expression to a Perl equivalent |
| 2023 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 2024 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2025 | /* allocate space for the transformed pattern */ |
| 2026 | size = strlen(pattern) + 1; |
| 2027 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2028 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2029 | perl_regex[0] = '\0'; |
| 2030 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2031 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 2032 | brack = 0; |
| 2033 | idx = 0; |
| 2034 | orig_ptr = pattern; |
| 2035 | while (orig_ptr[0]) { |
| 2036 | switch (orig_ptr[0]) { |
| 2037 | case '$': |
| 2038 | case '^': |
| 2039 | if (!brack) { |
| 2040 | /* make space for the extra character */ |
| 2041 | ++size; |
| 2042 | perl_regex = ly_realloc(perl_regex, size); |
| 2043 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2044 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2045 | /* print escape slash */ |
| 2046 | perl_regex[idx] = '\\'; |
| 2047 | ++idx; |
| 2048 | } |
| 2049 | break; |
| 2050 | case '[': |
| 2051 | /* must not be escaped */ |
| 2052 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2053 | ++brack; |
| 2054 | } |
| 2055 | break; |
| 2056 | case ']': |
| 2057 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2058 | /* pattern was checked and compiled already */ |
| 2059 | assert(brack); |
| 2060 | --brack; |
| 2061 | } |
| 2062 | break; |
| 2063 | default: |
| 2064 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2065 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2066 | |
| 2067 | /* copy char */ |
| 2068 | perl_regex[idx] = orig_ptr[0]; |
| 2069 | |
| 2070 | ++idx; |
| 2071 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2072 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2073 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2074 | |
| 2075 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2076 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2077 | start = ptr - perl_regex; |
| 2078 | |
| 2079 | ptr = strchr(ptr, '}'); |
| 2080 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2081 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2082 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 2083 | free(perl_regex); |
| 2084 | return LY_EVALID; |
| 2085 | } |
| 2086 | end = (ptr - perl_regex) + 1; |
| 2087 | |
| 2088 | /* need more space */ |
| 2089 | if (end - start < URANGE_LEN) { |
| 2090 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2091 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | /* find our range */ |
| 2095 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2096 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2097 | break; |
| 2098 | } |
| 2099 | } |
| 2100 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2101 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2102 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2103 | free(perl_regex); |
| 2104 | return LY_EVALID; |
| 2105 | } |
| 2106 | |
| 2107 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2108 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2109 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2110 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2111 | } |
| 2112 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2113 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2114 | } |
| 2115 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2116 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2117 | /* skip brackets */ |
| 2118 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2119 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2120 | } else { |
| 2121 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2122 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2127 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2128 | PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE, |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2129 | &err_code, &err_offset, NULL); |
| 2130 | if (!code_local) { |
| 2131 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2132 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2133 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2134 | free(perl_regex); |
| 2135 | return LY_EVALID; |
| 2136 | } |
| 2137 | free(perl_regex); |
| 2138 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2139 | if (code) { |
| 2140 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2141 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2142 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | return LY_SUCCESS; |
| 2146 | |
| 2147 | #undef URANGE_LEN |
| 2148 | } |
| 2149 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2150 | /** |
| 2151 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2152 | * @param[in] ctx Compile context. |
| 2153 | * @param[in] patterns_p Array of parsed patterns from the current type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2154 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2155 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2156 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2157 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2158 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2159 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2160 | lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2161 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2162 | { |
| 2163 | struct lysc_pattern **pattern; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2164 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2165 | LY_ERR ret = LY_SUCCESS; |
| 2166 | |
| 2167 | /* first, copy the patterns from the base type */ |
| 2168 | if (base_patterns) { |
| 2169 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2170 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2171 | } |
| 2172 | |
| 2173 | LY_ARRAY_FOR(patterns_p, u) { |
| 2174 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2175 | *pattern = calloc(1, sizeof **pattern); |
| 2176 | ++(*pattern)->refcount; |
| 2177 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2178 | ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2179 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2180 | |
| 2181 | if (patterns_p[u].arg[0] == 0x15) { |
| 2182 | (*pattern)->inverted = 1; |
| 2183 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2184 | DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr, ret, done); |
| 2185 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done); |
| 2186 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done); |
| 2187 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done); |
| 2188 | DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2189 | COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2190 | } |
| 2191 | done: |
| 2192 | return ret; |
| 2193 | } |
| 2194 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2195 | /** |
| 2196 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2197 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2198 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2199 | 0 /* LY_TYPE_UNKNOWN */, |
| 2200 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2201 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2202 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2203 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2204 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2205 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2206 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2207 | 0 /* LY_TYPE_BOOL */, |
| 2208 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2209 | 0 /* LY_TYPE_EMPTY */, |
| 2210 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2211 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2212 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2213 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2214 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2215 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2216 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2217 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2218 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2219 | }; |
| 2220 | |
| 2221 | /** |
| 2222 | * @brief stringification of the YANG built-in data types |
| 2223 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2224 | const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2225 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2226 | "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2227 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2228 | /** |
| 2229 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2230 | * @param[in] ctx Compile context. |
| 2231 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2232 | * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2233 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2234 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2235 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2236 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2237 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2238 | lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2239 | struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2240 | { |
| 2241 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2242 | LY_ARRAY_COUNT_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2243 | int32_t value = 0; |
| 2244 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2245 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2246 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2247 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2248 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2249 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2250 | return LY_EVALID; |
| 2251 | } |
| 2252 | |
| 2253 | LY_ARRAY_FOR(enums_p, u) { |
| 2254 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2255 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done); |
| 2256 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->dsc, ret, done); |
| 2257 | DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2258 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2259 | if (base_enums) { |
| 2260 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2261 | LY_ARRAY_FOR(base_enums, v) { |
| 2262 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2263 | break; |
| 2264 | } |
| 2265 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2266 | if (v == LY_ARRAY_COUNT(base_enums)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2267 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2268 | "Invalid %s - derived type adds new item \"%s\".", |
| 2269 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2270 | return LY_EVALID; |
| 2271 | } |
| 2272 | match = v; |
| 2273 | } |
| 2274 | |
| 2275 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2276 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2277 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2278 | e->value = (int32_t)enums_p[u].value; |
| 2279 | if (!u || e->value >= value) { |
| 2280 | value = e->value + 1; |
| 2281 | } |
| 2282 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2283 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2284 | if (e->value == (*enums)[v].value) { |
| 2285 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2286 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2287 | e->value, e->name, (*enums)[v].name); |
| 2288 | return LY_EVALID; |
| 2289 | } |
| 2290 | } |
| 2291 | } else if (base_enums) { |
| 2292 | /* inherit the assigned value */ |
| 2293 | e->value = base_enums[match].value; |
| 2294 | if (!u || e->value >= value) { |
| 2295 | value = e->value + 1; |
| 2296 | } |
| 2297 | } else { |
| 2298 | /* assign value automatically */ |
| 2299 | if (u && value == INT32_MIN) { |
| 2300 | /* counter overflow */ |
| 2301 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2302 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2303 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2304 | return LY_EVALID; |
| 2305 | } |
| 2306 | e->value = value++; |
| 2307 | } |
| 2308 | } else { /* LY_TYPE_BITS */ |
| 2309 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2310 | e->value = (int32_t)enums_p[u].value; |
| 2311 | if (!u || (uint32_t)e->value >= position) { |
| 2312 | position = (uint32_t)e->value + 1; |
| 2313 | } |
| 2314 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2315 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2316 | if (e->value == (*enums)[v].value) { |
| 2317 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2318 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2319 | (uint32_t)e->value, e->name, (*enums)[v].name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2320 | return LY_EVALID; |
| 2321 | } |
| 2322 | } |
| 2323 | } else if (base_enums) { |
| 2324 | /* inherit the assigned value */ |
| 2325 | e->value = base_enums[match].value; |
| 2326 | if (!u || (uint32_t)e->value >= position) { |
| 2327 | position = (uint32_t)e->value + 1; |
| 2328 | } |
| 2329 | } else { |
| 2330 | /* assign value automatically */ |
| 2331 | if (u && position == 0) { |
| 2332 | /* counter overflow */ |
| 2333 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2334 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2335 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2336 | return LY_EVALID; |
| 2337 | } |
| 2338 | e->value = position++; |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | if (base_enums) { |
| 2343 | /* the assigned values must not change from the derived type */ |
| 2344 | if (e->value != base_enums[match].value) { |
| 2345 | if (basetype == LY_TYPE_ENUM) { |
| 2346 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2347 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2348 | e->name, base_enums[match].value, e->value); |
| 2349 | } else { |
| 2350 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2351 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2352 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2353 | } |
| 2354 | return LY_EVALID; |
| 2355 | } |
| 2356 | } |
| 2357 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2358 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2359 | COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM : LYEXT_PAR_TYPE_BIT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2360 | |
| 2361 | if (basetype == LY_TYPE_BITS) { |
| 2362 | /* keep bits ordered by position */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2363 | for (v = u; v && (*enums)[v - 1].value > e->value; --v) {} |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2364 | if (v != u) { |
| 2365 | memcpy(&storage, e, sizeof *e); |
| 2366 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2367 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2368 | } |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | done: |
| 2373 | return ret; |
| 2374 | } |
| 2375 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2376 | /** |
| 2377 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2378 | * |
| 2379 | * path-arg = absolute-path / relative-path |
| 2380 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2381 | * relative-path = 1*(".." "/") descendant-path |
| 2382 | * |
| 2383 | * @param[in,out] path Path to parse. |
| 2384 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2385 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2386 | * @param[out] name Name of the token. |
| 2387 | * @param[out] nam_len Length of the name. |
| 2388 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2389 | * must not be changed between consecutive calls. -1 if the |
| 2390 | * path is absolute. |
| 2391 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2392 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2393 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2394 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2395 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2396 | int32_t *parent_times, ly_bool *has_predicate) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2397 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2398 | int32_t par_times = 0; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2399 | |
| 2400 | assert(path && *path); |
| 2401 | assert(parent_times); |
| 2402 | assert(prefix); |
| 2403 | assert(prefix_len); |
| 2404 | assert(name); |
| 2405 | assert(name_len); |
| 2406 | assert(has_predicate); |
| 2407 | |
| 2408 | *prefix = NULL; |
| 2409 | *prefix_len = 0; |
| 2410 | *name = NULL; |
| 2411 | *name_len = 0; |
| 2412 | *has_predicate = 0; |
| 2413 | |
| 2414 | if (!*parent_times) { |
| 2415 | if (!strncmp(*path, "..", 2)) { |
| 2416 | *path += 2; |
| 2417 | ++par_times; |
| 2418 | while (!strncmp(*path, "/..", 3)) { |
| 2419 | *path += 3; |
| 2420 | ++par_times; |
| 2421 | } |
| 2422 | } |
| 2423 | if (par_times) { |
| 2424 | *parent_times = par_times; |
| 2425 | } else { |
| 2426 | *parent_times = -1; |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | if (**path != '/') { |
| 2431 | return LY_EINVAL; |
| 2432 | } |
| 2433 | /* skip '/' */ |
| 2434 | ++(*path); |
| 2435 | |
| 2436 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2437 | LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2438 | |
| 2439 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2440 | /* path continues by another token or this is the last token */ |
| 2441 | return LY_SUCCESS; |
| 2442 | } else if ((*path)[0] != '[') { |
| 2443 | /* unexpected character */ |
| 2444 | return LY_EINVAL; |
| 2445 | } else { |
| 2446 | /* predicate starting with [ */ |
| 2447 | *has_predicate = 1; |
| 2448 | return LY_SUCCESS; |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2453 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2454 | * |
| 2455 | * The set of features used for target must be a subset of features used for the leafref. |
| 2456 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2457 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2458 | * |
| 2459 | * @param[in] refnode The leafref node. |
| 2460 | * @param[in] target Tha target node of the leafref. |
| 2461 | * @return LY_SUCCESS or LY_EVALID; |
| 2462 | */ |
| 2463 | static LY_ERR |
| 2464 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2465 | { |
| 2466 | LY_ERR ret = LY_EVALID; |
| 2467 | const struct lysc_node *iter; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2468 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2469 | struct ly_set features = {0}; |
| 2470 | |
| 2471 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2472 | if (iter->iffeatures) { |
| 2473 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2474 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2475 | LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0, NULL), cleanup); |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | /* we should have, in features set, a superset of features applicable to the target node. |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2482 | * If the feature is not present, we don;t have a subset of features applicable |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2483 | * to the leafref itself. */ |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2484 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2485 | if (iter->iffeatures) { |
| 2486 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2487 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2488 | if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) { |
| 2489 | /* feature not present */ |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2490 | goto cleanup; |
| 2491 | } |
| 2492 | } |
| 2493 | } |
| 2494 | } |
| 2495 | } |
| 2496 | ret = LY_SUCCESS; |
| 2497 | |
| 2498 | cleanup: |
| 2499 | ly_set_erase(&features, NULL); |
| 2500 | return ret; |
| 2501 | } |
| 2502 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2503 | static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2504 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2505 | struct lysc_type **type, const char **units, const char **dflt, struct lys_module **dflt_mod); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2506 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2507 | /** |
| 2508 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2509 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2510 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2511 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2512 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2513 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2514 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2515 | * @param[in] module Context module for the leafref path and identityref (to correctly resolve prefixes) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2516 | * @param[in] basetype Base YANG built-in type of the type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2517 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2518 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2519 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2520 | * @return LY_ERR value. |
| 2521 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2522 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2523 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame^] | 2524 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, struct lys_module *module, |
| 2525 | LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2526 | { |
| 2527 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2528 | struct lysc_type_bin *bin; |
| 2529 | struct lysc_type_num *num; |
| 2530 | struct lysc_type_str *str; |
| 2531 | struct lysc_type_bits *bits; |
| 2532 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2533 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2534 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2535 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2536 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2537 | |
| 2538 | switch (basetype) { |
| 2539 | case LY_TYPE_BINARY: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2540 | bin = (struct lysc_type_bin *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2541 | |
| 2542 | /* 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] | 2543 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2544 | 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] | 2545 | base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2546 | if (!tpdfname) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame^] | 2547 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, cleanup); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2548 | } |
| 2549 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2550 | break; |
| 2551 | case LY_TYPE_BITS: |
| 2552 | /* RFC 7950 9.7 - bits */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2553 | bits = (struct lysc_type_bits *)(*type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2554 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2555 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2556 | base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL, |
| 2557 | (struct lysc_type_bitenum_item **)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2558 | } |
| 2559 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2560 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2561 | /* 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] | 2562 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2563 | 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] | 2564 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2565 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", ""); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2566 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2567 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2568 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2569 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2570 | case LY_TYPE_DEC64: |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2571 | dec = (struct lysc_type_dec *)(*type); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2572 | |
| 2573 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2574 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2575 | if (!type_p->fraction_digits) { |
| 2576 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2577 | 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] | 2578 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2579 | 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] | 2580 | } |
| 2581 | return LY_EVALID; |
| 2582 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2583 | dec->fraction_digits = type_p->fraction_digits; |
| 2584 | } else { |
| 2585 | if (type_p->fraction_digits) { |
| 2586 | /* fraction digits is prohibited in types not directly derived from built-in decimal64 */ |
| 2587 | if (tpdfname) { |
| 2588 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2589 | "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.", |
| 2590 | tpdfname); |
| 2591 | } else { |
| 2592 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2593 | "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type."); |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2594 | } |
| 2595 | return LY_EVALID; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2596 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2597 | dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2598 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2599 | |
| 2600 | /* RFC 7950 9.2.4 - range */ |
| 2601 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2602 | 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] | 2603 | base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2604 | if (!tpdfname) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame^] | 2605 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, cleanup); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2606 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2607 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2608 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2609 | case LY_TYPE_STRING: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2610 | str = (struct lysc_type_str *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2611 | |
| 2612 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2613 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2614 | 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] | 2615 | base ? ((struct lysc_type_str *)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2616 | if (!tpdfname) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame^] | 2617 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, cleanup); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2618 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2619 | } else if (base && ((struct lysc_type_str *)base)->length) { |
| 2620 | 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] | 2621 | } |
| 2622 | |
| 2623 | /* RFC 7950 9.4.5 - pattern */ |
| 2624 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2625 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2626 | base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns)); |
| 2627 | } else if (base && ((struct lysc_type_str *)base)->patterns) { |
| 2628 | 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] | 2629 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2630 | break; |
| 2631 | case LY_TYPE_ENUM: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2632 | enumeration = (struct lysc_type_enum *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2633 | |
| 2634 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2635 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2636 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2637 | base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2638 | } |
| 2639 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2640 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2641 | /* 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] | 2642 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2643 | 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] | 2644 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2645 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", ""); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2646 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2647 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2648 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2649 | break; |
| 2650 | case LY_TYPE_INT8: |
| 2651 | case LY_TYPE_UINT8: |
| 2652 | case LY_TYPE_INT16: |
| 2653 | case LY_TYPE_UINT16: |
| 2654 | case LY_TYPE_INT32: |
| 2655 | case LY_TYPE_UINT32: |
| 2656 | case LY_TYPE_INT64: |
| 2657 | case LY_TYPE_UINT64: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2658 | num = (struct lysc_type_num *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2659 | |
| 2660 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2661 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2662 | 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] | 2663 | base ? ((struct lysc_type_num *)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2664 | if (!tpdfname) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame^] | 2665 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, cleanup); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2666 | } |
| 2667 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2668 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2669 | case LY_TYPE_IDENT: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2670 | idref = (struct lysc_type_identityref *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2671 | |
| 2672 | /* RFC 7950 9.10.2 - base */ |
| 2673 | if (type_p->bases) { |
| 2674 | if (base) { |
| 2675 | /* only the directly derived identityrefs can contain base specification */ |
| 2676 | if (tpdfname) { |
| 2677 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2678 | "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] | 2679 | tpdfname); |
| 2680 | } else { |
| 2681 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2682 | "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] | 2683 | } |
| 2684 | return LY_EVALID; |
| 2685 | } |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2686 | 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] | 2687 | } |
| 2688 | |
| 2689 | if (!base && !type_p->flags) { |
| 2690 | /* type derived from identityref built-in type must contain at least one base */ |
| 2691 | if (tpdfname) { |
| 2692 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2693 | } else { |
| 2694 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2695 | } |
| 2696 | return LY_EVALID; |
| 2697 | } |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2698 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2699 | case LY_TYPE_LEAFREF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2700 | lref = (struct lysc_type_leafref *)*type; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2701 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2702 | /* RFC 7950 9.9.3 - require-instance */ |
| 2703 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2704 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2705 | if (tpdfname) { |
| 2706 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2707 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2708 | } else { |
| 2709 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2710 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2711 | } |
| 2712 | return LY_EVALID; |
| 2713 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2714 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2715 | } else if (base) { |
| 2716 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2717 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2718 | } else { |
| 2719 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2720 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2721 | } |
| 2722 | if (type_p->path) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame^] | 2723 | LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2724 | lref->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2725 | } else if (base) { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame^] | 2726 | LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path)); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2727 | lref->path_context = ((struct lysc_type_leafref *)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2728 | } else if (tpdfname) { |
| 2729 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2730 | return LY_EVALID; |
| 2731 | } else { |
| 2732 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2733 | return LY_EVALID; |
| 2734 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2735 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2736 | case LY_TYPE_INST: |
| 2737 | /* RFC 7950 9.9.3 - require-instance */ |
| 2738 | if (type_p->flags & LYS_SET_REQINST) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2739 | ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2740 | } else { |
| 2741 | /* default is true */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2742 | ((struct lysc_type_instanceid *)(*type))->require_instance = 1; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2743 | } |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2744 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2745 | case LY_TYPE_UNION: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2746 | un = (struct lysc_type_union *)(*type); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2747 | |
| 2748 | /* RFC 7950 7.4 - type */ |
| 2749 | if (type_p->types) { |
| 2750 | if (base) { |
| 2751 | /* only the directly derived union can contain types specification */ |
| 2752 | if (tpdfname) { |
| 2753 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2754 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2755 | tpdfname); |
| 2756 | } else { |
| 2757 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2758 | "Invalid type substatement for the type not directly derived from union built-in type."); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2759 | } |
| 2760 | return LY_EVALID; |
| 2761 | } |
| 2762 | /* compile the type */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2763 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID); |
| 2764 | 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] | 2765 | 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] | 2766 | &type_p->types[u], &un->types[u + additional], NULL, NULL, NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2767 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2768 | /* add space for additional types from the union subtype */ |
| 2769 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2770 | LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1, |
| 2771 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2772 | |
| 2773 | /* copy subtypes of the subtype union */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2774 | 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] | 2775 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2776 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2777 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2778 | 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] | 2779 | lref = (struct lysc_type_leafref *)un->types[u + additional]; |
| 2780 | |
| 2781 | lref->basetype = LY_TYPE_LEAFREF; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame^] | 2782 | LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2783 | lref->refcount = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2784 | lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance; |
| 2785 | 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] | 2786 | /* TODO extensions */ |
| 2787 | |
| 2788 | } else { |
| 2789 | un->types[u + additional] = un_aux->types[v]; |
| 2790 | ++un_aux->types[v]->refcount; |
| 2791 | } |
| 2792 | ++additional; |
| 2793 | LY_ARRAY_INCREMENT(un->types); |
| 2794 | } |
| 2795 | /* compensate u increment in main loop */ |
| 2796 | --additional; |
| 2797 | |
| 2798 | /* free the replaced union subtype */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2799 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2800 | } else { |
| 2801 | LY_ARRAY_INCREMENT(un->types); |
| 2802 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2803 | } |
| 2804 | } |
| 2805 | |
| 2806 | if (!base && !type_p->flags) { |
| 2807 | /* type derived from union built-in type must contain at least one type */ |
| 2808 | if (tpdfname) { |
| 2809 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2810 | } else { |
| 2811 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2812 | } |
| 2813 | return LY_EVALID; |
| 2814 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2815 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2816 | case LY_TYPE_BOOL: |
| 2817 | case LY_TYPE_EMPTY: |
| 2818 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2819 | break; |
| 2820 | } |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame^] | 2821 | |
| 2822 | if (tpdfname) { |
| 2823 | switch (basetype) { |
| 2824 | case LY_TYPE_BINARY: |
| 2825 | type_p->compiled = *type; |
| 2826 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2827 | break; |
| 2828 | case LY_TYPE_BITS: |
| 2829 | type_p->compiled = *type; |
| 2830 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2831 | break; |
| 2832 | case LY_TYPE_DEC64: |
| 2833 | type_p->compiled = *type; |
| 2834 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2835 | break; |
| 2836 | case LY_TYPE_STRING: |
| 2837 | type_p->compiled = *type; |
| 2838 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2839 | break; |
| 2840 | case LY_TYPE_ENUM: |
| 2841 | type_p->compiled = *type; |
| 2842 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2843 | break; |
| 2844 | case LY_TYPE_INT8: |
| 2845 | case LY_TYPE_UINT8: |
| 2846 | case LY_TYPE_INT16: |
| 2847 | case LY_TYPE_UINT16: |
| 2848 | case LY_TYPE_INT32: |
| 2849 | case LY_TYPE_UINT32: |
| 2850 | case LY_TYPE_INT64: |
| 2851 | case LY_TYPE_UINT64: |
| 2852 | type_p->compiled = *type; |
| 2853 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2854 | break; |
| 2855 | case LY_TYPE_IDENT: |
| 2856 | type_p->compiled = *type; |
| 2857 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2858 | break; |
| 2859 | case LY_TYPE_LEAFREF: |
| 2860 | type_p->compiled = *type; |
| 2861 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2862 | break; |
| 2863 | case LY_TYPE_INST: |
| 2864 | type_p->compiled = *type; |
| 2865 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2866 | break; |
| 2867 | case LY_TYPE_UNION: |
| 2868 | type_p->compiled = *type; |
| 2869 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2870 | break; |
| 2871 | case LY_TYPE_BOOL: |
| 2872 | case LY_TYPE_EMPTY: |
| 2873 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2874 | break; |
| 2875 | } |
| 2876 | } |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2877 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame^] | 2878 | |
| 2879 | cleanup: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2880 | return ret; |
| 2881 | } |
| 2882 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2883 | /** |
| 2884 | * @brief Compile information about the leaf/leaf-list's type. |
| 2885 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2886 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2887 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2888 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2889 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2890 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2891 | * @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] | 2892 | * @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] | 2893 | * @param[out] dflt Default value for the type. |
| 2894 | * @param[out] dflt_mod Local module for the default value. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2895 | * @return LY_ERR value. |
| 2896 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2897 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2898 | 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] | 2899 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2900 | 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] | 2901 | { |
| 2902 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2903 | ly_bool dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2904 | struct type_context { |
| 2905 | const struct lysp_tpdf *tpdf; |
| 2906 | struct lysp_node *node; |
| 2907 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2908 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2909 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2910 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2911 | struct ly_set tpdf_chain = {0}; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2912 | |
| 2913 | assert((dflt && dflt_mod) || (!dflt && !dflt_mod)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2914 | |
| 2915 | (*type) = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2916 | if (dflt) { |
| 2917 | *dflt = NULL; |
| 2918 | *dflt_mod = NULL; |
| 2919 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2920 | |
| 2921 | tctx = calloc(1, sizeof *tctx); |
| 2922 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2923 | 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] | 2924 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2925 | ret == LY_SUCCESS; |
| 2926 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2927 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2928 | if (basetype) { |
| 2929 | break; |
| 2930 | } |
| 2931 | |
| 2932 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2933 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2934 | tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name); |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 2935 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2936 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2937 | if (units && !*units) { |
| 2938 | /* inherit units */ |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 2939 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret); |
| 2940 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2941 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2942 | if (dflt && !*dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2943 | /* inherit default */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2944 | *dflt = tctx->tpdf->dflt; |
| 2945 | *dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2946 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2947 | if (dummyloops && (!units || *units) && dflt && *dflt) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2948 | 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] | 2949 | break; |
| 2950 | } |
| 2951 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2952 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2953 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2954 | * 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] | 2955 | basetype = tctx->tpdf->type.compiled->basetype; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2956 | ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 2957 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2958 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2959 | if ((units && !*units) || (dflt && !*dflt)) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2960 | dummyloops = 1; |
| 2961 | goto preparenext; |
| 2962 | } else { |
| 2963 | tctx = NULL; |
| 2964 | break; |
| 2965 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2966 | } |
| 2967 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2968 | /* circular typedef reference detection */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2969 | for (uint32_t u = 0; u < tpdf_chain.count; u++) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2970 | /* local part */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2971 | tctx_iter = (struct type_context *)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2972 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2973 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2974 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2975 | free(tctx); |
| 2976 | ret = LY_EVALID; |
| 2977 | goto cleanup; |
| 2978 | } |
| 2979 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2980 | for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2981 | /* global part for unions corner case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2982 | tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2983 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2984 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2985 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2986 | free(tctx); |
| 2987 | ret = LY_EVALID; |
| 2988 | goto cleanup; |
| 2989 | } |
| 2990 | } |
| 2991 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2992 | /* store information for the following processing */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2993 | ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 2994 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2995 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2996 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2997 | /* prepare next loop */ |
| 2998 | tctx_prev = tctx; |
| 2999 | tctx = calloc(1, sizeof *tctx); |
| 3000 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3001 | } |
| 3002 | free(tctx); |
| 3003 | |
| 3004 | /* allocate type according to the basetype */ |
| 3005 | switch (basetype) { |
| 3006 | case LY_TYPE_BINARY: |
| 3007 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3008 | break; |
| 3009 | case LY_TYPE_BITS: |
| 3010 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3011 | break; |
| 3012 | case LY_TYPE_BOOL: |
| 3013 | case LY_TYPE_EMPTY: |
| 3014 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3015 | break; |
| 3016 | case LY_TYPE_DEC64: |
| 3017 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3018 | break; |
| 3019 | case LY_TYPE_ENUM: |
| 3020 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3021 | break; |
| 3022 | case LY_TYPE_IDENT: |
| 3023 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3024 | break; |
| 3025 | case LY_TYPE_INST: |
| 3026 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3027 | break; |
| 3028 | case LY_TYPE_LEAFREF: |
| 3029 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3030 | break; |
| 3031 | case LY_TYPE_STRING: |
| 3032 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3033 | break; |
| 3034 | case LY_TYPE_UNION: |
| 3035 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3036 | break; |
| 3037 | case LY_TYPE_INT8: |
| 3038 | case LY_TYPE_UINT8: |
| 3039 | case LY_TYPE_INT16: |
| 3040 | case LY_TYPE_UINT16: |
| 3041 | case LY_TYPE_INT32: |
| 3042 | case LY_TYPE_UINT32: |
| 3043 | case LY_TYPE_INT64: |
| 3044 | case LY_TYPE_UINT64: |
| 3045 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3046 | break; |
| 3047 | case LY_TYPE_UNKNOWN: |
| 3048 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3049 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3050 | ret = LY_EVALID; |
| 3051 | goto cleanup; |
| 3052 | } |
| 3053 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3054 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3055 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3056 | ly_data_type2str[basetype]); |
| 3057 | free(*type); |
| 3058 | (*type) = NULL; |
| 3059 | ret = LY_EVALID; |
| 3060 | goto cleanup; |
| 3061 | } |
| 3062 | |
| 3063 | /* get restrictions from the referred typedefs */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3064 | for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3065 | tctx = (struct type_context *)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3066 | |
| 3067 | /* remember the typedef context for circular check */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3068 | ret = ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
| 3069 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3070 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3071 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3072 | base = tctx->tpdf->type.compiled; |
| 3073 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3074 | } 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] | 3075 | /* no change, just use the type information from the base */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3076 | 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] | 3077 | ++base->refcount; |
| 3078 | continue; |
| 3079 | } |
| 3080 | |
| 3081 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3082 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3083 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3084 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3085 | ret = LY_EVALID; |
| 3086 | goto cleanup; |
| 3087 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3088 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3089 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3090 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3091 | ret = LY_EVALID; |
| 3092 | goto cleanup; |
| 3093 | } |
| 3094 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3095 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3096 | /* TODO user type plugins */ |
| 3097 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3098 | prev_type = *type; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3099 | 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] | 3100 | 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] | 3101 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3102 | LY_CHECK_GOTO(ret, cleanup); |
| 3103 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3104 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3105 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3106 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3107 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3108 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3109 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3110 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3111 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3112 | /* TODO user type plugins */ |
| 3113 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3114 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3115 | 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] | 3116 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 90b148f | 2020-05-06 17:49:40 +0200 | [diff] [blame] | 3117 | } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3118 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3119 | free(*type); |
| 3120 | (*type) = base; |
| 3121 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3122 | } |
| 3123 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3124 | 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] | 3125 | |
| 3126 | cleanup: |
| 3127 | ly_set_erase(&tpdf_chain, free); |
| 3128 | return ret; |
| 3129 | } |
| 3130 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3131 | /** |
| 3132 | * @brief Compile status information of the given node. |
| 3133 | * |
| 3134 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3135 | * has the status correctly set during the compilation. |
| 3136 | * |
| 3137 | * @param[in] ctx Compile context |
| 3138 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3139 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3140 | * the compatibility with the parent's status value. |
| 3141 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3142 | * @return LY_ERR value. |
| 3143 | */ |
| 3144 | static LY_ERR |
| 3145 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3146 | { |
| 3147 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3148 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3149 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3150 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3151 | if ((parent_flags & 0x3) != 0x3) { |
| 3152 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3153 | * combination of bits (0x3) which marks the uses_status value */ |
| 3154 | 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] | 3155 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3156 | } |
| 3157 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3158 | } else { |
| 3159 | (*node_flags) |= LYS_STATUS_CURR; |
| 3160 | } |
| 3161 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3162 | /* check status compatibility with the parent */ |
| 3163 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3164 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3165 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3166 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3167 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3168 | } else { /* LYS_STATUS_DEPRC */ |
| 3169 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3170 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3171 | } |
| 3172 | return LY_EVALID; |
| 3173 | } |
| 3174 | } |
| 3175 | return LY_SUCCESS; |
| 3176 | } |
| 3177 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3178 | /** |
| 3179 | * @brief Check uniqness of the node/action/notification name. |
| 3180 | * |
| 3181 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3182 | * structures, but they share the namespace so we need to check their name collisions. |
| 3183 | * |
| 3184 | * @param[in] ctx Compile context. |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3185 | * @param[in] parent Parent of the nodes to check, can be NULL. |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3186 | * @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] | 3187 | * @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] | 3188 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3189 | */ |
| 3190 | static LY_ERR |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3191 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name, |
| 3192 | const struct lysc_node *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3193 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3194 | const struct lysc_node *iter, *iter2; |
| 3195 | const struct lysc_action *actions; |
| 3196 | const struct lysc_notif *notifs; |
| 3197 | uint32_t getnext_flags; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3198 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3199 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3200 | #define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name)) |
| 3201 | |
| 3202 | if (exclude->nodetype == LYS_CASE) { |
| 3203 | /* check restricted only to all the cases */ |
| 3204 | assert(parent->nodetype == LYS_CHOICE); |
| 3205 | LY_LIST_FOR(lysc_node_children(parent, 0), iter) { |
| 3206 | if (CHECK_NODE(iter, exclude, name)) { |
| 3207 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case"); |
| 3208 | return LY_EEXIST; |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | return LY_SUCCESS; |
| 3213 | } |
| 3214 | |
| 3215 | /* no reason for our parent to be choice anymore */ |
| 3216 | assert(!parent || (parent->nodetype != LYS_CHOICE)); |
| 3217 | |
| 3218 | if (parent && (parent->nodetype == LYS_CASE)) { |
| 3219 | /* move to the first data definition parent */ |
| 3220 | parent = lysc_data_parent(parent); |
| 3221 | } |
| 3222 | |
| 3223 | getnext_flags = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE; |
| 3224 | if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) { |
| 3225 | getnext_flags |= LYS_GETNEXT_OUTPUT; |
| 3226 | } |
| 3227 | |
| 3228 | iter = NULL; |
| 3229 | while ((iter = lys_getnext(iter, parent, ctx->mod->compiled, getnext_flags))) { |
| 3230 | if (CHECK_NODE(iter, exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3231 | goto error; |
| 3232 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3233 | |
| 3234 | /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */ |
| 3235 | if (iter->nodetype == LYS_CHOICE) { |
| 3236 | iter2 = NULL; |
| 3237 | while ((iter2 = lys_getnext(iter2, iter, NULL, LYS_GETNEXT_NOSTATECHECK))) { |
| 3238 | if (CHECK_NODE(iter2, exclude, name)) { |
| 3239 | goto error; |
| 3240 | } |
| 3241 | } |
| 3242 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3243 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3244 | |
| 3245 | actions = parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3246 | LY_ARRAY_FOR(actions, u) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3247 | if (CHECK_NODE(&actions[u], exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3248 | goto error; |
| 3249 | } |
| 3250 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3251 | |
| 3252 | notifs = parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3253 | LY_ARRAY_FOR(notifs, u) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3254 | if (CHECK_NODE(¬ifs[u], exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3255 | goto error; |
| 3256 | } |
| 3257 | } |
| 3258 | return LY_SUCCESS; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3259 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3260 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3261 | 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] | 3262 | return LY_EEXIST; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3263 | |
| 3264 | #undef CHECK_NODE |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3265 | } |
| 3266 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3267 | 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] | 3268 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3269 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3270 | * @brief Compile parsed RPC/action schema node information. |
| 3271 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3272 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3273 | * @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] | 3274 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3275 | * @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). |
| 3276 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3277 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3278 | */ |
| 3279 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3280 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3281 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3282 | { |
| 3283 | LY_ERR ret = LY_SUCCESS; |
| 3284 | struct lysp_node *child_p; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3285 | LY_ARRAY_COUNT_TYPE u; |
| 3286 | uint32_t opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3287 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3288 | lysc_update_path(ctx, parent, action_p->name); |
| 3289 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3290 | /* member needed for uniqueness check lys_getnext() */ |
| 3291 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
| 3292 | action->module = ctx->mod; |
| 3293 | action->parent = parent; |
| 3294 | |
| 3295 | 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] | 3296 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3297 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3298 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3299 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3300 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3301 | return LY_EVALID; |
| 3302 | } |
| 3303 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3304 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3305 | action->sp = action_p; |
| 3306 | } |
| 3307 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3308 | |
| 3309 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3310 | * 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] | 3311 | 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] | 3312 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3313 | DUP_STRING_GOTO(ctx->ctx, action_p->name, action->name, ret, cleanup); |
| 3314 | DUP_STRING_GOTO(ctx->ctx, action_p->dsc, action->dsc, ret, cleanup); |
| 3315 | DUP_STRING_GOTO(ctx->ctx, action_p->ref, action->ref, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3316 | 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] | 3317 | 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] | 3318 | |
| 3319 | /* input */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3320 | lysc_update_path(ctx, (struct lysc_node *)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3321 | 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] | 3322 | 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] | 3323 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3324 | LY_LIST_FOR(action_p->input.data, child_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3325 | 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] | 3326 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3327 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3328 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3329 | |
| 3330 | /* output */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3331 | lysc_update_path(ctx, (struct lysc_node *)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3332 | 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] | 3333 | 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] | 3334 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3335 | LY_LIST_FOR(action_p->output.data, child_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3336 | 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] | 3337 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3338 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3339 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3340 | |
| 3341 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3342 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3343 | ret = ly_set_add(&ctx->xpath, action, 0, NULL); |
| 3344 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3345 | } |
| 3346 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3347 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3348 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3349 | return ret; |
| 3350 | } |
| 3351 | |
| 3352 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3353 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3354 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3355 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3356 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3357 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3358 | * @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] | 3359 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3360 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3361 | */ |
| 3362 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3363 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3364 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3365 | { |
| 3366 | LY_ERR ret = LY_SUCCESS; |
| 3367 | struct lysp_node *child_p; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3368 | LY_ARRAY_COUNT_TYPE u; |
| 3369 | uint32_t opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3370 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3371 | lysc_update_path(ctx, parent, notif_p->name); |
| 3372 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3373 | /* member needed for uniqueness check lys_getnext() */ |
| 3374 | notif->nodetype = LYS_NOTIF; |
| 3375 | notif->module = ctx->mod; |
| 3376 | notif->parent = parent; |
| 3377 | |
| 3378 | 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] | 3379 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3380 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3381 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3382 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3383 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3384 | return LY_EVALID; |
| 3385 | } |
| 3386 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3387 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3388 | notif->sp = notif_p; |
| 3389 | } |
| 3390 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3391 | |
| 3392 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3393 | * 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] | 3394 | ret = lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0)); |
| 3395 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3396 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3397 | DUP_STRING_GOTO(ctx->ctx, notif_p->name, notif->name, ret, cleanup); |
| 3398 | DUP_STRING_GOTO(ctx->ctx, notif_p->dsc, notif->dsc, ret, cleanup); |
| 3399 | DUP_STRING_GOTO(ctx->ctx, notif_p->ref, notif->ref, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3400 | 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] | 3401 | 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] | 3402 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3403 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3404 | ret = ly_set_add(&ctx->xpath, notif, 0, NULL); |
| 3405 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3406 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3407 | 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] | 3408 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3409 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3410 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3411 | ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status); |
| 3412 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3413 | } |
| 3414 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3415 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3416 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3417 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3418 | return ret; |
| 3419 | } |
| 3420 | |
| 3421 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3422 | * @brief Compile parsed container node information. |
| 3423 | * @param[in] ctx Compile context |
| 3424 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3425 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3426 | * is enriched with the container-specific information. |
| 3427 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3428 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3429 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3430 | 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] | 3431 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3432 | struct lysp_node_container *cont_p = (struct lysp_node_container *)node_p; |
| 3433 | struct lysc_node_container *cont = (struct lysc_node_container *)node; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3434 | struct lysp_node *child_p; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3435 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3436 | LY_ERR ret = LY_SUCCESS; |
| 3437 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3438 | if (cont_p->presence) { |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3439 | /* explicit presence */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3440 | cont->flags |= LYS_PRESENCE; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3441 | } else if (cont_p->musts) { |
| 3442 | /* container with a must condition */ |
Radek Krejci | 175f25b | 2020-08-13 12:02:36 +0200 | [diff] [blame] | 3443 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name); |
| 3444 | cont->flags |= LYS_PRESENCE; |
| 3445 | } else if (cont_p->when) { |
| 3446 | /* container with a when condition */ |
| 3447 | 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] | 3448 | cont->flags |= LYS_PRESENCE; |
| 3449 | } else if (cont_p->parent) { |
| 3450 | if (cont_p->parent->nodetype == LYS_CHOICE) { |
| 3451 | /* 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] | 3452 | 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] | 3453 | cont_p->name, cont_p->parent->name); |
| 3454 | cont->flags |= LYS_PRESENCE; |
| 3455 | } else if ((cont_p->parent->nodetype == LYS_CASE) |
| 3456 | && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) { |
| 3457 | /* 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] | 3458 | 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] | 3459 | cont_p->name, cont_p->parent->name); |
| 3460 | cont->flags |= LYS_PRESENCE; |
| 3461 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3462 | } |
| 3463 | |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3464 | /* more cases when the container has meaning but is kept NP for convenience: |
| 3465 | * - when condition |
| 3466 | * - direct child action/notification |
| 3467 | */ |
| 3468 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3469 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3470 | ret = lys_compile_node(ctx, child_p, node, 0); |
| 3471 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3472 | } |
| 3473 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3474 | 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] | 3475 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3476 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3477 | ret = ly_set_add(&ctx->xpath, cont, 0, NULL); |
| 3478 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3479 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3480 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3481 | 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] | 3482 | |
| 3483 | done: |
| 3484 | return ret; |
| 3485 | } |
| 3486 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3487 | /* |
| 3488 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3489 | * @param[in] ctx Compile context. |
| 3490 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3491 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3492 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3493 | * @return LY_ERR value. |
| 3494 | */ |
| 3495 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3496 | 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] | 3497 | struct lysc_node_leaf *leaf) |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3498 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3499 | const char *dflt; |
| 3500 | struct lys_module *dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3501 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3502 | 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] | 3503 | leaf->units ? NULL : &leaf->units, &dflt, &dflt_mod)); |
| 3504 | |
| 3505 | /* store default value, if any */ |
| 3506 | if (dflt && !(leaf->flags & LYS_SET_DFLT)) { |
| 3507 | 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] | 3508 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3509 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3510 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3511 | /* 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] | 3512 | LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3513 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3514 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3515 | LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) { |
| 3516 | 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] | 3517 | /* 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] | 3518 | LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3519 | } |
| 3520 | } |
| 3521 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3522 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3523 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3524 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3525 | return LY_EVALID; |
| 3526 | } |
| 3527 | } |
| 3528 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3529 | return LY_SUCCESS; |
| 3530 | } |
| 3531 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3532 | /** |
| 3533 | * @brief Compile parsed leaf node information. |
| 3534 | * @param[in] ctx Compile context |
| 3535 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3536 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3537 | * is enriched with the leaf-specific information. |
| 3538 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3539 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3540 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3541 | 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] | 3542 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3543 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)node_p; |
| 3544 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 3545 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3546 | LY_ERR ret = LY_SUCCESS; |
| 3547 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3548 | 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] | 3549 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3550 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3551 | ret = ly_set_add(&ctx->xpath, leaf, 0, NULL); |
| 3552 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3553 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3554 | if (leaf_p->units) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3555 | 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] | 3556 | leaf->flags |= LYS_SET_UNITS; |
| 3557 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3558 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3559 | /* compile type */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3560 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
| 3561 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3562 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3563 | /* store/update default value */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3564 | if (leaf_p->dflt) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3565 | 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] | 3566 | leaf->flags |= LYS_SET_DFLT; |
| 3567 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3568 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3569 | done: |
| 3570 | return ret; |
| 3571 | } |
| 3572 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3573 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3574 | * @brief Compile parsed leaf-list node information. |
| 3575 | * @param[in] ctx Compile context |
| 3576 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3577 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3578 | * is enriched with the leaf-list-specific information. |
| 3579 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3580 | */ |
| 3581 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3582 | 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] | 3583 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3584 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)node_p; |
| 3585 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3586 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3587 | LY_ERR ret = LY_SUCCESS; |
| 3588 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3589 | 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] | 3590 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3591 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3592 | ret = ly_set_add(&ctx->xpath, llist, 0, NULL); |
| 3593 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3594 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3595 | if (llist_p->units) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3596 | 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] | 3597 | llist->flags |= LYS_SET_UNITS; |
| 3598 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3599 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3600 | /* compile type */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3601 | ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf *)llist); |
| 3602 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 6a044b2 | 2020-01-15 12:25:39 +0100 | [diff] [blame] | 3603 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3604 | /* store/update default values */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3605 | if (llist_p->dflts) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3606 | 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] | 3607 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3608 | } |
| 3609 | |
| 3610 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3611 | if (llist->min) { |
| 3612 | llist->flags |= LYS_MAND_TRUE; |
| 3613 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3614 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3615 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3616 | done: |
| 3617 | return ret; |
| 3618 | } |
| 3619 | |
| 3620 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3621 | * @brief Compile information about list's uniques. |
| 3622 | * @param[in] ctx Compile context. |
| 3623 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3624 | * @param[in] uniques Sized array list of unique statements. |
| 3625 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3626 | * @return LY_ERR value. |
| 3627 | */ |
| 3628 | static LY_ERR |
| 3629 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3630 | { |
| 3631 | LY_ERR ret = LY_SUCCESS; |
| 3632 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3633 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3634 | const char *keystr, *delim; |
| 3635 | size_t len; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3636 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3637 | 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] | 3638 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3639 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3640 | for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3641 | config = -1; |
| 3642 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3643 | keystr = uniques[v]; |
| 3644 | while (keystr) { |
| 3645 | delim = strpbrk(keystr, " \t\n"); |
| 3646 | if (delim) { |
| 3647 | len = delim - keystr; |
| 3648 | while (isspace(*delim)) { |
| 3649 | ++delim; |
| 3650 | } |
| 3651 | } else { |
| 3652 | len = strlen(keystr); |
| 3653 | } |
| 3654 | |
| 3655 | /* unique node must be present */ |
| 3656 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3657 | ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, context_module, LYS_LEAF, 0, |
| 3658 | (const struct lysc_node **)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3659 | if (ret != LY_SUCCESS) { |
| 3660 | if (ret == LY_EDENIED) { |
| 3661 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3662 | "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] | 3663 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3664 | } |
| 3665 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3666 | } else if (flags) { |
| 3667 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3668 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3669 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3670 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | /* all referenced leafs must be of the same config type */ |
| 3674 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3675 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3676 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3677 | return LY_EVALID; |
| 3678 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3679 | config = 1; |
| 3680 | } else { /* LYS_CONFIG_R */ |
| 3681 | config = 0; |
| 3682 | } |
| 3683 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3684 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3685 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3686 | if (parent->nodetype == LYS_LIST) { |
| 3687 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3688 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3689 | return LY_EVALID; |
| 3690 | } |
| 3691 | } |
| 3692 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3693 | /* check status */ |
| 3694 | 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] | 3695 | (*key)->flags, (*key)->module, (*key)->name)); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3696 | |
| 3697 | /* mark leaf as unique */ |
| 3698 | (*key)->flags |= LYS_UNIQUE; |
| 3699 | |
| 3700 | /* next unique value in line */ |
| 3701 | keystr = delim; |
| 3702 | } |
| 3703 | /* next unique definition */ |
| 3704 | } |
| 3705 | |
| 3706 | return LY_SUCCESS; |
| 3707 | } |
| 3708 | |
| 3709 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3710 | * @brief Compile parsed list node information. |
| 3711 | * @param[in] ctx Compile context |
| 3712 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3713 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3714 | * is enriched with the list-specific information. |
| 3715 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3716 | */ |
| 3717 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3718 | 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] | 3719 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3720 | struct lysp_node_list *list_p = (struct lysp_node_list *)node_p; |
| 3721 | struct lysc_node_list *list = (struct lysc_node_list *)node; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3722 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3723 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3724 | size_t len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3725 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3726 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3727 | LY_ERR ret = LY_SUCCESS; |
| 3728 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3729 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3730 | if (list->min) { |
| 3731 | list->flags |= LYS_MAND_TRUE; |
| 3732 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3733 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3734 | |
| 3735 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3736 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3737 | } |
| 3738 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3739 | 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] | 3740 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3741 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3742 | LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL)); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3743 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3744 | |
| 3745 | /* keys */ |
| 3746 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3747 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3748 | return LY_EVALID; |
| 3749 | } |
| 3750 | |
| 3751 | /* find all the keys (must be direct children) */ |
| 3752 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3753 | if (!keystr) { |
| 3754 | /* keyless list */ |
| 3755 | list->flags |= LYS_KEYLESS; |
| 3756 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3757 | while (keystr) { |
| 3758 | delim = strpbrk(keystr, " \t\n"); |
| 3759 | if (delim) { |
| 3760 | len = delim - keystr; |
| 3761 | while (isspace(*delim)) { |
| 3762 | ++delim; |
| 3763 | } |
| 3764 | } else { |
| 3765 | len = strlen(keystr); |
| 3766 | } |
| 3767 | |
| 3768 | /* key node must be present */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3769 | 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] | 3770 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3771 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3772 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3773 | return LY_EVALID; |
| 3774 | } |
| 3775 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3776 | if (key->flags & LYS_KEY) { |
| 3777 | /* the node was already marked as a key */ |
| 3778 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3779 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3780 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3781 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3782 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3783 | lysc_update_path(ctx, (struct lysc_node *)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3784 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3785 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3786 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3787 | return LY_EVALID; |
| 3788 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3789 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3790 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3791 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3792 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3793 | "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] | 3794 | return LY_EVALID; |
| 3795 | } |
| 3796 | } else { |
| 3797 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3798 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3799 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3800 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3801 | return LY_EVALID; |
| 3802 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3803 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3804 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3805 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3806 | return LY_EVALID; |
| 3807 | } |
| 3808 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3809 | |
| 3810 | /* check status */ |
| 3811 | 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] | 3812 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3813 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3814 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3815 | if (key->dflt) { |
| 3816 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3817 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3818 | free(key->dflt); |
| 3819 | key->dflt = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3820 | } |
| 3821 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3822 | key->flags |= LYS_KEY; |
| 3823 | |
| 3824 | /* move it to the correct position */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3825 | 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] | 3826 | /* fix links in closest previous siblings of the key */ |
| 3827 | if (key->next) { |
| 3828 | key->next->prev = key->prev; |
| 3829 | } else { |
| 3830 | /* last child */ |
| 3831 | list->child->prev = key->prev; |
| 3832 | } |
| 3833 | if (key->prev->next) { |
| 3834 | key->prev->next = key->next; |
| 3835 | } |
| 3836 | /* fix links in the key */ |
| 3837 | if (prev_key) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3838 | key->prev = (struct lysc_node *)prev_key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3839 | key->next = prev_key->next; |
| 3840 | } else { |
| 3841 | key->prev = list->child->prev; |
| 3842 | key->next = list->child; |
| 3843 | } |
| 3844 | /* fix links in closes future siblings of the key */ |
| 3845 | if (prev_key) { |
| 3846 | if (prev_key->next) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3847 | prev_key->next->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3848 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3849 | list->child->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3850 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3851 | prev_key->next = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3852 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3853 | list->child->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3854 | } |
| 3855 | /* fix links in parent */ |
| 3856 | if (!key->prev->next) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3857 | list->child = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3858 | } |
| 3859 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3860 | |
| 3861 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3862 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3863 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3864 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | /* uniques */ |
| 3868 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3869 | 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] | 3870 | } |
| 3871 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3872 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3873 | 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] | 3874 | |
| 3875 | done: |
| 3876 | return ret; |
| 3877 | } |
| 3878 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3879 | /** |
| 3880 | * @brief Do some checks and set the default choice's case. |
| 3881 | * |
| 3882 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3883 | * |
| 3884 | * @param[in] ctx Compile context. |
| 3885 | * @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, |
| 3886 | * not the reference to the imported module. |
| 3887 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3888 | * @return LY_ERR value. |
| 3889 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3890 | static LY_ERR |
| 3891 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3892 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3893 | struct lysc_node *iter, *node = (struct lysc_node *)ch; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3894 | const char *prefix = NULL, *name; |
| 3895 | size_t prefix_len = 0; |
| 3896 | |
| 3897 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3898 | name = strchr(dflt, ':'); |
| 3899 | if (name) { |
| 3900 | prefix = dflt; |
| 3901 | prefix_len = name - prefix; |
| 3902 | ++name; |
| 3903 | } else { |
| 3904 | name = dflt; |
| 3905 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 3906 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3907 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3908 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3909 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3910 | prefix_len, prefix); |
| 3911 | return LY_EVALID; |
| 3912 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3913 | 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] | 3914 | if (!ch->dflt) { |
| 3915 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3916 | "Default case \"%s\" not found.", dflt); |
| 3917 | return LY_EVALID; |
| 3918 | } |
| 3919 | /* no mandatory nodes directly under the default case */ |
| 3920 | LY_LIST_FOR(ch->dflt->child, iter) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3921 | if (iter->parent != (struct lysc_node *)ch->dflt) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3922 | break; |
| 3923 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3924 | if (iter->flags & LYS_MAND_TRUE) { |
| 3925 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3926 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3927 | return LY_EVALID; |
| 3928 | } |
| 3929 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3930 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3931 | return LY_SUCCESS; |
| 3932 | } |
| 3933 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3934 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3935 | 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] | 3936 | { |
| 3937 | struct lys_module *mod; |
| 3938 | const char *prefix = NULL, *name; |
| 3939 | size_t prefix_len = 0; |
| 3940 | struct lysc_node_case *cs; |
| 3941 | struct lysc_node *node; |
| 3942 | |
| 3943 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3944 | name = strchr(dflt, ':'); |
| 3945 | if (name) { |
| 3946 | prefix = dflt; |
| 3947 | prefix_len = name - prefix; |
| 3948 | ++name; |
| 3949 | } else { |
| 3950 | name = dflt; |
| 3951 | } |
| 3952 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3953 | if (prefix) { |
| 3954 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3955 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3956 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3957 | "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] | 3958 | return LY_EVALID; |
| 3959 | } |
| 3960 | } else { |
| 3961 | mod = ctx->mod; |
| 3962 | } |
| 3963 | /* get the default case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3964 | 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] | 3965 | if (!cs) { |
| 3966 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3967 | "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] | 3968 | return LY_EVALID; |
| 3969 | } |
| 3970 | |
| 3971 | /* check that there is no mandatory node */ |
| 3972 | LY_LIST_FOR(cs->child, node) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3973 | if (node->parent != (struct lysc_node *)cs) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3974 | break; |
| 3975 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3976 | if (node->flags & LYS_MAND_TRUE) { |
| 3977 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3978 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 3979 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3980 | return LY_EVALID; |
| 3981 | } |
| 3982 | } |
| 3983 | |
| 3984 | /* set the default case in choice */ |
| 3985 | ch->dflt = cs; |
| 3986 | cs->flags |= LYS_SET_DFLT; |
| 3987 | |
| 3988 | return LY_SUCCESS; |
| 3989 | } |
| 3990 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3991 | /** |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3992 | * @brief Compile choice children. |
| 3993 | * |
| 3994 | * @param[in] ctx Compile context |
| 3995 | * @param[in] child_p Parsed choice children nodes. |
| 3996 | * @param[in] node Compiled choice node to compile and add children to. |
| 3997 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3998 | */ |
| 3999 | static LY_ERR |
| 4000 | lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node) |
| 4001 | { |
| 4002 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 8f5fad2 | 2020-09-15 16:50:54 +0200 | [diff] [blame] | 4003 | struct lysp_node *child_p_next = child_p->next; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4004 | struct lysp_node_case *cs_p; |
| 4005 | |
| 4006 | if (child_p->nodetype == LYS_CASE) { |
| 4007 | /* standard case under choice */ |
| 4008 | ret = lys_compile_node(ctx, child_p, node, 0); |
| 4009 | } else { |
| 4010 | /* we need the implicit case first, so create a fake parsed case */ |
| 4011 | cs_p = calloc(1, sizeof *cs_p); |
| 4012 | cs_p->nodetype = LYS_CASE; |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 4013 | DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4014 | cs_p->child = child_p; |
| 4015 | |
| 4016 | /* make the child the only case child */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4017 | child_p->next = NULL; |
| 4018 | |
| 4019 | /* compile it normally */ |
| 4020 | ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0); |
| 4021 | |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 4022 | free_fake_node: |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4023 | /* free the fake parsed node and correct pointers back */ |
| 4024 | cs_p->child = NULL; |
| 4025 | lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p); |
Radek Krejci | 8f5fad2 | 2020-09-15 16:50:54 +0200 | [diff] [blame] | 4026 | child_p->next = child_p_next; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | return ret; |
| 4030 | } |
| 4031 | |
| 4032 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4033 | * @brief Compile parsed choice node information. |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4034 | * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4035 | * @param[in] ctx Compile context |
| 4036 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4037 | * @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] | 4038 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4039 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4040 | */ |
| 4041 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4042 | 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] | 4043 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4044 | struct lysp_node_choice *ch_p = (struct lysp_node_choice *)node_p; |
| 4045 | struct lysc_node_choice *ch = (struct lysc_node_choice *)node; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4046 | struct lysp_node *child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4047 | LY_ERR ret = LY_SUCCESS; |
| 4048 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4049 | assert(node->nodetype == LYS_CHOICE); |
| 4050 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4051 | LY_LIST_FOR(ch_p->child, child_p) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4052 | LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4053 | } |
| 4054 | |
| 4055 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4056 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4057 | 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] | 4058 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4059 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4060 | return ret; |
| 4061 | } |
| 4062 | |
| 4063 | /** |
| 4064 | * @brief Compile parsed anydata or anyxml node information. |
| 4065 | * @param[in] ctx Compile context |
| 4066 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4067 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4068 | * is enriched with the any-specific information. |
| 4069 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4070 | */ |
| 4071 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4072 | 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] | 4073 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4074 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)node_p; |
| 4075 | struct lysc_node_anydata *any = (struct lysc_node_anydata *)node; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4076 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4077 | LY_ERR ret = LY_SUCCESS; |
| 4078 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4079 | 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] | 4080 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4081 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4082 | ret = ly_set_add(&ctx->xpath, any, 0, NULL); |
| 4083 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4084 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4085 | |
| 4086 | if (any->flags & LYS_CONFIG_W) { |
Radek Krejci | 5c4ed7b | 2020-08-12 11:29:44 +0200 | [diff] [blame] | 4087 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s", |
| 4088 | 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] | 4089 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4090 | done: |
| 4091 | return ret; |
| 4092 | } |
| 4093 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4094 | /** |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4095 | * @brief Connect the node into the siblings list and check its name uniqueness. Also, |
| 4096 | * keep specific order of augments targetting the same node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4097 | * |
| 4098 | * @param[in] ctx Compile context |
| 4099 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4100 | * the choice itself is expected instead of a specific case node. |
| 4101 | * @param[in] node Schema node to connect into the list. |
| 4102 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4103 | * 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] | 4104 | */ |
| 4105 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4106 | 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] | 4107 | { |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4108 | struct lysc_node **children, *start, *end; |
| 4109 | const struct lys_module *mod; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4110 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4111 | node->parent = parent; |
| 4112 | |
| 4113 | if (parent) { |
| 4114 | if (parent->nodetype == LYS_CHOICE) { |
| 4115 | assert(node->nodetype == LYS_CASE); |
| 4116 | children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases; |
| 4117 | } else { |
| 4118 | children = lysc_node_children_p(parent, ctx->options); |
| 4119 | } |
| 4120 | assert(children); |
| 4121 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4122 | if (!(*children)) { |
| 4123 | /* first child */ |
| 4124 | *children = node; |
| 4125 | } else if (*children != node) { |
| 4126 | /* by the condition in previous branch we cover the choice/case children |
| 4127 | * - the children list is shared by the choice and the the first case, in addition |
| 4128 | * the first child of each case must be referenced from the case node. So the node is |
| 4129 | * actually always already inserted in case it is the first children - so here such |
| 4130 | * a situation actually corresponds to the first branch */ |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4131 | if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module) |
| 4132 | && (strcmp((*children)->prev->module->name, node->module->name) > 0)) { |
| 4133 | /* some augments are already connected and we are connecting new ones, |
| 4134 | * keep module name order and insert the node into the children list */ |
| 4135 | end = (*children); |
| 4136 | do { |
| 4137 | end = end->prev; |
| 4138 | mod = end->module; |
| 4139 | while (end->prev->module == mod) { |
| 4140 | end = end->prev; |
| 4141 | } |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 4142 | } 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] | 4143 | |
| 4144 | /* we have the last existing node after our node, easily get the first before and connect it */ |
| 4145 | start = end->prev; |
| 4146 | start->next = node; |
| 4147 | node->next = end; |
| 4148 | end->prev = node; |
| 4149 | node->prev = start; |
| 4150 | } else { |
| 4151 | /* insert at the end of the parent's children list */ |
| 4152 | (*children)->prev->next = node; |
| 4153 | node->prev = (*children)->prev; |
| 4154 | (*children)->prev = node; |
| 4155 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4156 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4157 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4158 | /* check the name uniqueness (even for an only child, it may be in case) */ |
| 4159 | if (lys_compile_node_uniqness(ctx, parent, node->name, node)) { |
| 4160 | return LY_EEXIST; |
| 4161 | } |
| 4162 | } else { |
| 4163 | /* top-level element */ |
| 4164 | if (!ctx->mod->compiled->data) { |
| 4165 | ctx->mod->compiled->data = node; |
| 4166 | } else { |
| 4167 | /* insert at the end of the module's top-level nodes list */ |
| 4168 | ctx->mod->compiled->data->prev->next = node; |
| 4169 | node->prev = ctx->mod->compiled->data->prev; |
| 4170 | ctx->mod->compiled->data->prev = node; |
| 4171 | } |
| 4172 | |
| 4173 | /* check the name uniqueness on top-level */ |
| 4174 | if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) { |
| 4175 | return LY_EEXIST; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4176 | } |
| 4177 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4178 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4179 | return LY_SUCCESS; |
| 4180 | } |
| 4181 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4182 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4183 | * @brief Prepare the case structure in choice node for the new data node. |
| 4184 | * |
| 4185 | * 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 |
| 4186 | * created in the choice when the first child was processed. |
| 4187 | * |
| 4188 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4189 | * @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] | 4190 | * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4191 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4192 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4193 | * @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, |
| 4194 | * 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] | 4195 | */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4196 | static LY_ERR |
| 4197 | 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] | 4198 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4199 | struct lysp_node *child_p; |
| 4200 | struct lysp_node_case *cs_p = (struct lysp_node_case *)node_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4201 | |
Radek Krejci | 3942480 | 2020-08-12 09:31:00 +0200 | [diff] [blame] | 4202 | if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4203 | /* we have to add an implicit case node into the parent choice */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4204 | } else if (node_p->nodetype == LYS_CASE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4205 | /* explicit parent case */ |
| 4206 | LY_LIST_FOR(cs_p->child, child_p) { |
| 4207 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4208 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4209 | } else { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4210 | LOGINT_RET(ctx->ctx); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4211 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4212 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4213 | return LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4214 | } |
| 4215 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4216 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4217 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4218 | * |
| 4219 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4220 | * @param[in] node Target node where the config is supposed to be changed. |
| 4221 | * @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] | 4222 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4223 | * 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] | 4224 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4225 | * @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] | 4226 | * @return LY_ERR value. |
| 4227 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4228 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4229 | 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] | 4230 | ly_bool inheriting, ly_bool refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4231 | { |
| 4232 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4233 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4234 | |
| 4235 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4236 | /* nothing to do */ |
| 4237 | return LY_SUCCESS; |
| 4238 | } |
| 4239 | |
| 4240 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4241 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4242 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4243 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4244 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4245 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4246 | return LY_EVALID; |
| 4247 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4248 | node->flags |= LYS_SET_CONFIG; |
| 4249 | } else { |
| 4250 | if (node->flags & LYS_SET_CONFIG) { |
| 4251 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4252 | /* setting config flags, but have node with explicit config true */ |
| 4253 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4254 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4255 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4256 | return LY_EVALID; |
| 4257 | } |
| 4258 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4259 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4260 | return LY_SUCCESS; |
| 4261 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4262 | } |
| 4263 | node->flags &= ~LYS_CONFIG_MASK; |
| 4264 | node->flags |= config; |
| 4265 | |
| 4266 | /* inherit the change into the children */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4267 | LY_LIST_FOR((struct lysc_node *)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4268 | 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] | 4269 | } |
| 4270 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4271 | return LY_SUCCESS; |
| 4272 | } |
| 4273 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4274 | /** |
| 4275 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4276 | * |
| 4277 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4278 | * the flag to such parents from a mandatory children. |
| 4279 | * |
| 4280 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4281 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4282 | * (mandatory children was removed). |
| 4283 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4284 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4285 | lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add) |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4286 | { |
| 4287 | struct lysc_node *iter; |
| 4288 | |
| 4289 | if (add) { /* set flag */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4290 | 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] | 4291 | parent = parent->parent) { |
| 4292 | parent->flags |= LYS_MAND_TRUE; |
| 4293 | } |
| 4294 | } else { /* unset flag */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4295 | 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] | 4296 | 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] | 4297 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4298 | /* there is another mandatory node */ |
| 4299 | return; |
| 4300 | } |
| 4301 | } |
| 4302 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4303 | parent->flags &= ~LYS_MAND_TRUE; |
| 4304 | } |
| 4305 | } |
| 4306 | } |
| 4307 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4308 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4309 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4310 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4311 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4312 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4313 | */ |
| 4314 | static void |
| 4315 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4316 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4317 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4318 | size_t len; |
| 4319 | |
| 4320 | len = strlen(aug_p->nodeid); |
| 4321 | LY_ARRAY_FOR(result, v) { |
| 4322 | if (strlen(result[v]->nodeid) <= len) { |
| 4323 | continue; |
| 4324 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4325 | if (v < LY_ARRAY_COUNT(result)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4326 | /* move the rest of array */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4327 | 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] | 4328 | break; |
| 4329 | } |
| 4330 | } |
| 4331 | result[v] = aug_p; |
| 4332 | LY_ARRAY_INCREMENT(result); |
| 4333 | } |
| 4334 | |
| 4335 | /** |
| 4336 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4337 | * |
| 4338 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4339 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4340 | * |
| 4341 | * @param[in] ctx Compile context. |
| 4342 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4343 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4344 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4345 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4346 | * @return LY_ERR value. |
| 4347 | */ |
| 4348 | LY_ERR |
| 4349 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4350 | { |
| 4351 | struct lysp_augment **result = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4352 | LY_ARRAY_COUNT_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4353 | |
| 4354 | assert(augments); |
| 4355 | |
| 4356 | /* get count of the augments in module and all its submodules */ |
| 4357 | if (aug_p) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4358 | count += LY_ARRAY_COUNT(aug_p); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4359 | } |
| 4360 | LY_ARRAY_FOR(inc_p, u) { |
| 4361 | if (inc_p[u].submodule->augments) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4362 | count += LY_ARRAY_COUNT(inc_p[u].submodule->augments); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4363 | } |
| 4364 | } |
| 4365 | |
| 4366 | if (!count) { |
| 4367 | *augments = NULL; |
| 4368 | return LY_SUCCESS; |
| 4369 | } |
| 4370 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4371 | |
| 4372 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4373 | * together, so there can be even /z/y betwwen them. */ |
| 4374 | LY_ARRAY_FOR(aug_p, u) { |
| 4375 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4376 | } |
| 4377 | LY_ARRAY_FOR(inc_p, u) { |
| 4378 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4379 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4380 | } |
| 4381 | } |
| 4382 | |
| 4383 | *augments = result; |
| 4384 | return LY_SUCCESS; |
| 4385 | } |
| 4386 | |
| 4387 | /** |
| 4388 | * @brief Compile the parsed augment connecting it into its target. |
| 4389 | * |
| 4390 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4391 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4392 | * are already implemented and compiled. |
| 4393 | * |
| 4394 | * @param[in] ctx Compile context. |
| 4395 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4396 | * @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 |
| 4397 | * children in case of the augmenting uses data. |
| 4398 | * @return LY_SUCCESS on success. |
| 4399 | * @return LY_EVALID on failure. |
| 4400 | */ |
| 4401 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4402 | 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] | 4403 | { |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4404 | LY_ERR ret = LY_SUCCESS, rc; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4405 | struct lysp_node *node_p; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4406 | struct lysc_node *target; /* target target of the augment */ |
| 4407 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4408 | struct lysc_when **when, *when_shared; |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4409 | struct lys_module **aug_mod; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4410 | ly_bool allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4411 | uint16_t flags = 0; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4412 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4413 | uint32_t opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4414 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4415 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4416 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4417 | |
Michal Vasko | cb18c7f | 2020-08-24 09:22:28 +0200 | [diff] [blame] | 4418 | 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] | 4419 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4420 | 1, (const struct lysc_node **)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4421 | if (ret != LY_SUCCESS) { |
| 4422 | if (ret == LY_EDENIED) { |
| 4423 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4424 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4425 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4426 | } |
| 4427 | return LY_EVALID; |
| 4428 | } |
| 4429 | |
| 4430 | /* check for mandatory nodes |
| 4431 | * - new cases augmenting some choice can have mandatory nodes |
| 4432 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4433 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4434 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4435 | allow_mandatory = 1; |
| 4436 | } |
| 4437 | |
| 4438 | when_shared = NULL; |
| 4439 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4440 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4441 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4442 | && !((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] | 4443 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4444 | && !(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] | 4445 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4446 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4447 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4448 | return LY_EVALID; |
| 4449 | } |
| 4450 | |
| 4451 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4452 | ctx->options |= flags; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4453 | if (target->nodetype == LYS_CHOICE) { |
| 4454 | LY_CHECK_RET(lys_compile_node_choice_child(ctx, node_p, target)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4455 | } else { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4456 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4457 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4458 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4459 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4460 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4461 | * 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] | 4462 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4463 | /* 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] | 4464 | 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] | 4465 | } else if (target->nodetype == LYS_CHOICE) { |
| 4466 | /* 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] | 4467 | node = ((struct lysc_node_choice *)target)->cases->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4468 | } else { |
| 4469 | /* the compiled node is the last child of the target */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4470 | node = (struct lysc_node *)lysc_node_children(target, flags); |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4471 | if (!node) { |
| 4472 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4473 | break; |
| 4474 | } |
| 4475 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4476 | } |
| 4477 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4478 | 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] | 4479 | node->flags &= ~LYS_MAND_TRUE; |
| 4480 | lys_compile_mandatory_parents(target, 0); |
| 4481 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4482 | "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] | 4483 | return LY_EVALID; |
| 4484 | } |
| 4485 | |
| 4486 | /* pass augment's when to all the children */ |
| 4487 | if (aug_p->when) { |
| 4488 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4489 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4490 | 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] | 4491 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4492 | |
| 4493 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4494 | /* do not check "when" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4495 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 4496 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4497 | } |
| 4498 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4499 | when_shared = *when; |
| 4500 | } else { |
| 4501 | ++when_shared->refcount; |
| 4502 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4503 | |
| 4504 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4505 | /* 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] | 4506 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 4507 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4508 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4509 | } |
| 4510 | } |
| 4511 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4512 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4513 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4514 | switch (target->nodetype) { |
| 4515 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4516 | 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] | 4517 | u, lys_compile_action, 0, ret, error); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4518 | 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] | 4519 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4520 | break; |
| 4521 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4522 | 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] | 4523 | u, lys_compile_action, 0, ret, error); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4524 | 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] | 4525 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4526 | break; |
| 4527 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4528 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4529 | if (aug_p->actions) { |
| 4530 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4531 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4532 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4533 | return LY_EVALID; |
| 4534 | } |
| 4535 | if (aug_p->notifs) { |
| 4536 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4537 | "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] | 4538 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4539 | return LY_EVALID; |
| 4540 | } |
| 4541 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4542 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4543 | /* add this module into the target module augmented_by, if not there already */ |
| 4544 | rc = LY_SUCCESS; |
| 4545 | LY_ARRAY_FOR(target->module->compiled->augmented_by, v) { |
| 4546 | if (target->module->compiled->augmented_by[v] == ctx->mod) { |
| 4547 | rc = LY_EEXIST; |
| 4548 | break; |
| 4549 | } |
| 4550 | } |
| 4551 | if (!rc) { |
| 4552 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error); |
| 4553 | *aug_mod = ctx->mod; |
| 4554 | } |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4555 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4556 | lysc_update_path(ctx, NULL, NULL); |
| 4557 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4558 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4559 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4560 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4561 | return ret; |
| 4562 | } |
| 4563 | |
| 4564 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4565 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4566 | * |
| 4567 | * @param[in] ctx Compile context. |
| 4568 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4569 | * @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] | 4570 | * @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] | 4571 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4572 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4573 | * 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] | 4574 | * @return LY_ERR value. |
| 4575 | */ |
| 4576 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4577 | 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] | 4578 | { |
| 4579 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4580 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4581 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4582 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4583 | return LY_EVALID; |
| 4584 | } |
| 4585 | |
| 4586 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4587 | /* check if node has default value */ |
| 4588 | if (node->nodetype & LYS_LEAF) { |
| 4589 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4590 | if (refine_flag) { |
| 4591 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4592 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4593 | return LY_EVALID; |
| 4594 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4595 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4596 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4597 | if (refine_flag) { |
| 4598 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4599 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4600 | return LY_EVALID; |
| 4601 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4602 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4603 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4604 | 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] | 4605 | return LY_EVALID; |
| 4606 | } |
| 4607 | |
| 4608 | node->flags &= ~LYS_MAND_FALSE; |
| 4609 | node->flags |= LYS_MAND_TRUE; |
| 4610 | lys_compile_mandatory_parents(node->parent, 1); |
| 4611 | } else { |
| 4612 | /* make mandatory false */ |
| 4613 | node->flags &= ~LYS_MAND_TRUE; |
| 4614 | node->flags |= LYS_MAND_FALSE; |
| 4615 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4616 | } |
| 4617 | return LY_SUCCESS; |
| 4618 | } |
| 4619 | |
| 4620 | /** |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4621 | * @brief Find grouping for a uses. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4622 | * |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4623 | * @param[in] ctx Compile context. |
| 4624 | * @param[in] uses_p Parsed uses node. |
| 4625 | * @param[out] gpr_p Found grouping on success. |
| 4626 | * @param[out] grp_mod Module of @p grp_p on success. |
| 4627 | * @return LY_ERR value. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4628 | */ |
| 4629 | static LY_ERR |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4630 | lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_grp **grp_p, |
| 4631 | struct lys_module **grp_mod) |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4632 | { |
| 4633 | struct lysp_node *node_p; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4634 | struct lysp_grp *grp; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4635 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4636 | ly_bool found = 0; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4637 | const char *id, *name, *prefix; |
| 4638 | size_t prefix_len, name_len; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4639 | struct lys_module *mod; |
| 4640 | |
| 4641 | *grp_p = NULL; |
| 4642 | *grp_mod = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4643 | |
| 4644 | /* search for the grouping definition */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4645 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4646 | 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] | 4647 | if (prefix) { |
| 4648 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4649 | if (!mod) { |
| 4650 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4651 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4652 | return LY_EVALID; |
| 4653 | } |
| 4654 | } else { |
| 4655 | mod = ctx->mod_def; |
| 4656 | } |
| 4657 | if (mod == ctx->mod_def) { |
| 4658 | 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] | 4659 | grp = (struct lysp_grp *)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4660 | LY_ARRAY_FOR(grp, u) { |
| 4661 | if (!strcmp(grp[u].name, name)) { |
| 4662 | grp = &grp[u]; |
| 4663 | found = 1; |
| 4664 | break; |
| 4665 | } |
| 4666 | } |
| 4667 | } |
| 4668 | } |
| 4669 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4670 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4671 | grp = mod->parsed->groupings; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4672 | LY_ARRAY_FOR(grp, u) { |
| 4673 | if (!strcmp(grp[u].name, name)) { |
| 4674 | grp = &grp[u]; |
| 4675 | found = 1; |
| 4676 | break; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4677 | } |
| 4678 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4679 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4680 | /* ... and all the submodules */ |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4681 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4682 | grp = mod->parsed->includes[u].submodule->groupings; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4683 | LY_ARRAY_FOR(grp, v) { |
| 4684 | if (!strcmp(grp[v].name, name)) { |
| 4685 | grp = &grp[v]; |
| 4686 | found = 1; |
| 4687 | break; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4688 | } |
| 4689 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4690 | if (found) { |
| 4691 | break; |
| 4692 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4693 | } |
| 4694 | } |
| 4695 | } |
| 4696 | if (!found) { |
| 4697 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4698 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4699 | return LY_EVALID; |
| 4700 | } |
| 4701 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4702 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4703 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4704 | grp->flags |= LYS_USED_GRP; |
| 4705 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4706 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4707 | *grp_p = grp; |
| 4708 | *grp_mod = mod; |
| 4709 | return LY_SUCCESS; |
| 4710 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4711 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4712 | static LY_ERR |
| 4713 | lys_compile_refines(struct lysc_ctx *ctx, struct lysp_refine *refines, const struct lysc_node *context_node) |
| 4714 | { |
| 4715 | struct lysc_node *node; |
| 4716 | LY_ARRAY_COUNT_TYPE u; |
| 4717 | struct lysp_refine *rfn; |
| 4718 | LY_ERR ret = LY_SUCCESS; |
| 4719 | uint32_t min, max; |
| 4720 | uint16_t flags; |
| 4721 | struct ly_set refined = {0}; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4722 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4723 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4724 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4725 | /* apply refine */ |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4726 | LY_ARRAY_FOR(refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4727 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4728 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4729 | 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] | 4730 | 0, 0, (const struct lysc_node **)&node, &flags); |
| 4731 | LY_CHECK_GOTO(ret, cleanup); |
| 4732 | ret = ly_set_add(&refined, node, LY_SET_OPT_USEASLIST, NULL); |
| 4733 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4734 | |
| 4735 | /* default value */ |
| 4736 | if (rfn->dflts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4737 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4738 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4739 | "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] | 4740 | lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4741 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4742 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4743 | } |
| 4744 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4745 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4746 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4747 | lys_nodetype2str(node->nodetype)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4748 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4749 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4750 | } |
| 4751 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4752 | /* postpone default compilation when the tree is complete */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4753 | ret = lysc_incomplete_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0], ctx->mod_def); |
| 4754 | LY_CHECK_GOTO(ret, cleanup); |
| 4755 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4756 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4757 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4758 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4759 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4760 | "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] | 4761 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4762 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4763 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4764 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4765 | /* postpone default compilation when the tree is complete */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4766 | ret = lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts, ctx->mod_def); |
| 4767 | LY_CHECK_GOTO(ret, cleanup); |
| 4768 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4769 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4770 | } else if (node->nodetype == LYS_CHOICE) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4771 | if (((struct lysc_node_choice *)node)->dflt) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4772 | /* unset LYS_SET_DFLT from the current default case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4773 | ((struct lysc_node_choice *)node)->dflt->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4774 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4775 | ret = lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice *)node); |
| 4776 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4777 | } |
| 4778 | } |
| 4779 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4780 | /* description */ |
| 4781 | if (rfn->dsc) { |
| 4782 | FREE_STRING(ctx->ctx, node->dsc); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 4783 | 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] | 4784 | } |
| 4785 | |
| 4786 | /* reference */ |
| 4787 | if (rfn->ref) { |
| 4788 | FREE_STRING(ctx->ctx, node->ref); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 4789 | 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] | 4790 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4791 | |
| 4792 | /* config */ |
| 4793 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4794 | if (!flags) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4795 | ret = lys_compile_change_config(ctx, node, rfn->flags, 0, 1); |
| 4796 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4797 | } else { |
| 4798 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4799 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4800 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4801 | } |
| 4802 | |
| 4803 | /* mandatory */ |
| 4804 | if (rfn->flags & LYS_MAND_MASK) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4805 | ret = lys_compile_change_mandatory(ctx, node, rfn->flags, 1); |
| 4806 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4807 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4808 | |
| 4809 | /* presence */ |
| 4810 | if (rfn->presence) { |
| 4811 | if (node->nodetype != LYS_CONTAINER) { |
| 4812 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4813 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4814 | lys_nodetype2str(node->nodetype)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4815 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4816 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4817 | } |
| 4818 | node->flags |= LYS_PRESENCE; |
| 4819 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4820 | |
| 4821 | /* must */ |
| 4822 | if (rfn->musts) { |
| 4823 | switch (node->nodetype) { |
| 4824 | case LYS_LEAF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4825 | 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] | 4826 | break; |
| 4827 | case LYS_LEAFLIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4828 | 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] | 4829 | break; |
| 4830 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4831 | 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] | 4832 | break; |
| 4833 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4834 | 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] | 4835 | break; |
| 4836 | case LYS_ANYXML: |
| 4837 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4838 | 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] | 4839 | break; |
| 4840 | default: |
| 4841 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4842 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 4843 | lys_nodetype2str(node->nodetype)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4844 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4845 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4846 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4847 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 4848 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4849 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4850 | |
| 4851 | /* min/max-elements */ |
| 4852 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4853 | switch (node->nodetype) { |
| 4854 | case LYS_LEAFLIST: |
| 4855 | if (rfn->flags & LYS_SET_MAX) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4856 | ((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] | 4857 | } |
| 4858 | if (rfn->flags & LYS_SET_MIN) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4859 | ((struct lysc_node_leaflist *)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4860 | if (rfn->min) { |
| 4861 | node->flags |= LYS_MAND_TRUE; |
| 4862 | lys_compile_mandatory_parents(node->parent, 1); |
| 4863 | } else { |
| 4864 | node->flags &= ~LYS_MAND_TRUE; |
| 4865 | lys_compile_mandatory_parents(node->parent, 0); |
| 4866 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4867 | } |
| 4868 | break; |
| 4869 | case LYS_LIST: |
| 4870 | if (rfn->flags & LYS_SET_MAX) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4871 | ((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] | 4872 | } |
| 4873 | if (rfn->flags & LYS_SET_MIN) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4874 | ((struct lysc_node_list *)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4875 | if (rfn->min) { |
| 4876 | node->flags |= LYS_MAND_TRUE; |
| 4877 | lys_compile_mandatory_parents(node->parent, 1); |
| 4878 | } else { |
| 4879 | node->flags &= ~LYS_MAND_TRUE; |
| 4880 | lys_compile_mandatory_parents(node->parent, 0); |
| 4881 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4882 | } |
| 4883 | break; |
| 4884 | default: |
| 4885 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4886 | "Invalid refine of %s statement - %s cannot hold this statement.", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4887 | (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] | 4888 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4889 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4890 | } |
| 4891 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4892 | |
| 4893 | /* if-feature */ |
| 4894 | if (rfn->iffeatures) { |
| 4895 | /* 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] | 4896 | 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] | 4897 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4898 | |
| 4899 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4900 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4901 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4902 | /* 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] | 4903 | for (uint32_t i = 0; i < refined.count; ++i) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4904 | node = (struct lysc_node *)refined.objs[i]; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4905 | rfn = &refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4906 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4907 | |
| 4908 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 4909 | if ((node->flags & LYS_MAND_TRUE) && |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4910 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) || |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4911 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 4912 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4913 | "Invalid refine of default - the node is mandatory."); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4914 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4915 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4916 | } |
| 4917 | |
| 4918 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4919 | if (node->nodetype == LYS_LIST) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4920 | min = ((struct lysc_node_list *)node)->min; |
| 4921 | max = ((struct lysc_node_list *)node)->max; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4922 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4923 | min = ((struct lysc_node_leaflist *)node)->min; |
| 4924 | max = ((struct lysc_node_leaflist *)node)->max; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4925 | } |
| 4926 | if (min > max) { |
| 4927 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4928 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4929 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4930 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4931 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4932 | } |
| 4933 | } |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4934 | |
| 4935 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4936 | } |
| 4937 | |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4938 | cleanup: |
| 4939 | ly_set_erase(&refined, NULL); |
| 4940 | lysc_update_path(ctx, NULL, NULL); |
| 4941 | return ret; |
| 4942 | } |
| 4943 | |
| 4944 | /** |
| 4945 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4946 | * If present, also apply uses's modificators. |
| 4947 | * |
| 4948 | * @param[in] ctx Compile context |
| 4949 | * @param[in] uses_p Parsed uses schema node. |
| 4950 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4951 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4952 | * the compile context. |
| 4953 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4954 | */ |
| 4955 | static LY_ERR |
| 4956 | lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p) |
| 4957 | { |
| 4958 | struct lysp_node *node_p; |
Radek Krejci | 8f5fad2 | 2020-09-15 16:50:54 +0200 | [diff] [blame] | 4959 | struct lysc_node *child = NULL, *iter; |
Michal Vasko | 601ddb3 | 2020-08-31 16:25:35 +0200 | [diff] [blame] | 4960 | /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */ |
| 4961 | struct lysc_node_container context_node_fake = |
| 4962 | {.nodetype = LYS_CONTAINER, |
| 4963 | .module = ctx->mod, |
| 4964 | .flags = parent ? parent->flags : 0, |
| 4965 | .child = NULL, .next = NULL, |
| 4966 | .prev = (struct lysc_node *)&context_node_fake, |
| 4967 | .actions = NULL, .notifs = NULL}; |
| 4968 | struct lysp_grp *grp = NULL; |
| 4969 | LY_ARRAY_COUNT_TYPE u; |
| 4970 | uint32_t grp_stack_count; |
| 4971 | struct lys_module *grp_mod, *mod_old; |
| 4972 | LY_ERR ret = LY_SUCCESS; |
| 4973 | struct lysc_when **when, *when_shared; |
| 4974 | struct lysp_augment **augments = NULL; |
| 4975 | LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0; |
| 4976 | struct lysc_notif **notifs = NULL; |
| 4977 | struct lysc_action **actions = NULL; |
| 4978 | |
| 4979 | /* find the referenced grouping */ |
| 4980 | LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod)); |
| 4981 | |
| 4982 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4983 | grp_stack_count = ctx->groupings.count; |
| 4984 | LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL)); |
| 4985 | if (grp_stack_count == ctx->groupings.count) { |
| 4986 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4987 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4988 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4989 | return LY_EVALID; |
| 4990 | } |
| 4991 | |
| 4992 | /* switch context's mod_def */ |
| 4993 | mod_old = ctx->mod_def; |
| 4994 | ctx->mod_def = grp_mod; |
| 4995 | |
| 4996 | /* check status */ |
| 4997 | ret = lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, grp_mod, grp->name); |
| 4998 | LY_CHECK_GOTO(ret, cleanup); |
| 4999 | |
| 5000 | /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly |
| 5001 | * applu refine and augment only to the nodes from the uses */ |
| 5002 | if (parent) { |
| 5003 | child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 5004 | } else if (ctx->mod->compiled->data) { |
| 5005 | child = ctx->mod->compiled->data; |
| 5006 | } else { |
| 5007 | child = NULL; |
| 5008 | } |
| 5009 | /* remember the last child */ |
| 5010 | if (child) { |
| 5011 | child = child->prev; |
| 5012 | } |
| 5013 | |
| 5014 | /* compile data nodes */ |
| 5015 | LY_LIST_FOR(grp->data, node_p) { |
| 5016 | /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */ |
| 5017 | ret = lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3); |
| 5018 | LY_CHECK_GOTO(ret, cleanup); |
| 5019 | } |
| 5020 | |
| 5021 | /* split the children and add the uses's data into the fake context node */ |
| 5022 | if (child) { |
| 5023 | context_node_fake.child = child->next; |
| 5024 | } else if (parent) { |
| 5025 | context_node_fake.child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 5026 | } else if (ctx->mod->compiled->data) { |
| 5027 | context_node_fake.child = ctx->mod->compiled->data; |
| 5028 | } |
| 5029 | if (context_node_fake.child) { |
| 5030 | /* remember child as the last data node added by grouping to fix the list later */ |
| 5031 | child = context_node_fake.child->prev; |
| 5032 | context_node_fake.child->prev = NULL; |
| 5033 | } |
| 5034 | |
| 5035 | when_shared = NULL; |
| 5036 | LY_LIST_FOR(context_node_fake.child, iter) { |
| 5037 | iter->parent = (struct lysc_node *)&context_node_fake; |
| 5038 | |
| 5039 | /* pass uses's when to all the data children, actions and notifications are ignored */ |
| 5040 | if (uses_p->when) { |
| 5041 | LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup); |
| 5042 | if (!when_shared) { |
| 5043 | ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when); |
| 5044 | LY_CHECK_GOTO(ret, cleanup); |
| 5045 | |
| 5046 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5047 | /* do not check "when" semantics in a grouping */ |
| 5048 | ret = ly_set_add(&ctx->xpath, iter, 0, NULL); |
| 5049 | LY_CHECK_GOTO(ret, cleanup); |
| 5050 | } |
| 5051 | |
| 5052 | when_shared = *when; |
| 5053 | } else { |
| 5054 | ++when_shared->refcount; |
| 5055 | (*when) = when_shared; |
| 5056 | |
| 5057 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5058 | /* in this case check "when" again for all children because of dummy node check */ |
| 5059 | ret = ly_set_add(&ctx->xpath, iter, 0, NULL); |
| 5060 | LY_CHECK_GOTO(ret, cleanup); |
| 5061 | } |
| 5062 | } |
| 5063 | } |
| 5064 | } |
| 5065 | |
| 5066 | /* compile actions */ |
| 5067 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 5068 | if (actions) { |
| 5069 | actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0; |
| 5070 | COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup); |
| 5071 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 5072 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 5073 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup); |
| 5074 | LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index; |
| 5075 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions); |
| 5076 | } |
| 5077 | } |
| 5078 | |
| 5079 | /* compile notifications */ |
| 5080 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 5081 | if (notifs) { |
| 5082 | notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0; |
| 5083 | COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup); |
| 5084 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 5085 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 5086 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup); |
| 5087 | LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index; |
| 5088 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs); |
| 5089 | } |
| 5090 | } |
| 5091 | |
| 5092 | /* sort and apply augments */ |
| 5093 | ret = lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments); |
| 5094 | LY_CHECK_GOTO(ret, cleanup); |
| 5095 | LY_ARRAY_FOR(augments, u) { |
| 5096 | ret = lys_compile_augment(ctx, augments[u], (struct lysc_node *)&context_node_fake); |
| 5097 | LY_CHECK_GOTO(ret, cleanup); |
| 5098 | } |
| 5099 | |
| 5100 | /* reload previous context's mod_def */ |
| 5101 | ctx->mod_def = mod_old; |
| 5102 | |
| 5103 | /* apply all refines */ |
| 5104 | ret = lys_compile_refines(ctx, uses_p->refines, (struct lysc_node *)&context_node_fake); |
| 5105 | LY_CHECK_GOTO(ret, cleanup); |
| 5106 | |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5107 | if (first_p) { |
| 5108 | *first_p = context_node_fake.child; |
| 5109 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5110 | |
| 5111 | cleanup: |
| 5112 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5113 | if (context_node_fake.child) { |
| 5114 | context_node_fake.child->prev = child; |
| 5115 | } |
| 5116 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5117 | child->parent = parent; |
| 5118 | } |
| 5119 | |
| 5120 | if (uses_p->augments || uses_p->refines) { |
| 5121 | /* 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] | 5122 | if (context_node_fake.actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5123 | 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] | 5124 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5125 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5126 | if (context_node_fake.notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5127 | 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] | 5128 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5129 | } |
| 5130 | } |
| 5131 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5132 | /* reload previous context's mod_def */ |
| 5133 | ctx->mod_def = mod_old; |
| 5134 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5135 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5136 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5137 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5138 | |
| 5139 | return ret; |
| 5140 | } |
| 5141 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5142 | static int |
| 5143 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5144 | { |
| 5145 | struct lysp_node *iter; |
| 5146 | int len = 0; |
| 5147 | |
| 5148 | *path = NULL; |
| 5149 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5150 | char *s = *path; |
| 5151 | char *id; |
| 5152 | |
| 5153 | switch (iter->nodetype) { |
| 5154 | case LYS_USES: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5155 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5156 | break; |
| 5157 | case LYS_GROUPING: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5158 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5159 | break; |
| 5160 | case LYS_AUGMENT: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5161 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5162 | break; |
| 5163 | default: |
| 5164 | id = strdup(iter->name); |
| 5165 | break; |
| 5166 | } |
| 5167 | |
| 5168 | if (!iter->parent) { |
| 5169 | /* print prefix */ |
| 5170 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5171 | } else { |
| 5172 | /* prefix is the same as in parent */ |
| 5173 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5174 | } |
| 5175 | free(s); |
| 5176 | free(id); |
| 5177 | } |
| 5178 | |
| 5179 | if (len < 0) { |
| 5180 | free(*path); |
| 5181 | *path = NULL; |
| 5182 | } else if (len == 0) { |
| 5183 | *path = strdup("/"); |
| 5184 | len = 1; |
| 5185 | } |
| 5186 | return len; |
| 5187 | } |
| 5188 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5189 | /** |
| 5190 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5191 | * |
| 5192 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5193 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5194 | */ |
| 5195 | static LY_ERR |
| 5196 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5197 | { |
| 5198 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5199 | char *path; |
| 5200 | int len; |
| 5201 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5202 | struct lysp_node_uses fake_uses = { |
| 5203 | .parent = node_p, |
| 5204 | .nodetype = LYS_USES, |
| 5205 | .flags = 0, .next = NULL, |
| 5206 | .name = grp->name, |
| 5207 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5208 | .refines = NULL, .augments = NULL |
| 5209 | }; |
| 5210 | struct lysc_node_container fake_container = { |
| 5211 | .nodetype = LYS_CONTAINER, |
| 5212 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5213 | .module = ctx->mod, |
| 5214 | .sp = NULL, .parent = NULL, .next = NULL, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5215 | .prev = (struct lysc_node *)&fake_container, |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5216 | .name = "fake", |
| 5217 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5218 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5219 | }; |
| 5220 | |
| 5221 | if (grp->parent) { |
| 5222 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5223 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5224 | |
| 5225 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5226 | if (len < 0) { |
| 5227 | LOGMEM(ctx->ctx); |
| 5228 | return LY_EMEM; |
| 5229 | } |
| 5230 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5231 | ctx->path_len = (uint32_t)len; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5232 | free(path); |
| 5233 | |
| 5234 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5235 | lysc_update_path(ctx, NULL, grp->name); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5236 | 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] | 5237 | lysc_update_path(ctx, NULL, NULL); |
| 5238 | lysc_update_path(ctx, NULL, NULL); |
| 5239 | |
| 5240 | ctx->path_len = 1; |
| 5241 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5242 | |
| 5243 | /* cleanup */ |
| 5244 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5245 | |
| 5246 | return ret; |
| 5247 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5248 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5249 | /** |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5250 | * @brief Set config flags for a node. |
| 5251 | * |
| 5252 | * @param[in] ctx Compile context. |
| 5253 | * @param[in] node Compiled node config to set. |
| 5254 | * @param[in] parent Parent of @p node. |
| 5255 | * @return LY_ERR value. |
| 5256 | */ |
| 5257 | static LY_ERR |
| 5258 | lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent) |
| 5259 | { |
| 5260 | if (node->nodetype == LYS_CASE) { |
| 5261 | /* case never has any config */ |
| 5262 | assert(!(node->flags & LYS_CONFIG_MASK)); |
| 5263 | return LY_SUCCESS; |
| 5264 | } |
| 5265 | |
| 5266 | /* adjust parent to always get the ancestor with config */ |
| 5267 | if (parent && (parent->nodetype == LYS_CASE)) { |
| 5268 | parent = parent->parent; |
| 5269 | assert(parent); |
| 5270 | } |
| 5271 | |
| 5272 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
| 5273 | /* ignore config statements inside RPC/action data */ |
| 5274 | node->flags &= ~LYS_CONFIG_MASK; |
| 5275 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5276 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
| 5277 | /* ignore config statements inside Notification data */ |
| 5278 | node->flags &= ~LYS_CONFIG_MASK; |
| 5279 | node->flags |= LYS_CONFIG_R; |
| 5280 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
| 5281 | /* config not explicitely set, inherit it from parent */ |
| 5282 | if (parent) { |
| 5283 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5284 | } else { |
| 5285 | /* default is config true */ |
| 5286 | node->flags |= LYS_CONFIG_W; |
| 5287 | } |
| 5288 | } else { |
| 5289 | /* config set explicitely */ |
| 5290 | node->flags |= LYS_SET_CONFIG; |
| 5291 | } |
| 5292 | |
| 5293 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5294 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5295 | "Configuration node cannot be child of any state data node."); |
| 5296 | return LY_EVALID; |
| 5297 | } |
| 5298 | |
| 5299 | return LY_SUCCESS; |
| 5300 | } |
| 5301 | |
| 5302 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5303 | * @brief Compile parsed schema node information. |
| 5304 | * @param[in] ctx Compile context |
| 5305 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5306 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5307 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5308 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5309 | * @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). |
| 5310 | * 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] | 5311 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5312 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5313 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5314 | 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] | 5315 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5316 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5317 | struct lysc_node *node = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5318 | struct lysc_when **when; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5319 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5320 | 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] | 5321 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5322 | if (node_p->nodetype != LYS_USES) { |
| 5323 | lysc_update_path(ctx, parent, node_p->name); |
| 5324 | } else { |
| 5325 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5326 | lysc_update_path(ctx, NULL, node_p->name); |
| 5327 | } |
| 5328 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5329 | switch (node_p->nodetype) { |
| 5330 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5331 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5332 | node_compile_spec = lys_compile_node_container; |
| 5333 | break; |
| 5334 | case LYS_LEAF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5335 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5336 | node_compile_spec = lys_compile_node_leaf; |
| 5337 | break; |
| 5338 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5339 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5340 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5341 | break; |
| 5342 | case LYS_LEAFLIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5343 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5344 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5345 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5346 | case LYS_CHOICE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5347 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5348 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5349 | break; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5350 | case LYS_CASE: |
| 5351 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case)); |
| 5352 | node_compile_spec = lys_compile_node_case; |
| 5353 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5354 | case LYS_ANYXML: |
| 5355 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5356 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5357 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5358 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5359 | case LYS_USES: |
Michal Vasko | 9e8de2d | 2020-09-01 08:17:10 +0200 | [diff] [blame] | 5360 | 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] | 5361 | lysc_update_path(ctx, NULL, NULL); |
| 5362 | lysc_update_path(ctx, NULL, NULL); |
| 5363 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5364 | default: |
| 5365 | LOGINT(ctx->ctx); |
| 5366 | return LY_EINT; |
| 5367 | } |
| 5368 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5369 | node->nodetype = node_p->nodetype; |
| 5370 | node->module = ctx->mod; |
| 5371 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5372 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5373 | |
| 5374 | /* config */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5375 | ret = lys_compile_config(ctx, node, parent); |
| 5376 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5377 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5378 | /* list ordering */ |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5379 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5380 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5381 | 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] | 5382 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5383 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5384 | node->flags &= ~LYS_ORDBY_MASK; |
| 5385 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5386 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5387 | /* default ordering is system */ |
| 5388 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5389 | } |
| 5390 | } |
| 5391 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5392 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5393 | * 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] | 5394 | 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] | 5395 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5396 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5397 | node->sp = node_p; |
| 5398 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 5399 | DUP_STRING_GOTO(ctx->ctx, node_p->name, node->name, ret, error); |
| 5400 | DUP_STRING_GOTO(ctx->ctx, node_p->dsc, node->dsc, ret, error); |
| 5401 | DUP_STRING_GOTO(ctx->ctx, node_p->ref, node->ref, ret, error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5402 | if (node_p->when) { |
| 5403 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5404 | 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] | 5405 | |
| 5406 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5407 | /* do not check "when" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 5408 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 5409 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5410 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5411 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5412 | 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] | 5413 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5414 | /* insert into parent's children/compiled module (we can no longer free the node separately on error) */ |
| 5415 | LY_CHECK_RET(lys_compile_node_connect(ctx, parent, node)); |
| 5416 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5417 | /* nodetype-specific part */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5418 | LY_CHECK_RET(node_compile_spec(ctx, node_p, node)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5419 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5420 | /* final compilation tasks that require the node to be connected */ |
| 5421 | 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] | 5422 | if (node->flags & LYS_MAND_TRUE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5423 | /* inherit LYS_MAND_TRUE in parent containers */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5424 | lys_compile_mandatory_parents(parent, 1); |
| 5425 | } |
| 5426 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5427 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5428 | return LY_SUCCESS; |
| 5429 | |
| 5430 | error: |
| 5431 | lysc_node_free(ctx->ctx, node); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5432 | done: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5433 | return ret; |
| 5434 | } |
| 5435 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5436 | static void |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5437 | lysc_node_unlink(struct lysc_node *node) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5438 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5439 | struct lysc_node *parent, *child; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5440 | struct lysc_module *modc = node->module->compiled; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5441 | |
| 5442 | parent = node->parent; |
| 5443 | |
| 5444 | /* parent's first child */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5445 | if (parent && lysc_node_children(parent, node->flags) == node) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5446 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5447 | } else if (modc->data == node) { |
| 5448 | modc->data = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5449 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5450 | |
| 5451 | /* special choice case unlinking */ |
| 5452 | if (parent && parent->nodetype == LYS_CHOICE) { |
| 5453 | if (((struct lysc_node_choice *)parent)->dflt == (struct lysc_node_case *)node) { |
| 5454 | /* default case removed */ |
| 5455 | ((struct lysc_node_choice *)parent)->dflt = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5456 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5457 | } |
| 5458 | |
| 5459 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5460 | if (node->prev->next) { |
| 5461 | node->prev->next = node->next; |
| 5462 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5463 | if (node->next) { |
| 5464 | node->next->prev = node->prev; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5465 | } else { |
| 5466 | /* last child */ |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5467 | if (parent) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5468 | child = (struct lysc_node *)lysc_node_children(parent, node->flags); |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5469 | } else { |
| 5470 | child = modc->data; |
| 5471 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5472 | if (child) { |
| 5473 | child->prev = node->prev; |
| 5474 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5475 | } |
| 5476 | } |
| 5477 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 5478 | struct lysc_deviation { |
| 5479 | const char *nodeid; |
| 5480 | struct lysc_node *target; /* target node of the deviation */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5481 | 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] | 5482 | uint16_t flags; /* target's flags from lysc_resolve_schema_nodeid() */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5483 | ly_bool not_supported; /* flag if deviates contains not-supported deviate */ |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 5484 | }; |
| 5485 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5486 | /* MACROS for deviates checking */ |
| 5487 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5488 | if (!(target->nodetype & (NODETYPES))) { \ |
| 5489 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\ |
| 5490 | goto cleanup; \ |
| 5491 | } |
| 5492 | |
| 5493 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5494 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
| 5495 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
| 5496 | lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
| 5497 | goto cleanup; \ |
| 5498 | } |
| 5499 | |
| 5500 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5501 | if (!((TYPE)target)->MEMBER || COND) { \ |
| 5502 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
| 5503 | goto cleanup; \ |
| 5504 | } |
| 5505 | |
| 5506 | /** |
| 5507 | * @brief Apply deviate add. |
| 5508 | * |
| 5509 | * @param[in] ctx Compile context. |
| 5510 | * @param[in] target Deviation target. |
| 5511 | * @param[in] dev_flags Internal deviation flags. |
| 5512 | * @param[in] d Deviate add to apply. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5513 | * @return LY_ERR value. |
| 5514 | */ |
| 5515 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5516 | 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] | 5517 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 5518 | LY_ERR ret = LY_EVALID, rc = LY_SUCCESS; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5519 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 5520 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5521 | LY_ARRAY_COUNT_TYPE x; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5522 | |
| 5523 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5524 | if (((TYPE)target)->MEMBER COND) { \ |
| 5525 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5526 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5527 | PROPERTY, ((TYPE)target)->MEMBER); \ |
| 5528 | goto cleanup; \ |
| 5529 | } |
| 5530 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5531 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5532 | if (((TYPE)target)->MEMBER && (COND)) { \ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5533 | ly_bool dynamic_ = 0; const char *val_; \ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5534 | 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] | 5535 | ctx->mod_def, &dynamic_); \ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5536 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5537 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5538 | if (dynamic_) {free((void*)val_);} \ |
| 5539 | goto cleanup; \ |
| 5540 | } |
| 5541 | |
| 5542 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
| 5543 | if (((TYPE)target)->MEMBER && (COND)) { \ |
| 5544 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5545 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5546 | PROPERTY, ((TYPE)target)->VALUEMEMBER); \ |
| 5547 | goto cleanup; \ |
| 5548 | } |
| 5549 | |
| 5550 | /* [units-stmt] */ |
| 5551 | if (d->units) { |
| 5552 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5553 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units); |
| 5554 | |
| 5555 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 5556 | DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units, rc); |
| 5557 | LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5558 | } |
| 5559 | |
| 5560 | /* *must-stmt */ |
| 5561 | if (d->musts) { |
| 5562 | switch (target->nodetype) { |
| 5563 | case LYS_CONTAINER: |
| 5564 | case LYS_LIST: |
| 5565 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts, |
| 5566 | x, lys_compile_must, ret, cleanup); |
| 5567 | break; |
| 5568 | case LYS_LEAF: |
| 5569 | case LYS_LEAFLIST: |
| 5570 | case LYS_ANYDATA: |
| 5571 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts, |
| 5572 | x, lys_compile_must, ret, cleanup); |
| 5573 | break; |
| 5574 | case LYS_NOTIF: |
| 5575 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts, |
| 5576 | x, lys_compile_must, ret, cleanup); |
| 5577 | break; |
| 5578 | case LYS_RPC: |
| 5579 | case LYS_ACTION: |
| 5580 | if (dev_flags & LYSC_OPT_RPC_INPUT) { |
| 5581 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts, |
| 5582 | x, lys_compile_must, ret, cleanup); |
| 5583 | break; |
| 5584 | } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) { |
| 5585 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts, |
| 5586 | x, lys_compile_must, ret, cleanup); |
| 5587 | break; |
| 5588 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5589 | /* fall through */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5590 | default: |
| 5591 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5592 | lys_nodetype2str(target->nodetype), "add", "must"); |
| 5593 | goto cleanup; |
| 5594 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 5595 | ret = ly_set_add(&ctx->xpath, target, 0, NULL); |
| 5596 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | /* *unique-stmt */ |
| 5600 | if (d->uniques) { |
| 5601 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5602 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup); |
| 5603 | } |
| 5604 | |
| 5605 | /* *default-stmt */ |
| 5606 | if (d->dflts) { |
| 5607 | switch (target->nodetype) { |
| 5608 | case LYS_LEAF: |
| 5609 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5610 | 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] | 5611 | if (leaf->dflt) { |
| 5612 | /* first, remove the default value taken from the type */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5613 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5614 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5615 | free(leaf->dflt); |
| 5616 | leaf->dflt = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5617 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5618 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5619 | /* store the default value in unres */ |
Michal Vasko | 31a82d5 | 2020-08-21 08:11:48 +0200 | [diff] [blame] | 5620 | 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] | 5621 | target->flags |= LYS_SET_DFLT; |
| 5622 | break; |
| 5623 | case LYS_LEAFLIST: |
| 5624 | if (llist->dflts && !(target->flags & LYS_SET_DFLT)) { |
| 5625 | /* first, remove the default value taken from the type */ |
| 5626 | LY_ARRAY_FOR(llist->dflts, x) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5627 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5628 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5629 | free(llist->dflts[x]); |
| 5630 | } |
| 5631 | LY_ARRAY_FREE(llist->dflts); |
| 5632 | llist->dflts = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5633 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5634 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5635 | /* store the default values in unres */ |
| 5636 | 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] | 5637 | target->flags |= LYS_SET_DFLT; |
| 5638 | break; |
| 5639 | case LYS_CHOICE: |
| 5640 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
| 5641 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name); |
| 5642 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5643 | * to allow making the default case even the augmented case from the deviating module */ |
| 5644 | if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) { |
| 5645 | goto cleanup; |
| 5646 | } |
| 5647 | break; |
| 5648 | default: |
| 5649 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5650 | lys_nodetype2str(target->nodetype), "add", "default"); |
| 5651 | goto cleanup; |
| 5652 | } |
| 5653 | } |
| 5654 | |
| 5655 | /* [config-stmt] */ |
| 5656 | if (d->flags & LYS_CONFIG_MASK) { |
| 5657 | if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 5658 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5659 | lys_nodetype2str(target->nodetype), "add", "config"); |
| 5660 | goto cleanup; |
| 5661 | } |
| 5662 | if (dev_flags) { |
| 5663 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 5664 | dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
| 5665 | } |
| 5666 | if (target->flags & LYS_SET_CONFIG) { |
| 5667 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5668 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5669 | target->flags & LYS_CONFIG_W ? "true" : "false"); |
| 5670 | goto cleanup; |
| 5671 | } |
| 5672 | LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup); |
| 5673 | } |
| 5674 | |
| 5675 | /* [mandatory-stmt] */ |
| 5676 | if (d->flags & LYS_MAND_MASK) { |
| 5677 | if (target->flags & LYS_MAND_MASK) { |
| 5678 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5679 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5680 | target->flags & LYS_MAND_TRUE ? "true" : "false"); |
| 5681 | goto cleanup; |
| 5682 | } |
| 5683 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup); |
| 5684 | } |
| 5685 | |
| 5686 | /* [min-elements-stmt] */ |
| 5687 | if (d->flags & LYS_SET_MIN) { |
| 5688 | if (target->nodetype == LYS_LEAFLIST) { |
| 5689 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements"); |
| 5690 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5691 | ((struct lysc_node_leaflist *)target)->min = d->min; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5692 | } else if (target->nodetype == LYS_LIST) { |
| 5693 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements"); |
| 5694 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5695 | ((struct lysc_node_list *)target)->min = d->min; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5696 | } else { |
| 5697 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5698 | lys_nodetype2str(target->nodetype), "add", "min-elements"); |
| 5699 | goto cleanup; |
| 5700 | } |
| 5701 | if (d->min) { |
| 5702 | target->flags |= LYS_MAND_TRUE; |
| 5703 | } |
| 5704 | } |
| 5705 | |
| 5706 | /* [max-elements-stmt] */ |
| 5707 | if (d->flags & LYS_SET_MAX) { |
| 5708 | if (target->nodetype == LYS_LEAFLIST) { |
| 5709 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements"); |
| 5710 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5711 | ((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] | 5712 | } else if (target->nodetype == LYS_LIST) { |
| 5713 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements"); |
| 5714 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5715 | ((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] | 5716 | } else { |
| 5717 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5718 | lys_nodetype2str(target->nodetype), "add", "max-elements"); |
| 5719 | goto cleanup; |
| 5720 | } |
| 5721 | } |
| 5722 | |
| 5723 | ret = LY_SUCCESS; |
| 5724 | |
| 5725 | cleanup: |
| 5726 | return ret; |
| 5727 | } |
| 5728 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5729 | static LY_ERR |
| 5730 | lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt) |
| 5731 | { |
| 5732 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5733 | ly_bool dyn = 0; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5734 | const char *orig_dflt; |
| 5735 | uint32_t i; |
| 5736 | |
| 5737 | if (target->module != ctx->mod) { |
| 5738 | /* foreign deviation */ |
| 5739 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt); |
| 5740 | |
| 5741 | /* check that the value matches */ |
| 5742 | orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn); |
| 5743 | if (strcmp(orig_dflt, dflt)) { |
| 5744 | goto error; |
| 5745 | } |
| 5746 | if (dyn) { |
| 5747 | free((char *)orig_dflt); |
| 5748 | } |
| 5749 | |
| 5750 | /* remove the default specification */ |
| 5751 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5752 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5753 | free(leaf->dflt); |
| 5754 | leaf->dflt = NULL; |
| 5755 | } else { |
| 5756 | /* local deviation */ |
| 5757 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt); |
| 5758 | |
| 5759 | /* find the incomplete default */ |
| 5760 | orig_dflt = NULL; |
| 5761 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 5762 | if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) { |
| 5763 | orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt; |
| 5764 | break; |
| 5765 | } |
| 5766 | } |
| 5767 | LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT); |
| 5768 | |
| 5769 | /* check that the value matches */ |
| 5770 | if (strcmp(orig_dflt, dflt)) { |
| 5771 | goto error; |
| 5772 | } |
| 5773 | |
| 5774 | /* update the list of incomplete default values */ |
| 5775 | lysc_incomplete_dflt_remove(ctx, target); |
| 5776 | } |
| 5777 | |
| 5778 | target->flags &= ~LYS_SET_DFLT; |
| 5779 | return LY_SUCCESS; |
| 5780 | |
| 5781 | error: |
| 5782 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5783 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 5784 | dflt, orig_dflt); |
| 5785 | if (dyn) { |
| 5786 | free((char *)orig_dflt); |
| 5787 | } |
| 5788 | cleanup: |
| 5789 | return LY_EVALID; |
| 5790 | } |
| 5791 | |
| 5792 | static LY_ERR |
| 5793 | lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts) |
| 5794 | { |
| 5795 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5796 | ly_bool dyn = 0, found; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5797 | const char *orig_dflt, **orig_dflts; |
| 5798 | uint32_t i; |
| 5799 | LY_ARRAY_COUNT_TYPE x, y; |
| 5800 | |
| 5801 | if (target->module != ctx->mod) { |
| 5802 | /* foreign deviation */ |
| 5803 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]); |
| 5804 | LY_ARRAY_FOR(dflts, x) { |
| 5805 | found = 0; |
| 5806 | LY_ARRAY_FOR(llist->dflts, y) { |
| 5807 | orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn); |
| 5808 | if (!strcmp(orig_dflt, dflts[x])) { |
| 5809 | if (dyn) { |
| 5810 | free((char *)orig_dflt); |
| 5811 | } |
| 5812 | found = 1; |
| 5813 | break; |
| 5814 | } |
| 5815 | if (dyn) { |
| 5816 | free((char *)orig_dflt); |
| 5817 | } |
| 5818 | } |
| 5819 | if (!found) { |
| 5820 | goto error; |
| 5821 | } |
| 5822 | |
| 5823 | /* update compiled default values */ |
| 5824 | LY_ARRAY_DECREMENT(llist->dflts); |
| 5825 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 5826 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 5827 | free(llist->dflts[y]); |
| 5828 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts)); |
| 5829 | } |
| 5830 | if (!LY_ARRAY_COUNT(llist->dflts)) { |
| 5831 | LY_ARRAY_FREE(llist->dflts); |
| 5832 | llist->dflts = NULL; |
| 5833 | llist->flags &= ~LYS_SET_DFLT; |
| 5834 | } |
| 5835 | } else { |
| 5836 | /* local deviation */ |
| 5837 | orig_dflt = NULL; |
| 5838 | orig_dflts = NULL; |
| 5839 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 5840 | if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) { |
| 5841 | orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt; |
| 5842 | orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts; |
| 5843 | break; |
| 5844 | } |
| 5845 | } |
| 5846 | LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT); |
| 5847 | |
| 5848 | if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) { |
| 5849 | /* TODO it is not currently possible to remove just one default value from incomplete defaults array */ |
| 5850 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5851 | "Local deviation deleting leaf-list defaults is not supported."); |
| 5852 | return LY_EVALID; |
| 5853 | } |
| 5854 | |
| 5855 | LY_ARRAY_FOR(dflts, x) { |
| 5856 | found = 0; |
| 5857 | if (orig_dflts) { |
| 5858 | LY_ARRAY_FOR(orig_dflts, y) { |
| 5859 | if (!strcmp(orig_dflts[y], dflts[x])) { |
| 5860 | found = 1; |
| 5861 | break; |
| 5862 | } |
| 5863 | } |
| 5864 | } else if (!strcmp(orig_dflt, dflts[x])) { |
| 5865 | found = 1; |
| 5866 | } |
| 5867 | if (!found) { |
| 5868 | goto error; |
| 5869 | } |
| 5870 | |
| 5871 | /* update the list of incomplete default values */ |
| 5872 | lysc_incomplete_dflt_remove(ctx, target); |
| 5873 | } |
| 5874 | |
| 5875 | llist->flags &= ~LYS_SET_DFLT; |
| 5876 | } |
| 5877 | |
| 5878 | return LY_SUCCESS; |
| 5879 | |
| 5880 | error: |
| 5881 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 5882 | "which does not match any of the target's property values.", dflts[x]); |
| 5883 | cleanup: |
| 5884 | return LY_EVALID; |
| 5885 | } |
| 5886 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5887 | /** |
| 5888 | * @brief Apply deviate delete. |
| 5889 | * |
| 5890 | * @param[in] ctx Compile context. |
| 5891 | * @param[in] target Deviation target. |
| 5892 | * @param[in] dev_flags Internal deviation flags. |
| 5893 | * @param[in] d Deviate delete to apply. |
| 5894 | * @return LY_ERR value. |
| 5895 | */ |
| 5896 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5897 | 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] | 5898 | { |
| 5899 | LY_ERR ret = LY_EVALID; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5900 | struct lysc_node_list *list = (struct lysc_node_list *)target; |
| 5901 | LY_ARRAY_COUNT_TYPE x, y, z; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5902 | size_t prefix_len, name_len; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5903 | const char *prefix, *name, *nodeid; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5904 | struct lys_module *mod; |
| 5905 | |
| 5906 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5907 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \ |
| 5908 | LY_ARRAY_FOR(d->ARRAY_DEV, x) { \ |
| 5909 | LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \ |
| 5910 | if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \ |
| 5911 | } \ |
| 5912 | if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \ |
| 5913 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5914 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5915 | PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \ |
| 5916 | goto cleanup; \ |
| 5917 | } \ |
| 5918 | LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \ |
| 5919 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \ |
| 5920 | memmove(&((TYPE)target)->ARRAY_TRG[y], \ |
| 5921 | &((TYPE)target)->ARRAY_TRG[y + 1], \ |
| 5922 | (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \ |
| 5923 | } \ |
| 5924 | if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \ |
| 5925 | LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \ |
| 5926 | ((TYPE)target)->ARRAY_TRG = NULL; \ |
| 5927 | } |
| 5928 | |
| 5929 | /* [units-stmt] */ |
| 5930 | if (d->units) { |
| 5931 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5932 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, 0, units, "deleting", "units", d->units); |
| 5933 | if (strcmp(((struct lysc_node_leaf *)target)->units, d->units)) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5934 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5935 | "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] | 5936 | d->units, ((struct lysc_node_leaf *)target)->units); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5937 | goto cleanup; |
| 5938 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5939 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf *)target)->units); |
| 5940 | ((struct lysc_node_leaf *)target)->units = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5941 | } |
| 5942 | |
| 5943 | /* *must-stmt */ |
| 5944 | if (d->musts) { |
| 5945 | switch (target->nodetype) { |
| 5946 | case LYS_CONTAINER: |
| 5947 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5948 | 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] | 5949 | break; |
| 5950 | case LYS_LEAF: |
| 5951 | case LYS_LEAFLIST: |
| 5952 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5953 | 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] | 5954 | break; |
| 5955 | case LYS_NOTIF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5956 | 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] | 5957 | break; |
| 5958 | case LYS_RPC: |
| 5959 | case LYS_ACTION: |
| 5960 | if (dev_flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5961 | 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] | 5962 | break; |
| 5963 | } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5964 | 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] | 5965 | break; |
| 5966 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5967 | /* fall through */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5968 | default: |
| 5969 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5970 | lys_nodetype2str(target->nodetype), "delete", "must"); |
| 5971 | goto cleanup; |
| 5972 | } |
| 5973 | } |
| 5974 | |
| 5975 | /* *unique-stmt */ |
| 5976 | if (d->uniques) { |
| 5977 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 5978 | LY_ARRAY_FOR(d->uniques, x) { |
| 5979 | LY_ARRAY_FOR(list->uniques, z) { |
| 5980 | for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 5981 | nodeid = strpbrk(name, " \t\n"); |
| 5982 | if (nodeid) { |
| 5983 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
| 5984 | break; |
| 5985 | } |
| 5986 | while (isspace(*nodeid)) { |
| 5987 | ++nodeid; |
| 5988 | } |
| 5989 | } else { |
| 5990 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 5991 | break; |
| 5992 | } |
| 5993 | } |
| 5994 | } |
| 5995 | if (!name) { |
| 5996 | /* complete match - remove the unique */ |
| 5997 | LY_ARRAY_DECREMENT(list->uniques); |
| 5998 | LY_ARRAY_FREE(list->uniques[z]); |
| 5999 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques)); |
| 6000 | --z; |
| 6001 | break; |
| 6002 | } |
| 6003 | } |
| 6004 | if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) { |
| 6005 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6006 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6007 | d->uniques[x]); |
| 6008 | goto cleanup; |
| 6009 | } |
| 6010 | } |
| 6011 | if (!LY_ARRAY_COUNT(list->uniques)) { |
| 6012 | LY_ARRAY_FREE(list->uniques); |
| 6013 | list->uniques = NULL; |
| 6014 | } |
| 6015 | } |
| 6016 | |
| 6017 | /* *default-stmt */ |
| 6018 | if (d->dflts) { |
| 6019 | switch (target->nodetype) { |
| 6020 | case LYS_LEAF: |
| 6021 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6022 | 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] | 6023 | break; |
| 6024 | case LYS_LEAFLIST: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6025 | 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] | 6026 | break; |
| 6027 | case LYS_CHOICE: |
| 6028 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6029 | 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] | 6030 | nodeid = d->dflts[0]; |
| 6031 | LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup); |
| 6032 | if (prefix) { |
| 6033 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6034 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6035 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6036 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6037 | "The prefix does not match any imported module of the deviation module.", d->dflts[0]); |
| 6038 | goto cleanup; |
| 6039 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6040 | if (mod != ((struct lysc_node_choice *)target)->dflt->module) { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6041 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6042 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6043 | "The prefix does not match the default case's module.", d->dflts[0]); |
| 6044 | goto cleanup; |
| 6045 | } |
| 6046 | } |
| 6047 | /* else { |
| 6048 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6049 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6050 | * the module of the default case, just matching its name */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6051 | if (strcmp(name, ((struct lysc_node_choice *)target)->dflt->name)) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6052 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6053 | "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] | 6054 | d->dflts[0], ((struct lysc_node_choice *)target)->dflt->name); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6055 | goto cleanup; |
| 6056 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6057 | ((struct lysc_node_choice *)target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6058 | ((struct lysc_node_choice *)target)->dflt = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6059 | break; |
| 6060 | default: |
| 6061 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6062 | lys_nodetype2str(target->nodetype), "delete", "default"); |
| 6063 | goto cleanup; |
| 6064 | } |
| 6065 | } |
| 6066 | |
| 6067 | ret = LY_SUCCESS; |
| 6068 | |
| 6069 | cleanup: |
| 6070 | return ret; |
| 6071 | } |
| 6072 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6073 | static LY_ERR |
| 6074 | lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target) |
| 6075 | { |
| 6076 | LY_ERR ret; |
| 6077 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 6078 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 6079 | struct ly_err_item *err = NULL; |
| 6080 | LY_ARRAY_COUNT_TYPE x; |
| 6081 | const char *dflt; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6082 | ly_bool dyn; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6083 | |
| 6084 | if (target->module != ctx->mod) { |
| 6085 | /* foreign deviation */ |
| 6086 | if (target->nodetype == LYS_LEAF) { |
| 6087 | dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn); |
| 6088 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6089 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6090 | |
| 6091 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 6092 | LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err); |
| 6093 | if (dyn) { |
| 6094 | free((char *)dflt); |
| 6095 | } |
| 6096 | if (err) { |
| 6097 | ly_err_print(err); |
| 6098 | ctx->path[0] = '\0'; |
| 6099 | lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6100 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6101 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6102 | ly_err_free(err); |
| 6103 | } |
| 6104 | LY_CHECK_RET(ret); |
| 6105 | |
| 6106 | ++leaf->dflt->realtype->refcount; |
| 6107 | } else { /* LY_LEAFLIST */ |
| 6108 | LY_ARRAY_FOR(llist->dflts, x) { |
| 6109 | dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn); |
| 6110 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6111 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6112 | |
| 6113 | ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 6114 | LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err); |
| 6115 | if (dyn) { |
| 6116 | free((char *)dflt); |
| 6117 | } |
| 6118 | if (err) { |
| 6119 | ly_err_print(err); |
| 6120 | ctx->path[0] = '\0'; |
| 6121 | lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6122 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6123 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6124 | ly_err_free(err); |
| 6125 | } |
| 6126 | LY_CHECK_RET(ret); |
| 6127 | |
| 6128 | ++llist->dflts[x]->realtype->refcount; |
| 6129 | } |
| 6130 | } |
| 6131 | } else { |
| 6132 | /* local deviation */ |
| 6133 | |
| 6134 | /* these default were not compiled yet, so they will use the new type automatically */ |
| 6135 | } |
| 6136 | |
| 6137 | return LY_SUCCESS; |
| 6138 | } |
| 6139 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6140 | /** |
| 6141 | * @brief Apply deviate replace. |
| 6142 | * |
| 6143 | * @param[in] ctx Compile context. |
| 6144 | * @param[in] target Deviation target. |
| 6145 | * @param[in] d Deviate replace to apply. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6146 | * @return LY_ERR value. |
| 6147 | */ |
| 6148 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6149 | 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] | 6150 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 6151 | LY_ERR ret = LY_EVALID, rc = LY_SUCCESS; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6152 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 6153 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 6154 | LY_ARRAY_COUNT_TYPE x; |
| 6155 | |
| 6156 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 6157 | if (!(((TYPE)target)->MEMBER COND)) { \ |
| 6158 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 6159 | "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \ |
| 6160 | goto cleanup; \ |
| 6161 | } |
| 6162 | |
| 6163 | /* [type-stmt] */ |
| 6164 | if (d->type) { |
| 6165 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6166 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6167 | lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type); |
| 6168 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6169 | /* remove only default value inherited from the type */ |
| 6170 | if (!(target->flags & LYS_SET_DFLT)) { |
| 6171 | if (target->module != ctx->mod) { |
| 6172 | /* foreign deviation - the target has default from the previous type, remove it */ |
| 6173 | if (target->nodetype == LYS_LEAF) { |
| 6174 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6175 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6176 | free(leaf->dflt); |
| 6177 | leaf->dflt = NULL; |
| 6178 | } else { /* LYS_LEAFLIST */ |
| 6179 | LY_ARRAY_FOR(llist->dflts, x) { |
| 6180 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6181 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6182 | free(llist->dflts[x]); |
| 6183 | } |
| 6184 | LY_ARRAY_FREE(llist->dflts); |
| 6185 | llist->dflts = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6186 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6187 | } else { |
| 6188 | /* local deviation */ |
| 6189 | lysc_incomplete_dflt_remove(ctx, target); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6190 | } |
| 6191 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6192 | |
| 6193 | LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf)); |
| 6194 | |
| 6195 | if (target->flags & LYS_SET_DFLT) { |
| 6196 | /* the term default value(s) needs to be recompiled */ |
| 6197 | LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target)); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6198 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6199 | } |
| 6200 | |
| 6201 | /* [units-stmt] */ |
| 6202 | if (d->units) { |
| 6203 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6204 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS), |
| 6205 | units, "replacing", "units", d->units); |
| 6206 | |
| 6207 | lydict_remove(ctx->ctx, leaf->units); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 6208 | DUP_STRING(ctx->ctx, d->units, leaf->units, rc); |
| 6209 | LY_CHECK_ERR_GOTO(rc, ret = rc, cleanup); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6210 | } |
| 6211 | |
| 6212 | /* [default-stmt] */ |
| 6213 | if (d->dflt) { |
| 6214 | switch (target->nodetype) { |
| 6215 | case LYS_LEAF: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6216 | if (target->module != ctx->mod) { |
| 6217 | /* foreign deviation */ |
| 6218 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing", |
| 6219 | "default", d->dflt); |
| 6220 | |
| 6221 | /* remove the default specification */ |
| 6222 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6223 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6224 | free(leaf->dflt); |
| 6225 | leaf->dflt = NULL; |
| 6226 | } else { |
| 6227 | /* local deviation */ |
| 6228 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing", |
| 6229 | "default", d->dflt); |
| 6230 | assert(!leaf->dflt); |
| 6231 | } |
| 6232 | |
| 6233 | /* store the new default value */ |
| 6234 | 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] | 6235 | break; |
| 6236 | case LYS_CHOICE: |
| 6237 | DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt); |
| 6238 | if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) { |
| 6239 | goto cleanup; |
| 6240 | } |
| 6241 | break; |
| 6242 | default: |
| 6243 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6244 | lys_nodetype2str(target->nodetype), "replace", "default"); |
| 6245 | goto cleanup; |
| 6246 | } |
| 6247 | } |
| 6248 | |
| 6249 | /* [config-stmt] */ |
| 6250 | if (d->flags & LYS_CONFIG_MASK) { |
| 6251 | if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 6252 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6253 | lys_nodetype2str(target->nodetype), "replace", "config"); |
| 6254 | goto cleanup; |
| 6255 | } |
| 6256 | if (!(target->flags & LYS_SET_CONFIG)) { |
| 6257 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6258 | "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6259 | goto cleanup; |
| 6260 | } |
| 6261 | LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup); |
| 6262 | } |
| 6263 | |
| 6264 | /* [mandatory-stmt] */ |
| 6265 | if (d->flags & LYS_MAND_MASK) { |
| 6266 | if (!(target->flags & LYS_MAND_MASK)) { |
| 6267 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6268 | "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6269 | goto cleanup; |
| 6270 | } |
| 6271 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup); |
| 6272 | } |
| 6273 | |
| 6274 | /* [min-elements-stmt] */ |
| 6275 | if (d->flags & LYS_SET_MIN) { |
| 6276 | if (target->nodetype == LYS_LEAFLIST) { |
| 6277 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements"); |
| 6278 | /* change value */ |
| 6279 | ((struct lysc_node_leaflist *)target)->min = d->min; |
| 6280 | } else if (target->nodetype == LYS_LIST) { |
| 6281 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements"); |
| 6282 | /* change value */ |
| 6283 | ((struct lysc_node_list *)target)->min = d->min; |
| 6284 | } else { |
| 6285 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6286 | lys_nodetype2str(target->nodetype), "replace", "min-elements"); |
| 6287 | goto cleanup; |
| 6288 | } |
| 6289 | if (d->min) { |
| 6290 | target->flags |= LYS_MAND_TRUE; |
| 6291 | } |
| 6292 | } |
| 6293 | |
| 6294 | /* [max-elements-stmt] */ |
| 6295 | if (d->flags & LYS_SET_MAX) { |
| 6296 | if (target->nodetype == LYS_LEAFLIST) { |
| 6297 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements"); |
| 6298 | /* change value */ |
| 6299 | ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1; |
| 6300 | } else if (target->nodetype == LYS_LIST) { |
| 6301 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements"); |
| 6302 | /* change value */ |
| 6303 | ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1; |
| 6304 | } else { |
| 6305 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6306 | lys_nodetype2str(target->nodetype), "replace", "max-elements"); |
| 6307 | goto cleanup; |
| 6308 | } |
| 6309 | } |
| 6310 | |
| 6311 | ret = LY_SUCCESS; |
| 6312 | |
| 6313 | cleanup: |
| 6314 | return ret; |
| 6315 | } |
| 6316 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6317 | /** |
| 6318 | * @brief Apply all deviations of one target node. |
| 6319 | * |
| 6320 | * @param[in] ctx Compile context. |
| 6321 | * @param[in] dev Deviation structure to apply. |
| 6322 | * @return LY_ERR value. |
| 6323 | */ |
| 6324 | static LY_ERR |
| 6325 | lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6326 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6327 | LY_ERR ret = LY_EVALID; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6328 | struct lysc_node *target = dev->target; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6329 | struct lysc_action *rpcs; |
| 6330 | struct lysc_notif *notifs; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6331 | struct lysp_deviate *d; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6332 | LY_ARRAY_COUNT_TYPE v, x; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6333 | uint32_t min, max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6334 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6335 | lysc_update_path(ctx, NULL, dev->nodeid); |
| 6336 | |
| 6337 | /* not-supported */ |
| 6338 | if (dev->not_supported) { |
| 6339 | if (LY_ARRAY_COUNT(dev->deviates) > 1) { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 6340 | 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] | 6341 | LY_ARRAY_COUNT(dev->deviates), dev->nodeid); |
| 6342 | } |
| 6343 | |
| 6344 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 6345 | if (target->parent) { \ |
| 6346 | ARRAY = (TYPE*)GETFUNC(target->parent); \ |
| 6347 | } else { \ |
| 6348 | ARRAY = target->module->compiled->ARRAY; \ |
| 6349 | } \ |
| 6350 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 6351 | if (&ARRAY[x] == (TYPE*)target) { break; } \ |
| 6352 | } \ |
| 6353 | if (x < LY_ARRAY_COUNT(ARRAY)) { \ |
| 6354 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 6355 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 6356 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 6357 | } |
| 6358 | |
| 6359 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 6360 | if (dev->flags & LYSC_OPT_RPC_INPUT) { |
| 6361 | /* remove RPC's/action's input */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6362 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input); |
| 6363 | memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input); |
| 6364 | FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free); |
| 6365 | ((struct lysc_action *)target)->input_exts = NULL; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6366 | } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6367 | /* remove RPC's/action's output */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6368 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output); |
| 6369 | memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output); |
| 6370 | FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free); |
| 6371 | ((struct lysc_action *)target)->output_exts = NULL; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6372 | } else { |
| 6373 | /* remove RPC/action */ |
| 6374 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
| 6375 | } |
| 6376 | } else if (target->nodetype == LYS_NOTIF) { |
| 6377 | /* remove Notification */ |
| 6378 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
| 6379 | } else { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 6380 | if (target->parent && (target->parent->nodetype == LYS_CASE) && (target->prev == target)) { |
| 6381 | /* remove the target node with its parent case node because it is the only node of the case */ |
| 6382 | lysc_node_unlink(target->parent); |
| 6383 | lysc_node_free(ctx->ctx, target->parent); |
| 6384 | } else { |
| 6385 | /* remove the target node */ |
| 6386 | lysc_node_unlink(target); |
| 6387 | lysc_node_free(ctx->ctx, target); |
| 6388 | } |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6389 | } |
| 6390 | |
| 6391 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 6392 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
| 6393 | return LY_SUCCESS; |
| 6394 | } |
| 6395 | |
| 6396 | /* list of deviates (not-supported is not present in the list) */ |
| 6397 | LY_ARRAY_FOR(dev->deviates, v) { |
| 6398 | d = dev->deviates[v]; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6399 | switch (d->mod) { |
| 6400 | case LYS_DEV_ADD: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6401 | 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] | 6402 | break; |
| 6403 | case LYS_DEV_DELETE: |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6404 | 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] | 6405 | break; |
| 6406 | case LYS_DEV_REPLACE: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6407 | 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] | 6408 | break; |
| 6409 | default: |
| 6410 | LOGINT(ctx->ctx); |
| 6411 | goto cleanup; |
| 6412 | } |
| 6413 | } |
| 6414 | |
| 6415 | /* final check when all deviations of a single target node are applied */ |
| 6416 | |
| 6417 | /* check min-max compatibility */ |
| 6418 | if (target->nodetype == LYS_LEAFLIST) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6419 | min = ((struct lysc_node_leaflist *)target)->min; |
| 6420 | max = ((struct lysc_node_leaflist *)target)->max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6421 | } else if (target->nodetype == LYS_LIST) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6422 | min = ((struct lysc_node_list *)target)->min; |
| 6423 | max = ((struct lysc_node_list *)target)->max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6424 | } else { |
| 6425 | min = max = 0; |
| 6426 | } |
| 6427 | if (min > max) { |
| 6428 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements " |
| 6429 | "after deviation: min value %u is bigger than max value %u.", min, max); |
| 6430 | goto cleanup; |
| 6431 | } |
| 6432 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6433 | /* check mandatory - default compatibility */ |
| 6434 | if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT) |
| 6435 | && (target->flags & LYS_MAND_TRUE)) { |
| 6436 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6437 | "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype)); |
| 6438 | goto cleanup; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6439 | } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)target)->dflt |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6440 | && (target->flags & LYS_MAND_TRUE)) { |
| 6441 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice."); |
| 6442 | goto cleanup; |
| 6443 | } |
| 6444 | if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) { |
| 6445 | /* mandatory node under a default case */ |
| 6446 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6447 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6448 | lys_nodetype2str(target->nodetype), target->name, target->parent->name); |
| 6449 | goto cleanup; |
| 6450 | } |
| 6451 | |
| 6452 | /* success */ |
| 6453 | ret = LY_SUCCESS; |
| 6454 | |
| 6455 | cleanup: |
| 6456 | lysc_update_path(ctx, NULL, NULL); |
| 6457 | return ret; |
| 6458 | } |
| 6459 | |
| 6460 | LY_ERR |
| 6461 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
| 6462 | { |
| 6463 | LY_ERR ret = LY_EVALID; |
| 6464 | struct ly_set devs_p = {0}; |
| 6465 | struct ly_set targets = {0}; |
| 6466 | struct lysc_node *target; /* target target of the deviation */ |
| 6467 | struct lysp_deviation *dev; |
| 6468 | struct lysp_deviate *d, **dp_new; |
| 6469 | LY_ARRAY_COUNT_TYPE u, v; |
| 6470 | struct lysc_deviation **devs = NULL; |
| 6471 | struct lys_module *target_mod, **dev_mod; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6472 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6473 | |
| 6474 | /* get all deviations from the module and all its submodules ... */ |
| 6475 | LY_ARRAY_FOR(mod_p->deviations, u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6476 | 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] | 6477 | } |
| 6478 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 6479 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6480 | 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] | 6481 | } |
| 6482 | } |
| 6483 | if (!devs_p.count) { |
| 6484 | /* nothing to do */ |
| 6485 | return LY_SUCCESS; |
| 6486 | } |
| 6487 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6488 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 6489 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6490 | /* ... and group them by the target node */ |
| 6491 | devs = calloc(devs_p.count, sizeof *devs); |
| 6492 | for (u = 0; u < devs_p.count; ++u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6493 | uint32_t index; |
| 6494 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6495 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6496 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6497 | |
| 6498 | /* resolve the target */ |
Michal Vasko | cb18c7f | 2020-08-24 09:22:28 +0200 | [diff] [blame] | 6499 | 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] | 6500 | (const struct lysc_node **)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6501 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6502 | /* move the target pointer to input/output to make them different from the action and |
| 6503 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 6504 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 6505 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 6506 | if (flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6507 | target = (struct lysc_node *)&((struct lysc_action *)target)->input; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6508 | flags |= LYSC_OPT_INTERNAL; |
| 6509 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6510 | target = (struct lysc_node *)&((struct lysc_action *)target)->output; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6511 | flags |= LYSC_OPT_INTERNAL; |
| 6512 | } |
| 6513 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6514 | /* insert into the set of targets with duplicity detection */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6515 | ret = ly_set_add(&targets, target, 0, &index); |
| 6516 | LY_CHECK_GOTO(ret, cleanup); |
| 6517 | if (!devs[index]) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6518 | /* new record */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6519 | devs[index] = calloc(1, sizeof **devs); |
| 6520 | devs[index]->target = target; |
| 6521 | devs[index]->nodeid = dev->nodeid; |
| 6522 | devs[index]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6523 | } |
| 6524 | /* add deviates into the deviation's list of deviates */ |
Michal Vasko | 5c38f36 | 2020-08-24 09:22:51 +0200 | [diff] [blame] | 6525 | LY_LIST_FOR(dev->deviates, d) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6526 | 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] | 6527 | *dp_new = d; |
| 6528 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6529 | devs[index]->not_supported = 1; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6530 | } |
| 6531 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6532 | |
| 6533 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6534 | } |
| 6535 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6536 | /* apply deviations */ |
| 6537 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6538 | ly_bool match = 0; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6539 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6540 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 6541 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 6542 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6543 | 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] | 6544 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6545 | 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] | 6546 | } |
| 6547 | } |
| 6548 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6549 | /* remember target module (the target node may be removed) */ |
| 6550 | target_mod = devs[u]->target->module; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6551 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6552 | /* apply the deviation */ |
| 6553 | LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6554 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6555 | /* 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] | 6556 | LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) { |
| 6557 | if (target_mod->compiled->deviated_by[v] == mod_p->mod) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6558 | match = 1; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6559 | break; |
| 6560 | } |
| 6561 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6562 | if (!match) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6563 | 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] | 6564 | *dev_mod = mod_p->mod; |
| 6565 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6566 | } |
| 6567 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6568 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6569 | ret = LY_SUCCESS; |
| 6570 | |
| 6571 | cleanup: |
| 6572 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6573 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6574 | free(devs[u]); |
| 6575 | } |
| 6576 | free(devs); |
| 6577 | ly_set_erase(&targets, NULL); |
| 6578 | ly_set_erase(&devs_p, NULL); |
| 6579 | |
| 6580 | return ret; |
| 6581 | } |
| 6582 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6583 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6584 | * @brief Compile the given YANG submodule into the main module. |
| 6585 | * @param[in] ctx Compile context |
| 6586 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6587 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6588 | */ |
| 6589 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6590 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6591 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6592 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6593 | LY_ERR ret = LY_SUCCESS; |
| 6594 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6595 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6596 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6597 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6598 | |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 6599 | /* features are compiled directly into the compiled module structure, |
| 6600 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6601 | * The features compilation is finished in the main module (lys_compile()). */ |
| 6602 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->mod->features); |
| 6603 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6604 | |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 6605 | ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->mod->identities); |
| 6606 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6607 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6608 | /* data nodes */ |
| 6609 | LY_LIST_FOR(submod->data, node_p) { |
| 6610 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6611 | LY_CHECK_GOTO(ret, error); |
| 6612 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6613 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6614 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6615 | 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] | 6616 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6617 | error: |
| 6618 | return ret; |
| 6619 | } |
| 6620 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6621 | static void * |
| 6622 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6623 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6624 | for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6625 | if (substmts[u].stmt == stmt) { |
| 6626 | return substmts[u].storage; |
| 6627 | } |
| 6628 | } |
| 6629 | return NULL; |
| 6630 | } |
| 6631 | |
| 6632 | LY_ERR |
| 6633 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6634 | { |
| 6635 | LY_ERR ret = LY_EVALID, r; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6636 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6637 | struct lysp_stmt *stmt; |
| 6638 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6639 | |
| 6640 | /* check for invalid substatements */ |
| 6641 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6642 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6643 | continue; |
| 6644 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6645 | for (u = 0; substmts[u].stmt; ++u) { |
| 6646 | if (substmts[u].stmt == stmt->kw) { |
| 6647 | break; |
| 6648 | } |
| 6649 | } |
| 6650 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6651 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6652 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6653 | goto cleanup; |
| 6654 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6655 | } |
| 6656 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6657 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6658 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6659 | /* keep order of the processing the same as the order in the defined substmts, |
| 6660 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6661 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6662 | ly_bool stmt_present = 0; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6663 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6664 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6665 | if (substmts[u].stmt != stmt->kw) { |
| 6666 | continue; |
| 6667 | } |
| 6668 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6669 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6670 | if (substmts[u].storage) { |
| 6671 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6672 | case LY_STMT_STATUS: |
| 6673 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6674 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6675 | break; |
| 6676 | case LY_STMT_UNITS: { |
| 6677 | const char **units; |
| 6678 | |
| 6679 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6680 | /* single item */ |
| 6681 | if (*((const char **)substmts[u].storage)) { |
| 6682 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6683 | goto cleanup; |
| 6684 | } |
| 6685 | units = (const char **)substmts[u].storage; |
| 6686 | } else { |
| 6687 | /* sized array */ |
| 6688 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6689 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6690 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 6691 | r = lydict_insert(ctx->ctx, stmt->arg, 0, units); |
| 6692 | LY_CHECK_ERR_GOTO(r, ret = r, cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6693 | break; |
| 6694 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6695 | case LY_STMT_TYPE: { |
| 6696 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6697 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6698 | |
| 6699 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6700 | /* single item */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6701 | if (*(struct lysc_type **)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6702 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6703 | goto cleanup; |
| 6704 | } |
| 6705 | compiled = substmts[u].storage; |
| 6706 | } else { |
| 6707 | /* sized array */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6708 | struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6709 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6710 | compiled = (void *)type; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6711 | } |
| 6712 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6713 | 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] | 6714 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL, |
| 6715 | 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] | 6716 | 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] | 6717 | lysp_type_free(ctx->ctx, parsed); |
| 6718 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6719 | break; |
| 6720 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6721 | case LY_STMT_IF_FEATURE: { |
| 6722 | struct lysc_iffeature *iff = NULL; |
| 6723 | |
| 6724 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6725 | /* single item */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6726 | if (((struct lysc_iffeature *)substmts[u].storage)->features) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6727 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6728 | goto cleanup; |
| 6729 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6730 | iff = (struct lysc_iffeature *)substmts[u].storage; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6731 | } else { |
| 6732 | /* sized array */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6733 | struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6734 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6735 | } |
| 6736 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6737 | break; |
| 6738 | } |
| 6739 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6740 | * 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] | 6741 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6742 | 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.", |
| 6743 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6744 | goto cleanup; |
| 6745 | } |
| 6746 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6747 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6748 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6749 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6750 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6751 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6752 | goto cleanup; |
| 6753 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6754 | } |
| 6755 | |
| 6756 | ret = LY_SUCCESS; |
| 6757 | |
| 6758 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6759 | return ret; |
| 6760 | } |
| 6761 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6762 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6763 | * @brief Check when for cyclic dependencies. |
| 6764 | * @param[in] set Set with all the referenced nodes. |
| 6765 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6766 | * @return LY_ERR value |
| 6767 | */ |
| 6768 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6769 | 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] | 6770 | { |
| 6771 | struct lyxp_set tmp_set; |
| 6772 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6773 | uint32_t i, j; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6774 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6775 | struct lysc_when *when; |
| 6776 | LY_ERR ret = LY_SUCCESS; |
| 6777 | |
| 6778 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6779 | |
| 6780 | /* prepare in_ctx of the set */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6781 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6782 | xp_scnode = &set->val.scnodes[i]; |
| 6783 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6784 | if (xp_scnode->in_ctx != -1) { |
| 6785 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6786 | xp_scnode->in_ctx = 1; |
| 6787 | } |
| 6788 | } |
| 6789 | |
| 6790 | for (i = 0; i < set->used; ++i) { |
| 6791 | xp_scnode = &set->val.scnodes[i]; |
| 6792 | if (xp_scnode->in_ctx != 1) { |
| 6793 | /* already checked */ |
| 6794 | continue; |
| 6795 | } |
| 6796 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6797 | 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] | 6798 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6799 | /* no when to check */ |
| 6800 | xp_scnode->in_ctx = 0; |
| 6801 | continue; |
| 6802 | } |
| 6803 | |
| 6804 | node = xp_scnode->scnode; |
| 6805 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6806 | LY_ARRAY_FOR(node->when, u) { |
| 6807 | when = node->when[u]; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6808 | ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6809 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 6810 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6811 | 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] | 6812 | goto cleanup; |
| 6813 | } |
| 6814 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6815 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6816 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6817 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6818 | /* try to find this node in our set */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6819 | uint32_t idx; |
| 6820 | 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] | 6821 | 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] | 6822 | ret = LY_EVALID; |
| 6823 | goto cleanup; |
| 6824 | } |
| 6825 | |
| 6826 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6827 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6828 | } else { |
| 6829 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6830 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6831 | } |
| 6832 | } |
| 6833 | |
| 6834 | /* merge this set into the global when set */ |
| 6835 | lyxp_set_scnode_merge(set, &tmp_set); |
| 6836 | } |
| 6837 | |
| 6838 | /* check when of non-data parents as well */ |
| 6839 | node = node->parent; |
| 6840 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 6841 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6842 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 6843 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6844 | } |
| 6845 | |
| 6846 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6847 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6848 | return ret; |
| 6849 | } |
| 6850 | |
| 6851 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6852 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 6853 | * @param[in] ctx Compile context. |
| 6854 | * @param[in] node Node to check. |
| 6855 | * @return LY_ERR value |
| 6856 | */ |
| 6857 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6858 | 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] | 6859 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6860 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6861 | uint32_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6862 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6863 | uint32_t opts; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6864 | ly_bool input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6865 | struct lysc_when **when = NULL; |
| 6866 | struct lysc_must *musts = NULL; |
| 6867 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6868 | const struct lysc_node *op; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6869 | |
| 6870 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6871 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6872 | if (node->flags & LYS_CONFIG_R) { |
| 6873 | for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent); |
| 6874 | if (op) { |
| 6875 | /* we are actually in output */ |
| 6876 | opts = LYXP_SCNODE_OUTPUT; |
| 6877 | } |
| 6878 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6879 | |
| 6880 | switch (node->nodetype) { |
| 6881 | case LYS_CONTAINER: |
| 6882 | when = ((struct lysc_node_container *)node)->when; |
| 6883 | musts = ((struct lysc_node_container *)node)->musts; |
| 6884 | break; |
| 6885 | case LYS_CHOICE: |
| 6886 | when = ((struct lysc_node_choice *)node)->when; |
| 6887 | break; |
| 6888 | case LYS_LEAF: |
| 6889 | when = ((struct lysc_node_leaf *)node)->when; |
| 6890 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 6891 | break; |
| 6892 | case LYS_LEAFLIST: |
| 6893 | when = ((struct lysc_node_leaflist *)node)->when; |
| 6894 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 6895 | break; |
| 6896 | case LYS_LIST: |
| 6897 | when = ((struct lysc_node_list *)node)->when; |
| 6898 | musts = ((struct lysc_node_list *)node)->musts; |
| 6899 | break; |
| 6900 | case LYS_ANYXML: |
| 6901 | case LYS_ANYDATA: |
| 6902 | when = ((struct lysc_node_anydata *)node)->when; |
| 6903 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 6904 | break; |
| 6905 | case LYS_CASE: |
| 6906 | when = ((struct lysc_node_case *)node)->when; |
| 6907 | break; |
| 6908 | case LYS_NOTIF: |
| 6909 | musts = ((struct lysc_notif *)node)->musts; |
| 6910 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6911 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6912 | case LYS_ACTION: |
| 6913 | /* first process input musts */ |
| 6914 | musts = ((struct lysc_action *)node)->input.musts; |
| 6915 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6916 | default: |
| 6917 | /* nothing to check */ |
| 6918 | break; |
| 6919 | } |
| 6920 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6921 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6922 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6923 | 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] | 6924 | 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] | 6925 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6926 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[u]->cond->expr); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6927 | goto cleanup; |
| 6928 | } |
| 6929 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6930 | ctx->path[0] = '\0'; |
| 6931 | lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6932 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6933 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6934 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 6935 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6936 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6937 | /* 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] | 6938 | 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] | 6939 | schema->name); |
| 6940 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6941 | |
| 6942 | /* check dummy node accessing */ |
| 6943 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6944 | 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] | 6945 | ret = LY_EVALID; |
| 6946 | goto cleanup; |
| 6947 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6948 | } |
| 6949 | } |
| 6950 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6951 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6952 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6953 | LY_CHECK_GOTO(ret, cleanup); |
| 6954 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6955 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6956 | } |
| 6957 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6958 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6959 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6960 | LY_ARRAY_FOR(musts, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6961 | 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] | 6962 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6963 | 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] | 6964 | goto cleanup; |
| 6965 | } |
| 6966 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6967 | ctx->path[0] = '\0'; |
| 6968 | 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] | 6969 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6970 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6971 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +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, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 6974 | 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] | 6975 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6976 | } |
| 6977 | } |
| 6978 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6979 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6980 | } |
| 6981 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6982 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6983 | /* now check output musts */ |
| 6984 | input_done = 1; |
| 6985 | musts = ((struct lysc_action *)node)->output.musts; |
| 6986 | opts = LYXP_SCNODE_OUTPUT; |
| 6987 | goto check_musts; |
| 6988 | } |
| 6989 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6990 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6991 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6992 | return ret; |
| 6993 | } |
| 6994 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 6995 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6996 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 6997 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6998 | const struct lysc_node *target = NULL, *siter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6999 | struct ly_path *p; |
| 7000 | struct lysc_type *type; |
| 7001 | |
| 7002 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7003 | |
| 7004 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 7005 | LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, |
| 7006 | 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] | 7007 | LY_PREF_SCHEMA, lref->path_context, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7008 | |
| 7009 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7010 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7011 | ly_path_free(node->module->ctx, p); |
| 7012 | |
| 7013 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 7014 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7015 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 7016 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
| 7017 | return LY_EVALID; |
| 7018 | } |
| 7019 | |
| 7020 | /* check status */ |
| 7021 | ctx->path[0] = '\0'; |
| 7022 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7023 | ctx->path_len = strlen(ctx->path); |
| 7024 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 7025 | return LY_EVALID; |
| 7026 | } |
| 7027 | ctx->path_len = 1; |
| 7028 | ctx->path[1] = '\0'; |
| 7029 | |
| 7030 | /* check config */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7031 | if (lref->require_instance) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 7032 | 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] | 7033 | if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7034 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
| 7035 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
| 7036 | return LY_EVALID; |
| 7037 | } |
| 7038 | } |
| 7039 | |
| 7040 | /* store the target's type and check for circular chain of leafrefs */ |
| 7041 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 7042 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 7043 | if (type == (struct lysc_type *)lref) { |
| 7044 | /* circular chain detected */ |
| 7045 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7046 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
| 7047 | return LY_EVALID; |
| 7048 | } |
| 7049 | } |
| 7050 | |
| 7051 | /* check if leafref and its target are under common if-features */ |
| 7052 | if (lys_compile_leafref_features_validate(node, target)) { |
| 7053 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7054 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 7055 | " features applicable to the leafref itself.", lref->path->expr); |
| 7056 | return LY_EVALID; |
| 7057 | } |
| 7058 | |
| 7059 | return LY_SUCCESS; |
| 7060 | } |
| 7061 | |
| 7062 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7063 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7064 | { |
| 7065 | struct lysc_ext_instance *ext; |
| 7066 | struct lysp_ext_instance *ext_p = NULL; |
| 7067 | struct lysp_stmt *stmt; |
| 7068 | const struct lys_module *ext_mod; |
| 7069 | LY_ERR ret = LY_SUCCESS; |
| 7070 | |
| 7071 | /* create the parsed extension instance manually */ |
| 7072 | ext_p = calloc(1, sizeof *ext_p); |
| 7073 | 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] | 7074 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "md:annotation", 0, &ext_p->name), cleanup); |
| 7075 | 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] | 7076 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7077 | ext_p->insubstmt_index = 0; |
| 7078 | |
Radek Krejci | 87e2525 | 2020-09-15 13:28:31 +0200 | [diff] [blame] | 7079 | ext_p->child = stmt = calloc(1, sizeof *ext_p->child); |
| 7080 | LY_CHECK_ERR_GOTO(!stmt, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7081 | LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, "type", 0, &stmt->stmt), cleanup); |
| 7082 | 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] | 7083 | stmt->kw = LY_STMT_TYPE; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7084 | |
| 7085 | /* allocate new extension instance */ |
| 7086 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7087 | |
| 7088 | /* manually get extension definition module */ |
| 7089 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7090 | |
| 7091 | /* compile the extension instance */ |
| 7092 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7093 | |
| 7094 | cleanup: |
| 7095 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7096 | free(ext_p); |
| 7097 | return ret; |
| 7098 | } |
| 7099 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7100 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7101 | 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] | 7102 | const struct lys_module *dflt_mod, struct lyd_value *storage) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7103 | { |
| 7104 | LY_ERR ret; |
| 7105 | struct ly_err_item *err = NULL; |
| 7106 | |
| 7107 | ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 7108 | LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err); |
| 7109 | if (err) { |
| 7110 | ly_err_print(err); |
| 7111 | ctx->path[0] = '\0'; |
| 7112 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7113 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7114 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7115 | ly_err_free(err); |
| 7116 | } |
| 7117 | if (!ret) { |
| 7118 | ++storage->realtype->refcount; |
| 7119 | return LY_SUCCESS; |
| 7120 | } |
| 7121 | return ret; |
| 7122 | } |
| 7123 | |
| 7124 | static LY_ERR |
| 7125 | 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] | 7126 | const struct lys_module *dflt_mod) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7127 | { |
| 7128 | LY_ERR ret; |
| 7129 | |
| 7130 | assert(!leaf->dflt); |
| 7131 | |
| 7132 | if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) { |
| 7133 | /* ignore default values for keys and mandatory leaves */ |
| 7134 | return LY_SUCCESS; |
| 7135 | } |
| 7136 | |
| 7137 | /* allocate the default value */ |
| 7138 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 7139 | LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM); |
| 7140 | |
| 7141 | /* store the default value */ |
| 7142 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt); |
| 7143 | if (ret) { |
| 7144 | free(leaf->dflt); |
| 7145 | leaf->dflt = NULL; |
| 7146 | } |
| 7147 | |
| 7148 | return ret; |
| 7149 | } |
| 7150 | |
| 7151 | static LY_ERR |
| 7152 | 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] | 7153 | const struct lys_module *dflt_mod) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7154 | { |
| 7155 | LY_ERR ret; |
| 7156 | LY_ARRAY_COUNT_TYPE orig_count, u, v; |
| 7157 | |
| 7158 | assert(dflt || dflts); |
| 7159 | |
| 7160 | if (llist->dflts) { |
| 7161 | /* there were already some defaults and we are adding new by deviations */ |
| 7162 | assert(dflts); |
| 7163 | orig_count = LY_ARRAY_COUNT(llist->dflts); |
| 7164 | } else { |
| 7165 | orig_count = 0; |
| 7166 | } |
| 7167 | |
| 7168 | /* allocate new items */ |
| 7169 | if (dflts) { |
| 7170 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM); |
| 7171 | } else { |
| 7172 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM); |
| 7173 | } |
| 7174 | |
| 7175 | /* fill each new default value */ |
| 7176 | if (dflts) { |
| 7177 | LY_ARRAY_FOR(dflts, u) { |
| 7178 | llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts); |
| 7179 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod, |
| 7180 | llist->dflts[orig_count + u]); |
| 7181 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret); |
| 7182 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7183 | } |
| 7184 | } else { |
| 7185 | llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts); |
| 7186 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]); |
| 7187 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret); |
| 7188 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7189 | } |
| 7190 | |
| 7191 | /* check default value uniqueness */ |
| 7192 | if (llist->flags & LYS_CONFIG_W) { |
| 7193 | /* configuration data values must be unique - so check the default values */ |
| 7194 | for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) { |
| 7195 | for (v = 0; v < u; ++v) { |
| 7196 | 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] | 7197 | ly_bool dynamic = 0; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7198 | const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic); |
| 7199 | |
| 7200 | lysc_update_path(ctx, llist->parent, llist->name); |
| 7201 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7202 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 7203 | lysc_update_path(ctx, NULL, NULL); |
| 7204 | if (dynamic) { |
| 7205 | free((char *)val); |
| 7206 | } |
| 7207 | return LY_EVALID; |
| 7208 | } |
| 7209 | } |
| 7210 | } |
| 7211 | } |
| 7212 | |
| 7213 | return LY_SUCCESS; |
| 7214 | } |
| 7215 | |
| 7216 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7217 | lys_compile_unres(struct lysc_ctx *ctx) |
| 7218 | { |
| 7219 | struct lysc_node *node; |
| 7220 | struct lysc_type *type, *typeiter; |
| 7221 | struct lysc_type_leafref *lref; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7222 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7223 | uint32_t i; |
| 7224 | |
| 7225 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7226 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7227 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 7228 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7229 | node = ctx->leafrefs.objs[i]; |
| 7230 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7231 | type = ((struct lysc_node_leaf *)node)->type; |
| 7232 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7233 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 7234 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7235 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7236 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7237 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 7238 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 7239 | } |
| 7240 | } |
| 7241 | } |
| 7242 | } |
| 7243 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7244 | /* store pointer to the real type */ |
| 7245 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 7246 | if (type->basetype == LY_TYPE_LEAFREF) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7247 | for (typeiter = ((struct lysc_type_leafref *)type)->realtype; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7248 | typeiter->basetype == LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7249 | typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {} |
| 7250 | ((struct lysc_type_leafref *)type)->realtype = typeiter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7251 | } else if (type->basetype == LY_TYPE_UNION) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7252 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7253 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7254 | 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] | 7255 | typeiter->basetype == LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7256 | typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {} |
| 7257 | ((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] | 7258 | } |
| 7259 | } |
| 7260 | } |
| 7261 | } |
| 7262 | |
| 7263 | /* check xpath */ |
| 7264 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 7265 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 7266 | } |
| 7267 | |
| 7268 | /* finish incomplete default values compilation */ |
| 7269 | for (i = 0; i < ctx->dflts.count; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7270 | struct lysc_incomplete_dflt *r = ctx->dflts.objs[i]; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7271 | if (r->leaf->nodetype == LYS_LEAF) { |
| 7272 | LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod)); |
| 7273 | } else { |
| 7274 | 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] | 7275 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7276 | } |
| 7277 | |
| 7278 | return LY_SUCCESS; |
| 7279 | } |
| 7280 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7281 | LY_ERR |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7282 | lys_compile(struct lys_module *mod, uint32_t options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7283 | { |
| 7284 | struct lysc_ctx ctx = {0}; |
| 7285 | struct lysc_module *mod_c; |
| 7286 | struct lysp_module *sp; |
| 7287 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7288 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7289 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7290 | struct lys_module *m; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7291 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7292 | uint32_t i; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7293 | uint16_t compile_id; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7294 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7295 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7296 | 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] | 7297 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7298 | if (!mod->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7299 | /* just imported modules are not compiled */ |
| 7300 | return LY_SUCCESS; |
| 7301 | } |
| 7302 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7303 | compile_id = ++mod->ctx->module_set_id; |
| 7304 | sp = mod->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7305 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7306 | ctx.ctx = mod->ctx; |
| 7307 | ctx.mod = mod; |
| 7308 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7309 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7310 | ctx.path_len = 1; |
| 7311 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7312 | |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7313 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 7314 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 7315 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7316 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7317 | LY_ARRAY_FOR(sp->imports, u) { |
| 7318 | LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error); |
| 7319 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7320 | |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 7321 | /* features precompilation */ |
| 7322 | if (!mod->features && sp->features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7323 | /* features are compiled directly into the compiled module structure, |
| 7324 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */ |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 7325 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7326 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 7327 | } /* else the features are already precompiled */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7328 | |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 7329 | /* similarly, identities precompilation */ |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 7330 | if (!mod->identities && sp->identities) { |
| 7331 | ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod->identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7332 | LY_CHECK_GOTO(ret, error); |
| 7333 | } |
Radek Krejci | 14915cc | 2020-09-14 17:28:13 +0200 | [diff] [blame] | 7334 | |
| 7335 | /* compile submodules |
| 7336 | * - must be between features/identities precompilation and finishing their compilation to cover features/identities from |
| 7337 | * submodules */ |
| 7338 | LY_ARRAY_FOR(sp->includes, u) { |
| 7339 | LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error); |
| 7340 | } |
| 7341 | |
| 7342 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7343 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7344 | * are present. */ |
| 7345 | LY_ARRAY_FOR(sp->features, u) { |
| 7346 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod->features); |
| 7347 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7348 | } |
| 7349 | lysc_update_path(&ctx, NULL, "{submodule}"); |
| 7350 | LY_ARRAY_FOR(sp->includes, v) { |
| 7351 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
| 7352 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
| 7353 | ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod->features); |
| 7354 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7355 | } |
| 7356 | lysc_update_path(&ctx, NULL, NULL); |
| 7357 | } |
| 7358 | lysc_update_path(&ctx, NULL, NULL); |
| 7359 | |
| 7360 | /* identities, work similarly to features with the precompilation */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7361 | if (sp->identities) { |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 7362 | LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod->identities), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7363 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7364 | lysc_update_path(&ctx, NULL, "{submodule}"); |
| 7365 | LY_ARRAY_FOR(sp->includes, v) { |
| 7366 | if (sp->includes[v].submodule->identities) { |
| 7367 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 80d281e | 2020-09-14 17:42:54 +0200 | [diff] [blame] | 7368 | ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod->identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7369 | LY_CHECK_GOTO(ret, error); |
| 7370 | lysc_update_path(&ctx, NULL, NULL); |
| 7371 | } |
| 7372 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7373 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7374 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7375 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7376 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7377 | 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] | 7378 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7379 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7380 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7381 | 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] | 7382 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7383 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7384 | 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] | 7385 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7386 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7387 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7388 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7389 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7390 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7391 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7392 | /* extension instances TODO cover extension instances from submodules */ |
| 7393 | 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] | 7394 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7395 | /* finish compilation for all unresolved items in the context */ |
| 7396 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7397 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7398 | /* validate non-instantiated groupings from the parsed schema, |
| 7399 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7400 | ctx.options |= LYSC_OPT_GROUPING; |
| 7401 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7402 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7403 | 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] | 7404 | } |
| 7405 | } |
| 7406 | LY_LIST_FOR(sp->data, node_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7407 | grps = (struct lysp_grp *)lysp_node_groupings(node_p); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7408 | LY_ARRAY_FOR(grps, u) { |
| 7409 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7410 | 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] | 7411 | } |
| 7412 | } |
| 7413 | } |
| 7414 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7415 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7416 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7417 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7418 | } |
| 7419 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7420 | #if 0 |
| 7421 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7422 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7423 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7424 | * the anotation definitions available in the internal schema structure. */ |
| 7425 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7426 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7427 | lys_free(mod, NULL, 1, 1); |
| 7428 | return NULL; |
| 7429 | } |
| 7430 | } |
| 7431 | #endif |
| 7432 | |
| 7433 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7434 | if (!strcmp(mod->name, "ietf-netconf-with-defaults")) { |
| 7435 | 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] | 7436 | } |
| 7437 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7438 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7439 | ly_set_erase(&ctx.xpath, NULL); |
| 7440 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7441 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7442 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7443 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7444 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7445 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7446 | lysp_module_free(mod->parsed); |
| 7447 | mod->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7448 | } |
| 7449 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7450 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7451 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7452 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7453 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7454 | if (m->implemented > 1) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7455 | m->implemented = 1; |
| 7456 | } |
| 7457 | } |
| 7458 | } |
| 7459 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7460 | return LY_SUCCESS; |
| 7461 | |
| 7462 | error: |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7463 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7464 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7465 | ly_set_erase(&ctx.xpath, NULL); |
| 7466 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7467 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7468 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7469 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7470 | lysc_module_free(mod_c, NULL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 7471 | mod->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7472 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7473 | /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency |
| 7474 | * of this module, since this module can be also compiled from dependency, there can be some other modules being |
| 7475 | * 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] | 7476 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7477 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7478 | if ((m->implemented >= compile_id) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7479 | /* revert features list to the precompiled state */ |
| 7480 | lys_feature_precompile_revert(&ctx, m); |
| 7481 | /* mark module as imported-only / not-implemented */ |
| 7482 | m->implemented = 0; |
| 7483 | /* free the compiled version of the module */ |
| 7484 | lysc_module_free(m->compiled, NULL); |
| 7485 | m->compiled = NULL; |
| 7486 | } |
| 7487 | } |
| 7488 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7489 | return ret; |
| 7490 | } |