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, |
| 45 | void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod); |
| 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, |
| 153 | struct lys_module *dflt_mod) |
| 154 | { |
| 155 | struct lysc_incomplete_dflt *r; |
| 156 | uint32_t i; |
| 157 | |
| 158 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 159 | r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i]; |
| 160 | if (r->leaf == leaf) { |
| 161 | /* just replace the default */ |
| 162 | r->dflt = dflt; |
| 163 | return LY_SUCCESS; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | r = malloc(sizeof *r); |
| 168 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 169 | r->leaf = leaf; |
| 170 | r->dflt = dflt; |
| 171 | r->dflts = NULL; |
| 172 | r->dflt_mod = dflt_mod; |
| 173 | ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST); |
| 174 | |
| 175 | return LY_SUCCESS; |
| 176 | } |
| 177 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 178 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 179 | * @brief Add record into the compile context's list of incomplete default values. |
| 180 | * @param[in] ctx Compile context with the incomplete default values list. |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 181 | * @param[in] term Term context node with the default value. |
| 182 | * @param[in] value String default value. |
| 183 | * @param[in] val_len Length of @p value. |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 184 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 185 | * @return LY_EMEM in case of memory allocation failure. |
| 186 | * @return LY_SUCCESS |
| 187 | */ |
| 188 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 189 | lysc_incomplete_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char **dflts, |
| 190 | struct lys_module *dflt_mod) |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 191 | { |
| 192 | struct lysc_incomplete_dflt *r; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 193 | uint32_t i; |
| 194 | |
| 195 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 196 | r = (struct lysc_incomplete_dflt *)ctx->dflts.objs[i]; |
| 197 | if (r->llist == llist) { |
| 198 | /* just replace the defaults */ |
| 199 | r->dflts = dflts; |
| 200 | return LY_SUCCESS; |
| 201 | } |
| 202 | } |
| 203 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 204 | r = malloc(sizeof *r); |
| 205 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 206 | r->llist = llist; |
| 207 | r->dflt = NULL; |
| 208 | r->dflts = dflts; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 209 | r->dflt_mod = dflt_mod; |
| 210 | ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST); |
| 211 | |
| 212 | return LY_SUCCESS; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 217 | * @param[in] ctx Compile context with the incomplete default values list. |
| 218 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 219 | */ |
| 220 | static void |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 221 | lysc_incomplete_dflt_remove(struct lysc_ctx *ctx, struct lysc_node *term) |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 222 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 223 | uint32_t u; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 224 | struct lysc_incomplete_dflt *r; |
| 225 | |
| 226 | for (u = 0; u < ctx->dflts.count; ++u) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 227 | r = ctx->dflts.objs[u]; |
| 228 | if (r->leaf == (struct lysc_node_leaf *)term) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 229 | free(ctx->dflts.objs[u]); |
| 230 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 231 | --ctx->dflts.count; |
| 232 | return; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 237 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 238 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 239 | { |
| 240 | int len; |
| 241 | int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
| 242 | |
| 243 | if (!name) { |
| 244 | /* removing last path segment */ |
| 245 | if (ctx->path[ctx->path_len - 1] == '}') { |
| 246 | for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len); |
| 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: |
| 255 | for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len); |
| 256 | if (ctx->path_len == 0) { |
| 257 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 258 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | /* set new terminating NULL-byte */ |
| 262 | ctx->path[ctx->path_len] = '\0'; |
| 263 | } else { |
| 264 | if (ctx->path_len > 1) { |
| 265 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 266 | /* extension of the special tag */ |
| 267 | nextlevel = 2; |
| 268 | --ctx->path_len; |
| 269 | } else { |
| 270 | /* there is already some path, so add next level */ |
| 271 | nextlevel = 1; |
| 272 | } |
| 273 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 274 | |
| 275 | if (nextlevel != 2) { |
| 276 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 277 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 278 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 279 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 280 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 281 | } |
| 282 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 283 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 284 | } |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 285 | if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) { |
| 286 | /* output truncated */ |
| 287 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 288 | } else { |
| 289 | ctx->path_len += len; |
| 290 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 295 | * @brief Duplicate the compiled pattern structure. |
| 296 | * |
| 297 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 298 | * |
| 299 | * @param[in] orig The pattern structure to duplicate. |
| 300 | * @return The duplicated structure to use. |
| 301 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 302 | static struct lysc_pattern* |
| 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 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 319 | static struct lysc_pattern** |
| 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 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 343 | struct lysc_range* |
| 344 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 345 | { |
| 346 | struct lysc_range *dup; |
| 347 | LY_ERR ret; |
| 348 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 349 | assert(orig); |
| 350 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 351 | dup = calloc(1, sizeof *dup); |
| 352 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 353 | if (orig->parts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 354 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup); |
| 355 | LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts); |
| 356 | memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 357 | } |
| 358 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 359 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 360 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 361 | |
| 362 | return dup; |
| 363 | cleanup: |
| 364 | free(dup); |
| 365 | (void) ret; /* set but not used due to the return type */ |
| 366 | return NULL; |
| 367 | } |
| 368 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 369 | /** |
| 370 | * @brief Stack for processing if-feature expressions. |
| 371 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 372 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 373 | int size; /**< number of items in the stack */ |
| 374 | int index; /**< first empty item */ |
| 375 | uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 376 | }; |
| 377 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 378 | /** |
| 379 | * @brief Add @ref ifftokens into the stack. |
| 380 | * @param[in] stack The if-feature stack to use. |
| 381 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 382 | * @return LY_EMEM in case of memory allocation error |
| 383 | * @return LY_ESUCCESS if the value successfully stored. |
| 384 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 385 | static LY_ERR |
| 386 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 387 | { |
| 388 | if (stack->index == stack->size) { |
| 389 | stack->size += 4; |
| 390 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 391 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 392 | } |
| 393 | stack->stack[stack->index++] = value; |
| 394 | return LY_SUCCESS; |
| 395 | } |
| 396 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 397 | /** |
| 398 | * @brief Get (and remove) the last item form the stack. |
| 399 | * @param[in] stack The if-feature stack to use. |
| 400 | * @return The value from the top of the stack. |
| 401 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 402 | static uint8_t |
| 403 | iff_stack_pop(struct iff_stack *stack) |
| 404 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 405 | assert(stack && stack->index); |
| 406 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 407 | stack->index--; |
| 408 | return stack->stack[stack->index]; |
| 409 | } |
| 410 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 411 | /** |
| 412 | * @brief Clean up the stack. |
| 413 | * @param[in] stack The if-feature stack to use. |
| 414 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 415 | static void |
| 416 | iff_stack_clean(struct iff_stack *stack) |
| 417 | { |
| 418 | stack->size = 0; |
| 419 | free(stack->stack); |
| 420 | } |
| 421 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 422 | /** |
| 423 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 424 | * (libyang format of the if-feature expression). |
| 425 | * @param[in,out] list The 2bits array to modify. |
| 426 | * @param[in] op The operand (@ref ifftokens) to store. |
| 427 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 428 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 429 | static void |
| 430 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 431 | { |
| 432 | uint8_t *item; |
| 433 | uint8_t mask = 3; |
| 434 | |
| 435 | assert(pos >= 0); |
| 436 | assert(op <= 3); /* max 2 bits */ |
| 437 | |
| 438 | item = &list[pos / 4]; |
| 439 | mask = mask << 2 * (pos % 4); |
| 440 | *item = (*item) & ~mask; |
| 441 | *item = (*item) | (op << 2 * (pos % 4)); |
| 442 | } |
| 443 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 444 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 445 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 446 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 447 | /** |
| 448 | * @brief Find a feature of the given name and referenced in the given module. |
| 449 | * |
| 450 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 451 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 452 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 453 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 454 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 455 | * assigned till that time will be still valid. |
| 456 | * |
| 457 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 458 | * @param[in] name Name of the feature including possible prefix. |
| 459 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 460 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 461 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 462 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 463 | lys_feature_find(struct lys_module *mod, const char *name, size_t len) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 464 | { |
| 465 | size_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 466 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 467 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 468 | |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 469 | assert(mod); |
| 470 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 471 | for (i = 0; i < len; ++i) { |
| 472 | if (name[i] == ':') { |
| 473 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 474 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 475 | LY_CHECK_RET(!mod, NULL); |
| 476 | |
| 477 | name = &name[i + 1]; |
| 478 | len = len - i - 1; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 483 | if (mod->implemented) { |
| 484 | /* module is implemented so there is already the compiled schema */ |
| 485 | flist = mod->compiled->features; |
| 486 | } else { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 487 | flist = mod->dis_features; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 488 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 489 | LY_ARRAY_FOR(flist, u) { |
| 490 | f = &flist[u]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 491 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 492 | return f; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | return NULL; |
| 497 | } |
| 498 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 499 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 500 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 501 | */ |
| 502 | static LY_ERR |
| 503 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 504 | { |
| 505 | LY_ERR ret = LY_SUCCESS; |
| 506 | |
| 507 | if (!ext_p->compiled) { |
| 508 | lysc_update_path(ctx, NULL, "{extension}"); |
| 509 | lysc_update_path(ctx, NULL, ext_p->name); |
| 510 | |
| 511 | /* compile the extension definition */ |
| 512 | ext_p->compiled = calloc(1, sizeof **ext); |
| 513 | ext_p->compiled->refcount = 1; |
| 514 | DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name); |
| 515 | DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument); |
| 516 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 517 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 518 | |
| 519 | lysc_update_path(ctx, NULL, NULL); |
| 520 | lysc_update_path(ctx, NULL, NULL); |
| 521 | |
| 522 | /* find extension definition plugin */ |
| 523 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 524 | } |
| 525 | |
| 526 | *ext = lysc_ext_dup(ext_p->compiled); |
| 527 | |
| 528 | done: |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 533 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 534 | * |
| 535 | * @param[in] ctx Compilation context. |
| 536 | * @param[in] ext_p Parsed extension instance. |
| 537 | * @param[in,out] ext Prepared compiled extension instance. |
| 538 | * @param[in] parent Extension instance parent. |
| 539 | * @param[in] parent_type Extension instance parent type. |
| 540 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 541 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 542 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 543 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, |
| 544 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 545 | { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 546 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 547 | const char *name; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 548 | size_t u; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 549 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 550 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 551 | |
| 552 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 553 | ext->insubstmt = ext_p->insubstmt; |
| 554 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 555 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 556 | ext->parent = parent; |
| 557 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 558 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 559 | lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}"); |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 560 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 561 | /* get module where the extension definition should be placed */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 562 | for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u); |
| 563 | if (ext_p->yin) { |
| 564 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 565 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 566 | * YANG (import) prefix must be done here. */ |
| 567 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
| 568 | prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0); |
| 569 | u = 0; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 570 | } else { |
| 571 | assert(ctx->mod_def->parsed); |
| 572 | LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) { |
| 573 | if (!ly_strncmp(ctx->mod_def->parsed->imports[v].module->ns, ext_p->name, u - 1)) { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 574 | char *s; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 575 | LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->parsed->imports[v].prefix, &ext_p->name[u]) == -1, |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 576 | ret = LY_EMEM, cleanup); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 577 | prefixed_name = lydict_insert_zc(ctx->ctx, s); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 578 | u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 579 | break; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | if (!prefixed_name) { |
| 584 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 585 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
| 586 | goto cleanup; |
| 587 | } |
| 588 | } else { |
| 589 | prefixed_name = ext_p->name; |
| 590 | } |
| 591 | lysc_update_path(ctx, NULL, prefixed_name); |
| 592 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 593 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 594 | ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 595 | if (!ext_mod) { |
| 596 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 597 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
| 598 | goto cleanup; |
| 599 | } else if (!ext_mod->parsed->extensions) { |
| 600 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 601 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 602 | prefixed_name, ext_mod->name); |
| 603 | goto cleanup; |
| 604 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 605 | } |
| 606 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 607 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 608 | /* find the parsed extension definition there */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 609 | LY_ARRAY_FOR(ext_mod->parsed->extensions, v) { |
| 610 | if (!strcmp(name, ext_mod->parsed->extensions[v].name)) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 611 | /* compile extension definition and assign it */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 612 | LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 613 | break; |
| 614 | } |
| 615 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 616 | if (!ext->def) { |
| 617 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 618 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
| 619 | goto cleanup; |
| 620 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 621 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 622 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 623 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 624 | * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 625 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 626 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 627 | struct lysp_stmt *stmt = NULL; |
| 628 | |
| 629 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 630 | /* ... argument was the first XML child element */ |
| 631 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 632 | /* TODO check namespace of the statement */ |
| 633 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 634 | stmt = ext_p->child; |
| 635 | } |
| 636 | } |
| 637 | } else { |
| 638 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 639 | * with LYS_YIN_ATTR flag */ |
| 640 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 641 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 642 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 643 | break; |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | if (!stmt) { |
| 648 | /* missing extension's argument */ |
| 649 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 650 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
| 651 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 652 | |
| 653 | } |
| 654 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 655 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 656 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 657 | if (prefixed_name != ext_p->name) { |
| 658 | lydict_remove(ctx->ctx, ext_p->name); |
| 659 | ext_p->name = prefixed_name; |
| 660 | if (!ext_p->argument && ext->argument) { |
| 661 | ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0); |
| 662 | } |
| 663 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 664 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 665 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 666 | if (ext->argument) { |
| 667 | lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument); |
| 668 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 669 | LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 670 | if (ext->argument) { |
| 671 | lysc_update_path(ctx, NULL, NULL); |
| 672 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 673 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 674 | ext_p->compiled = ext; |
| 675 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 676 | ret = LY_SUCCESS; |
| 677 | cleanup: |
| 678 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 679 | lydict_remove(ctx->ctx, prefixed_name); |
| 680 | } |
| 681 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 682 | lysc_update_path(ctx, NULL, NULL); |
| 683 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 684 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 685 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 689 | * @brief Compile information from the if-feature statement |
| 690 | * @param[in] ctx Compile context. |
| 691 | * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 692 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 693 | * @return LY_ERR value. |
| 694 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 695 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 696 | lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 697 | { |
| 698 | const char *c = *value; |
| 699 | int r, rc = EXIT_FAILURE; |
| 700 | int i, j, last_not, checkversion = 0; |
| 701 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 702 | uint8_t op; |
| 703 | struct iff_stack stack = {0, 0, NULL}; |
| 704 | struct lysc_feature *f; |
| 705 | |
| 706 | assert(c); |
| 707 | |
| 708 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 709 | for (i = j = last_not = 0; c[i]; i++) { |
| 710 | if (c[i] == '(') { |
| 711 | j++; |
| 712 | checkversion = 1; |
| 713 | continue; |
| 714 | } else if (c[i] == ')') { |
| 715 | j--; |
| 716 | continue; |
| 717 | } else if (isspace(c[i])) { |
| 718 | checkversion = 1; |
| 719 | continue; |
| 720 | } |
| 721 | |
| 722 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 723 | int sp; |
| 724 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 725 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 726 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 727 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 728 | return LY_EVALID; |
| 729 | } else if (!isspace(c[i + r])) { |
| 730 | /* feature name starting with the not/and/or */ |
| 731 | last_not = 0; |
| 732 | f_size++; |
| 733 | } else if (c[i] == 'n') { /* not operation */ |
| 734 | if (last_not) { |
| 735 | /* double not */ |
| 736 | expr_size = expr_size - 2; |
| 737 | last_not = 0; |
| 738 | } else { |
| 739 | last_not = 1; |
| 740 | } |
| 741 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 742 | if (f_exp != f_size) { |
| 743 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 744 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 745 | return LY_EVALID; |
| 746 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 747 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 748 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 749 | /* not a not operation */ |
| 750 | last_not = 0; |
| 751 | } |
| 752 | i += r; |
| 753 | } else { |
| 754 | f_size++; |
| 755 | last_not = 0; |
| 756 | } |
| 757 | expr_size++; |
| 758 | |
| 759 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 760 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 761 | i--; |
| 762 | break; |
| 763 | } |
| 764 | i++; |
| 765 | } |
| 766 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 767 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 768 | /* not matching count of ( and ) */ |
| 769 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 770 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 771 | return LY_EVALID; |
| 772 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 773 | if (f_exp != f_size) { |
| 774 | /* features do not match the needed arguments for the logical operations */ |
| 775 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 776 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 777 | "the required number of operands for the operations.", *value); |
| 778 | return LY_EVALID; |
| 779 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 780 | |
| 781 | if (checkversion || expr_size > 1) { |
| 782 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 783 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 784 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 785 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 786 | return LY_EVALID; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /* allocate the memory */ |
| 791 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 792 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 793 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 794 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 795 | |
| 796 | stack.size = expr_size; |
| 797 | f_size--; expr_size--; /* used as indexes from now */ |
| 798 | |
| 799 | for (i--; i >= 0; i--) { |
| 800 | if (c[i] == ')') { |
| 801 | /* push it on stack */ |
| 802 | iff_stack_push(&stack, LYS_IFF_RP); |
| 803 | continue; |
| 804 | } else if (c[i] == '(') { |
| 805 | /* pop from the stack into result all operators until ) */ |
| 806 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 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)); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 891 | start = start->parent); |
| 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, |
| 995 | struct lysp_ident *identities_p, struct lysc_ident **identities) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 996 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 997 | LY_ARRAY_COUNT_TYPE offset = 0, u, v; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 998 | struct lysc_ctx context = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 999 | LY_ERR ret = LY_SUCCESS; |
| 1000 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1001 | assert(ctx_sc || ctx); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1002 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1003 | if (!ctx_sc) { |
| 1004 | context.ctx = ctx; |
| 1005 | context.mod = module; |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame] | 1006 | context.mod_def = module; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1007 | context.path_len = 1; |
| 1008 | context.path[0] = '/'; |
| 1009 | ctx_sc = &context; |
| 1010 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1011 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1012 | if (!identities_p) { |
| 1013 | return LY_SUCCESS; |
| 1014 | } |
| 1015 | if (*identities) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1016 | offset = LY_ARRAY_COUNT(*identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | lysc_update_path(ctx_sc, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1020 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1021 | LY_ARRAY_FOR(identities_p, u) { |
| 1022 | lysc_update_path(ctx_sc, NULL, identities_p[u].name); |
| 1023 | |
| 1024 | LY_ARRAY_INCREMENT(*identities); |
| 1025 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name); |
| 1026 | DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name); |
| 1027 | DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc); |
| 1028 | DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref); |
| 1029 | (*identities)[offset + u].module = ctx_sc->mod; |
| 1030 | COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v, |
| 1031 | lys_compile_iffeature, ret, done); |
| 1032 | /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */ |
| 1033 | COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u], |
| 1034 | LYEXT_PAR_IDENT, ret, done); |
| 1035 | (*identities)[offset + u].flags = identities_p[u].flags; |
| 1036 | |
| 1037 | lysc_update_path(ctx_sc, NULL, NULL); |
| 1038 | } |
| 1039 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1040 | done: |
| 1041 | return ret; |
| 1042 | } |
| 1043 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1044 | /** |
| 1045 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 1046 | * |
| 1047 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 1048 | * |
| 1049 | * @param[in] ctx Compile context for logging. |
| 1050 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 1051 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 1052 | * @return LY_SUCCESS if everything is ok. |
| 1053 | * @return LY_EVALID if the identity is derived from itself. |
| 1054 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1055 | static LY_ERR |
| 1056 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 1057 | { |
| 1058 | LY_ERR ret = LY_EVALID; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1059 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1060 | struct ly_set recursion = {0}; |
| 1061 | struct lysc_ident *drv; |
| 1062 | |
| 1063 | if (!derived) { |
| 1064 | return LY_SUCCESS; |
| 1065 | } |
| 1066 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1067 | for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1068 | if (ident == derived[u]) { |
| 1069 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1070 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1071 | goto cleanup; |
| 1072 | } |
| 1073 | ly_set_add(&recursion, derived[u], 0); |
| 1074 | } |
| 1075 | |
| 1076 | for (v = 0; v < recursion.count; ++v) { |
| 1077 | drv = recursion.objs[v]; |
| 1078 | if (!drv->derived) { |
| 1079 | continue; |
| 1080 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1081 | for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1082 | if (ident == drv->derived[u]) { |
| 1083 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1084 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1085 | goto cleanup; |
| 1086 | } |
| 1087 | ly_set_add(&recursion, drv->derived[u], 0); |
| 1088 | } |
| 1089 | } |
| 1090 | ret = LY_SUCCESS; |
| 1091 | |
| 1092 | cleanup: |
| 1093 | ly_set_erase(&recursion, NULL); |
| 1094 | return ret; |
| 1095 | } |
| 1096 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1097 | /** |
| 1098 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1099 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1100 | * For bases in identity set backlinks to them from the base identities. For identityref, store |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1101 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1102 | * to distinguish these two use cases. |
| 1103 | * |
| 1104 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1105 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1106 | * @param[in] ident Referencing identity to work with, NULL for identityref. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1107 | * @param[in] bases Array of bases of identityref to fill in. |
| 1108 | * @return LY_ERR value. |
| 1109 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1110 | static LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1111 | lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p, |
| 1112 | struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1113 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1114 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1115 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1116 | struct lys_module *mod; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1117 | struct lysc_ident **idref, *identities; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1118 | |
| 1119 | assert(ident || bases); |
| 1120 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1121 | if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1122 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1123 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1124 | return LY_EVALID; |
| 1125 | } |
| 1126 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1127 | LY_ARRAY_FOR(bases_p, u) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1128 | s = strchr(bases_p[u], ':'); |
| 1129 | if (s) { |
| 1130 | /* prefixed identity */ |
| 1131 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1132 | mod = lys_module_find_prefix(context_module, bases_p[u], s - bases_p[u]); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1133 | } else { |
| 1134 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1135 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1136 | } |
| 1137 | if (!mod) { |
| 1138 | if (ident) { |
| 1139 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1140 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1141 | } else { |
| 1142 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1143 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1144 | } |
| 1145 | return LY_EVALID; |
| 1146 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1147 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1148 | idref = NULL; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1149 | if (mod->compiled) { |
| 1150 | identities = mod->compiled->identities; |
| 1151 | } else { |
| 1152 | identities = mod->dis_identities; |
| 1153 | } |
| 1154 | LY_ARRAY_FOR(identities, v) { |
| 1155 | if (!strcmp(name, identities[v].name)) { |
| 1156 | if (ident) { |
| 1157 | if (ident == &identities[v]) { |
| 1158 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1159 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1160 | return LY_EVALID; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1161 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1162 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived)); |
| 1163 | /* we have match! store the backlink */ |
| 1164 | LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM); |
| 1165 | *idref = ident; |
| 1166 | } else { |
| 1167 | /* we have match! store the found identity */ |
| 1168 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
| 1169 | *idref = &identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1170 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1171 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | if (!idref || !(*idref)) { |
| 1175 | if (ident) { |
| 1176 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1177 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1178 | } else { |
| 1179 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1180 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1181 | } |
| 1182 | return LY_EVALID; |
| 1183 | } |
| 1184 | } |
| 1185 | return LY_SUCCESS; |
| 1186 | } |
| 1187 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1188 | /** |
| 1189 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1190 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1191 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1192 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1193 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1194 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1195 | static LY_ERR |
| 1196 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1197 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1198 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1199 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1200 | lysc_update_path(ctx, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1201 | for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1202 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1203 | continue; |
| 1204 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1205 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1206 | LY_CHECK_RET(lys_compile_identity_bases(ctx, idents[u].module, idents_p[u].bases, &idents[u], NULL)); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1207 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1208 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1209 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1210 | return LY_SUCCESS; |
| 1211 | } |
| 1212 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1213 | LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1214 | lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 1215 | struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1216 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1217 | LY_ARRAY_COUNT_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1218 | struct lysc_ctx context = {0}; |
| 1219 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1220 | assert(ctx_sc || ctx); |
| 1221 | |
| 1222 | if (!ctx_sc) { |
| 1223 | context.ctx = ctx; |
| 1224 | context.mod = module; |
| 1225 | context.path_len = 1; |
| 1226 | context.path[0] = '/'; |
| 1227 | ctx_sc = &context; |
| 1228 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1229 | |
| 1230 | if (!features_p) { |
| 1231 | return LY_SUCCESS; |
| 1232 | } |
| 1233 | if (*features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1234 | offset = LY_ARRAY_COUNT(*features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1235 | } |
| 1236 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1237 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1238 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1239 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1240 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1241 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1242 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1243 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1244 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1245 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1246 | DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1247 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1248 | (*features)[offset + u].module = ctx_sc->mod; |
| 1249 | |
| 1250 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1251 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1252 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1253 | |
| 1254 | return LY_SUCCESS; |
| 1255 | } |
| 1256 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1257 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1258 | * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement). |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1259 | * |
| 1260 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1261 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1262 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1263 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1264 | * being currently processed). |
| 1265 | * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature) |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1266 | * @return LY_SUCCESS if everything is ok. |
| 1267 | * @return LY_EVALID if the feature references indirectly itself. |
| 1268 | */ |
| 1269 | static LY_ERR |
| 1270 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1271 | { |
| 1272 | LY_ERR ret = LY_EVALID; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1273 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1274 | struct ly_set recursion = {0}; |
| 1275 | struct lysc_feature *drv; |
| 1276 | |
| 1277 | if (!depfeatures) { |
| 1278 | return LY_SUCCESS; |
| 1279 | } |
| 1280 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1281 | for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1282 | if (feature == depfeatures[u]) { |
| 1283 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1284 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1285 | goto cleanup; |
| 1286 | } |
| 1287 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1288 | } |
| 1289 | |
| 1290 | for (v = 0; v < recursion.count; ++v) { |
| 1291 | drv = recursion.objs[v]; |
| 1292 | if (!drv->depfeatures) { |
| 1293 | continue; |
| 1294 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1295 | for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1296 | if (feature == drv->depfeatures[u]) { |
| 1297 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1298 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1299 | goto cleanup; |
| 1300 | } |
| 1301 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1302 | } |
| 1303 | } |
| 1304 | ret = LY_SUCCESS; |
| 1305 | |
| 1306 | cleanup: |
| 1307 | ly_set_erase(&recursion, NULL); |
| 1308 | return ret; |
| 1309 | } |
| 1310 | |
| 1311 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1312 | * @brief Create pre-compiled features array. |
| 1313 | * |
| 1314 | * See lys_feature_precompile() for more details. |
| 1315 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1316 | * @param[in] ctx Compile context. |
| 1317 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1318 | * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1319 | * @return LY_ERR value. |
| 1320 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1321 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1322 | lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1323 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1324 | LY_ARRAY_COUNT_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1325 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1326 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1327 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1328 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1329 | /* find the preprecompiled feature */ |
| 1330 | LY_ARRAY_FOR(features, x) { |
| 1331 | if (strcmp(features[x].name, feature_p->name)) { |
| 1332 | continue; |
| 1333 | } |
| 1334 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1335 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1336 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1337 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1338 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1339 | 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] | 1340 | 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] | 1341 | if (feature->iffeatures) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1342 | for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1343 | if (feature->iffeatures[u].features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1344 | for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1345 | /* check for circular dependency - direct reference first,... */ |
| 1346 | if (feature == feature->iffeatures[u].features[v]) { |
| 1347 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1348 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1349 | return LY_EVALID; |
| 1350 | } |
| 1351 | /* ... and indirect circular reference */ |
| 1352 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1353 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1354 | /* add itself into the dependants list */ |
| 1355 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1356 | *df = feature; |
| 1357 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1358 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1359 | } |
| 1360 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1361 | lysc_update_path(ctx, NULL, NULL); |
| 1362 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1363 | done: |
| 1364 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1365 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1366 | |
| 1367 | LOGINT(ctx->ctx); |
| 1368 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1369 | } |
| 1370 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1371 | /** |
| 1372 | * @brief Revert compiled list of features back to the precompiled state. |
| 1373 | * |
| 1374 | * 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] | 1375 | * 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] | 1376 | * |
| 1377 | * @param[in] ctx Compilation context. |
| 1378 | * @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] | 1379 | * and supposed to hold the dis_features list. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1380 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1381 | static void |
| 1382 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1383 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1384 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1385 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 1386 | if (mod->compiled) { |
| 1387 | /* keep the dis_features list until the complete lys_module is freed */ |
| 1388 | mod->dis_features = mod->compiled->features; |
| 1389 | mod->compiled->features = NULL; |
| 1390 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1391 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1392 | /* 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] | 1393 | * which may points into the data being freed here */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1394 | LY_ARRAY_FOR(mod->dis_features, u) { |
| 1395 | LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) { |
| 1396 | lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1397 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1398 | LY_ARRAY_FREE(mod->dis_features[u].iffeatures); |
| 1399 | mod->dis_features[u].iffeatures = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1400 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1401 | LY_ARRAY_FOR(mod->dis_features[u].exts, v) { |
| 1402 | lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1403 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1404 | LY_ARRAY_FREE(mod->dis_features[u].exts); |
| 1405 | mod->dis_features[u].exts = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1406 | } |
| 1407 | } |
| 1408 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1409 | /** |
| 1410 | * @brief Validate and normalize numeric value from a range definition. |
| 1411 | * @param[in] ctx Compile context. |
| 1412 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1413 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1414 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1415 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1416 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1417 | * @param[in] value String value of the range boundary. |
| 1418 | * @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. |
| 1419 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1420 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1421 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1422 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1423 | 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] | 1424 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1425 | size_t fraction = 0, size; |
| 1426 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1427 | *len = 0; |
| 1428 | |
| 1429 | assert(value); |
| 1430 | /* parse value */ |
| 1431 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1432 | return LY_EVALID; |
| 1433 | } |
| 1434 | |
| 1435 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1436 | ++(*len); |
| 1437 | } |
| 1438 | |
| 1439 | while (isdigit(value[*len])) { |
| 1440 | ++(*len); |
| 1441 | } |
| 1442 | |
| 1443 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1444 | if (basetype == LY_TYPE_DEC64) { |
| 1445 | goto decimal; |
| 1446 | } else { |
| 1447 | *valcopy = strndup(value, *len); |
| 1448 | return LY_SUCCESS; |
| 1449 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1450 | } |
| 1451 | fraction = *len; |
| 1452 | |
| 1453 | ++(*len); |
| 1454 | while (isdigit(value[*len])) { |
| 1455 | ++(*len); |
| 1456 | } |
| 1457 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1458 | if (basetype == LY_TYPE_DEC64) { |
| 1459 | decimal: |
| 1460 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1461 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1462 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1463 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1464 | *len, value, frdigits); |
| 1465 | return LY_EINVAL; |
| 1466 | } |
| 1467 | if (fraction) { |
| 1468 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1469 | } else { |
| 1470 | size = (*len) + frdigits + 1; |
| 1471 | } |
| 1472 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1473 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1474 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1475 | (*valcopy)[size - 1] = '\0'; |
| 1476 | if (fraction) { |
| 1477 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1478 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1479 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1480 | } else { |
| 1481 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1482 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1483 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1484 | } |
| 1485 | return LY_SUCCESS; |
| 1486 | } |
| 1487 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1488 | /** |
| 1489 | * @brief Check that values in range are in ascendant order. |
| 1490 | * @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] | 1491 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1492 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1493 | * @param[in] value Current value to check. |
| 1494 | * @param[in] prev_value The last seen value. |
| 1495 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1496 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1497 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1498 | range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1499 | { |
| 1500 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1501 | 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] | 1502 | return LY_EEXIST; |
| 1503 | } |
| 1504 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1505 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1506 | return LY_EEXIST; |
| 1507 | } |
| 1508 | } |
| 1509 | return LY_SUCCESS; |
| 1510 | } |
| 1511 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1512 | /** |
| 1513 | * @brief Set min/max value of the range part. |
| 1514 | * @param[in] ctx Compile context. |
| 1515 | * @param[in] part Range part structure to fill. |
| 1516 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1517 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1518 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1519 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1520 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1521 | * @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] | 1522 | * @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] | 1523 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1524 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1525 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1526 | * frdigits value), LY_EMEM. |
| 1527 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1528 | static LY_ERR |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 1529 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, |
| 1530 | int first, int length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1531 | { |
| 1532 | LY_ERR ret = LY_SUCCESS; |
| 1533 | char *valcopy = NULL; |
| 1534 | size_t len; |
| 1535 | |
| 1536 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1537 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1538 | LY_CHECK_GOTO(ret, finalize); |
| 1539 | } |
| 1540 | if (!valcopy && base_range) { |
| 1541 | if (max) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1542 | 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] | 1543 | } else { |
| 1544 | part->min_64 = base_range->parts[0].min_64; |
| 1545 | } |
| 1546 | if (!first) { |
| 1547 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1548 | } |
| 1549 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1553 | case LY_TYPE_INT8: /* range */ |
| 1554 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1555 | 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] | 1556 | } else if (max) { |
| 1557 | part->max_64 = INT64_C(127); |
| 1558 | } else { |
| 1559 | part->min_64 = INT64_C(-128); |
| 1560 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1561 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1562 | 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] | 1563 | } |
| 1564 | break; |
| 1565 | case LY_TYPE_INT16: /* range */ |
| 1566 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1567 | 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] | 1568 | } else if (max) { |
| 1569 | part->max_64 = INT64_C(32767); |
| 1570 | } else { |
| 1571 | part->min_64 = INT64_C(-32768); |
| 1572 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1573 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1574 | 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] | 1575 | } |
| 1576 | break; |
| 1577 | case LY_TYPE_INT32: /* range */ |
| 1578 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1579 | 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] | 1580 | } else if (max) { |
| 1581 | part->max_64 = INT64_C(2147483647); |
| 1582 | } else { |
| 1583 | part->min_64 = INT64_C(-2147483648); |
| 1584 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1585 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1586 | 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] | 1587 | } |
| 1588 | break; |
| 1589 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1590 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1591 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1592 | 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] | 1593 | max ? &part->max_64 : &part->min_64); |
| 1594 | } else if (max) { |
| 1595 | part->max_64 = INT64_C(9223372036854775807); |
| 1596 | } else { |
| 1597 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1598 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1599 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1600 | 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] | 1601 | } |
| 1602 | break; |
| 1603 | case LY_TYPE_UINT8: /* range */ |
| 1604 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1605 | 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] | 1606 | } else if (max) { |
| 1607 | part->max_u64 = UINT64_C(255); |
| 1608 | } else { |
| 1609 | part->min_u64 = UINT64_C(0); |
| 1610 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1611 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1612 | 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] | 1613 | } |
| 1614 | break; |
| 1615 | case LY_TYPE_UINT16: /* range */ |
| 1616 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1617 | 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] | 1618 | } else if (max) { |
| 1619 | part->max_u64 = UINT64_C(65535); |
| 1620 | } else { |
| 1621 | part->min_u64 = UINT64_C(0); |
| 1622 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1623 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1624 | 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] | 1625 | } |
| 1626 | break; |
| 1627 | case LY_TYPE_UINT32: /* range */ |
| 1628 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1629 | 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] | 1630 | } else if (max) { |
| 1631 | part->max_u64 = UINT64_C(4294967295); |
| 1632 | } else { |
| 1633 | part->min_u64 = UINT64_C(0); |
| 1634 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1635 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1636 | 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] | 1637 | } |
| 1638 | break; |
| 1639 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1640 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1641 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1642 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1643 | 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] | 1644 | } else if (max) { |
| 1645 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1646 | } else { |
| 1647 | part->min_u64 = UINT64_C(0); |
| 1648 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1649 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1650 | 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] | 1651 | } |
| 1652 | break; |
| 1653 | default: |
| 1654 | LOGINT(ctx->ctx); |
| 1655 | ret = LY_EINT; |
| 1656 | } |
| 1657 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1658 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1659 | if (ret == LY_EDENIED) { |
| 1660 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1661 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1662 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1663 | } else if (ret == LY_EVALID) { |
| 1664 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1665 | "Invalid %s restriction - invalid value \"%s\".", |
| 1666 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1667 | } else if (ret == LY_EEXIST) { |
| 1668 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1669 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1670 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1671 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1672 | } else if (!ret && value) { |
| 1673 | *value = *value + len; |
| 1674 | } |
| 1675 | free(valcopy); |
| 1676 | return ret; |
| 1677 | } |
| 1678 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1679 | /** |
| 1680 | * @brief Compile the parsed range restriction. |
| 1681 | * @param[in] ctx Compile context. |
| 1682 | * @param[in] range_p Parsed range structure to compile. |
| 1683 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1684 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1685 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1686 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1687 | * range restriction must be more restrictive than the base_range. |
| 1688 | * @param[in,out] range Pointer to the created current range structure. |
| 1689 | * @return LY_ERR value. |
| 1690 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1691 | static LY_ERR |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 1692 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, |
| 1693 | uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1694 | { |
| 1695 | LY_ERR ret = LY_EVALID; |
| 1696 | const char *expr; |
| 1697 | struct lysc_range_part *parts = NULL, *part; |
| 1698 | int range_expected = 0, uns; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1699 | LY_ARRAY_COUNT_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1700 | |
| 1701 | assert(range); |
| 1702 | assert(range_p); |
| 1703 | |
| 1704 | expr = range_p->arg; |
| 1705 | while(1) { |
| 1706 | if (isspace(*expr)) { |
| 1707 | ++expr; |
| 1708 | } else if (*expr == '\0') { |
| 1709 | if (range_expected) { |
| 1710 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1711 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1712 | length_restr ? "length" : "range", range_p->arg); |
| 1713 | goto cleanup; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1714 | } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1715 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1716 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1717 | length_restr ? "length" : "range", range_p->arg); |
| 1718 | goto cleanup; |
| 1719 | } |
| 1720 | parts_done++; |
| 1721 | break; |
| 1722 | } else if (!strncmp(expr, "min", 3)) { |
| 1723 | if (parts) { |
| 1724 | /* min cannot be used elsewhere than in the first part */ |
| 1725 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1726 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1727 | expr - range_p->arg, range_p->arg); |
| 1728 | goto cleanup; |
| 1729 | } |
| 1730 | expr += 3; |
| 1731 | |
| 1732 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1733 | 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] | 1734 | part->max_64 = part->min_64; |
| 1735 | } else if (*expr == '|') { |
| 1736 | if (!parts || range_expected) { |
| 1737 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1738 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1739 | goto cleanup; |
| 1740 | } |
| 1741 | expr++; |
| 1742 | parts_done++; |
| 1743 | /* process next part of the expression */ |
| 1744 | } else if (!strncmp(expr, "..", 2)) { |
| 1745 | expr += 2; |
| 1746 | while (isspace(*expr)) { |
| 1747 | expr++; |
| 1748 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1749 | if (!parts || LY_ARRAY_COUNT(parts) == parts_done) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1750 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1751 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1752 | goto cleanup; |
| 1753 | } |
| 1754 | /* continue expecting the upper boundary */ |
| 1755 | range_expected = 1; |
| 1756 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1757 | /* number */ |
| 1758 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1759 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1760 | 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] | 1761 | range_expected = 0; |
| 1762 | } else { |
| 1763 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1764 | 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] | 1765 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1766 | part->max_64 = part->min_64; |
| 1767 | } |
| 1768 | |
| 1769 | /* continue with possible another expression part */ |
| 1770 | } else if (!strncmp(expr, "max", 3)) { |
| 1771 | expr += 3; |
| 1772 | while (isspace(*expr)) { |
| 1773 | expr++; |
| 1774 | } |
| 1775 | if (*expr != '\0') { |
| 1776 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1777 | length_restr ? "length" : "range", expr); |
| 1778 | goto cleanup; |
| 1779 | } |
| 1780 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1781 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1782 | 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] | 1783 | range_expected = 0; |
| 1784 | } else { |
| 1785 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1786 | 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] | 1787 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1788 | part->min_64 = part->max_64; |
| 1789 | } |
| 1790 | } else { |
| 1791 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1792 | length_restr ? "length" : "range", expr); |
| 1793 | goto cleanup; |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | /* check with the previous range/length restriction */ |
| 1798 | if (base_range) { |
| 1799 | switch (basetype) { |
| 1800 | case LY_TYPE_BINARY: |
| 1801 | case LY_TYPE_UINT8: |
| 1802 | case LY_TYPE_UINT16: |
| 1803 | case LY_TYPE_UINT32: |
| 1804 | case LY_TYPE_UINT64: |
| 1805 | case LY_TYPE_STRING: |
| 1806 | uns = 1; |
| 1807 | break; |
| 1808 | case LY_TYPE_DEC64: |
| 1809 | case LY_TYPE_INT8: |
| 1810 | case LY_TYPE_INT16: |
| 1811 | case LY_TYPE_INT32: |
| 1812 | case LY_TYPE_INT64: |
| 1813 | uns = 0; |
| 1814 | break; |
| 1815 | default: |
| 1816 | LOGINT(ctx->ctx); |
| 1817 | ret = LY_EINT; |
| 1818 | goto cleanup; |
| 1819 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1820 | 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] | 1821 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1822 | goto baseerror; |
| 1823 | } |
| 1824 | /* current lower bound is not lower than the base */ |
| 1825 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1826 | /* base has single value */ |
| 1827 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1828 | /* both lower bounds are the same */ |
| 1829 | if (parts[u].min_64 != parts[u].max_64) { |
| 1830 | /* current continues with a range */ |
| 1831 | goto baseerror; |
| 1832 | } else { |
| 1833 | /* equal single values, move both forward */ |
| 1834 | ++v; |
| 1835 | continue; |
| 1836 | } |
| 1837 | } else { |
| 1838 | /* base is single value lower than current range, so the |
| 1839 | * value from base range is removed in the current, |
| 1840 | * move only base and repeat checking */ |
| 1841 | ++v; |
| 1842 | --u; |
| 1843 | continue; |
| 1844 | } |
| 1845 | } else { |
| 1846 | /* base is the range */ |
| 1847 | if (parts[u].min_64 == parts[u].max_64) { |
| 1848 | /* current is a single value */ |
| 1849 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1850 | /* current is behind the base range, so base range is omitted, |
| 1851 | * move the base and keep the current for further check */ |
| 1852 | ++v; |
| 1853 | --u; |
| 1854 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1855 | continue; |
| 1856 | } else { |
| 1857 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1858 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1859 | /* higher bound is higher than the current higher bound */ |
| 1860 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1861 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1862 | * continue with the same current, but move the base */ |
| 1863 | --u; |
| 1864 | ++v; |
| 1865 | continue; |
| 1866 | } |
| 1867 | /* current range starts within the base range but end behind it */ |
| 1868 | goto baseerror; |
| 1869 | } else { |
| 1870 | /* current range is smaller than the base, |
| 1871 | * move current, but stay with the base */ |
| 1872 | continue; |
| 1873 | } |
| 1874 | } |
| 1875 | } |
| 1876 | } |
| 1877 | if (u != parts_done) { |
| 1878 | baseerror: |
| 1879 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1880 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1881 | length_restr ? "length" : "range", range_p->arg); |
| 1882 | goto cleanup; |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | if (!(*range)) { |
| 1887 | *range = calloc(1, sizeof **range); |
| 1888 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1889 | } |
| 1890 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1891 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1892 | if (range_p->eapptag) { |
| 1893 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1894 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1895 | } |
| 1896 | if (range_p->emsg) { |
| 1897 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1898 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1899 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1900 | if (range_p->dsc) { |
| 1901 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1902 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1903 | } |
| 1904 | if (range_p->ref) { |
| 1905 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1906 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1907 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1908 | /* extensions are taken only from the last range by the caller */ |
| 1909 | |
| 1910 | (*range)->parts = parts; |
| 1911 | parts = NULL; |
| 1912 | ret = LY_SUCCESS; |
| 1913 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1914 | LY_ARRAY_FREE(parts); |
| 1915 | |
| 1916 | return ret; |
| 1917 | } |
| 1918 | |
| 1919 | /** |
| 1920 | * @brief Checks pattern syntax. |
| 1921 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1922 | * @param[in] ctx Context. |
| 1923 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1924 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1925 | * @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] | 1926 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1927 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1928 | LY_ERR |
| 1929 | 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] | 1930 | { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1931 | int idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1932 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1933 | int err_code; |
| 1934 | const char *orig_ptr; |
| 1935 | PCRE2_SIZE err_offset; |
| 1936 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1937 | #define URANGE_LEN 19 |
| 1938 | char *ublock2urange[][2] = { |
| 1939 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1940 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1941 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1942 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1943 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1944 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1945 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1946 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1947 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1948 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1949 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1950 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1951 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1952 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1953 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1954 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1955 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1956 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1957 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1958 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1959 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1960 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1961 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1962 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1963 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1964 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1965 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1966 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1967 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1968 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1969 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1970 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1971 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1972 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1973 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1974 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1975 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1976 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1977 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1978 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1979 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1980 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1981 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1982 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1983 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1984 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1985 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1986 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1987 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1988 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1989 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1990 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1991 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1992 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1993 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1994 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1995 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1996 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1997 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1998 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1999 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 2000 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 2001 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 2002 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 2003 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 2004 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 2005 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 2006 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 2007 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 2008 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 2009 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 2010 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 2011 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 2012 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 2013 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 2014 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 2015 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 2016 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 2017 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 2018 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 2019 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 2020 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 2021 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 2022 | {NULL, NULL} |
| 2023 | }; |
| 2024 | |
| 2025 | /* adjust the expression to a Perl equivalent |
| 2026 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 2027 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2028 | /* allocate space for the transformed pattern */ |
| 2029 | size = strlen(pattern) + 1; |
| 2030 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2031 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2032 | perl_regex[0] = '\0'; |
| 2033 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2034 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 2035 | brack = 0; |
| 2036 | idx = 0; |
| 2037 | orig_ptr = pattern; |
| 2038 | while (orig_ptr[0]) { |
| 2039 | switch (orig_ptr[0]) { |
| 2040 | case '$': |
| 2041 | case '^': |
| 2042 | if (!brack) { |
| 2043 | /* make space for the extra character */ |
| 2044 | ++size; |
| 2045 | perl_regex = ly_realloc(perl_regex, size); |
| 2046 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2047 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2048 | /* print escape slash */ |
| 2049 | perl_regex[idx] = '\\'; |
| 2050 | ++idx; |
| 2051 | } |
| 2052 | break; |
| 2053 | case '[': |
| 2054 | /* must not be escaped */ |
| 2055 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2056 | ++brack; |
| 2057 | } |
| 2058 | break; |
| 2059 | case ']': |
| 2060 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2061 | /* pattern was checked and compiled already */ |
| 2062 | assert(brack); |
| 2063 | --brack; |
| 2064 | } |
| 2065 | break; |
| 2066 | default: |
| 2067 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2068 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2069 | |
| 2070 | /* copy char */ |
| 2071 | perl_regex[idx] = orig_ptr[0]; |
| 2072 | |
| 2073 | ++idx; |
| 2074 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2075 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2076 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2077 | |
| 2078 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2079 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2080 | start = ptr - perl_regex; |
| 2081 | |
| 2082 | ptr = strchr(ptr, '}'); |
| 2083 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2084 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2085 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 2086 | free(perl_regex); |
| 2087 | return LY_EVALID; |
| 2088 | } |
| 2089 | end = (ptr - perl_regex) + 1; |
| 2090 | |
| 2091 | /* need more space */ |
| 2092 | if (end - start < URANGE_LEN) { |
| 2093 | 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] | 2094 | 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] | 2095 | } |
| 2096 | |
| 2097 | /* find our range */ |
| 2098 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2099 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2100 | break; |
| 2101 | } |
| 2102 | } |
| 2103 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2104 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2105 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2106 | free(perl_regex); |
| 2107 | return LY_EVALID; |
| 2108 | } |
| 2109 | |
| 2110 | /* 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] | 2111 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2112 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2113 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2114 | } |
| 2115 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2116 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2117 | } |
| 2118 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2119 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2120 | /* skip brackets */ |
| 2121 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2122 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2123 | } else { |
| 2124 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2125 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2126 | } |
| 2127 | } |
| 2128 | |
| 2129 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2130 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2131 | 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] | 2132 | &err_code, &err_offset, NULL); |
| 2133 | if (!code_local) { |
| 2134 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2135 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2136 | 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] | 2137 | free(perl_regex); |
| 2138 | return LY_EVALID; |
| 2139 | } |
| 2140 | free(perl_regex); |
| 2141 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2142 | if (code) { |
| 2143 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2144 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2145 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | return LY_SUCCESS; |
| 2149 | |
| 2150 | #undef URANGE_LEN |
| 2151 | } |
| 2152 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2153 | /** |
| 2154 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2155 | * @param[in] ctx Compile context. |
| 2156 | * @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] | 2157 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2158 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2159 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2160 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2161 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2162 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2163 | lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2164 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 2165 | { |
| 2166 | struct lysc_pattern **pattern; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2167 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2168 | LY_ERR ret = LY_SUCCESS; |
| 2169 | |
| 2170 | /* first, copy the patterns from the base type */ |
| 2171 | if (base_patterns) { |
| 2172 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2173 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2174 | } |
| 2175 | |
| 2176 | LY_ARRAY_FOR(patterns_p, u) { |
| 2177 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2178 | *pattern = calloc(1, sizeof **pattern); |
| 2179 | ++(*pattern)->refcount; |
| 2180 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2181 | 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] | 2182 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2183 | |
| 2184 | if (patterns_p[u].arg[0] == 0x15) { |
| 2185 | (*pattern)->inverted = 1; |
| 2186 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2187 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2188 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2189 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2190 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2191 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2192 | 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] | 2193 | } |
| 2194 | done: |
| 2195 | return ret; |
| 2196 | } |
| 2197 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2198 | /** |
| 2199 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2200 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2201 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2202 | 0 /* LY_TYPE_UNKNOWN */, |
| 2203 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2204 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2205 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2206 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2207 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2208 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2209 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2210 | 0 /* LY_TYPE_BOOL */, |
| 2211 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2212 | 0 /* LY_TYPE_EMPTY */, |
| 2213 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2214 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2215 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2216 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2217 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2218 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2219 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2220 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2221 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2222 | }; |
| 2223 | |
| 2224 | /** |
| 2225 | * @brief stringification of the YANG built-in data types |
| 2226 | */ |
| 2227 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2228 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2229 | "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] | 2230 | }; |
| 2231 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2232 | /** |
| 2233 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2234 | * @param[in] ctx Compile context. |
| 2235 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2236 | * @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] | 2237 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2238 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2239 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2240 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2241 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2242 | lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2243 | 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] | 2244 | { |
| 2245 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2246 | LY_ARRAY_COUNT_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2247 | int32_t value = 0; |
| 2248 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2249 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2250 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2251 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2252 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2253 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2254 | return LY_EVALID; |
| 2255 | } |
| 2256 | |
| 2257 | LY_ARRAY_FOR(enums_p, u) { |
| 2258 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2259 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2260 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2261 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2262 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2263 | if (base_enums) { |
| 2264 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2265 | LY_ARRAY_FOR(base_enums, v) { |
| 2266 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2267 | break; |
| 2268 | } |
| 2269 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2270 | if (v == LY_ARRAY_COUNT(base_enums)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2271 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2272 | "Invalid %s - derived type adds new item \"%s\".", |
| 2273 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2274 | return LY_EVALID; |
| 2275 | } |
| 2276 | match = v; |
| 2277 | } |
| 2278 | |
| 2279 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2280 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2281 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2282 | e->value = (int32_t)enums_p[u].value; |
| 2283 | if (!u || e->value >= value) { |
| 2284 | value = e->value + 1; |
| 2285 | } |
| 2286 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2287 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2288 | if (e->value == (*enums)[v].value) { |
| 2289 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2290 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2291 | e->value, e->name, (*enums)[v].name); |
| 2292 | return LY_EVALID; |
| 2293 | } |
| 2294 | } |
| 2295 | } else if (base_enums) { |
| 2296 | /* inherit the assigned value */ |
| 2297 | e->value = base_enums[match].value; |
| 2298 | if (!u || e->value >= value) { |
| 2299 | value = e->value + 1; |
| 2300 | } |
| 2301 | } else { |
| 2302 | /* assign value automatically */ |
| 2303 | if (u && value == INT32_MIN) { |
| 2304 | /* counter overflow */ |
| 2305 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2306 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2307 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2308 | return LY_EVALID; |
| 2309 | } |
| 2310 | e->value = value++; |
| 2311 | } |
| 2312 | } else { /* LY_TYPE_BITS */ |
| 2313 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2314 | e->value = (int32_t)enums_p[u].value; |
| 2315 | if (!u || (uint32_t)e->value >= position) { |
| 2316 | position = (uint32_t)e->value + 1; |
| 2317 | } |
| 2318 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2319 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2320 | if (e->value == (*enums)[v].value) { |
| 2321 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2322 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2323 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2324 | return LY_EVALID; |
| 2325 | } |
| 2326 | } |
| 2327 | } else if (base_enums) { |
| 2328 | /* inherit the assigned value */ |
| 2329 | e->value = base_enums[match].value; |
| 2330 | if (!u || (uint32_t)e->value >= position) { |
| 2331 | position = (uint32_t)e->value + 1; |
| 2332 | } |
| 2333 | } else { |
| 2334 | /* assign value automatically */ |
| 2335 | if (u && position == 0) { |
| 2336 | /* counter overflow */ |
| 2337 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2338 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2339 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2340 | return LY_EVALID; |
| 2341 | } |
| 2342 | e->value = position++; |
| 2343 | } |
| 2344 | } |
| 2345 | |
| 2346 | if (base_enums) { |
| 2347 | /* the assigned values must not change from the derived type */ |
| 2348 | if (e->value != base_enums[match].value) { |
| 2349 | if (basetype == LY_TYPE_ENUM) { |
| 2350 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2351 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2352 | e->name, base_enums[match].value, e->value); |
| 2353 | } else { |
| 2354 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2355 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2356 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2357 | } |
| 2358 | return LY_EVALID; |
| 2359 | } |
| 2360 | } |
| 2361 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2362 | 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] | 2363 | 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] | 2364 | |
| 2365 | if (basetype == LY_TYPE_BITS) { |
| 2366 | /* keep bits ordered by position */ |
| 2367 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2368 | if (v != u) { |
| 2369 | memcpy(&storage, e, sizeof *e); |
| 2370 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2371 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2372 | } |
| 2373 | } |
| 2374 | } |
| 2375 | |
| 2376 | done: |
| 2377 | return ret; |
| 2378 | } |
| 2379 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2380 | /** |
| 2381 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2382 | * |
| 2383 | * path-arg = absolute-path / relative-path |
| 2384 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2385 | * relative-path = 1*(".." "/") descendant-path |
| 2386 | * |
| 2387 | * @param[in,out] path Path to parse. |
| 2388 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2389 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2390 | * @param[out] name Name of the token. |
| 2391 | * @param[out] nam_len Length of the name. |
| 2392 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2393 | * must not be changed between consecutive calls. -1 if the |
| 2394 | * path is absolute. |
| 2395 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2396 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2397 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2398 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2399 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2400 | int *parent_times, int *has_predicate) |
| 2401 | { |
| 2402 | int par_times = 0; |
| 2403 | |
| 2404 | assert(path && *path); |
| 2405 | assert(parent_times); |
| 2406 | assert(prefix); |
| 2407 | assert(prefix_len); |
| 2408 | assert(name); |
| 2409 | assert(name_len); |
| 2410 | assert(has_predicate); |
| 2411 | |
| 2412 | *prefix = NULL; |
| 2413 | *prefix_len = 0; |
| 2414 | *name = NULL; |
| 2415 | *name_len = 0; |
| 2416 | *has_predicate = 0; |
| 2417 | |
| 2418 | if (!*parent_times) { |
| 2419 | if (!strncmp(*path, "..", 2)) { |
| 2420 | *path += 2; |
| 2421 | ++par_times; |
| 2422 | while (!strncmp(*path, "/..", 3)) { |
| 2423 | *path += 3; |
| 2424 | ++par_times; |
| 2425 | } |
| 2426 | } |
| 2427 | if (par_times) { |
| 2428 | *parent_times = par_times; |
| 2429 | } else { |
| 2430 | *parent_times = -1; |
| 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | if (**path != '/') { |
| 2435 | return LY_EINVAL; |
| 2436 | } |
| 2437 | /* skip '/' */ |
| 2438 | ++(*path); |
| 2439 | |
| 2440 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2441 | 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] | 2442 | |
| 2443 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2444 | /* path continues by another token or this is the last token */ |
| 2445 | return LY_SUCCESS; |
| 2446 | } else if ((*path)[0] != '[') { |
| 2447 | /* unexpected character */ |
| 2448 | return LY_EINVAL; |
| 2449 | } else { |
| 2450 | /* predicate starting with [ */ |
| 2451 | *has_predicate = 1; |
| 2452 | return LY_SUCCESS; |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2457 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2458 | * |
| 2459 | * The set of features used for target must be a subset of features used for the leafref. |
| 2460 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2461 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2462 | * |
| 2463 | * @param[in] refnode The leafref node. |
| 2464 | * @param[in] target Tha target node of the leafref. |
| 2465 | * @return LY_SUCCESS or LY_EVALID; |
| 2466 | */ |
| 2467 | static LY_ERR |
| 2468 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2469 | { |
| 2470 | LY_ERR ret = LY_EVALID; |
| 2471 | const struct lysc_node *iter; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2472 | LY_ARRAY_COUNT_TYPE u, v, count; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2473 | struct ly_set features = {0}; |
| 2474 | |
| 2475 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2476 | if (iter->iffeatures) { |
| 2477 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2478 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2479 | LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup); |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2480 | } |
| 2481 | } |
| 2482 | } |
| 2483 | } |
| 2484 | |
| 2485 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2486 | * So when adding features applicable to the target into the features set, we should not be |
| 2487 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2488 | * to the leafref itself. */ |
| 2489 | count = features.count; |
| 2490 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2491 | if (iter->iffeatures) { |
| 2492 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2493 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2494 | if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) { |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2495 | /* new feature was added (or LY_EMEM) */ |
| 2496 | goto cleanup; |
| 2497 | } |
| 2498 | } |
| 2499 | } |
| 2500 | } |
| 2501 | } |
| 2502 | ret = LY_SUCCESS; |
| 2503 | |
| 2504 | cleanup: |
| 2505 | ly_set_erase(&features, NULL); |
| 2506 | return ret; |
| 2507 | } |
| 2508 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2509 | static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2510 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2511 | 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] | 2512 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2513 | /** |
| 2514 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2515 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2516 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2517 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2518 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2519 | * @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] | 2520 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2521 | * @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] | 2522 | * @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] | 2523 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2524 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2525 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2526 | * @return LY_ERR value. |
| 2527 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2528 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2529 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2530 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2531 | struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, |
| 2532 | struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2533 | { |
| 2534 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2535 | struct lysc_type_bin *bin; |
| 2536 | struct lysc_type_num *num; |
| 2537 | struct lysc_type_str *str; |
| 2538 | struct lysc_type_bits *bits; |
| 2539 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2540 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2541 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2542 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2543 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2544 | |
| 2545 | switch (basetype) { |
| 2546 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2547 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2548 | |
| 2549 | /* 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] | 2550 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2551 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2552 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2553 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2554 | 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] | 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | if (tpdfname) { |
| 2559 | type_p->compiled = *type; |
| 2560 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2561 | } |
| 2562 | break; |
| 2563 | case LY_TYPE_BITS: |
| 2564 | /* RFC 7950 9.7 - bits */ |
| 2565 | bits = (struct lysc_type_bits*)(*type); |
| 2566 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2567 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2568 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2569 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2570 | } |
| 2571 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2572 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2573 | /* 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] | 2574 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2575 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2576 | } else { |
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", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2578 | free(*type); |
| 2579 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2580 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2581 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | if (tpdfname) { |
| 2585 | type_p->compiled = *type; |
| 2586 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2587 | } |
| 2588 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2589 | case LY_TYPE_DEC64: |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2590 | dec = (struct lysc_type_dec *)(*type); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2591 | |
| 2592 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2593 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2594 | if (!type_p->fraction_digits) { |
| 2595 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2596 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2597 | } else { |
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", ""); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2599 | free(*type); |
| 2600 | *type = NULL; |
| 2601 | } |
| 2602 | return LY_EVALID; |
| 2603 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2604 | dec->fraction_digits = type_p->fraction_digits; |
| 2605 | } else { |
| 2606 | if (type_p->fraction_digits) { |
| 2607 | /* fraction digits is prohibited in types not directly derived from built-in decimal64 */ |
| 2608 | if (tpdfname) { |
| 2609 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2610 | "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.", |
| 2611 | tpdfname); |
| 2612 | } else { |
| 2613 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2614 | "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type."); |
| 2615 | free(*type); |
| 2616 | *type = NULL; |
| 2617 | } |
| 2618 | return LY_EVALID; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2619 | } |
Radek Krejci | 115a74d | 2020-08-14 22:18:12 +0200 | [diff] [blame] | 2620 | dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2621 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2622 | |
| 2623 | /* RFC 7950 9.2.4 - range */ |
| 2624 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2625 | 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] | 2626 | base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2627 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2628 | 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] | 2629 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | if (tpdfname) { |
| 2633 | type_p->compiled = *type; |
| 2634 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2635 | } |
| 2636 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2637 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2638 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2639 | |
| 2640 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2641 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2642 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2643 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2644 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2645 | 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] | 2646 | } |
| 2647 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2648 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2649 | } |
| 2650 | |
| 2651 | /* RFC 7950 9.4.5 - pattern */ |
| 2652 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2653 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2654 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2655 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2656 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2657 | } |
| 2658 | |
| 2659 | if (tpdfname) { |
| 2660 | type_p->compiled = *type; |
| 2661 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2662 | } |
| 2663 | break; |
| 2664 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2665 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2666 | |
| 2667 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2668 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2669 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2670 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2671 | } |
| 2672 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2673 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2674 | /* 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] | 2675 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2676 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2677 | } else { |
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", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2679 | free(*type); |
| 2680 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2681 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2682 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | if (tpdfname) { |
| 2686 | type_p->compiled = *type; |
| 2687 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2688 | } |
| 2689 | break; |
| 2690 | case LY_TYPE_INT8: |
| 2691 | case LY_TYPE_UINT8: |
| 2692 | case LY_TYPE_INT16: |
| 2693 | case LY_TYPE_UINT16: |
| 2694 | case LY_TYPE_INT32: |
| 2695 | case LY_TYPE_UINT32: |
| 2696 | case LY_TYPE_INT64: |
| 2697 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2698 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2699 | |
| 2700 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2701 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2702 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2703 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2704 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2705 | 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] | 2706 | } |
| 2707 | } |
| 2708 | |
| 2709 | if (tpdfname) { |
| 2710 | type_p->compiled = *type; |
| 2711 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2712 | } |
| 2713 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2714 | case LY_TYPE_IDENT: |
| 2715 | idref = (struct lysc_type_identityref*)(*type); |
| 2716 | |
| 2717 | /* RFC 7950 9.10.2 - base */ |
| 2718 | if (type_p->bases) { |
| 2719 | if (base) { |
| 2720 | /* only the directly derived identityrefs can contain base specification */ |
| 2721 | if (tpdfname) { |
| 2722 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2723 | "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] | 2724 | tpdfname); |
| 2725 | } else { |
| 2726 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2727 | "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] | 2728 | free(*type); |
| 2729 | *type = NULL; |
| 2730 | } |
| 2731 | return LY_EVALID; |
| 2732 | } |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2733 | 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] | 2734 | } |
| 2735 | |
| 2736 | if (!base && !type_p->flags) { |
| 2737 | /* type derived from identityref built-in type must contain at least one base */ |
| 2738 | if (tpdfname) { |
| 2739 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2740 | } else { |
| 2741 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2742 | free(*type); |
| 2743 | *type = NULL; |
| 2744 | } |
| 2745 | return LY_EVALID; |
| 2746 | } |
| 2747 | |
| 2748 | if (tpdfname) { |
| 2749 | type_p->compiled = *type; |
| 2750 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2751 | } |
| 2752 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2753 | case LY_TYPE_LEAFREF: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2754 | lref = (struct lysc_type_leafref*)*type; |
| 2755 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2756 | /* RFC 7950 9.9.3 - require-instance */ |
| 2757 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2758 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2759 | if (tpdfname) { |
| 2760 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2761 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2762 | } else { |
| 2763 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2764 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2765 | free(*type); |
| 2766 | *type = NULL; |
| 2767 | } |
| 2768 | return LY_EVALID; |
| 2769 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2770 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2771 | } else if (base) { |
| 2772 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2773 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2774 | } else { |
| 2775 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2776 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2777 | } |
| 2778 | if (type_p->path) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2779 | lref->path = lyxp_expr_dup(ctx->ctx, type_p->path); |
| 2780 | lref->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2781 | } else if (base) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2782 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path); |
| 2783 | lref->path_context = ((struct lysc_type_leafref*)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2784 | } else if (tpdfname) { |
| 2785 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2786 | return LY_EVALID; |
| 2787 | } else { |
| 2788 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2789 | free(*type); |
| 2790 | *type = NULL; |
| 2791 | return LY_EVALID; |
| 2792 | } |
| 2793 | if (tpdfname) { |
| 2794 | type_p->compiled = *type; |
| 2795 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2796 | } |
| 2797 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2798 | case LY_TYPE_INST: |
| 2799 | /* RFC 7950 9.9.3 - require-instance */ |
| 2800 | if (type_p->flags & LYS_SET_REQINST) { |
| 2801 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2802 | } else { |
| 2803 | /* default is true */ |
| 2804 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2805 | } |
| 2806 | |
| 2807 | if (tpdfname) { |
| 2808 | type_p->compiled = *type; |
| 2809 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2810 | } |
| 2811 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2812 | case LY_TYPE_UNION: |
| 2813 | un = (struct lysc_type_union*)(*type); |
| 2814 | |
| 2815 | /* RFC 7950 7.4 - type */ |
| 2816 | if (type_p->types) { |
| 2817 | if (base) { |
| 2818 | /* only the directly derived union can contain types specification */ |
| 2819 | if (tpdfname) { |
| 2820 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2821 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2822 | tpdfname); |
| 2823 | } else { |
| 2824 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2825 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2826 | free(*type); |
| 2827 | *type = NULL; |
| 2828 | } |
| 2829 | return LY_EVALID; |
| 2830 | } |
| 2831 | /* compile the type */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2832 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID); |
| 2833 | 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] | 2834 | 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] | 2835 | &type_p->types[u], &un->types[u + additional], NULL, NULL, NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2836 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2837 | /* add space for additional types from the union subtype */ |
| 2838 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2839 | LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t*)(type_p->types) - 1)) + additional + LY_ARRAY_COUNT(un_aux->types) - 1, |
Radek Krejci | c4fa029 | 2020-05-14 10:54:49 +0200 | [diff] [blame] | 2840 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2841 | |
| 2842 | /* copy subtypes of the subtype union */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2843 | 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] | 2844 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2845 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2846 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2847 | 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] | 2848 | lref = (struct lysc_type_leafref *)un->types[u + additional]; |
| 2849 | |
| 2850 | lref->basetype = LY_TYPE_LEAFREF; |
| 2851 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path); |
| 2852 | lref->refcount = 1; |
| 2853 | lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2854 | 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] | 2855 | /* TODO extensions */ |
| 2856 | |
| 2857 | } else { |
| 2858 | un->types[u + additional] = un_aux->types[v]; |
| 2859 | ++un_aux->types[v]->refcount; |
| 2860 | } |
| 2861 | ++additional; |
| 2862 | LY_ARRAY_INCREMENT(un->types); |
| 2863 | } |
| 2864 | /* compensate u increment in main loop */ |
| 2865 | --additional; |
| 2866 | |
| 2867 | /* free the replaced union subtype */ |
| 2868 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2869 | } else { |
| 2870 | LY_ARRAY_INCREMENT(un->types); |
| 2871 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2872 | } |
| 2873 | } |
| 2874 | |
| 2875 | if (!base && !type_p->flags) { |
| 2876 | /* type derived from union built-in type must contain at least one type */ |
| 2877 | if (tpdfname) { |
| 2878 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2879 | } else { |
| 2880 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2881 | free(*type); |
| 2882 | *type = NULL; |
| 2883 | } |
| 2884 | return LY_EVALID; |
| 2885 | } |
| 2886 | |
| 2887 | if (tpdfname) { |
| 2888 | type_p->compiled = *type; |
| 2889 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2890 | } |
| 2891 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2892 | case LY_TYPE_BOOL: |
| 2893 | case LY_TYPE_EMPTY: |
| 2894 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2895 | break; |
| 2896 | } |
| 2897 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2898 | done: |
| 2899 | return ret; |
| 2900 | } |
| 2901 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2902 | /** |
| 2903 | * @brief Compile information about the leaf/leaf-list's type. |
| 2904 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2905 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2906 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2907 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2908 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2909 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2910 | * @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] | 2911 | * @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] | 2912 | * @param[out] dflt Default value for the type. |
| 2913 | * @param[out] dflt_mod Local module for the default value. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2914 | * @return LY_ERR value. |
| 2915 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2916 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2917 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2918 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2919 | 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] | 2920 | { |
| 2921 | LY_ERR ret = LY_SUCCESS; |
| 2922 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2923 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2924 | struct type_context { |
| 2925 | const struct lysp_tpdf *tpdf; |
| 2926 | struct lysp_node *node; |
| 2927 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2928 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2929 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2930 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2931 | struct ly_set tpdf_chain = {0}; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2932 | |
| 2933 | assert((dflt && dflt_mod) || (!dflt && !dflt_mod)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2934 | |
| 2935 | (*type) = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2936 | if (dflt) { |
| 2937 | *dflt = NULL; |
| 2938 | *dflt_mod = NULL; |
| 2939 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2940 | |
| 2941 | tctx = calloc(1, sizeof *tctx); |
| 2942 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2943 | 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] | 2944 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2945 | ret == LY_SUCCESS; |
| 2946 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2947 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2948 | if (basetype) { |
| 2949 | break; |
| 2950 | } |
| 2951 | |
| 2952 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2953 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2954 | 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] | 2955 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2956 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2957 | if (units && !*units) { |
| 2958 | /* inherit units */ |
| 2959 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2960 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2961 | if (dflt && !*dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2962 | /* inherit default */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2963 | *dflt = tctx->tpdf->dflt; |
| 2964 | *dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2965 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2966 | if (dummyloops && (!units || *units) && dflt && *dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2967 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 2968 | break; |
| 2969 | } |
| 2970 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2971 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2972 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2973 | * 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] | 2974 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2975 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2976 | if ((units && !*units) || (dflt && !*dflt)) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2977 | dummyloops = 1; |
| 2978 | goto preparenext; |
| 2979 | } else { |
| 2980 | tctx = NULL; |
| 2981 | break; |
| 2982 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2983 | } |
| 2984 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2985 | /* circular typedef reference detection */ |
| 2986 | for (u = 0; u < tpdf_chain.count; u++) { |
| 2987 | /* local part */ |
| 2988 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 2989 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2990 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2991 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2992 | free(tctx); |
| 2993 | ret = LY_EVALID; |
| 2994 | goto cleanup; |
| 2995 | } |
| 2996 | } |
| 2997 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 2998 | /* global part for unions corner case */ |
| 2999 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 3000 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3001 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3002 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3003 | free(tctx); |
| 3004 | ret = LY_EVALID; |
| 3005 | goto cleanup; |
| 3006 | } |
| 3007 | } |
| 3008 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3009 | /* store information for the following processing */ |
| 3010 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3011 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3012 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3013 | /* prepare next loop */ |
| 3014 | tctx_prev = tctx; |
| 3015 | tctx = calloc(1, sizeof *tctx); |
| 3016 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3017 | } |
| 3018 | free(tctx); |
| 3019 | |
| 3020 | /* allocate type according to the basetype */ |
| 3021 | switch (basetype) { |
| 3022 | case LY_TYPE_BINARY: |
| 3023 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3024 | break; |
| 3025 | case LY_TYPE_BITS: |
| 3026 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3027 | break; |
| 3028 | case LY_TYPE_BOOL: |
| 3029 | case LY_TYPE_EMPTY: |
| 3030 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3031 | break; |
| 3032 | case LY_TYPE_DEC64: |
| 3033 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3034 | break; |
| 3035 | case LY_TYPE_ENUM: |
| 3036 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3037 | break; |
| 3038 | case LY_TYPE_IDENT: |
| 3039 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3040 | break; |
| 3041 | case LY_TYPE_INST: |
| 3042 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3043 | break; |
| 3044 | case LY_TYPE_LEAFREF: |
| 3045 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3046 | break; |
| 3047 | case LY_TYPE_STRING: |
| 3048 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3049 | break; |
| 3050 | case LY_TYPE_UNION: |
| 3051 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3052 | break; |
| 3053 | case LY_TYPE_INT8: |
| 3054 | case LY_TYPE_UINT8: |
| 3055 | case LY_TYPE_INT16: |
| 3056 | case LY_TYPE_UINT16: |
| 3057 | case LY_TYPE_INT32: |
| 3058 | case LY_TYPE_UINT32: |
| 3059 | case LY_TYPE_INT64: |
| 3060 | case LY_TYPE_UINT64: |
| 3061 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3062 | break; |
| 3063 | case LY_TYPE_UNKNOWN: |
| 3064 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3065 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3066 | ret = LY_EVALID; |
| 3067 | goto cleanup; |
| 3068 | } |
| 3069 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3070 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3071 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3072 | ly_data_type2str[basetype]); |
| 3073 | free(*type); |
| 3074 | (*type) = NULL; |
| 3075 | ret = LY_EVALID; |
| 3076 | goto cleanup; |
| 3077 | } |
| 3078 | |
| 3079 | /* get restrictions from the referred typedefs */ |
| 3080 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3081 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3082 | |
| 3083 | /* remember the typedef context for circular check */ |
| 3084 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3085 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3086 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3087 | base = tctx->tpdf->type.compiled; |
| 3088 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3089 | } 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] | 3090 | /* no change, just use the type information from the base */ |
| 3091 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3092 | ++base->refcount; |
| 3093 | continue; |
| 3094 | } |
| 3095 | |
| 3096 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3097 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3098 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3099 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3100 | ret = LY_EVALID; |
| 3101 | goto cleanup; |
| 3102 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3103 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3104 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3105 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3106 | ret = LY_EVALID; |
| 3107 | goto cleanup; |
| 3108 | } |
| 3109 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3110 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3111 | /* TODO user type plugins */ |
| 3112 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3113 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3114 | 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] | 3115 | 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] | 3116 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3117 | LY_CHECK_GOTO(ret, cleanup); |
| 3118 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3119 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3120 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3121 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3122 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3123 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3124 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3125 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3126 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3127 | /* TODO user type plugins */ |
| 3128 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3129 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3130 | 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] | 3131 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 90b148f | 2020-05-06 17:49:40 +0200 | [diff] [blame] | 3132 | } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3133 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3134 | free(*type); |
| 3135 | (*type) = base; |
| 3136 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3137 | } |
| 3138 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3139 | 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] | 3140 | |
| 3141 | cleanup: |
| 3142 | ly_set_erase(&tpdf_chain, free); |
| 3143 | return ret; |
| 3144 | } |
| 3145 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3146 | /** |
| 3147 | * @brief Compile status information of the given node. |
| 3148 | * |
| 3149 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3150 | * has the status correctly set during the compilation. |
| 3151 | * |
| 3152 | * @param[in] ctx Compile context |
| 3153 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3154 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3155 | * the compatibility with the parent's status value. |
| 3156 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3157 | * @return LY_ERR value. |
| 3158 | */ |
| 3159 | static LY_ERR |
| 3160 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3161 | { |
| 3162 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3163 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3164 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3165 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3166 | if ((parent_flags & 0x3) != 0x3) { |
| 3167 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3168 | * combination of bits (0x3) which marks the uses_status value */ |
| 3169 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3170 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3171 | } |
| 3172 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3173 | } else { |
| 3174 | (*node_flags) |= LYS_STATUS_CURR; |
| 3175 | } |
| 3176 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3177 | /* check status compatibility with the parent */ |
| 3178 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3179 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3180 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3181 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3182 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3183 | } else { /* LYS_STATUS_DEPRC */ |
| 3184 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3185 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3186 | } |
| 3187 | return LY_EVALID; |
| 3188 | } |
| 3189 | } |
| 3190 | return LY_SUCCESS; |
| 3191 | } |
| 3192 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3193 | /** |
| 3194 | * @brief Check uniqness of the node/action/notification name. |
| 3195 | * |
| 3196 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3197 | * structures, but they share the namespace so we need to check their name collisions. |
| 3198 | * |
| 3199 | * @param[in] ctx Compile context. |
| 3200 | * @param[in] children List (linked list) of data nodes to go through. |
| 3201 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3202 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3203 | * @param[in] name Name of the item to find in the given lists. |
| 3204 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3205 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3206 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3207 | */ |
| 3208 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3209 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions, |
| 3210 | const struct lysc_notif *notifs, const char *name, void *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3211 | { |
| 3212 | const struct lysc_node *iter; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3213 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3214 | |
| 3215 | LY_LIST_FOR(children, iter) { |
| 3216 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3217 | goto error; |
| 3218 | } |
| 3219 | } |
| 3220 | LY_ARRAY_FOR(actions, u) { |
| 3221 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3222 | goto error; |
| 3223 | } |
| 3224 | } |
| 3225 | LY_ARRAY_FOR(notifs, u) { |
| 3226 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3227 | goto error; |
| 3228 | } |
| 3229 | } |
| 3230 | return LY_SUCCESS; |
| 3231 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3232 | 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] | 3233 | return LY_EEXIST; |
| 3234 | } |
| 3235 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3236 | 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] | 3237 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3238 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3239 | * @brief Compile parsed RPC/action schema node information. |
| 3240 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3241 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3242 | * @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] | 3243 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3244 | * @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). |
| 3245 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3246 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3247 | */ |
| 3248 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3249 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3250 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3251 | { |
| 3252 | LY_ERR ret = LY_SUCCESS; |
| 3253 | struct lysp_node *child_p; |
| 3254 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3255 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3256 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3257 | lysc_update_path(ctx, parent, action_p->name); |
| 3258 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3259 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3260 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3261 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3262 | action_p->name, action)) { |
| 3263 | return LY_EVALID; |
| 3264 | } |
| 3265 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3266 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3267 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3268 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3269 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3270 | return LY_EVALID; |
| 3271 | } |
| 3272 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3273 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3274 | action->module = ctx->mod; |
| 3275 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3276 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3277 | action->sp = action_p; |
| 3278 | } |
| 3279 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3280 | |
| 3281 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3282 | * 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] | 3283 | 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] | 3284 | |
| 3285 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3286 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3287 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3288 | 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] | 3289 | 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] | 3290 | |
| 3291 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3292 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3293 | 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] | 3294 | 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] | 3295 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3296 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3297 | 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] | 3298 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3299 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3300 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3301 | |
| 3302 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3303 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3304 | 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] | 3305 | 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] | 3306 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3307 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3308 | 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] | 3309 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3310 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3311 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3312 | |
| 3313 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3314 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3315 | ly_set_add(&ctx->xpath, action, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3316 | } |
| 3317 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3318 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3319 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3320 | return ret; |
| 3321 | } |
| 3322 | |
| 3323 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3324 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3325 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3326 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3327 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3328 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3329 | * @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] | 3330 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3331 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3332 | */ |
| 3333 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3334 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3335 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3336 | { |
| 3337 | LY_ERR ret = LY_SUCCESS; |
| 3338 | struct lysp_node *child_p; |
| 3339 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3340 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3341 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3342 | lysc_update_path(ctx, parent, notif_p->name); |
| 3343 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3344 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3345 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3346 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3347 | notif_p->name, notif)) { |
| 3348 | return LY_EVALID; |
| 3349 | } |
| 3350 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3351 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3352 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3353 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3354 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3355 | return LY_EVALID; |
| 3356 | } |
| 3357 | |
| 3358 | notif->nodetype = LYS_NOTIF; |
| 3359 | notif->module = ctx->mod; |
| 3360 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3361 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3362 | notif->sp = notif_p; |
| 3363 | } |
| 3364 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3365 | |
| 3366 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3367 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3368 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3369 | |
| 3370 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3371 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3372 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3373 | 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] | 3374 | 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] | 3375 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3376 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3377 | ly_set_add(&ctx->xpath, notif, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3378 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3379 | 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] | 3380 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3381 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3382 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3383 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3384 | } |
| 3385 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3386 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3387 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3388 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3389 | return ret; |
| 3390 | } |
| 3391 | |
| 3392 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3393 | * @brief Compile parsed container node information. |
| 3394 | * @param[in] ctx Compile context |
| 3395 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3396 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3397 | * is enriched with the container-specific information. |
| 3398 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3399 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3400 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3401 | 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] | 3402 | { |
| 3403 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3404 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3405 | struct lysp_node *child_p; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3406 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3407 | LY_ERR ret = LY_SUCCESS; |
| 3408 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3409 | if (cont_p->presence) { |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3410 | /* explicit presence */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3411 | cont->flags |= LYS_PRESENCE; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3412 | } else if (cont_p->musts) { |
| 3413 | /* container with a must condition */ |
Radek Krejci | 175f25b | 2020-08-13 12:02:36 +0200 | [diff] [blame] | 3414 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its \"must\" condition.", cont_p->name); |
| 3415 | cont->flags |= LYS_PRESENCE; |
| 3416 | } else if (cont_p->when) { |
| 3417 | /* container with a when condition */ |
| 3418 | 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] | 3419 | cont->flags |= LYS_PRESENCE; |
| 3420 | } else if (cont_p->parent) { |
| 3421 | if (cont_p->parent->nodetype == LYS_CHOICE) { |
| 3422 | /* 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] | 3423 | 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] | 3424 | cont_p->name, cont_p->parent->name); |
| 3425 | cont->flags |= LYS_PRESENCE; |
| 3426 | } else if ((cont_p->parent->nodetype == LYS_CASE) |
| 3427 | && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) { |
| 3428 | /* 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] | 3429 | 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] | 3430 | cont_p->name, cont_p->parent->name); |
| 3431 | cont->flags |= LYS_PRESENCE; |
| 3432 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3433 | } |
| 3434 | |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3435 | /* more cases when the container has meaning but is kept NP for convenience: |
| 3436 | * - when condition |
| 3437 | * - direct child action/notification |
| 3438 | */ |
| 3439 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3440 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3441 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3442 | } |
| 3443 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3444 | 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] | 3445 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3446 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3447 | ly_set_add(&ctx->xpath, cont, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3448 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3449 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3450 | 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] | 3451 | |
| 3452 | done: |
| 3453 | return ret; |
| 3454 | } |
| 3455 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3456 | /* |
| 3457 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3458 | * @param[in] ctx Compile context. |
| 3459 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3460 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3461 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3462 | * @return LY_ERR value. |
| 3463 | */ |
| 3464 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3465 | lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, |
| 3466 | struct lysc_node_leaf *leaf) |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3467 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3468 | const char *dflt; |
| 3469 | struct lys_module *dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3470 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3471 | 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] | 3472 | leaf->units ? NULL : &leaf->units, &dflt, &dflt_mod)); |
| 3473 | |
| 3474 | /* store default value, if any */ |
| 3475 | if (dflt && !(leaf->flags & LYS_SET_DFLT)) { |
| 3476 | 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] | 3477 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3478 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3479 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3480 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3481 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3482 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3483 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3484 | LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) { |
| 3485 | 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] | 3486 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3487 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3488 | } |
| 3489 | } |
| 3490 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3491 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3492 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3493 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3494 | return LY_EVALID; |
| 3495 | } |
| 3496 | } |
| 3497 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3498 | return LY_SUCCESS; |
| 3499 | } |
| 3500 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3501 | /** |
| 3502 | * @brief Compile parsed leaf node information. |
| 3503 | * @param[in] ctx Compile context |
| 3504 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3505 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3506 | * is enriched with the leaf-specific information. |
| 3507 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3508 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3509 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3510 | 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] | 3511 | { |
| 3512 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3513 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 3514 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3515 | LY_ERR ret = LY_SUCCESS; |
| 3516 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3517 | 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] | 3518 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3519 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3520 | ly_set_add(&ctx->xpath, leaf, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3521 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3522 | if (leaf_p->units) { |
| 3523 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3524 | leaf->flags |= LYS_SET_UNITS; |
| 3525 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3526 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3527 | /* compile type */ |
| 3528 | LY_CHECK_RET(lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf)); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3529 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3530 | /* store/update default value */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3531 | if (leaf_p->dflt) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3532 | 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] | 3533 | leaf->flags |= LYS_SET_DFLT; |
| 3534 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3535 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3536 | done: |
| 3537 | return ret; |
| 3538 | } |
| 3539 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3540 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3541 | * @brief Compile parsed leaf-list node information. |
| 3542 | * @param[in] ctx Compile context |
| 3543 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3544 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3545 | * is enriched with the leaf-list-specific information. |
| 3546 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3547 | */ |
| 3548 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3549 | 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] | 3550 | { |
| 3551 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3552 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3553 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3554 | LY_ERR ret = LY_SUCCESS; |
| 3555 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3556 | 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] | 3557 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3558 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3559 | ly_set_add(&ctx->xpath, llist, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3560 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3561 | if (llist_p->units) { |
| 3562 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3563 | llist->flags |= LYS_SET_UNITS; |
| 3564 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3565 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3566 | /* compile type */ |
| 3567 | LY_CHECK_RET(lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf *)llist)); |
Michal Vasko | 6a044b2 | 2020-01-15 12:25:39 +0100 | [diff] [blame] | 3568 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3569 | /* store/update default values */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3570 | if (llist_p->dflts) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3571 | 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] | 3572 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3576 | if (llist->min) { |
| 3577 | llist->flags |= LYS_MAND_TRUE; |
| 3578 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3579 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3580 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3581 | done: |
| 3582 | return ret; |
| 3583 | } |
| 3584 | |
| 3585 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3586 | * @brief Compile information about list's uniques. |
| 3587 | * @param[in] ctx Compile context. |
| 3588 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3589 | * @param[in] uniques Sized array list of unique statements. |
| 3590 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3591 | * @return LY_ERR value. |
| 3592 | */ |
| 3593 | static LY_ERR |
| 3594 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3595 | { |
| 3596 | LY_ERR ret = LY_SUCCESS; |
| 3597 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3598 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3599 | const char *keystr, *delim; |
| 3600 | size_t len; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3601 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3602 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3603 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3604 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3605 | for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3606 | config = -1; |
| 3607 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3608 | keystr = uniques[v]; |
| 3609 | while (keystr) { |
| 3610 | delim = strpbrk(keystr, " \t\n"); |
| 3611 | if (delim) { |
| 3612 | len = delim - keystr; |
| 3613 | while (isspace(*delim)) { |
| 3614 | ++delim; |
| 3615 | } |
| 3616 | } else { |
| 3617 | len = strlen(keystr); |
| 3618 | } |
| 3619 | |
| 3620 | /* unique node must be present */ |
| 3621 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3622 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3623 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3624 | if (ret != LY_SUCCESS) { |
| 3625 | if (ret == LY_EDENIED) { |
| 3626 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3627 | "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] | 3628 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3629 | } |
| 3630 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3631 | } else if (flags) { |
| 3632 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3633 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3634 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3635 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3636 | } |
| 3637 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3638 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3639 | /* all referenced leafs must be of the same config type */ |
| 3640 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3641 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3642 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3643 | return LY_EVALID; |
| 3644 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3645 | config = 1; |
| 3646 | } else { /* LYS_CONFIG_R */ |
| 3647 | config = 0; |
| 3648 | } |
| 3649 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3650 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3651 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3652 | if (parent->nodetype == LYS_LIST) { |
| 3653 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3654 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3655 | return LY_EVALID; |
| 3656 | } |
| 3657 | } |
| 3658 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3659 | /* check status */ |
| 3660 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3661 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3662 | |
| 3663 | /* mark leaf as unique */ |
| 3664 | (*key)->flags |= LYS_UNIQUE; |
| 3665 | |
| 3666 | /* next unique value in line */ |
| 3667 | keystr = delim; |
| 3668 | } |
| 3669 | /* next unique definition */ |
| 3670 | } |
| 3671 | |
| 3672 | return LY_SUCCESS; |
| 3673 | } |
| 3674 | |
| 3675 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3676 | * @brief Compile parsed list node information. |
| 3677 | * @param[in] ctx Compile context |
| 3678 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3679 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3680 | * is enriched with the list-specific information. |
| 3681 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3682 | */ |
| 3683 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3684 | 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] | 3685 | { |
| 3686 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3687 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3688 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3689 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3690 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3691 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3692 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3693 | LY_ERR ret = LY_SUCCESS; |
| 3694 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3695 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3696 | if (list->min) { |
| 3697 | list->flags |= LYS_MAND_TRUE; |
| 3698 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3699 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3700 | |
| 3701 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3702 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3703 | } |
| 3704 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3705 | 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] | 3706 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3707 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3708 | ly_set_add(&ctx->xpath, list, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3709 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3710 | |
| 3711 | /* keys */ |
| 3712 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3713 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3714 | return LY_EVALID; |
| 3715 | } |
| 3716 | |
| 3717 | /* find all the keys (must be direct children) */ |
| 3718 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3719 | if (!keystr) { |
| 3720 | /* keyless list */ |
| 3721 | list->flags |= LYS_KEYLESS; |
| 3722 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3723 | while (keystr) { |
| 3724 | delim = strpbrk(keystr, " \t\n"); |
| 3725 | if (delim) { |
| 3726 | len = delim - keystr; |
| 3727 | while (isspace(*delim)) { |
| 3728 | ++delim; |
| 3729 | } |
| 3730 | } else { |
| 3731 | len = strlen(keystr); |
| 3732 | } |
| 3733 | |
| 3734 | /* key node must be present */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3735 | 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] | 3736 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3737 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3738 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3739 | return LY_EVALID; |
| 3740 | } |
| 3741 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3742 | if (key->flags & LYS_KEY) { |
| 3743 | /* the node was already marked as a key */ |
| 3744 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3745 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3746 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3747 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3748 | |
| 3749 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3750 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3751 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3752 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3753 | return LY_EVALID; |
| 3754 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3755 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3756 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3757 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3758 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3759 | "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] | 3760 | return LY_EVALID; |
| 3761 | } |
| 3762 | } else { |
| 3763 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3764 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3765 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3766 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3767 | return LY_EVALID; |
| 3768 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3769 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3770 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3771 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3772 | return LY_EVALID; |
| 3773 | } |
| 3774 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3775 | |
| 3776 | /* check status */ |
| 3777 | 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] | 3778 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3779 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3780 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3781 | if (key->dflt) { |
| 3782 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3783 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3784 | free(key->dflt); |
| 3785 | key->dflt = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3786 | } |
| 3787 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3788 | key->flags |= LYS_KEY; |
| 3789 | |
| 3790 | /* move it to the correct position */ |
| 3791 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 3792 | /* fix links in closest previous siblings of the key */ |
| 3793 | if (key->next) { |
| 3794 | key->next->prev = key->prev; |
| 3795 | } else { |
| 3796 | /* last child */ |
| 3797 | list->child->prev = key->prev; |
| 3798 | } |
| 3799 | if (key->prev->next) { |
| 3800 | key->prev->next = key->next; |
| 3801 | } |
| 3802 | /* fix links in the key */ |
| 3803 | if (prev_key) { |
| 3804 | key->prev = (struct lysc_node*)prev_key; |
| 3805 | key->next = prev_key->next; |
| 3806 | } else { |
| 3807 | key->prev = list->child->prev; |
| 3808 | key->next = list->child; |
| 3809 | } |
| 3810 | /* fix links in closes future siblings of the key */ |
| 3811 | if (prev_key) { |
| 3812 | if (prev_key->next) { |
| 3813 | prev_key->next->prev = (struct lysc_node*)key; |
| 3814 | } else { |
| 3815 | list->child->prev = (struct lysc_node*)key; |
| 3816 | } |
| 3817 | prev_key->next = (struct lysc_node*)key; |
| 3818 | } else { |
| 3819 | list->child->prev = (struct lysc_node*)key; |
| 3820 | } |
| 3821 | /* fix links in parent */ |
| 3822 | if (!key->prev->next) { |
| 3823 | list->child = (struct lysc_node*)key; |
| 3824 | } |
| 3825 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3826 | |
| 3827 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3828 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3829 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3830 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | /* uniques */ |
| 3834 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3835 | 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] | 3836 | } |
| 3837 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3838 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3839 | 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] | 3840 | |
| 3841 | done: |
| 3842 | return ret; |
| 3843 | } |
| 3844 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3845 | /** |
| 3846 | * @brief Do some checks and set the default choice's case. |
| 3847 | * |
| 3848 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3849 | * |
| 3850 | * @param[in] ctx Compile context. |
| 3851 | * @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, |
| 3852 | * not the reference to the imported module. |
| 3853 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3854 | * @return LY_ERR value. |
| 3855 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3856 | static LY_ERR |
| 3857 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3858 | { |
| 3859 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3860 | const char *prefix = NULL, *name; |
| 3861 | size_t prefix_len = 0; |
| 3862 | |
| 3863 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3864 | name = strchr(dflt, ':'); |
| 3865 | if (name) { |
| 3866 | prefix = dflt; |
| 3867 | prefix_len = name - prefix; |
| 3868 | ++name; |
| 3869 | } else { |
| 3870 | name = dflt; |
| 3871 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 3872 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3873 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3874 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3875 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3876 | prefix_len, prefix); |
| 3877 | return LY_EVALID; |
| 3878 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3879 | 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] | 3880 | if (!ch->dflt) { |
| 3881 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3882 | "Default case \"%s\" not found.", dflt); |
| 3883 | return LY_EVALID; |
| 3884 | } |
| 3885 | /* no mandatory nodes directly under the default case */ |
| 3886 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3887 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3888 | break; |
| 3889 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3890 | if (iter->flags & LYS_MAND_TRUE) { |
| 3891 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3892 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3893 | return LY_EVALID; |
| 3894 | } |
| 3895 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3896 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3897 | return LY_SUCCESS; |
| 3898 | } |
| 3899 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3900 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3901 | 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] | 3902 | { |
| 3903 | struct lys_module *mod; |
| 3904 | const char *prefix = NULL, *name; |
| 3905 | size_t prefix_len = 0; |
| 3906 | struct lysc_node_case *cs; |
| 3907 | struct lysc_node *node; |
| 3908 | |
| 3909 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3910 | name = strchr(dflt, ':'); |
| 3911 | if (name) { |
| 3912 | prefix = dflt; |
| 3913 | prefix_len = name - prefix; |
| 3914 | ++name; |
| 3915 | } else { |
| 3916 | name = dflt; |
| 3917 | } |
| 3918 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3919 | if (prefix) { |
| 3920 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3921 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3922 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3923 | "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] | 3924 | return LY_EVALID; |
| 3925 | } |
| 3926 | } else { |
| 3927 | mod = ctx->mod; |
| 3928 | } |
| 3929 | /* get the default case */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3930 | 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] | 3931 | if (!cs) { |
| 3932 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3933 | "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] | 3934 | return LY_EVALID; |
| 3935 | } |
| 3936 | |
| 3937 | /* check that there is no mandatory node */ |
| 3938 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3939 | if (node->parent != (struct lysc_node*)cs) { |
| 3940 | break; |
| 3941 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3942 | if (node->flags & LYS_MAND_TRUE) { |
| 3943 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3944 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 3945 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3946 | return LY_EVALID; |
| 3947 | } |
| 3948 | } |
| 3949 | |
| 3950 | /* set the default case in choice */ |
| 3951 | ch->dflt = cs; |
| 3952 | cs->flags |= LYS_SET_DFLT; |
| 3953 | |
| 3954 | return LY_SUCCESS; |
| 3955 | } |
| 3956 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3957 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3958 | * @brief Compile parsed choice node information. |
| 3959 | * @param[in] ctx Compile context |
| 3960 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3961 | * @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] | 3962 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3963 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3964 | */ |
| 3965 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3966 | 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] | 3967 | { |
| 3968 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 3969 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 3970 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3971 | LY_ERR ret = LY_SUCCESS; |
| 3972 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3973 | LY_LIST_FOR(ch_p->child, child_p) { |
| 3974 | if (child_p->nodetype == LYS_CASE) { |
| 3975 | LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3976 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3977 | } |
| 3978 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3979 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3980 | } |
| 3981 | } |
| 3982 | |
| 3983 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3984 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3985 | 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] | 3986 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3987 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3988 | return ret; |
| 3989 | } |
| 3990 | |
| 3991 | /** |
| 3992 | * @brief Compile parsed anydata or anyxml node information. |
| 3993 | * @param[in] ctx Compile context |
| 3994 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3995 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3996 | * is enriched with the any-specific information. |
| 3997 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3998 | */ |
| 3999 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4000 | 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] | 4001 | { |
| 4002 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4003 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4004 | unsigned int u; |
| 4005 | LY_ERR ret = LY_SUCCESS; |
| 4006 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4007 | 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] | 4008 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4009 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4010 | ly_set_add(&ctx->xpath, any, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4011 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4012 | |
| 4013 | if (any->flags & LYS_CONFIG_W) { |
Radek Krejci | 5c4ed7b | 2020-08-12 11:29:44 +0200 | [diff] [blame] | 4014 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended. %s", |
| 4015 | 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] | 4016 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4017 | done: |
| 4018 | return ret; |
| 4019 | } |
| 4020 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4021 | /** |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4022 | * @brief Connect the node into the siblings list and check its name uniqueness. Also, |
| 4023 | * keep specific order of augments targetting the same node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4024 | * |
| 4025 | * @param[in] ctx Compile context |
| 4026 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4027 | * the choice itself is expected instead of a specific case node. |
| 4028 | * @param[in] node Schema node to connect into the list. |
| 4029 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4030 | * 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] | 4031 | */ |
| 4032 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4033 | 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] | 4034 | { |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4035 | struct lysc_node **children, *start, *end; |
| 4036 | const struct lys_module *mod; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4037 | |
| 4038 | if (node->nodetype == LYS_CASE) { |
| 4039 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4040 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4041 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4042 | } |
| 4043 | if (children) { |
| 4044 | if (!(*children)) { |
| 4045 | /* first child */ |
| 4046 | *children = node; |
| 4047 | } else if (*children != node) { |
| 4048 | /* by the condition in previous branch we cover the choice/case children |
| 4049 | * - the children list is shared by the choice and the the first case, in addition |
| 4050 | * the first child of each case must be referenced from the case node. So the node is |
| 4051 | * actually always already inserted in case it is the first children - so here such |
| 4052 | * a situation actually corresponds to the first branch */ |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4053 | if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module) |
| 4054 | && (strcmp((*children)->prev->module->name, node->module->name) > 0)) { |
| 4055 | /* some augments are already connected and we are connecting new ones, |
| 4056 | * keep module name order and insert the node into the children list */ |
| 4057 | end = (*children); |
| 4058 | do { |
| 4059 | end = end->prev; |
| 4060 | mod = end->module; |
| 4061 | while (end->prev->module == mod) { |
| 4062 | end = end->prev; |
| 4063 | } |
| 4064 | } while ((end->prev->module != (*children)->module) && (end->prev->module != node->module) |
| 4065 | && (strcmp(mod->name, node->module->name) > 0)); |
| 4066 | |
| 4067 | /* we have the last existing node after our node, easily get the first before and connect it */ |
| 4068 | start = end->prev; |
| 4069 | start->next = node; |
| 4070 | node->next = end; |
| 4071 | end->prev = node; |
| 4072 | node->prev = start; |
| 4073 | } else { |
| 4074 | /* insert at the end of the parent's children list */ |
| 4075 | (*children)->prev->next = node; |
| 4076 | node->prev = (*children)->prev; |
| 4077 | (*children)->prev = node; |
| 4078 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4079 | |
| 4080 | /* check the name uniqueness */ |
| 4081 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4082 | lysc_node_notifs(parent), node->name, node)) { |
| 4083 | return LY_EEXIST; |
| 4084 | } |
| 4085 | } |
| 4086 | } |
| 4087 | return LY_SUCCESS; |
| 4088 | } |
| 4089 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4090 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4091 | * @brief Prepare the case structure in choice node for the new data node. |
| 4092 | * |
| 4093 | * 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 |
| 4094 | * created in the choice when the first child was processed. |
| 4095 | * |
| 4096 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4097 | * @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] | 4098 | * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4099 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4100 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4101 | * @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, |
| 4102 | * 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] | 4103 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4104 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4105 | lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node_choice *ch, struct lysc_node *child) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4106 | { |
| 4107 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4108 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4109 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4110 | unsigned int u; |
| 4111 | LY_ERR ret; |
| 4112 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4113 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4114 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4115 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4116 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4117 | return NULL; \ |
| 4118 | } \ |
| 4119 | } |
| 4120 | |
Radek Krejci | 3942480 | 2020-08-12 09:31:00 +0200 | [diff] [blame] | 4121 | if (node_p->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4122 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4123 | |
| 4124 | /* we have to add an implicit case node into the parent choice */ |
| 4125 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4126 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4127 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4128 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4129 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4130 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4131 | /* the case is already present since the child is not its first children */ |
| 4132 | return (struct lysc_node_case*)ch->cases->prev; |
| 4133 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4134 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4135 | |
| 4136 | /* explicit parent case is not present (this is its first child) */ |
| 4137 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4138 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4139 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4140 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4141 | cs->sp = node_p; |
| 4142 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4143 | /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */ |
Radek Krejci | bae745f | 2019-04-09 16:28:00 +0200 | [diff] [blame] | 4144 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4145 | |
| 4146 | if (node_p->when) { |
| 4147 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4148 | ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4149 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4150 | |
| 4151 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4152 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4153 | ly_set_add(&ctx->xpath, cs, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4154 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4155 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4156 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4157 | } else { |
| 4158 | LOGINT(ctx->ctx); |
| 4159 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4160 | } |
| 4161 | cs->module = ctx->mod; |
| 4162 | cs->prev = (struct lysc_node*)cs; |
| 4163 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4164 | lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4165 | cs->child = child; |
| 4166 | |
| 4167 | return cs; |
| 4168 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4169 | if (cs) { |
| 4170 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4171 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4172 | return NULL; |
| 4173 | |
| 4174 | #undef UNIQUE_CHECK |
| 4175 | } |
| 4176 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4177 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4178 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4179 | * |
| 4180 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4181 | * @param[in] node Target node where the config is supposed to be changed. |
| 4182 | * @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] | 4183 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4184 | * 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] | 4185 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4186 | * @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] | 4187 | * @return LY_ERR value. |
| 4188 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4189 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4190 | lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4191 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4192 | { |
| 4193 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4194 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4195 | |
| 4196 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4197 | /* nothing to do */ |
| 4198 | return LY_SUCCESS; |
| 4199 | } |
| 4200 | |
| 4201 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4202 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4203 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4204 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4205 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4206 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4207 | return LY_EVALID; |
| 4208 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4209 | node->flags |= LYS_SET_CONFIG; |
| 4210 | } else { |
| 4211 | if (node->flags & LYS_SET_CONFIG) { |
| 4212 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4213 | /* setting config flags, but have node with explicit config true */ |
| 4214 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4215 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4216 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4217 | return LY_EVALID; |
| 4218 | } |
| 4219 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4220 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4221 | return LY_SUCCESS; |
| 4222 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4223 | } |
| 4224 | node->flags &= ~LYS_CONFIG_MASK; |
| 4225 | node->flags |= config; |
| 4226 | |
| 4227 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4228 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4229 | 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] | 4230 | } |
| 4231 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4232 | return LY_SUCCESS; |
| 4233 | } |
| 4234 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4235 | /** |
| 4236 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4237 | * |
| 4238 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4239 | * the flag to such parents from a mandatory children. |
| 4240 | * |
| 4241 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4242 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4243 | * (mandatory children was removed). |
| 4244 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4245 | void |
| 4246 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4247 | { |
| 4248 | struct lysc_node *iter; |
| 4249 | |
| 4250 | if (add) { /* set flag */ |
| 4251 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4252 | parent = parent->parent) { |
| 4253 | parent->flags |= LYS_MAND_TRUE; |
| 4254 | } |
| 4255 | } else { /* unset flag */ |
| 4256 | for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4257 | 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] | 4258 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4259 | /* there is another mandatory node */ |
| 4260 | return; |
| 4261 | } |
| 4262 | } |
| 4263 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4264 | parent->flags &= ~LYS_MAND_TRUE; |
| 4265 | } |
| 4266 | } |
| 4267 | } |
| 4268 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4269 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4270 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4271 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4272 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4273 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4274 | */ |
| 4275 | static void |
| 4276 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4277 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4278 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4279 | size_t len; |
| 4280 | |
| 4281 | len = strlen(aug_p->nodeid); |
| 4282 | LY_ARRAY_FOR(result, v) { |
| 4283 | if (strlen(result[v]->nodeid) <= len) { |
| 4284 | continue; |
| 4285 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4286 | if (v < LY_ARRAY_COUNT(result)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4287 | /* move the rest of array */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4288 | 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] | 4289 | break; |
| 4290 | } |
| 4291 | } |
| 4292 | result[v] = aug_p; |
| 4293 | LY_ARRAY_INCREMENT(result); |
| 4294 | } |
| 4295 | |
| 4296 | /** |
| 4297 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4298 | * |
| 4299 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4300 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4301 | * |
| 4302 | * @param[in] ctx Compile context. |
| 4303 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4304 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4305 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4306 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4307 | * @return LY_ERR value. |
| 4308 | */ |
| 4309 | LY_ERR |
| 4310 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4311 | { |
| 4312 | struct lysp_augment **result = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4313 | LY_ARRAY_COUNT_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4314 | |
| 4315 | assert(augments); |
| 4316 | |
| 4317 | /* get count of the augments in module and all its submodules */ |
| 4318 | if (aug_p) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4319 | count += LY_ARRAY_COUNT(aug_p); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4320 | } |
| 4321 | LY_ARRAY_FOR(inc_p, u) { |
| 4322 | if (inc_p[u].submodule->augments) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4323 | count += LY_ARRAY_COUNT(inc_p[u].submodule->augments); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4324 | } |
| 4325 | } |
| 4326 | |
| 4327 | if (!count) { |
| 4328 | *augments = NULL; |
| 4329 | return LY_SUCCESS; |
| 4330 | } |
| 4331 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4332 | |
| 4333 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4334 | * together, so there can be even /z/y betwwen them. */ |
| 4335 | LY_ARRAY_FOR(aug_p, u) { |
| 4336 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4337 | } |
| 4338 | LY_ARRAY_FOR(inc_p, u) { |
| 4339 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4340 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4341 | } |
| 4342 | } |
| 4343 | |
| 4344 | *augments = result; |
| 4345 | return LY_SUCCESS; |
| 4346 | } |
| 4347 | |
| 4348 | /** |
| 4349 | * @brief Compile the parsed augment connecting it into its target. |
| 4350 | * |
| 4351 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4352 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4353 | * are already implemented and compiled. |
| 4354 | * |
| 4355 | * @param[in] ctx Compile context. |
| 4356 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4357 | * @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 |
| 4358 | * children in case of the augmenting uses data. |
| 4359 | * @return LY_SUCCESS on success. |
| 4360 | * @return LY_EVALID on failure. |
| 4361 | */ |
| 4362 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4363 | 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] | 4364 | { |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4365 | LY_ERR ret = LY_SUCCESS, rc; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4366 | struct lysp_node *node_p, *case_node_p; |
| 4367 | struct lysc_node *target; /* target target of the augment */ |
| 4368 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4369 | struct lysc_when **when, *when_shared; |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4370 | struct lys_module **aug_mod; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4371 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4372 | uint16_t flags = 0; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4373 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4374 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4375 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4376 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4377 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4378 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4379 | ret = lys_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] | 4380 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4381 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4382 | if (ret != LY_SUCCESS) { |
| 4383 | if (ret == LY_EDENIED) { |
| 4384 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4385 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4386 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4387 | } |
| 4388 | return LY_EVALID; |
| 4389 | } |
| 4390 | |
| 4391 | /* check for mandatory nodes |
| 4392 | * - new cases augmenting some choice can have mandatory nodes |
| 4393 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4394 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4395 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4396 | allow_mandatory = 1; |
| 4397 | } |
| 4398 | |
| 4399 | when_shared = NULL; |
| 4400 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4401 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4402 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4403 | && !((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] | 4404 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4405 | && !(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] | 4406 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4407 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4408 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4409 | return LY_EVALID; |
| 4410 | } |
| 4411 | |
| 4412 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4413 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4414 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4415 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4416 | } else { |
| 4417 | LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4418 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4419 | } |
| 4420 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4421 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4422 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4423 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4424 | * 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] | 4425 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4426 | /* the compiled node is the last child of the target (but it is a case, so we have to be careful and stop) */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4427 | 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] | 4428 | } else if (target->nodetype == LYS_CHOICE) { |
| 4429 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4430 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4431 | } else { |
| 4432 | /* the compiled node is the last child of the target */ |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4433 | node = (struct lysc_node*)lysc_node_children(target, flags); |
| 4434 | if (!node) { |
| 4435 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4436 | break; |
| 4437 | } |
| 4438 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4439 | } |
| 4440 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4441 | 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] | 4442 | node->flags &= ~LYS_MAND_TRUE; |
| 4443 | lys_compile_mandatory_parents(target, 0); |
| 4444 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4445 | "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] | 4446 | return LY_EVALID; |
| 4447 | } |
| 4448 | |
| 4449 | /* pass augment's when to all the children */ |
| 4450 | if (aug_p->when) { |
| 4451 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4452 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4453 | 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] | 4454 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4455 | |
| 4456 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4457 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4458 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4459 | } |
| 4460 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4461 | when_shared = *when; |
| 4462 | } else { |
| 4463 | ++when_shared->refcount; |
| 4464 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4465 | |
| 4466 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4467 | /* in this case check "when" again for all children because of dummy node check */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4468 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4469 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4470 | } |
| 4471 | } |
| 4472 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4473 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4474 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4475 | switch (target->nodetype) { |
| 4476 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4477 | 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] | 4478 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4479 | 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] | 4480 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4481 | break; |
| 4482 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4483 | 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] | 4484 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4485 | 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] | 4486 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4487 | break; |
| 4488 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4489 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4490 | if (aug_p->actions) { |
| 4491 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4492 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4493 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4494 | return LY_EVALID; |
| 4495 | } |
| 4496 | if (aug_p->notifs) { |
| 4497 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4498 | "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] | 4499 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4500 | return LY_EVALID; |
| 4501 | } |
| 4502 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4503 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4504 | /* add this module into the target module augmented_by, if not there already */ |
| 4505 | rc = LY_SUCCESS; |
| 4506 | LY_ARRAY_FOR(target->module->compiled->augmented_by, v) { |
| 4507 | if (target->module->compiled->augmented_by[v] == ctx->mod) { |
| 4508 | rc = LY_EEXIST; |
| 4509 | break; |
| 4510 | } |
| 4511 | } |
| 4512 | if (!rc) { |
| 4513 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error); |
| 4514 | *aug_mod = ctx->mod; |
| 4515 | } |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4516 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4517 | lysc_update_path(ctx, NULL, NULL); |
| 4518 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4519 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4520 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4521 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4522 | return ret; |
| 4523 | } |
| 4524 | |
| 4525 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4526 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4527 | * |
| 4528 | * @param[in] ctx Compile context. |
| 4529 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4530 | * @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] | 4531 | * @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] | 4532 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4533 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4534 | * 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] | 4535 | * @return LY_ERR value. |
| 4536 | */ |
| 4537 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4538 | lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, int refine_flag) |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4539 | { |
| 4540 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4541 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4542 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4543 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4544 | return LY_EVALID; |
| 4545 | } |
| 4546 | |
| 4547 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4548 | /* check if node has default value */ |
| 4549 | if (node->nodetype & LYS_LEAF) { |
| 4550 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4551 | if (refine_flag) { |
| 4552 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4553 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4554 | return LY_EVALID; |
| 4555 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4556 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4557 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice *)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4558 | if (refine_flag) { |
| 4559 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4560 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4561 | return LY_EVALID; |
| 4562 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4563 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4564 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4565 | 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] | 4566 | return LY_EVALID; |
| 4567 | } |
| 4568 | |
| 4569 | node->flags &= ~LYS_MAND_FALSE; |
| 4570 | node->flags |= LYS_MAND_TRUE; |
| 4571 | lys_compile_mandatory_parents(node->parent, 1); |
| 4572 | } else { |
| 4573 | /* make mandatory false */ |
| 4574 | node->flags &= ~LYS_MAND_TRUE; |
| 4575 | node->flags |= LYS_MAND_FALSE; |
| 4576 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4577 | } |
| 4578 | return LY_SUCCESS; |
| 4579 | } |
| 4580 | |
| 4581 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4582 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4583 | * If present, also apply uses's modificators. |
| 4584 | * |
| 4585 | * @param[in] ctx Compile context |
| 4586 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4587 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4588 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4589 | * the compile context. |
| 4590 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4591 | */ |
| 4592 | static LY_ERR |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4593 | 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] | 4594 | { |
| 4595 | struct lysp_node *node_p; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4596 | struct lysc_node *node, *child, *iter; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4597 | /* 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] | 4598 | struct lysc_node_container context_node_fake = |
| 4599 | {.nodetype = LYS_CONTAINER, |
| 4600 | .module = ctx->mod, |
| 4601 | .flags = parent ? parent->flags : 0, |
| 4602 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4603 | .prev = (struct lysc_node*)&context_node_fake, |
| 4604 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4605 | struct lysp_grp *grp = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4606 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4607 | uint32_t grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4608 | int found; |
| 4609 | const char *id, *name, *prefix; |
| 4610 | size_t prefix_len, name_len; |
| 4611 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4612 | struct lysp_refine *rfn; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4613 | LY_ERR ret = LY_EVALID; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4614 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4615 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4616 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4617 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4618 | struct lysp_augment **augments = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4619 | LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4620 | struct lysc_notif **notifs = NULL; |
| 4621 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4622 | |
| 4623 | /* search for the grouping definition */ |
| 4624 | found = 0; |
| 4625 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4626 | 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] | 4627 | if (prefix) { |
| 4628 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4629 | if (!mod) { |
| 4630 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4631 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4632 | return LY_EVALID; |
| 4633 | } |
| 4634 | } else { |
| 4635 | mod = ctx->mod_def; |
| 4636 | } |
| 4637 | if (mod == ctx->mod_def) { |
| 4638 | for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4639 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4640 | LY_ARRAY_FOR(grp, u) { |
| 4641 | if (!strcmp(grp[u].name, name)) { |
| 4642 | grp = &grp[u]; |
| 4643 | found = 1; |
| 4644 | break; |
| 4645 | } |
| 4646 | } |
| 4647 | } |
| 4648 | } |
| 4649 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4650 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4651 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4652 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4653 | for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4654 | if (!strcmp(grp[u].name, name)) { |
| 4655 | grp = &grp[u]; |
| 4656 | found = 1; |
| 4657 | } |
| 4658 | } |
| 4659 | } |
| 4660 | if (!found && mod->parsed->includes) { |
| 4661 | /* ... and all the submodules */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4662 | for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4663 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4664 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4665 | for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4666 | if (!strcmp(grp[v].name, name)) { |
| 4667 | grp = &grp[v]; |
| 4668 | found = 1; |
| 4669 | } |
| 4670 | } |
| 4671 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4672 | } |
| 4673 | } |
| 4674 | } |
| 4675 | if (!found) { |
| 4676 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4677 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4678 | return LY_EVALID; |
| 4679 | } |
| 4680 | |
| 4681 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4682 | grp_stack_count = ctx->groupings.count; |
| 4683 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4684 | if (grp_stack_count == ctx->groupings.count) { |
| 4685 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4686 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4687 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4688 | return LY_EVALID; |
| 4689 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4690 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4691 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4692 | grp->flags |= LYS_USED_GRP; |
| 4693 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4694 | |
| 4695 | /* switch context's mod_def */ |
| 4696 | mod_old = ctx->mod_def; |
| 4697 | ctx->mod_def = mod; |
| 4698 | |
| 4699 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4700 | LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), cleanup); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4701 | |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4702 | /* remember the currently last child before processing the uses - it is needed to split the siblings to corretly |
| 4703 | * applu refine and augment only to the nodes from the uses */ |
| 4704 | if (parent) { |
| 4705 | if (parent->nodetype == LYS_CASE) { |
| 4706 | child = (struct lysc_node*)lysc_node_children(parent->parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4707 | } else { |
| 4708 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4709 | } |
| 4710 | } else if (ctx->mod->compiled->data) { |
| 4711 | child = ctx->mod->compiled->data; |
| 4712 | } else { |
| 4713 | child = NULL; |
| 4714 | } |
| 4715 | /* remember the last child */ |
| 4716 | if (child) { |
| 4717 | child = child->prev; |
| 4718 | } |
| 4719 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4720 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4721 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4722 | /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4723 | LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4724 | } |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4725 | |
| 4726 | /* split the children and add the uses's data into the fake context node */ |
| 4727 | if (child) { |
| 4728 | context_node_fake.child = child->next; |
| 4729 | } else if (parent) { |
| 4730 | context_node_fake.child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4731 | } else if (ctx->mod->compiled->data) { |
| 4732 | context_node_fake.child = ctx->mod->compiled->data; |
| 4733 | } |
| 4734 | if (context_node_fake.child) { |
| 4735 | /* remember child as the last data node added by grouping to fix the list later */ |
| 4736 | child = context_node_fake.child->prev; |
| 4737 | context_node_fake.child->prev = NULL; |
| 4738 | } |
| 4739 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4740 | when_shared = NULL; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4741 | LY_LIST_FOR(context_node_fake.child, iter) { |
| 4742 | iter->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4743 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4744 | /* 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] | 4745 | if (uses_p->when) { |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4746 | LY_ARRAY_NEW_GOTO(ctx->ctx, iter->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4747 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4748 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 4749 | |
| 4750 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4751 | /* do not check "when" semantics in a grouping */ |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4752 | ly_set_add(&ctx->xpath, iter, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4753 | } |
| 4754 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4755 | when_shared = *when; |
| 4756 | } else { |
| 4757 | ++when_shared->refcount; |
| 4758 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4759 | |
| 4760 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4761 | /* in this case check "when" again for all children because of dummy node check */ |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 4762 | ly_set_add(&ctx->xpath, iter, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4763 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4764 | } |
| 4765 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4766 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4767 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4768 | /* compile actions */ |
| 4769 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4770 | if (actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4771 | actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4772 | 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] | 4773 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4774 | /* 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] | 4775 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup); |
| 4776 | LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index; |
| 4777 | 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] | 4778 | } |
| 4779 | } |
| 4780 | |
| 4781 | /* compile notifications */ |
| 4782 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4783 | if (notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4784 | notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4785 | 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] | 4786 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4787 | /* 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] | 4788 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup); |
| 4789 | LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index; |
| 4790 | 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] | 4791 | } |
| 4792 | } |
| 4793 | |
| 4794 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4795 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4796 | LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4797 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4798 | LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4799 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4800 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4801 | /* reload previous context's mod_def */ |
| 4802 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4803 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4804 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4805 | /* apply refine */ |
| 4806 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4807 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4808 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4809 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node*)&context_node_fake, ctx->mod, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4810 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4811 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4812 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4813 | |
| 4814 | /* default value */ |
| 4815 | if (rfn->dflts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4816 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4817 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4818 | "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.", |
| 4819 | lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4820 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4821 | } |
| 4822 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4823 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4824 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4825 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4826 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4827 | } |
| 4828 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4829 | /* postpone default compilation when the tree is complete */ |
| 4830 | LY_CHECK_GOTO(lysc_incomplete_leaf_dflt_add(ctx, (struct lysc_node_leaf *)node, rfn->dflts[0], |
| 4831 | ctx->mod_def), cleanup); |
| 4832 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4833 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4834 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4835 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4836 | "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4837 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4838 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4839 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4840 | /* postpone default compilation when the tree is complete */ |
| 4841 | LY_CHECK_GOTO(lysc_incomplete_llist_dflts_add(ctx, (struct lysc_node_leaflist *)node, rfn->dflts, |
| 4842 | ctx->mod_def), cleanup); |
| 4843 | node->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4844 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4845 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4846 | /* unset LYS_SET_DFLT from the current default case */ |
| 4847 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4848 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4849 | LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4850 | } |
| 4851 | } |
| 4852 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4853 | /* description */ |
| 4854 | if (rfn->dsc) { |
| 4855 | FREE_STRING(ctx->ctx, node->dsc); |
| 4856 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4857 | } |
| 4858 | |
| 4859 | /* reference */ |
| 4860 | if (rfn->ref) { |
| 4861 | FREE_STRING(ctx->ctx, node->ref); |
| 4862 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4863 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4864 | |
| 4865 | /* config */ |
| 4866 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4867 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4868 | LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4869 | } else { |
| 4870 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4871 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4872 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | /* mandatory */ |
| 4876 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4877 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4878 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4879 | |
| 4880 | /* presence */ |
| 4881 | if (rfn->presence) { |
| 4882 | if (node->nodetype != LYS_CONTAINER) { |
| 4883 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4884 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4885 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4886 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4887 | } |
| 4888 | node->flags |= LYS_PRESENCE; |
| 4889 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4890 | |
| 4891 | /* must */ |
| 4892 | if (rfn->musts) { |
| 4893 | switch (node->nodetype) { |
| 4894 | case LYS_LEAF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4895 | 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] | 4896 | break; |
| 4897 | case LYS_LEAFLIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4898 | 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] | 4899 | break; |
| 4900 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4901 | 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] | 4902 | break; |
| 4903 | case LYS_CONTAINER: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4904 | 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] | 4905 | break; |
| 4906 | case LYS_ANYXML: |
| 4907 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4908 | 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] | 4909 | break; |
| 4910 | default: |
| 4911 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4912 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 4913 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4914 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4915 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4916 | ly_set_add(&ctx->xpath, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4917 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4918 | |
| 4919 | /* min/max-elements */ |
| 4920 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4921 | switch (node->nodetype) { |
| 4922 | case LYS_LEAFLIST: |
| 4923 | if (rfn->flags & LYS_SET_MAX) { |
| 4924 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4925 | } |
| 4926 | if (rfn->flags & LYS_SET_MIN) { |
| 4927 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4928 | if (rfn->min) { |
| 4929 | node->flags |= LYS_MAND_TRUE; |
| 4930 | lys_compile_mandatory_parents(node->parent, 1); |
| 4931 | } else { |
| 4932 | node->flags &= ~LYS_MAND_TRUE; |
| 4933 | lys_compile_mandatory_parents(node->parent, 0); |
| 4934 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4935 | } |
| 4936 | break; |
| 4937 | case LYS_LIST: |
| 4938 | if (rfn->flags & LYS_SET_MAX) { |
| 4939 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4940 | } |
| 4941 | if (rfn->flags & LYS_SET_MIN) { |
| 4942 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4943 | if (rfn->min) { |
| 4944 | node->flags |= LYS_MAND_TRUE; |
| 4945 | lys_compile_mandatory_parents(node->parent, 1); |
| 4946 | } else { |
| 4947 | node->flags &= ~LYS_MAND_TRUE; |
| 4948 | lys_compile_mandatory_parents(node->parent, 0); |
| 4949 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4950 | } |
| 4951 | break; |
| 4952 | default: |
| 4953 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4954 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 4955 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4956 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4957 | } |
| 4958 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4959 | |
| 4960 | /* if-feature */ |
| 4961 | if (rfn->iffeatures) { |
| 4962 | /* 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] | 4963 | 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] | 4964 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4965 | |
| 4966 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4967 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4968 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4969 | /* 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] | 4970 | for (uint32_t i = 0; i < refined.count; ++i) { |
| 4971 | node = (struct lysc_node*)refined.objs[i]; |
| 4972 | rfn = &uses_p->refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4973 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4974 | |
| 4975 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 4976 | if ((node->flags & LYS_MAND_TRUE) && |
| 4977 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 4978 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 4979 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4980 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4981 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4982 | } |
| 4983 | |
| 4984 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4985 | if (node->nodetype == LYS_LIST) { |
| 4986 | min = ((struct lysc_node_list*)node)->min; |
| 4987 | max = ((struct lysc_node_list*)node)->max; |
| 4988 | } else { |
| 4989 | min = ((struct lysc_node_leaflist*)node)->min; |
| 4990 | max = ((struct lysc_node_leaflist*)node)->max; |
| 4991 | } |
| 4992 | if (min > max) { |
| 4993 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4994 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 4995 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4996 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4997 | } |
| 4998 | } |
| 4999 | } |
| 5000 | |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5001 | if (first_p) { |
| 5002 | *first_p = context_node_fake.child; |
| 5003 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5004 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5005 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5006 | |
| 5007 | cleanup: |
| 5008 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5009 | if (context_node_fake.child) { |
| 5010 | context_node_fake.child->prev = child; |
| 5011 | } |
| 5012 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5013 | child->parent = parent; |
| 5014 | } |
| 5015 | |
| 5016 | if (uses_p->augments || uses_p->refines) { |
| 5017 | /* 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] | 5018 | if (context_node_fake.actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5019 | 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] | 5020 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5021 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5022 | if (context_node_fake.notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5023 | 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] | 5024 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5025 | } |
| 5026 | } |
| 5027 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5028 | /* reload previous context's mod_def */ |
| 5029 | ctx->mod_def = mod_old; |
| 5030 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5031 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5032 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5033 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5034 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5035 | |
| 5036 | return ret; |
| 5037 | } |
| 5038 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5039 | static int |
| 5040 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5041 | { |
| 5042 | struct lysp_node *iter; |
| 5043 | int len = 0; |
| 5044 | |
| 5045 | *path = NULL; |
| 5046 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5047 | char *s = *path; |
| 5048 | char *id; |
| 5049 | |
| 5050 | switch (iter->nodetype) { |
| 5051 | case LYS_USES: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5052 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5053 | break; |
| 5054 | case LYS_GROUPING: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5055 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5056 | break; |
| 5057 | case LYS_AUGMENT: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5058 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5059 | break; |
| 5060 | default: |
| 5061 | id = strdup(iter->name); |
| 5062 | break; |
| 5063 | } |
| 5064 | |
| 5065 | if (!iter->parent) { |
| 5066 | /* print prefix */ |
| 5067 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5068 | } else { |
| 5069 | /* prefix is the same as in parent */ |
| 5070 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5071 | } |
| 5072 | free(s); |
| 5073 | free(id); |
| 5074 | } |
| 5075 | |
| 5076 | if (len < 0) { |
| 5077 | free(*path); |
| 5078 | *path = NULL; |
| 5079 | } else if (len == 0) { |
| 5080 | *path = strdup("/"); |
| 5081 | len = 1; |
| 5082 | } |
| 5083 | return len; |
| 5084 | } |
| 5085 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5086 | /** |
| 5087 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5088 | * |
| 5089 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5090 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5091 | */ |
| 5092 | static LY_ERR |
| 5093 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5094 | { |
| 5095 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5096 | char *path; |
| 5097 | int len; |
| 5098 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5099 | struct lysp_node_uses fake_uses = { |
| 5100 | .parent = node_p, |
| 5101 | .nodetype = LYS_USES, |
| 5102 | .flags = 0, .next = NULL, |
| 5103 | .name = grp->name, |
| 5104 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5105 | .refines = NULL, .augments = NULL |
| 5106 | }; |
| 5107 | struct lysc_node_container fake_container = { |
| 5108 | .nodetype = LYS_CONTAINER, |
| 5109 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5110 | .module = ctx->mod, |
| 5111 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5112 | .prev = (struct lysc_node*)&fake_container, |
| 5113 | .name = "fake", |
| 5114 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5115 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5116 | }; |
| 5117 | |
| 5118 | if (grp->parent) { |
| 5119 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5120 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5121 | |
| 5122 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5123 | if (len < 0) { |
| 5124 | LOGMEM(ctx->ctx); |
| 5125 | return LY_EMEM; |
| 5126 | } |
| 5127 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5128 | ctx->path_len = (uint16_t)len; |
| 5129 | free(path); |
| 5130 | |
| 5131 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5132 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5133 | 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] | 5134 | lysc_update_path(ctx, NULL, NULL); |
| 5135 | lysc_update_path(ctx, NULL, NULL); |
| 5136 | |
| 5137 | ctx->path_len = 1; |
| 5138 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5139 | |
| 5140 | /* cleanup */ |
| 5141 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5142 | |
| 5143 | return ret; |
| 5144 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5145 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5146 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5147 | * @brief Compile parsed schema node information. |
| 5148 | * @param[in] ctx Compile context |
| 5149 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5150 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5151 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5152 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5153 | * @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). |
| 5154 | * 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] | 5155 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5156 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5157 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5158 | 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] | 5159 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5160 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5161 | struct lysc_node *node = NULL; |
| 5162 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5163 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5164 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5165 | 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] | 5166 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5167 | if (node_p->nodetype != LYS_USES) { |
| 5168 | lysc_update_path(ctx, parent, node_p->name); |
| 5169 | } else { |
| 5170 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5171 | lysc_update_path(ctx, NULL, node_p->name); |
| 5172 | } |
| 5173 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5174 | switch (node_p->nodetype) { |
| 5175 | case LYS_CONTAINER: |
| 5176 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5177 | node_compile_spec = lys_compile_node_container; |
| 5178 | break; |
| 5179 | case LYS_LEAF: |
| 5180 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5181 | node_compile_spec = lys_compile_node_leaf; |
| 5182 | break; |
| 5183 | case LYS_LIST: |
| 5184 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5185 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5186 | break; |
| 5187 | case LYS_LEAFLIST: |
| 5188 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5189 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5190 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5191 | case LYS_CHOICE: |
| 5192 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5193 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5194 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5195 | case LYS_ANYXML: |
| 5196 | case LYS_ANYDATA: |
| 5197 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5198 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5199 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5200 | case LYS_USES: |
Radek Krejci | c6b4f44 | 2020-08-12 14:45:18 +0200 | [diff] [blame] | 5201 | if (parent && parent->nodetype == LYS_CHOICE) { |
| 5202 | assert(node_p->parent->nodetype == LYS_CASE); |
| 5203 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5204 | cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, NULL); |
| 5205 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
| 5206 | } |
| 5207 | |
| 5208 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, cs ? (struct lysc_node*)cs : parent, &node); |
| 5209 | if (cs) { |
| 5210 | cs->child = node; |
| 5211 | lysc_update_path(ctx, NULL, NULL); |
| 5212 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5213 | lysc_update_path(ctx, NULL, NULL); |
| 5214 | lysc_update_path(ctx, NULL, NULL); |
| 5215 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5216 | default: |
| 5217 | LOGINT(ctx->ctx); |
| 5218 | return LY_EINT; |
| 5219 | } |
| 5220 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5221 | node->nodetype = node_p->nodetype; |
| 5222 | node->module = ctx->mod; |
| 5223 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5224 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5225 | |
| 5226 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5227 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5228 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5229 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5230 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5231 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5232 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5233 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5234 | node->flags |= LYS_CONFIG_R; |
| 5235 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5236 | /* config not explicitely set, inherit it from parent */ |
| 5237 | if (parent) { |
| 5238 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5239 | } else { |
| 5240 | /* default is config true */ |
| 5241 | node->flags |= LYS_CONFIG_W; |
| 5242 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5243 | } else { |
| 5244 | /* config set explicitely */ |
| 5245 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5246 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5247 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5248 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5249 | "Configuration node cannot be child of any state data node."); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5250 | ret = LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5251 | goto error; |
| 5252 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5253 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5254 | /* *list ordering */ |
| 5255 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5256 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5257 | LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).", |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5258 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5259 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5260 | node->flags &= ~LYS_ORDBY_MASK; |
| 5261 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5262 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5263 | /* default ordering is system */ |
| 5264 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5265 | } |
| 5266 | } |
| 5267 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5268 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5269 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5270 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5271 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5272 | * the parent is choice (parent here) or some case (so we have to get its flags to check) */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5273 | 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] | 5274 | } |
| 5275 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5276 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5277 | node->sp = node_p; |
| 5278 | } |
| 5279 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5280 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5281 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5282 | if (node_p->when) { |
| 5283 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5284 | 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] | 5285 | |
| 5286 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5287 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5288 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5289 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5290 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5291 | 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] | 5292 | |
| 5293 | /* nodetype-specific part */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5294 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5295 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5296 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5297 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5298 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5299 | if (node->flags & LYS_MAND_TRUE) { |
| 5300 | lys_compile_mandatory_parents(parent, 1); |
| 5301 | } |
| 5302 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5303 | lysc_update_path(ctx, NULL, NULL); |
| 5304 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5305 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5306 | if (parent) { |
| 5307 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5308 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5309 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5310 | } else { |
| 5311 | lysc_update_path(ctx, parent, node->name); |
| 5312 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5313 | cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5314 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5315 | if (uses_status) { |
| 5316 | |
| 5317 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5318 | /* the postponed status check of the node and its real parent - in case of implicit case, |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5319 | * it directly gets the same status flags as the choice; |
| 5320 | * uses_status cannot be applied here since uses cannot be child statement of choice */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5321 | LY_CHECK_GOTO(ret = lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5322 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5323 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5324 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5325 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5326 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5327 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5328 | LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5329 | |
| 5330 | if (parent->nodetype == LYS_CHOICE) { |
| 5331 | lysc_update_path(ctx, NULL, NULL); |
| 5332 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5333 | } else { |
| 5334 | /* top-level element */ |
| 5335 | if (!ctx->mod->compiled->data) { |
| 5336 | ctx->mod->compiled->data = node; |
| 5337 | } else { |
| 5338 | /* insert at the end of the module's top-level nodes list */ |
| 5339 | ctx->mod->compiled->data->prev->next = node; |
| 5340 | node->prev = ctx->mod->compiled->data->prev; |
| 5341 | ctx->mod->compiled->data->prev = node; |
| 5342 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5343 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5344 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5345 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5346 | return LY_EVALID; |
| 5347 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5348 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5349 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5350 | |
| 5351 | return LY_SUCCESS; |
| 5352 | |
| 5353 | error: |
| 5354 | lysc_node_free(ctx->ctx, node); |
| 5355 | return ret; |
| 5356 | } |
| 5357 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5358 | static void |
| 5359 | lysc_disconnect(struct lysc_node *node) |
| 5360 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5361 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5362 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5363 | struct lysc_module *modc = node->module->compiled; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5364 | int remove_cs = 0; |
| 5365 | |
| 5366 | parent = node->parent; |
| 5367 | |
| 5368 | /* parent's first child */ |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5369 | if (parent && parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5370 | cs = (struct lysc_node_case*)node; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5371 | } else if (parent && parent->nodetype == LYS_CASE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5372 | /* disconnecting some node in a case */ |
| 5373 | cs = (struct lysc_node_case*)parent; |
| 5374 | parent = cs->parent; |
| 5375 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5376 | if (child == node) { |
| 5377 | if (cs->child == child) { |
| 5378 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5379 | /* case with a single child -> remove also the case */ |
| 5380 | child->parent = NULL; |
| 5381 | remove_cs = 1; |
| 5382 | } else { |
| 5383 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5384 | } |
| 5385 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5386 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5387 | } |
| 5388 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5389 | if (!remove_cs) { |
| 5390 | cs = NULL; |
| 5391 | } |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5392 | } else if (parent && lysc_node_children(parent, node->flags) == node) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5393 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5394 | } else if (modc->data == node) { |
| 5395 | modc->data = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5396 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5397 | if (cs) { |
| 5398 | if (remove_cs) { |
| 5399 | /* cs has only one child which is being also removed */ |
| 5400 | lysc_disconnect((struct lysc_node*)cs); |
| 5401 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5402 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5403 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5404 | /* default case removed */ |
| 5405 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5406 | } |
| 5407 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5408 | /* first case removed */ |
| 5409 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5410 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5411 | if (cs->child) { |
| 5412 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5413 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5414 | prev = cs->child->prev; |
| 5415 | } /* else all the children are under a single case */ |
| 5416 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5417 | if (child->parent != (struct lysc_node*)cs) { |
| 5418 | break; |
| 5419 | } |
| 5420 | lysc_node_free(node->module->ctx, child); |
| 5421 | } |
| 5422 | if (prev) { |
| 5423 | if (prev->next) { |
| 5424 | prev->next = child; |
| 5425 | } |
| 5426 | if (child) { |
| 5427 | child->prev = prev; |
| 5428 | } else { |
| 5429 | /* link from the first child under the cases */ |
| 5430 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5431 | } |
| 5432 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5433 | } |
| 5434 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5435 | } |
| 5436 | |
| 5437 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5438 | if (node->prev->next) { |
| 5439 | node->prev->next = node->next; |
| 5440 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5441 | if (node->next) { |
| 5442 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5443 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6b0dbc6 | 2020-08-14 23:51:43 +0200 | [diff] [blame] | 5444 | if (parent) { |
| 5445 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
| 5446 | } else { |
| 5447 | child = modc->data; |
| 5448 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5449 | if (child) { |
| 5450 | child->prev = node->prev; |
| 5451 | } |
| 5452 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5453 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5454 | } |
| 5455 | } |
| 5456 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 5457 | struct lysc_deviation { |
| 5458 | const char *nodeid; |
| 5459 | struct lysc_node *target; /* target node of the deviation */ |
| 5460 | struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */ |
| 5461 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
| 5462 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5463 | }; |
| 5464 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5465 | /* MACROS for deviates checking */ |
| 5466 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5467 | if (!(target->nodetype & (NODETYPES))) { \ |
| 5468 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(target->nodetype), DEVTYPE, PROPERTY);\ |
| 5469 | goto cleanup; \ |
| 5470 | } |
| 5471 | |
| 5472 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5473 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
| 5474 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
| 5475 | lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
| 5476 | goto cleanup; \ |
| 5477 | } |
| 5478 | |
| 5479 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5480 | if (!((TYPE)target)->MEMBER || COND) { \ |
| 5481 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
| 5482 | goto cleanup; \ |
| 5483 | } |
| 5484 | |
| 5485 | /** |
| 5486 | * @brief Apply deviate add. |
| 5487 | * |
| 5488 | * @param[in] ctx Compile context. |
| 5489 | * @param[in] target Deviation target. |
| 5490 | * @param[in] dev_flags Internal deviation flags. |
| 5491 | * @param[in] d Deviate add to apply. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5492 | * @return LY_ERR value. |
| 5493 | */ |
| 5494 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5495 | lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysc_node *target, int dev_flags, struct lysp_deviate_add *d) |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5496 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5497 | LY_ERR ret = LY_EVALID; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5498 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 5499 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5500 | LY_ARRAY_COUNT_TYPE x; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5501 | |
| 5502 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5503 | if (((TYPE)target)->MEMBER COND) { \ |
| 5504 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5505 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5506 | PROPERTY, ((TYPE)target)->MEMBER); \ |
| 5507 | goto cleanup; \ |
| 5508 | } |
| 5509 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5510 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5511 | if (((TYPE)target)->MEMBER && (COND)) { \ |
| 5512 | int dynamic_ = 0; const char *val_; \ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5513 | 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] | 5514 | ctx->mod_def, &dynamic_); \ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5515 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5516 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5517 | if (dynamic_) {free((void*)val_);} \ |
| 5518 | goto cleanup; \ |
| 5519 | } |
| 5520 | |
| 5521 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
| 5522 | if (((TYPE)target)->MEMBER && (COND)) { \ |
| 5523 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5524 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5525 | PROPERTY, ((TYPE)target)->VALUEMEMBER); \ |
| 5526 | goto cleanup; \ |
| 5527 | } |
| 5528 | |
| 5529 | /* [units-stmt] */ |
| 5530 | if (d->units) { |
| 5531 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5532 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf *, (target->flags & LYS_SET_UNITS), units, "units", units); |
| 5533 | |
| 5534 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf *)target)->units); |
| 5535 | DUP_STRING(ctx->ctx, d->units, ((struct lysc_node_leaf *)target)->units); |
| 5536 | } |
| 5537 | |
| 5538 | /* *must-stmt */ |
| 5539 | if (d->musts) { |
| 5540 | switch (target->nodetype) { |
| 5541 | case LYS_CONTAINER: |
| 5542 | case LYS_LIST: |
| 5543 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_container *)target)->musts, |
| 5544 | x, lys_compile_must, ret, cleanup); |
| 5545 | break; |
| 5546 | case LYS_LEAF: |
| 5547 | case LYS_LEAFLIST: |
| 5548 | case LYS_ANYDATA: |
| 5549 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_node_leaf *)target)->musts, |
| 5550 | x, lys_compile_must, ret, cleanup); |
| 5551 | break; |
| 5552 | case LYS_NOTIF: |
| 5553 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_notif *)target)->musts, |
| 5554 | x, lys_compile_must, ret, cleanup); |
| 5555 | break; |
| 5556 | case LYS_RPC: |
| 5557 | case LYS_ACTION: |
| 5558 | if (dev_flags & LYSC_OPT_RPC_INPUT) { |
| 5559 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->input.musts, |
| 5560 | x, lys_compile_must, ret, cleanup); |
| 5561 | break; |
| 5562 | } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) { |
| 5563 | COMPILE_ARRAY_GOTO(ctx, d->musts, ((struct lysc_action *)target)->output.musts, |
| 5564 | x, lys_compile_must, ret, cleanup); |
| 5565 | break; |
| 5566 | } |
| 5567 | /* fall through */ |
| 5568 | default: |
| 5569 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5570 | lys_nodetype2str(target->nodetype), "add", "must"); |
| 5571 | goto cleanup; |
| 5572 | } |
| 5573 | ly_set_add(&ctx->xpath, target, 0); |
| 5574 | } |
| 5575 | |
| 5576 | /* *unique-stmt */ |
| 5577 | if (d->uniques) { |
| 5578 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5579 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d->uniques, (struct lysc_node_list *)target), cleanup); |
| 5580 | } |
| 5581 | |
| 5582 | /* *default-stmt */ |
| 5583 | if (d->dflts) { |
| 5584 | switch (target->nodetype) { |
| 5585 | case LYS_LEAF: |
| 5586 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5587 | 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] | 5588 | if (leaf->dflt) { |
| 5589 | /* first, remove the default value taken from the type */ |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5590 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5591 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5592 | free(leaf->dflt); |
| 5593 | leaf->dflt = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5594 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5595 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5596 | /* store the default value in unres */ |
| 5597 | lysc_incomplete_leaf_dflt_add(ctx, leaf, d->dflts[0], ctx->mod_def); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5598 | target->flags |= LYS_SET_DFLT; |
| 5599 | break; |
| 5600 | case LYS_LEAFLIST: |
| 5601 | if (llist->dflts && !(target->flags & LYS_SET_DFLT)) { |
| 5602 | /* first, remove the default value taken from the type */ |
| 5603 | LY_ARRAY_FOR(llist->dflts, x) { |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5604 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5605 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5606 | free(llist->dflts[x]); |
| 5607 | } |
| 5608 | LY_ARRAY_FREE(llist->dflts); |
| 5609 | llist->dflts = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5610 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5611 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5612 | /* store the default values in unres */ |
| 5613 | 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] | 5614 | target->flags |= LYS_SET_DFLT; |
| 5615 | break; |
| 5616 | case LYS_CHOICE: |
| 5617 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
| 5618 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice *, 1, dflt, "default", dflt->name); |
| 5619 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5620 | * to allow making the default case even the augmented case from the deviating module */ |
| 5621 | if (lys_compile_deviation_set_choice_dflt(ctx, d->dflts[0], (struct lysc_node_choice *)target)) { |
| 5622 | goto cleanup; |
| 5623 | } |
| 5624 | break; |
| 5625 | default: |
| 5626 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5627 | lys_nodetype2str(target->nodetype), "add", "default"); |
| 5628 | goto cleanup; |
| 5629 | } |
| 5630 | } |
| 5631 | |
| 5632 | /* [config-stmt] */ |
| 5633 | if (d->flags & LYS_CONFIG_MASK) { |
| 5634 | if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 5635 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5636 | lys_nodetype2str(target->nodetype), "add", "config"); |
| 5637 | goto cleanup; |
| 5638 | } |
| 5639 | if (dev_flags) { |
| 5640 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 5641 | dev_flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
| 5642 | } |
| 5643 | if (target->flags & LYS_SET_CONFIG) { |
| 5644 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5645 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5646 | target->flags & LYS_CONFIG_W ? "true" : "false"); |
| 5647 | goto cleanup; |
| 5648 | } |
| 5649 | LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup); |
| 5650 | } |
| 5651 | |
| 5652 | /* [mandatory-stmt] */ |
| 5653 | if (d->flags & LYS_MAND_MASK) { |
| 5654 | if (target->flags & LYS_MAND_MASK) { |
| 5655 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5656 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5657 | target->flags & LYS_MAND_TRUE ? "true" : "false"); |
| 5658 | goto cleanup; |
| 5659 | } |
| 5660 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup); |
| 5661 | } |
| 5662 | |
| 5663 | /* [min-elements-stmt] */ |
| 5664 | if (d->flags & LYS_SET_MIN) { |
| 5665 | if (target->nodetype == LYS_LEAFLIST) { |
| 5666 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements"); |
| 5667 | /* change value */ |
| 5668 | ((struct lysc_node_leaflist*)target)->min = d->min; |
| 5669 | } else if (target->nodetype == LYS_LIST) { |
| 5670 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements"); |
| 5671 | /* change value */ |
| 5672 | ((struct lysc_node_list*)target)->min = d->min; |
| 5673 | } else { |
| 5674 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5675 | lys_nodetype2str(target->nodetype), "add", "min-elements"); |
| 5676 | goto cleanup; |
| 5677 | } |
| 5678 | if (d->min) { |
| 5679 | target->flags |= LYS_MAND_TRUE; |
| 5680 | } |
| 5681 | } |
| 5682 | |
| 5683 | /* [max-elements-stmt] */ |
| 5684 | if (d->flags & LYS_SET_MAX) { |
| 5685 | if (target->nodetype == LYS_LEAFLIST) { |
| 5686 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements"); |
| 5687 | /* change value */ |
| 5688 | ((struct lysc_node_leaflist*)target)->max = d->max ? d->max : (uint32_t)-1; |
| 5689 | } else if (target->nodetype == LYS_LIST) { |
| 5690 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements"); |
| 5691 | /* change value */ |
| 5692 | ((struct lysc_node_list*)target)->max = d->max ? d->max : (uint32_t)-1; |
| 5693 | } else { |
| 5694 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5695 | lys_nodetype2str(target->nodetype), "add", "max-elements"); |
| 5696 | goto cleanup; |
| 5697 | } |
| 5698 | } |
| 5699 | |
| 5700 | ret = LY_SUCCESS; |
| 5701 | |
| 5702 | cleanup: |
| 5703 | return ret; |
| 5704 | } |
| 5705 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5706 | static LY_ERR |
| 5707 | lys_apply_deviate_delete_leaf_dflt(struct lysc_ctx *ctx, struct lysc_node *target, const char *dflt) |
| 5708 | { |
| 5709 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 5710 | int dyn = 0; |
| 5711 | const char *orig_dflt; |
| 5712 | uint32_t i; |
| 5713 | |
| 5714 | if (target->module != ctx->mod) { |
| 5715 | /* foreign deviation */ |
| 5716 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "deleting", "default", dflt); |
| 5717 | |
| 5718 | /* check that the value matches */ |
| 5719 | orig_dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_SCHEMA, ctx->mod_def, &dyn); |
| 5720 | if (strcmp(orig_dflt, dflt)) { |
| 5721 | goto error; |
| 5722 | } |
| 5723 | if (dyn) { |
| 5724 | free((char *)orig_dflt); |
| 5725 | } |
| 5726 | |
| 5727 | /* remove the default specification */ |
| 5728 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5729 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5730 | free(leaf->dflt); |
| 5731 | leaf->dflt = NULL; |
| 5732 | } else { |
| 5733 | /* local deviation */ |
| 5734 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "deleting", "default", dflt); |
| 5735 | |
| 5736 | /* find the incomplete default */ |
| 5737 | orig_dflt = NULL; |
| 5738 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 5739 | if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->leaf == leaf) { |
| 5740 | orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt; |
| 5741 | break; |
| 5742 | } |
| 5743 | } |
| 5744 | LY_CHECK_ERR_RET(!orig_dflt, LOGINT(ctx->ctx), LY_EINT); |
| 5745 | |
| 5746 | /* check that the value matches */ |
| 5747 | if (strcmp(orig_dflt, dflt)) { |
| 5748 | goto error; |
| 5749 | } |
| 5750 | |
| 5751 | /* update the list of incomplete default values */ |
| 5752 | lysc_incomplete_dflt_remove(ctx, target); |
| 5753 | } |
| 5754 | |
| 5755 | target->flags &= ~LYS_SET_DFLT; |
| 5756 | return LY_SUCCESS; |
| 5757 | |
| 5758 | error: |
| 5759 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5760 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 5761 | dflt, orig_dflt); |
| 5762 | if (dyn) { |
| 5763 | free((char *)orig_dflt); |
| 5764 | } |
| 5765 | cleanup: |
| 5766 | return LY_EVALID; |
| 5767 | } |
| 5768 | |
| 5769 | static LY_ERR |
| 5770 | lys_apply_deviate_delete_llist_dflts(struct lysc_ctx *ctx, struct lysc_node *target, const char **dflts) |
| 5771 | { |
| 5772 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 5773 | int dyn = 0, found; |
| 5774 | const char *orig_dflt, **orig_dflts; |
| 5775 | uint32_t i; |
| 5776 | LY_ARRAY_COUNT_TYPE x, y; |
| 5777 | |
| 5778 | if (target->module != ctx->mod) { |
| 5779 | /* foreign deviation */ |
| 5780 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist *, 0, dflts, "deleting", "default", dflts[0]); |
| 5781 | LY_ARRAY_FOR(dflts, x) { |
| 5782 | found = 0; |
| 5783 | LY_ARRAY_FOR(llist->dflts, y) { |
| 5784 | orig_dflt = llist->type->plugin->print(llist->dflts[y], LY_PREF_SCHEMA, ctx->mod_def, &dyn); |
| 5785 | if (!strcmp(orig_dflt, dflts[x])) { |
| 5786 | if (dyn) { |
| 5787 | free((char *)orig_dflt); |
| 5788 | } |
| 5789 | found = 1; |
| 5790 | break; |
| 5791 | } |
| 5792 | if (dyn) { |
| 5793 | free((char *)orig_dflt); |
| 5794 | } |
| 5795 | } |
| 5796 | if (!found) { |
| 5797 | goto error; |
| 5798 | } |
| 5799 | |
| 5800 | /* update compiled default values */ |
| 5801 | LY_ARRAY_DECREMENT(llist->dflts); |
| 5802 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 5803 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 5804 | free(llist->dflts[y]); |
| 5805 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts)); |
| 5806 | } |
| 5807 | if (!LY_ARRAY_COUNT(llist->dflts)) { |
| 5808 | LY_ARRAY_FREE(llist->dflts); |
| 5809 | llist->dflts = NULL; |
| 5810 | llist->flags &= ~LYS_SET_DFLT; |
| 5811 | } |
| 5812 | } else { |
| 5813 | /* local deviation */ |
| 5814 | orig_dflt = NULL; |
| 5815 | orig_dflts = NULL; |
| 5816 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 5817 | if (((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->llist == llist) { |
| 5818 | orig_dflt = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflt; |
| 5819 | orig_dflts = ((struct lysc_incomplete_dflt *)ctx->dflts.objs[i])->dflts; |
| 5820 | break; |
| 5821 | } |
| 5822 | } |
| 5823 | LY_CHECK_ERR_RET(!orig_dflt && !orig_dflts, LOGINT(ctx->ctx), LY_EINT); |
| 5824 | |
| 5825 | if (orig_dflts && (LY_ARRAY_COUNT(orig_dflts) > 1)) { |
| 5826 | /* TODO it is not currently possible to remove just one default value from incomplete defaults array */ |
| 5827 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5828 | "Local deviation deleting leaf-list defaults is not supported."); |
| 5829 | return LY_EVALID; |
| 5830 | } |
| 5831 | |
| 5832 | LY_ARRAY_FOR(dflts, x) { |
| 5833 | found = 0; |
| 5834 | if (orig_dflts) { |
| 5835 | LY_ARRAY_FOR(orig_dflts, y) { |
| 5836 | if (!strcmp(orig_dflts[y], dflts[x])) { |
| 5837 | found = 1; |
| 5838 | break; |
| 5839 | } |
| 5840 | } |
| 5841 | } else if (!strcmp(orig_dflt, dflts[x])) { |
| 5842 | found = 1; |
| 5843 | } |
| 5844 | if (!found) { |
| 5845 | goto error; |
| 5846 | } |
| 5847 | |
| 5848 | /* update the list of incomplete default values */ |
| 5849 | lysc_incomplete_dflt_remove(ctx, target); |
| 5850 | } |
| 5851 | |
| 5852 | llist->flags &= ~LYS_SET_DFLT; |
| 5853 | } |
| 5854 | |
| 5855 | return LY_SUCCESS; |
| 5856 | |
| 5857 | error: |
| 5858 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 5859 | "which does not match any of the target's property values.", dflts[x]); |
| 5860 | cleanup: |
| 5861 | return LY_EVALID; |
| 5862 | } |
| 5863 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5864 | /** |
| 5865 | * @brief Apply deviate delete. |
| 5866 | * |
| 5867 | * @param[in] ctx Compile context. |
| 5868 | * @param[in] target Deviation target. |
| 5869 | * @param[in] dev_flags Internal deviation flags. |
| 5870 | * @param[in] d Deviate delete to apply. |
| 5871 | * @return LY_ERR value. |
| 5872 | */ |
| 5873 | static LY_ERR |
| 5874 | lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysc_node *target, int dev_flags, struct lysp_deviate_del *d) |
| 5875 | { |
| 5876 | LY_ERR ret = LY_EVALID; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5877 | struct lysc_node_list *list = (struct lysc_node_list *)target; |
| 5878 | LY_ARRAY_COUNT_TYPE x, y, z; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5879 | size_t prefix_len, name_len; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5880 | const char *prefix, *name, *nodeid; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 5881 | struct lys_module *mod; |
| 5882 | |
| 5883 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5884 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d->ARRAY_DEV[0]VALMEMBER); \ |
| 5885 | LY_ARRAY_FOR(d->ARRAY_DEV, x) { \ |
| 5886 | LY_ARRAY_FOR(((TYPE)target)->ARRAY_TRG, y) { \ |
| 5887 | if (!strcmp(((TYPE)target)->ARRAY_TRG[y]VALMEMBER_CMP, d->ARRAY_DEV[x]VALMEMBER)) { break; } \ |
| 5888 | } \ |
| 5889 | if (y == LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \ |
| 5890 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5891 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5892 | PROPERTY, d->ARRAY_DEV[x]VALMEMBER); \ |
| 5893 | goto cleanup; \ |
| 5894 | } \ |
| 5895 | LY_ARRAY_DECREMENT(((TYPE)target)->ARRAY_TRG); \ |
| 5896 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)target)->ARRAY_TRG[y]); \ |
| 5897 | memmove(&((TYPE)target)->ARRAY_TRG[y], \ |
| 5898 | &((TYPE)target)->ARRAY_TRG[y + 1], \ |
| 5899 | (LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG) - y) * (sizeof *((TYPE)target)->ARRAY_TRG)); \ |
| 5900 | } \ |
| 5901 | if (!LY_ARRAY_COUNT(((TYPE)target)->ARRAY_TRG)) { \ |
| 5902 | LY_ARRAY_FREE(((TYPE)target)->ARRAY_TRG); \ |
| 5903 | ((TYPE)target)->ARRAY_TRG = NULL; \ |
| 5904 | } |
| 5905 | |
| 5906 | /* [units-stmt] */ |
| 5907 | if (d->units) { |
| 5908 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
| 5909 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d->units); |
| 5910 | if (strcmp(((struct lysc_node_leaf*)target)->units, d->units)) { |
| 5911 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5912 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 5913 | d->units, ((struct lysc_node_leaf*)target)->units); |
| 5914 | goto cleanup; |
| 5915 | } |
| 5916 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)target)->units); |
| 5917 | ((struct lysc_node_leaf*)target)->units = NULL; |
| 5918 | } |
| 5919 | |
| 5920 | /* *must-stmt */ |
| 5921 | if (d->musts) { |
| 5922 | switch (target->nodetype) { |
| 5923 | case LYS_CONTAINER: |
| 5924 | case LYS_LIST: |
| 5925 | DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5926 | break; |
| 5927 | case LYS_LEAF: |
| 5928 | case LYS_LEAFLIST: |
| 5929 | case LYS_ANYDATA: |
| 5930 | DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5931 | break; |
| 5932 | case LYS_NOTIF: |
| 5933 | DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5934 | break; |
| 5935 | case LYS_RPC: |
| 5936 | case LYS_ACTION: |
| 5937 | if (dev_flags & LYSC_OPT_RPC_INPUT) { |
| 5938 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5939 | break; |
| 5940 | } else if (dev_flags & LYSC_OPT_RPC_OUTPUT) { |
| 5941 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5942 | break; |
| 5943 | } |
| 5944 | /* fall through */ |
| 5945 | default: |
| 5946 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 5947 | lys_nodetype2str(target->nodetype), "delete", "must"); |
| 5948 | goto cleanup; |
| 5949 | } |
| 5950 | } |
| 5951 | |
| 5952 | /* *unique-stmt */ |
| 5953 | if (d->uniques) { |
| 5954 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 5955 | LY_ARRAY_FOR(d->uniques, x) { |
| 5956 | LY_ARRAY_FOR(list->uniques, z) { |
| 5957 | for (name = d->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 5958 | nodeid = strpbrk(name, " \t\n"); |
| 5959 | if (nodeid) { |
| 5960 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
| 5961 | break; |
| 5962 | } |
| 5963 | while (isspace(*nodeid)) { |
| 5964 | ++nodeid; |
| 5965 | } |
| 5966 | } else { |
| 5967 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 5968 | break; |
| 5969 | } |
| 5970 | } |
| 5971 | } |
| 5972 | if (!name) { |
| 5973 | /* complete match - remove the unique */ |
| 5974 | LY_ARRAY_DECREMENT(list->uniques); |
| 5975 | LY_ARRAY_FREE(list->uniques[z]); |
| 5976 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_COUNT(list->uniques) - z) * (sizeof *list->uniques)); |
| 5977 | --z; |
| 5978 | break; |
| 5979 | } |
| 5980 | } |
| 5981 | if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) { |
| 5982 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5983 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 5984 | d->uniques[x]); |
| 5985 | goto cleanup; |
| 5986 | } |
| 5987 | } |
| 5988 | if (!LY_ARRAY_COUNT(list->uniques)) { |
| 5989 | LY_ARRAY_FREE(list->uniques); |
| 5990 | list->uniques = NULL; |
| 5991 | } |
| 5992 | } |
| 5993 | |
| 5994 | /* *default-stmt */ |
| 5995 | if (d->dflts) { |
| 5996 | switch (target->nodetype) { |
| 5997 | case LYS_LEAF: |
| 5998 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 5999 | 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] | 6000 | break; |
| 6001 | case LYS_LEAFLIST: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6002 | 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] | 6003 | break; |
| 6004 | case LYS_CHOICE: |
| 6005 | DEV_CHECK_CARDINALITY(d->dflts, 1, "default"); |
| 6006 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d->dflts[0]); |
| 6007 | nodeid = d->dflts[0]; |
| 6008 | LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup); |
| 6009 | if (prefix) { |
| 6010 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6011 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6012 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
| 6013 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6014 | "The prefix does not match any imported module of the deviation module.", d->dflts[0]); |
| 6015 | goto cleanup; |
| 6016 | } |
| 6017 | if (mod != ((struct lysc_node_choice*)target)->dflt->module) { |
| 6018 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
| 6019 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6020 | "The prefix does not match the default case's module.", d->dflts[0]); |
| 6021 | goto cleanup; |
| 6022 | } |
| 6023 | } |
| 6024 | /* else { |
| 6025 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6026 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6027 | * the module of the default case, just matching its name */ |
| 6028 | if (strcmp(name, ((struct lysc_node_choice*)target)->dflt->name)) { |
| 6029 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6030 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6031 | d->dflts[0], ((struct lysc_node_choice*)target)->dflt->name); |
| 6032 | goto cleanup; |
| 6033 | } |
| 6034 | ((struct lysc_node_choice*)target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6035 | ((struct lysc_node_choice*)target)->dflt = NULL; |
| 6036 | break; |
| 6037 | default: |
| 6038 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6039 | lys_nodetype2str(target->nodetype), "delete", "default"); |
| 6040 | goto cleanup; |
| 6041 | } |
| 6042 | } |
| 6043 | |
| 6044 | ret = LY_SUCCESS; |
| 6045 | |
| 6046 | cleanup: |
| 6047 | return ret; |
| 6048 | } |
| 6049 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6050 | static LY_ERR |
| 6051 | lys_apply_deviate_replace_dflt_recompile(struct lysc_ctx *ctx, struct lysc_node *target) |
| 6052 | { |
| 6053 | LY_ERR ret; |
| 6054 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 6055 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 6056 | struct ly_err_item *err = NULL; |
| 6057 | LY_ARRAY_COUNT_TYPE x; |
| 6058 | const char *dflt; |
| 6059 | int dyn; |
| 6060 | |
| 6061 | if (target->module != ctx->mod) { |
| 6062 | /* foreign deviation */ |
| 6063 | if (target->nodetype == LYS_LEAF) { |
| 6064 | dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LY_PREF_JSON, NULL, &dyn); |
| 6065 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6066 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6067 | |
| 6068 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 6069 | LY_PREF_JSON, NULL, target, NULL, leaf->dflt, &err); |
| 6070 | if (dyn) { |
| 6071 | free((char *)dflt); |
| 6072 | } |
| 6073 | if (err) { |
| 6074 | ly_err_print(err); |
| 6075 | ctx->path[0] = '\0'; |
| 6076 | lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6077 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6078 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6079 | ly_err_free(err); |
| 6080 | } |
| 6081 | LY_CHECK_RET(ret); |
| 6082 | |
| 6083 | ++leaf->dflt->realtype->refcount; |
| 6084 | } else { /* LY_LEAFLIST */ |
| 6085 | LY_ARRAY_FOR(llist->dflts, x) { |
| 6086 | dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LY_PREF_JSON, NULL, &dyn); |
| 6087 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6088 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6089 | |
| 6090 | ret = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 6091 | LY_PREF_JSON, NULL, target, NULL, llist->dflts[x], &err); |
| 6092 | if (dyn) { |
| 6093 | free((char *)dflt); |
| 6094 | } |
| 6095 | if (err) { |
| 6096 | ly_err_print(err); |
| 6097 | ctx->path[0] = '\0'; |
| 6098 | lysc_path(target, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6099 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6100 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6101 | ly_err_free(err); |
| 6102 | } |
| 6103 | LY_CHECK_RET(ret); |
| 6104 | |
| 6105 | ++llist->dflts[x]->realtype->refcount; |
| 6106 | } |
| 6107 | } |
| 6108 | } else { |
| 6109 | /* local deviation */ |
| 6110 | |
| 6111 | /* these default were not compiled yet, so they will use the new type automatically */ |
| 6112 | } |
| 6113 | |
| 6114 | return LY_SUCCESS; |
| 6115 | } |
| 6116 | |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6117 | /** |
| 6118 | * @brief Apply deviate replace. |
| 6119 | * |
| 6120 | * @param[in] ctx Compile context. |
| 6121 | * @param[in] target Deviation target. |
| 6122 | * @param[in] d Deviate replace to apply. |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6123 | * @return LY_ERR value. |
| 6124 | */ |
| 6125 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6126 | 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] | 6127 | { |
| 6128 | LY_ERR ret = LY_EVALID; |
| 6129 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)target; |
| 6130 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)target; |
| 6131 | LY_ARRAY_COUNT_TYPE x; |
| 6132 | |
| 6133 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 6134 | if (!(((TYPE)target)->MEMBER COND)) { \ |
| 6135 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 6136 | "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d->MEMBER); \ |
| 6137 | goto cleanup; \ |
| 6138 | } |
| 6139 | |
| 6140 | /* [type-stmt] */ |
| 6141 | if (d->type) { |
| 6142 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6143 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6144 | lysc_type_free(ctx->ctx, ((struct lysc_node_leaf *)target)->type); |
| 6145 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6146 | /* remove only default value inherited from the type */ |
| 6147 | if (!(target->flags & LYS_SET_DFLT)) { |
| 6148 | if (target->module != ctx->mod) { |
| 6149 | /* foreign deviation - the target has default from the previous type, remove it */ |
| 6150 | if (target->nodetype == LYS_LEAF) { |
| 6151 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6152 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6153 | free(leaf->dflt); |
| 6154 | leaf->dflt = NULL; |
| 6155 | } else { /* LYS_LEAFLIST */ |
| 6156 | LY_ARRAY_FOR(llist->dflts, x) { |
| 6157 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6158 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6159 | free(llist->dflts[x]); |
| 6160 | } |
| 6161 | LY_ARRAY_FREE(llist->dflts); |
| 6162 | llist->dflts = NULL; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6163 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6164 | } else { |
| 6165 | /* local deviation */ |
| 6166 | lysc_incomplete_dflt_remove(ctx, target); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6167 | } |
| 6168 | } |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6169 | |
| 6170 | LY_CHECK_RET(lys_compile_node_type(ctx, NULL, d->type, leaf)); |
| 6171 | |
| 6172 | if (target->flags & LYS_SET_DFLT) { |
| 6173 | /* the term default value(s) needs to be recompiled */ |
| 6174 | LY_CHECK_RET(lys_apply_deviate_replace_dflt_recompile(ctx, target)); |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6175 | } |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6176 | } |
| 6177 | |
| 6178 | /* [units-stmt] */ |
| 6179 | if (d->units) { |
| 6180 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6181 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_UNITS), |
| 6182 | units, "replacing", "units", d->units); |
| 6183 | |
| 6184 | lydict_remove(ctx->ctx, leaf->units); |
| 6185 | DUP_STRING(ctx->ctx, d->units, leaf->units); |
| 6186 | } |
| 6187 | |
| 6188 | /* [default-stmt] */ |
| 6189 | if (d->dflt) { |
| 6190 | switch (target->nodetype) { |
| 6191 | case LYS_LEAF: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6192 | if (target->module != ctx->mod) { |
| 6193 | /* foreign deviation */ |
| 6194 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), dflt, "replacing", |
| 6195 | "default", d->dflt); |
| 6196 | |
| 6197 | /* remove the default specification */ |
| 6198 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6199 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6200 | free(leaf->dflt); |
| 6201 | leaf->dflt = NULL; |
| 6202 | } else { |
| 6203 | /* local deviation */ |
| 6204 | DEV_CHECK_PRESENCE(struct lysc_node_leaf *, !(target->flags & LYS_SET_DFLT), name, "replacing", |
| 6205 | "default", d->dflt); |
| 6206 | assert(!leaf->dflt); |
| 6207 | } |
| 6208 | |
| 6209 | /* store the new default value */ |
| 6210 | 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] | 6211 | break; |
| 6212 | case LYS_CHOICE: |
| 6213 | DEV_CHECK_PRESENCE(struct lysc_node_choice *, 0, dflt, "replacing", "default", d->dflt); |
| 6214 | if (lys_compile_deviation_set_choice_dflt(ctx, d->dflt, (struct lysc_node_choice *)target)) { |
| 6215 | goto cleanup; |
| 6216 | } |
| 6217 | break; |
| 6218 | default: |
| 6219 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6220 | lys_nodetype2str(target->nodetype), "replace", "default"); |
| 6221 | goto cleanup; |
| 6222 | } |
| 6223 | } |
| 6224 | |
| 6225 | /* [config-stmt] */ |
| 6226 | if (d->flags & LYS_CONFIG_MASK) { |
| 6227 | if (target->nodetype & (LYS_CASE | LYS_INOUT | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 6228 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6229 | lys_nodetype2str(target->nodetype), "replace", "config"); |
| 6230 | goto cleanup; |
| 6231 | } |
| 6232 | if (!(target->flags & LYS_SET_CONFIG)) { |
| 6233 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6234 | "replacing", "config", d->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6235 | goto cleanup; |
| 6236 | } |
| 6237 | LY_CHECK_GOTO(lys_compile_change_config(ctx, target, d->flags, 0, 0), cleanup); |
| 6238 | } |
| 6239 | |
| 6240 | /* [mandatory-stmt] */ |
| 6241 | if (d->flags & LYS_MAND_MASK) { |
| 6242 | if (!(target->flags & LYS_MAND_MASK)) { |
| 6243 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
| 6244 | "replacing", "mandatory", d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6245 | goto cleanup; |
| 6246 | } |
| 6247 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, target, d->flags, 0), cleanup); |
| 6248 | } |
| 6249 | |
| 6250 | /* [min-elements-stmt] */ |
| 6251 | if (d->flags & LYS_SET_MIN) { |
| 6252 | if (target->nodetype == LYS_LEAFLIST) { |
| 6253 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, > 0, min, "min-elements"); |
| 6254 | /* change value */ |
| 6255 | ((struct lysc_node_leaflist *)target)->min = d->min; |
| 6256 | } else if (target->nodetype == LYS_LIST) { |
| 6257 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, > 0, min, "min-elements"); |
| 6258 | /* change value */ |
| 6259 | ((struct lysc_node_list *)target)->min = d->min; |
| 6260 | } else { |
| 6261 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6262 | lys_nodetype2str(target->nodetype), "replace", "min-elements"); |
| 6263 | goto cleanup; |
| 6264 | } |
| 6265 | if (d->min) { |
| 6266 | target->flags |= LYS_MAND_TRUE; |
| 6267 | } |
| 6268 | } |
| 6269 | |
| 6270 | /* [max-elements-stmt] */ |
| 6271 | if (d->flags & LYS_SET_MAX) { |
| 6272 | if (target->nodetype == LYS_LEAFLIST) { |
| 6273 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist *, < (uint32_t)-1, max, "max-elements"); |
| 6274 | /* change value */ |
| 6275 | ((struct lysc_node_leaflist *)target)->max = d->max ? d->max : (uint32_t)-1; |
| 6276 | } else if (target->nodetype == LYS_LIST) { |
| 6277 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list *, < (uint32_t)-1, max, "max-elements"); |
| 6278 | /* change value */ |
| 6279 | ((struct lysc_node_list *)target)->max = d->max ? d->max : (uint32_t)-1; |
| 6280 | } else { |
| 6281 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
| 6282 | lys_nodetype2str(target->nodetype), "replace", "max-elements"); |
| 6283 | goto cleanup; |
| 6284 | } |
| 6285 | } |
| 6286 | |
| 6287 | ret = LY_SUCCESS; |
| 6288 | |
| 6289 | cleanup: |
| 6290 | return ret; |
| 6291 | } |
| 6292 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6293 | /** |
| 6294 | * @brief Apply all deviations of one target node. |
| 6295 | * |
| 6296 | * @param[in] ctx Compile context. |
| 6297 | * @param[in] dev Deviation structure to apply. |
| 6298 | * @return LY_ERR value. |
| 6299 | */ |
| 6300 | static LY_ERR |
| 6301 | lys_apply_deviation(struct lysc_ctx *ctx, struct lysc_deviation *dev) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6302 | { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6303 | LY_ERR ret = LY_EVALID; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6304 | struct lysc_node *target = dev->target; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6305 | struct lysc_action *rpcs; |
| 6306 | struct lysc_notif *notifs; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6307 | struct lysp_deviate *d; |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6308 | LY_ARRAY_COUNT_TYPE v, x; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6309 | uint32_t min, max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6310 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6311 | lysc_update_path(ctx, NULL, dev->nodeid); |
| 6312 | |
| 6313 | /* not-supported */ |
| 6314 | if (dev->not_supported) { |
| 6315 | if (LY_ARRAY_COUNT(dev->deviates) > 1) { |
| 6316 | LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.", |
| 6317 | LY_ARRAY_COUNT(dev->deviates), dev->nodeid); |
| 6318 | } |
| 6319 | |
| 6320 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 6321 | if (target->parent) { \ |
| 6322 | ARRAY = (TYPE*)GETFUNC(target->parent); \ |
| 6323 | } else { \ |
| 6324 | ARRAY = target->module->compiled->ARRAY; \ |
| 6325 | } \ |
| 6326 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 6327 | if (&ARRAY[x] == (TYPE*)target) { break; } \ |
| 6328 | } \ |
| 6329 | if (x < LY_ARRAY_COUNT(ARRAY)) { \ |
| 6330 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 6331 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_COUNT(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 6332 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 6333 | } |
| 6334 | |
| 6335 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 6336 | if (dev->flags & LYSC_OPT_RPC_INPUT) { |
| 6337 | /* remove RPC's/action's input */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6338 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->input); |
| 6339 | memset(&((struct lysc_action *)target)->input, 0, sizeof ((struct lysc_action *)target)->input); |
| 6340 | FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->input_exts, lysc_ext_instance_free); |
| 6341 | ((struct lysc_action *)target)->input_exts = NULL; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6342 | } else if (dev->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6343 | /* remove RPC's/action's output */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6344 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action *)target)->output); |
| 6345 | memset(&((struct lysc_action *)target)->output, 0, sizeof ((struct lysc_action *)target)->output); |
| 6346 | FREE_ARRAY(ctx->ctx, ((struct lysc_action *)target)->output_exts, lysc_ext_instance_free); |
| 6347 | ((struct lysc_action *)target)->output_exts = NULL; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6348 | } else { |
| 6349 | /* remove RPC/action */ |
| 6350 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
| 6351 | } |
| 6352 | } else if (target->nodetype == LYS_NOTIF) { |
| 6353 | /* remove Notification */ |
| 6354 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
| 6355 | } else { |
| 6356 | /* remove the target node */ |
| 6357 | lysc_disconnect(target); |
| 6358 | lysc_node_free(ctx->ctx, target); |
| 6359 | } |
| 6360 | |
| 6361 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 6362 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
| 6363 | return LY_SUCCESS; |
| 6364 | } |
| 6365 | |
| 6366 | /* list of deviates (not-supported is not present in the list) */ |
| 6367 | LY_ARRAY_FOR(dev->deviates, v) { |
| 6368 | d = dev->deviates[v]; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6369 | switch (d->mod) { |
| 6370 | case LYS_DEV_ADD: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6371 | 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] | 6372 | break; |
| 6373 | case LYS_DEV_DELETE: |
Michal Vasko | ccc062a | 2020-08-13 08:34:50 +0200 | [diff] [blame] | 6374 | 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] | 6375 | break; |
| 6376 | case LYS_DEV_REPLACE: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 6377 | 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] | 6378 | break; |
| 6379 | default: |
| 6380 | LOGINT(ctx->ctx); |
| 6381 | goto cleanup; |
| 6382 | } |
| 6383 | } |
| 6384 | |
| 6385 | /* final check when all deviations of a single target node are applied */ |
| 6386 | |
| 6387 | /* check min-max compatibility */ |
| 6388 | if (target->nodetype == LYS_LEAFLIST) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6389 | min = ((struct lysc_node_leaflist *)target)->min; |
| 6390 | max = ((struct lysc_node_leaflist *)target)->max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6391 | } else if (target->nodetype == LYS_LIST) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6392 | min = ((struct lysc_node_list *)target)->min; |
| 6393 | max = ((struct lysc_node_list *)target)->max; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6394 | } else { |
| 6395 | min = max = 0; |
| 6396 | } |
| 6397 | if (min > max) { |
| 6398 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements " |
| 6399 | "after deviation: min value %u is bigger than max value %u.", min, max); |
| 6400 | goto cleanup; |
| 6401 | } |
| 6402 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6403 | /* check mandatory - default compatibility */ |
| 6404 | if ((target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (target->flags & LYS_SET_DFLT) |
| 6405 | && (target->flags & LYS_MAND_TRUE)) { |
| 6406 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6407 | "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(target->nodetype)); |
| 6408 | goto cleanup; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6409 | } else if ((target->nodetype & LYS_CHOICE) && ((struct lysc_node_choice* )target)->dflt |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6410 | && (target->flags & LYS_MAND_TRUE)) { |
| 6411 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice."); |
| 6412 | goto cleanup; |
| 6413 | } |
| 6414 | if (target->parent && (target->parent->flags & LYS_SET_DFLT) && (target->flags & LYS_MAND_TRUE)) { |
| 6415 | /* mandatory node under a default case */ |
| 6416 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6417 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6418 | lys_nodetype2str(target->nodetype), target->name, target->parent->name); |
| 6419 | goto cleanup; |
| 6420 | } |
| 6421 | |
| 6422 | /* success */ |
| 6423 | ret = LY_SUCCESS; |
| 6424 | |
| 6425 | cleanup: |
| 6426 | lysc_update_path(ctx, NULL, NULL); |
| 6427 | return ret; |
| 6428 | } |
| 6429 | |
| 6430 | LY_ERR |
| 6431 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
| 6432 | { |
| 6433 | LY_ERR ret = LY_EVALID; |
| 6434 | struct ly_set devs_p = {0}; |
| 6435 | struct ly_set targets = {0}; |
| 6436 | struct lysc_node *target; /* target target of the deviation */ |
| 6437 | struct lysp_deviation *dev; |
| 6438 | struct lysp_deviate *d, **dp_new; |
| 6439 | LY_ARRAY_COUNT_TYPE u, v; |
| 6440 | struct lysc_deviation **devs = NULL; |
| 6441 | struct lys_module *target_mod, **dev_mod; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6442 | uint16_t flags; |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6443 | int i; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6444 | |
| 6445 | /* get all deviations from the module and all its submodules ... */ |
| 6446 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 6447 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 6448 | } |
| 6449 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 6450 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 6451 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 6452 | } |
| 6453 | } |
| 6454 | if (!devs_p.count) { |
| 6455 | /* nothing to do */ |
| 6456 | return LY_SUCCESS; |
| 6457 | } |
| 6458 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6459 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 6460 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6461 | /* ... and group them by the target node */ |
| 6462 | devs = calloc(devs_p.count, sizeof *devs); |
| 6463 | for (u = 0; u < devs_p.count; ++u) { |
| 6464 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6465 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6466 | |
| 6467 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6468 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 6469 | (const struct lysc_node**)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6470 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6471 | /* move the target pointer to input/output to make them different from the action and |
| 6472 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 6473 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 6474 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 6475 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 6476 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 6477 | flags |= LYSC_OPT_INTERNAL; |
| 6478 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 6479 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 6480 | flags |= LYSC_OPT_INTERNAL; |
| 6481 | } |
| 6482 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6483 | /* insert into the set of targets with duplicity detection */ |
| 6484 | i = ly_set_add(&targets, target, 0); |
| 6485 | if (!devs[i]) { |
| 6486 | /* new record */ |
| 6487 | devs[i] = calloc(1, sizeof **devs); |
| 6488 | devs[i]->target = target; |
| 6489 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6490 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6491 | } |
| 6492 | /* add deviates into the deviation's list of deviates */ |
| 6493 | for (d = dev->deviates; d; d = d->next) { |
| 6494 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 6495 | *dp_new = d; |
| 6496 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 6497 | devs[i]->not_supported = 1; |
| 6498 | } |
| 6499 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6500 | |
| 6501 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6502 | } |
| 6503 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6504 | /* apply deviations */ |
| 6505 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 6506 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 6507 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 6508 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6509 | 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] | 6510 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6511 | 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] | 6512 | } |
| 6513 | } |
| 6514 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6515 | /* remember target module (the target node may be removed) */ |
| 6516 | target_mod = devs[u]->target->module; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6517 | |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6518 | /* apply the deviation */ |
| 6519 | LY_CHECK_GOTO(ret = lys_apply_deviation(ctx, devs[u]), cleanup); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6520 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6521 | /* 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] | 6522 | i = 0; |
| 6523 | LY_ARRAY_FOR(target_mod->compiled->deviated_by, v) { |
| 6524 | if (target_mod->compiled->deviated_by[v] == mod_p->mod) { |
| 6525 | i = 1; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6526 | break; |
| 6527 | } |
| 6528 | } |
Michal Vasko | 1a3dc3b | 2020-08-12 15:47:42 +0200 | [diff] [blame] | 6529 | if (!i) { |
| 6530 | 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] | 6531 | *dev_mod = mod_p->mod; |
| 6532 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6533 | } |
| 6534 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6535 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6536 | ret = LY_SUCCESS; |
| 6537 | |
| 6538 | cleanup: |
| 6539 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6540 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6541 | free(devs[u]); |
| 6542 | } |
| 6543 | free(devs); |
| 6544 | ly_set_erase(&targets, NULL); |
| 6545 | ly_set_erase(&devs_p, NULL); |
| 6546 | |
| 6547 | return ret; |
| 6548 | } |
| 6549 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6550 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6551 | * @brief Compile the given YANG submodule into the main module. |
| 6552 | * @param[in] ctx Compile context |
| 6553 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6554 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6555 | */ |
| 6556 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6557 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6558 | { |
| 6559 | unsigned int u; |
| 6560 | LY_ERR ret = LY_SUCCESS; |
| 6561 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6562 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6563 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6564 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6565 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6566 | if (!mainmod->mod->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6567 | /* features are compiled directly into the compiled module structure, |
| 6568 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6569 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6570 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6571 | LY_CHECK_GOTO(ret, error); |
| 6572 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6573 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6574 | if (!mainmod->mod->dis_identities) { |
| 6575 | ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities); |
| 6576 | LY_CHECK_GOTO(ret, error); |
| 6577 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6578 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6579 | /* data nodes */ |
| 6580 | LY_LIST_FOR(submod->data, node_p) { |
| 6581 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6582 | LY_CHECK_GOTO(ret, error); |
| 6583 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6584 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6585 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6586 | 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] | 6587 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6588 | error: |
| 6589 | return ret; |
| 6590 | } |
| 6591 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6592 | static void * |
| 6593 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6594 | { |
| 6595 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6596 | if (substmts[u].stmt == stmt) { |
| 6597 | return substmts[u].storage; |
| 6598 | } |
| 6599 | } |
| 6600 | return NULL; |
| 6601 | } |
| 6602 | |
| 6603 | LY_ERR |
| 6604 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6605 | { |
| 6606 | LY_ERR ret = LY_EVALID, r; |
| 6607 | unsigned int u; |
| 6608 | struct lysp_stmt *stmt; |
| 6609 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6610 | |
| 6611 | /* check for invalid substatements */ |
| 6612 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6613 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6614 | continue; |
| 6615 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6616 | for (u = 0; substmts[u].stmt; ++u) { |
| 6617 | if (substmts[u].stmt == stmt->kw) { |
| 6618 | break; |
| 6619 | } |
| 6620 | } |
| 6621 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6622 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6623 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6624 | goto cleanup; |
| 6625 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6626 | } |
| 6627 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6628 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6629 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6630 | /* keep order of the processing the same as the order in the defined substmts, |
| 6631 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6632 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6633 | int stmt_present = 0; |
| 6634 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6635 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6636 | if (substmts[u].stmt != stmt->kw) { |
| 6637 | continue; |
| 6638 | } |
| 6639 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6640 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6641 | if (substmts[u].storage) { |
| 6642 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6643 | case LY_STMT_STATUS: |
| 6644 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6645 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6646 | break; |
| 6647 | case LY_STMT_UNITS: { |
| 6648 | const char **units; |
| 6649 | |
| 6650 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6651 | /* single item */ |
| 6652 | if (*((const char **)substmts[u].storage)) { |
| 6653 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6654 | goto cleanup; |
| 6655 | } |
| 6656 | units = (const char **)substmts[u].storage; |
| 6657 | } else { |
| 6658 | /* sized array */ |
| 6659 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6660 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6661 | } |
| 6662 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6663 | break; |
| 6664 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6665 | case LY_STMT_TYPE: { |
| 6666 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6667 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6668 | |
| 6669 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6670 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6671 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6672 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6673 | goto cleanup; |
| 6674 | } |
| 6675 | compiled = substmts[u].storage; |
| 6676 | } else { |
| 6677 | /* sized array */ |
| 6678 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6679 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6680 | compiled = (void*)type; |
| 6681 | } |
| 6682 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6683 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6684 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6685 | 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] | 6686 | 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] | 6687 | lysp_type_free(ctx->ctx, parsed); |
| 6688 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6689 | break; |
| 6690 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6691 | case LY_STMT_IF_FEATURE: { |
| 6692 | struct lysc_iffeature *iff = NULL; |
| 6693 | |
| 6694 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6695 | /* single item */ |
| 6696 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6697 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6698 | goto cleanup; |
| 6699 | } |
| 6700 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6701 | } else { |
| 6702 | /* sized array */ |
| 6703 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6704 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6705 | } |
| 6706 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6707 | break; |
| 6708 | } |
| 6709 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6710 | * 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] | 6711 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6712 | 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.", |
| 6713 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6714 | goto cleanup; |
| 6715 | } |
| 6716 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6717 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6718 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6719 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6720 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6721 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6722 | goto cleanup; |
| 6723 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6724 | } |
| 6725 | |
| 6726 | ret = LY_SUCCESS; |
| 6727 | |
| 6728 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6729 | return ret; |
| 6730 | } |
| 6731 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6732 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6733 | * @brief Check when for cyclic dependencies. |
| 6734 | * @param[in] set Set with all the referenced nodes. |
| 6735 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6736 | * @return LY_ERR value |
| 6737 | */ |
| 6738 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6739 | 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] | 6740 | { |
| 6741 | struct lyxp_set tmp_set; |
| 6742 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6743 | uint32_t i, j; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6744 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6745 | int idx; |
| 6746 | struct lysc_when *when; |
| 6747 | LY_ERR ret = LY_SUCCESS; |
| 6748 | |
| 6749 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6750 | |
| 6751 | /* prepare in_ctx of the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6752 | for ( i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6753 | xp_scnode = &set->val.scnodes[i]; |
| 6754 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6755 | if (xp_scnode->in_ctx != -1) { |
| 6756 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6757 | xp_scnode->in_ctx = 1; |
| 6758 | } |
| 6759 | } |
| 6760 | |
| 6761 | for (i = 0; i < set->used; ++i) { |
| 6762 | xp_scnode = &set->val.scnodes[i]; |
| 6763 | if (xp_scnode->in_ctx != 1) { |
| 6764 | /* already checked */ |
| 6765 | continue; |
| 6766 | } |
| 6767 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6768 | 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] | 6769 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6770 | /* no when to check */ |
| 6771 | xp_scnode->in_ctx = 0; |
| 6772 | continue; |
| 6773 | } |
| 6774 | |
| 6775 | node = xp_scnode->scnode; |
| 6776 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6777 | LY_ARRAY_FOR(node->when, u) { |
| 6778 | when = node->when[u]; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6779 | ret = lyxp_atomize(when->cond, LY_PREF_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6780 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 6781 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6782 | 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] | 6783 | goto cleanup; |
| 6784 | } |
| 6785 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6786 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6787 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6788 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6789 | /* try to find this node in our set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6790 | idx = lyxp_set_scnode_dup_node_check(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6791 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6792 | 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] | 6793 | ret = LY_EVALID; |
| 6794 | goto cleanup; |
| 6795 | } |
| 6796 | |
| 6797 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6798 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6799 | } else { |
| 6800 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6801 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6802 | } |
| 6803 | } |
| 6804 | |
| 6805 | /* merge this set into the global when set */ |
| 6806 | lyxp_set_scnode_merge(set, &tmp_set); |
| 6807 | } |
| 6808 | |
| 6809 | /* check when of non-data parents as well */ |
| 6810 | node = node->parent; |
| 6811 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 6812 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6813 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 6814 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6815 | } |
| 6816 | |
| 6817 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6818 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6819 | return ret; |
| 6820 | } |
| 6821 | |
| 6822 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6823 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 6824 | * @param[in] ctx Compile context. |
| 6825 | * @param[in] node Node to check. |
| 6826 | * @return LY_ERR value |
| 6827 | */ |
| 6828 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6829 | 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] | 6830 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6831 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6832 | uint32_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6833 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6834 | int opts, input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6835 | struct lysc_when **when = NULL; |
| 6836 | struct lysc_must *musts = NULL; |
| 6837 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6838 | const struct lysc_node *op; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6839 | |
| 6840 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6841 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6842 | if (node->flags & LYS_CONFIG_R) { |
| 6843 | for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent); |
| 6844 | if (op) { |
| 6845 | /* we are actually in output */ |
| 6846 | opts = LYXP_SCNODE_OUTPUT; |
| 6847 | } |
| 6848 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6849 | |
| 6850 | switch (node->nodetype) { |
| 6851 | case LYS_CONTAINER: |
| 6852 | when = ((struct lysc_node_container *)node)->when; |
| 6853 | musts = ((struct lysc_node_container *)node)->musts; |
| 6854 | break; |
| 6855 | case LYS_CHOICE: |
| 6856 | when = ((struct lysc_node_choice *)node)->when; |
| 6857 | break; |
| 6858 | case LYS_LEAF: |
| 6859 | when = ((struct lysc_node_leaf *)node)->when; |
| 6860 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 6861 | break; |
| 6862 | case LYS_LEAFLIST: |
| 6863 | when = ((struct lysc_node_leaflist *)node)->when; |
| 6864 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 6865 | break; |
| 6866 | case LYS_LIST: |
| 6867 | when = ((struct lysc_node_list *)node)->when; |
| 6868 | musts = ((struct lysc_node_list *)node)->musts; |
| 6869 | break; |
| 6870 | case LYS_ANYXML: |
| 6871 | case LYS_ANYDATA: |
| 6872 | when = ((struct lysc_node_anydata *)node)->when; |
| 6873 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 6874 | break; |
| 6875 | case LYS_CASE: |
| 6876 | when = ((struct lysc_node_case *)node)->when; |
| 6877 | break; |
| 6878 | case LYS_NOTIF: |
| 6879 | musts = ((struct lysc_notif *)node)->musts; |
| 6880 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6881 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6882 | case LYS_ACTION: |
| 6883 | /* first process input musts */ |
| 6884 | musts = ((struct lysc_action *)node)->input.musts; |
| 6885 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6886 | default: |
| 6887 | /* nothing to check */ |
| 6888 | break; |
| 6889 | } |
| 6890 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6891 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6892 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6893 | 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] | 6894 | 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] | 6895 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6896 | 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] | 6897 | goto cleanup; |
| 6898 | } |
| 6899 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6900 | ctx->path[0] = '\0'; |
| 6901 | 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] | 6902 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6903 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6904 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 6905 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6906 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6907 | /* 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] | 6908 | 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] | 6909 | schema->name); |
| 6910 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6911 | |
| 6912 | /* check dummy node accessing */ |
| 6913 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6914 | 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] | 6915 | ret = LY_EVALID; |
| 6916 | goto cleanup; |
| 6917 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6918 | } |
| 6919 | } |
| 6920 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6921 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6922 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6923 | LY_CHECK_GOTO(ret, cleanup); |
| 6924 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6925 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6926 | } |
| 6927 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6928 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6929 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6930 | LY_ARRAY_FOR(musts, u) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6931 | 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] | 6932 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6933 | 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] | 6934 | goto cleanup; |
| 6935 | } |
| 6936 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6937 | ctx->path[0] = '\0'; |
| 6938 | 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] | 6939 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6940 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6941 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6942 | /* 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] | 6943 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 6944 | 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] | 6945 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6946 | } |
| 6947 | } |
| 6948 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6949 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6950 | } |
| 6951 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6952 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6953 | /* now check output musts */ |
| 6954 | input_done = 1; |
| 6955 | musts = ((struct lysc_action *)node)->output.musts; |
| 6956 | opts = LYXP_SCNODE_OUTPUT; |
| 6957 | goto check_musts; |
| 6958 | } |
| 6959 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6960 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6961 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6962 | return ret; |
| 6963 | } |
| 6964 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 6965 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6966 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 6967 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6968 | const struct lysc_node *target = NULL, *siter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6969 | struct ly_path *p; |
| 6970 | struct lysc_type *type; |
| 6971 | |
| 6972 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 6973 | |
| 6974 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 6975 | LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, |
| 6976 | 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] | 6977 | LY_PREF_SCHEMA, lref->path_context, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6978 | |
| 6979 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6980 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6981 | ly_path_free(node->module->ctx, p); |
| 6982 | |
| 6983 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 6984 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6985 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 6986 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
| 6987 | return LY_EVALID; |
| 6988 | } |
| 6989 | |
| 6990 | /* check status */ |
| 6991 | ctx->path[0] = '\0'; |
| 6992 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6993 | ctx->path_len = strlen(ctx->path); |
| 6994 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 6995 | return LY_EVALID; |
| 6996 | } |
| 6997 | ctx->path_len = 1; |
| 6998 | ctx->path[1] = '\0'; |
| 6999 | |
| 7000 | /* check config */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7001 | if (lref->require_instance) { |
| 7002 | for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent); |
| 7003 | if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7004 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
| 7005 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
| 7006 | return LY_EVALID; |
| 7007 | } |
| 7008 | } |
| 7009 | |
| 7010 | /* store the target's type and check for circular chain of leafrefs */ |
| 7011 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 7012 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 7013 | if (type == (struct lysc_type *)lref) { |
| 7014 | /* circular chain detected */ |
| 7015 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7016 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
| 7017 | return LY_EVALID; |
| 7018 | } |
| 7019 | } |
| 7020 | |
| 7021 | /* check if leafref and its target are under common if-features */ |
| 7022 | if (lys_compile_leafref_features_validate(node, target)) { |
| 7023 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7024 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 7025 | " features applicable to the leafref itself.", lref->path->expr); |
| 7026 | return LY_EVALID; |
| 7027 | } |
| 7028 | |
| 7029 | return LY_SUCCESS; |
| 7030 | } |
| 7031 | |
| 7032 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7033 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7034 | { |
| 7035 | struct lysc_ext_instance *ext; |
| 7036 | struct lysp_ext_instance *ext_p = NULL; |
| 7037 | struct lysp_stmt *stmt; |
| 7038 | const struct lys_module *ext_mod; |
| 7039 | LY_ERR ret = LY_SUCCESS; |
| 7040 | |
| 7041 | /* create the parsed extension instance manually */ |
| 7042 | ext_p = calloc(1, sizeof *ext_p); |
| 7043 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 7044 | ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0); |
| 7045 | ext_p->argument = lydict_insert(ctx->ctx, "default", 0); |
| 7046 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7047 | ext_p->insubstmt_index = 0; |
| 7048 | |
| 7049 | stmt = calloc(1, sizeof *ext_p->child); |
| 7050 | stmt->stmt = lydict_insert(ctx->ctx, "type", 0); |
| 7051 | stmt->arg = lydict_insert(ctx->ctx, "boolean", 0); |
| 7052 | stmt->kw = LY_STMT_TYPE; |
| 7053 | ext_p->child = stmt; |
| 7054 | |
| 7055 | /* allocate new extension instance */ |
| 7056 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7057 | |
| 7058 | /* manually get extension definition module */ |
| 7059 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7060 | |
| 7061 | /* compile the extension instance */ |
| 7062 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7063 | |
| 7064 | cleanup: |
| 7065 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7066 | free(ext_p); |
| 7067 | return ret; |
| 7068 | } |
| 7069 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7070 | static LY_ERR |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7071 | lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_type *type, const char *dflt, |
| 7072 | const struct lys_module *dflt_mod, struct lyd_value *storage) |
| 7073 | { |
| 7074 | LY_ERR ret; |
| 7075 | struct ly_err_item *err = NULL; |
| 7076 | |
| 7077 | ret = type->plugin->store(ctx->ctx, type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA, |
| 7078 | LY_PREF_SCHEMA, (void *)dflt_mod, node, NULL, storage, &err); |
| 7079 | if (err) { |
| 7080 | ly_err_print(err); |
| 7081 | ctx->path[0] = '\0'; |
| 7082 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7083 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7084 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7085 | ly_err_free(err); |
| 7086 | } |
| 7087 | if (!ret) { |
| 7088 | ++storage->realtype->refcount; |
| 7089 | return LY_SUCCESS; |
| 7090 | } |
| 7091 | return ret; |
| 7092 | } |
| 7093 | |
| 7094 | static LY_ERR |
| 7095 | lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const char *dflt, |
| 7096 | const struct lys_module *dflt_mod) |
| 7097 | { |
| 7098 | LY_ERR ret; |
| 7099 | |
| 7100 | assert(!leaf->dflt); |
| 7101 | |
| 7102 | if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) { |
| 7103 | /* ignore default values for keys and mandatory leaves */ |
| 7104 | return LY_SUCCESS; |
| 7105 | } |
| 7106 | |
| 7107 | /* allocate the default value */ |
| 7108 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 7109 | LY_CHECK_ERR_RET(!leaf->dflt, LOGMEM(ctx->ctx), LY_EMEM); |
| 7110 | |
| 7111 | /* store the default value */ |
| 7112 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)leaf, leaf->type, dflt, dflt_mod, leaf->dflt); |
| 7113 | if (ret) { |
| 7114 | free(leaf->dflt); |
| 7115 | leaf->dflt = NULL; |
| 7116 | } |
| 7117 | |
| 7118 | return ret; |
| 7119 | } |
| 7120 | |
| 7121 | static LY_ERR |
| 7122 | lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, const char *dflt, const char **dflts, |
| 7123 | const struct lys_module *dflt_mod) |
| 7124 | { |
| 7125 | LY_ERR ret; |
| 7126 | LY_ARRAY_COUNT_TYPE orig_count, u, v; |
| 7127 | |
| 7128 | assert(dflt || dflts); |
| 7129 | |
| 7130 | if (llist->dflts) { |
| 7131 | /* there were already some defaults and we are adding new by deviations */ |
| 7132 | assert(dflts); |
| 7133 | orig_count = LY_ARRAY_COUNT(llist->dflts); |
| 7134 | } else { |
| 7135 | orig_count = 0; |
| 7136 | } |
| 7137 | |
| 7138 | /* allocate new items */ |
| 7139 | if (dflts) { |
| 7140 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM); |
| 7141 | } else { |
| 7142 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM); |
| 7143 | } |
| 7144 | |
| 7145 | /* fill each new default value */ |
| 7146 | if (dflts) { |
| 7147 | LY_ARRAY_FOR(dflts, u) { |
| 7148 | llist->dflts[orig_count + u] = calloc(1, sizeof **llist->dflts); |
| 7149 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflts[u], dflt_mod, |
| 7150 | llist->dflts[orig_count + u]); |
| 7151 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count + u]), ret); |
| 7152 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7153 | } |
| 7154 | } else { |
| 7155 | llist->dflts[orig_count] = calloc(1, sizeof **llist->dflts); |
| 7156 | ret = lys_compile_unres_dflt(ctx, (struct lysc_node *)llist, llist->type, dflt, dflt_mod, llist->dflts[orig_count]); |
| 7157 | LY_CHECK_ERR_RET(ret, free(llist->dflts[orig_count]), ret); |
| 7158 | LY_ARRAY_INCREMENT(llist->dflts); |
| 7159 | } |
| 7160 | |
| 7161 | /* check default value uniqueness */ |
| 7162 | if (llist->flags & LYS_CONFIG_W) { |
| 7163 | /* configuration data values must be unique - so check the default values */ |
| 7164 | for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) { |
| 7165 | for (v = 0; v < u; ++v) { |
| 7166 | if (!llist->dflts[u]->realtype->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 7167 | int dynamic = 0; |
| 7168 | const char *val = llist->type->plugin->print(llist->dflts[u], LY_PREF_SCHEMA, (void *)dflt_mod, &dynamic); |
| 7169 | |
| 7170 | lysc_update_path(ctx, llist->parent, llist->name); |
| 7171 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7172 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 7173 | lysc_update_path(ctx, NULL, NULL); |
| 7174 | if (dynamic) { |
| 7175 | free((char *)val); |
| 7176 | } |
| 7177 | return LY_EVALID; |
| 7178 | } |
| 7179 | } |
| 7180 | } |
| 7181 | } |
| 7182 | |
| 7183 | return LY_SUCCESS; |
| 7184 | } |
| 7185 | |
| 7186 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7187 | lys_compile_unres(struct lysc_ctx *ctx) |
| 7188 | { |
| 7189 | struct lysc_node *node; |
| 7190 | struct lysc_type *type, *typeiter; |
| 7191 | struct lysc_type_leafref *lref; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7192 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7193 | uint32_t i; |
| 7194 | |
| 7195 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7196 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7197 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 7198 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7199 | node = ctx->leafrefs.objs[i]; |
| 7200 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7201 | type = ((struct lysc_node_leaf *)node)->type; |
| 7202 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7203 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 7204 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7205 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7206 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7207 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 7208 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 7209 | } |
| 7210 | } |
| 7211 | } |
| 7212 | } |
| 7213 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7214 | /* store pointer to the real type */ |
| 7215 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 7216 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7217 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 7218 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7219 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7220 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
| 7221 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7222 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7223 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7224 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 7225 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7226 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7227 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 7228 | } |
| 7229 | } |
| 7230 | } |
| 7231 | } |
| 7232 | |
| 7233 | /* check xpath */ |
| 7234 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 7235 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 7236 | } |
| 7237 | |
| 7238 | /* finish incomplete default values compilation */ |
| 7239 | for (i = 0; i < ctx->dflts.count; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7240 | struct lysc_incomplete_dflt *r = ctx->dflts.objs[i]; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 7241 | if (r->leaf->nodetype == LYS_LEAF) { |
| 7242 | LY_CHECK_RET(lys_compile_unres_leaf_dlft(ctx, r->leaf, r->dflt, r->dflt_mod)); |
| 7243 | } else { |
| 7244 | 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] | 7245 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7246 | } |
| 7247 | |
| 7248 | return LY_SUCCESS; |
| 7249 | } |
| 7250 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7251 | LY_ERR |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7252 | lys_compile(struct lys_module **mod, int options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7253 | { |
| 7254 | struct lysc_ctx ctx = {0}; |
| 7255 | struct lysc_module *mod_c; |
| 7256 | struct lysp_module *sp; |
| 7257 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7258 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7259 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7260 | struct lys_module *m; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7261 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7262 | uint32_t i; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7263 | uint16_t compile_id; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7264 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7265 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7266 | 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] | 7267 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7268 | if (!(*mod)->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7269 | /* just imported modules are not compiled */ |
| 7270 | return LY_SUCCESS; |
| 7271 | } |
| 7272 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7273 | compile_id = ++(*mod)->ctx->module_set_id; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7274 | sp = (*mod)->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7275 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7276 | ctx.ctx = (*mod)->ctx; |
| 7277 | ctx.mod = *mod; |
| 7278 | ctx.mod_def = *mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7279 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7280 | ctx.path_len = 1; |
| 7281 | ctx.path[0] = '/'; |
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 | (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 7284 | LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM); |
| 7285 | mod_c->mod = *mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7286 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7287 | LY_ARRAY_FOR(sp->imports, u) { |
| 7288 | LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error); |
| 7289 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7290 | LY_ARRAY_FOR(sp->includes, u) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7291 | LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7292 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7293 | |
| 7294 | /* features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7295 | if ((*mod)->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7296 | /* there is already precompiled array of features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7297 | mod_c->features = (*mod)->dis_features; |
| 7298 | (*mod)->dis_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7299 | } else { |
| 7300 | /* features are compiled directly into the compiled module structure, |
| 7301 | * 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] | 7302 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7303 | LY_CHECK_GOTO(ret, error); |
| 7304 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7305 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7306 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7307 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7308 | * are present. */ |
| 7309 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7310 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7311 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7312 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7313 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7314 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7315 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7316 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7317 | 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] | 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, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7321 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7322 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7323 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7324 | /* identities, work similarly to features with the precompilation */ |
| 7325 | if ((*mod)->dis_identities) { |
| 7326 | mod_c->identities = (*mod)->dis_identities; |
| 7327 | (*mod)->dis_identities = NULL; |
| 7328 | } else { |
| 7329 | ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities); |
| 7330 | LY_CHECK_GOTO(ret, error); |
| 7331 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7332 | if (sp->identities) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7333 | 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] | 7334 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7335 | lysc_update_path(&ctx, NULL, "{submodule}"); |
| 7336 | LY_ARRAY_FOR(sp->includes, v) { |
| 7337 | if (sp->includes[v].submodule->identities) { |
| 7338 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
| 7339 | ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities); |
| 7340 | LY_CHECK_GOTO(ret, error); |
| 7341 | lysc_update_path(&ctx, NULL, NULL); |
| 7342 | } |
| 7343 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7344 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7345 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7346 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7347 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7348 | 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] | 7349 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7350 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7351 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7352 | 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] | 7353 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7354 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7355 | 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] | 7356 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7357 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7358 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7359 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7360 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7361 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7362 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7363 | /* extension instances TODO cover extension instances from submodules */ |
| 7364 | 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] | 7365 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7366 | /* finish compilation for all unresolved items in the context */ |
| 7367 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7368 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7369 | /* validate non-instantiated groupings from the parsed schema, |
| 7370 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7371 | ctx.options |= LYSC_OPT_GROUPING; |
| 7372 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7373 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7374 | LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7375 | } |
| 7376 | } |
| 7377 | LY_LIST_FOR(sp->data, node_p) { |
| 7378 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7379 | LY_ARRAY_FOR(grps, u) { |
| 7380 | if (!(grps[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, &grps[u]), error); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7382 | } |
| 7383 | } |
| 7384 | } |
| 7385 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7386 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7387 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7388 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7389 | } |
| 7390 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7391 | #if 0 |
| 7392 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7393 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7394 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7395 | * the anotation definitions available in the internal schema structure. */ |
| 7396 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7397 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7398 | lys_free(mod, NULL, 1, 1); |
| 7399 | return NULL; |
| 7400 | } |
| 7401 | } |
| 7402 | #endif |
| 7403 | |
| 7404 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7405 | if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) { |
| 7406 | 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] | 7407 | } |
| 7408 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7409 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7410 | ly_set_erase(&ctx.xpath, NULL); |
| 7411 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7412 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7413 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7414 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7415 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7416 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7417 | lysp_module_free((*mod)->parsed); |
| 7418 | (*mod)->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7419 | } |
| 7420 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7421 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7422 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7423 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7424 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7425 | if (m->implemented > 1) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7426 | m->implemented = 1; |
| 7427 | } |
| 7428 | } |
| 7429 | } |
| 7430 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7431 | return LY_SUCCESS; |
| 7432 | |
| 7433 | error: |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7434 | lys_feature_precompile_revert(&ctx, *mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7435 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7436 | ly_set_erase(&ctx.xpath, NULL); |
| 7437 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7438 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7439 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7440 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7441 | lysc_module_free(mod_c, NULL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7442 | (*mod)->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7443 | |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7444 | /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency |
| 7445 | * of this module, since this module can be also compiled from dependency, there can be some other modules being |
| 7446 | * 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] | 7447 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7448 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | a46012b | 2020-08-12 15:41:04 +0200 | [diff] [blame] | 7449 | if ((m->implemented >= compile_id) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7450 | /* revert features list to the precompiled state */ |
| 7451 | lys_feature_precompile_revert(&ctx, m); |
| 7452 | /* mark module as imported-only / not-implemented */ |
| 7453 | m->implemented = 0; |
| 7454 | /* free the compiled version of the module */ |
| 7455 | lysc_module_free(m->compiled, NULL); |
| 7456 | m->compiled = NULL; |
| 7457 | } |
| 7458 | } |
| 7459 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7460 | /* remove the module itself from the context and free it */ |
| 7461 | ly_set_rm(&ctx.ctx->list, *mod, NULL); |
| 7462 | lys_module_free(*mod, NULL); |
| 7463 | *mod = NULL; |
| 7464 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7465 | return ret; |
| 7466 | } |