Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema tree implementation |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 16 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | #include <stdint.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 21 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 24 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 26 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | #include "dict.h" |
| 29 | #include "log.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 30 | #include "path.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 31 | #include "parser.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 32 | #include "parser_schema.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 33 | #include "plugins_exts.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | #include "plugins_types.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 35 | #include "plugins_exts_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "set.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 37 | #include "tree.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 38 | #include "tree_data.h" |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 39 | #include "tree_data_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 40 | #include "tree_schema.h" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 41 | #include "tree_schema_internal.h" |
| 42 | #include "xpath.h" |
| 43 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 44 | static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 45 | void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod); |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 46 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 47 | /** |
| 48 | * @brief Duplicate string into dictionary |
| 49 | * @param[in] CTX libyang context of the dictionary. |
| 50 | * @param[in] ORIG String to duplicate. |
| 51 | * @param[out] DUP Where to store the result. |
| 52 | */ |
| 53 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 54 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 55 | #define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 56 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 57 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 58 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 59 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 60 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 61 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 62 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 63 | } \ |
| 64 | } |
| 65 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 66 | #define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 67 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 68 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 69 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 70 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 71 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 72 | RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 73 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 74 | } \ |
| 75 | } |
| 76 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 77 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \ |
| 78 | if (EXTS_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 79 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \ |
| 80 | for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 81 | LY_ARRAY_INCREMENT(EXT_C); \ |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 82 | RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 83 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 84 | } \ |
| 85 | } |
| 86 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 87 | #define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 88 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 89 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 90 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 91 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 92 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 93 | RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 94 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 95 | } \ |
| 96 | } |
| 97 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 98 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 99 | if (MEMBER_P) { \ |
| 100 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 101 | LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 102 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 103 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 104 | } |
| 105 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 106 | #define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 107 | if (MEMBER_P) { \ |
| 108 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 109 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 110 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 111 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 112 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 113 | } |
| 114 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 115 | #define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 116 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 117 | for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 118 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 119 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 120 | return LY_EVALID; \ |
| 121 | } \ |
| 122 | } \ |
| 123 | } |
| 124 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 125 | #define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 126 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 127 | for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \ |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 128 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \ |
| 129 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 130 | return LY_EVALID; \ |
| 131 | } \ |
| 132 | } \ |
| 133 | } |
| 134 | |
| 135 | struct lysc_ext * |
| 136 | lysc_ext_dup(struct lysc_ext *orig) |
| 137 | { |
| 138 | ++orig->refcount; |
| 139 | return orig; |
| 140 | } |
| 141 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 142 | static struct lysc_ext_instance * |
| 143 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 144 | { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 145 | /* TODO - extensions, increase refcount */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 146 | (void) ctx; |
| 147 | (void) orig; |
| 148 | return NULL; |
| 149 | } |
| 150 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 151 | static LY_ERR |
| 152 | lysc_incomplete_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 153 | struct lys_module *dflt_mod) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 154 | { |
| 155 | struct lysc_incomplete_dflt *r; |
| 156 | uint32_t i; |
| 157 | |
| 158 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 159 | r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i]; |
| 160 | if (r->leaf == leaf) { |
| 161 | /* just replace the default */ |
| 162 | r->dflt = dflt; |
| 163 | return LY_SUCCESS; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | r = malloc(sizeof *r); |
| 168 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 169 | r->leaf = leaf; |
| 170 | r->dflt = dflt; |
| 171 | r->dflts = NULL; |
| 172 | r->dflt_mod = dflt_mod; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 173 | 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] | 174 | |
| 175 | return LY_SUCCESS; |
| 176 | } |
| 177 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 178 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 179 | * @brief Add record into the compile context's list of incomplete default values. |
| 180 | * @param[in] ctx Compile context with the incomplete default values list. |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 181 | * @param[in] term Term context node with the default value. |
| 182 | * @param[in] value String default value. |
| 183 | * @param[in] val_len Length of @p value. |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 184 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 185 | * @return LY_EMEM in case of memory allocation failure. |
| 186 | * @return LY_SUCCESS |
| 187 | */ |
| 188 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 189 | lysc_incomplete_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char **dflts, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 190 | struct lys_module *dflt_mod) |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 191 | { |
| 192 | struct lysc_incomplete_dflt *r; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 193 | uint32_t i; |
| 194 | |
| 195 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 196 | r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i]; |
| 197 | if (r->llist == llist) { |
| 198 | /* just replace the defaults */ |
| 199 | r->dflts = dflts; |
| 200 | return LY_SUCCESS; |
| 201 | } |
| 202 | } |
| 203 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 204 | r = malloc(sizeof *r); |
| 205 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 206 | r->llist = llist; |
| 207 | r->dflt = NULL; |
| 208 | r->dflts = dflts; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 209 | r->dflt_mod = dflt_mod; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 210 | 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] | 211 | |
| 212 | return LY_SUCCESS; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 217 | * @param[in] ctx Compile context with the incomplete default values list. |
| 218 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 219 | */ |
| 220 | static void |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 221 | lysc_incomplete_dflt_remove(struct lysc_ctx *ctx, struct lysc_node *term) |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 222 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 223 | uint32_t u; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 224 | struct lysc_incomplete_dflt *r; |
| 225 | |
| 226 | for (u = 0; u < ctx->dflts.count; ++u) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 227 | r = ctx->dflts.objs[u]; |
| 228 | if (r->leaf == (struct lysc_node_leaf *)term) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 229 | free(ctx->dflts.objs[u]); |
| 230 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 231 | --ctx->dflts.count; |
| 232 | return; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 237 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 238 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 239 | { |
| 240 | int len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 241 | uint8_t nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 242 | |
| 243 | if (!name) { |
| 244 | /* removing last path segment */ |
| 245 | if (ctx->path[ctx->path_len - 1] == '}') { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 246 | for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {} |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 247 | if (ctx->path[ctx->path_len] == '=') { |
| 248 | ctx->path[ctx->path_len++] = '}'; |
| 249 | } else { |
| 250 | /* not a top-level special tag, remove also preceiding '/' */ |
| 251 | goto remove_nodelevel; |
| 252 | } |
| 253 | } else { |
| 254 | remove_nodelevel: |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 255 | for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {} |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 256 | if (ctx->path_len == 0) { |
| 257 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 258 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | /* set new terminating NULL-byte */ |
| 262 | ctx->path[ctx->path_len] = '\0'; |
| 263 | } else { |
| 264 | if (ctx->path_len > 1) { |
| 265 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 266 | /* extension of the special tag */ |
| 267 | nextlevel = 2; |
| 268 | --ctx->path_len; |
| 269 | } else { |
| 270 | /* there is already some path, so add next level */ |
| 271 | nextlevel = 1; |
| 272 | } |
| 273 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 274 | |
| 275 | if (nextlevel != 2) { |
| 276 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 277 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 278 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 279 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 280 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 281 | } |
| 282 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 283 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 284 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 285 | if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) { |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 286 | /* output truncated */ |
| 287 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 288 | } else { |
| 289 | ctx->path_len += len; |
| 290 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 295 | * @brief Duplicate the compiled pattern structure. |
| 296 | * |
| 297 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 298 | * |
| 299 | * @param[in] orig The pattern structure to duplicate. |
| 300 | * @return The duplicated structure to use. |
| 301 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 302 | static struct lysc_pattern * |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 303 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 304 | { |
| 305 | ++orig->refcount; |
| 306 | return orig; |
| 307 | } |
| 308 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 309 | /** |
| 310 | * @brief Duplicate the array of compiled patterns. |
| 311 | * |
| 312 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 313 | * |
| 314 | * @param[in] ctx Libyang context for logging. |
| 315 | * @param[in] orig The patterns sized array to duplicate. |
| 316 | * @return New sized array as a copy of @p orig. |
| 317 | * @return NULL in case of memory allocation error. |
| 318 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 319 | static struct lysc_pattern ** |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 320 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 321 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 322 | struct lysc_pattern **dup = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 323 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 324 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 325 | assert(orig); |
| 326 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 327 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 328 | LY_ARRAY_FOR(orig, u) { |
| 329 | dup[u] = lysc_pattern_dup(orig[u]); |
| 330 | LY_ARRAY_INCREMENT(dup); |
| 331 | } |
| 332 | return dup; |
| 333 | } |
| 334 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 335 | /** |
| 336 | * @brief Duplicate compiled range structure. |
| 337 | * |
| 338 | * @param[in] ctx Libyang context for logging. |
| 339 | * @param[in] orig The range structure to be duplicated. |
| 340 | * @return New compiled range structure as a copy of @p orig. |
| 341 | * @return NULL in case of memory allocation error. |
| 342 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 343 | struct lysc_range * |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 344 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 345 | { |
| 346 | struct lysc_range *dup; |
| 347 | LY_ERR ret; |
| 348 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 349 | assert(orig); |
| 350 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 351 | dup = calloc(1, sizeof *dup); |
| 352 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 353 | if (orig->parts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 354 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup); |
| 355 | LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts); |
| 356 | memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 357 | } |
| 358 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 359 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 360 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 361 | |
| 362 | return dup; |
| 363 | cleanup: |
| 364 | free(dup); |
| 365 | (void) ret; /* set but not used due to the return type */ |
| 366 | return NULL; |
| 367 | } |
| 368 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 369 | /** |
| 370 | * @brief Stack for processing if-feature expressions. |
| 371 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 372 | struct iff_stack { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 373 | size_t size; /**< number of items in the stack */ |
| 374 | size_t index; /**< first empty item */ |
| 375 | uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 376 | }; |
| 377 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 378 | /** |
| 379 | * @brief Add @ref ifftokens into the stack. |
| 380 | * @param[in] stack The if-feature stack to use. |
| 381 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 382 | * @return LY_EMEM in case of memory allocation error |
| 383 | * @return LY_ESUCCESS if the value successfully stored. |
| 384 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 385 | static LY_ERR |
| 386 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 387 | { |
| 388 | if (stack->index == stack->size) { |
| 389 | stack->size += 4; |
| 390 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 391 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 392 | } |
| 393 | stack->stack[stack->index++] = value; |
| 394 | return LY_SUCCESS; |
| 395 | } |
| 396 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 397 | /** |
| 398 | * @brief Get (and remove) the last item form the stack. |
| 399 | * @param[in] stack The if-feature stack to use. |
| 400 | * @return The value from the top of the stack. |
| 401 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 402 | static uint8_t |
| 403 | iff_stack_pop(struct iff_stack *stack) |
| 404 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 405 | assert(stack && stack->index); |
| 406 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 407 | stack->index--; |
| 408 | return stack->stack[stack->index]; |
| 409 | } |
| 410 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 411 | /** |
| 412 | * @brief Clean up the stack. |
| 413 | * @param[in] stack The if-feature stack to use. |
| 414 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 415 | static void |
| 416 | iff_stack_clean(struct iff_stack *stack) |
| 417 | { |
| 418 | stack->size = 0; |
| 419 | free(stack->stack); |
| 420 | } |
| 421 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 422 | /** |
| 423 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 424 | * (libyang format of the if-feature expression). |
| 425 | * @param[in,out] list The 2bits array to modify. |
| 426 | * @param[in] op The operand (@ref ifftokens) to store. |
| 427 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 428 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 429 | static void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 430 | iff_setop(uint8_t *list, uint8_t op, size_t pos) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 431 | { |
| 432 | uint8_t *item; |
| 433 | uint8_t mask = 3; |
| 434 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 435 | assert(op <= 3); /* max 2 bits */ |
| 436 | |
| 437 | item = &list[pos / 4]; |
| 438 | mask = mask << 2 * (pos % 4); |
| 439 | *item = (*item) & ~mask; |
| 440 | *item = (*item) | (op << 2 * (pos % 4)); |
| 441 | } |
| 442 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 443 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 444 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 445 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 446 | /** |
| 447 | * @brief Find a feature of the given name and referenced in the given module. |
| 448 | * |
| 449 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 450 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 451 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 452 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 453 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 454 | * assigned till that time will be still valid. |
| 455 | * |
| 456 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 457 | * @param[in] name Name of the feature including possible prefix. |
| 458 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 459 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 460 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 461 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 462 | 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] | 463 | { |
| 464 | size_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 465 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 466 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 467 | |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 468 | assert(mod); |
| 469 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 470 | for (i = 0; i < len; ++i) { |
| 471 | if (name[i] == ':') { |
| 472 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 473 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 474 | LY_CHECK_RET(!mod, NULL); |
| 475 | |
| 476 | name = &name[i + 1]; |
| 477 | len = len - i - 1; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 482 | if (mod->implemented) { |
| 483 | /* module is implemented so there is already the compiled schema */ |
| 484 | flist = mod->compiled->features; |
| 485 | } else { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 486 | flist = mod->dis_features; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 487 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 488 | LY_ARRAY_FOR(flist, u) { |
| 489 | f = &flist[u]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 490 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 491 | return f; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | return NULL; |
| 496 | } |
| 497 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 498 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 499 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 500 | */ |
| 501 | static LY_ERR |
| 502 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 503 | { |
| 504 | LY_ERR ret = LY_SUCCESS; |
| 505 | |
| 506 | if (!ext_p->compiled) { |
| 507 | lysc_update_path(ctx, NULL, "{extension}"); |
| 508 | lysc_update_path(ctx, NULL, ext_p->name); |
| 509 | |
| 510 | /* compile the extension definition */ |
| 511 | ext_p->compiled = calloc(1, sizeof **ext); |
| 512 | ext_p->compiled->refcount = 1; |
| 513 | DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name); |
| 514 | DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument); |
| 515 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 516 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 517 | |
| 518 | lysc_update_path(ctx, NULL, NULL); |
| 519 | lysc_update_path(ctx, NULL, NULL); |
| 520 | |
| 521 | /* find extension definition plugin */ |
| 522 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 523 | } |
| 524 | |
| 525 | *ext = lysc_ext_dup(ext_p->compiled); |
| 526 | |
| 527 | done: |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 532 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 533 | * |
| 534 | * @param[in] ctx Compilation context. |
| 535 | * @param[in] ext_p Parsed extension instance. |
| 536 | * @param[in,out] ext Prepared compiled extension instance. |
| 537 | * @param[in] parent Extension instance parent. |
| 538 | * @param[in] parent_type Extension instance parent type. |
| 539 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 540 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 541 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 542 | 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] | 543 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 544 | { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 545 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 546 | const char *name; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 547 | size_t u; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 548 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 549 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 550 | |
| 551 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 552 | ext->insubstmt = ext_p->insubstmt; |
| 553 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 554 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 555 | ext->parent = parent; |
| 556 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 557 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 558 | 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] | 559 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 560 | /* get module where the extension definition should be placed */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 561 | 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] | 562 | if (ext_p->yin) { |
| 563 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 564 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 565 | * YANG (import) prefix must be done here. */ |
| 566 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
| 567 | prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0); |
| 568 | u = 0; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 569 | } else { |
| 570 | assert(ctx->mod_def->parsed); |
| 571 | LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) { |
| 572 | 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] | 573 | char *s; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 574 | 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] | 575 | ret = LY_EMEM, cleanup); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 576 | prefixed_name = lydict_insert_zc(ctx->ctx, s); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 577 | u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 578 | break; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | if (!prefixed_name) { |
| 583 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 584 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
| 585 | goto cleanup; |
| 586 | } |
| 587 | } else { |
| 588 | prefixed_name = ext_p->name; |
| 589 | } |
| 590 | lysc_update_path(ctx, NULL, prefixed_name); |
| 591 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 592 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 593 | 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] | 594 | if (!ext_mod) { |
| 595 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 596 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
| 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); |
| 602 | goto cleanup; |
| 603 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 604 | } |
| 605 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 606 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 607 | /* find the parsed extension definition there */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 608 | LY_ARRAY_FOR(ext_mod->parsed->extensions, v) { |
| 609 | if (!strcmp(name, ext_mod->parsed->extensions[v].name)) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 610 | /* compile extension definition and assign it */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 611 | LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 612 | break; |
| 613 | } |
| 614 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 615 | if (!ext->def) { |
| 616 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 617 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
| 618 | goto cleanup; |
| 619 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 620 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 621 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 622 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 623 | * 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] | 624 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 625 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 626 | struct lysp_stmt *stmt = NULL; |
| 627 | |
| 628 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 629 | /* ... argument was the first XML child element */ |
| 630 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 631 | /* TODO check namespace of the statement */ |
| 632 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 633 | stmt = ext_p->child; |
| 634 | } |
| 635 | } |
| 636 | } else { |
| 637 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 638 | * with LYS_YIN_ATTR flag */ |
| 639 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 640 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 641 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 642 | break; |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | if (!stmt) { |
| 647 | /* missing extension's argument */ |
| 648 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 649 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
| 650 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 651 | |
| 652 | } |
| 653 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 654 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 655 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 656 | if (prefixed_name != ext_p->name) { |
| 657 | lydict_remove(ctx->ctx, ext_p->name); |
| 658 | ext_p->name = prefixed_name; |
| 659 | if (!ext_p->argument && ext->argument) { |
| 660 | ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0); |
| 661 | } |
| 662 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 663 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 664 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 665 | if (ext->argument) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 666 | lysc_update_path(ctx, (struct lysc_node *)ext, ext->argument); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 667 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 668 | LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 669 | if (ext->argument) { |
| 670 | lysc_update_path(ctx, NULL, NULL); |
| 671 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 672 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 673 | ext_p->compiled = ext; |
| 674 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 675 | ret = LY_SUCCESS; |
| 676 | cleanup: |
| 677 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 678 | lydict_remove(ctx->ctx, prefixed_name); |
| 679 | } |
| 680 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 681 | lysc_update_path(ctx, NULL, NULL); |
| 682 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 683 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 684 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 688 | * @brief Compile information from the if-feature statement |
| 689 | * @param[in] ctx Compile context. |
| 690 | * @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] | 691 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 692 | * @return LY_ERR value. |
| 693 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 694 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 695 | 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] | 696 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 697 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 698 | const char *c = *value; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 699 | int64_t i, j; |
| 700 | int8_t op_len, last_not = 0, checkversion = 0; |
| 701 | 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] | 702 | uint8_t op; |
| 703 | struct iff_stack stack = {0, 0, NULL}; |
| 704 | struct lysc_feature *f; |
| 705 | |
| 706 | assert(c); |
| 707 | |
| 708 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 709 | for (i = j = 0; c[i]; i++) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 710 | if (c[i] == '(') { |
| 711 | j++; |
| 712 | checkversion = 1; |
| 713 | continue; |
| 714 | } else if (c[i] == ')') { |
| 715 | j--; |
| 716 | continue; |
| 717 | } else if (isspace(c[i])) { |
| 718 | checkversion = 1; |
| 719 | continue; |
| 720 | } |
| 721 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 722 | if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) { |
| 723 | uint64_t spaces; |
| 724 | for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++); |
| 725 | if (c[i + op_len + spaces] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 726 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 727 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 728 | return LY_EVALID; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 729 | } else if (!isspace(c[i + op_len])) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 730 | /* feature name starting with the not/and/or */ |
| 731 | last_not = 0; |
| 732 | f_size++; |
| 733 | } else if (c[i] == 'n') { /* not operation */ |
| 734 | if (last_not) { |
| 735 | /* double not */ |
| 736 | expr_size = expr_size - 2; |
| 737 | last_not = 0; |
| 738 | } else { |
| 739 | last_not = 1; |
| 740 | } |
| 741 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 742 | if (f_exp != f_size) { |
| 743 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 744 | "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] | 745 | return LY_EVALID; |
| 746 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 747 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 748 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 749 | /* not a not operation */ |
| 750 | last_not = 0; |
| 751 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 752 | i += op_len; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 753 | } else { |
| 754 | f_size++; |
| 755 | last_not = 0; |
| 756 | } |
| 757 | expr_size++; |
| 758 | |
| 759 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 760 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 761 | i--; |
| 762 | break; |
| 763 | } |
| 764 | i++; |
| 765 | } |
| 766 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 767 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 768 | /* not matching count of ( and ) */ |
| 769 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 770 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 771 | return LY_EVALID; |
| 772 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 773 | if (f_exp != f_size) { |
| 774 | /* features do not match the needed arguments for the logical operations */ |
| 775 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 776 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 777 | "the required number of operands for the operations.", *value); |
| 778 | return LY_EVALID; |
| 779 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 780 | |
| 781 | if (checkversion || expr_size > 1) { |
| 782 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 783 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 784 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 785 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 786 | return LY_EVALID; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /* allocate the memory */ |
| 791 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 792 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 793 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 794 | 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] | 795 | |
| 796 | stack.size = expr_size; |
| 797 | f_size--; expr_size--; /* used as indexes from now */ |
| 798 | |
| 799 | for (i--; i >= 0; i--) { |
| 800 | if (c[i] == ')') { |
| 801 | /* push it on stack */ |
| 802 | iff_stack_push(&stack, LYS_IFF_RP); |
| 803 | continue; |
| 804 | } else if (c[i] == '(') { |
| 805 | /* pop from the stack into result all operators until ) */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 806 | while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 807 | iff_setop(iff->expr, op, expr_size--); |
| 808 | } |
| 809 | continue; |
| 810 | } else if (isspace(c[i])) { |
| 811 | continue; |
| 812 | } |
| 813 | |
| 814 | /* end of operator or operand -> find beginning and get what is it */ |
| 815 | j = i + 1; |
| 816 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 817 | i--; |
| 818 | } |
| 819 | i++; /* go back by one step */ |
| 820 | |
| 821 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 822 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 823 | /* double not */ |
| 824 | iff_stack_pop(&stack); |
| 825 | } else { |
| 826 | /* not has the highest priority, so do not pop from the stack |
| 827 | * as in case of AND and OR */ |
| 828 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 829 | } |
| 830 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 831 | /* as for OR - pop from the stack all operators with the same or higher |
| 832 | * priority and store them to the result, then push the AND to the stack */ |
| 833 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 834 | op = iff_stack_pop(&stack); |
| 835 | iff_setop(iff->expr, op, expr_size--); |
| 836 | } |
| 837 | iff_stack_push(&stack, LYS_IFF_AND); |
| 838 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 839 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 840 | op = iff_stack_pop(&stack); |
| 841 | iff_setop(iff->expr, op, expr_size--); |
| 842 | } |
| 843 | iff_stack_push(&stack, LYS_IFF_OR); |
| 844 | } else { |
| 845 | /* feature name, length is j - i */ |
| 846 | |
| 847 | /* add it to the expression */ |
| 848 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 849 | |
| 850 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 851 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 852 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 853 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 854 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 855 | iff->features[f_size] = f; |
| 856 | LY_ARRAY_INCREMENT(iff->features); |
| 857 | f_size--; |
| 858 | } |
| 859 | } |
| 860 | while (stack.index) { |
| 861 | op = iff_stack_pop(&stack); |
| 862 | iff_setop(iff->expr, op, expr_size--); |
| 863 | } |
| 864 | |
| 865 | if (++expr_size || ++f_size) { |
| 866 | /* not all expected operators and operands found */ |
| 867 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 868 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 869 | rc = LY_EINT; |
| 870 | } else { |
| 871 | rc = LY_SUCCESS; |
| 872 | } |
| 873 | |
| 874 | error: |
| 875 | /* cleanup */ |
| 876 | iff_stack_clean(&stack); |
| 877 | |
| 878 | return rc; |
| 879 | } |
| 880 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 881 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 882 | * @brief Get the XPath context node for the given schema node. |
| 883 | * @param[in] start The schema node where the XPath expression appears. |
| 884 | * @return The context node to evaluate XPath expression in given schema node. |
| 885 | * @return NULL in case the context node is the root node. |
| 886 | */ |
| 887 | static struct lysc_node * |
| 888 | lysc_xpath_context(struct lysc_node *start) |
| 889 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 890 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 891 | start = start->parent) {} |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 892 | return start; |
| 893 | } |
| 894 | |
| 895 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 896 | * @brief Compile information from the when statement |
| 897 | * @param[in] ctx Compile context. |
| 898 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 899 | * @param[in] flags Flags of the node with the "when" defiition. |
| 900 | * @param[in] node Node that inherited the "when" definition, must be connected to parents. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 901 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 902 | * @return LY_ERR value. |
| 903 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 904 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 905 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 906 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 907 | LY_ERR ret = LY_SUCCESS; |
| 908 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 909 | *when = calloc(1, sizeof **when); |
| 910 | (*when)->refcount = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 911 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 912 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 913 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 914 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 915 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 916 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 917 | COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 918 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 919 | |
| 920 | done: |
| 921 | return ret; |
| 922 | } |
| 923 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 924 | /** |
| 925 | * @brief Compile information from the must statement |
| 926 | * @param[in] ctx Compile context. |
| 927 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 928 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 929 | * @return LY_ERR value. |
| 930 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 931 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 932 | lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 933 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 934 | LY_ERR ret = LY_SUCCESS; |
| 935 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 936 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 937 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 938 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 939 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 940 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 941 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 942 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 943 | COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 944 | |
| 945 | done: |
| 946 | return ret; |
| 947 | } |
| 948 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 949 | /** |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 950 | * @brief Compile information in the import statement - make sure there is the target module |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 951 | * @param[in] ctx Compile context. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 952 | * @param[in] imp_p The parsed import statement structure to fill the module to. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 953 | * @return LY_ERR value. |
| 954 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 955 | static LY_ERR |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 956 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 957 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 958 | const struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 959 | LY_ERR ret = LY_SUCCESS; |
| 960 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 961 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 962 | * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added, |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 963 | * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 964 | if (!imp_p->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 965 | /* try to use filepath if present */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 966 | if (imp_p->module->filepath) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 967 | struct ly_in *in; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 968 | if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 969 | LOGINT(ctx->ctx); |
| 970 | } else { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 971 | LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)], |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 972 | ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 973 | if (mod != imp_p->module) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 974 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 975 | imp_p->module->filepath, imp_p->module->name); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 976 | mod = NULL; |
| 977 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 978 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 979 | ly_in_free(in, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 980 | } |
| 981 | if (!mod) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 982 | if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, (struct lys_module **)&mod)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 983 | LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.", |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 984 | imp_p->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 985 | return LY_ENOTFOUND; |
| 986 | } |
| 987 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 988 | } |
| 989 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 990 | return ret; |
| 991 | } |
| 992 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 993 | LY_ERR |
| 994 | lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 995 | struct lysp_ident *identities_p, struct lysc_ident **identities) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 996 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 997 | LY_ARRAY_COUNT_TYPE offset = 0, u, v; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 998 | struct lysc_ctx context = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 999 | LY_ERR ret = LY_SUCCESS; |
| 1000 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1001 | assert(ctx_sc || ctx); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1002 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1003 | if (!ctx_sc) { |
| 1004 | context.ctx = ctx; |
| 1005 | context.mod = module; |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 1006 | context.mod_def = module; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1007 | context.path_len = 1; |
| 1008 | context.path[0] = '/'; |
| 1009 | ctx_sc = &context; |
| 1010 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1011 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1012 | if (!identities_p) { |
| 1013 | return LY_SUCCESS; |
| 1014 | } |
| 1015 | if (*identities) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1016 | offset = LY_ARRAY_COUNT(*identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | lysc_update_path(ctx_sc, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1020 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1021 | LY_ARRAY_FOR(identities_p, u) { |
| 1022 | lysc_update_path(ctx_sc, NULL, identities_p[u].name); |
| 1023 | |
| 1024 | LY_ARRAY_INCREMENT(*identities); |
| 1025 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name); |
| 1026 | DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name); |
| 1027 | DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc); |
| 1028 | DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref); |
| 1029 | (*identities)[offset + u].module = ctx_sc->mod; |
| 1030 | COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v, |
| 1031 | lys_compile_iffeature, ret, done); |
| 1032 | /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */ |
| 1033 | COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u], |
| 1034 | LYEXT_PAR_IDENT, ret, done); |
| 1035 | (*identities)[offset + u].flags = identities_p[u].flags; |
| 1036 | |
| 1037 | lysc_update_path(ctx_sc, NULL, NULL); |
| 1038 | } |
| 1039 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1040 | done: |
| 1041 | return ret; |
| 1042 | } |
| 1043 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1044 | /** |
| 1045 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 1046 | * |
| 1047 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 1048 | * |
| 1049 | * @param[in] ctx Compile context for logging. |
| 1050 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 1051 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 1052 | * @return LY_SUCCESS if everything is ok. |
| 1053 | * @return LY_EVALID if the identity is derived from itself. |
| 1054 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1055 | static LY_ERR |
| 1056 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 1057 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1058 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1059 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1060 | struct ly_set recursion = {0}; |
| 1061 | struct lysc_ident *drv; |
| 1062 | |
| 1063 | if (!derived) { |
| 1064 | return LY_SUCCESS; |
| 1065 | } |
| 1066 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1067 | for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1068 | if (ident == derived[u]) { |
| 1069 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1070 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1071 | ret = LY_EVALID; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1072 | goto cleanup; |
| 1073 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1074 | ret = ly_set_add(&recursion, derived[u], 0, NULL); |
| 1075 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | for (v = 0; v < recursion.count; ++v) { |
| 1079 | drv = recursion.objs[v]; |
| 1080 | if (!drv->derived) { |
| 1081 | continue; |
| 1082 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1083 | for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1084 | if (ident == drv->derived[u]) { |
| 1085 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1086 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1087 | ret = LY_EVALID; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1088 | goto cleanup; |
| 1089 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1090 | ret = ly_set_add(&recursion, drv->derived[u], 0, NULL); |
| 1091 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1092 | } |
| 1093 | } |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1094 | |
| 1095 | cleanup: |
| 1096 | ly_set_erase(&recursion, NULL); |
| 1097 | return ret; |
| 1098 | } |
| 1099 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1100 | /** |
| 1101 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1102 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1103 | * 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] | 1104 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1105 | * to distinguish these two use cases. |
| 1106 | * |
| 1107 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1108 | * @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] | 1109 | * @param[in] ident Referencing identity to work with, NULL for identityref. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1110 | * @param[in] bases Array of bases of identityref to fill in. |
| 1111 | * @return LY_ERR value. |
| 1112 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1113 | static LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1114 | 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] | 1115 | struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1116 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1117 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1118 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1119 | struct lys_module *mod; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1120 | struct lysc_ident **idref, *identities; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1121 | |
| 1122 | assert(ident || bases); |
| 1123 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1124 | if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1125 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1126 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1127 | return LY_EVALID; |
| 1128 | } |
| 1129 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1130 | LY_ARRAY_FOR(bases_p, u) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1131 | s = strchr(bases_p[u], ':'); |
| 1132 | if (s) { |
| 1133 | /* prefixed identity */ |
| 1134 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1135 | 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] | 1136 | } else { |
| 1137 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1138 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1139 | } |
| 1140 | if (!mod) { |
| 1141 | if (ident) { |
| 1142 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1143 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1144 | } else { |
| 1145 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1146 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1147 | } |
| 1148 | return LY_EVALID; |
| 1149 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1150 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1151 | idref = NULL; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1152 | if (mod->compiled) { |
| 1153 | identities = mod->compiled->identities; |
| 1154 | } else { |
| 1155 | identities = mod->dis_identities; |
| 1156 | } |
| 1157 | LY_ARRAY_FOR(identities, v) { |
| 1158 | if (!strcmp(name, identities[v].name)) { |
| 1159 | if (ident) { |
| 1160 | if (ident == &identities[v]) { |
| 1161 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1162 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1163 | return LY_EVALID; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1164 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1165 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived)); |
| 1166 | /* we have match! store the backlink */ |
| 1167 | LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM); |
| 1168 | *idref = ident; |
| 1169 | } else { |
| 1170 | /* we have match! store the found identity */ |
| 1171 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
| 1172 | *idref = &identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1173 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1174 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | if (!idref || !(*idref)) { |
| 1178 | if (ident) { |
| 1179 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1180 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1181 | } else { |
| 1182 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1183 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1184 | } |
| 1185 | return LY_EVALID; |
| 1186 | } |
| 1187 | } |
| 1188 | return LY_SUCCESS; |
| 1189 | } |
| 1190 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1191 | /** |
| 1192 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1193 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1194 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1195 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1196 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1197 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1198 | static LY_ERR |
| 1199 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1200 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1201 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1202 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1203 | lysc_update_path(ctx, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1204 | for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1205 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1206 | continue; |
| 1207 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1208 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1209 | 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] | 1210 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1211 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1212 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1213 | return LY_SUCCESS; |
| 1214 | } |
| 1215 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1216 | LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1217 | 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] | 1218 | struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1219 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1220 | LY_ARRAY_COUNT_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1221 | struct lysc_ctx context = {0}; |
| 1222 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1223 | assert(ctx_sc || ctx); |
| 1224 | |
| 1225 | if (!ctx_sc) { |
| 1226 | context.ctx = ctx; |
| 1227 | context.mod = module; |
| 1228 | context.path_len = 1; |
| 1229 | context.path[0] = '/'; |
| 1230 | ctx_sc = &context; |
| 1231 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1232 | |
| 1233 | if (!features_p) { |
| 1234 | return LY_SUCCESS; |
| 1235 | } |
| 1236 | if (*features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1237 | offset = LY_ARRAY_COUNT(*features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1238 | } |
| 1239 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1240 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1241 | 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] | 1242 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1243 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1244 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1245 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1246 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1247 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1248 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1249 | DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1250 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1251 | (*features)[offset + u].module = ctx_sc->mod; |
| 1252 | |
| 1253 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1254 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1255 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1256 | |
| 1257 | return LY_SUCCESS; |
| 1258 | } |
| 1259 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1260 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1261 | * @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] | 1262 | * |
| 1263 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1264 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1265 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1266 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1267 | * being currently processed). |
| 1268 | * @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] | 1269 | * @return LY_SUCCESS if everything is ok. |
| 1270 | * @return LY_EVALID if the feature references indirectly itself. |
| 1271 | */ |
| 1272 | static LY_ERR |
| 1273 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1274 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1275 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1276 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1277 | struct ly_set recursion = {0}; |
| 1278 | struct lysc_feature *drv; |
| 1279 | |
| 1280 | if (!depfeatures) { |
| 1281 | return LY_SUCCESS; |
| 1282 | } |
| 1283 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1284 | for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1285 | if (feature == depfeatures[u]) { |
| 1286 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1287 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1288 | ret = LY_EVALID; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1289 | goto cleanup; |
| 1290 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1291 | ret = ly_set_add(&recursion, depfeatures[u], 0, NULL); |
| 1292 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | for (v = 0; v < recursion.count; ++v) { |
| 1296 | drv = recursion.objs[v]; |
| 1297 | if (!drv->depfeatures) { |
| 1298 | continue; |
| 1299 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1300 | for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1301 | if (feature == drv->depfeatures[u]) { |
| 1302 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1303 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1304 | ret = LY_EVALID; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1305 | goto cleanup; |
| 1306 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 1307 | ly_set_add(&recursion, drv->depfeatures[u], 0, NULL); |
| 1308 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1309 | } |
| 1310 | } |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1311 | |
| 1312 | cleanup: |
| 1313 | ly_set_erase(&recursion, NULL); |
| 1314 | return ret; |
| 1315 | } |
| 1316 | |
| 1317 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1318 | * @brief Create pre-compiled features array. |
| 1319 | * |
| 1320 | * See lys_feature_precompile() for more details. |
| 1321 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1322 | * @param[in] ctx Compile context. |
| 1323 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1324 | * @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] | 1325 | * @return LY_ERR value. |
| 1326 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1327 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1328 | 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] | 1329 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1330 | LY_ARRAY_COUNT_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1331 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1332 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1333 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1334 | /* find the preprecompiled feature */ |
| 1335 | LY_ARRAY_FOR(features, x) { |
| 1336 | if (strcmp(features[x].name, feature_p->name)) { |
| 1337 | continue; |
| 1338 | } |
| 1339 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1340 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1341 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1342 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1343 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1344 | 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] | 1345 | 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] | 1346 | if (feature->iffeatures) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1347 | for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1348 | if (feature->iffeatures[u].features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1349 | for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1350 | /* check for circular dependency - direct reference first,... */ |
| 1351 | if (feature == feature->iffeatures[u].features[v]) { |
| 1352 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1353 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1354 | return LY_EVALID; |
| 1355 | } |
| 1356 | /* ... and indirect circular reference */ |
| 1357 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1358 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1359 | /* add itself into the dependants list */ |
| 1360 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1361 | *df = feature; |
| 1362 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1363 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1364 | } |
| 1365 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1366 | lysc_update_path(ctx, NULL, NULL); |
| 1367 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1368 | done: |
| 1369 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1370 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1371 | |
| 1372 | LOGINT(ctx->ctx); |
| 1373 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1374 | } |
| 1375 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1376 | /** |
| 1377 | * @brief Revert compiled list of features back to the precompiled state. |
| 1378 | * |
| 1379 | * 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] | 1380 | * 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] | 1381 | * |
| 1382 | * @param[in] ctx Compilation context. |
| 1383 | * @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] | 1384 | * and supposed to hold the dis_features list. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1385 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1386 | static void |
| 1387 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1388 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1389 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1390 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 1391 | if (mod->compiled) { |
| 1392 | /* keep the dis_features list until the complete lys_module is freed */ |
| 1393 | mod->dis_features = mod->compiled->features; |
| 1394 | mod->compiled->features = NULL; |
| 1395 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1396 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1397 | /* 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] | 1398 | * which may points into the data being freed here */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1399 | LY_ARRAY_FOR(mod->dis_features, u) { |
| 1400 | LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) { |
| 1401 | lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1402 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1403 | LY_ARRAY_FREE(mod->dis_features[u].iffeatures); |
| 1404 | mod->dis_features[u].iffeatures = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1405 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1406 | LY_ARRAY_FOR(mod->dis_features[u].exts, v) { |
| 1407 | lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1408 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1409 | LY_ARRAY_FREE(mod->dis_features[u].exts); |
| 1410 | mod->dis_features[u].exts = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1414 | /** |
| 1415 | * @brief Validate and normalize numeric value from a range definition. |
| 1416 | * @param[in] ctx Compile context. |
| 1417 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1418 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1419 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1420 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1421 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1422 | * @param[in] value String value of the range boundary. |
| 1423 | * @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. |
| 1424 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1425 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1426 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1427 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1428 | 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] | 1429 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1430 | size_t fraction = 0, size; |
| 1431 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1432 | *len = 0; |
| 1433 | |
| 1434 | assert(value); |
| 1435 | /* parse value */ |
| 1436 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1437 | return LY_EVALID; |
| 1438 | } |
| 1439 | |
| 1440 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1441 | ++(*len); |
| 1442 | } |
| 1443 | |
| 1444 | while (isdigit(value[*len])) { |
| 1445 | ++(*len); |
| 1446 | } |
| 1447 | |
| 1448 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1449 | if (basetype == LY_TYPE_DEC64) { |
| 1450 | goto decimal; |
| 1451 | } else { |
| 1452 | *valcopy = strndup(value, *len); |
| 1453 | return LY_SUCCESS; |
| 1454 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1455 | } |
| 1456 | fraction = *len; |
| 1457 | |
| 1458 | ++(*len); |
| 1459 | while (isdigit(value[*len])) { |
| 1460 | ++(*len); |
| 1461 | } |
| 1462 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1463 | if (basetype == LY_TYPE_DEC64) { |
| 1464 | decimal: |
| 1465 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1466 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1467 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1468 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1469 | *len, value, frdigits); |
| 1470 | return LY_EINVAL; |
| 1471 | } |
| 1472 | if (fraction) { |
| 1473 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1474 | } else { |
| 1475 | size = (*len) + frdigits + 1; |
| 1476 | } |
| 1477 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1478 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1479 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1480 | (*valcopy)[size - 1] = '\0'; |
| 1481 | if (fraction) { |
| 1482 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1483 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1484 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1485 | } else { |
| 1486 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1487 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1488 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1489 | } |
| 1490 | return LY_SUCCESS; |
| 1491 | } |
| 1492 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1493 | /** |
| 1494 | * @brief Check that values in range are in ascendant order. |
| 1495 | * @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] | 1496 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1497 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1498 | * @param[in] value Current value to check. |
| 1499 | * @param[in] prev_value The last seen value. |
| 1500 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1501 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1502 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1503 | range_part_check_ascendancy(uint8_t unsigned_value, uint8_t max, int64_t value, int64_t prev_value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1504 | { |
| 1505 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1506 | 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] | 1507 | return LY_EEXIST; |
| 1508 | } |
| 1509 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1510 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1511 | return LY_EEXIST; |
| 1512 | } |
| 1513 | } |
| 1514 | return LY_SUCCESS; |
| 1515 | } |
| 1516 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1517 | /** |
| 1518 | * @brief Set min/max value of the range part. |
| 1519 | * @param[in] ctx Compile context. |
| 1520 | * @param[in] part Range part structure to fill. |
| 1521 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1522 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1523 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1524 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1525 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1526 | * @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] | 1527 | * @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] | 1528 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1529 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1530 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1531 | * frdigits value), LY_EMEM. |
| 1532 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1533 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1534 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, uint8_t max, int64_t prev, LY_DATA_TYPE basetype, |
| 1535 | uint8_t first, uint8_t 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] | 1536 | { |
| 1537 | LY_ERR ret = LY_SUCCESS; |
| 1538 | char *valcopy = NULL; |
| 1539 | size_t len; |
| 1540 | |
| 1541 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1542 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1543 | LY_CHECK_GOTO(ret, finalize); |
| 1544 | } |
| 1545 | if (!valcopy && base_range) { |
| 1546 | if (max) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1547 | 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] | 1548 | } else { |
| 1549 | part->min_64 = base_range->parts[0].min_64; |
| 1550 | } |
| 1551 | if (!first) { |
| 1552 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1553 | } |
| 1554 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1558 | case LY_TYPE_INT8: /* range */ |
| 1559 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1560 | 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] | 1561 | } else if (max) { |
| 1562 | part->max_64 = INT64_C(127); |
| 1563 | } else { |
| 1564 | part->min_64 = INT64_C(-128); |
| 1565 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1566 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1567 | 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] | 1568 | } |
| 1569 | break; |
| 1570 | case LY_TYPE_INT16: /* range */ |
| 1571 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1572 | 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] | 1573 | } else if (max) { |
| 1574 | part->max_64 = INT64_C(32767); |
| 1575 | } else { |
| 1576 | part->min_64 = INT64_C(-32768); |
| 1577 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1578 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1579 | 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] | 1580 | } |
| 1581 | break; |
| 1582 | case LY_TYPE_INT32: /* range */ |
| 1583 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1584 | 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] | 1585 | } else if (max) { |
| 1586 | part->max_64 = INT64_C(2147483647); |
| 1587 | } else { |
| 1588 | part->min_64 = INT64_C(-2147483648); |
| 1589 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1590 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1591 | 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] | 1592 | } |
| 1593 | break; |
| 1594 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1595 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1596 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1597 | 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] | 1598 | max ? &part->max_64 : &part->min_64); |
| 1599 | } else if (max) { |
| 1600 | part->max_64 = INT64_C(9223372036854775807); |
| 1601 | } else { |
| 1602 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1603 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1604 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1605 | 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] | 1606 | } |
| 1607 | break; |
| 1608 | case LY_TYPE_UINT8: /* range */ |
| 1609 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1610 | 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] | 1611 | } else if (max) { |
| 1612 | part->max_u64 = UINT64_C(255); |
| 1613 | } else { |
| 1614 | part->min_u64 = UINT64_C(0); |
| 1615 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1616 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1617 | 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] | 1618 | } |
| 1619 | break; |
| 1620 | case LY_TYPE_UINT16: /* range */ |
| 1621 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1622 | 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] | 1623 | } else if (max) { |
| 1624 | part->max_u64 = UINT64_C(65535); |
| 1625 | } else { |
| 1626 | part->min_u64 = UINT64_C(0); |
| 1627 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1628 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1629 | 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] | 1630 | } |
| 1631 | break; |
| 1632 | case LY_TYPE_UINT32: /* range */ |
| 1633 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1634 | 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] | 1635 | } else if (max) { |
| 1636 | part->max_u64 = UINT64_C(4294967295); |
| 1637 | } else { |
| 1638 | part->min_u64 = UINT64_C(0); |
| 1639 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1640 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1641 | 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] | 1642 | } |
| 1643 | break; |
| 1644 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1645 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1646 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1647 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1648 | 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] | 1649 | } else if (max) { |
| 1650 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1651 | } else { |
| 1652 | part->min_u64 = UINT64_C(0); |
| 1653 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1654 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1655 | 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] | 1656 | } |
| 1657 | break; |
| 1658 | default: |
| 1659 | LOGINT(ctx->ctx); |
| 1660 | ret = LY_EINT; |
| 1661 | } |
| 1662 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1663 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1664 | if (ret == LY_EDENIED) { |
| 1665 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1666 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1667 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1668 | } else if (ret == LY_EVALID) { |
| 1669 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1670 | "Invalid %s restriction - invalid value \"%s\".", |
| 1671 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1672 | } else if (ret == LY_EEXIST) { |
| 1673 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1674 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1675 | length_restr ? "length" : "range", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1676 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1677 | } else if (!ret && value) { |
| 1678 | *value = *value + len; |
| 1679 | } |
| 1680 | free(valcopy); |
| 1681 | return ret; |
| 1682 | } |
| 1683 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1684 | /** |
| 1685 | * @brief Compile the parsed range restriction. |
| 1686 | * @param[in] ctx Compile context. |
| 1687 | * @param[in] range_p Parsed range structure to compile. |
| 1688 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1689 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1690 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1691 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1692 | * range restriction must be more restrictive than the base_range. |
| 1693 | * @param[in,out] range Pointer to the created current range structure. |
| 1694 | * @return LY_ERR value. |
| 1695 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1696 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1697 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, uint8_t length_restr, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1698 | uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1699 | { |
| 1700 | LY_ERR ret = LY_EVALID; |
| 1701 | const char *expr; |
| 1702 | struct lysc_range_part *parts = NULL, *part; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1703 | uint8_t range_expected = 0, uns; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1704 | LY_ARRAY_COUNT_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1705 | |
| 1706 | assert(range); |
| 1707 | assert(range_p); |
| 1708 | |
| 1709 | expr = range_p->arg; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1710 | while (1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1711 | if (isspace(*expr)) { |
| 1712 | ++expr; |
| 1713 | } else if (*expr == '\0') { |
| 1714 | if (range_expected) { |
| 1715 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1716 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1717 | length_restr ? "length" : "range", range_p->arg); |
| 1718 | goto cleanup; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1719 | } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1720 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1721 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1722 | length_restr ? "length" : "range", range_p->arg); |
| 1723 | goto cleanup; |
| 1724 | } |
| 1725 | parts_done++; |
| 1726 | break; |
| 1727 | } else if (!strncmp(expr, "min", 3)) { |
| 1728 | if (parts) { |
| 1729 | /* min cannot be used elsewhere than in the first part */ |
| 1730 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1731 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1732 | expr - range_p->arg, range_p->arg); |
| 1733 | goto cleanup; |
| 1734 | } |
| 1735 | expr += 3; |
| 1736 | |
| 1737 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1738 | 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] | 1739 | part->max_64 = part->min_64; |
| 1740 | } else if (*expr == '|') { |
| 1741 | if (!parts || range_expected) { |
| 1742 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1743 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1744 | goto cleanup; |
| 1745 | } |
| 1746 | expr++; |
| 1747 | parts_done++; |
| 1748 | /* process next part of the expression */ |
| 1749 | } else if (!strncmp(expr, "..", 2)) { |
| 1750 | expr += 2; |
| 1751 | while (isspace(*expr)) { |
| 1752 | expr++; |
| 1753 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1754 | if (!parts || LY_ARRAY_COUNT(parts) == parts_done) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1755 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1756 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1757 | goto cleanup; |
| 1758 | } |
| 1759 | /* continue expecting the upper boundary */ |
| 1760 | range_expected = 1; |
| 1761 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1762 | /* number */ |
| 1763 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1764 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1765 | 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] | 1766 | range_expected = 0; |
| 1767 | } else { |
| 1768 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1769 | 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] | 1770 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1771 | part->max_64 = part->min_64; |
| 1772 | } |
| 1773 | |
| 1774 | /* continue with possible another expression part */ |
| 1775 | } else if (!strncmp(expr, "max", 3)) { |
| 1776 | expr += 3; |
| 1777 | while (isspace(*expr)) { |
| 1778 | expr++; |
| 1779 | } |
| 1780 | if (*expr != '\0') { |
| 1781 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1782 | length_restr ? "length" : "range", expr); |
| 1783 | goto cleanup; |
| 1784 | } |
| 1785 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1786 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1787 | 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] | 1788 | range_expected = 0; |
| 1789 | } else { |
| 1790 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1791 | 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] | 1792 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1793 | part->min_64 = part->max_64; |
| 1794 | } |
| 1795 | } else { |
| 1796 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1797 | length_restr ? "length" : "range", expr); |
| 1798 | goto cleanup; |
| 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | /* check with the previous range/length restriction */ |
| 1803 | if (base_range) { |
| 1804 | switch (basetype) { |
| 1805 | case LY_TYPE_BINARY: |
| 1806 | case LY_TYPE_UINT8: |
| 1807 | case LY_TYPE_UINT16: |
| 1808 | case LY_TYPE_UINT32: |
| 1809 | case LY_TYPE_UINT64: |
| 1810 | case LY_TYPE_STRING: |
| 1811 | uns = 1; |
| 1812 | break; |
| 1813 | case LY_TYPE_DEC64: |
| 1814 | case LY_TYPE_INT8: |
| 1815 | case LY_TYPE_INT16: |
| 1816 | case LY_TYPE_INT32: |
| 1817 | case LY_TYPE_INT64: |
| 1818 | uns = 0; |
| 1819 | break; |
| 1820 | default: |
| 1821 | LOGINT(ctx->ctx); |
| 1822 | ret = LY_EINT; |
| 1823 | goto cleanup; |
| 1824 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1825 | 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] | 1826 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1827 | goto baseerror; |
| 1828 | } |
| 1829 | /* current lower bound is not lower than the base */ |
| 1830 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1831 | /* base has single value */ |
| 1832 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1833 | /* both lower bounds are the same */ |
| 1834 | if (parts[u].min_64 != parts[u].max_64) { |
| 1835 | /* current continues with a range */ |
| 1836 | goto baseerror; |
| 1837 | } else { |
| 1838 | /* equal single values, move both forward */ |
| 1839 | ++v; |
| 1840 | continue; |
| 1841 | } |
| 1842 | } else { |
| 1843 | /* base is single value lower than current range, so the |
| 1844 | * value from base range is removed in the current, |
| 1845 | * move only base and repeat checking */ |
| 1846 | ++v; |
| 1847 | --u; |
| 1848 | continue; |
| 1849 | } |
| 1850 | } else { |
| 1851 | /* base is the range */ |
| 1852 | if (parts[u].min_64 == parts[u].max_64) { |
| 1853 | /* current is a single value */ |
| 1854 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1855 | /* current is behind the base range, so base range is omitted, |
| 1856 | * move the base and keep the current for further check */ |
| 1857 | ++v; |
| 1858 | --u; |
| 1859 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1860 | continue; |
| 1861 | } else { |
| 1862 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1863 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1864 | /* higher bound is higher than the current higher bound */ |
| 1865 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1866 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1867 | * continue with the same current, but move the base */ |
| 1868 | --u; |
| 1869 | ++v; |
| 1870 | continue; |
| 1871 | } |
| 1872 | /* current range starts within the base range but end behind it */ |
| 1873 | goto baseerror; |
| 1874 | } else { |
| 1875 | /* current range is smaller than the base, |
| 1876 | * move current, but stay with the base */ |
| 1877 | continue; |
| 1878 | } |
| 1879 | } |
| 1880 | } |
| 1881 | } |
| 1882 | if (u != parts_done) { |
| 1883 | baseerror: |
| 1884 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1885 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1886 | length_restr ? "length" : "range", range_p->arg); |
| 1887 | goto cleanup; |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | if (!(*range)) { |
| 1892 | *range = calloc(1, sizeof **range); |
| 1893 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1894 | } |
| 1895 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1896 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1897 | if (range_p->eapptag) { |
| 1898 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1899 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1900 | } |
| 1901 | if (range_p->emsg) { |
| 1902 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1903 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1904 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1905 | if (range_p->dsc) { |
| 1906 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1907 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1908 | } |
| 1909 | if (range_p->ref) { |
| 1910 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1911 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1912 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1913 | /* extensions are taken only from the last range by the caller */ |
| 1914 | |
| 1915 | (*range)->parts = parts; |
| 1916 | parts = NULL; |
| 1917 | ret = LY_SUCCESS; |
| 1918 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1919 | LY_ARRAY_FREE(parts); |
| 1920 | |
| 1921 | return ret; |
| 1922 | } |
| 1923 | |
| 1924 | /** |
| 1925 | * @brief Checks pattern syntax. |
| 1926 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1927 | * @param[in] ctx Context. |
| 1928 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1929 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1930 | * @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] | 1931 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1932 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1933 | LY_ERR |
| 1934 | 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] | 1935 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1936 | size_t idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1937 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1938 | int err_code; |
| 1939 | const char *orig_ptr; |
| 1940 | PCRE2_SIZE err_offset; |
| 1941 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1942 | #define URANGE_LEN 19 |
| 1943 | char *ublock2urange[][2] = { |
| 1944 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1945 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1946 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1947 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1948 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1949 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1950 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1951 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1952 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1953 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1954 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1955 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1956 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1957 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1958 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1959 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1960 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1961 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1962 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1963 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1964 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1965 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1966 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1967 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1968 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1969 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1970 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1971 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1972 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1973 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1974 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1975 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1976 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1977 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1978 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1979 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1980 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1981 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1982 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1983 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1984 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1985 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1986 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1987 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1988 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1989 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1990 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1991 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1992 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1993 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1994 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1995 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1996 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1997 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1998 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1999 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 2000 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 2001 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 2002 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 2003 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 2004 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 2005 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 2006 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 2007 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 2008 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 2009 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 2010 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 2011 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 2012 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 2013 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 2014 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 2015 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 2016 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 2017 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 2018 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 2019 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 2020 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 2021 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 2022 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 2023 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 2024 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 2025 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 2026 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 2027 | {NULL, NULL} |
| 2028 | }; |
| 2029 | |
| 2030 | /* adjust the expression to a Perl equivalent |
| 2031 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 2032 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2033 | /* allocate space for the transformed pattern */ |
| 2034 | size = strlen(pattern) + 1; |
| 2035 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2036 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2037 | perl_regex[0] = '\0'; |
| 2038 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2039 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 2040 | brack = 0; |
| 2041 | idx = 0; |
| 2042 | orig_ptr = pattern; |
| 2043 | while (orig_ptr[0]) { |
| 2044 | switch (orig_ptr[0]) { |
| 2045 | case '$': |
| 2046 | case '^': |
| 2047 | if (!brack) { |
| 2048 | /* make space for the extra character */ |
| 2049 | ++size; |
| 2050 | perl_regex = ly_realloc(perl_regex, size); |
| 2051 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2052 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2053 | /* print escape slash */ |
| 2054 | perl_regex[idx] = '\\'; |
| 2055 | ++idx; |
| 2056 | } |
| 2057 | break; |
| 2058 | case '[': |
| 2059 | /* must not be escaped */ |
| 2060 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2061 | ++brack; |
| 2062 | } |
| 2063 | break; |
| 2064 | case ']': |
| 2065 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2066 | /* pattern was checked and compiled already */ |
| 2067 | assert(brack); |
| 2068 | --brack; |
| 2069 | } |
| 2070 | break; |
| 2071 | default: |
| 2072 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2073 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2074 | |
| 2075 | /* copy char */ |
| 2076 | perl_regex[idx] = orig_ptr[0]; |
| 2077 | |
| 2078 | ++idx; |
| 2079 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2080 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2081 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2082 | |
| 2083 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2084 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2085 | start = ptr - perl_regex; |
| 2086 | |
| 2087 | ptr = strchr(ptr, '}'); |
| 2088 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2089 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2090 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 2091 | free(perl_regex); |
| 2092 | return LY_EVALID; |
| 2093 | } |
| 2094 | end = (ptr - perl_regex) + 1; |
| 2095 | |
| 2096 | /* need more space */ |
| 2097 | if (end - start < URANGE_LEN) { |
| 2098 | 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] | 2099 | 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] | 2100 | } |
| 2101 | |
| 2102 | /* find our range */ |
| 2103 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2104 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2105 | break; |
| 2106 | } |
| 2107 | } |
| 2108 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2109 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2110 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2111 | free(perl_regex); |
| 2112 | return LY_EVALID; |
| 2113 | } |
| 2114 | |
| 2115 | /* 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] | 2116 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2117 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2118 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2119 | } |
| 2120 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2121 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2122 | } |
| 2123 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2124 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2125 | /* skip brackets */ |
| 2126 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2127 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2128 | } else { |
| 2129 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2130 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2135 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2136 | 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] | 2137 | &err_code, &err_offset, NULL); |
| 2138 | if (!code_local) { |
| 2139 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2140 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2141 | 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] | 2142 | free(perl_regex); |
| 2143 | return LY_EVALID; |
| 2144 | } |
| 2145 | free(perl_regex); |
| 2146 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2147 | if (code) { |
| 2148 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2149 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2150 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | return LY_SUCCESS; |
| 2154 | |
| 2155 | #undef URANGE_LEN |
| 2156 | } |
| 2157 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2158 | /** |
| 2159 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2160 | * @param[in] ctx Compile context. |
| 2161 | * @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] | 2162 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2163 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2164 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2165 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2166 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2167 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2168 | 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] | 2169 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2170 | { |
| 2171 | struct lysc_pattern **pattern; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2172 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2173 | LY_ERR ret = LY_SUCCESS; |
| 2174 | |
| 2175 | /* first, copy the patterns from the base type */ |
| 2176 | if (base_patterns) { |
| 2177 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2178 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2179 | } |
| 2180 | |
| 2181 | LY_ARRAY_FOR(patterns_p, u) { |
| 2182 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2183 | *pattern = calloc(1, sizeof **pattern); |
| 2184 | ++(*pattern)->refcount; |
| 2185 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2186 | 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] | 2187 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2188 | |
| 2189 | if (patterns_p[u].arg[0] == 0x15) { |
| 2190 | (*pattern)->inverted = 1; |
| 2191 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2192 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2193 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2194 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2195 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2196 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2197 | 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] | 2198 | } |
| 2199 | done: |
| 2200 | return ret; |
| 2201 | } |
| 2202 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2203 | /** |
| 2204 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2205 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2206 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2207 | 0 /* LY_TYPE_UNKNOWN */, |
| 2208 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2209 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2210 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2211 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2212 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2213 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2214 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2215 | 0 /* LY_TYPE_BOOL */, |
| 2216 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2217 | 0 /* LY_TYPE_EMPTY */, |
| 2218 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2219 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2220 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2221 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2222 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2223 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2224 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2225 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2226 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2227 | }; |
| 2228 | |
| 2229 | /** |
| 2230 | * @brief stringification of the YANG built-in data types |
| 2231 | */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2232 | 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] | 2233 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2234 | "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] | 2235 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2236 | /** |
| 2237 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2238 | * @param[in] ctx Compile context. |
| 2239 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2240 | * @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] | 2241 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2242 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2243 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2244 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2245 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2246 | 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] | 2247 | 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] | 2248 | { |
| 2249 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2250 | LY_ARRAY_COUNT_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2251 | int32_t value = 0; |
| 2252 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2253 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2254 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2255 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2256 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2257 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2258 | return LY_EVALID; |
| 2259 | } |
| 2260 | |
| 2261 | LY_ARRAY_FOR(enums_p, u) { |
| 2262 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2263 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2264 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2265 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2266 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2267 | if (base_enums) { |
| 2268 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2269 | LY_ARRAY_FOR(base_enums, v) { |
| 2270 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2271 | break; |
| 2272 | } |
| 2273 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2274 | if (v == LY_ARRAY_COUNT(base_enums)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2275 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2276 | "Invalid %s - derived type adds new item \"%s\".", |
| 2277 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2278 | return LY_EVALID; |
| 2279 | } |
| 2280 | match = v; |
| 2281 | } |
| 2282 | |
| 2283 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2284 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2285 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2286 | e->value = (int32_t)enums_p[u].value; |
| 2287 | if (!u || e->value >= value) { |
| 2288 | value = e->value + 1; |
| 2289 | } |
| 2290 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2291 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2292 | if (e->value == (*enums)[v].value) { |
| 2293 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2294 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2295 | e->value, e->name, (*enums)[v].name); |
| 2296 | return LY_EVALID; |
| 2297 | } |
| 2298 | } |
| 2299 | } else if (base_enums) { |
| 2300 | /* inherit the assigned value */ |
| 2301 | e->value = base_enums[match].value; |
| 2302 | if (!u || e->value >= value) { |
| 2303 | value = e->value + 1; |
| 2304 | } |
| 2305 | } else { |
| 2306 | /* assign value automatically */ |
| 2307 | if (u && value == INT32_MIN) { |
| 2308 | /* counter overflow */ |
| 2309 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2310 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2311 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2312 | return LY_EVALID; |
| 2313 | } |
| 2314 | e->value = value++; |
| 2315 | } |
| 2316 | } else { /* LY_TYPE_BITS */ |
| 2317 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2318 | e->value = (int32_t)enums_p[u].value; |
| 2319 | if (!u || (uint32_t)e->value >= position) { |
| 2320 | position = (uint32_t)e->value + 1; |
| 2321 | } |
| 2322 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2323 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2324 | if (e->value == (*enums)[v].value) { |
| 2325 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2326 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2327 | (uint32_t)e->value, e->name, (*enums)[v].name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2328 | return LY_EVALID; |
| 2329 | } |
| 2330 | } |
| 2331 | } else if (base_enums) { |
| 2332 | /* inherit the assigned value */ |
| 2333 | e->value = base_enums[match].value; |
| 2334 | if (!u || (uint32_t)e->value >= position) { |
| 2335 | position = (uint32_t)e->value + 1; |
| 2336 | } |
| 2337 | } else { |
| 2338 | /* assign value automatically */ |
| 2339 | if (u && position == 0) { |
| 2340 | /* counter overflow */ |
| 2341 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2342 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2343 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2344 | return LY_EVALID; |
| 2345 | } |
| 2346 | e->value = position++; |
| 2347 | } |
| 2348 | } |
| 2349 | |
| 2350 | if (base_enums) { |
| 2351 | /* the assigned values must not change from the derived type */ |
| 2352 | if (e->value != base_enums[match].value) { |
| 2353 | if (basetype == LY_TYPE_ENUM) { |
| 2354 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2355 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2356 | e->name, base_enums[match].value, e->value); |
| 2357 | } else { |
| 2358 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2359 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2360 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2361 | } |
| 2362 | return LY_EVALID; |
| 2363 | } |
| 2364 | } |
| 2365 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2366 | 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] | 2367 | 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] | 2368 | |
| 2369 | if (basetype == LY_TYPE_BITS) { |
| 2370 | /* keep bits ordered by position */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2371 | for (v = u; v && (*enums)[v - 1].value > e->value; --v) {} |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2372 | if (v != u) { |
| 2373 | memcpy(&storage, e, sizeof *e); |
| 2374 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2375 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2376 | } |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | done: |
| 2381 | return ret; |
| 2382 | } |
| 2383 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2384 | /** |
| 2385 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2386 | * |
| 2387 | * path-arg = absolute-path / relative-path |
| 2388 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2389 | * relative-path = 1*(".." "/") descendant-path |
| 2390 | * |
| 2391 | * @param[in,out] path Path to parse. |
| 2392 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2393 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2394 | * @param[out] name Name of the token. |
| 2395 | * @param[out] nam_len Length of the name. |
| 2396 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2397 | * must not be changed between consecutive calls. -1 if the |
| 2398 | * path is absolute. |
| 2399 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2400 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2401 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2402 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2403 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2404 | int32_t *parent_times, uint8_t *has_predicate) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2405 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2406 | int32_t par_times = 0; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2407 | |
| 2408 | assert(path && *path); |
| 2409 | assert(parent_times); |
| 2410 | assert(prefix); |
| 2411 | assert(prefix_len); |
| 2412 | assert(name); |
| 2413 | assert(name_len); |
| 2414 | assert(has_predicate); |
| 2415 | |
| 2416 | *prefix = NULL; |
| 2417 | *prefix_len = 0; |
| 2418 | *name = NULL; |
| 2419 | *name_len = 0; |
| 2420 | *has_predicate = 0; |
| 2421 | |
| 2422 | if (!*parent_times) { |
| 2423 | if (!strncmp(*path, "..", 2)) { |
| 2424 | *path += 2; |
| 2425 | ++par_times; |
| 2426 | while (!strncmp(*path, "/..", 3)) { |
| 2427 | *path += 3; |
| 2428 | ++par_times; |
| 2429 | } |
| 2430 | } |
| 2431 | if (par_times) { |
| 2432 | *parent_times = par_times; |
| 2433 | } else { |
| 2434 | *parent_times = -1; |
| 2435 | } |
| 2436 | } |
| 2437 | |
| 2438 | if (**path != '/') { |
| 2439 | return LY_EINVAL; |
| 2440 | } |
| 2441 | /* skip '/' */ |
| 2442 | ++(*path); |
| 2443 | |
| 2444 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2445 | 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] | 2446 | |
| 2447 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2448 | /* path continues by another token or this is the last token */ |
| 2449 | return LY_SUCCESS; |
| 2450 | } else if ((*path)[0] != '[') { |
| 2451 | /* unexpected character */ |
| 2452 | return LY_EINVAL; |
| 2453 | } else { |
| 2454 | /* predicate starting with [ */ |
| 2455 | *has_predicate = 1; |
| 2456 | return LY_SUCCESS; |
| 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2461 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2462 | * |
| 2463 | * The set of features used for target must be a subset of features used for the leafref. |
| 2464 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2465 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2466 | * |
| 2467 | * @param[in] refnode The leafref node. |
| 2468 | * @param[in] target Tha target node of the leafref. |
| 2469 | * @return LY_SUCCESS or LY_EVALID; |
| 2470 | */ |
| 2471 | static LY_ERR |
| 2472 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2473 | { |
| 2474 | LY_ERR ret = LY_EVALID; |
| 2475 | const struct lysc_node *iter; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2476 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2477 | struct ly_set features = {0}; |
| 2478 | |
| 2479 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2480 | if (iter->iffeatures) { |
| 2481 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2482 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2483 | 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] | 2484 | } |
| 2485 | } |
| 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | /* 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] | 2490 | * 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] | 2491 | * to the leafref itself. */ |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2492 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2493 | if (iter->iffeatures) { |
| 2494 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2495 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2496 | if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) { |
| 2497 | /* feature not present */ |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2498 | goto cleanup; |
| 2499 | } |
| 2500 | } |
| 2501 | } |
| 2502 | } |
| 2503 | } |
| 2504 | ret = LY_SUCCESS; |
| 2505 | |
| 2506 | cleanup: |
| 2507 | ly_set_erase(&features, NULL); |
| 2508 | return ret; |
| 2509 | } |
| 2510 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2511 | 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] | 2512 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2513 | 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] | 2514 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2515 | /** |
| 2516 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2517 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2518 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2519 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2520 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2521 | * @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] | 2522 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2523 | * @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] | 2524 | * @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] | 2525 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2526 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2527 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2528 | * @return LY_ERR value. |
| 2529 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2530 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2531 | 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] | 2532 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2533 | struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, |
| 2534 | struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2535 | { |
| 2536 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2537 | struct lysc_type_bin *bin; |
| 2538 | struct lysc_type_num *num; |
| 2539 | struct lysc_type_str *str; |
| 2540 | struct lysc_type_bits *bits; |
| 2541 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2542 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2543 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2544 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2545 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2546 | |
| 2547 | switch (basetype) { |
| 2548 | case LY_TYPE_BINARY: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2549 | bin = (struct lysc_type_bin *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2550 | |
| 2551 | /* 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] | 2552 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2553 | 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] | 2554 | base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2555 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2556 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | if (tpdfname) { |
| 2561 | type_p->compiled = *type; |
| 2562 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2563 | } |
| 2564 | break; |
| 2565 | case LY_TYPE_BITS: |
| 2566 | /* RFC 7950 9.7 - bits */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2567 | bits = (struct lysc_type_bits *)(*type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2568 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2569 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2570 | base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL, |
| 2571 | (struct lysc_type_bitenum_item **)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2572 | } |
| 2573 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2574 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2575 | /* 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] | 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, "bit", "bits type ", tpdfname); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +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, "bit", "bits type", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2580 | free(*type); |
| 2581 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2582 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2583 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | if (tpdfname) { |
| 2587 | type_p->compiled = *type; |
| 2588 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2589 | } |
| 2590 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2591 | case LY_TYPE_DEC64: |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2592 | dec = (struct lysc_type_dec *)(*type); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2593 | |
| 2594 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2595 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2596 | if (!type_p->fraction_digits) { |
| 2597 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2598 | 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] | 2599 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2600 | 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] | 2601 | free(*type); |
| 2602 | *type = NULL; |
| 2603 | } |
| 2604 | return LY_EVALID; |
| 2605 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2606 | dec->fraction_digits = type_p->fraction_digits; |
| 2607 | } else { |
| 2608 | if (type_p->fraction_digits) { |
| 2609 | /* fraction digits is prohibited in types not directly derived from built-in decimal64 */ |
| 2610 | if (tpdfname) { |
| 2611 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2612 | "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.", |
| 2613 | tpdfname); |
| 2614 | } else { |
| 2615 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2616 | "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type."); |
| 2617 | free(*type); |
| 2618 | *type = NULL; |
| 2619 | } |
| 2620 | return LY_EVALID; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2621 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2622 | dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2623 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2624 | |
| 2625 | /* RFC 7950 9.2.4 - range */ |
| 2626 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2627 | 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] | 2628 | base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2629 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2630 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, done); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2631 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | if (tpdfname) { |
| 2635 | type_p->compiled = *type; |
| 2636 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2637 | } |
| 2638 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2639 | case LY_TYPE_STRING: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2640 | str = (struct lysc_type_str *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2641 | |
| 2642 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2643 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2644 | 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] | 2645 | base ? ((struct lysc_type_str *)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2646 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2647 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2648 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2649 | } else if (base && ((struct lysc_type_str *)base)->length) { |
| 2650 | 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] | 2651 | } |
| 2652 | |
| 2653 | /* RFC 7950 9.4.5 - pattern */ |
| 2654 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2655 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2656 | base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns)); |
| 2657 | } else if (base && ((struct lysc_type_str *)base)->patterns) { |
| 2658 | 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] | 2659 | } |
| 2660 | |
| 2661 | if (tpdfname) { |
| 2662 | type_p->compiled = *type; |
| 2663 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2664 | } |
| 2665 | break; |
| 2666 | case LY_TYPE_ENUM: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2667 | enumeration = (struct lysc_type_enum *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2668 | |
| 2669 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2670 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2671 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2672 | base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2673 | } |
| 2674 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2675 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2676 | /* 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] | 2677 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2678 | 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] | 2679 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2680 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2681 | free(*type); |
| 2682 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2683 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2684 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | if (tpdfname) { |
| 2688 | type_p->compiled = *type; |
| 2689 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2690 | } |
| 2691 | break; |
| 2692 | case LY_TYPE_INT8: |
| 2693 | case LY_TYPE_UINT8: |
| 2694 | case LY_TYPE_INT16: |
| 2695 | case LY_TYPE_UINT16: |
| 2696 | case LY_TYPE_INT32: |
| 2697 | case LY_TYPE_UINT32: |
| 2698 | case LY_TYPE_INT64: |
| 2699 | case LY_TYPE_UINT64: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2700 | num = (struct lysc_type_num *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2701 | |
| 2702 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2703 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2704 | 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] | 2705 | base ? ((struct lysc_type_num *)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2706 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2707 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2708 | } |
| 2709 | } |
| 2710 | |
| 2711 | if (tpdfname) { |
| 2712 | type_p->compiled = *type; |
| 2713 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2714 | } |
| 2715 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2716 | case LY_TYPE_IDENT: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2717 | idref = (struct lysc_type_identityref *)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2718 | |
| 2719 | /* RFC 7950 9.10.2 - base */ |
| 2720 | if (type_p->bases) { |
| 2721 | if (base) { |
| 2722 | /* only the directly derived identityrefs can contain base specification */ |
| 2723 | if (tpdfname) { |
| 2724 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2725 | "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.", |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2726 | tpdfname); |
| 2727 | } else { |
| 2728 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2729 | "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] | 2730 | free(*type); |
| 2731 | *type = NULL; |
| 2732 | } |
| 2733 | return LY_EVALID; |
| 2734 | } |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2735 | 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] | 2736 | } |
| 2737 | |
| 2738 | if (!base && !type_p->flags) { |
| 2739 | /* type derived from identityref built-in type must contain at least one base */ |
| 2740 | if (tpdfname) { |
| 2741 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2742 | } else { |
| 2743 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2744 | free(*type); |
| 2745 | *type = NULL; |
| 2746 | } |
| 2747 | return LY_EVALID; |
| 2748 | } |
| 2749 | |
| 2750 | if (tpdfname) { |
| 2751 | type_p->compiled = *type; |
| 2752 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2753 | } |
| 2754 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2755 | case LY_TYPE_LEAFREF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2756 | lref = (struct lysc_type_leafref *)*type; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2757 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2758 | /* RFC 7950 9.9.3 - require-instance */ |
| 2759 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2760 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2761 | if (tpdfname) { |
| 2762 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2763 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2764 | } else { |
| 2765 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2766 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2767 | free(*type); |
| 2768 | *type = NULL; |
| 2769 | } |
| 2770 | return LY_EVALID; |
| 2771 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2772 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2773 | } else if (base) { |
| 2774 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2775 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2776 | } else { |
| 2777 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2778 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2779 | } |
| 2780 | if (type_p->path) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2781 | lref->path = lyxp_expr_dup(ctx->ctx, type_p->path); |
| 2782 | lref->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2783 | } else if (base) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2784 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path); |
| 2785 | lref->path_context = ((struct lysc_type_leafref *)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2786 | } else if (tpdfname) { |
| 2787 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2788 | return LY_EVALID; |
| 2789 | } else { |
| 2790 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2791 | free(*type); |
| 2792 | *type = NULL; |
| 2793 | return LY_EVALID; |
| 2794 | } |
| 2795 | if (tpdfname) { |
| 2796 | type_p->compiled = *type; |
| 2797 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2798 | } |
| 2799 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2800 | case LY_TYPE_INST: |
| 2801 | /* RFC 7950 9.9.3 - require-instance */ |
| 2802 | if (type_p->flags & LYS_SET_REQINST) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2803 | ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2804 | } else { |
| 2805 | /* default is true */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2806 | ((struct lysc_type_instanceid *)(*type))->require_instance = 1; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | if (tpdfname) { |
| 2810 | type_p->compiled = *type; |
| 2811 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2812 | } |
| 2813 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2814 | case LY_TYPE_UNION: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2815 | un = (struct lysc_type_union *)(*type); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2816 | |
| 2817 | /* RFC 7950 7.4 - type */ |
| 2818 | if (type_p->types) { |
| 2819 | if (base) { |
| 2820 | /* only the directly derived union can contain types specification */ |
| 2821 | if (tpdfname) { |
| 2822 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2823 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2824 | tpdfname); |
| 2825 | } else { |
| 2826 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2827 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2828 | free(*type); |
| 2829 | *type = NULL; |
| 2830 | } |
| 2831 | return LY_EVALID; |
| 2832 | } |
| 2833 | /* compile the type */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2834 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID); |
| 2835 | 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] | 2836 | 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] | 2837 | &type_p->types[u], &un->types[u + additional], NULL, NULL, NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2838 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2839 | /* add space for additional types from the union subtype */ |
| 2840 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2841 | LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t *)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1, |
| 2842 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2843 | |
| 2844 | /* copy subtypes of the subtype union */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2845 | 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] | 2846 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2847 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2848 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2849 | 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] | 2850 | lref = (struct lysc_type_leafref *)un->types[u + additional]; |
| 2851 | |
| 2852 | lref->basetype = LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2853 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2854 | lref->refcount = 1; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2855 | lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance; |
| 2856 | 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] | 2857 | /* TODO extensions */ |
| 2858 | |
| 2859 | } else { |
| 2860 | un->types[u + additional] = un_aux->types[v]; |
| 2861 | ++un_aux->types[v]->refcount; |
| 2862 | } |
| 2863 | ++additional; |
| 2864 | LY_ARRAY_INCREMENT(un->types); |
| 2865 | } |
| 2866 | /* compensate u increment in main loop */ |
| 2867 | --additional; |
| 2868 | |
| 2869 | /* free the replaced union subtype */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2870 | lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2871 | } else { |
| 2872 | LY_ARRAY_INCREMENT(un->types); |
| 2873 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | if (!base && !type_p->flags) { |
| 2878 | /* type derived from union built-in type must contain at least one type */ |
| 2879 | if (tpdfname) { |
| 2880 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2881 | } else { |
| 2882 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2883 | free(*type); |
| 2884 | *type = NULL; |
| 2885 | } |
| 2886 | return LY_EVALID; |
| 2887 | } |
| 2888 | |
| 2889 | if (tpdfname) { |
| 2890 | type_p->compiled = *type; |
| 2891 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2892 | } |
| 2893 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2894 | case LY_TYPE_BOOL: |
| 2895 | case LY_TYPE_EMPTY: |
| 2896 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2897 | break; |
| 2898 | } |
| 2899 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2900 | done: |
| 2901 | return ret; |
| 2902 | } |
| 2903 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2904 | /** |
| 2905 | * @brief Compile information about the leaf/leaf-list's type. |
| 2906 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2907 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2908 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2909 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2910 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2911 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2912 | * @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] | 2913 | * @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] | 2914 | * @param[out] dflt Default value for the type. |
| 2915 | * @param[out] dflt_mod Local module for the default value. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2916 | * @return LY_ERR value. |
| 2917 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2918 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2919 | 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] | 2920 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2921 | 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] | 2922 | { |
| 2923 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2924 | uint8_t dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2925 | struct type_context { |
| 2926 | const struct lysp_tpdf *tpdf; |
| 2927 | struct lysp_node *node; |
| 2928 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2929 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2930 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2931 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2932 | struct ly_set tpdf_chain = {0}; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2933 | |
| 2934 | assert((dflt && dflt_mod) || (!dflt && !dflt_mod)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2935 | |
| 2936 | (*type) = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2937 | if (dflt) { |
| 2938 | *dflt = NULL; |
| 2939 | *dflt_mod = NULL; |
| 2940 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2941 | |
| 2942 | tctx = calloc(1, sizeof *tctx); |
| 2943 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2944 | 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] | 2945 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2946 | ret == LY_SUCCESS; |
| 2947 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2948 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2949 | if (basetype) { |
| 2950 | break; |
| 2951 | } |
| 2952 | |
| 2953 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2954 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2955 | tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2956 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2957 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2958 | if (units && !*units) { |
| 2959 | /* inherit units */ |
| 2960 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2961 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2962 | if (dflt && !*dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2963 | /* inherit default */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2964 | *dflt = tctx->tpdf->dflt; |
| 2965 | *dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2966 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2967 | if (dummyloops && (!units || *units) && dflt && *dflt) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2968 | 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] | 2969 | break; |
| 2970 | } |
| 2971 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2972 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2973 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2974 | * 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] | 2975 | basetype = tctx->tpdf->type.compiled->basetype; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 2976 | ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
| 2977 | LY_CHECK_GOTO(ret, cleanup); |
| 2978 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2979 | if ((units && !*units) || (dflt && !*dflt)) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2980 | dummyloops = 1; |
| 2981 | goto preparenext; |
| 2982 | } else { |
| 2983 | tctx = NULL; |
| 2984 | break; |
| 2985 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2986 | } |
| 2987 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2988 | /* circular typedef reference detection */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2989 | for (uint32_t u = 0; u < tpdf_chain.count; u++) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2990 | /* local part */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2991 | tctx_iter = (struct type_context *)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2992 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2993 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2994 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2995 | free(tctx); |
| 2996 | ret = LY_EVALID; |
| 2997 | goto cleanup; |
| 2998 | } |
| 2999 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3000 | for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3001 | /* global part for unions corner case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3002 | tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3003 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3004 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3005 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3006 | free(tctx); |
| 3007 | ret = LY_EVALID; |
| 3008 | goto cleanup; |
| 3009 | } |
| 3010 | } |
| 3011 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3012 | /* store information for the following processing */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3013 | ret = ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
| 3014 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3015 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3016 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3017 | /* prepare next loop */ |
| 3018 | tctx_prev = tctx; |
| 3019 | tctx = calloc(1, sizeof *tctx); |
| 3020 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3021 | } |
| 3022 | free(tctx); |
| 3023 | |
| 3024 | /* allocate type according to the basetype */ |
| 3025 | switch (basetype) { |
| 3026 | case LY_TYPE_BINARY: |
| 3027 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3028 | break; |
| 3029 | case LY_TYPE_BITS: |
| 3030 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3031 | break; |
| 3032 | case LY_TYPE_BOOL: |
| 3033 | case LY_TYPE_EMPTY: |
| 3034 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3035 | break; |
| 3036 | case LY_TYPE_DEC64: |
| 3037 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3038 | break; |
| 3039 | case LY_TYPE_ENUM: |
| 3040 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3041 | break; |
| 3042 | case LY_TYPE_IDENT: |
| 3043 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3044 | break; |
| 3045 | case LY_TYPE_INST: |
| 3046 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3047 | break; |
| 3048 | case LY_TYPE_LEAFREF: |
| 3049 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3050 | break; |
| 3051 | case LY_TYPE_STRING: |
| 3052 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3053 | break; |
| 3054 | case LY_TYPE_UNION: |
| 3055 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3056 | break; |
| 3057 | case LY_TYPE_INT8: |
| 3058 | case LY_TYPE_UINT8: |
| 3059 | case LY_TYPE_INT16: |
| 3060 | case LY_TYPE_UINT16: |
| 3061 | case LY_TYPE_INT32: |
| 3062 | case LY_TYPE_UINT32: |
| 3063 | case LY_TYPE_INT64: |
| 3064 | case LY_TYPE_UINT64: |
| 3065 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3066 | break; |
| 3067 | case LY_TYPE_UNKNOWN: |
| 3068 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3069 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3070 | ret = LY_EVALID; |
| 3071 | goto cleanup; |
| 3072 | } |
| 3073 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3074 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3075 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3076 | ly_data_type2str[basetype]); |
| 3077 | free(*type); |
| 3078 | (*type) = NULL; |
| 3079 | ret = LY_EVALID; |
| 3080 | goto cleanup; |
| 3081 | } |
| 3082 | |
| 3083 | /* get restrictions from the referred typedefs */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3084 | for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3085 | tctx = (struct type_context *)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3086 | |
| 3087 | /* remember the typedef context for circular check */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3088 | ret = ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST, NULL); |
| 3089 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3090 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3091 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3092 | base = tctx->tpdf->type.compiled; |
| 3093 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3094 | } 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] | 3095 | /* no change, just use the type information from the base */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3096 | 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] | 3097 | ++base->refcount; |
| 3098 | continue; |
| 3099 | } |
| 3100 | |
| 3101 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3102 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3103 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3104 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3105 | ret = LY_EVALID; |
| 3106 | goto cleanup; |
| 3107 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3108 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3109 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3110 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3111 | ret = LY_EVALID; |
| 3112 | goto cleanup; |
| 3113 | } |
| 3114 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3115 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3116 | /* TODO user type plugins */ |
| 3117 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3118 | prev_type = *type; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3119 | 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] | 3120 | 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] | 3121 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3122 | LY_CHECK_GOTO(ret, cleanup); |
| 3123 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3124 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3125 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3126 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3127 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3128 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3129 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3130 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3131 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3132 | /* TODO user type plugins */ |
| 3133 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3134 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3135 | 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] | 3136 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 90b148f | 2020-05-06 17:49:40 +0200 | [diff] [blame] | 3137 | } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3138 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3139 | free(*type); |
| 3140 | (*type) = base; |
| 3141 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3142 | } |
| 3143 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3144 | 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] | 3145 | |
| 3146 | cleanup: |
| 3147 | ly_set_erase(&tpdf_chain, free); |
| 3148 | return ret; |
| 3149 | } |
| 3150 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3151 | /** |
| 3152 | * @brief Compile status information of the given node. |
| 3153 | * |
| 3154 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3155 | * has the status correctly set during the compilation. |
| 3156 | * |
| 3157 | * @param[in] ctx Compile context |
| 3158 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3159 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3160 | * the compatibility with the parent's status value. |
| 3161 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3162 | * @return LY_ERR value. |
| 3163 | */ |
| 3164 | static LY_ERR |
| 3165 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3166 | { |
| 3167 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3168 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3169 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3170 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3171 | if ((parent_flags & 0x3) != 0x3) { |
| 3172 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3173 | * combination of bits (0x3) which marks the uses_status value */ |
| 3174 | 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] | 3175 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3176 | } |
| 3177 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3178 | } else { |
| 3179 | (*node_flags) |= LYS_STATUS_CURR; |
| 3180 | } |
| 3181 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3182 | /* check status compatibility with the parent */ |
| 3183 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3184 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3185 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3186 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3187 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3188 | } else { /* LYS_STATUS_DEPRC */ |
| 3189 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3190 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3191 | } |
| 3192 | return LY_EVALID; |
| 3193 | } |
| 3194 | } |
| 3195 | return LY_SUCCESS; |
| 3196 | } |
| 3197 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3198 | /** |
| 3199 | * @brief Check uniqness of the node/action/notification name. |
| 3200 | * |
| 3201 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3202 | * structures, but they share the namespace so we need to check their name collisions. |
| 3203 | * |
| 3204 | * @param[in] ctx Compile context. |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3205 | * @param[in] parent Parent of the nodes to check, can be NULL. |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3206 | * @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] | 3207 | * @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] | 3208 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3209 | */ |
| 3210 | static LY_ERR |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3211 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name, |
| 3212 | const struct lysc_node *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3213 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3214 | const struct lysc_node *iter, *iter2; |
| 3215 | const struct lysc_action *actions; |
| 3216 | const struct lysc_notif *notifs; |
| 3217 | uint32_t getnext_flags; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3218 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3219 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3220 | #define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name)) |
| 3221 | |
| 3222 | if (exclude->nodetype == LYS_CASE) { |
| 3223 | /* check restricted only to all the cases */ |
| 3224 | assert(parent->nodetype == LYS_CHOICE); |
| 3225 | LY_LIST_FOR(lysc_node_children(parent, 0), iter) { |
| 3226 | if (CHECK_NODE(iter, exclude, name)) { |
| 3227 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "case"); |
| 3228 | return LY_EEXIST; |
| 3229 | } |
| 3230 | } |
| 3231 | |
| 3232 | return LY_SUCCESS; |
| 3233 | } |
| 3234 | |
| 3235 | /* no reason for our parent to be choice anymore */ |
| 3236 | assert(!parent || (parent->nodetype != LYS_CHOICE)); |
| 3237 | |
| 3238 | if (parent && (parent->nodetype == LYS_CASE)) { |
| 3239 | /* move to the first data definition parent */ |
| 3240 | parent = lysc_data_parent(parent); |
| 3241 | } |
| 3242 | |
| 3243 | getnext_flags = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE; |
| 3244 | if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION)) && (exclude->flags & LYS_CONFIG_R)) { |
| 3245 | getnext_flags |= LYS_GETNEXT_OUTPUT; |
| 3246 | } |
| 3247 | |
| 3248 | iter = NULL; |
| 3249 | while ((iter = lys_getnext(iter, parent, ctx->mod->compiled, getnext_flags))) { |
| 3250 | if (CHECK_NODE(iter, exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3251 | goto error; |
| 3252 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3253 | |
| 3254 | /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */ |
| 3255 | if (iter->nodetype == LYS_CHOICE) { |
| 3256 | iter2 = NULL; |
| 3257 | while ((iter2 = lys_getnext(iter2, iter, NULL, LYS_GETNEXT_NOSTATECHECK))) { |
| 3258 | if (CHECK_NODE(iter2, exclude, name)) { |
| 3259 | goto error; |
| 3260 | } |
| 3261 | } |
| 3262 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3263 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3264 | |
| 3265 | actions = parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3266 | LY_ARRAY_FOR(actions, u) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3267 | if (CHECK_NODE(&actions[u], exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3268 | goto error; |
| 3269 | } |
| 3270 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3271 | |
| 3272 | notifs = parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3273 | LY_ARRAY_FOR(notifs, u) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3274 | if (CHECK_NODE(¬ifs[u], exclude, name)) { |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3275 | goto error; |
| 3276 | } |
| 3277 | } |
| 3278 | return LY_SUCCESS; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3279 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3280 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3281 | 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] | 3282 | return LY_EEXIST; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3283 | |
| 3284 | #undef CHECK_NODE |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3285 | } |
| 3286 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3287 | 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] | 3288 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3289 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3290 | * @brief Compile parsed RPC/action schema node information. |
| 3291 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3292 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3293 | * @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] | 3294 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3295 | * @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). |
| 3296 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3297 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3298 | */ |
| 3299 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3300 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3301 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3302 | { |
| 3303 | LY_ERR ret = LY_SUCCESS; |
| 3304 | struct lysp_node *child_p; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3305 | LY_ARRAY_COUNT_TYPE u; |
| 3306 | uint32_t opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3307 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3308 | lysc_update_path(ctx, parent, action_p->name); |
| 3309 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3310 | /* member needed for uniqueness check lys_getnext() */ |
| 3311 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
| 3312 | action->module = ctx->mod; |
| 3313 | action->parent = parent; |
| 3314 | |
| 3315 | 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] | 3316 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3317 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3318 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3319 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3320 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3321 | return LY_EVALID; |
| 3322 | } |
| 3323 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3324 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3325 | action->sp = action_p; |
| 3326 | } |
| 3327 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3328 | |
| 3329 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3330 | * 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] | 3331 | 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] | 3332 | |
| 3333 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3334 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3335 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3336 | 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] | 3337 | 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] | 3338 | |
| 3339 | /* input */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3340 | lysc_update_path(ctx, (struct lysc_node *)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3341 | 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] | 3342 | 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] | 3343 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3344 | LY_LIST_FOR(action_p->input.data, child_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3345 | 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] | 3346 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3347 | lysc_update_path(ctx, NULL, NULL); |
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 | |
| 3350 | /* output */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3351 | lysc_update_path(ctx, (struct lysc_node *)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3352 | 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] | 3353 | 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] | 3354 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3355 | LY_LIST_FOR(action_p->output.data, child_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3356 | 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] | 3357 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3358 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3359 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3360 | |
| 3361 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3362 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3363 | ret = ly_set_add(&ctx->xpath, action, 0, NULL); |
| 3364 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3365 | } |
| 3366 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3367 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3368 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3369 | return ret; |
| 3370 | } |
| 3371 | |
| 3372 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3373 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3374 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3375 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3376 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3377 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3378 | * @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] | 3379 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3380 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3381 | */ |
| 3382 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3383 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3384 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3385 | { |
| 3386 | LY_ERR ret = LY_SUCCESS; |
| 3387 | struct lysp_node *child_p; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3388 | LY_ARRAY_COUNT_TYPE u; |
| 3389 | uint32_t opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3390 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3391 | lysc_update_path(ctx, parent, notif_p->name); |
| 3392 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 3393 | /* member needed for uniqueness check lys_getnext() */ |
| 3394 | notif->nodetype = LYS_NOTIF; |
| 3395 | notif->module = ctx->mod; |
| 3396 | notif->parent = parent; |
| 3397 | |
| 3398 | 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] | 3399 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3400 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3401 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3402 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3403 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3404 | return LY_EVALID; |
| 3405 | } |
| 3406 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3407 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3408 | notif->sp = notif_p; |
| 3409 | } |
| 3410 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3411 | |
| 3412 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3413 | * 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] | 3414 | ret = lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0)); |
| 3415 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3416 | |
| 3417 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3418 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3419 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3420 | 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] | 3421 | 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] | 3422 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3423 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3424 | ret = ly_set_add(&ctx->xpath, notif, 0, NULL); |
| 3425 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3426 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3427 | 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] | 3428 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3429 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3430 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3431 | ret = lys_compile_node(ctx, child_p, (struct lysc_node *)notif, uses_status); |
| 3432 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3433 | } |
| 3434 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3435 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3436 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3437 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3438 | return ret; |
| 3439 | } |
| 3440 | |
| 3441 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3442 | * @brief Compile parsed container node information. |
| 3443 | * @param[in] ctx Compile context |
| 3444 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3445 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3446 | * is enriched with the container-specific information. |
| 3447 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3448 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3449 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3450 | 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] | 3451 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3452 | struct lysp_node_container *cont_p = (struct lysp_node_container *)node_p; |
| 3453 | struct lysc_node_container *cont = (struct lysc_node_container *)node; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3454 | struct lysp_node *child_p; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3455 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3456 | LY_ERR ret = LY_SUCCESS; |
| 3457 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3458 | if (cont_p->presence) { |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3459 | /* explicit presence */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3460 | cont->flags |= LYS_PRESENCE; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3461 | } else if (cont_p->musts) { |
| 3462 | /* container with a must condition */ |
Radek Krejci | 175f25b | 2020-08-13 12:02:36 +0200 | [diff] [blame] | 3463 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name); |
| 3464 | cont->flags |= LYS_PRESENCE; |
| 3465 | } else if (cont_p->when) { |
| 3466 | /* container with a when condition */ |
| 3467 | 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] | 3468 | cont->flags |= LYS_PRESENCE; |
| 3469 | } else if (cont_p->parent) { |
| 3470 | if (cont_p->parent->nodetype == LYS_CHOICE) { |
| 3471 | /* 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] | 3472 | 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] | 3473 | cont_p->name, cont_p->parent->name); |
| 3474 | cont->flags |= LYS_PRESENCE; |
| 3475 | } else if ((cont_p->parent->nodetype == LYS_CASE) |
| 3476 | && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) { |
| 3477 | /* 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] | 3478 | 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] | 3479 | cont_p->name, cont_p->parent->name); |
| 3480 | cont->flags |= LYS_PRESENCE; |
| 3481 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3482 | } |
| 3483 | |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3484 | /* more cases when the container has meaning but is kept NP for convenience: |
| 3485 | * - when condition |
| 3486 | * - direct child action/notification |
| 3487 | */ |
| 3488 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3489 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3490 | ret = lys_compile_node(ctx, child_p, node, 0); |
| 3491 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3492 | } |
| 3493 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3494 | 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] | 3495 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3496 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3497 | ret = ly_set_add(&ctx->xpath, cont, 0, NULL); |
| 3498 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3499 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3500 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3501 | 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] | 3502 | |
| 3503 | done: |
| 3504 | return ret; |
| 3505 | } |
| 3506 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3507 | /* |
| 3508 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3509 | * @param[in] ctx Compile context. |
| 3510 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3511 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3512 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3513 | * @return LY_ERR value. |
| 3514 | */ |
| 3515 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3516 | 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] | 3517 | struct lysc_node_leaf *leaf) |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3518 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3519 | const char *dflt; |
| 3520 | struct lys_module *dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3521 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3522 | 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] | 3523 | leaf->units ? NULL : &leaf->units, &dflt, &dflt_mod)); |
| 3524 | |
| 3525 | /* store default value, if any */ |
| 3526 | if (dflt && !(leaf->flags & LYS_SET_DFLT)) { |
| 3527 | 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] | 3528 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3529 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3530 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3531 | /* 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] | 3532 | LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3533 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3534 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3535 | LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) { |
| 3536 | 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] | 3537 | /* 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] | 3538 | LY_CHECK_RET(ly_set_add(&ctx->leafrefs, leaf, 0, NULL)); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3539 | } |
| 3540 | } |
| 3541 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3542 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3543 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3544 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3545 | return LY_EVALID; |
| 3546 | } |
| 3547 | } |
| 3548 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3549 | return LY_SUCCESS; |
| 3550 | } |
| 3551 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3552 | /** |
| 3553 | * @brief Compile parsed leaf node information. |
| 3554 | * @param[in] ctx Compile context |
| 3555 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3556 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3557 | * is enriched with the leaf-specific information. |
| 3558 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3559 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3560 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3561 | 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] | 3562 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3563 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)node_p; |
| 3564 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 3565 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3566 | LY_ERR ret = LY_SUCCESS; |
| 3567 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3568 | 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] | 3569 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3570 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3571 | ret = ly_set_add(&ctx->xpath, leaf, 0, NULL); |
| 3572 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3573 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3574 | if (leaf_p->units) { |
| 3575 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3576 | leaf->flags |= LYS_SET_UNITS; |
| 3577 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3578 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3579 | /* compile type */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3580 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
| 3581 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3582 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3583 | /* store/update default value */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3584 | if (leaf_p->dflt) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3585 | 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] | 3586 | leaf->flags |= LYS_SET_DFLT; |
| 3587 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3588 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3589 | done: |
| 3590 | return ret; |
| 3591 | } |
| 3592 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3593 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3594 | * @brief Compile parsed leaf-list node information. |
| 3595 | * @param[in] ctx Compile context |
| 3596 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3597 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3598 | * is enriched with the leaf-list-specific information. |
| 3599 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3600 | */ |
| 3601 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3602 | 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] | 3603 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3604 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)node_p; |
| 3605 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3606 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3607 | LY_ERR ret = LY_SUCCESS; |
| 3608 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3609 | 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] | 3610 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3611 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3612 | ret = ly_set_add(&ctx->xpath, llist, 0, NULL); |
| 3613 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3614 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3615 | if (llist_p->units) { |
| 3616 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3617 | llist->flags |= LYS_SET_UNITS; |
| 3618 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3619 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3620 | /* compile type */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3621 | ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf *)llist); |
| 3622 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 6a044b2 | 2020-01-15 12:25:39 +0100 | [diff] [blame] | 3623 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3624 | /* store/update default values */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3625 | if (llist_p->dflts) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3626 | 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] | 3627 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3628 | } |
| 3629 | |
| 3630 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3631 | if (llist->min) { |
| 3632 | llist->flags |= LYS_MAND_TRUE; |
| 3633 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3634 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3635 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3636 | done: |
| 3637 | return ret; |
| 3638 | } |
| 3639 | |
| 3640 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3641 | * @brief Compile information about list's uniques. |
| 3642 | * @param[in] ctx Compile context. |
| 3643 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3644 | * @param[in] uniques Sized array list of unique statements. |
| 3645 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3646 | * @return LY_ERR value. |
| 3647 | */ |
| 3648 | static LY_ERR |
| 3649 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3650 | { |
| 3651 | LY_ERR ret = LY_SUCCESS; |
| 3652 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3653 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3654 | const char *keystr, *delim; |
| 3655 | size_t len; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3656 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3657 | 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] | 3658 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3659 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3660 | for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3661 | config = -1; |
| 3662 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3663 | keystr = uniques[v]; |
| 3664 | while (keystr) { |
| 3665 | delim = strpbrk(keystr, " \t\n"); |
| 3666 | if (delim) { |
| 3667 | len = delim - keystr; |
| 3668 | while (isspace(*delim)) { |
| 3669 | ++delim; |
| 3670 | } |
| 3671 | } else { |
| 3672 | len = strlen(keystr); |
| 3673 | } |
| 3674 | |
| 3675 | /* unique node must be present */ |
| 3676 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3677 | ret = lysc_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node *)list, context_module, LYS_LEAF, 0, |
| 3678 | (const struct lysc_node **)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3679 | if (ret != LY_SUCCESS) { |
| 3680 | if (ret == LY_EDENIED) { |
| 3681 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3682 | "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] | 3683 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3684 | } |
| 3685 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3686 | } else if (flags) { |
| 3687 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3688 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3689 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3690 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3691 | } |
| 3692 | |
| 3693 | /* all referenced leafs must be of the same config type */ |
| 3694 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3695 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3696 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3697 | return LY_EVALID; |
| 3698 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3699 | config = 1; |
| 3700 | } else { /* LYS_CONFIG_R */ |
| 3701 | config = 0; |
| 3702 | } |
| 3703 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3704 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3705 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3706 | if (parent->nodetype == LYS_LIST) { |
| 3707 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3708 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3709 | return LY_EVALID; |
| 3710 | } |
| 3711 | } |
| 3712 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3713 | /* check status */ |
| 3714 | 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] | 3715 | (*key)->flags, (*key)->module, (*key)->name)); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3716 | |
| 3717 | /* mark leaf as unique */ |
| 3718 | (*key)->flags |= LYS_UNIQUE; |
| 3719 | |
| 3720 | /* next unique value in line */ |
| 3721 | keystr = delim; |
| 3722 | } |
| 3723 | /* next unique definition */ |
| 3724 | } |
| 3725 | |
| 3726 | return LY_SUCCESS; |
| 3727 | } |
| 3728 | |
| 3729 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3730 | * @brief Compile parsed list node information. |
| 3731 | * @param[in] ctx Compile context |
| 3732 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3733 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3734 | * is enriched with the list-specific information. |
| 3735 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3736 | */ |
| 3737 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3738 | 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] | 3739 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3740 | struct lysp_node_list *list_p = (struct lysp_node_list *)node_p; |
| 3741 | struct lysc_node_list *list = (struct lysc_node_list *)node; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3742 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3743 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3744 | size_t len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3745 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3746 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3747 | LY_ERR ret = LY_SUCCESS; |
| 3748 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3749 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3750 | if (list->min) { |
| 3751 | list->flags |= LYS_MAND_TRUE; |
| 3752 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3753 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3754 | |
| 3755 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3756 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3757 | } |
| 3758 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3759 | 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] | 3760 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3761 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3762 | LY_CHECK_RET(ly_set_add(&ctx->xpath, list, 0, NULL)); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3763 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3764 | |
| 3765 | /* keys */ |
| 3766 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3767 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3768 | return LY_EVALID; |
| 3769 | } |
| 3770 | |
| 3771 | /* find all the keys (must be direct children) */ |
| 3772 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3773 | if (!keystr) { |
| 3774 | /* keyless list */ |
| 3775 | list->flags |= LYS_KEYLESS; |
| 3776 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3777 | while (keystr) { |
| 3778 | delim = strpbrk(keystr, " \t\n"); |
| 3779 | if (delim) { |
| 3780 | len = delim - keystr; |
| 3781 | while (isspace(*delim)) { |
| 3782 | ++delim; |
| 3783 | } |
| 3784 | } else { |
| 3785 | len = strlen(keystr); |
| 3786 | } |
| 3787 | |
| 3788 | /* key node must be present */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3789 | 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] | 3790 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3791 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3792 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3793 | return LY_EVALID; |
| 3794 | } |
| 3795 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3796 | if (key->flags & LYS_KEY) { |
| 3797 | /* the node was already marked as a key */ |
| 3798 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3799 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3800 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3801 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3802 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3803 | lysc_update_path(ctx, (struct lysc_node *)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3804 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3805 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3806 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3807 | return LY_EVALID; |
| 3808 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3809 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3810 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3811 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3812 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3813 | "List's key 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] | 3814 | return LY_EVALID; |
| 3815 | } |
| 3816 | } else { |
| 3817 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3818 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3819 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3820 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3821 | return LY_EVALID; |
| 3822 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3823 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3824 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3825 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3826 | return LY_EVALID; |
| 3827 | } |
| 3828 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3829 | |
| 3830 | /* check status */ |
| 3831 | 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] | 3832 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3833 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3834 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3835 | if (key->dflt) { |
| 3836 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3837 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3838 | free(key->dflt); |
| 3839 | key->dflt = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3840 | } |
| 3841 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3842 | key->flags |= LYS_KEY; |
| 3843 | |
| 3844 | /* move it to the correct position */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3845 | 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] | 3846 | /* fix links in closest previous siblings of the key */ |
| 3847 | if (key->next) { |
| 3848 | key->next->prev = key->prev; |
| 3849 | } else { |
| 3850 | /* last child */ |
| 3851 | list->child->prev = key->prev; |
| 3852 | } |
| 3853 | if (key->prev->next) { |
| 3854 | key->prev->next = key->next; |
| 3855 | } |
| 3856 | /* fix links in the key */ |
| 3857 | if (prev_key) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3858 | key->prev = (struct lysc_node *)prev_key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3859 | key->next = prev_key->next; |
| 3860 | } else { |
| 3861 | key->prev = list->child->prev; |
| 3862 | key->next = list->child; |
| 3863 | } |
| 3864 | /* fix links in closes future siblings of the key */ |
| 3865 | if (prev_key) { |
| 3866 | if (prev_key->next) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3867 | prev_key->next->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3868 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3869 | list->child->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3870 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3871 | prev_key->next = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3872 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3873 | list->child->prev = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3874 | } |
| 3875 | /* fix links in parent */ |
| 3876 | if (!key->prev->next) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3877 | list->child = (struct lysc_node *)key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3878 | } |
| 3879 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3880 | |
| 3881 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3882 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3883 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3884 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | /* uniques */ |
| 3888 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3889 | 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] | 3890 | } |
| 3891 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3892 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3893 | 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] | 3894 | |
| 3895 | done: |
| 3896 | return ret; |
| 3897 | } |
| 3898 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3899 | /** |
| 3900 | * @brief Do some checks and set the default choice's case. |
| 3901 | * |
| 3902 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3903 | * |
| 3904 | * @param[in] ctx Compile context. |
| 3905 | * @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, |
| 3906 | * not the reference to the imported module. |
| 3907 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3908 | * @return LY_ERR value. |
| 3909 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3910 | static LY_ERR |
| 3911 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3912 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3913 | struct lysc_node *iter, *node = (struct lysc_node *)ch; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3914 | const char *prefix = NULL, *name; |
| 3915 | size_t prefix_len = 0; |
| 3916 | |
| 3917 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3918 | name = strchr(dflt, ':'); |
| 3919 | if (name) { |
| 3920 | prefix = dflt; |
| 3921 | prefix_len = name - prefix; |
| 3922 | ++name; |
| 3923 | } else { |
| 3924 | name = dflt; |
| 3925 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 3926 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3927 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3928 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3929 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3930 | prefix_len, prefix); |
| 3931 | return LY_EVALID; |
| 3932 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3933 | 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] | 3934 | if (!ch->dflt) { |
| 3935 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3936 | "Default case \"%s\" not found.", dflt); |
| 3937 | return LY_EVALID; |
| 3938 | } |
| 3939 | /* no mandatory nodes directly under the default case */ |
| 3940 | LY_LIST_FOR(ch->dflt->child, iter) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3941 | if (iter->parent != (struct lysc_node *)ch->dflt) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3942 | break; |
| 3943 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3944 | if (iter->flags & LYS_MAND_TRUE) { |
| 3945 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3946 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3947 | return LY_EVALID; |
| 3948 | } |
| 3949 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3950 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3951 | return LY_SUCCESS; |
| 3952 | } |
| 3953 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3954 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3955 | 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] | 3956 | { |
| 3957 | struct lys_module *mod; |
| 3958 | const char *prefix = NULL, *name; |
| 3959 | size_t prefix_len = 0; |
| 3960 | struct lysc_node_case *cs; |
| 3961 | struct lysc_node *node; |
| 3962 | |
| 3963 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3964 | name = strchr(dflt, ':'); |
| 3965 | if (name) { |
| 3966 | prefix = dflt; |
| 3967 | prefix_len = name - prefix; |
| 3968 | ++name; |
| 3969 | } else { |
| 3970 | name = dflt; |
| 3971 | } |
| 3972 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3973 | if (prefix) { |
| 3974 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3975 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3976 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3977 | "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] | 3978 | return LY_EVALID; |
| 3979 | } |
| 3980 | } else { |
| 3981 | mod = ctx->mod; |
| 3982 | } |
| 3983 | /* get the default case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3984 | 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] | 3985 | if (!cs) { |
| 3986 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3987 | "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] | 3988 | return LY_EVALID; |
| 3989 | } |
| 3990 | |
| 3991 | /* check that there is no mandatory node */ |
| 3992 | LY_LIST_FOR(cs->child, node) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3993 | if (node->parent != (struct lysc_node *)cs) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3994 | break; |
| 3995 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3996 | if (node->flags & LYS_MAND_TRUE) { |
| 3997 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3998 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 3999 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4000 | return LY_EVALID; |
| 4001 | } |
| 4002 | } |
| 4003 | |
| 4004 | /* set the default case in choice */ |
| 4005 | ch->dflt = cs; |
| 4006 | cs->flags |= LYS_SET_DFLT; |
| 4007 | |
| 4008 | return LY_SUCCESS; |
| 4009 | } |
| 4010 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4011 | /** |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4012 | * @brief Compile choice children. |
| 4013 | * |
| 4014 | * @param[in] ctx Compile context |
| 4015 | * @param[in] child_p Parsed choice children nodes. |
| 4016 | * @param[in] node Compiled choice node to compile and add children to. |
| 4017 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4018 | */ |
| 4019 | static LY_ERR |
| 4020 | lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node) |
| 4021 | { |
| 4022 | LY_ERR ret = LY_SUCCESS; |
| 4023 | struct lysp_node *child_next_p; |
| 4024 | struct lysp_node_case *cs_p; |
| 4025 | |
| 4026 | if (child_p->nodetype == LYS_CASE) { |
| 4027 | /* standard case under choice */ |
| 4028 | ret = lys_compile_node(ctx, child_p, node, 0); |
| 4029 | } else { |
| 4030 | /* we need the implicit case first, so create a fake parsed case */ |
| 4031 | cs_p = calloc(1, sizeof *cs_p); |
| 4032 | cs_p->nodetype = LYS_CASE; |
| 4033 | DUP_STRING(ctx->ctx, child_p->name, cs_p->name); |
| 4034 | cs_p->child = child_p; |
| 4035 | |
| 4036 | /* make the child the only case child */ |
| 4037 | child_next_p = child_p->next; |
| 4038 | child_p->next = NULL; |
| 4039 | |
| 4040 | /* compile it normally */ |
| 4041 | ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0); |
| 4042 | |
| 4043 | /* free the fake parsed node and correct pointers back */ |
| 4044 | cs_p->child = NULL; |
| 4045 | lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p); |
| 4046 | child_p->next = child_next_p; |
| 4047 | } |
| 4048 | |
| 4049 | return ret; |
| 4050 | } |
| 4051 | |
| 4052 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4053 | * @brief Compile parsed choice node information. |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4054 | * |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4055 | * @param[in] ctx Compile context |
| 4056 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4057 | * @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] | 4058 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4059 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4060 | */ |
| 4061 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4062 | 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] | 4063 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4064 | struct lysp_node_choice *ch_p = (struct lysp_node_choice *)node_p; |
| 4065 | struct lysc_node_choice *ch = (struct lysc_node_choice *)node; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4066 | struct lysp_node *child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4067 | LY_ERR ret = LY_SUCCESS; |
| 4068 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4069 | assert(node->nodetype == LYS_CHOICE); |
| 4070 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4071 | LY_LIST_FOR(ch_p->child, child_p) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4072 | LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4076 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4077 | 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] | 4078 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4079 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4080 | return ret; |
| 4081 | } |
| 4082 | |
| 4083 | /** |
| 4084 | * @brief Compile parsed anydata or anyxml node information. |
| 4085 | * @param[in] ctx Compile context |
| 4086 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4087 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4088 | * is enriched with the any-specific information. |
| 4089 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4090 | */ |
| 4091 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4092 | 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] | 4093 | { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4094 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)node_p; |
| 4095 | struct lysc_node_anydata *any = (struct lysc_node_anydata *)node; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4096 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4097 | LY_ERR ret = LY_SUCCESS; |
| 4098 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4099 | 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] | 4100 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4101 | /* do not check "must" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4102 | ret = ly_set_add(&ctx->xpath, any, 0, NULL); |
| 4103 | LY_CHECK_GOTO(ret, done); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4104 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4105 | |
| 4106 | if (any->flags & LYS_CONFIG_W) { |
Radek Krejci | 5c4ed7b | 2020-08-12 11:29:44 +0200 | [diff] [blame] | 4107 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s", |
| 4108 | 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] | 4109 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4110 | done: |
| 4111 | return ret; |
| 4112 | } |
| 4113 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4114 | /** |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4115 | * @brief Connect the node into the siblings list and check its name uniqueness. Also, |
| 4116 | * keep specific order of augments targetting the same node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4117 | * |
| 4118 | * @param[in] ctx Compile context |
| 4119 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4120 | * the choice itself is expected instead of a specific case node. |
| 4121 | * @param[in] node Schema node to connect into the list. |
| 4122 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4123 | * 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] | 4124 | */ |
| 4125 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4126 | 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] | 4127 | { |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4128 | struct lysc_node **children, *start, *end; |
| 4129 | const struct lys_module *mod; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4130 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4131 | node->parent = parent; |
| 4132 | |
| 4133 | if (parent) { |
| 4134 | if (parent->nodetype == LYS_CHOICE) { |
| 4135 | assert(node->nodetype == LYS_CASE); |
| 4136 | children = (struct lysc_node **)&((struct lysc_node_choice *)parent)->cases; |
| 4137 | } else { |
| 4138 | children = lysc_node_children_p(parent, ctx->options); |
| 4139 | } |
| 4140 | assert(children); |
| 4141 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4142 | if (!(*children)) { |
| 4143 | /* first child */ |
| 4144 | *children = node; |
| 4145 | } else if (*children != node) { |
| 4146 | /* by the condition in previous branch we cover the choice/case children |
| 4147 | * - the children list is shared by the choice and the the first case, in addition |
| 4148 | * the first child of each case must be referenced from the case node. So the node is |
| 4149 | * actually always already inserted in case it is the first children - so here such |
| 4150 | * a situation actually corresponds to the first branch */ |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4151 | if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module) |
| 4152 | && (strcmp((*children)->prev->module->name, node->module->name) > 0)) { |
| 4153 | /* some augments are already connected and we are connecting new ones, |
| 4154 | * keep module name order and insert the node into the children list */ |
| 4155 | end = (*children); |
| 4156 | do { |
| 4157 | end = end->prev; |
| 4158 | mod = end->module; |
| 4159 | while (end->prev->module == mod) { |
| 4160 | end = end->prev; |
| 4161 | } |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 4162 | } 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] | 4163 | |
| 4164 | /* we have the last existing node after our node, easily get the first before and connect it */ |
| 4165 | start = end->prev; |
| 4166 | start->next = node; |
| 4167 | node->next = end; |
| 4168 | end->prev = node; |
| 4169 | node->prev = start; |
| 4170 | } else { |
| 4171 | /* insert at the end of the parent's children list */ |
| 4172 | (*children)->prev->next = node; |
| 4173 | node->prev = (*children)->prev; |
| 4174 | (*children)->prev = node; |
| 4175 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4176 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4177 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4178 | /* check the name uniqueness (even for an only child, it may be in case) */ |
| 4179 | if (lys_compile_node_uniqness(ctx, parent, node->name, node)) { |
| 4180 | return LY_EEXIST; |
| 4181 | } |
| 4182 | } else { |
| 4183 | /* top-level element */ |
| 4184 | if (!ctx->mod->compiled->data) { |
| 4185 | ctx->mod->compiled->data = node; |
| 4186 | } else { |
| 4187 | /* insert at the end of the module's top-level nodes list */ |
| 4188 | ctx->mod->compiled->data->prev->next = node; |
| 4189 | node->prev = ctx->mod->compiled->data->prev; |
| 4190 | ctx->mod->compiled->data->prev = node; |
| 4191 | } |
| 4192 | |
| 4193 | /* check the name uniqueness on top-level */ |
| 4194 | if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) { |
| 4195 | return LY_EEXIST; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4196 | } |
| 4197 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4198 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4199 | return LY_SUCCESS; |
| 4200 | } |
| 4201 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4202 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4203 | * @brief Prepare the case structure in choice node for the new data node. |
| 4204 | * |
| 4205 | * 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 |
| 4206 | * created in the choice when the first child was processed. |
| 4207 | * |
| 4208 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4209 | * @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] | 4210 | * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4211 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4212 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4213 | * @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, |
| 4214 | * 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] | 4215 | */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4216 | static LY_ERR |
| 4217 | 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] | 4218 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4219 | struct lysp_node *child_p; |
| 4220 | struct lysp_node_case *cs_p = (struct lysp_node_case *)node_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4221 | |
Radek Krejci | 3942480 | 2020-08-12 09:31:00 +0200 | [diff] [blame] | 4222 | if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4223 | /* we have to add an implicit case node into the parent choice */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4224 | } else if (node_p->nodetype == LYS_CASE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4225 | /* explicit parent case */ |
| 4226 | LY_LIST_FOR(cs_p->child, child_p) { |
| 4227 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4228 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4229 | } else { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4230 | LOGINT_RET(ctx->ctx); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4231 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4232 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4233 | return LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4234 | } |
| 4235 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4236 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4237 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4238 | * |
| 4239 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4240 | * @param[in] node Target node where the config is supposed to be changed. |
| 4241 | * @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] | 4242 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4243 | * 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] | 4244 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4245 | * @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] | 4246 | * @return LY_ERR value. |
| 4247 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4248 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4249 | lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4250 | uint8_t inheriting, uint8_t refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4251 | { |
| 4252 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4253 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4254 | |
| 4255 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4256 | /* nothing to do */ |
| 4257 | return LY_SUCCESS; |
| 4258 | } |
| 4259 | |
| 4260 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4261 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4262 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4263 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4264 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4265 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4266 | return LY_EVALID; |
| 4267 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4268 | node->flags |= LYS_SET_CONFIG; |
| 4269 | } else { |
| 4270 | if (node->flags & LYS_SET_CONFIG) { |
| 4271 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4272 | /* setting config flags, but have node with explicit config true */ |
| 4273 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4274 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4275 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4276 | return LY_EVALID; |
| 4277 | } |
| 4278 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4279 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4280 | return LY_SUCCESS; |
| 4281 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4282 | } |
| 4283 | node->flags &= ~LYS_CONFIG_MASK; |
| 4284 | node->flags |= config; |
| 4285 | |
| 4286 | /* inherit the change into the children */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4287 | LY_LIST_FOR((struct lysc_node *)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4288 | 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] | 4289 | } |
| 4290 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4291 | return LY_SUCCESS; |
| 4292 | } |
| 4293 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4294 | /** |
| 4295 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4296 | * |
| 4297 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4298 | * the flag to such parents from a mandatory children. |
| 4299 | * |
| 4300 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4301 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4302 | * (mandatory children was removed). |
| 4303 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4304 | static void |
| 4305 | lys_compile_mandatory_parents(struct lysc_node *parent, uint8_t add) |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4306 | { |
| 4307 | struct lysc_node *iter; |
| 4308 | |
| 4309 | if (add) { /* set flag */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4310 | 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] | 4311 | parent = parent->parent) { |
| 4312 | parent->flags |= LYS_MAND_TRUE; |
| 4313 | } |
| 4314 | } else { /* unset flag */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4315 | 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] | 4316 | 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] | 4317 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4318 | /* there is another mandatory node */ |
| 4319 | return; |
| 4320 | } |
| 4321 | } |
| 4322 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4323 | parent->flags &= ~LYS_MAND_TRUE; |
| 4324 | } |
| 4325 | } |
| 4326 | } |
| 4327 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4328 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4329 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4330 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4331 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4332 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4333 | */ |
| 4334 | static void |
| 4335 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4336 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4337 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4338 | size_t len; |
| 4339 | |
| 4340 | len = strlen(aug_p->nodeid); |
| 4341 | LY_ARRAY_FOR(result, v) { |
| 4342 | if (strlen(result[v]->nodeid) <= len) { |
| 4343 | continue; |
| 4344 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4345 | if (v < LY_ARRAY_COUNT(result)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4346 | /* move the rest of array */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4347 | 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] | 4348 | break; |
| 4349 | } |
| 4350 | } |
| 4351 | result[v] = aug_p; |
| 4352 | LY_ARRAY_INCREMENT(result); |
| 4353 | } |
| 4354 | |
| 4355 | /** |
| 4356 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4357 | * |
| 4358 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4359 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4360 | * |
| 4361 | * @param[in] ctx Compile context. |
| 4362 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4363 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4364 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4365 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4366 | * @return LY_ERR value. |
| 4367 | */ |
| 4368 | LY_ERR |
| 4369 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4370 | { |
| 4371 | struct lysp_augment **result = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4372 | LY_ARRAY_COUNT_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4373 | |
| 4374 | assert(augments); |
| 4375 | |
| 4376 | /* get count of the augments in module and all its submodules */ |
| 4377 | if (aug_p) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4378 | count += LY_ARRAY_COUNT(aug_p); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4379 | } |
| 4380 | LY_ARRAY_FOR(inc_p, u) { |
| 4381 | if (inc_p[u].submodule->augments) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4382 | count += LY_ARRAY_COUNT(inc_p[u].submodule->augments); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4383 | } |
| 4384 | } |
| 4385 | |
| 4386 | if (!count) { |
| 4387 | *augments = NULL; |
| 4388 | return LY_SUCCESS; |
| 4389 | } |
| 4390 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4391 | |
| 4392 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4393 | * together, so there can be even /z/y betwwen them. */ |
| 4394 | LY_ARRAY_FOR(aug_p, u) { |
| 4395 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4396 | } |
| 4397 | LY_ARRAY_FOR(inc_p, u) { |
| 4398 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4399 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4400 | } |
| 4401 | } |
| 4402 | |
| 4403 | *augments = result; |
| 4404 | return LY_SUCCESS; |
| 4405 | } |
| 4406 | |
| 4407 | /** |
| 4408 | * @brief Compile the parsed augment connecting it into its target. |
| 4409 | * |
| 4410 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4411 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4412 | * are already implemented and compiled. |
| 4413 | * |
| 4414 | * @param[in] ctx Compile context. |
| 4415 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4416 | * @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 |
| 4417 | * children in case of the augmenting uses data. |
| 4418 | * @return LY_SUCCESS on success. |
| 4419 | * @return LY_EVALID on failure. |
| 4420 | */ |
| 4421 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4422 | 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] | 4423 | { |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4424 | LY_ERR ret = LY_SUCCESS, rc; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4425 | struct lysp_node *node_p; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4426 | struct lysc_node *target; /* target target of the augment */ |
| 4427 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4428 | struct lysc_when **when, *when_shared; |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4429 | struct lys_module **aug_mod; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4430 | uint8_t allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4431 | uint16_t flags = 0; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4432 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4433 | uint32_t opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4434 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4435 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4436 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4437 | |
Michal Vasko | cb18c7f | 2020-08-24 09:22:28 +0200 | [diff] [blame] | 4438 | 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] | 4439 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4440 | 1, (const struct lysc_node **)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4441 | if (ret != LY_SUCCESS) { |
| 4442 | if (ret == LY_EDENIED) { |
| 4443 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4444 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4445 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4446 | } |
| 4447 | return LY_EVALID; |
| 4448 | } |
| 4449 | |
| 4450 | /* check for mandatory nodes |
| 4451 | * - new cases augmenting some choice can have mandatory nodes |
| 4452 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4453 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4454 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4455 | allow_mandatory = 1; |
| 4456 | } |
| 4457 | |
| 4458 | when_shared = NULL; |
| 4459 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4460 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4461 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4462 | && !((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] | 4463 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4464 | && !(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] | 4465 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4466 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4467 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4468 | return LY_EVALID; |
| 4469 | } |
| 4470 | |
| 4471 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4472 | ctx->options |= flags; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4473 | if (target->nodetype == LYS_CHOICE) { |
| 4474 | LY_CHECK_RET(lys_compile_node_choice_child(ctx, node_p, target)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4475 | } else { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4476 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4477 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4478 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4479 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4480 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4481 | * 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] | 4482 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4483 | /* 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] | 4484 | 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] | 4485 | } else if (target->nodetype == LYS_CHOICE) { |
| 4486 | /* 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] | 4487 | node = ((struct lysc_node_choice *)target)->cases->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4488 | } else { |
| 4489 | /* the compiled node is the last child of the target */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4490 | node = (struct lysc_node *)lysc_node_children(target, flags); |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4491 | if (!node) { |
| 4492 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4493 | break; |
| 4494 | } |
| 4495 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4496 | } |
| 4497 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4498 | 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] | 4499 | node->flags &= ~LYS_MAND_TRUE; |
| 4500 | lys_compile_mandatory_parents(target, 0); |
| 4501 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4502 | "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] | 4503 | return LY_EVALID; |
| 4504 | } |
| 4505 | |
| 4506 | /* pass augment's when to all the children */ |
| 4507 | if (aug_p->when) { |
| 4508 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4509 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4510 | 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] | 4511 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4512 | |
| 4513 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4514 | /* do not check "when" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4515 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 4516 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4517 | } |
| 4518 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4519 | when_shared = *when; |
| 4520 | } else { |
| 4521 | ++when_shared->refcount; |
| 4522 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4523 | |
| 4524 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4525 | /* 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] | 4526 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 4527 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4528 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4529 | } |
| 4530 | } |
| 4531 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4532 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4533 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4534 | switch (target->nodetype) { |
| 4535 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4536 | 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] | 4537 | u, lys_compile_action, 0, ret, error); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4538 | 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] | 4539 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4540 | break; |
| 4541 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4542 | 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] | 4543 | u, lys_compile_action, 0, ret, error); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4544 | 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] | 4545 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4546 | break; |
| 4547 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4548 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4549 | if (aug_p->actions) { |
| 4550 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4551 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4552 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4553 | return LY_EVALID; |
| 4554 | } |
| 4555 | if (aug_p->notifs) { |
| 4556 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4557 | "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] | 4558 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4559 | return LY_EVALID; |
| 4560 | } |
| 4561 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4562 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4563 | /* add this module into the target module augmented_by, if not there already */ |
| 4564 | rc = LY_SUCCESS; |
| 4565 | LY_ARRAY_FOR(target->module->compiled->augmented_by, v) { |
| 4566 | if (target->module->compiled->augmented_by[v] == ctx->mod) { |
| 4567 | rc = LY_EEXIST; |
| 4568 | break; |
| 4569 | } |
| 4570 | } |
| 4571 | if (!rc) { |
| 4572 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error); |
| 4573 | *aug_mod = ctx->mod; |
| 4574 | } |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4575 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4576 | lysc_update_path(ctx, NULL, NULL); |
| 4577 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4578 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4579 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4580 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4581 | return ret; |
| 4582 | } |
| 4583 | |
| 4584 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4585 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4586 | * |
| 4587 | * @param[in] ctx Compile context. |
| 4588 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4589 | * @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] | 4590 | * @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] | 4591 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4592 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4593 | * 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] | 4594 | * @return LY_ERR value. |
| 4595 | */ |
| 4596 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4597 | lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, uint8_t refine_flag) |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4598 | { |
| 4599 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4600 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4601 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4602 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4603 | return LY_EVALID; |
| 4604 | } |
| 4605 | |
| 4606 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4607 | /* check if node has default value */ |
| 4608 | if (node->nodetype & LYS_LEAF) { |
| 4609 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4610 | if (refine_flag) { |
| 4611 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4612 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4613 | return LY_EVALID; |
| 4614 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4615 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4616 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4617 | if (refine_flag) { |
| 4618 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4619 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4620 | return LY_EVALID; |
| 4621 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4622 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4623 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4624 | 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] | 4625 | return LY_EVALID; |
| 4626 | } |
| 4627 | |
| 4628 | node->flags &= ~LYS_MAND_FALSE; |
| 4629 | node->flags |= LYS_MAND_TRUE; |
| 4630 | lys_compile_mandatory_parents(node->parent, 1); |
| 4631 | } else { |
| 4632 | /* make mandatory false */ |
| 4633 | node->flags &= ~LYS_MAND_TRUE; |
| 4634 | node->flags |= LYS_MAND_FALSE; |
| 4635 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4636 | } |
| 4637 | return LY_SUCCESS; |
| 4638 | } |
| 4639 | |
| 4640 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4641 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4642 | * If present, also apply uses's modificators. |
| 4643 | * |
| 4644 | * @param[in] ctx Compile context |
| 4645 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4646 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4647 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4648 | * the compile context. |
| 4649 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4650 | */ |
| 4651 | static LY_ERR |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4652 | lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct lysc_node **first_p) |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4653 | { |
| 4654 | struct lysp_node *node_p; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4655 | struct lysc_node *node, *child, *iter; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4656 | /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4657 | struct lysc_node_container context_node_fake = |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4658 | {.nodetype = LYS_CONTAINER, |
| 4659 | .module = ctx->mod, |
| 4660 | .flags = parent ? parent->flags : 0, |
| 4661 | .child = NULL, .next = NULL, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4662 | .prev = (struct lysc_node *)&context_node_fake, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4663 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4664 | struct lysp_grp *grp = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4665 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4666 | uint32_t grp_stack_count; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4667 | uint8_t found = 0; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4668 | const char *id, *name, *prefix; |
| 4669 | size_t prefix_len, name_len; |
| 4670 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4671 | struct lysp_refine *rfn; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4672 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4673 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4674 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4675 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4676 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4677 | struct lysp_augment **augments = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4678 | LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4679 | struct lysc_notif **notifs = NULL; |
| 4680 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4681 | |
| 4682 | /* search for the grouping definition */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4683 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4684 | 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] | 4685 | if (prefix) { |
| 4686 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4687 | if (!mod) { |
| 4688 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4689 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4690 | return LY_EVALID; |
| 4691 | } |
| 4692 | } else { |
| 4693 | mod = ctx->mod_def; |
| 4694 | } |
| 4695 | if (mod == ctx->mod_def) { |
| 4696 | 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] | 4697 | grp = (struct lysp_grp *)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4698 | LY_ARRAY_FOR(grp, u) { |
| 4699 | if (!strcmp(grp[u].name, name)) { |
| 4700 | grp = &grp[u]; |
| 4701 | found = 1; |
| 4702 | break; |
| 4703 | } |
| 4704 | } |
| 4705 | } |
| 4706 | } |
| 4707 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4708 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4709 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4710 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4711 | for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4712 | if (!strcmp(grp[u].name, name)) { |
| 4713 | grp = &grp[u]; |
| 4714 | found = 1; |
| 4715 | } |
| 4716 | } |
| 4717 | } |
| 4718 | if (!found && mod->parsed->includes) { |
| 4719 | /* ... and all the submodules */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4720 | for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4721 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4722 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4723 | for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4724 | if (!strcmp(grp[v].name, name)) { |
| 4725 | grp = &grp[v]; |
| 4726 | found = 1; |
| 4727 | } |
| 4728 | } |
| 4729 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4730 | } |
| 4731 | } |
| 4732 | } |
| 4733 | if (!found) { |
| 4734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4735 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4736 | return LY_EVALID; |
| 4737 | } |
| 4738 | |
| 4739 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4740 | grp_stack_count = ctx->groupings.count; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4741 | LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL)); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4742 | if (grp_stack_count == ctx->groupings.count) { |
| 4743 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4744 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4745 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4746 | return LY_EVALID; |
| 4747 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4748 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4749 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4750 | grp->flags |= LYS_USED_GRP; |
| 4751 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4752 | |
| 4753 | /* switch context's mod_def */ |
| 4754 | mod_old = ctx->mod_def; |
| 4755 | ctx->mod_def = mod; |
| 4756 | |
| 4757 | /* check status */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4758 | ret = lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name); |
| 4759 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4760 | |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4761 | /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly |
| 4762 | * applu refine and augment only to the nodes from the uses */ |
| 4763 | if (parent) { |
| 4764 | if (parent->nodetype == LYS_CASE) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4765 | child = (struct lysc_node *)lysc_node_children(parent->parent, ctx->options & LYSC_OPT_RPC_MASK); |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4766 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4767 | child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4768 | } |
| 4769 | } else if (ctx->mod->compiled->data) { |
| 4770 | child = ctx->mod->compiled->data; |
| 4771 | } else { |
| 4772 | child = NULL; |
| 4773 | } |
| 4774 | /* remember the last child */ |
| 4775 | if (child) { |
| 4776 | child = child->prev; |
| 4777 | } |
| 4778 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4779 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4780 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4781 | /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4782 | ret = lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3); |
| 4783 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4784 | } |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4785 | |
| 4786 | /* split the children and add the uses's data into the fake context node */ |
| 4787 | if (child) { |
| 4788 | context_node_fake.child = child->next; |
| 4789 | } else if (parent) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4790 | context_node_fake.child = (struct lysc_node *)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4791 | } else if (ctx->mod->compiled->data) { |
| 4792 | context_node_fake.child = ctx->mod->compiled->data; |
| 4793 | } |
| 4794 | if (context_node_fake.child) { |
| 4795 | /* remember child as the last data node added by grouping to fix the list later */ |
| 4796 | child = context_node_fake.child->prev; |
| 4797 | context_node_fake.child->prev = NULL; |
| 4798 | } |
| 4799 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4800 | when_shared = NULL; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4801 | LY_LIST_FOR(context_node_fake.child, iter) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4802 | iter->parent = (struct lysc_node *)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4803 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4804 | /* pass uses's when to all the data children, actions and notifications are ignored */ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4805 | if (uses_p->when) { |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4806 | LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4807 | if (!when_shared) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4808 | ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when); |
| 4809 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4810 | |
| 4811 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4812 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4813 | ret = ly_set_add(&ctx->xpath, iter, 0, NULL); |
| 4814 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4815 | } |
| 4816 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4817 | when_shared = *when; |
| 4818 | } else { |
| 4819 | ++when_shared->refcount; |
| 4820 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4821 | |
| 4822 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4823 | /* in this case check "when" again for all children because of dummy node check */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4824 | ret = ly_set_add(&ctx->xpath, iter, 0, NULL); |
| 4825 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4826 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4827 | } |
| 4828 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4829 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4830 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4831 | /* compile actions */ |
| 4832 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4833 | if (actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4834 | actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4835 | COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4836 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4837 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4838 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup); |
| 4839 | LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index; |
| 4840 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_COUNT(context_node_fake.actions) * sizeof **actions); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4841 | } |
| 4842 | } |
| 4843 | |
| 4844 | /* compile notifications */ |
| 4845 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4846 | if (notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4847 | notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4848 | COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4849 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4850 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4851 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup); |
| 4852 | LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index; |
| 4853 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_COUNT(context_node_fake.notifs) * sizeof **notifs); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4854 | } |
| 4855 | } |
| 4856 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4857 | /* sort and apply augments */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4858 | ret = lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments); |
| 4859 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4860 | LY_ARRAY_FOR(augments, u) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4861 | ret = lys_compile_augment(ctx, augments[u], (struct lysc_node *)&context_node_fake); |
| 4862 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4863 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4864 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4865 | /* reload previous context's mod_def */ |
| 4866 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4867 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4868 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4869 | /* apply refine */ |
| 4870 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4871 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4872 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4873 | ret = lysc_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node *)&context_node_fake, ctx->mod, |
| 4874 | 0, 0, (const struct lysc_node **)&node, &flags); |
| 4875 | LY_CHECK_GOTO(ret, cleanup); |
| 4876 | ret = ly_set_add(&refined, node, LY_SET_OPT_USEASLIST, NULL); |
| 4877 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4878 | |
| 4879 | /* default value */ |
| 4880 | if (rfn->dflts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4881 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4882 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4883 | "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] | 4884 | lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4885 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4886 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4887 | } |
| 4888 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4889 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4890 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4891 | lys_nodetype2str(node->nodetype)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4892 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4893 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4894 | } |
| 4895 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4896 | /* postpone default compilation when the tree is complete */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4897 | ret = lysc_incomplete_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0], ctx->mod_def); |
| 4898 | LY_CHECK_GOTO(ret, cleanup); |
| 4899 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4900 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4901 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4902 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4903 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4904 | "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] | 4905 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4906 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4907 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4908 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4909 | /* postpone default compilation when the tree is complete */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4910 | ret = lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts, ctx->mod_def); |
| 4911 | LY_CHECK_GOTO(ret, cleanup); |
| 4912 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4913 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4914 | } else if (node->nodetype == LYS_CHOICE) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4915 | if (((struct lysc_node_choice *)node)->dflt) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4916 | /* unset LYS_SET_DFLT from the current default case */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4917 | ((struct lysc_node_choice *)node)->dflt->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4918 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4919 | ret = lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice *)node); |
| 4920 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4921 | } |
| 4922 | } |
| 4923 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4924 | /* description */ |
| 4925 | if (rfn->dsc) { |
| 4926 | FREE_STRING(ctx->ctx, node->dsc); |
| 4927 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4928 | } |
| 4929 | |
| 4930 | /* reference */ |
| 4931 | if (rfn->ref) { |
| 4932 | FREE_STRING(ctx->ctx, node->ref); |
| 4933 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4934 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4935 | |
| 4936 | /* config */ |
| 4937 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4938 | if (!flags) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4939 | ret = lys_compile_change_config(ctx, node, rfn->flags, 0, 1); |
| 4940 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4941 | } else { |
| 4942 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4943 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4944 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4945 | } |
| 4946 | |
| 4947 | /* mandatory */ |
| 4948 | if (rfn->flags & LYS_MAND_MASK) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4949 | ret = lys_compile_change_mandatory(ctx, node, rfn->flags, 1); |
| 4950 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4951 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4952 | |
| 4953 | /* presence */ |
| 4954 | if (rfn->presence) { |
| 4955 | if (node->nodetype != LYS_CONTAINER) { |
| 4956 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4957 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4958 | lys_nodetype2str(node->nodetype)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4959 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4960 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4961 | } |
| 4962 | node->flags |= LYS_PRESENCE; |
| 4963 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4964 | |
| 4965 | /* must */ |
| 4966 | if (rfn->musts) { |
| 4967 | switch (node->nodetype) { |
| 4968 | case LYS_LEAF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4969 | 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] | 4970 | break; |
| 4971 | case LYS_LEAFLIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4972 | 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] | 4973 | break; |
| 4974 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4975 | 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] | 4976 | break; |
| 4977 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4978 | 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] | 4979 | break; |
| 4980 | case LYS_ANYXML: |
| 4981 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 4982 | 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] | 4983 | break; |
| 4984 | default: |
| 4985 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4986 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 4987 | lys_nodetype2str(node->nodetype)); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4988 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4989 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4990 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 4991 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 4992 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4993 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4994 | |
| 4995 | /* min/max-elements */ |
| 4996 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4997 | switch (node->nodetype) { |
| 4998 | case LYS_LEAFLIST: |
| 4999 | if (rfn->flags & LYS_SET_MAX) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5000 | ((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] | 5001 | } |
| 5002 | if (rfn->flags & LYS_SET_MIN) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5003 | ((struct lysc_node_leaflist *)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5004 | if (rfn->min) { |
| 5005 | node->flags |= LYS_MAND_TRUE; |
| 5006 | lys_compile_mandatory_parents(node->parent, 1); |
| 5007 | } else { |
| 5008 | node->flags &= ~LYS_MAND_TRUE; |
| 5009 | lys_compile_mandatory_parents(node->parent, 0); |
| 5010 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5011 | } |
| 5012 | break; |
| 5013 | case LYS_LIST: |
| 5014 | if (rfn->flags & LYS_SET_MAX) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5015 | ((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] | 5016 | } |
| 5017 | if (rfn->flags & LYS_SET_MIN) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5018 | ((struct lysc_node_list *)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5019 | if (rfn->min) { |
| 5020 | node->flags |= LYS_MAND_TRUE; |
| 5021 | lys_compile_mandatory_parents(node->parent, 1); |
| 5022 | } else { |
| 5023 | node->flags &= ~LYS_MAND_TRUE; |
| 5024 | lys_compile_mandatory_parents(node->parent, 0); |
| 5025 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5026 | } |
| 5027 | break; |
| 5028 | default: |
| 5029 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5030 | "Invalid refine of %s statement - %s cannot hold this statement.", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5031 | (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] | 5032 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5033 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5034 | } |
| 5035 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5036 | |
| 5037 | /* if-feature */ |
| 5038 | if (rfn->iffeatures) { |
| 5039 | /* 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] | 5040 | 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] | 5041 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5042 | |
| 5043 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5044 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5045 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5046 | /* 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] | 5047 | for (uint32_t i = 0; i < refined.count; ++i) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5048 | node = (struct lysc_node *)refined.objs[i]; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5049 | rfn = &uses_p->refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5050 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5051 | |
| 5052 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5053 | if ((node->flags & LYS_MAND_TRUE) && |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5054 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) || |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5055 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5056 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5057 | "Invalid refine of default - the node is mandatory."); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5058 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5059 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5060 | } |
| 5061 | |
| 5062 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5063 | if (node->nodetype == LYS_LIST) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5064 | min = ((struct lysc_node_list *)node)->min; |
| 5065 | max = ((struct lysc_node_list *)node)->max; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5066 | } else { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5067 | min = ((struct lysc_node_leaflist *)node)->min; |
| 5068 | max = ((struct lysc_node_leaflist *)node)->max; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5069 | } |
| 5070 | if (min > max) { |
| 5071 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5072 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5073 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5074 | ret = LY_EVALID; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5075 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5076 | } |
| 5077 | } |
| 5078 | } |
| 5079 | |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5080 | if (first_p) { |
| 5081 | *first_p = context_node_fake.child; |
| 5082 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5083 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5084 | |
| 5085 | cleanup: |
| 5086 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5087 | if (context_node_fake.child) { |
| 5088 | context_node_fake.child->prev = child; |
| 5089 | } |
| 5090 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5091 | child->parent = parent; |
| 5092 | } |
| 5093 | |
| 5094 | if (uses_p->augments || uses_p->refines) { |
| 5095 | /* 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] | 5096 | if (context_node_fake.actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5097 | 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] | 5098 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5099 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5100 | if (context_node_fake.notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5101 | 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] | 5102 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5103 | } |
| 5104 | } |
| 5105 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5106 | /* reload previous context's mod_def */ |
| 5107 | ctx->mod_def = mod_old; |
| 5108 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5109 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5110 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5111 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5112 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5113 | |
| 5114 | return ret; |
| 5115 | } |
| 5116 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5117 | static int |
| 5118 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5119 | { |
| 5120 | struct lysp_node *iter; |
| 5121 | int len = 0; |
| 5122 | |
| 5123 | *path = NULL; |
| 5124 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5125 | char *s = *path; |
| 5126 | char *id; |
| 5127 | |
| 5128 | switch (iter->nodetype) { |
| 5129 | case LYS_USES: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5130 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5131 | break; |
| 5132 | case LYS_GROUPING: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5133 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5134 | break; |
| 5135 | case LYS_AUGMENT: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5136 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5137 | break; |
| 5138 | default: |
| 5139 | id = strdup(iter->name); |
| 5140 | break; |
| 5141 | } |
| 5142 | |
| 5143 | if (!iter->parent) { |
| 5144 | /* print prefix */ |
| 5145 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5146 | } else { |
| 5147 | /* prefix is the same as in parent */ |
| 5148 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5149 | } |
| 5150 | free(s); |
| 5151 | free(id); |
| 5152 | } |
| 5153 | |
| 5154 | if (len < 0) { |
| 5155 | free(*path); |
| 5156 | *path = NULL; |
| 5157 | } else if (len == 0) { |
| 5158 | *path = strdup("/"); |
| 5159 | len = 1; |
| 5160 | } |
| 5161 | return len; |
| 5162 | } |
| 5163 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5164 | /** |
| 5165 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5166 | * |
| 5167 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5168 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5169 | */ |
| 5170 | static LY_ERR |
| 5171 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5172 | { |
| 5173 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5174 | char *path; |
| 5175 | int len; |
| 5176 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5177 | struct lysp_node_uses fake_uses = { |
| 5178 | .parent = node_p, |
| 5179 | .nodetype = LYS_USES, |
| 5180 | .flags = 0, .next = NULL, |
| 5181 | .name = grp->name, |
| 5182 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5183 | .refines = NULL, .augments = NULL |
| 5184 | }; |
| 5185 | struct lysc_node_container fake_container = { |
| 5186 | .nodetype = LYS_CONTAINER, |
| 5187 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5188 | .module = ctx->mod, |
| 5189 | .sp = NULL, .parent = NULL, .next = NULL, |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5190 | .prev = (struct lysc_node *)&fake_container, |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5191 | .name = "fake", |
| 5192 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5193 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5194 | }; |
| 5195 | |
| 5196 | if (grp->parent) { |
| 5197 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5198 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5199 | |
| 5200 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5201 | if (len < 0) { |
| 5202 | LOGMEM(ctx->ctx); |
| 5203 | return LY_EMEM; |
| 5204 | } |
| 5205 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5206 | ctx->path_len = (uint32_t)len; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5207 | free(path); |
| 5208 | |
| 5209 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5210 | lysc_update_path(ctx, NULL, grp->name); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5211 | 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] | 5212 | lysc_update_path(ctx, NULL, NULL); |
| 5213 | lysc_update_path(ctx, NULL, NULL); |
| 5214 | |
| 5215 | ctx->path_len = 1; |
| 5216 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5217 | |
| 5218 | /* cleanup */ |
| 5219 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5220 | |
| 5221 | return ret; |
| 5222 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5223 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5224 | /** |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5225 | * @brief Set config flags for a node. |
| 5226 | * |
| 5227 | * @param[in] ctx Compile context. |
| 5228 | * @param[in] node Compiled node config to set. |
| 5229 | * @param[in] parent Parent of @p node. |
| 5230 | * @return LY_ERR value. |
| 5231 | */ |
| 5232 | static LY_ERR |
| 5233 | lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent) |
| 5234 | { |
| 5235 | if (node->nodetype == LYS_CASE) { |
| 5236 | /* case never has any config */ |
| 5237 | assert(!(node->flags & LYS_CONFIG_MASK)); |
| 5238 | return LY_SUCCESS; |
| 5239 | } |
| 5240 | |
| 5241 | /* adjust parent to always get the ancestor with config */ |
| 5242 | if (parent && (parent->nodetype == LYS_CASE)) { |
| 5243 | parent = parent->parent; |
| 5244 | assert(parent); |
| 5245 | } |
| 5246 | |
| 5247 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
| 5248 | /* ignore config statements inside RPC/action data */ |
| 5249 | node->flags &= ~LYS_CONFIG_MASK; |
| 5250 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5251 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
| 5252 | /* ignore config statements inside Notification data */ |
| 5253 | node->flags &= ~LYS_CONFIG_MASK; |
| 5254 | node->flags |= LYS_CONFIG_R; |
| 5255 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
| 5256 | /* config not explicitely set, inherit it from parent */ |
| 5257 | if (parent) { |
| 5258 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5259 | } else { |
| 5260 | /* default is config true */ |
| 5261 | node->flags |= LYS_CONFIG_W; |
| 5262 | } |
| 5263 | } else { |
| 5264 | /* config set explicitely */ |
| 5265 | node->flags |= LYS_SET_CONFIG; |
| 5266 | } |
| 5267 | |
| 5268 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5269 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5270 | "Configuration node cannot be child of any state data node."); |
| 5271 | return LY_EVALID; |
| 5272 | } |
| 5273 | |
| 5274 | return LY_SUCCESS; |
| 5275 | } |
| 5276 | |
| 5277 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5278 | * @brief Compile parsed schema node information. |
| 5279 | * @param[in] ctx Compile context |
| 5280 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5281 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5282 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5283 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5284 | * @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). |
| 5285 | * 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] | 5286 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5287 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5288 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5289 | 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] | 5290 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5291 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5292 | struct lysc_node *node = NULL; |
| 5293 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5294 | struct lysc_when **when; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5295 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5296 | 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] | 5297 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5298 | if (node_p->nodetype != LYS_USES) { |
| 5299 | lysc_update_path(ctx, parent, node_p->name); |
| 5300 | } else { |
| 5301 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5302 | lysc_update_path(ctx, NULL, node_p->name); |
| 5303 | } |
| 5304 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5305 | switch (node_p->nodetype) { |
| 5306 | case LYS_CONTAINER: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5307 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5308 | node_compile_spec = lys_compile_node_container; |
| 5309 | break; |
| 5310 | case LYS_LEAF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5311 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5312 | node_compile_spec = lys_compile_node_leaf; |
| 5313 | break; |
| 5314 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5315 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5316 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5317 | break; |
| 5318 | case LYS_LEAFLIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5319 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5320 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5321 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5322 | case LYS_CHOICE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5323 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5324 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5325 | break; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5326 | case LYS_CASE: |
| 5327 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case)); |
| 5328 | node_compile_spec = lys_compile_node_case; |
| 5329 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5330 | case LYS_ANYXML: |
| 5331 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5332 | node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5333 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5334 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5335 | case LYS_USES: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5336 | ret = lys_compile_uses(ctx, (struct lysp_node_uses *)node_p, cs ? (struct lysc_node *)cs : parent, &node); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5337 | lysc_update_path(ctx, NULL, NULL); |
| 5338 | lysc_update_path(ctx, NULL, NULL); |
| 5339 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5340 | default: |
| 5341 | LOGINT(ctx->ctx); |
| 5342 | return LY_EINT; |
| 5343 | } |
| 5344 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5345 | node->nodetype = node_p->nodetype; |
| 5346 | node->module = ctx->mod; |
| 5347 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5348 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5349 | |
| 5350 | /* config */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5351 | ret = lys_compile_config(ctx, node, parent); |
| 5352 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5353 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5354 | /* list ordering */ |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5355 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5356 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5357 | 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] | 5358 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5359 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5360 | node->flags &= ~LYS_ORDBY_MASK; |
| 5361 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5362 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5363 | /* default ordering is system */ |
| 5364 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5365 | } |
| 5366 | } |
| 5367 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5368 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5369 | * 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] | 5370 | 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] | 5371 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5372 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5373 | node->sp = node_p; |
| 5374 | } |
| 5375 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5376 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5377 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5378 | if (node_p->when) { |
| 5379 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5380 | 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] | 5381 | |
| 5382 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5383 | /* do not check "when" semantics in a grouping */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 5384 | ret = ly_set_add(&ctx->xpath, node, 0, NULL); |
| 5385 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5386 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5387 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5388 | 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] | 5389 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5390 | /* insert into parent's children/compiled module (we can no longer free the node separately on error) */ |
| 5391 | LY_CHECK_RET(lys_compile_node_connect(ctx, parent, node)); |
| 5392 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5393 | /* nodetype-specific part */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5394 | LY_CHECK_RET(node_compile_spec(ctx, node_p, node)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5395 | |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5396 | /* final compilation tasks that require the node to be connected */ |
| 5397 | 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] | 5398 | if (node->flags & LYS_MAND_TRUE) { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5399 | /* inherit LYS_MAND_TRUE in parent containers */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5400 | lys_compile_mandatory_parents(parent, 1); |
| 5401 | } |
| 5402 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5403 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5404 | return LY_SUCCESS; |
| 5405 | |
| 5406 | error: |
| 5407 | lysc_node_free(ctx->ctx, node); |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5408 | done: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5409 | return ret; |
| 5410 | } |
| 5411 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5412 | static void |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5413 | lysc_node_unlink(struct lysc_node *node) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5414 | { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5415 | struct lysc_node *parent, *child; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5416 | struct lysc_module *modc = node->module->compiled; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5417 | |
| 5418 | parent = node->parent; |
| 5419 | |
| 5420 | /* parent's first child */ |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5421 | if (parent && lysc_node_children(parent, node->flags) == node) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5422 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5423 | } else if (modc->data == node) { |
| 5424 | modc->data = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5425 | } |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5426 | |
| 5427 | /* special choice case unlinking */ |
| 5428 | if (parent && parent->nodetype == LYS_CHOICE) { |
| 5429 | if (((struct lysc_node_choice *)parent)->dflt == (struct lysc_node_case *)node) { |
| 5430 | /* default case removed */ |
| 5431 | ((struct lysc_node_choice *)parent)->dflt = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5432 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5433 | } |
| 5434 | |
| 5435 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5436 | if (node->prev->next) { |
| 5437 | node->prev->next = node->next; |
| 5438 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5439 | if (node->next) { |
| 5440 | node->next->prev = node->prev; |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 5441 | } else { |
| 5442 | /* last child */ |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5443 | if (parent) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5444 | child = (struct lysc_node *)lysc_node_children(parent, node->flags); |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5445 | } else { |
| 5446 | child = modc->data; |
| 5447 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5448 | if (child) { |
| 5449 | child->prev = node->prev; |
| 5450 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5451 | } |
| 5452 | } |
| 5453 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 5454 | struct lysc_deviation { |
| 5455 | const char *nodeid; |
| 5456 | struct lysc_node *target; /* target node of the deviation */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5457 | 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] | 5458 | uint16_t flags; /* target's flags from lysc_resolve_schema_nodeid() */ |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 5459 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5460 | }; |
| 5461 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5462 | /* MACROS for deviates checking */ |
| 5463 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5464 | if (!(target->nodetype & (NODETYPES))) { \ |
| 5465 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\ |
| 5466 | goto cleanup; \ |
| 5467 | } |
| 5468 | |
| 5469 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5470 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
| 5471 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
| 5472 | lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
| 5473 | goto cleanup; \ |
| 5474 | } |
| 5475 | |
| 5476 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5477 | if (!((TYPE)target)->MEMBER || COND) { \ |
| 5478 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
| 5479 | goto cleanup; \ |
| 5480 | } |
| 5481 | |
| 5482 | /** |
| 5483 | * @brief Apply deviate add. |
| 5484 | * |
| 5485 | * @param[in] ctx Compile context. |
| 5486 | * @param[in] target Deviation target. |
| 5487 | * @param[in] dev_flags Internal deviation flags. |
| 5488 | * @param[in] d Deviate add to apply. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5489 | * @return LY_ERR value. |
| 5490 | */ |
| 5491 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5492 | 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] | 5493 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5494 | LY_ERR ret = LY_EVALID; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5495 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 5496 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5497 | LY_ARRAY_COUNT_TYPE x; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5498 | |
| 5499 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5500 | if (((TYPE)target)->MEMBER COND) { \ |
| 5501 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5502 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5503 | PROPERTY, ((TYPE)target)->MEMBER); \ |
| 5504 | goto cleanup; \ |
| 5505 | } |
| 5506 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5507 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5508 | if (((TYPE)target)->MEMBER && (COND)) { \ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5509 | uint8_t dynamic_ = 0; const char *val_; \ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5510 | 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] | 5511 | ctx->mod_def, &dynamic_); \ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5512 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5513 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5514 | if (dynamic_) {free((void*)val_);} \ |
| 5515 | goto cleanup; \ |
| 5516 | } |
| 5517 | |
| 5518 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
| 5519 | if (((TYPE)target)->MEMBER && (COND)) { \ |
| 5520 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5521 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5522 | PROPERTY, ((TYPE)target)->VALUEMEMBER); \ |
| 5523 | goto cleanup; \ |
| 5524 | } |
| 5525 | |
| 5526 | /* [units-stmt] */ |
| 5527 | if (d->units) { |
| 5528 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5529 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units); |
| 5530 | |
| 5531 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units); |
| 5532 | DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units); |
| 5533 | } |
| 5534 | |
| 5535 | /* *must-stmt */ |
| 5536 | if (d->musts) { |
| 5537 | switch (target->nodetype) { |
| 5538 | case LYS_CONTAINER: |
| 5539 | case LYS_LIST: |
| 5540 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts, |
| 5541 | x, lys_compile_must, ret, cleanup); |
| 5542 | break; |
| 5543 | case LYS_LEAF: |
| 5544 | case LYS_LEAFLIST: |
| 5545 | case LYS_ANYDATA: |
| 5546 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts, |
| 5547 | x, lys_compile_must, ret, cleanup); |
| 5548 | break; |
| 5549 | case LYS_NOTIF: |
| 5550 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts, |
| 5551 | x, lys_compile_must, ret, cleanup); |
| 5552 | break; |
| 5553 | case LYS_RPC: |
| 5554 | case LYS_ACTION: |
| 5555 | if (dev_flags & LYSC_OPT_RPC_INPUT) { |
| 5556 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts, |
| 5557 | x, lys_compile_must, ret, cleanup); |
| 5558 | break; |
| 5559 | } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) { |
| 5560 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts, |
| 5561 | x, lys_compile_must, ret, cleanup); |
| 5562 | break; |
| 5563 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5564 | /* fall through */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5565 | default: |
| 5566 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5567 | lys_nodetype2str(target->nodetype), "add", "must"); |
| 5568 | goto cleanup; |
| 5569 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 5570 | ret = ly_set_add(&ctx->xpath, target, 0, NULL); |
| 5571 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5572 | } |
| 5573 | |
| 5574 | /* *unique-stmt */ |
| 5575 | if (d->uniques) { |
| 5576 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5577 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup); |
| 5578 | } |
| 5579 | |
| 5580 | /* *default-stmt */ |
| 5581 | if (d->dflts) { |
| 5582 | switch (target->nodetype) { |
| 5583 | case LYS_LEAF: |
| 5584 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5585 | 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] | 5586 | if (leaf->dflt) { |
| 5587 | /* first, remove the default value taken from the type */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5588 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5589 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5590 | free(leaf->dflt); |
| 5591 | leaf->dflt = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5592 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5593 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5594 | /* store the default value in unres */ |
Michal Vasko | 31a82d5 | 2020-08-21 08:11:48 +0200 | [diff] [blame] | 5595 | 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] | 5596 | target->flags |= LYS_SET_DFLT; |
| 5597 | break; |
| 5598 | case LYS_LEAFLIST: |
| 5599 | if (llist->dflts && !(target->flags & LYS_SET_DFLT)) { |
| 5600 | /* first, remove the default value taken from the type */ |
| 5601 | LY_ARRAY_FOR(llist->dflts, x) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5602 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5603 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5604 | free(llist->dflts[x]); |
| 5605 | } |
| 5606 | LY_ARRAY_FREE(llist->dflts); |
| 5607 | llist->dflts = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5608 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5609 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5610 | /* store the default values in unres */ |
| 5611 | 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] | 5612 | target->flags |= LYS_SET_DFLT; |
| 5613 | break; |
| 5614 | case LYS_CHOICE: |
| 5615 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
| 5616 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name); |
| 5617 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5618 | * to allow making the default case even the augmented case from the deviating module */ |
| 5619 | if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) { |
| 5620 | goto cleanup; |
| 5621 | } |
| 5622 | break; |
| 5623 | default: |
| 5624 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5625 | lys_nodetype2str(target->nodetype), "add", "default"); |
| 5626 | goto cleanup; |
| 5627 | } |
| 5628 | } |
| 5629 | |
| 5630 | /* [config-stmt] */ |
| 5631 | if (d->flags & LYS_CONFIG_MASK) { |
| 5632 | if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 5633 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5634 | lys_nodetype2str(target->nodetype), "add", "config"); |
| 5635 | goto cleanup; |
| 5636 | } |
| 5637 | if (dev_flags) { |
| 5638 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 5639 | dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
| 5640 | } |
| 5641 | if (target->flags & LYS_SET_CONFIG) { |
| 5642 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5643 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5644 | target->flags & LYS_CONFIG_W ? "true" : "false"); |
| 5645 | goto cleanup; |
| 5646 | } |
| 5647 | LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup); |
| 5648 | } |
| 5649 | |
| 5650 | /* [mandatory-stmt] */ |
| 5651 | if (d->flags & LYS_MAND_MASK) { |
| 5652 | if (target->flags & LYS_MAND_MASK) { |
| 5653 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5654 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5655 | target->flags & LYS_MAND_TRUE ? "true" : "false"); |
| 5656 | goto cleanup; |
| 5657 | } |
| 5658 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup); |
| 5659 | } |
| 5660 | |
| 5661 | /* [min-elements-stmt] */ |
| 5662 | if (d->flags & LYS_SET_MIN) { |
| 5663 | if (target->nodetype == LYS_LEAFLIST) { |
| 5664 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements"); |
| 5665 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5666 | ((struct lysc_node_leaflist *)target)->min = d->min; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5667 | } else if (target->nodetype == LYS_LIST) { |
| 5668 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements"); |
| 5669 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5670 | ((struct lysc_node_list *)target)->min = d->min; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5671 | } else { |
| 5672 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5673 | lys_nodetype2str(target->nodetype), "add", "min-elements"); |
| 5674 | goto cleanup; |
| 5675 | } |
| 5676 | if (d->min) { |
| 5677 | target->flags |= LYS_MAND_TRUE; |
| 5678 | } |
| 5679 | } |
| 5680 | |
| 5681 | /* [max-elements-stmt] */ |
| 5682 | if (d->flags & LYS_SET_MAX) { |
| 5683 | if (target->nodetype == LYS_LEAFLIST) { |
| 5684 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements"); |
| 5685 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5686 | ((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] | 5687 | } else if (target->nodetype == LYS_LIST) { |
| 5688 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements"); |
| 5689 | /* change value */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5690 | ((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] | 5691 | } else { |
| 5692 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5693 | lys_nodetype2str(target->nodetype), "add", "max-elements"); |
| 5694 | goto cleanup; |
| 5695 | } |
| 5696 | } |
| 5697 | |
| 5698 | ret = LY_SUCCESS; |
| 5699 | |
| 5700 | cleanup: |
| 5701 | return ret; |
| 5702 | } |
| 5703 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5704 | static LY_ERR |
| 5705 | lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt) |
| 5706 | { |
| 5707 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5708 | uint8_t dyn = 0; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5709 | const char *orig_dflt; |
| 5710 | uint32_t i; |
| 5711 | |
| 5712 | if (target->module != ctx->mod) { |
| 5713 | /* foreign deviation */ |
| 5714 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt); |
| 5715 | |
| 5716 | /* check that the value matches */ |
| 5717 | orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn); |
| 5718 | if (strcmp(orig_dflt, dflt)) { |
| 5719 | goto error; |
| 5720 | } |
| 5721 | if (dyn) { |
| 5722 | free((char *)orig_dflt); |
| 5723 | } |
| 5724 | |
| 5725 | /* remove the default specification */ |
| 5726 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5727 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5728 | free(leaf->dflt); |
| 5729 | leaf->dflt = NULL; |
| 5730 | } else { |
| 5731 | /* local deviation */ |
| 5732 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt); |
| 5733 | |
| 5734 | /* find the incomplete default */ |
| 5735 | orig_dflt = NULL; |
| 5736 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 5737 | if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) { |
| 5738 | orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt; |
| 5739 | break; |
| 5740 | } |
| 5741 | } |
| 5742 | LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT); |
| 5743 | |
| 5744 | /* check that the value matches */ |
| 5745 | if (strcmp(orig_dflt, dflt)) { |
| 5746 | goto error; |
| 5747 | } |
| 5748 | |
| 5749 | /* update the list of incomplete default values */ |
| 5750 | lysc_incomplete_dflt_remove(ctx, target); |
| 5751 | } |
| 5752 | |
| 5753 | target->flags &= ~LYS_SET_DFLT; |
| 5754 | return LY_SUCCESS; |
| 5755 | |
| 5756 | error: |
| 5757 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5758 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 5759 | dflt, orig_dflt); |
| 5760 | if (dyn) { |
| 5761 | free((char *)orig_dflt); |
| 5762 | } |
| 5763 | cleanup: |
| 5764 | return LY_EVALID; |
| 5765 | } |
| 5766 | |
| 5767 | static LY_ERR |
| 5768 | lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts) |
| 5769 | { |
| 5770 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5771 | uint8_t dyn = 0, found; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5772 | const char *orig_dflt, **orig_dflts; |
| 5773 | uint32_t i; |
| 5774 | LY_ARRAY_COUNT_TYPE x, y; |
| 5775 | |
| 5776 | if (target->module != ctx->mod) { |
| 5777 | /* foreign deviation */ |
| 5778 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]); |
| 5779 | LY_ARRAY_FOR(dflts, x) { |
| 5780 | found = 0; |
| 5781 | LY_ARRAY_FOR(llist->dflts, y) { |
| 5782 | orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn); |
| 5783 | if (!strcmp(orig_dflt, dflts[x])) { |
| 5784 | if (dyn) { |
| 5785 | free((char *)orig_dflt); |
| 5786 | } |
| 5787 | found = 1; |
| 5788 | break; |
| 5789 | } |
| 5790 | if (dyn) { |
| 5791 | free((char *)orig_dflt); |
| 5792 | } |
| 5793 | } |
| 5794 | if (!found) { |
| 5795 | goto error; |
| 5796 | } |
| 5797 | |
| 5798 | /* update compiled default values */ |
| 5799 | LY_ARRAY_DECREMENT(llist->dflts); |
| 5800 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 5801 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 5802 | free(llist->dflts[y]); |
| 5803 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts)); |
| 5804 | } |
| 5805 | if (!LY_ARRAY_COUNT(llist->dflts)) { |
| 5806 | LY_ARRAY_FREE(llist->dflts); |
| 5807 | llist->dflts = NULL; |
| 5808 | llist->flags &= ~LYS_SET_DFLT; |
| 5809 | } |
| 5810 | } else { |
| 5811 | /* local deviation */ |
| 5812 | orig_dflt = NULL; |
| 5813 | orig_dflts = NULL; |
| 5814 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 5815 | if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) { |
| 5816 | orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt; |
| 5817 | orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts; |
| 5818 | break; |
| 5819 | } |
| 5820 | } |
| 5821 | LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT); |
| 5822 | |
| 5823 | if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) { |
| 5824 | /* TODO it is not currently possible to remove just one default value from incomplete defaults array */ |
| 5825 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5826 | "Local deviation deleting leaf-list defaults is not supported."); |
| 5827 | return LY_EVALID; |
| 5828 | } |
| 5829 | |
| 5830 | LY_ARRAY_FOR(dflts, x) { |
| 5831 | found = 0; |
| 5832 | if (orig_dflts) { |
| 5833 | LY_ARRAY_FOR(orig_dflts, y) { |
| 5834 | if (!strcmp(orig_dflts[y], dflts[x])) { |
| 5835 | found = 1; |
| 5836 | break; |
| 5837 | } |
| 5838 | } |
| 5839 | } else if (!strcmp(orig_dflt, dflts[x])) { |
| 5840 | found = 1; |
| 5841 | } |
| 5842 | if (!found) { |
| 5843 | goto error; |
| 5844 | } |
| 5845 | |
| 5846 | /* update the list of incomplete default values */ |
| 5847 | lysc_incomplete_dflt_remove(ctx, target); |
| 5848 | } |
| 5849 | |
| 5850 | llist->flags &= ~LYS_SET_DFLT; |
| 5851 | } |
| 5852 | |
| 5853 | return LY_SUCCESS; |
| 5854 | |
| 5855 | error: |
| 5856 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 5857 | "which does not match any of the target's property values.", dflts[x]); |
| 5858 | cleanup: |
| 5859 | return LY_EVALID; |
| 5860 | } |
| 5861 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5862 | /** |
| 5863 | * @brief Apply deviate delete. |
| 5864 | * |
| 5865 | * @param[in] ctx Compile context. |
| 5866 | * @param[in] target Deviation target. |
| 5867 | * @param[in] dev_flags Internal deviation flags. |
| 5868 | * @param[in] d Deviate delete to apply. |
| 5869 | * @return LY_ERR value. |
| 5870 | */ |
| 5871 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5872 | 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] | 5873 | { |
| 5874 | LY_ERR ret = LY_EVALID; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5875 | struct lysc_node_list *list = (struct lysc_node_list *)target; |
| 5876 | LY_ARRAY_COUNT_TYPE x, y, z; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5877 | size_t prefix_len, name_len; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5878 | const char *prefix, *name, *nodeid; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5879 | struct lys_module *mod; |
| 5880 | |
| 5881 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5882 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \ |
| 5883 | LY_ARRAY_FOR(d->ARRAY_DEV, x) { \ |
| 5884 | LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \ |
| 5885 | if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \ |
| 5886 | } \ |
| 5887 | if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \ |
| 5888 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5889 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5890 | PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \ |
| 5891 | goto cleanup; \ |
| 5892 | } \ |
| 5893 | LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \ |
| 5894 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \ |
| 5895 | memmove(&((TYPE)target)->ARRAY_TRG[y], \ |
| 5896 | &((TYPE)target)->ARRAY_TRG[y + 1], \ |
| 5897 | (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \ |
| 5898 | } \ |
| 5899 | if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \ |
| 5900 | LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \ |
| 5901 | ((TYPE)target)->ARRAY_TRG = NULL; \ |
| 5902 | } |
| 5903 | |
| 5904 | /* [units-stmt] */ |
| 5905 | if (d->units) { |
| 5906 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5907 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, 0, units, "deleting", "units", d->units); |
| 5908 | if (strcmp(((struct lysc_node_leaf *)target)->units, d->units)) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5909 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5910 | "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] | 5911 | d->units, ((struct lysc_node_leaf *)target)->units); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5912 | goto cleanup; |
| 5913 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5914 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf *)target)->units); |
| 5915 | ((struct lysc_node_leaf *)target)->units = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5916 | } |
| 5917 | |
| 5918 | /* *must-stmt */ |
| 5919 | if (d->musts) { |
| 5920 | switch (target->nodetype) { |
| 5921 | case LYS_CONTAINER: |
| 5922 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5923 | 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] | 5924 | break; |
| 5925 | case LYS_LEAF: |
| 5926 | case LYS_LEAFLIST: |
| 5927 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5928 | 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] | 5929 | break; |
| 5930 | case LYS_NOTIF: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5931 | 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] | 5932 | break; |
| 5933 | case LYS_RPC: |
| 5934 | case LYS_ACTION: |
| 5935 | if (dev_flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5936 | 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] | 5937 | break; |
| 5938 | } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 5939 | 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] | 5940 | break; |
| 5941 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5942 | /* fall through */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5943 | default: |
| 5944 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5945 | lys_nodetype2str(target->nodetype), "delete", "must"); |
| 5946 | goto cleanup; |
| 5947 | } |
| 5948 | } |
| 5949 | |
| 5950 | /* *unique-stmt */ |
| 5951 | if (d->uniques) { |
| 5952 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 5953 | LY_ARRAY_FOR(d->uniques, x) { |
| 5954 | LY_ARRAY_FOR(list->uniques, z) { |
| 5955 | for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 5956 | nodeid = strpbrk(name, " \t\n"); |
| 5957 | if (nodeid) { |
| 5958 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
| 5959 | break; |
| 5960 | } |
| 5961 | while (isspace(*nodeid)) { |
| 5962 | ++nodeid; |
| 5963 | } |
| 5964 | } else { |
| 5965 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 5966 | break; |
| 5967 | } |
| 5968 | } |
| 5969 | } |
| 5970 | if (!name) { |
| 5971 | /* complete match - remove the unique */ |
| 5972 | LY_ARRAY_DECREMENT(list->uniques); |
| 5973 | LY_ARRAY_FREE(list->uniques[z]); |
| 5974 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques)); |
| 5975 | --z; |
| 5976 | break; |
| 5977 | } |
| 5978 | } |
| 5979 | if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) { |
| 5980 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5981 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 5982 | d->uniques[x]); |
| 5983 | goto cleanup; |
| 5984 | } |
| 5985 | } |
| 5986 | if (!LY_ARRAY_COUNT(list->uniques)) { |
| 5987 | LY_ARRAY_FREE(list->uniques); |
| 5988 | list->uniques = NULL; |
| 5989 | } |
| 5990 | } |
| 5991 | |
| 5992 | /* *default-stmt */ |
| 5993 | if (d->dflts) { |
| 5994 | switch (target->nodetype) { |
| 5995 | case LYS_LEAF: |
| 5996 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5997 | 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] | 5998 | break; |
| 5999 | case LYS_LEAFLIST: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6000 | 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] | 6001 | break; |
| 6002 | case LYS_CHOICE: |
| 6003 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6004 | 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] | 6005 | nodeid = d->dflts[0]; |
| 6006 | LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup); |
| 6007 | if (prefix) { |
| 6008 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6009 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6010 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6011 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6012 | "The prefix does not match any imported module of the deviation module.", d->dflts[0]); |
| 6013 | goto cleanup; |
| 6014 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6015 | if (mod != ((struct lysc_node_choice *)target)->dflt->module) { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6016 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6017 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6018 | "The prefix does not match the default case's module.", d->dflts[0]); |
| 6019 | goto cleanup; |
| 6020 | } |
| 6021 | } |
| 6022 | /* else { |
| 6023 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6024 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6025 | * the module of the default case, just matching its name */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6026 | if (strcmp(name, ((struct lysc_node_choice *)target)->dflt->name)) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6027 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6028 | "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] | 6029 | d->dflts[0], ((struct lysc_node_choice *)target)->dflt->name); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6030 | goto cleanup; |
| 6031 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6032 | ((struct lysc_node_choice *)target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6033 | ((struct lysc_node_choice *)target)->dflt = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6034 | break; |
| 6035 | default: |
| 6036 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6037 | lys_nodetype2str(target->nodetype), "delete", "default"); |
| 6038 | goto cleanup; |
| 6039 | } |
| 6040 | } |
| 6041 | |
| 6042 | ret = LY_SUCCESS; |
| 6043 | |
| 6044 | cleanup: |
| 6045 | return ret; |
| 6046 | } |
| 6047 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6048 | static LY_ERR |
| 6049 | lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target) |
| 6050 | { |
| 6051 | LY_ERR ret; |
| 6052 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 6053 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 6054 | struct ly_err_item *err = NULL; |
| 6055 | LY_ARRAY_COUNT_TYPE x; |
| 6056 | const char *dflt; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6057 | uint8_t dyn; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6058 | |
| 6059 | if (target->module != ctx->mod) { |
| 6060 | /* foreign deviation */ |
| 6061 | if (target->nodetype == LYS_LEAF) { |
| 6062 | dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn); |
| 6063 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6064 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6065 | |
| 6066 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 6067 | LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err); |
| 6068 | if (dyn) { |
| 6069 | free((char *)dflt); |
| 6070 | } |
| 6071 | if (err) { |
| 6072 | ly_err_print(err); |
| 6073 | ctx->path[0] = '\0'; |
| 6074 | lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6075 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6076 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6077 | ly_err_free(err); |
| 6078 | } |
| 6079 | LY_CHECK_RET(ret); |
| 6080 | |
| 6081 | ++leaf->dflt->realtype->refcount; |
| 6082 | } else { /* LY_LEAFLIST */ |
| 6083 | LY_ARRAY_FOR(llist->dflts, x) { |
| 6084 | dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn); |
| 6085 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6086 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6087 | |
| 6088 | ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 6089 | LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err); |
| 6090 | if (dyn) { |
| 6091 | free((char *)dflt); |
| 6092 | } |
| 6093 | if (err) { |
| 6094 | ly_err_print(err); |
| 6095 | ctx->path[0] = '\0'; |
| 6096 | lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6097 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6098 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6099 | ly_err_free(err); |
| 6100 | } |
| 6101 | LY_CHECK_RET(ret); |
| 6102 | |
| 6103 | ++llist->dflts[x]->realtype->refcount; |
| 6104 | } |
| 6105 | } |
| 6106 | } else { |
| 6107 | /* local deviation */ |
| 6108 | |
| 6109 | /* these default were not compiled yet, so they will use the new type automatically */ |
| 6110 | } |
| 6111 | |
| 6112 | return LY_SUCCESS; |
| 6113 | } |
| 6114 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6115 | /** |
| 6116 | * @brief Apply deviate replace. |
| 6117 | * |
| 6118 | * @param[in] ctx Compile context. |
| 6119 | * @param[in] target Deviation target. |
| 6120 | * @param[in] d Deviate replace to apply. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6121 | * @return LY_ERR value. |
| 6122 | */ |
| 6123 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6124 | 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] | 6125 | { |
| 6126 | LY_ERR ret = LY_EVALID; |
| 6127 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 6128 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 6129 | LY_ARRAY_COUNT_TYPE x; |
| 6130 | |
| 6131 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 6132 | if (!(((TYPE)target)->MEMBER COND)) { \ |
| 6133 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 6134 | "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \ |
| 6135 | goto cleanup; \ |
| 6136 | } |
| 6137 | |
| 6138 | /* [type-stmt] */ |
| 6139 | if (d->type) { |
| 6140 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6141 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6142 | lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type); |
| 6143 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6144 | /* remove only default value inherited from the type */ |
| 6145 | if (!(target->flags & LYS_SET_DFLT)) { |
| 6146 | if (target->module != ctx->mod) { |
| 6147 | /* foreign deviation - the target has default from the previous type, remove it */ |
| 6148 | if (target->nodetype == LYS_LEAF) { |
| 6149 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6150 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6151 | free(leaf->dflt); |
| 6152 | leaf->dflt = NULL; |
| 6153 | } else { /* LYS_LEAFLIST */ |
| 6154 | LY_ARRAY_FOR(llist->dflts, x) { |
| 6155 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6156 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6157 | free(llist->dflts[x]); |
| 6158 | } |
| 6159 | LY_ARRAY_FREE(llist->dflts); |
| 6160 | llist->dflts = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6161 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6162 | } else { |
| 6163 | /* local deviation */ |
| 6164 | lysc_incomplete_dflt_remove(ctx, target); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6165 | } |
| 6166 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6167 | |
| 6168 | LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf)); |
| 6169 | |
| 6170 | if (target->flags & LYS_SET_DFLT) { |
| 6171 | /* the term default value(s) needs to be recompiled */ |
| 6172 | LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target)); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6173 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6174 | } |
| 6175 | |
| 6176 | /* [units-stmt] */ |
| 6177 | if (d->units) { |
| 6178 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6179 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS), |
| 6180 | units, "replacing", "units", d->units); |
| 6181 | |
| 6182 | lydict_remove(ctx->ctx, leaf->units); |
| 6183 | DUP_STRING(ctx->ctx, d->units, leaf->units); |
| 6184 | } |
| 6185 | |
| 6186 | /* [default-stmt] */ |
| 6187 | if (d->dflt) { |
| 6188 | switch (target->nodetype) { |
| 6189 | case LYS_LEAF: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6190 | if (target->module != ctx->mod) { |
| 6191 | /* foreign deviation */ |
| 6192 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing", |
| 6193 | "default", d->dflt); |
| 6194 | |
| 6195 | /* remove the default specification */ |
| 6196 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6197 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6198 | free(leaf->dflt); |
| 6199 | leaf->dflt = NULL; |
| 6200 | } else { |
| 6201 | /* local deviation */ |
| 6202 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing", |
| 6203 | "default", d->dflt); |
| 6204 | assert(!leaf->dflt); |
| 6205 | } |
| 6206 | |
| 6207 | /* store the new default value */ |
| 6208 | 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] | 6209 | break; |
| 6210 | case LYS_CHOICE: |
| 6211 | DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt); |
| 6212 | if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) { |
| 6213 | goto cleanup; |
| 6214 | } |
| 6215 | break; |
| 6216 | default: |
| 6217 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6218 | lys_nodetype2str(target->nodetype), "replace", "default"); |
| 6219 | goto cleanup; |
| 6220 | } |
| 6221 | } |
| 6222 | |
| 6223 | /* [config-stmt] */ |
| 6224 | if (d->flags & LYS_CONFIG_MASK) { |
| 6225 | if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 6226 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6227 | lys_nodetype2str(target->nodetype), "replace", "config"); |
| 6228 | goto cleanup; |
| 6229 | } |
| 6230 | if (!(target->flags & LYS_SET_CONFIG)) { |
| 6231 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6232 | "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6233 | goto cleanup; |
| 6234 | } |
| 6235 | LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup); |
| 6236 | } |
| 6237 | |
| 6238 | /* [mandatory-stmt] */ |
| 6239 | if (d->flags & LYS_MAND_MASK) { |
| 6240 | if (!(target->flags & LYS_MAND_MASK)) { |
| 6241 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6242 | "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6243 | goto cleanup; |
| 6244 | } |
| 6245 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup); |
| 6246 | } |
| 6247 | |
| 6248 | /* [min-elements-stmt] */ |
| 6249 | if (d->flags & LYS_SET_MIN) { |
| 6250 | if (target->nodetype == LYS_LEAFLIST) { |
| 6251 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements"); |
| 6252 | /* change value */ |
| 6253 | ((struct lysc_node_leaflist *)target)->min = d->min; |
| 6254 | } else if (target->nodetype == LYS_LIST) { |
| 6255 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements"); |
| 6256 | /* change value */ |
| 6257 | ((struct lysc_node_list *)target)->min = d->min; |
| 6258 | } else { |
| 6259 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6260 | lys_nodetype2str(target->nodetype), "replace", "min-elements"); |
| 6261 | goto cleanup; |
| 6262 | } |
| 6263 | if (d->min) { |
| 6264 | target->flags |= LYS_MAND_TRUE; |
| 6265 | } |
| 6266 | } |
| 6267 | |
| 6268 | /* [max-elements-stmt] */ |
| 6269 | if (d->flags & LYS_SET_MAX) { |
| 6270 | if (target->nodetype == LYS_LEAFLIST) { |
| 6271 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements"); |
| 6272 | /* change value */ |
| 6273 | ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1; |
| 6274 | } else if (target->nodetype == LYS_LIST) { |
| 6275 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements"); |
| 6276 | /* change value */ |
| 6277 | ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1; |
| 6278 | } else { |
| 6279 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6280 | lys_nodetype2str(target->nodetype), "replace", "max-elements"); |
| 6281 | goto cleanup; |
| 6282 | } |
| 6283 | } |
| 6284 | |
| 6285 | ret = LY_SUCCESS; |
| 6286 | |
| 6287 | cleanup: |
| 6288 | return ret; |
| 6289 | } |
| 6290 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6291 | /** |
| 6292 | * @brief Apply all deviations of one target node. |
| 6293 | * |
| 6294 | * @param[in] ctx Compile context. |
| 6295 | * @param[in] dev Deviation structure to apply. |
| 6296 | * @return LY_ERR value. |
| 6297 | */ |
| 6298 | static LY_ERR |
| 6299 | lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6300 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6301 | LY_ERR ret = LY_EVALID; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6302 | struct lysc_node *target = dev->target; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6303 | struct lysc_action *rpcs; |
| 6304 | struct lysc_notif *notifs; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6305 | struct lysp_deviate *d; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6306 | LY_ARRAY_COUNT_TYPE v, x; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6307 | uint32_t min, max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6308 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6309 | lysc_update_path(ctx, NULL, dev->nodeid); |
| 6310 | |
| 6311 | /* not-supported */ |
| 6312 | if (dev->not_supported) { |
| 6313 | if (LY_ARRAY_COUNT(dev->deviates) > 1) { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 6314 | 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] | 6315 | LY_ARRAY_COUNT(dev->deviates), dev->nodeid); |
| 6316 | } |
| 6317 | |
| 6318 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 6319 | if (target->parent) { \ |
| 6320 | ARRAY = (TYPE*)GETFUNC(target->parent); \ |
| 6321 | } else { \ |
| 6322 | ARRAY = target->module->compiled->ARRAY; \ |
| 6323 | } \ |
| 6324 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 6325 | if (&ARRAY[x] == (TYPE*)target) { break; } \ |
| 6326 | } \ |
| 6327 | if (x < LY_ARRAY_COUNT(ARRAY)) { \ |
| 6328 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 6329 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 6330 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 6331 | } |
| 6332 | |
| 6333 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 6334 | if (dev->flags & LYSC_OPT_RPC_INPUT) { |
| 6335 | /* remove RPC's/action's input */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6336 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input); |
| 6337 | memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input); |
| 6338 | FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free); |
| 6339 | ((struct lysc_action *)target)->input_exts = NULL; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6340 | } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6341 | /* remove RPC's/action's output */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6342 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output); |
| 6343 | memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output); |
| 6344 | FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free); |
| 6345 | ((struct lysc_action *)target)->output_exts = NULL; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6346 | } else { |
| 6347 | /* remove RPC/action */ |
| 6348 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
| 6349 | } |
| 6350 | } else if (target->nodetype == LYS_NOTIF) { |
| 6351 | /* remove Notification */ |
| 6352 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
| 6353 | } else { |
Michal Vasko | 20424b4 | 2020-08-31 12:29:38 +0200 | [diff] [blame] | 6354 | if (target->parent && (target->parent->nodetype == LYS_CASE) && (target->prev == target)) { |
| 6355 | /* remove the target node with its parent case node because it is the only node of the case */ |
| 6356 | lysc_node_unlink(target->parent); |
| 6357 | lysc_node_free(ctx->ctx, target->parent); |
| 6358 | } else { |
| 6359 | /* remove the target node */ |
| 6360 | lysc_node_unlink(target); |
| 6361 | lysc_node_free(ctx->ctx, target); |
| 6362 | } |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6363 | } |
| 6364 | |
| 6365 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 6366 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
| 6367 | return LY_SUCCESS; |
| 6368 | } |
| 6369 | |
| 6370 | /* list of deviates (not-supported is not present in the list) */ |
| 6371 | LY_ARRAY_FOR(dev->deviates, v) { |
| 6372 | d = dev->deviates[v]; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6373 | switch (d->mod) { |
| 6374 | case LYS_DEV_ADD: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6375 | 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] | 6376 | break; |
| 6377 | case LYS_DEV_DELETE: |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6378 | 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] | 6379 | break; |
| 6380 | case LYS_DEV_REPLACE: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6381 | 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] | 6382 | break; |
| 6383 | default: |
| 6384 | LOGINT(ctx->ctx); |
| 6385 | goto cleanup; |
| 6386 | } |
| 6387 | } |
| 6388 | |
| 6389 | /* final check when all deviations of a single target node are applied */ |
| 6390 | |
| 6391 | /* check min-max compatibility */ |
| 6392 | if (target->nodetype == LYS_LEAFLIST) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6393 | min = ((struct lysc_node_leaflist *)target)->min; |
| 6394 | max = ((struct lysc_node_leaflist *)target)->max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6395 | } else if (target->nodetype == LYS_LIST) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6396 | min = ((struct lysc_node_list *)target)->min; |
| 6397 | max = ((struct lysc_node_list *)target)->max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6398 | } else { |
| 6399 | min = max = 0; |
| 6400 | } |
| 6401 | if (min > max) { |
| 6402 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements " |
| 6403 | "after deviation: min value %u is bigger than max value %u.", min, max); |
| 6404 | goto cleanup; |
| 6405 | } |
| 6406 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6407 | /* check mandatory - default compatibility */ |
| 6408 | if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT) |
| 6409 | && (target->flags & LYS_MAND_TRUE)) { |
| 6410 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6411 | "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype)); |
| 6412 | goto cleanup; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6413 | } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)target)->dflt |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6414 | && (target->flags & LYS_MAND_TRUE)) { |
| 6415 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice."); |
| 6416 | goto cleanup; |
| 6417 | } |
| 6418 | if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) { |
| 6419 | /* mandatory node under a default case */ |
| 6420 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6421 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6422 | lys_nodetype2str(target->nodetype), target->name, target->parent->name); |
| 6423 | goto cleanup; |
| 6424 | } |
| 6425 | |
| 6426 | /* success */ |
| 6427 | ret = LY_SUCCESS; |
| 6428 | |
| 6429 | cleanup: |
| 6430 | lysc_update_path(ctx, NULL, NULL); |
| 6431 | return ret; |
| 6432 | } |
| 6433 | |
| 6434 | LY_ERR |
| 6435 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
| 6436 | { |
| 6437 | LY_ERR ret = LY_EVALID; |
| 6438 | struct ly_set devs_p = {0}; |
| 6439 | struct ly_set targets = {0}; |
| 6440 | struct lysc_node *target; /* target target of the deviation */ |
| 6441 | struct lysp_deviation *dev; |
| 6442 | struct lysp_deviate *d, **dp_new; |
| 6443 | LY_ARRAY_COUNT_TYPE u, v; |
| 6444 | struct lysc_deviation **devs = NULL; |
| 6445 | struct lys_module *target_mod, **dev_mod; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6446 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6447 | |
| 6448 | /* get all deviations from the module and all its submodules ... */ |
| 6449 | LY_ARRAY_FOR(mod_p->deviations, u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6450 | 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] | 6451 | } |
| 6452 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 6453 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6454 | 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] | 6455 | } |
| 6456 | } |
| 6457 | if (!devs_p.count) { |
| 6458 | /* nothing to do */ |
| 6459 | return LY_SUCCESS; |
| 6460 | } |
| 6461 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6462 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 6463 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6464 | /* ... and group them by the target node */ |
| 6465 | devs = calloc(devs_p.count, sizeof *devs); |
| 6466 | for (u = 0; u < devs_p.count; ++u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6467 | uint32_t index; |
| 6468 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6469 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6470 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6471 | |
| 6472 | /* resolve the target */ |
Michal Vasko | cb18c7f | 2020-08-24 09:22:28 +0200 | [diff] [blame] | 6473 | 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] | 6474 | (const struct lysc_node **)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6475 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6476 | /* move the target pointer to input/output to make them different from the action and |
| 6477 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 6478 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 6479 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 6480 | if (flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6481 | target = (struct lysc_node *)&((struct lysc_action *)target)->input; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6482 | flags |= LYSC_OPT_INTERNAL; |
| 6483 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6484 | target = (struct lysc_node *)&((struct lysc_action *)target)->output; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6485 | flags |= LYSC_OPT_INTERNAL; |
| 6486 | } |
| 6487 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6488 | /* insert into the set of targets with duplicity detection */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6489 | ret = ly_set_add(&targets, target, 0, &index); |
| 6490 | LY_CHECK_GOTO(ret, cleanup); |
| 6491 | if (!devs[index]) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6492 | /* new record */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6493 | devs[index] = calloc(1, sizeof **devs); |
| 6494 | devs[index]->target = target; |
| 6495 | devs[index]->nodeid = dev->nodeid; |
| 6496 | devs[index]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6497 | } |
| 6498 | /* add deviates into the deviation's list of deviates */ |
Michal Vasko | 5c38f36 | 2020-08-24 09:22:51 +0200 | [diff] [blame] | 6499 | LY_LIST_FOR(dev->deviates, d) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6500 | 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] | 6501 | *dp_new = d; |
| 6502 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6503 | devs[index]->not_supported = 1; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6504 | } |
| 6505 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6506 | |
| 6507 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6508 | } |
| 6509 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6510 | /* apply deviations */ |
| 6511 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6512 | uint8_t match = 0; |
| 6513 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6514 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 6515 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 6516 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6517 | 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] | 6518 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6519 | 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] | 6520 | } |
| 6521 | } |
| 6522 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6523 | /* remember target module (the target node may be removed) */ |
| 6524 | target_mod = devs[u]->target->module; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6525 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6526 | /* apply the deviation */ |
| 6527 | LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6528 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6529 | /* 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] | 6530 | LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) { |
| 6531 | if (target_mod->compiled->deviated_by[v] == mod_p->mod) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6532 | match = 1; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6533 | break; |
| 6534 | } |
| 6535 | } |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 6536 | if (!match) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6537 | 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] | 6538 | *dev_mod = mod_p->mod; |
| 6539 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6540 | } |
| 6541 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6542 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6543 | ret = LY_SUCCESS; |
| 6544 | |
| 6545 | cleanup: |
| 6546 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6547 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6548 | free(devs[u]); |
| 6549 | } |
| 6550 | free(devs); |
| 6551 | ly_set_erase(&targets, NULL); |
| 6552 | ly_set_erase(&devs_p, NULL); |
| 6553 | |
| 6554 | return ret; |
| 6555 | } |
| 6556 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6557 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6558 | * @brief Compile the given YANG submodule into the main module. |
| 6559 | * @param[in] ctx Compile context |
| 6560 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6561 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6562 | */ |
| 6563 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6564 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6565 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6566 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6567 | LY_ERR ret = LY_SUCCESS; |
| 6568 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6569 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6570 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6571 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6572 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6573 | if (!mainmod->mod->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6574 | /* features are compiled directly into the compiled module structure, |
| 6575 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6576 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6577 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6578 | LY_CHECK_GOTO(ret, error); |
| 6579 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6580 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6581 | if (!mainmod->mod->dis_identities) { |
| 6582 | ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities); |
| 6583 | LY_CHECK_GOTO(ret, error); |
| 6584 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6585 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6586 | /* data nodes */ |
| 6587 | LY_LIST_FOR(submod->data, node_p) { |
| 6588 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6589 | LY_CHECK_GOTO(ret, error); |
| 6590 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6591 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6592 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6593 | 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] | 6594 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6595 | error: |
| 6596 | return ret; |
| 6597 | } |
| 6598 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6599 | static void * |
| 6600 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6601 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6602 | for (LY_ARRAY_COUNT_TYPE u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6603 | if (substmts[u].stmt == stmt) { |
| 6604 | return substmts[u].storage; |
| 6605 | } |
| 6606 | } |
| 6607 | return NULL; |
| 6608 | } |
| 6609 | |
| 6610 | LY_ERR |
| 6611 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6612 | { |
| 6613 | LY_ERR ret = LY_EVALID, r; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6614 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6615 | struct lysp_stmt *stmt; |
| 6616 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6617 | |
| 6618 | /* check for invalid substatements */ |
| 6619 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6620 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6621 | continue; |
| 6622 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6623 | for (u = 0; substmts[u].stmt; ++u) { |
| 6624 | if (substmts[u].stmt == stmt->kw) { |
| 6625 | break; |
| 6626 | } |
| 6627 | } |
| 6628 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6629 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6630 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6631 | goto cleanup; |
| 6632 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6633 | } |
| 6634 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6635 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6636 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6637 | /* keep order of the processing the same as the order in the defined substmts, |
| 6638 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6639 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6640 | uint8_t stmt_present = 0; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6641 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6642 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6643 | if (substmts[u].stmt != stmt->kw) { |
| 6644 | continue; |
| 6645 | } |
| 6646 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6647 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6648 | if (substmts[u].storage) { |
| 6649 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6650 | case LY_STMT_STATUS: |
| 6651 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6652 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6653 | break; |
| 6654 | case LY_STMT_UNITS: { |
| 6655 | const char **units; |
| 6656 | |
| 6657 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6658 | /* single item */ |
| 6659 | if (*((const char **)substmts[u].storage)) { |
| 6660 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6661 | goto cleanup; |
| 6662 | } |
| 6663 | units = (const char **)substmts[u].storage; |
| 6664 | } else { |
| 6665 | /* sized array */ |
| 6666 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6667 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6668 | } |
| 6669 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6670 | break; |
| 6671 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6672 | case LY_STMT_TYPE: { |
| 6673 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6674 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6675 | |
| 6676 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6677 | /* single item */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6678 | if (*(struct lysc_type **)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6679 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6680 | goto cleanup; |
| 6681 | } |
| 6682 | compiled = substmts[u].storage; |
| 6683 | } else { |
| 6684 | /* sized array */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6685 | struct lysc_type ***types = (struct lysc_type ***)substmts[u].storage, **type = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6686 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6687 | compiled = (void *)type; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6688 | } |
| 6689 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6690 | 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] | 6691 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node *)ext->parent)->sp : NULL, |
| 6692 | 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] | 6693 | 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] | 6694 | lysp_type_free(ctx->ctx, parsed); |
| 6695 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6696 | break; |
| 6697 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6698 | case LY_STMT_IF_FEATURE: { |
| 6699 | struct lysc_iffeature *iff = NULL; |
| 6700 | |
| 6701 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6702 | /* single item */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6703 | if (((struct lysc_iffeature *)substmts[u].storage)->features) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6704 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6705 | goto cleanup; |
| 6706 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6707 | iff = (struct lysc_iffeature *)substmts[u].storage; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6708 | } else { |
| 6709 | /* sized array */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 6710 | struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6711 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6712 | } |
| 6713 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6714 | break; |
| 6715 | } |
| 6716 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6717 | * 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] | 6718 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6719 | 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.", |
| 6720 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6721 | goto cleanup; |
| 6722 | } |
| 6723 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6724 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6725 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6726 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6727 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6728 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6729 | goto cleanup; |
| 6730 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6731 | } |
| 6732 | |
| 6733 | ret = LY_SUCCESS; |
| 6734 | |
| 6735 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6736 | return ret; |
| 6737 | } |
| 6738 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6739 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6740 | * @brief Check when for cyclic dependencies. |
| 6741 | * @param[in] set Set with all the referenced nodes. |
| 6742 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6743 | * @return LY_ERR value |
| 6744 | */ |
| 6745 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6746 | 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] | 6747 | { |
| 6748 | struct lyxp_set tmp_set; |
| 6749 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6750 | uint32_t i, j; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6751 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6752 | struct lysc_when *when; |
| 6753 | LY_ERR ret = LY_SUCCESS; |
| 6754 | |
| 6755 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6756 | |
| 6757 | /* prepare in_ctx of the set */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 6758 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6759 | xp_scnode = &set->val.scnodes[i]; |
| 6760 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6761 | if (xp_scnode->in_ctx != -1) { |
| 6762 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6763 | xp_scnode->in_ctx = 1; |
| 6764 | } |
| 6765 | } |
| 6766 | |
| 6767 | for (i = 0; i < set->used; ++i) { |
| 6768 | xp_scnode = &set->val.scnodes[i]; |
| 6769 | if (xp_scnode->in_ctx != 1) { |
| 6770 | /* already checked */ |
| 6771 | continue; |
| 6772 | } |
| 6773 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6774 | 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] | 6775 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6776 | /* no when to check */ |
| 6777 | xp_scnode->in_ctx = 0; |
| 6778 | continue; |
| 6779 | } |
| 6780 | |
| 6781 | node = xp_scnode->scnode; |
| 6782 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6783 | LY_ARRAY_FOR(node->when, u) { |
| 6784 | when = node->when[u]; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6785 | ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6786 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 6787 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6788 | 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] | 6789 | goto cleanup; |
| 6790 | } |
| 6791 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6792 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6793 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6794 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6795 | /* try to find this node in our set */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6796 | uint32_t idx; |
| 6797 | 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] | 6798 | 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] | 6799 | ret = LY_EVALID; |
| 6800 | goto cleanup; |
| 6801 | } |
| 6802 | |
| 6803 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6804 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6805 | } else { |
| 6806 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6807 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6808 | } |
| 6809 | } |
| 6810 | |
| 6811 | /* merge this set into the global when set */ |
| 6812 | lyxp_set_scnode_merge(set, &tmp_set); |
| 6813 | } |
| 6814 | |
| 6815 | /* check when of non-data parents as well */ |
| 6816 | node = node->parent; |
| 6817 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 6818 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6819 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 6820 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6821 | } |
| 6822 | |
| 6823 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6824 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6825 | return ret; |
| 6826 | } |
| 6827 | |
| 6828 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6829 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 6830 | * @param[in] ctx Compile context. |
| 6831 | * @param[in] node Node to check. |
| 6832 | * @return LY_ERR value |
| 6833 | */ |
| 6834 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6835 | 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] | 6836 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6837 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6838 | uint32_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6839 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6840 | uint32_t opts; |
| 6841 | uint8_t input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6842 | struct lysc_when **when = NULL; |
| 6843 | struct lysc_must *musts = NULL; |
| 6844 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6845 | const struct lysc_node *op; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6846 | |
| 6847 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6848 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6849 | if (node->flags & LYS_CONFIG_R) { |
| 6850 | for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent); |
| 6851 | if (op) { |
| 6852 | /* we are actually in output */ |
| 6853 | opts = LYXP_SCNODE_OUTPUT; |
| 6854 | } |
| 6855 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6856 | |
| 6857 | switch (node->nodetype) { |
| 6858 | case LYS_CONTAINER: |
| 6859 | when = ((struct lysc_node_container *)node)->when; |
| 6860 | musts = ((struct lysc_node_container *)node)->musts; |
| 6861 | break; |
| 6862 | case LYS_CHOICE: |
| 6863 | when = ((struct lysc_node_choice *)node)->when; |
| 6864 | break; |
| 6865 | case LYS_LEAF: |
| 6866 | when = ((struct lysc_node_leaf *)node)->when; |
| 6867 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 6868 | break; |
| 6869 | case LYS_LEAFLIST: |
| 6870 | when = ((struct lysc_node_leaflist *)node)->when; |
| 6871 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 6872 | break; |
| 6873 | case LYS_LIST: |
| 6874 | when = ((struct lysc_node_list *)node)->when; |
| 6875 | musts = ((struct lysc_node_list *)node)->musts; |
| 6876 | break; |
| 6877 | case LYS_ANYXML: |
| 6878 | case LYS_ANYDATA: |
| 6879 | when = ((struct lysc_node_anydata *)node)->when; |
| 6880 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 6881 | break; |
| 6882 | case LYS_CASE: |
| 6883 | when = ((struct lysc_node_case *)node)->when; |
| 6884 | break; |
| 6885 | case LYS_NOTIF: |
| 6886 | musts = ((struct lysc_notif *)node)->musts; |
| 6887 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6888 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6889 | case LYS_ACTION: |
| 6890 | /* first process input musts */ |
| 6891 | musts = ((struct lysc_action *)node)->input.musts; |
| 6892 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6893 | default: |
| 6894 | /* nothing to check */ |
| 6895 | break; |
| 6896 | } |
| 6897 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6898 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6899 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6900 | 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] | 6901 | 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] | 6902 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6903 | 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] | 6904 | goto cleanup; |
| 6905 | } |
| 6906 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6907 | ctx->path[0] = '\0'; |
| 6908 | 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] | 6909 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6910 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6911 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 6912 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6913 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6914 | /* 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] | 6915 | 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] | 6916 | schema->name); |
| 6917 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6918 | |
| 6919 | /* check dummy node accessing */ |
| 6920 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6921 | 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] | 6922 | ret = LY_EVALID; |
| 6923 | goto cleanup; |
| 6924 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6925 | } |
| 6926 | } |
| 6927 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6928 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6929 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6930 | LY_CHECK_GOTO(ret, cleanup); |
| 6931 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6932 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6933 | } |
| 6934 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6935 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6936 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6937 | LY_ARRAY_FOR(musts, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6938 | 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] | 6939 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6940 | 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] | 6941 | goto cleanup; |
| 6942 | } |
| 6943 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6944 | ctx->path[0] = '\0'; |
| 6945 | 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] | 6946 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6947 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6948 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6949 | /* 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] | 6950 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 6951 | 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] | 6952 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6953 | } |
| 6954 | } |
| 6955 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6956 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6957 | } |
| 6958 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6959 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6960 | /* now check output musts */ |
| 6961 | input_done = 1; |
| 6962 | musts = ((struct lysc_action *)node)->output.musts; |
| 6963 | opts = LYXP_SCNODE_OUTPUT; |
| 6964 | goto check_musts; |
| 6965 | } |
| 6966 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6967 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6968 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6969 | return ret; |
| 6970 | } |
| 6971 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 6972 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6973 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 6974 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6975 | const struct lysc_node *target = NULL, *siter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6976 | struct ly_path *p; |
| 6977 | struct lysc_type *type; |
| 6978 | |
| 6979 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 6980 | |
| 6981 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 6982 | LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, |
| 6983 | 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] | 6984 | LY_PREF_SCHEMA, lref->path_context, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6985 | |
| 6986 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6987 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6988 | ly_path_free(node->module->ctx, p); |
| 6989 | |
| 6990 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 6991 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6992 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 6993 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
| 6994 | return LY_EVALID; |
| 6995 | } |
| 6996 | |
| 6997 | /* check status */ |
| 6998 | ctx->path[0] = '\0'; |
| 6999 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7000 | ctx->path_len = strlen(ctx->path); |
| 7001 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 7002 | return LY_EVALID; |
| 7003 | } |
| 7004 | ctx->path_len = 1; |
| 7005 | ctx->path[1] = '\0'; |
| 7006 | |
| 7007 | /* check config */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7008 | if (lref->require_instance) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 7009 | 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] | 7010 | if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7011 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
| 7012 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
| 7013 | return LY_EVALID; |
| 7014 | } |
| 7015 | } |
| 7016 | |
| 7017 | /* store the target's type and check for circular chain of leafrefs */ |
| 7018 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 7019 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 7020 | if (type == (struct lysc_type *)lref) { |
| 7021 | /* circular chain detected */ |
| 7022 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7023 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
| 7024 | return LY_EVALID; |
| 7025 | } |
| 7026 | } |
| 7027 | |
| 7028 | /* check if leafref and its target are under common if-features */ |
| 7029 | if (lys_compile_leafref_features_validate(node, target)) { |
| 7030 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7031 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 7032 | " features applicable to the leafref itself.", lref->path->expr); |
| 7033 | return LY_EVALID; |
| 7034 | } |
| 7035 | |
| 7036 | return LY_SUCCESS; |
| 7037 | } |
| 7038 | |
| 7039 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7040 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7041 | { |
| 7042 | struct lysc_ext_instance *ext; |
| 7043 | struct lysp_ext_instance *ext_p = NULL; |
| 7044 | struct lysp_stmt *stmt; |
| 7045 | const struct lys_module *ext_mod; |
| 7046 | LY_ERR ret = LY_SUCCESS; |
| 7047 | |
| 7048 | /* create the parsed extension instance manually */ |
| 7049 | ext_p = calloc(1, sizeof *ext_p); |
| 7050 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 7051 | ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0); |
| 7052 | ext_p->argument = lydict_insert(ctx->ctx, "default", 0); |
| 7053 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7054 | ext_p->insubstmt_index = 0; |
| 7055 | |
| 7056 | stmt = calloc(1, sizeof *ext_p->child); |
| 7057 | stmt->stmt = lydict_insert(ctx->ctx, "type", 0); |
| 7058 | stmt->arg = lydict_insert(ctx->ctx, "boolean", 0); |
| 7059 | stmt->kw = LY_STMT_TYPE; |
| 7060 | ext_p->child = stmt; |
| 7061 | |
| 7062 | /* allocate new extension instance */ |
| 7063 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7064 | |
| 7065 | /* manually get extension definition module */ |
| 7066 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7067 | |
| 7068 | /* compile the extension instance */ |
| 7069 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7070 | |
| 7071 | cleanup: |
| 7072 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7073 | free(ext_p); |
| 7074 | return ret; |
| 7075 | } |
| 7076 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7077 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7078 | 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] | 7079 | const struct lys_module *dflt_mod, struct lyd_value *storage) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7080 | { |
| 7081 | LY_ERR ret; |
| 7082 | struct ly_err_item *err = NULL; |
| 7083 | |
| 7084 | ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 7085 | LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err); |
| 7086 | if (err) { |
| 7087 | ly_err_print(err); |
| 7088 | ctx->path[0] = '\0'; |
| 7089 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7090 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7091 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7092 | ly_err_free(err); |
| 7093 | } |
| 7094 | if (!ret) { |
| 7095 | ++storage->realtype->refcount; |
| 7096 | return LY_SUCCESS; |
| 7097 | } |
| 7098 | return ret; |
| 7099 | } |
| 7100 | |
| 7101 | static LY_ERR |
| 7102 | 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] | 7103 | const struct lys_module *dflt_mod) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7104 | { |
| 7105 | LY_ERR ret; |
| 7106 | |
| 7107 | assert(!leaf->dflt); |
| 7108 | |
| 7109 | if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) { |
| 7110 | /* ignore default values for keys and mandatory leaves */ |
| 7111 | return LY_SUCCESS; |
| 7112 | } |
| 7113 | |
| 7114 | /* allocate the default value */ |
| 7115 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 7116 | LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM); |
| 7117 | |
| 7118 | /* store the default value */ |
| 7119 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt); |
| 7120 | if (ret) { |
| 7121 | free(leaf->dflt); |
| 7122 | leaf->dflt = NULL; |
| 7123 | } |
| 7124 | |
| 7125 | return ret; |
| 7126 | } |
| 7127 | |
| 7128 | static LY_ERR |
| 7129 | 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] | 7130 | const struct lys_module *dflt_mod) |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7131 | { |
| 7132 | LY_ERR ret; |
| 7133 | LY_ARRAY_COUNT_TYPE orig_count, u, v; |
| 7134 | |
| 7135 | assert(dflt || dflts); |
| 7136 | |
| 7137 | if (llist->dflts) { |
| 7138 | /* there were already some defaults and we are adding new by deviations */ |
| 7139 | assert(dflts); |
| 7140 | orig_count = LY_ARRAY_COUNT(llist->dflts); |
| 7141 | } else { |
| 7142 | orig_count = 0; |
| 7143 | } |
| 7144 | |
| 7145 | /* allocate new items */ |
| 7146 | if (dflts) { |
| 7147 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM); |
| 7148 | } else { |
| 7149 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM); |
| 7150 | } |
| 7151 | |
| 7152 | /* fill each new default value */ |
| 7153 | if (dflts) { |
| 7154 | LY_ARRAY_FOR(dflts, u) { |
| 7155 | llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts); |
| 7156 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod, |
| 7157 | llist->dflts[orig_count + u]); |
| 7158 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret); |
| 7159 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7160 | } |
| 7161 | } else { |
| 7162 | llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts); |
| 7163 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]); |
| 7164 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret); |
| 7165 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7166 | } |
| 7167 | |
| 7168 | /* check default value uniqueness */ |
| 7169 | if (llist->flags & LYS_CONFIG_W) { |
| 7170 | /* configuration data values must be unique - so check the default values */ |
| 7171 | for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) { |
| 7172 | for (v = 0; v < u; ++v) { |
| 7173 | if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7174 | uint8_t dynamic = 0; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7175 | const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic); |
| 7176 | |
| 7177 | lysc_update_path(ctx, llist->parent, llist->name); |
| 7178 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7179 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 7180 | lysc_update_path(ctx, NULL, NULL); |
| 7181 | if (dynamic) { |
| 7182 | free((char *)val); |
| 7183 | } |
| 7184 | return LY_EVALID; |
| 7185 | } |
| 7186 | } |
| 7187 | } |
| 7188 | } |
| 7189 | |
| 7190 | return LY_SUCCESS; |
| 7191 | } |
| 7192 | |
| 7193 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7194 | lys_compile_unres(struct lysc_ctx *ctx) |
| 7195 | { |
| 7196 | struct lysc_node *node; |
| 7197 | struct lysc_type *type, *typeiter; |
| 7198 | struct lysc_type_leafref *lref; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7199 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7200 | uint32_t i; |
| 7201 | |
| 7202 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7203 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7204 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 7205 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7206 | node = ctx->leafrefs.objs[i]; |
| 7207 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7208 | type = ((struct lysc_node_leaf *)node)->type; |
| 7209 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7210 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 7211 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7212 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7213 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7214 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 7215 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 7216 | } |
| 7217 | } |
| 7218 | } |
| 7219 | } |
| 7220 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7221 | /* store pointer to the real type */ |
| 7222 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 7223 | if (type->basetype == LY_TYPE_LEAFREF) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7224 | for (typeiter = ((struct lysc_type_leafref *)type)->realtype; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7225 | typeiter->basetype == LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7226 | typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {} |
| 7227 | ((struct lysc_type_leafref *)type)->realtype = typeiter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7228 | } else if (type->basetype == LY_TYPE_UNION) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7229 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7230 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7231 | 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] | 7232 | typeiter->basetype == LY_TYPE_LEAFREF; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7233 | typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {} |
| 7234 | ((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] | 7235 | } |
| 7236 | } |
| 7237 | } |
| 7238 | } |
| 7239 | |
| 7240 | /* check xpath */ |
| 7241 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 7242 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 7243 | } |
| 7244 | |
| 7245 | /* finish incomplete default values compilation */ |
| 7246 | for (i = 0; i < ctx->dflts.count; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7247 | struct lysc_incomplete_dflt *r = ctx->dflts.objs[i]; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7248 | if (r->leaf->nodetype == LYS_LEAF) { |
| 7249 | LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod)); |
| 7250 | } else { |
| 7251 | 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] | 7252 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7253 | } |
| 7254 | |
| 7255 | return LY_SUCCESS; |
| 7256 | } |
| 7257 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7258 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7259 | lys_compile(struct lys_module **mod, uint32_t options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7260 | { |
| 7261 | struct lysc_ctx ctx = {0}; |
| 7262 | struct lysc_module *mod_c; |
| 7263 | struct lysp_module *sp; |
| 7264 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7265 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7266 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7267 | struct lys_module *m; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7268 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7269 | uint32_t i; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7270 | uint16_t compile_id; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7271 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7272 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7273 | LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7274 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7275 | if (!(*mod)->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7276 | /* just imported modules are not compiled */ |
| 7277 | return LY_SUCCESS; |
| 7278 | } |
| 7279 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7280 | compile_id = ++(*mod)->ctx->module_set_id; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7281 | sp = (*mod)->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7282 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7283 | ctx.ctx = (*mod)->ctx; |
| 7284 | ctx.mod = *mod; |
| 7285 | ctx.mod_def = *mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7286 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7287 | ctx.path_len = 1; |
| 7288 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7289 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7290 | (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 7291 | LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM); |
| 7292 | mod_c->mod = *mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7293 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7294 | LY_ARRAY_FOR(sp->imports, u) { |
| 7295 | LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error); |
| 7296 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7297 | LY_ARRAY_FOR(sp->includes, u) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7298 | LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7299 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7300 | |
| 7301 | /* features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7302 | if ((*mod)->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7303 | /* there is already precompiled array of features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7304 | mod_c->features = (*mod)->dis_features; |
| 7305 | (*mod)->dis_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7306 | } else { |
| 7307 | /* features are compiled directly into the compiled module structure, |
| 7308 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7309 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7310 | LY_CHECK_GOTO(ret, error); |
| 7311 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7312 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7313 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7314 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7315 | * are present. */ |
| 7316 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7317 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7318 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7319 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7320 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7321 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7322 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7323 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7324 | ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7325 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7326 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7327 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7328 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7329 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7330 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7331 | /* identities, work similarly to features with the precompilation */ |
| 7332 | if ((*mod)->dis_identities) { |
| 7333 | mod_c->identities = (*mod)->dis_identities; |
| 7334 | (*mod)->dis_identities = NULL; |
| 7335 | } else { |
| 7336 | ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities); |
| 7337 | LY_CHECK_GOTO(ret, error); |
| 7338 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7339 | if (sp->identities) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7340 | LY_CHECK_GOTO(ret = lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7341 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7342 | lysc_update_path(&ctx, NULL, "{submodule}"); |
| 7343 | LY_ARRAY_FOR(sp->includes, v) { |
| 7344 | if (sp->includes[v].submodule->identities) { |
| 7345 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
| 7346 | ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities); |
| 7347 | LY_CHECK_GOTO(ret, error); |
| 7348 | lysc_update_path(&ctx, NULL, NULL); |
| 7349 | } |
| 7350 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7351 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7352 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7353 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7354 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7355 | 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] | 7356 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7357 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7358 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7359 | 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] | 7360 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7361 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7362 | 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] | 7363 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7364 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7365 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7366 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7367 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7368 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7369 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7370 | /* extension instances TODO cover extension instances from submodules */ |
| 7371 | 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] | 7372 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7373 | /* finish compilation for all unresolved items in the context */ |
| 7374 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7375 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7376 | /* validate non-instantiated groupings from the parsed schema, |
| 7377 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7378 | ctx.options |= LYSC_OPT_GROUPING; |
| 7379 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7380 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7381 | 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] | 7382 | } |
| 7383 | } |
| 7384 | LY_LIST_FOR(sp->data, node_p) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 7385 | grps = (struct lysp_grp *)lysp_node_groupings(node_p); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7386 | LY_ARRAY_FOR(grps, u) { |
| 7387 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7388 | 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] | 7389 | } |
| 7390 | } |
| 7391 | } |
| 7392 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7393 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7394 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7395 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7396 | } |
| 7397 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7398 | #if 0 |
| 7399 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7400 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7401 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7402 | * the anotation definitions available in the internal schema structure. */ |
| 7403 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7404 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7405 | lys_free(mod, NULL, 1, 1); |
| 7406 | return NULL; |
| 7407 | } |
| 7408 | } |
| 7409 | #endif |
| 7410 | |
| 7411 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7412 | if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) { |
| 7413 | 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] | 7414 | } |
| 7415 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7416 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7417 | ly_set_erase(&ctx.xpath, NULL); |
| 7418 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7419 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7420 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7421 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7422 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7423 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7424 | lysp_module_free((*mod)->parsed); |
| 7425 | (*mod)->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7426 | } |
| 7427 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7428 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7429 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7430 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7431 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7432 | if (m->implemented > 1) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7433 | m->implemented = 1; |
| 7434 | } |
| 7435 | } |
| 7436 | } |
| 7437 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7438 | return LY_SUCCESS; |
| 7439 | |
| 7440 | error: |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7441 | lys_feature_precompile_revert(&ctx, *mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7442 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7443 | ly_set_erase(&ctx.xpath, NULL); |
| 7444 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7445 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7446 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7447 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7448 | lysc_module_free(mod_c, NULL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7449 | (*mod)->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7450 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7451 | /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency |
| 7452 | * of this module, since this module can be also compiled from dependency, there can be some other modules being |
| 7453 | * 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] | 7454 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7455 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7456 | if ((m->implemented >= compile_id) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7457 | /* revert features list to the precompiled state */ |
| 7458 | lys_feature_precompile_revert(&ctx, m); |
| 7459 | /* mark module as imported-only / not-implemented */ |
| 7460 | m->implemented = 0; |
| 7461 | /* free the compiled version of the module */ |
| 7462 | lysc_module_free(m->compiled, NULL); |
| 7463 | m->compiled = NULL; |
| 7464 | } |
| 7465 | } |
| 7466 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7467 | /* remove the module itself from the context and free it */ |
| 7468 | ly_set_rm(&ctx.ctx->list, *mod, NULL); |
| 7469 | lys_module_free(*mod, NULL); |
| 7470 | *mod = NULL; |
| 7471 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7472 | return ret; |
| 7473 | } |